1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999-2018 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"
27 #include "observable.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 class arm_linux_nat_target final : public linux_nat_target
72 /* Add our register access methods. */
73 void fetch_registers (struct regcache *, int) override;
74 void store_registers (struct regcache *, int) override;
76 /* Add our hardware breakpoint and watchpoint implementation. */
77 int can_use_hw_breakpoint (enum bptype, int, int) override;
79 int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
81 int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
83 int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
85 int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
86 struct expression *) override;
88 int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
89 struct expression *) override;
90 int stopped_by_watchpoint () override;
92 int stopped_data_address (CORE_ADDR *) override;
94 int watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
96 const struct target_desc *read_description () override;
99 static arm_linux_nat_target the_arm_linux_nat_target;
101 /* Get the whole floating point state of the process and store it
105 fetch_fpregs (struct regcache *regcache)
108 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
110 /* Get the thread id for the ptrace call. */
111 tid = ptid_get_lwp (regcache_get_ptid (regcache));
113 /* Read the floating point state. */
114 if (have_ptrace_getregset == TRIBOOL_TRUE)
119 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
121 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
124 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
127 perror_with_name (_("Unable to fetch the floating point registers."));
130 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
131 fp + NWFPE_FPSR_OFFSET);
133 /* Fetch the floating point registers. */
134 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
135 supply_nwfpe_register (regcache, regno, fp);
138 /* Save the whole floating point state of the process using
139 the contents from regcache. */
142 store_fpregs (const struct regcache *regcache)
145 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
147 /* Get the thread id for the ptrace call. */
148 tid = ptid_get_lwp (regcache_get_ptid (regcache));
150 /* Read the floating point state. */
151 if (have_ptrace_getregset == TRIBOOL_TRUE)
153 elf_fpregset_t fpregs;
156 iov.iov_base = &fpregs;
157 iov.iov_len = sizeof (fpregs);
159 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
162 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
165 perror_with_name (_("Unable to fetch the floating point registers."));
168 if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
169 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
171 /* Store the floating point registers. */
172 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
173 if (REG_VALID == regcache_register_status (regcache, regno))
174 collect_nwfpe_register (regcache, regno, fp);
176 if (have_ptrace_getregset == TRIBOOL_TRUE)
181 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
183 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
186 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
189 perror_with_name (_("Unable to store floating point registers."));
192 /* Fetch all general registers of the process and store into
196 fetch_regs (struct regcache *regcache)
201 /* Get the thread id for the ptrace call. */
202 tid = ptid_get_lwp (regcache_get_ptid (regcache));
204 if (have_ptrace_getregset == TRIBOOL_TRUE)
208 iov.iov_base = ®s;
209 iov.iov_len = sizeof (regs);
211 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
214 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
217 perror_with_name (_("Unable to fetch general registers."));
219 aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, arm_apcs_32);
223 store_regs (const struct regcache *regcache)
228 /* Get the thread id for the ptrace call. */
229 tid = ptid_get_lwp (regcache_get_ptid (regcache));
231 /* Fetch the general registers. */
232 if (have_ptrace_getregset == TRIBOOL_TRUE)
236 iov.iov_base = ®s;
237 iov.iov_len = sizeof (regs);
239 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
242 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
245 perror_with_name (_("Unable to fetch general registers."));
247 aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, arm_apcs_32);
249 if (have_ptrace_getregset == TRIBOOL_TRUE)
253 iov.iov_base = ®s;
254 iov.iov_len = sizeof (regs);
256 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
259 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
262 perror_with_name (_("Unable to store general registers."));
265 /* Fetch all WMMX registers of the process and store into
268 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
271 fetch_wmmx_regs (struct regcache *regcache)
273 char regbuf[IWMMXT_REGS_SIZE];
276 /* Get the thread id for the ptrace call. */
277 tid = ptid_get_lwp (regcache_get_ptid (regcache));
279 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
281 perror_with_name (_("Unable to fetch WMMX registers."));
283 for (regno = 0; regno < 16; regno++)
284 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
287 for (regno = 0; regno < 2; regno++)
288 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
289 ®buf[16 * 8 + regno * 4]);
291 for (regno = 0; regno < 4; regno++)
292 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
293 ®buf[16 * 8 + 2 * 4 + regno * 4]);
297 store_wmmx_regs (const struct regcache *regcache)
299 char regbuf[IWMMXT_REGS_SIZE];
302 /* Get the thread id for the ptrace call. */
303 tid = ptid_get_lwp (regcache_get_ptid (regcache));
305 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
307 perror_with_name (_("Unable to fetch WMMX registers."));
309 for (regno = 0; regno < 16; regno++)
310 if (REG_VALID == regcache_register_status (regcache,
311 regno + ARM_WR0_REGNUM))
312 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
315 for (regno = 0; regno < 2; regno++)
316 if (REG_VALID == regcache_register_status (regcache,
317 regno + ARM_WCSSF_REGNUM))
318 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
319 ®buf[16 * 8 + regno * 4]);
321 for (regno = 0; regno < 4; regno++)
322 if (REG_VALID == regcache_register_status (regcache,
323 regno + ARM_WCGR0_REGNUM))
324 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
325 ®buf[16 * 8 + 2 * 4 + regno * 4]);
327 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
330 perror_with_name (_("Unable to store WMMX registers."));
334 fetch_vfp_regs (struct regcache *regcache)
336 gdb_byte regbuf[VFP_REGS_SIZE];
338 struct gdbarch *gdbarch = regcache->arch ();
339 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
341 /* Get the thread id for the ptrace call. */
342 tid = ptid_get_lwp (regcache_get_ptid (regcache));
344 if (have_ptrace_getregset == TRIBOOL_TRUE)
348 iov.iov_base = regbuf;
349 iov.iov_len = VFP_REGS_SIZE;
350 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
353 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
356 perror_with_name (_("Unable to fetch VFP registers."));
358 aarch32_vfp_regcache_supply (regcache, regbuf,
359 tdep->vfp_register_count);
363 store_vfp_regs (const struct regcache *regcache)
365 gdb_byte regbuf[VFP_REGS_SIZE];
367 struct gdbarch *gdbarch = regcache->arch ();
368 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
370 /* Get the thread id for the ptrace call. */
371 tid = ptid_get_lwp (regcache_get_ptid (regcache));
373 if (have_ptrace_getregset == TRIBOOL_TRUE)
377 iov.iov_base = regbuf;
378 iov.iov_len = VFP_REGS_SIZE;
379 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
382 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
385 perror_with_name (_("Unable to fetch VFP registers (for update)."));
387 aarch32_vfp_regcache_collect (regcache, regbuf,
388 tdep->vfp_register_count);
390 if (have_ptrace_getregset == TRIBOOL_TRUE)
394 iov.iov_base = regbuf;
395 iov.iov_len = VFP_REGS_SIZE;
396 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iov);
399 ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
402 perror_with_name (_("Unable to store VFP registers."));
405 /* Fetch registers from the child process. Fetch all registers if
406 regno == -1, otherwise fetch all general registers or all floating
407 point registers depending upon the value of regno. */
410 arm_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
412 struct gdbarch *gdbarch = regcache->arch ();
413 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
417 fetch_regs (regcache);
418 if (tdep->have_wmmx_registers)
419 fetch_wmmx_regs (regcache);
420 if (tdep->vfp_register_count > 0)
421 fetch_vfp_regs (regcache);
422 if (tdep->have_fpa_registers)
423 fetch_fpregs (regcache);
427 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
428 fetch_regs (regcache);
429 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
430 fetch_fpregs (regcache);
431 else if (tdep->have_wmmx_registers
432 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
433 fetch_wmmx_regs (regcache);
434 else if (tdep->vfp_register_count > 0
435 && regno >= ARM_D0_REGNUM
436 && (regno < ARM_D0_REGNUM + tdep->vfp_register_count
437 || regno == ARM_FPSCR_REGNUM))
438 fetch_vfp_regs (regcache);
442 /* Store registers back into the inferior. Store all registers if
443 regno == -1, otherwise store all general registers or all floating
444 point registers depending upon the value of regno. */
447 arm_linux_nat_target::store_registers (struct regcache *regcache, int regno)
449 struct gdbarch *gdbarch = regcache->arch ();
450 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
454 store_regs (regcache);
455 if (tdep->have_wmmx_registers)
456 store_wmmx_regs (regcache);
457 if (tdep->vfp_register_count > 0)
458 store_vfp_regs (regcache);
459 if (tdep->have_fpa_registers)
460 store_fpregs (regcache);
464 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
465 store_regs (regcache);
466 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
467 store_fpregs (regcache);
468 else if (tdep->have_wmmx_registers
469 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
470 store_wmmx_regs (regcache);
471 else if (tdep->vfp_register_count > 0
472 && regno >= ARM_D0_REGNUM
473 && (regno < ARM_D0_REGNUM + tdep->vfp_register_count
474 || regno == ARM_FPSCR_REGNUM))
475 store_vfp_regs (regcache);
479 /* Wrapper functions for the standard regset handling, used by
483 fill_gregset (const struct regcache *regcache,
484 gdb_gregset_t *gregsetp, int regno)
486 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
490 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
492 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
496 fill_fpregset (const struct regcache *regcache,
497 gdb_fpregset_t *fpregsetp, int regno)
499 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
502 /* Fill GDB's register array with the floating-point register values
506 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
508 arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
511 /* Fetch the thread-local storage pointer for libthread_db. */
514 ps_get_thread_area (struct ps_prochandle *ph,
515 lwpid_t lwpid, int idx, void **base)
517 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
520 /* IDX is the bias from the thread pointer to the beginning of the
521 thread descriptor. It has to be subtracted due to implementation
522 quirks in libthread_db. */
523 *base = (void *) ((char *)*base - idx);
528 const struct target_desc *
529 arm_linux_nat_target::read_description ()
531 CORE_ADDR arm_hwcap = 0;
533 if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
535 elf_gregset_t gpregs;
537 int tid = ptid_get_lwp (inferior_ptid);
539 iov.iov_base = &gpregs;
540 iov.iov_len = sizeof (gpregs);
542 /* Check if PTRACE_GETREGSET works. */
543 if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) < 0)
544 have_ptrace_getregset = TRIBOOL_FALSE;
546 have_ptrace_getregset = TRIBOOL_TRUE;
549 if (target_auxv_search (this, AT_HWCAP, &arm_hwcap) != 1)
551 return this->beneath->read_description ();
554 if (arm_hwcap & HWCAP_IWMMXT)
555 return tdesc_arm_with_iwmmxt;
557 if (arm_hwcap & HWCAP_VFP)
561 const struct target_desc * result = NULL;
563 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
564 Neon with VFPv3-D32. */
565 if (arm_hwcap & HWCAP_NEON)
566 result = tdesc_arm_with_neon;
567 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
568 result = tdesc_arm_with_vfpv3;
570 result = tdesc_arm_with_vfpv2;
572 /* Now make sure that the kernel supports reading these
573 registers. Support was added in 2.6.30. */
574 pid = ptid_get_lwp (inferior_ptid);
576 buf = (char *) alloca (VFP_REGS_SIZE);
577 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
584 return this->beneath->read_description ();
587 /* Information describing the hardware breakpoint capabilities. */
588 struct arm_linux_hwbp_cap
591 gdb_byte max_wp_length;
596 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
597 assume a maximum number of supported break-/watchpoints. */
601 /* Get hold of the Hardware Breakpoint information for the target we are
602 attached to. Returns NULL if the kernel doesn't support Hardware
603 breakpoints at all, or a pointer to the information structure. */
604 static const struct arm_linux_hwbp_cap *
605 arm_linux_get_hwbp_cap (void)
607 /* The info structure we return. */
608 static struct arm_linux_hwbp_cap info;
610 /* Is INFO in a good state? -1 means that no attempt has been made to
611 initialize INFO; 0 means an attempt has been made, but it failed; 1
612 means INFO is in an initialized state. */
613 static int available = -1;
620 tid = ptid_get_lwp (inferior_ptid);
621 if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
625 info.arch = (gdb_byte)((val >> 24) & 0xff);
626 info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
627 info.wp_count = (gdb_byte)((val >> 8) & 0xff);
628 info.bp_count = (gdb_byte)(val & 0xff);
630 if (info.wp_count > MAX_WPTS)
632 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
633 supports %d"), MAX_WPTS, info.wp_count);
634 info.wp_count = MAX_WPTS;
637 if (info.bp_count > MAX_BPTS)
639 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
640 supports %d"), MAX_BPTS, info.bp_count);
641 info.bp_count = MAX_BPTS;
643 available = (info.arch != 0);
647 return available == 1 ? &info : NULL;
650 /* How many hardware breakpoints are available? */
652 arm_linux_get_hw_breakpoint_count (void)
654 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
655 return cap != NULL ? cap->bp_count : 0;
658 /* How many hardware watchpoints are available? */
660 arm_linux_get_hw_watchpoint_count (void)
662 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
663 return cap != NULL ? cap->wp_count : 0;
666 /* Have we got a free break-/watch-point available for use? Returns -1 if
667 there is not an appropriate resource available, otherwise returns 1. */
669 arm_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
672 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
673 || type == bp_access_watchpoint || type == bp_watchpoint)
675 int count = arm_linux_get_hw_watchpoint_count ();
679 else if (cnt + ot > count)
682 else if (type == bp_hardware_breakpoint)
684 int count = arm_linux_get_hw_breakpoint_count ();
688 else if (cnt > count)
697 /* Enum describing the different types of ARM hardware break-/watch-points. */
706 /* Type describing an ARM Hardware Breakpoint Control register value. */
707 typedef unsigned int arm_hwbp_control_t;
709 /* Structure used to keep track of hardware break-/watch-points. */
710 struct arm_linux_hw_breakpoint
712 /* Address to break on, or being watched. */
713 unsigned int address;
714 /* Control register for break-/watch- point. */
715 arm_hwbp_control_t control;
718 /* Structure containing arrays of per process hardware break-/watchpoints
719 for caching address and control information.
721 The Linux ptrace interface to hardware break-/watch-points presents the
722 values in a vector centred around 0 (which is used fo generic information).
723 Positive indicies refer to breakpoint addresses/control registers, negative
724 indices to watchpoint addresses/control registers.
726 The Linux vector is indexed as follows:
727 -((i << 1) + 2): Control register for watchpoint i.
728 -((i << 1) + 1): Address register for watchpoint i.
729 0: Information register.
730 ((i << 1) + 1): Address register for breakpoint i.
731 ((i << 1) + 2): Control register for breakpoint i.
733 This structure is used as a per-thread cache of the state stored by the
734 kernel, so that we don't need to keep calling into the kernel to find a
737 We treat break-/watch-points with their enable bit clear as being deleted.
739 struct arm_linux_debug_reg_state
741 /* Hardware breakpoints for this process. */
742 struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
743 /* Hardware watchpoints for this process. */
744 struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
747 /* Per-process arch-specific data we want to keep. */
748 struct arm_linux_process_info
751 struct arm_linux_process_info *next;
752 /* The process identifier. */
754 /* Hardware break-/watchpoints state information. */
755 struct arm_linux_debug_reg_state state;
759 /* Per-thread arch-specific data we want to keep. */
762 /* Non-zero if our copy differs from what's recorded in the thread. */
763 char bpts_changed[MAX_BPTS];
764 char wpts_changed[MAX_WPTS];
767 static struct arm_linux_process_info *arm_linux_process_list = NULL;
769 /* Find process data for process PID. */
771 static struct arm_linux_process_info *
772 arm_linux_find_process_pid (pid_t pid)
774 struct arm_linux_process_info *proc;
776 for (proc = arm_linux_process_list; proc; proc = proc->next)
777 if (proc->pid == pid)
783 /* Add process data for process PID. Returns newly allocated info
786 static struct arm_linux_process_info *
787 arm_linux_add_process (pid_t pid)
789 struct arm_linux_process_info *proc;
791 proc = XCNEW (struct arm_linux_process_info);
794 proc->next = arm_linux_process_list;
795 arm_linux_process_list = proc;
800 /* Get data specific info for process PID, creating it if necessary.
801 Never returns NULL. */
803 static struct arm_linux_process_info *
804 arm_linux_process_info_get (pid_t pid)
806 struct arm_linux_process_info *proc;
808 proc = arm_linux_find_process_pid (pid);
810 proc = arm_linux_add_process (pid);
815 /* Called whenever GDB is no longer debugging process PID. It deletes
816 data structures that keep track of debug register state. */
819 arm_linux_forget_process (pid_t pid)
821 struct arm_linux_process_info *proc, **proc_link;
823 proc = arm_linux_process_list;
824 proc_link = &arm_linux_process_list;
828 if (proc->pid == pid)
830 *proc_link = proc->next;
836 proc_link = &proc->next;
841 /* Get hardware break-/watchpoint state for process PID. */
843 static struct arm_linux_debug_reg_state *
844 arm_linux_get_debug_reg_state (pid_t pid)
846 return &arm_linux_process_info_get (pid)->state;
849 /* Initialize an ARM hardware break-/watch-point control register value.
850 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
851 type of break-/watch-point; ENABLE indicates whether the point is enabled.
853 static arm_hwbp_control_t
854 arm_hwbp_control_initialize (unsigned byte_address_select,
855 arm_hwbp_type hwbp_type,
858 gdb_assert ((byte_address_select & ~0xffU) == 0);
859 gdb_assert (hwbp_type != arm_hwbp_break
860 || ((byte_address_select & 0xfU) != 0));
862 return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
865 /* Does the breakpoint control value CONTROL have the enable bit set? */
867 arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
869 return control & 0x1;
872 /* Change a breakpoint control word so that it is in the disabled state. */
873 static arm_hwbp_control_t
874 arm_hwbp_control_disable (arm_hwbp_control_t control)
876 return control & ~0x1;
879 /* Initialise the hardware breakpoint structure P. The breakpoint will be
880 enabled, and will point to the placed address of BP_TGT. */
882 arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
883 struct bp_target_info *bp_tgt,
884 struct arm_linux_hw_breakpoint *p)
887 CORE_ADDR address = bp_tgt->placed_address = bp_tgt->reqstd_address;
889 /* We have to create a mask for the control register which says which bits
890 of the word pointed to by address to break on. */
891 if (arm_pc_is_thumb (gdbarch, address))
902 p->address = (unsigned int) address;
903 p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
906 /* Get the ARM hardware breakpoint type from the TYPE value we're
907 given when asked to set a watchpoint. */
909 arm_linux_get_hwbp_type (enum target_hw_bp_type type)
912 return arm_hwbp_load;
913 else if (type == hw_write)
914 return arm_hwbp_store;
916 return arm_hwbp_access;
919 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
920 to LEN. The type of watchpoint is given in RW. */
922 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len,
923 enum target_hw_bp_type type,
924 struct arm_linux_hw_breakpoint *p)
926 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
929 gdb_assert (cap != NULL);
930 gdb_assert (cap->max_wp_length != 0);
932 mask = (1 << len) - 1;
934 p->address = (unsigned int) addr;
935 p->control = arm_hwbp_control_initialize (mask,
936 arm_linux_get_hwbp_type (type), 1);
939 /* Are two break-/watch-points equal? */
941 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
942 const struct arm_linux_hw_breakpoint *p2)
944 return p1->address == p2->address && p1->control == p2->control;
947 /* Callback to mark a watch-/breakpoint to be updated in all threads of
948 the current process. */
950 struct update_registers_data
957 update_registers_callback (struct lwp_info *lwp, void *arg)
959 struct update_registers_data *data = (struct update_registers_data *) arg;
961 if (lwp->arch_private == NULL)
962 lwp->arch_private = XCNEW (struct arch_lwp_info);
964 /* The actual update is done later just before resuming the lwp,
965 we just mark that the registers need updating. */
967 lwp->arch_private->wpts_changed[data->index] = 1;
969 lwp->arch_private->bpts_changed[data->index] = 1;
971 /* If the lwp isn't stopped, force it to momentarily pause, so
972 we can update its breakpoint registers. */
974 linux_stop_lwp (lwp);
979 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
980 =1) BPT for thread TID. */
982 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt,
988 struct arm_linux_hw_breakpoint* bpts;
989 struct update_registers_data data;
991 pid = ptid_get_pid (inferior_ptid);
992 pid_ptid = pid_to_ptid (pid);
996 count = arm_linux_get_hw_watchpoint_count ();
997 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1001 count = arm_linux_get_hw_breakpoint_count ();
1002 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1005 for (i = 0; i < count; ++i)
1006 if (!arm_hwbp_control_is_enabled (bpts[i].control))
1008 data.watch = watchpoint;
1011 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1015 gdb_assert (i != count);
1018 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1019 (WATCHPOINT = 1) BPT for thread TID. */
1021 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt,
1027 struct arm_linux_hw_breakpoint* bpts;
1028 struct update_registers_data data;
1030 pid = ptid_get_pid (inferior_ptid);
1031 pid_ptid = pid_to_ptid (pid);
1035 count = arm_linux_get_hw_watchpoint_count ();
1036 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1040 count = arm_linux_get_hw_breakpoint_count ();
1041 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1044 for (i = 0; i < count; ++i)
1045 if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
1047 data.watch = watchpoint;
1049 bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
1050 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1054 gdb_assert (i != count);
1057 /* Insert a Hardware breakpoint. */
1059 arm_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
1060 struct bp_target_info *bp_tgt)
1062 struct lwp_info *lp;
1063 struct arm_linux_hw_breakpoint p;
1065 if (arm_linux_get_hw_breakpoint_count () == 0)
1068 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1070 arm_linux_insert_hw_breakpoint1 (&p, 0);
1075 /* Remove a hardware breakpoint. */
1077 arm_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
1078 struct bp_target_info *bp_tgt)
1080 struct lwp_info *lp;
1081 struct arm_linux_hw_breakpoint p;
1083 if (arm_linux_get_hw_breakpoint_count () == 0)
1086 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1088 arm_linux_remove_hw_breakpoint1 (&p, 0);
1093 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1096 arm_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
1098 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1099 CORE_ADDR max_wp_length, aligned_addr;
1101 /* Can not set watchpoints for zero or negative lengths. */
1105 /* Need to be able to use the ptrace interface. */
1106 if (cap == NULL || cap->wp_count == 0)
1109 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1110 range covered by a watchpoint. */
1111 max_wp_length = (CORE_ADDR)cap->max_wp_length;
1112 aligned_addr = addr & ~(max_wp_length - 1);
1114 if (aligned_addr + max_wp_length < addr + len)
1117 /* The current ptrace interface can only handle watchpoints that are a
1119 if ((len & (len - 1)) != 0)
1122 /* All tests passed so we must be able to set a watchpoint. */
1126 /* Insert a Hardware breakpoint. */
1128 arm_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
1129 enum target_hw_bp_type rw,
1130 struct expression *cond)
1132 struct lwp_info *lp;
1133 struct arm_linux_hw_breakpoint p;
1135 if (arm_linux_get_hw_watchpoint_count () == 0)
1138 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1140 arm_linux_insert_hw_breakpoint1 (&p, 1);
1145 /* Remove a hardware breakpoint. */
1147 arm_linux_nat_target::remove_watchpoint (CORE_ADDR addr,
1148 int len, enum target_hw_bp_type rw,
1149 struct expression *cond)
1151 struct lwp_info *lp;
1152 struct arm_linux_hw_breakpoint p;
1154 if (arm_linux_get_hw_watchpoint_count () == 0)
1157 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1159 arm_linux_remove_hw_breakpoint1 (&p, 1);
1164 /* What was the data address the target was stopped on accessing. */
1166 arm_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
1171 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
1174 /* This must be a hardware breakpoint. */
1175 if (siginfo.si_signo != SIGTRAP
1176 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1179 /* We must be able to set hardware watchpoints. */
1180 if (arm_linux_get_hw_watchpoint_count () == 0)
1183 slot = siginfo.si_errno;
1185 /* If we are in a positive slot then we're looking at a breakpoint and not
1190 *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
1194 /* Has the target been stopped by hitting a watchpoint? */
1196 arm_linux_nat_target::stopped_by_watchpoint ()
1199 return stopped_data_address (&addr);
1203 arm_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
1207 return start <= addr && start + length - 1 >= addr;
1210 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1211 in the parent thread to the child thread. */
1213 arm_linux_new_thread (struct lwp_info *lp)
1216 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
1218 /* Mark that all the hardware breakpoint/watchpoint register pairs
1219 for this thread need to be initialized. */
1221 for (i = 0; i < MAX_BPTS; i++)
1223 info->bpts_changed[i] = 1;
1224 info->wpts_changed[i] = 1;
1227 lp->arch_private = info;
1230 /* Function to call when a thread is being deleted. */
1233 arm_linux_delete_thread (struct arch_lwp_info *arch_lwp)
1238 /* Called when resuming a thread.
1239 The hardware debug registers are updated when there is any change. */
1242 arm_linux_prepare_to_resume (struct lwp_info *lwp)
1245 struct arm_linux_hw_breakpoint *bpts, *wpts;
1246 struct arch_lwp_info *arm_lwp_info = lwp->arch_private;
1248 pid = ptid_get_lwp (lwp->ptid);
1249 bpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->bpts;
1250 wpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->wpts;
1252 /* NULL means this is the main thread still going through the shell,
1253 or, no watchpoint has been set yet. In that case, there's
1255 if (arm_lwp_info == NULL)
1258 for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
1259 if (arm_lwp_info->bpts_changed[i])
1262 if (arm_hwbp_control_is_enabled (bpts[i].control))
1263 if (ptrace (PTRACE_SETHBPREGS, pid,
1264 (PTRACE_TYPE_ARG3) ((i << 1) + 1), &bpts[i].address) < 0)
1265 perror_with_name (_("Unexpected error setting breakpoint"));
1267 if (bpts[i].control != 0)
1268 if (ptrace (PTRACE_SETHBPREGS, pid,
1269 (PTRACE_TYPE_ARG3) ((i << 1) + 2), &bpts[i].control) < 0)
1270 perror_with_name (_("Unexpected error setting breakpoint"));
1272 arm_lwp_info->bpts_changed[i] = 0;
1275 for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
1276 if (arm_lwp_info->wpts_changed[i])
1279 if (arm_hwbp_control_is_enabled (wpts[i].control))
1280 if (ptrace (PTRACE_SETHBPREGS, pid,
1281 (PTRACE_TYPE_ARG3) -((i << 1) + 1), &wpts[i].address) < 0)
1282 perror_with_name (_("Unexpected error setting watchpoint"));
1284 if (wpts[i].control != 0)
1285 if (ptrace (PTRACE_SETHBPREGS, pid,
1286 (PTRACE_TYPE_ARG3) -((i << 1) + 2), &wpts[i].control) < 0)
1287 perror_with_name (_("Unexpected error setting watchpoint"));
1289 arm_lwp_info->wpts_changed[i] = 0;
1293 /* linux_nat_new_fork hook. */
1296 arm_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
1299 struct arm_linux_debug_reg_state *parent_state;
1300 struct arm_linux_debug_reg_state *child_state;
1302 /* NULL means no watchpoint has ever been set in the parent. In
1303 that case, there's nothing to do. */
1304 if (parent->arch_private == NULL)
1307 /* GDB core assumes the child inherits the watchpoints/hw
1308 breakpoints of the parent, and will remove them all from the
1309 forked off process. Copy the debug registers mirrors into the
1310 new process so that all breakpoints and watchpoints can be
1311 removed together. */
1313 parent_pid = ptid_get_pid (parent->ptid);
1314 parent_state = arm_linux_get_debug_reg_state (parent_pid);
1315 child_state = arm_linux_get_debug_reg_state (child_pid);
1316 *child_state = *parent_state;
1320 _initialize_arm_linux_nat (void)
1322 target_ops *t = &the_arm_linux_nat_target;
1324 /* Register the target. */
1325 linux_target = &the_arm_linux_nat_target;
1328 /* Handle thread creation and exit. */
1329 linux_nat_set_new_thread (t, arm_linux_new_thread);
1330 linux_nat_set_delete_thread (t, arm_linux_delete_thread);
1331 linux_nat_set_prepare_to_resume (t, arm_linux_prepare_to_resume);
1333 /* Handle process creation and exit. */
1334 linux_nat_set_new_fork (t, arm_linux_new_fork);
1335 linux_nat_set_forget_process (t, arm_linux_forget_process);