1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
24 #include "linux-nat.h"
25 #include "target-descriptions.h"
28 #include "gdbthread.h"
31 #include "arm-linux-tdep.h"
33 #include <elf/common.h>
35 #include <sys/ptrace.h>
36 #include <sys/utsname.h>
37 #include <sys/procfs.h>
39 /* Prototypes for supply_gregset etc. */
42 /* Defines ps_err_e, struct ps_prochandle. */
43 #include "gdb_proc_service.h"
45 #ifndef PTRACE_GET_THREAD_AREA
46 #define PTRACE_GET_THREAD_AREA 22
49 #ifndef PTRACE_GETWMMXREGS
50 #define PTRACE_GETWMMXREGS 18
51 #define PTRACE_SETWMMXREGS 19
54 #ifndef PTRACE_GETVFPREGS
55 #define PTRACE_GETVFPREGS 27
56 #define PTRACE_SETVFPREGS 28
59 #ifndef PTRACE_GETHBPREGS
60 #define PTRACE_GETHBPREGS 29
61 #define PTRACE_SETHBPREGS 30
64 extern int arm_apcs_32;
66 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
67 case we may be tracing more than one process at a time. In that
68 case, inferior_ptid will contain the main process ID and the
69 individual thread (process) ID. get_thread_id () is used to get
70 the thread id if it's available, and the process id otherwise. */
73 get_thread_id (ptid_t ptid)
75 int tid = ptid_get_lwp (ptid);
77 tid = ptid_get_pid (ptid);
81 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
83 /* Get the value of a particular register from the floating point
84 state of the process and store it into regcache. */
87 fetch_fpregister (struct regcache *regcache, int regno)
90 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
92 /* Get the thread id for the ptrace call. */
93 tid = GET_THREAD_ID (inferior_ptid);
95 /* Read the floating point state. */
96 if (have_ptrace_getregset)
101 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
103 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
106 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
110 warning (_("Unable to fetch floating point register."));
115 if (ARM_FPS_REGNUM == regno)
116 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
117 fp + NWFPE_FPSR_OFFSET);
119 /* Fetch the floating point register. */
120 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
121 supply_nwfpe_register (regcache, regno, fp);
124 /* Get the whole floating point state of the process and store it
128 fetch_fpregs (struct regcache *regcache)
131 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
133 /* Get the thread id for the ptrace call. */
134 tid = GET_THREAD_ID (inferior_ptid);
136 /* Read the floating point state. */
137 if (have_ptrace_getregset)
142 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
144 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
147 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
151 warning (_("Unable to fetch the floating point registers."));
156 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
157 fp + NWFPE_FPSR_OFFSET);
159 /* Fetch the floating point registers. */
160 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
161 supply_nwfpe_register (regcache, regno, fp);
164 /* Save a particular register into the floating point state of the
165 process using the contents from regcache. */
168 store_fpregister (const struct regcache *regcache, int regno)
171 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
173 /* Get the thread id for the ptrace call. */
174 tid = GET_THREAD_ID (inferior_ptid);
176 /* Read the floating point state. */
177 if (have_ptrace_getregset)
182 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
184 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
187 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
191 warning (_("Unable to fetch the floating point registers."));
196 if (ARM_FPS_REGNUM == regno
197 && REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
198 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
200 /* Store the floating point register. */
201 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
202 collect_nwfpe_register (regcache, regno, fp);
204 if (have_ptrace_getregset)
209 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
211 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
214 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
218 warning (_("Unable to store floating point register."));
223 /* Save the whole floating point state of the process using
224 the contents from regcache. */
227 store_fpregs (const struct regcache *regcache)
230 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
232 /* Get the thread id for the ptrace call. */
233 tid = GET_THREAD_ID (inferior_ptid);
235 /* Read the floating point state. */
236 if (have_ptrace_getregset)
238 elf_fpregset_t fpregs;
241 iov.iov_base = &fpregs;
242 iov.iov_len = sizeof (fpregs);
244 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
247 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
251 warning (_("Unable to fetch the floating point registers."));
256 if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
257 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
259 /* Store the floating point registers. */
260 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
261 if (REG_VALID == regcache_register_status (regcache, regno))
262 collect_nwfpe_register (regcache, regno, fp);
264 if (have_ptrace_getregset)
269 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
271 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
274 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
278 warning (_("Unable to store floating point registers."));
283 /* Fetch a general register of the process and store into
287 fetch_register (struct regcache *regcache, int regno)
292 /* Get the thread id for the ptrace call. */
293 tid = GET_THREAD_ID (inferior_ptid);
295 if (have_ptrace_getregset)
299 iov.iov_base = ®s;
300 iov.iov_len = sizeof (regs);
302 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
305 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
309 warning (_("Unable to fetch general register."));
313 if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
314 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
316 if (ARM_PS_REGNUM == regno)
319 regcache_raw_supply (regcache, ARM_PS_REGNUM,
320 (char *) ®s[ARM_CPSR_GREGNUM]);
322 regcache_raw_supply (regcache, ARM_PS_REGNUM,
323 (char *) ®s[ARM_PC_REGNUM]);
326 if (ARM_PC_REGNUM == regno)
328 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
329 (get_regcache_arch (regcache),
330 regs[ARM_PC_REGNUM]);
331 regcache_raw_supply (regcache, ARM_PC_REGNUM,
332 (char *) ®s[ARM_PC_REGNUM]);
336 /* Fetch all general registers of the process and store into
340 fetch_regs (struct regcache *regcache)
345 /* Get the thread id for the ptrace call. */
346 tid = GET_THREAD_ID (inferior_ptid);
348 if (have_ptrace_getregset)
352 iov.iov_base = ®s;
353 iov.iov_len = sizeof (regs);
355 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
358 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
362 warning (_("Unable to fetch general registers."));
366 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
367 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
370 regcache_raw_supply (regcache, ARM_PS_REGNUM,
371 (char *) ®s[ARM_CPSR_GREGNUM]);
373 regcache_raw_supply (regcache, ARM_PS_REGNUM,
374 (char *) ®s[ARM_PC_REGNUM]);
376 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
377 (get_regcache_arch (regcache), regs[ARM_PC_REGNUM]);
378 regcache_raw_supply (regcache, ARM_PC_REGNUM,
379 (char *) ®s[ARM_PC_REGNUM]);
382 /* Store all general registers of the process from the values in
386 store_register (const struct regcache *regcache, int regno)
391 if (REG_VALID != regcache_register_status (regcache, regno))
394 /* Get the thread id for the ptrace call. */
395 tid = GET_THREAD_ID (inferior_ptid);
397 /* Get the general registers from the process. */
398 if (have_ptrace_getregset)
402 iov.iov_base = ®s;
403 iov.iov_len = sizeof (regs);
405 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
408 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
412 warning (_("Unable to fetch general registers."));
416 if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
417 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
418 else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
419 regcache_raw_collect (regcache, regno,
420 (char *) ®s[ARM_CPSR_GREGNUM]);
421 else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
422 regcache_raw_collect (regcache, ARM_PC_REGNUM,
423 (char *) ®s[ARM_PC_REGNUM]);
425 if (have_ptrace_getregset)
429 iov.iov_base = ®s;
430 iov.iov_len = sizeof (regs);
432 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
435 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
439 warning (_("Unable to store general register."));
445 store_regs (const struct regcache *regcache)
450 /* Get the thread id for the ptrace call. */
451 tid = GET_THREAD_ID (inferior_ptid);
453 /* Fetch the general registers. */
454 if (have_ptrace_getregset)
458 iov.iov_base = ®s;
459 iov.iov_len = sizeof (regs);
461 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
464 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
468 warning (_("Unable to fetch general registers."));
472 for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
474 if (REG_VALID == regcache_register_status (regcache, regno))
475 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
478 if (arm_apcs_32 && REG_VALID == regcache_register_status (regcache, ARM_PS_REGNUM))
479 regcache_raw_collect (regcache, ARM_PS_REGNUM,
480 (char *) ®s[ARM_CPSR_GREGNUM]);
482 if (have_ptrace_getregset)
486 iov.iov_base = ®s;
487 iov.iov_len = sizeof (regs);
489 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
492 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
496 warning (_("Unable to store general registers."));
501 /* Fetch all WMMX registers of the process and store into
504 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
507 fetch_wmmx_regs (struct regcache *regcache)
509 char regbuf[IWMMXT_REGS_SIZE];
512 /* Get the thread id for the ptrace call. */
513 tid = GET_THREAD_ID (inferior_ptid);
515 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
518 warning (_("Unable to fetch WMMX registers."));
522 for (regno = 0; regno < 16; regno++)
523 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
526 for (regno = 0; regno < 2; regno++)
527 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
528 ®buf[16 * 8 + regno * 4]);
530 for (regno = 0; regno < 4; regno++)
531 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
532 ®buf[16 * 8 + 2 * 4 + regno * 4]);
536 store_wmmx_regs (const struct regcache *regcache)
538 char regbuf[IWMMXT_REGS_SIZE];
541 /* Get the thread id for the ptrace call. */
542 tid = GET_THREAD_ID (inferior_ptid);
544 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
547 warning (_("Unable to fetch WMMX registers."));
551 for (regno = 0; regno < 16; regno++)
552 if (REG_VALID == regcache_register_status (regcache,
553 regno + ARM_WR0_REGNUM))
554 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
557 for (regno = 0; regno < 2; regno++)
558 if (REG_VALID == regcache_register_status (regcache,
559 regno + ARM_WCSSF_REGNUM))
560 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
561 ®buf[16 * 8 + regno * 4]);
563 for (regno = 0; regno < 4; regno++)
564 if (REG_VALID == regcache_register_status (regcache,
565 regno + ARM_WCGR0_REGNUM))
566 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
567 ®buf[16 * 8 + 2 * 4 + regno * 4]);
569 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
573 warning (_("Unable to store WMMX registers."));
578 /* Fetch and store VFP Registers. The kernel object has space for 32
579 64-bit registers, and the FPSCR. This is even when on a VFPv2 or
581 #define VFP_REGS_SIZE (32 * 8 + 4)
584 fetch_vfp_regs (struct regcache *regcache)
586 char regbuf[VFP_REGS_SIZE];
588 struct gdbarch *gdbarch = get_regcache_arch (regcache);
589 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
591 /* Get the thread id for the ptrace call. */
592 tid = GET_THREAD_ID (inferior_ptid);
594 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
597 warning (_("Unable to fetch VFP registers."));
601 for (regno = 0; regno < tdep->vfp_register_count; regno++)
602 regcache_raw_supply (regcache, regno + ARM_D0_REGNUM,
603 (char *) regbuf + regno * 8);
605 regcache_raw_supply (regcache, ARM_FPSCR_REGNUM,
606 (char *) regbuf + 32 * 8);
610 store_vfp_regs (const struct regcache *regcache)
612 char regbuf[VFP_REGS_SIZE];
614 struct gdbarch *gdbarch = get_regcache_arch (regcache);
615 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
617 /* Get the thread id for the ptrace call. */
618 tid = GET_THREAD_ID (inferior_ptid);
620 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
623 warning (_("Unable to fetch VFP registers (for update)."));
627 for (regno = 0; regno < tdep->vfp_register_count; regno++)
628 regcache_raw_collect (regcache, regno + ARM_D0_REGNUM,
629 (char *) regbuf + regno * 8);
631 regcache_raw_collect (regcache, ARM_FPSCR_REGNUM,
632 (char *) regbuf + 32 * 8);
634 ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
638 warning (_("Unable to store VFP registers."));
643 /* Fetch registers from the child process. Fetch all registers if
644 regno == -1, otherwise fetch all general registers or all floating
645 point registers depending upon the value of regno. */
648 arm_linux_fetch_inferior_registers (struct target_ops *ops,
649 struct regcache *regcache, int regno)
651 struct gdbarch *gdbarch = get_regcache_arch (regcache);
652 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
656 fetch_regs (regcache);
657 fetch_fpregs (regcache);
658 if (tdep->have_wmmx_registers)
659 fetch_wmmx_regs (regcache);
660 if (tdep->vfp_register_count > 0)
661 fetch_vfp_regs (regcache);
665 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
666 fetch_register (regcache, regno);
667 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
668 fetch_fpregister (regcache, regno);
669 else if (tdep->have_wmmx_registers
670 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
671 fetch_wmmx_regs (regcache);
672 else if (tdep->vfp_register_count > 0
673 && regno >= ARM_D0_REGNUM
674 && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
675 fetch_vfp_regs (regcache);
679 /* Store registers back into the inferior. Store all registers if
680 regno == -1, otherwise store all general registers or all floating
681 point registers depending upon the value of regno. */
684 arm_linux_store_inferior_registers (struct target_ops *ops,
685 struct regcache *regcache, int regno)
687 struct gdbarch *gdbarch = get_regcache_arch (regcache);
688 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
692 store_regs (regcache);
693 store_fpregs (regcache);
694 if (tdep->have_wmmx_registers)
695 store_wmmx_regs (regcache);
696 if (tdep->vfp_register_count > 0)
697 store_vfp_regs (regcache);
701 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
702 store_register (regcache, regno);
703 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
704 store_fpregister (regcache, regno);
705 else if (tdep->have_wmmx_registers
706 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
707 store_wmmx_regs (regcache);
708 else if (tdep->vfp_register_count > 0
709 && regno >= ARM_D0_REGNUM
710 && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
711 store_vfp_regs (regcache);
715 /* Wrapper functions for the standard regset handling, used by
719 fill_gregset (const struct regcache *regcache,
720 gdb_gregset_t *gregsetp, int regno)
722 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
726 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
728 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
732 fill_fpregset (const struct regcache *regcache,
733 gdb_fpregset_t *fpregsetp, int regno)
735 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
738 /* Fill GDB's register array with the floating-point register values
742 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
744 arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
747 /* Fetch the thread-local storage pointer for libthread_db. */
750 ps_get_thread_area (const struct ps_prochandle *ph,
751 lwpid_t lwpid, int idx, void **base)
753 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
756 /* IDX is the bias from the thread pointer to the beginning of the
757 thread descriptor. It has to be subtracted due to implementation
758 quirks in libthread_db. */
759 *base = (void *) ((char *)*base - idx);
764 static const struct target_desc *
765 arm_linux_read_description (struct target_ops *ops)
767 CORE_ADDR arm_hwcap = 0;
769 if (have_ptrace_getregset == -1)
771 elf_gregset_t gpregs;
773 int tid = GET_THREAD_ID (inferior_ptid);
775 iov.iov_base = &gpregs;
776 iov.iov_len = sizeof (gpregs);
778 /* Check if PTRACE_GETREGSET works. */
779 if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) < 0)
780 have_ptrace_getregset = 0;
782 have_ptrace_getregset = 1;
785 if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
787 return ops->beneath->to_read_description (ops->beneath);
790 if (arm_hwcap & HWCAP_IWMMXT)
791 return tdesc_arm_with_iwmmxt;
793 if (arm_hwcap & HWCAP_VFP)
797 const struct target_desc * result = NULL;
799 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
800 Neon with VFPv3-D32. */
801 if (arm_hwcap & HWCAP_NEON)
802 result = tdesc_arm_with_neon;
803 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
804 result = tdesc_arm_with_vfpv3;
806 result = tdesc_arm_with_vfpv2;
808 /* Now make sure that the kernel supports reading these
809 registers. Support was added in 2.6.30. */
810 pid = ptid_get_lwp (inferior_ptid);
812 buf = alloca (VFP_REGS_SIZE);
813 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
820 return ops->beneath->to_read_description (ops->beneath);
823 /* Information describing the hardware breakpoint capabilities. */
824 struct arm_linux_hwbp_cap
827 gdb_byte max_wp_length;
832 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
833 assume a maximum number of supported break-/watchpoints. */
837 /* Get hold of the Hardware Breakpoint information for the target we are
838 attached to. Returns NULL if the kernel doesn't support Hardware
839 breakpoints at all, or a pointer to the information structure. */
840 static const struct arm_linux_hwbp_cap *
841 arm_linux_get_hwbp_cap (void)
843 /* The info structure we return. */
844 static struct arm_linux_hwbp_cap info;
846 /* Is INFO in a good state? -1 means that no attempt has been made to
847 initialize INFO; 0 means an attempt has been made, but it failed; 1
848 means INFO is in an initialized state. */
849 static int available = -1;
856 tid = GET_THREAD_ID (inferior_ptid);
857 if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
861 info.arch = (gdb_byte)((val >> 24) & 0xff);
862 info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
863 info.wp_count = (gdb_byte)((val >> 8) & 0xff);
864 info.bp_count = (gdb_byte)(val & 0xff);
866 if (info.wp_count > MAX_WPTS)
868 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
869 supports %d"), MAX_WPTS, info.wp_count);
870 info.wp_count = MAX_WPTS;
873 if (info.bp_count > MAX_BPTS)
875 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
876 supports %d"), MAX_BPTS, info.bp_count);
877 info.bp_count = MAX_BPTS;
879 available = (info.arch != 0);
883 return available == 1 ? &info : NULL;
886 /* How many hardware breakpoints are available? */
888 arm_linux_get_hw_breakpoint_count (void)
890 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
891 return cap != NULL ? cap->bp_count : 0;
894 /* How many hardware watchpoints are available? */
896 arm_linux_get_hw_watchpoint_count (void)
898 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
899 return cap != NULL ? cap->wp_count : 0;
902 /* Have we got a free break-/watch-point available for use? Returns -1 if
903 there is not an appropriate resource available, otherwise returns 1. */
905 arm_linux_can_use_hw_breakpoint (struct target_ops *self,
906 int type, int cnt, int ot)
908 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
909 || type == bp_access_watchpoint || type == bp_watchpoint)
911 int count = arm_linux_get_hw_watchpoint_count ();
915 else if (cnt + ot > count)
918 else if (type == bp_hardware_breakpoint)
920 int count = arm_linux_get_hw_breakpoint_count ();
924 else if (cnt > count)
933 /* Enum describing the different types of ARM hardware break-/watch-points. */
942 /* Type describing an ARM Hardware Breakpoint Control register value. */
943 typedef unsigned int arm_hwbp_control_t;
945 /* Structure used to keep track of hardware break-/watch-points. */
946 struct arm_linux_hw_breakpoint
948 /* Address to break on, or being watched. */
949 unsigned int address;
950 /* Control register for break-/watch- point. */
951 arm_hwbp_control_t control;
954 /* Structure containing arrays of per process hardware break-/watchpoints
955 for caching address and control information.
957 The Linux ptrace interface to hardware break-/watch-points presents the
958 values in a vector centred around 0 (which is used fo generic information).
959 Positive indicies refer to breakpoint addresses/control registers, negative
960 indices to watchpoint addresses/control registers.
962 The Linux vector is indexed as follows:
963 -((i << 1) + 2): Control register for watchpoint i.
964 -((i << 1) + 1): Address register for watchpoint i.
965 0: Information register.
966 ((i << 1) + 1): Address register for breakpoint i.
967 ((i << 1) + 2): Control register for breakpoint i.
969 This structure is used as a per-thread cache of the state stored by the
970 kernel, so that we don't need to keep calling into the kernel to find a
973 We treat break-/watch-points with their enable bit clear as being deleted.
975 struct arm_linux_debug_reg_state
977 /* Hardware breakpoints for this process. */
978 struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
979 /* Hardware watchpoints for this process. */
980 struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
983 /* Per-process arch-specific data we want to keep. */
984 struct arm_linux_process_info
987 struct arm_linux_process_info *next;
988 /* The process identifier. */
990 /* Hardware break-/watchpoints state information. */
991 struct arm_linux_debug_reg_state state;
995 /* Per-thread arch-specific data we want to keep. */
998 /* Non-zero if our copy differs from what's recorded in the thread. */
999 char bpts_changed[MAX_BPTS];
1000 char wpts_changed[MAX_WPTS];
1003 static struct arm_linux_process_info *arm_linux_process_list = NULL;
1005 /* Find process data for process PID. */
1007 static struct arm_linux_process_info *
1008 arm_linux_find_process_pid (pid_t pid)
1010 struct arm_linux_process_info *proc;
1012 for (proc = arm_linux_process_list; proc; proc = proc->next)
1013 if (proc->pid == pid)
1019 /* Add process data for process PID. Returns newly allocated info
1022 static struct arm_linux_process_info *
1023 arm_linux_add_process (pid_t pid)
1025 struct arm_linux_process_info *proc;
1027 proc = xcalloc (1, sizeof (*proc));
1030 proc->next = arm_linux_process_list;
1031 arm_linux_process_list = proc;
1036 /* Get data specific info for process PID, creating it if necessary.
1037 Never returns NULL. */
1039 static struct arm_linux_process_info *
1040 arm_linux_process_info_get (pid_t pid)
1042 struct arm_linux_process_info *proc;
1044 proc = arm_linux_find_process_pid (pid);
1046 proc = arm_linux_add_process (pid);
1051 /* Called whenever GDB is no longer debugging process PID. It deletes
1052 data structures that keep track of debug register state. */
1055 arm_linux_forget_process (pid_t pid)
1057 struct arm_linux_process_info *proc, **proc_link;
1059 proc = arm_linux_process_list;
1060 proc_link = &arm_linux_process_list;
1062 while (proc != NULL)
1064 if (proc->pid == pid)
1066 *proc_link = proc->next;
1072 proc_link = &proc->next;
1077 /* Get hardware break-/watchpoint state for process PID. */
1079 static struct arm_linux_debug_reg_state *
1080 arm_linux_get_debug_reg_state (pid_t pid)
1082 return &arm_linux_process_info_get (pid)->state;
1085 /* Initialize an ARM hardware break-/watch-point control register value.
1086 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
1087 type of break-/watch-point; ENABLE indicates whether the point is enabled.
1089 static arm_hwbp_control_t
1090 arm_hwbp_control_initialize (unsigned byte_address_select,
1091 arm_hwbp_type hwbp_type,
1094 gdb_assert ((byte_address_select & ~0xffU) == 0);
1095 gdb_assert (hwbp_type != arm_hwbp_break
1096 || ((byte_address_select & 0xfU) != 0));
1098 return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
1101 /* Does the breakpoint control value CONTROL have the enable bit set? */
1103 arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
1105 return control & 0x1;
1108 /* Change a breakpoint control word so that it is in the disabled state. */
1109 static arm_hwbp_control_t
1110 arm_hwbp_control_disable (arm_hwbp_control_t control)
1112 return control & ~0x1;
1115 /* Initialise the hardware breakpoint structure P. The breakpoint will be
1116 enabled, and will point to the placed address of BP_TGT. */
1118 arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
1119 struct bp_target_info *bp_tgt,
1120 struct arm_linux_hw_breakpoint *p)
1123 CORE_ADDR address = bp_tgt->placed_address = bp_tgt->reqstd_address;
1125 /* We have to create a mask for the control register which says which bits
1126 of the word pointed to by address to break on. */
1127 if (arm_pc_is_thumb (gdbarch, address))
1138 p->address = (unsigned int) address;
1139 p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
1142 /* Get the ARM hardware breakpoint type from the RW value we're given when
1143 asked to set a watchpoint. */
1144 static arm_hwbp_type
1145 arm_linux_get_hwbp_type (int rw)
1148 return arm_hwbp_load;
1149 else if (rw == hw_write)
1150 return arm_hwbp_store;
1152 return arm_hwbp_access;
1155 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
1156 to LEN. The type of watchpoint is given in RW. */
1158 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len, int rw,
1159 struct arm_linux_hw_breakpoint *p)
1161 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1164 gdb_assert (cap != NULL);
1165 gdb_assert (cap->max_wp_length != 0);
1167 mask = (1 << len) - 1;
1169 p->address = (unsigned int) addr;
1170 p->control = arm_hwbp_control_initialize (mask,
1171 arm_linux_get_hwbp_type (rw), 1);
1174 /* Are two break-/watch-points equal? */
1176 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
1177 const struct arm_linux_hw_breakpoint *p2)
1179 return p1->address == p2->address && p1->control == p2->control;
1182 /* Callback to mark a watch-/breakpoint to be updated in all threads of
1183 the current process. */
1185 struct update_registers_data
1192 update_registers_callback (struct lwp_info *lwp, void *arg)
1194 struct update_registers_data *data = (struct update_registers_data *) arg;
1196 if (lwp->arch_private == NULL)
1197 lwp->arch_private = XCNEW (struct arch_lwp_info);
1199 /* The actual update is done later just before resuming the lwp,
1200 we just mark that the registers need updating. */
1202 lwp->arch_private->wpts_changed[data->index] = 1;
1204 lwp->arch_private->bpts_changed[data->index] = 1;
1206 /* If the lwp isn't stopped, force it to momentarily pause, so
1207 we can update its breakpoint registers. */
1209 linux_stop_lwp (lwp);
1214 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
1215 =1) BPT for thread TID. */
1217 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt,
1223 struct arm_linux_hw_breakpoint* bpts;
1224 struct update_registers_data data;
1226 pid = ptid_get_pid (inferior_ptid);
1227 pid_ptid = pid_to_ptid (pid);
1231 count = arm_linux_get_hw_watchpoint_count ();
1232 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1236 count = arm_linux_get_hw_breakpoint_count ();
1237 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1240 for (i = 0; i < count; ++i)
1241 if (!arm_hwbp_control_is_enabled (bpts[i].control))
1243 data.watch = watchpoint;
1246 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1250 gdb_assert (i != count);
1253 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1254 (WATCHPOINT = 1) BPT for thread TID. */
1256 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt,
1262 struct arm_linux_hw_breakpoint* bpts;
1263 struct update_registers_data data;
1265 pid = ptid_get_pid (inferior_ptid);
1266 pid_ptid = pid_to_ptid (pid);
1270 count = arm_linux_get_hw_watchpoint_count ();
1271 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1275 count = arm_linux_get_hw_breakpoint_count ();
1276 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1279 for (i = 0; i < count; ++i)
1280 if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
1282 data.watch = watchpoint;
1284 bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
1285 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1289 gdb_assert (i != count);
1292 /* Insert a Hardware breakpoint. */
1294 arm_linux_insert_hw_breakpoint (struct target_ops *self,
1295 struct gdbarch *gdbarch,
1296 struct bp_target_info *bp_tgt)
1298 struct lwp_info *lp;
1299 struct arm_linux_hw_breakpoint p;
1301 if (arm_linux_get_hw_breakpoint_count () == 0)
1304 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1306 arm_linux_insert_hw_breakpoint1 (&p, 0);
1311 /* Remove a hardware breakpoint. */
1313 arm_linux_remove_hw_breakpoint (struct target_ops *self,
1314 struct gdbarch *gdbarch,
1315 struct bp_target_info *bp_tgt)
1317 struct lwp_info *lp;
1318 struct arm_linux_hw_breakpoint p;
1320 if (arm_linux_get_hw_breakpoint_count () == 0)
1323 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1325 arm_linux_remove_hw_breakpoint1 (&p, 0);
1330 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1333 arm_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
1334 CORE_ADDR addr, int len)
1336 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1337 CORE_ADDR max_wp_length, aligned_addr;
1339 /* Can not set watchpoints for zero or negative lengths. */
1343 /* Need to be able to use the ptrace interface. */
1344 if (cap == NULL || cap->wp_count == 0)
1347 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1348 range covered by a watchpoint. */
1349 max_wp_length = (CORE_ADDR)cap->max_wp_length;
1350 aligned_addr = addr & ~(max_wp_length - 1);
1352 if (aligned_addr + max_wp_length < addr + len)
1355 /* The current ptrace interface can only handle watchpoints that are a
1357 if ((len & (len - 1)) != 0)
1360 /* All tests passed so we must be able to set a watchpoint. */
1364 /* Insert a Hardware breakpoint. */
1366 arm_linux_insert_watchpoint (struct target_ops *self,
1367 CORE_ADDR addr, int len, int rw,
1368 struct expression *cond)
1370 struct lwp_info *lp;
1371 struct arm_linux_hw_breakpoint p;
1373 if (arm_linux_get_hw_watchpoint_count () == 0)
1376 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1378 arm_linux_insert_hw_breakpoint1 (&p, 1);
1383 /* Remove a hardware breakpoint. */
1385 arm_linux_remove_watchpoint (struct target_ops *self,
1386 CORE_ADDR addr, int len, int rw,
1387 struct expression *cond)
1389 struct lwp_info *lp;
1390 struct arm_linux_hw_breakpoint p;
1392 if (arm_linux_get_hw_watchpoint_count () == 0)
1395 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1397 arm_linux_remove_hw_breakpoint1 (&p, 1);
1402 /* What was the data address the target was stopped on accessing. */
1404 arm_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1409 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
1412 /* This must be a hardware breakpoint. */
1413 if (siginfo.si_signo != SIGTRAP
1414 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1417 /* We must be able to set hardware watchpoints. */
1418 if (arm_linux_get_hw_watchpoint_count () == 0)
1421 slot = siginfo.si_errno;
1423 /* If we are in a positive slot then we're looking at a breakpoint and not
1428 *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
1432 /* Has the target been stopped by hitting a watchpoint? */
1434 arm_linux_stopped_by_watchpoint (struct target_ops *ops)
1437 return arm_linux_stopped_data_address (ops, &addr);
1441 arm_linux_watchpoint_addr_within_range (struct target_ops *target,
1443 CORE_ADDR start, int length)
1445 return start <= addr && start + length - 1 >= addr;
1448 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1449 in the parent thread to the child thread. */
1451 arm_linux_new_thread (struct lwp_info *lp)
1454 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
1456 /* Mark that all the hardware breakpoint/watchpoint register pairs
1457 for this thread need to be initialized. */
1459 for (i = 0; i < MAX_BPTS; i++)
1461 info->bpts_changed[i] = 1;
1462 info->wpts_changed[i] = 1;
1465 lp->arch_private = info;
1468 /* Called when resuming a thread.
1469 The hardware debug registers are updated when there is any change. */
1472 arm_linux_prepare_to_resume (struct lwp_info *lwp)
1475 struct arm_linux_hw_breakpoint *bpts, *wpts;
1476 struct arch_lwp_info *arm_lwp_info = lwp->arch_private;
1478 pid = ptid_get_lwp (lwp->ptid);
1479 bpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->bpts;
1480 wpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->wpts;
1482 /* NULL means this is the main thread still going through the shell,
1483 or, no watchpoint has been set yet. In that case, there's
1485 if (arm_lwp_info == NULL)
1488 for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
1489 if (arm_lwp_info->bpts_changed[i])
1492 if (arm_hwbp_control_is_enabled (bpts[i].control))
1493 if (ptrace (PTRACE_SETHBPREGS, pid,
1494 (PTRACE_TYPE_ARG3) ((i << 1) + 1), &bpts[i].address) < 0)
1495 perror_with_name (_("Unexpected error setting breakpoint"));
1497 if (bpts[i].control != 0)
1498 if (ptrace (PTRACE_SETHBPREGS, pid,
1499 (PTRACE_TYPE_ARG3) ((i << 1) + 2), &bpts[i].control) < 0)
1500 perror_with_name (_("Unexpected error setting breakpoint"));
1502 arm_lwp_info->bpts_changed[i] = 0;
1505 for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
1506 if (arm_lwp_info->wpts_changed[i])
1509 if (arm_hwbp_control_is_enabled (wpts[i].control))
1510 if (ptrace (PTRACE_SETHBPREGS, pid,
1511 (PTRACE_TYPE_ARG3) -((i << 1) + 1), &wpts[i].address) < 0)
1512 perror_with_name (_("Unexpected error setting watchpoint"));
1514 if (wpts[i].control != 0)
1515 if (ptrace (PTRACE_SETHBPREGS, pid,
1516 (PTRACE_TYPE_ARG3) -((i << 1) + 2), &wpts[i].control) < 0)
1517 perror_with_name (_("Unexpected error setting watchpoint"));
1519 arm_lwp_info->wpts_changed[i] = 0;
1523 /* linux_nat_new_fork hook. */
1526 arm_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
1529 struct arm_linux_debug_reg_state *parent_state;
1530 struct arm_linux_debug_reg_state *child_state;
1532 /* NULL means no watchpoint has ever been set in the parent. In
1533 that case, there's nothing to do. */
1534 if (parent->arch_private == NULL)
1537 /* GDB core assumes the child inherits the watchpoints/hw
1538 breakpoints of the parent, and will remove them all from the
1539 forked off process. Copy the debug registers mirrors into the
1540 new process so that all breakpoints and watchpoints can be
1541 removed together. */
1543 parent_pid = ptid_get_pid (parent->ptid);
1544 parent_state = arm_linux_get_debug_reg_state (parent_pid);
1545 child_state = arm_linux_get_debug_reg_state (child_pid);
1546 *child_state = *parent_state;
1549 void _initialize_arm_linux_nat (void);
1552 _initialize_arm_linux_nat (void)
1554 struct target_ops *t;
1556 /* Fill in the generic GNU/Linux methods. */
1557 t = linux_target ();
1559 /* Add our register access methods. */
1560 t->to_fetch_registers = arm_linux_fetch_inferior_registers;
1561 t->to_store_registers = arm_linux_store_inferior_registers;
1563 /* Add our hardware breakpoint and watchpoint implementation. */
1564 t->to_can_use_hw_breakpoint = arm_linux_can_use_hw_breakpoint;
1565 t->to_insert_hw_breakpoint = arm_linux_insert_hw_breakpoint;
1566 t->to_remove_hw_breakpoint = arm_linux_remove_hw_breakpoint;
1567 t->to_region_ok_for_hw_watchpoint = arm_linux_region_ok_for_hw_watchpoint;
1568 t->to_insert_watchpoint = arm_linux_insert_watchpoint;
1569 t->to_remove_watchpoint = arm_linux_remove_watchpoint;
1570 t->to_stopped_by_watchpoint = arm_linux_stopped_by_watchpoint;
1571 t->to_stopped_data_address = arm_linux_stopped_data_address;
1572 t->to_watchpoint_addr_within_range = arm_linux_watchpoint_addr_within_range;
1574 t->to_read_description = arm_linux_read_description;
1576 /* Register the target. */
1577 linux_nat_add_target (t);
1579 /* Handle thread creation and exit. */
1580 linux_nat_set_new_thread (t, arm_linux_new_thread);
1581 linux_nat_set_prepare_to_resume (t, arm_linux_prepare_to_resume);
1583 /* Handle process creation and exit. */
1584 linux_nat_set_new_fork (t, arm_linux_new_fork);
1585 linux_nat_set_forget_process (t, arm_linux_forget_process);