1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999-2017 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"
32 #include "aarch32-linux-nat.h"
34 #include <elf/common.h>
36 #include "nat/gdb_ptrace.h"
37 #include <sys/utsname.h>
38 #include <sys/procfs.h>
40 #include "nat/linux-ptrace.h"
42 /* Prototypes for supply_gregset etc. */
45 /* Defines ps_err_e, struct ps_prochandle. */
46 #include "gdb_proc_service.h"
48 #ifndef PTRACE_GET_THREAD_AREA
49 #define PTRACE_GET_THREAD_AREA 22
52 #ifndef PTRACE_GETWMMXREGS
53 #define PTRACE_GETWMMXREGS 18
54 #define PTRACE_SETWMMXREGS 19
57 #ifndef PTRACE_GETVFPREGS
58 #define PTRACE_GETVFPREGS 27
59 #define PTRACE_SETVFPREGS 28
62 #ifndef PTRACE_GETHBPREGS
63 #define PTRACE_GETHBPREGS 29
64 #define PTRACE_SETHBPREGS 30
67 extern int arm_apcs_32;
69 /* Get the whole floating point state of the process and store it
73 fetch_fpregs (struct regcache *regcache)
76 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
78 /* Get the thread id for the ptrace call. */
79 tid = ptid_get_lwp (regcache_get_ptid (regcache));
81 /* Read the floating point state. */
82 if (have_ptrace_getregset == TRIBOOL_TRUE)
87 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
89 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
92 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
95 perror_with_name (_("Unable to fetch the floating point registers."));
98 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
99 fp + NWFPE_FPSR_OFFSET);
101 /* Fetch the floating point registers. */
102 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
103 supply_nwfpe_register (regcache, regno, fp);
106 /* Save the whole floating point state of the process using
107 the contents from regcache. */
110 store_fpregs (const struct regcache *regcache)
113 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
115 /* Get the thread id for the ptrace call. */
116 tid = ptid_get_lwp (regcache_get_ptid (regcache));
118 /* Read the floating point state. */
119 if (have_ptrace_getregset == TRIBOOL_TRUE)
121 elf_fpregset_t fpregs;
124 iov.iov_base = &fpregs;
125 iov.iov_len = sizeof (fpregs);
127 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
130 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
133 perror_with_name (_("Unable to fetch the floating point registers."));
136 if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
137 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
139 /* Store the floating point registers. */
140 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
141 if (REG_VALID == regcache_register_status (regcache, regno))
142 collect_nwfpe_register (regcache, regno, fp);
144 if (have_ptrace_getregset == TRIBOOL_TRUE)
149 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
151 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
154 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
157 perror_with_name (_("Unable to store floating point registers."));
160 /* Fetch all general registers of the process and store into
164 fetch_regs (struct regcache *regcache)
169 /* Get the thread id for the ptrace call. */
170 tid = ptid_get_lwp (regcache_get_ptid (regcache));
172 if (have_ptrace_getregset == TRIBOOL_TRUE)
176 iov.iov_base = ®s;
177 iov.iov_len = sizeof (regs);
179 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
182 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
185 perror_with_name (_("Unable to fetch general registers."));
187 aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, arm_apcs_32);
191 store_regs (const struct regcache *regcache)
196 /* Get the thread id for the ptrace call. */
197 tid = ptid_get_lwp (regcache_get_ptid (regcache));
199 /* Fetch the general registers. */
200 if (have_ptrace_getregset == TRIBOOL_TRUE)
204 iov.iov_base = ®s;
205 iov.iov_len = sizeof (regs);
207 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
210 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
213 perror_with_name (_("Unable to fetch general registers."));
215 aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, arm_apcs_32);
217 if (have_ptrace_getregset == TRIBOOL_TRUE)
221 iov.iov_base = ®s;
222 iov.iov_len = sizeof (regs);
224 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
227 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
230 perror_with_name (_("Unable to store general registers."));
233 /* Fetch all WMMX registers of the process and store into
236 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
239 fetch_wmmx_regs (struct regcache *regcache)
241 char regbuf[IWMMXT_REGS_SIZE];
244 /* Get the thread id for the ptrace call. */
245 tid = ptid_get_lwp (regcache_get_ptid (regcache));
247 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
249 perror_with_name (_("Unable to fetch WMMX registers."));
251 for (regno = 0; regno < 16; regno++)
252 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
255 for (regno = 0; regno < 2; regno++)
256 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
257 ®buf[16 * 8 + regno * 4]);
259 for (regno = 0; regno < 4; regno++)
260 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
261 ®buf[16 * 8 + 2 * 4 + regno * 4]);
265 store_wmmx_regs (const struct regcache *regcache)
267 char regbuf[IWMMXT_REGS_SIZE];
270 /* Get the thread id for the ptrace call. */
271 tid = ptid_get_lwp (regcache_get_ptid (regcache));
273 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
275 perror_with_name (_("Unable to fetch WMMX registers."));
277 for (regno = 0; regno < 16; regno++)
278 if (REG_VALID == regcache_register_status (regcache,
279 regno + ARM_WR0_REGNUM))
280 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
283 for (regno = 0; regno < 2; regno++)
284 if (REG_VALID == regcache_register_status (regcache,
285 regno + ARM_WCSSF_REGNUM))
286 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
287 ®buf[16 * 8 + regno * 4]);
289 for (regno = 0; regno < 4; regno++)
290 if (REG_VALID == regcache_register_status (regcache,
291 regno + ARM_WCGR0_REGNUM))
292 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
293 ®buf[16 * 8 + 2 * 4 + regno * 4]);
295 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
298 perror_with_name (_("Unable to store WMMX registers."));
302 fetch_vfp_regs (struct regcache *regcache)
304 gdb_byte regbuf[VFP_REGS_SIZE];
306 struct gdbarch *gdbarch = get_regcache_arch (regcache);
307 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
309 /* Get the thread id for the ptrace call. */
310 tid = ptid_get_lwp (regcache_get_ptid (regcache));
312 if (have_ptrace_getregset == TRIBOOL_TRUE)
316 iov.iov_base = regbuf;
317 iov.iov_len = VFP_REGS_SIZE;
318 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
321 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
324 perror_with_name (_("Unable to fetch VFP registers."));
326 aarch32_vfp_regcache_supply (regcache, regbuf,
327 tdep->vfp_register_count);
331 store_vfp_regs (const struct regcache *regcache)
333 gdb_byte regbuf[VFP_REGS_SIZE];
335 struct gdbarch *gdbarch = get_regcache_arch (regcache);
336 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
338 /* Get the thread id for the ptrace call. */
339 tid = ptid_get_lwp (regcache_get_ptid (regcache));
341 if (have_ptrace_getregset == TRIBOOL_TRUE)
345 iov.iov_base = regbuf;
346 iov.iov_len = VFP_REGS_SIZE;
347 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
350 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
353 perror_with_name (_("Unable to fetch VFP registers (for update)."));
355 aarch32_vfp_regcache_collect (regcache, regbuf,
356 tdep->vfp_register_count);
358 if (have_ptrace_getregset == TRIBOOL_TRUE)
362 iov.iov_base = regbuf;
363 iov.iov_len = VFP_REGS_SIZE;
364 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iov);
367 ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
370 perror_with_name (_("Unable to store VFP registers."));
373 /* Fetch registers from the child process. Fetch all registers if
374 regno == -1, otherwise fetch all general registers or all floating
375 point registers depending upon the value of regno. */
378 arm_linux_fetch_inferior_registers (struct target_ops *ops,
379 struct regcache *regcache, int regno)
381 struct gdbarch *gdbarch = get_regcache_arch (regcache);
382 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
386 fetch_regs (regcache);
387 if (tdep->have_wmmx_registers)
388 fetch_wmmx_regs (regcache);
389 if (tdep->vfp_register_count > 0)
390 fetch_vfp_regs (regcache);
391 if (tdep->have_fpa_registers)
392 fetch_fpregs (regcache);
396 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
397 fetch_regs (regcache);
398 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
399 fetch_fpregs (regcache);
400 else if (tdep->have_wmmx_registers
401 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
402 fetch_wmmx_regs (regcache);
403 else if (tdep->vfp_register_count > 0
404 && regno >= ARM_D0_REGNUM
405 && (regno < ARM_D0_REGNUM + tdep->vfp_register_count
406 || regno == ARM_FPSCR_REGNUM))
407 fetch_vfp_regs (regcache);
411 /* Store registers back into the inferior. Store all registers if
412 regno == -1, otherwise store all general registers or all floating
413 point registers depending upon the value of regno. */
416 arm_linux_store_inferior_registers (struct target_ops *ops,
417 struct regcache *regcache, int regno)
419 struct gdbarch *gdbarch = get_regcache_arch (regcache);
420 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
424 store_regs (regcache);
425 if (tdep->have_wmmx_registers)
426 store_wmmx_regs (regcache);
427 if (tdep->vfp_register_count > 0)
428 store_vfp_regs (regcache);
429 if (tdep->have_fpa_registers)
430 store_fpregs (regcache);
434 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
435 store_regs (regcache);
436 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
437 store_fpregs (regcache);
438 else if (tdep->have_wmmx_registers
439 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
440 store_wmmx_regs (regcache);
441 else if (tdep->vfp_register_count > 0
442 && regno >= ARM_D0_REGNUM
443 && (regno < ARM_D0_REGNUM + tdep->vfp_register_count
444 || regno == ARM_FPSCR_REGNUM))
445 store_vfp_regs (regcache);
449 /* Wrapper functions for the standard regset handling, used by
453 fill_gregset (const struct regcache *regcache,
454 gdb_gregset_t *gregsetp, int regno)
456 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
460 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
462 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
466 fill_fpregset (const struct regcache *regcache,
467 gdb_fpregset_t *fpregsetp, int regno)
469 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
472 /* Fill GDB's register array with the floating-point register values
476 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
478 arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
481 /* Fetch the thread-local storage pointer for libthread_db. */
484 ps_get_thread_area (struct ps_prochandle *ph,
485 lwpid_t lwpid, int idx, void **base)
487 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
490 /* IDX is the bias from the thread pointer to the beginning of the
491 thread descriptor. It has to be subtracted due to implementation
492 quirks in libthread_db. */
493 *base = (void *) ((char *)*base - idx);
498 static const struct target_desc *
499 arm_linux_read_description (struct target_ops *ops)
501 CORE_ADDR arm_hwcap = 0;
503 if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
505 elf_gregset_t gpregs;
507 int tid = ptid_get_lwp (inferior_ptid);
509 iov.iov_base = &gpregs;
510 iov.iov_len = sizeof (gpregs);
512 /* Check if PTRACE_GETREGSET works. */
513 if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) < 0)
514 have_ptrace_getregset = TRIBOOL_FALSE;
516 have_ptrace_getregset = TRIBOOL_TRUE;
519 if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
521 return ops->beneath->to_read_description (ops->beneath);
524 if (arm_hwcap & HWCAP_IWMMXT)
525 return tdesc_arm_with_iwmmxt;
527 if (arm_hwcap & HWCAP_VFP)
531 const struct target_desc * result = NULL;
533 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
534 Neon with VFPv3-D32. */
535 if (arm_hwcap & HWCAP_NEON)
536 result = tdesc_arm_with_neon;
537 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
538 result = tdesc_arm_with_vfpv3;
540 result = tdesc_arm_with_vfpv2;
542 /* Now make sure that the kernel supports reading these
543 registers. Support was added in 2.6.30. */
544 pid = ptid_get_lwp (inferior_ptid);
546 buf = (char *) alloca (VFP_REGS_SIZE);
547 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
554 return ops->beneath->to_read_description (ops->beneath);
557 /* Information describing the hardware breakpoint capabilities. */
558 struct arm_linux_hwbp_cap
561 gdb_byte max_wp_length;
566 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
567 assume a maximum number of supported break-/watchpoints. */
571 /* Get hold of the Hardware Breakpoint information for the target we are
572 attached to. Returns NULL if the kernel doesn't support Hardware
573 breakpoints at all, or a pointer to the information structure. */
574 static const struct arm_linux_hwbp_cap *
575 arm_linux_get_hwbp_cap (void)
577 /* The info structure we return. */
578 static struct arm_linux_hwbp_cap info;
580 /* Is INFO in a good state? -1 means that no attempt has been made to
581 initialize INFO; 0 means an attempt has been made, but it failed; 1
582 means INFO is in an initialized state. */
583 static int available = -1;
590 tid = ptid_get_lwp (inferior_ptid);
591 if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
595 info.arch = (gdb_byte)((val >> 24) & 0xff);
596 info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
597 info.wp_count = (gdb_byte)((val >> 8) & 0xff);
598 info.bp_count = (gdb_byte)(val & 0xff);
600 if (info.wp_count > MAX_WPTS)
602 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
603 supports %d"), MAX_WPTS, info.wp_count);
604 info.wp_count = MAX_WPTS;
607 if (info.bp_count > MAX_BPTS)
609 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
610 supports %d"), MAX_BPTS, info.bp_count);
611 info.bp_count = MAX_BPTS;
613 available = (info.arch != 0);
617 return available == 1 ? &info : NULL;
620 /* How many hardware breakpoints are available? */
622 arm_linux_get_hw_breakpoint_count (void)
624 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
625 return cap != NULL ? cap->bp_count : 0;
628 /* How many hardware watchpoints are available? */
630 arm_linux_get_hw_watchpoint_count (void)
632 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
633 return cap != NULL ? cap->wp_count : 0;
636 /* Have we got a free break-/watch-point available for use? Returns -1 if
637 there is not an appropriate resource available, otherwise returns 1. */
639 arm_linux_can_use_hw_breakpoint (struct target_ops *self,
643 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
644 || type == bp_access_watchpoint || type == bp_watchpoint)
646 int count = arm_linux_get_hw_watchpoint_count ();
650 else if (cnt + ot > count)
653 else if (type == bp_hardware_breakpoint)
655 int count = arm_linux_get_hw_breakpoint_count ();
659 else if (cnt > count)
668 /* Enum describing the different types of ARM hardware break-/watch-points. */
677 /* Type describing an ARM Hardware Breakpoint Control register value. */
678 typedef unsigned int arm_hwbp_control_t;
680 /* Structure used to keep track of hardware break-/watch-points. */
681 struct arm_linux_hw_breakpoint
683 /* Address to break on, or being watched. */
684 unsigned int address;
685 /* Control register for break-/watch- point. */
686 arm_hwbp_control_t control;
689 /* Structure containing arrays of per process hardware break-/watchpoints
690 for caching address and control information.
692 The Linux ptrace interface to hardware break-/watch-points presents the
693 values in a vector centred around 0 (which is used fo generic information).
694 Positive indicies refer to breakpoint addresses/control registers, negative
695 indices to watchpoint addresses/control registers.
697 The Linux vector is indexed as follows:
698 -((i << 1) + 2): Control register for watchpoint i.
699 -((i << 1) + 1): Address register for watchpoint i.
700 0: Information register.
701 ((i << 1) + 1): Address register for breakpoint i.
702 ((i << 1) + 2): Control register for breakpoint i.
704 This structure is used as a per-thread cache of the state stored by the
705 kernel, so that we don't need to keep calling into the kernel to find a
708 We treat break-/watch-points with their enable bit clear as being deleted.
710 struct arm_linux_debug_reg_state
712 /* Hardware breakpoints for this process. */
713 struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
714 /* Hardware watchpoints for this process. */
715 struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
718 /* Per-process arch-specific data we want to keep. */
719 struct arm_linux_process_info
722 struct arm_linux_process_info *next;
723 /* The process identifier. */
725 /* Hardware break-/watchpoints state information. */
726 struct arm_linux_debug_reg_state state;
730 /* Per-thread arch-specific data we want to keep. */
733 /* Non-zero if our copy differs from what's recorded in the thread. */
734 char bpts_changed[MAX_BPTS];
735 char wpts_changed[MAX_WPTS];
738 static struct arm_linux_process_info *arm_linux_process_list = NULL;
740 /* Find process data for process PID. */
742 static struct arm_linux_process_info *
743 arm_linux_find_process_pid (pid_t pid)
745 struct arm_linux_process_info *proc;
747 for (proc = arm_linux_process_list; proc; proc = proc->next)
748 if (proc->pid == pid)
754 /* Add process data for process PID. Returns newly allocated info
757 static struct arm_linux_process_info *
758 arm_linux_add_process (pid_t pid)
760 struct arm_linux_process_info *proc;
762 proc = XCNEW (struct arm_linux_process_info);
765 proc->next = arm_linux_process_list;
766 arm_linux_process_list = proc;
771 /* Get data specific info for process PID, creating it if necessary.
772 Never returns NULL. */
774 static struct arm_linux_process_info *
775 arm_linux_process_info_get (pid_t pid)
777 struct arm_linux_process_info *proc;
779 proc = arm_linux_find_process_pid (pid);
781 proc = arm_linux_add_process (pid);
786 /* Called whenever GDB is no longer debugging process PID. It deletes
787 data structures that keep track of debug register state. */
790 arm_linux_forget_process (pid_t pid)
792 struct arm_linux_process_info *proc, **proc_link;
794 proc = arm_linux_process_list;
795 proc_link = &arm_linux_process_list;
799 if (proc->pid == pid)
801 *proc_link = proc->next;
807 proc_link = &proc->next;
812 /* Get hardware break-/watchpoint state for process PID. */
814 static struct arm_linux_debug_reg_state *
815 arm_linux_get_debug_reg_state (pid_t pid)
817 return &arm_linux_process_info_get (pid)->state;
820 /* Initialize an ARM hardware break-/watch-point control register value.
821 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
822 type of break-/watch-point; ENABLE indicates whether the point is enabled.
824 static arm_hwbp_control_t
825 arm_hwbp_control_initialize (unsigned byte_address_select,
826 arm_hwbp_type hwbp_type,
829 gdb_assert ((byte_address_select & ~0xffU) == 0);
830 gdb_assert (hwbp_type != arm_hwbp_break
831 || ((byte_address_select & 0xfU) != 0));
833 return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
836 /* Does the breakpoint control value CONTROL have the enable bit set? */
838 arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
840 return control & 0x1;
843 /* Change a breakpoint control word so that it is in the disabled state. */
844 static arm_hwbp_control_t
845 arm_hwbp_control_disable (arm_hwbp_control_t control)
847 return control & ~0x1;
850 /* Initialise the hardware breakpoint structure P. The breakpoint will be
851 enabled, and will point to the placed address of BP_TGT. */
853 arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
854 struct bp_target_info *bp_tgt,
855 struct arm_linux_hw_breakpoint *p)
858 CORE_ADDR address = bp_tgt->placed_address = bp_tgt->reqstd_address;
860 /* We have to create a mask for the control register which says which bits
861 of the word pointed to by address to break on. */
862 if (arm_pc_is_thumb (gdbarch, address))
873 p->address = (unsigned int) address;
874 p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
877 /* Get the ARM hardware breakpoint type from the TYPE value we're
878 given when asked to set a watchpoint. */
880 arm_linux_get_hwbp_type (enum target_hw_bp_type type)
883 return arm_hwbp_load;
884 else if (type == hw_write)
885 return arm_hwbp_store;
887 return arm_hwbp_access;
890 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
891 to LEN. The type of watchpoint is given in RW. */
893 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len,
894 enum target_hw_bp_type type,
895 struct arm_linux_hw_breakpoint *p)
897 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
900 gdb_assert (cap != NULL);
901 gdb_assert (cap->max_wp_length != 0);
903 mask = (1 << len) - 1;
905 p->address = (unsigned int) addr;
906 p->control = arm_hwbp_control_initialize (mask,
907 arm_linux_get_hwbp_type (type), 1);
910 /* Are two break-/watch-points equal? */
912 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
913 const struct arm_linux_hw_breakpoint *p2)
915 return p1->address == p2->address && p1->control == p2->control;
918 /* Callback to mark a watch-/breakpoint to be updated in all threads of
919 the current process. */
921 struct update_registers_data
928 update_registers_callback (struct lwp_info *lwp, void *arg)
930 struct update_registers_data *data = (struct update_registers_data *) arg;
932 if (lwp->arch_private == NULL)
933 lwp->arch_private = XCNEW (struct arch_lwp_info);
935 /* The actual update is done later just before resuming the lwp,
936 we just mark that the registers need updating. */
938 lwp->arch_private->wpts_changed[data->index] = 1;
940 lwp->arch_private->bpts_changed[data->index] = 1;
942 /* If the lwp isn't stopped, force it to momentarily pause, so
943 we can update its breakpoint registers. */
945 linux_stop_lwp (lwp);
950 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
951 =1) BPT for thread TID. */
953 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt,
959 struct arm_linux_hw_breakpoint* bpts;
960 struct update_registers_data data;
962 pid = ptid_get_pid (inferior_ptid);
963 pid_ptid = pid_to_ptid (pid);
967 count = arm_linux_get_hw_watchpoint_count ();
968 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
972 count = arm_linux_get_hw_breakpoint_count ();
973 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
976 for (i = 0; i < count; ++i)
977 if (!arm_hwbp_control_is_enabled (bpts[i].control))
979 data.watch = watchpoint;
982 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
986 gdb_assert (i != count);
989 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
990 (WATCHPOINT = 1) BPT for thread TID. */
992 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt,
998 struct arm_linux_hw_breakpoint* bpts;
999 struct update_registers_data data;
1001 pid = ptid_get_pid (inferior_ptid);
1002 pid_ptid = pid_to_ptid (pid);
1006 count = arm_linux_get_hw_watchpoint_count ();
1007 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1011 count = arm_linux_get_hw_breakpoint_count ();
1012 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1015 for (i = 0; i < count; ++i)
1016 if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
1018 data.watch = watchpoint;
1020 bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
1021 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1025 gdb_assert (i != count);
1028 /* Insert a Hardware breakpoint. */
1030 arm_linux_insert_hw_breakpoint (struct target_ops *self,
1031 struct gdbarch *gdbarch,
1032 struct bp_target_info *bp_tgt)
1034 struct lwp_info *lp;
1035 struct arm_linux_hw_breakpoint p;
1037 if (arm_linux_get_hw_breakpoint_count () == 0)
1040 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1042 arm_linux_insert_hw_breakpoint1 (&p, 0);
1047 /* Remove a hardware breakpoint. */
1049 arm_linux_remove_hw_breakpoint (struct target_ops *self,
1050 struct gdbarch *gdbarch,
1051 struct bp_target_info *bp_tgt)
1053 struct lwp_info *lp;
1054 struct arm_linux_hw_breakpoint p;
1056 if (arm_linux_get_hw_breakpoint_count () == 0)
1059 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1061 arm_linux_remove_hw_breakpoint1 (&p, 0);
1066 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1069 arm_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
1070 CORE_ADDR addr, int len)
1072 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1073 CORE_ADDR max_wp_length, aligned_addr;
1075 /* Can not set watchpoints for zero or negative lengths. */
1079 /* Need to be able to use the ptrace interface. */
1080 if (cap == NULL || cap->wp_count == 0)
1083 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1084 range covered by a watchpoint. */
1085 max_wp_length = (CORE_ADDR)cap->max_wp_length;
1086 aligned_addr = addr & ~(max_wp_length - 1);
1088 if (aligned_addr + max_wp_length < addr + len)
1091 /* The current ptrace interface can only handle watchpoints that are a
1093 if ((len & (len - 1)) != 0)
1096 /* All tests passed so we must be able to set a watchpoint. */
1100 /* Insert a Hardware breakpoint. */
1102 arm_linux_insert_watchpoint (struct target_ops *self,
1103 CORE_ADDR addr, int len,
1104 enum target_hw_bp_type rw,
1105 struct expression *cond)
1107 struct lwp_info *lp;
1108 struct arm_linux_hw_breakpoint p;
1110 if (arm_linux_get_hw_watchpoint_count () == 0)
1113 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1115 arm_linux_insert_hw_breakpoint1 (&p, 1);
1120 /* Remove a hardware breakpoint. */
1122 arm_linux_remove_watchpoint (struct target_ops *self, CORE_ADDR addr,
1123 int len, enum target_hw_bp_type rw,
1124 struct expression *cond)
1126 struct lwp_info *lp;
1127 struct arm_linux_hw_breakpoint p;
1129 if (arm_linux_get_hw_watchpoint_count () == 0)
1132 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1134 arm_linux_remove_hw_breakpoint1 (&p, 1);
1139 /* What was the data address the target was stopped on accessing. */
1141 arm_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1146 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
1149 /* This must be a hardware breakpoint. */
1150 if (siginfo.si_signo != SIGTRAP
1151 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1154 /* We must be able to set hardware watchpoints. */
1155 if (arm_linux_get_hw_watchpoint_count () == 0)
1158 slot = siginfo.si_errno;
1160 /* If we are in a positive slot then we're looking at a breakpoint and not
1165 *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
1169 /* Has the target been stopped by hitting a watchpoint? */
1171 arm_linux_stopped_by_watchpoint (struct target_ops *ops)
1174 return arm_linux_stopped_data_address (ops, &addr);
1178 arm_linux_watchpoint_addr_within_range (struct target_ops *target,
1180 CORE_ADDR start, int length)
1182 return start <= addr && start + length - 1 >= addr;
1185 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1186 in the parent thread to the child thread. */
1188 arm_linux_new_thread (struct lwp_info *lp)
1191 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
1193 /* Mark that all the hardware breakpoint/watchpoint register pairs
1194 for this thread need to be initialized. */
1196 for (i = 0; i < MAX_BPTS; i++)
1198 info->bpts_changed[i] = 1;
1199 info->wpts_changed[i] = 1;
1202 lp->arch_private = info;
1205 /* Called when resuming a thread.
1206 The hardware debug registers are updated when there is any change. */
1209 arm_linux_prepare_to_resume (struct lwp_info *lwp)
1212 struct arm_linux_hw_breakpoint *bpts, *wpts;
1213 struct arch_lwp_info *arm_lwp_info = lwp->arch_private;
1215 pid = ptid_get_lwp (lwp->ptid);
1216 bpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->bpts;
1217 wpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->wpts;
1219 /* NULL means this is the main thread still going through the shell,
1220 or, no watchpoint has been set yet. In that case, there's
1222 if (arm_lwp_info == NULL)
1225 for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
1226 if (arm_lwp_info->bpts_changed[i])
1229 if (arm_hwbp_control_is_enabled (bpts[i].control))
1230 if (ptrace (PTRACE_SETHBPREGS, pid,
1231 (PTRACE_TYPE_ARG3) ((i << 1) + 1), &bpts[i].address) < 0)
1232 perror_with_name (_("Unexpected error setting breakpoint"));
1234 if (bpts[i].control != 0)
1235 if (ptrace (PTRACE_SETHBPREGS, pid,
1236 (PTRACE_TYPE_ARG3) ((i << 1) + 2), &bpts[i].control) < 0)
1237 perror_with_name (_("Unexpected error setting breakpoint"));
1239 arm_lwp_info->bpts_changed[i] = 0;
1242 for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
1243 if (arm_lwp_info->wpts_changed[i])
1246 if (arm_hwbp_control_is_enabled (wpts[i].control))
1247 if (ptrace (PTRACE_SETHBPREGS, pid,
1248 (PTRACE_TYPE_ARG3) -((i << 1) + 1), &wpts[i].address) < 0)
1249 perror_with_name (_("Unexpected error setting watchpoint"));
1251 if (wpts[i].control != 0)
1252 if (ptrace (PTRACE_SETHBPREGS, pid,
1253 (PTRACE_TYPE_ARG3) -((i << 1) + 2), &wpts[i].control) < 0)
1254 perror_with_name (_("Unexpected error setting watchpoint"));
1256 arm_lwp_info->wpts_changed[i] = 0;
1260 /* linux_nat_new_fork hook. */
1263 arm_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
1266 struct arm_linux_debug_reg_state *parent_state;
1267 struct arm_linux_debug_reg_state *child_state;
1269 /* NULL means no watchpoint has ever been set in the parent. In
1270 that case, there's nothing to do. */
1271 if (parent->arch_private == NULL)
1274 /* GDB core assumes the child inherits the watchpoints/hw
1275 breakpoints of the parent, and will remove them all from the
1276 forked off process. Copy the debug registers mirrors into the
1277 new process so that all breakpoints and watchpoints can be
1278 removed together. */
1280 parent_pid = ptid_get_pid (parent->ptid);
1281 parent_state = arm_linux_get_debug_reg_state (parent_pid);
1282 child_state = arm_linux_get_debug_reg_state (child_pid);
1283 *child_state = *parent_state;
1287 _initialize_arm_linux_nat (void)
1289 struct target_ops *t;
1291 /* Fill in the generic GNU/Linux methods. */
1292 t = linux_target ();
1294 /* Add our register access methods. */
1295 t->to_fetch_registers = arm_linux_fetch_inferior_registers;
1296 t->to_store_registers = arm_linux_store_inferior_registers;
1298 /* Add our hardware breakpoint and watchpoint implementation. */
1299 t->to_can_use_hw_breakpoint = arm_linux_can_use_hw_breakpoint;
1300 t->to_insert_hw_breakpoint = arm_linux_insert_hw_breakpoint;
1301 t->to_remove_hw_breakpoint = arm_linux_remove_hw_breakpoint;
1302 t->to_region_ok_for_hw_watchpoint = arm_linux_region_ok_for_hw_watchpoint;
1303 t->to_insert_watchpoint = arm_linux_insert_watchpoint;
1304 t->to_remove_watchpoint = arm_linux_remove_watchpoint;
1305 t->to_stopped_by_watchpoint = arm_linux_stopped_by_watchpoint;
1306 t->to_stopped_data_address = arm_linux_stopped_data_address;
1307 t->to_watchpoint_addr_within_range = arm_linux_watchpoint_addr_within_range;
1309 t->to_read_description = arm_linux_read_description;
1311 /* Register the target. */
1312 linux_nat_add_target (t);
1314 /* Handle thread creation and exit. */
1315 linux_nat_set_new_thread (t, arm_linux_new_thread);
1316 linux_nat_set_prepare_to_resume (t, arm_linux_prepare_to_resume);
1318 /* Handle process creation and exit. */
1319 linux_nat_set_new_fork (t, arm_linux_new_fork);
1320 linux_nat_set_forget_process (t, arm_linux_forget_process);