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 bool stopped_by_watchpoint () override;
92 bool stopped_data_address (CORE_ADDR *) override;
94 bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
96 const struct target_desc *read_description () override;
98 /* Override linux_nat_target low methods. */
100 /* Handle thread creation and exit. */
101 void low_new_thread (struct lwp_info *lp) override;
102 void low_delete_thread (struct arch_lwp_info *lp) override;
103 void low_prepare_to_resume (struct lwp_info *lp) override;
105 /* Handle process creation and exit. */
106 void low_new_fork (struct lwp_info *parent, pid_t child_pid) override;
107 void low_forget_process (pid_t pid) override;
110 static arm_linux_nat_target the_arm_linux_nat_target;
112 /* Get the whole floating point state of the process and store it
116 fetch_fpregs (struct regcache *regcache)
119 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
121 /* Get the thread id for the ptrace call. */
122 tid = ptid_get_lwp (regcache->ptid ());
124 /* Read the floating point state. */
125 if (have_ptrace_getregset == TRIBOOL_TRUE)
130 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
132 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
135 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
138 perror_with_name (_("Unable to fetch the floating point registers."));
141 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
142 fp + NWFPE_FPSR_OFFSET);
144 /* Fetch the floating point registers. */
145 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
146 supply_nwfpe_register (regcache, regno, fp);
149 /* Save the whole floating point state of the process using
150 the contents from regcache. */
153 store_fpregs (const struct regcache *regcache)
156 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
158 /* Get the thread id for the ptrace call. */
159 tid = ptid_get_lwp (regcache->ptid ());
161 /* Read the floating point state. */
162 if (have_ptrace_getregset == TRIBOOL_TRUE)
164 elf_fpregset_t fpregs;
167 iov.iov_base = &fpregs;
168 iov.iov_len = sizeof (fpregs);
170 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
173 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
176 perror_with_name (_("Unable to fetch the floating point registers."));
179 if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
180 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
182 /* Store the floating point registers. */
183 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
184 if (REG_VALID == regcache_register_status (regcache, regno))
185 collect_nwfpe_register (regcache, regno, fp);
187 if (have_ptrace_getregset == TRIBOOL_TRUE)
192 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
194 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
197 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
200 perror_with_name (_("Unable to store floating point registers."));
203 /* Fetch all general registers of the process and store into
207 fetch_regs (struct regcache *regcache)
212 /* Get the thread id for the ptrace call. */
213 tid = ptid_get_lwp (regcache->ptid ());
215 if (have_ptrace_getregset == TRIBOOL_TRUE)
219 iov.iov_base = ®s;
220 iov.iov_len = sizeof (regs);
222 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
225 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
228 perror_with_name (_("Unable to fetch general registers."));
230 aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, arm_apcs_32);
234 store_regs (const struct regcache *regcache)
239 /* Get the thread id for the ptrace call. */
240 tid = ptid_get_lwp (regcache->ptid ());
242 /* Fetch the general registers. */
243 if (have_ptrace_getregset == TRIBOOL_TRUE)
247 iov.iov_base = ®s;
248 iov.iov_len = sizeof (regs);
250 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
253 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
256 perror_with_name (_("Unable to fetch general registers."));
258 aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, arm_apcs_32);
260 if (have_ptrace_getregset == TRIBOOL_TRUE)
264 iov.iov_base = ®s;
265 iov.iov_len = sizeof (regs);
267 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
270 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
273 perror_with_name (_("Unable to store general registers."));
276 /* Fetch all WMMX registers of the process and store into
279 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
282 fetch_wmmx_regs (struct regcache *regcache)
284 char regbuf[IWMMXT_REGS_SIZE];
287 /* Get the thread id for the ptrace call. */
288 tid = ptid_get_lwp (regcache->ptid ());
290 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
292 perror_with_name (_("Unable to fetch WMMX registers."));
294 for (regno = 0; regno < 16; regno++)
295 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
298 for (regno = 0; regno < 2; regno++)
299 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
300 ®buf[16 * 8 + regno * 4]);
302 for (regno = 0; regno < 4; regno++)
303 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
304 ®buf[16 * 8 + 2 * 4 + regno * 4]);
308 store_wmmx_regs (const struct regcache *regcache)
310 char regbuf[IWMMXT_REGS_SIZE];
313 /* Get the thread id for the ptrace call. */
314 tid = ptid_get_lwp (regcache->ptid ());
316 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
318 perror_with_name (_("Unable to fetch WMMX registers."));
320 for (regno = 0; regno < 16; regno++)
321 if (REG_VALID == regcache_register_status (regcache,
322 regno + ARM_WR0_REGNUM))
323 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
326 for (regno = 0; regno < 2; regno++)
327 if (REG_VALID == regcache_register_status (regcache,
328 regno + ARM_WCSSF_REGNUM))
329 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
330 ®buf[16 * 8 + regno * 4]);
332 for (regno = 0; regno < 4; regno++)
333 if (REG_VALID == regcache_register_status (regcache,
334 regno + ARM_WCGR0_REGNUM))
335 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
336 ®buf[16 * 8 + 2 * 4 + regno * 4]);
338 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
341 perror_with_name (_("Unable to store WMMX registers."));
345 fetch_vfp_regs (struct regcache *regcache)
347 gdb_byte regbuf[VFP_REGS_SIZE];
349 struct gdbarch *gdbarch = regcache->arch ();
350 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
352 /* Get the thread id for the ptrace call. */
353 tid = ptid_get_lwp (regcache->ptid ());
355 if (have_ptrace_getregset == TRIBOOL_TRUE)
359 iov.iov_base = regbuf;
360 iov.iov_len = VFP_REGS_SIZE;
361 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
364 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
367 perror_with_name (_("Unable to fetch VFP registers."));
369 aarch32_vfp_regcache_supply (regcache, regbuf,
370 tdep->vfp_register_count);
374 store_vfp_regs (const struct regcache *regcache)
376 gdb_byte regbuf[VFP_REGS_SIZE];
378 struct gdbarch *gdbarch = regcache->arch ();
379 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
381 /* Get the thread id for the ptrace call. */
382 tid = ptid_get_lwp (regcache->ptid ());
384 if (have_ptrace_getregset == TRIBOOL_TRUE)
388 iov.iov_base = regbuf;
389 iov.iov_len = VFP_REGS_SIZE;
390 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
393 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
396 perror_with_name (_("Unable to fetch VFP registers (for update)."));
398 aarch32_vfp_regcache_collect (regcache, regbuf,
399 tdep->vfp_register_count);
401 if (have_ptrace_getregset == TRIBOOL_TRUE)
405 iov.iov_base = regbuf;
406 iov.iov_len = VFP_REGS_SIZE;
407 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iov);
410 ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
413 perror_with_name (_("Unable to store VFP registers."));
416 /* Fetch registers from the child process. Fetch all registers if
417 regno == -1, otherwise fetch all general registers or all floating
418 point registers depending upon the value of regno. */
421 arm_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
423 struct gdbarch *gdbarch = regcache->arch ();
424 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
428 fetch_regs (regcache);
429 if (tdep->have_wmmx_registers)
430 fetch_wmmx_regs (regcache);
431 if (tdep->vfp_register_count > 0)
432 fetch_vfp_regs (regcache);
433 if (tdep->have_fpa_registers)
434 fetch_fpregs (regcache);
438 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
439 fetch_regs (regcache);
440 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
441 fetch_fpregs (regcache);
442 else if (tdep->have_wmmx_registers
443 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
444 fetch_wmmx_regs (regcache);
445 else if (tdep->vfp_register_count > 0
446 && regno >= ARM_D0_REGNUM
447 && (regno < ARM_D0_REGNUM + tdep->vfp_register_count
448 || regno == ARM_FPSCR_REGNUM))
449 fetch_vfp_regs (regcache);
453 /* Store registers back into the inferior. Store all registers if
454 regno == -1, otherwise store all general registers or all floating
455 point registers depending upon the value of regno. */
458 arm_linux_nat_target::store_registers (struct regcache *regcache, int regno)
460 struct gdbarch *gdbarch = regcache->arch ();
461 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
465 store_regs (regcache);
466 if (tdep->have_wmmx_registers)
467 store_wmmx_regs (regcache);
468 if (tdep->vfp_register_count > 0)
469 store_vfp_regs (regcache);
470 if (tdep->have_fpa_registers)
471 store_fpregs (regcache);
475 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
476 store_regs (regcache);
477 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
478 store_fpregs (regcache);
479 else if (tdep->have_wmmx_registers
480 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
481 store_wmmx_regs (regcache);
482 else if (tdep->vfp_register_count > 0
483 && regno >= ARM_D0_REGNUM
484 && (regno < ARM_D0_REGNUM + tdep->vfp_register_count
485 || regno == ARM_FPSCR_REGNUM))
486 store_vfp_regs (regcache);
490 /* Wrapper functions for the standard regset handling, used by
494 fill_gregset (const struct regcache *regcache,
495 gdb_gregset_t *gregsetp, int regno)
497 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
501 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
503 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
507 fill_fpregset (const struct regcache *regcache,
508 gdb_fpregset_t *fpregsetp, int regno)
510 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
513 /* Fill GDB's register array with the floating-point register values
517 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
519 arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
522 /* Fetch the thread-local storage pointer for libthread_db. */
525 ps_get_thread_area (struct ps_prochandle *ph,
526 lwpid_t lwpid, int idx, void **base)
528 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
531 /* IDX is the bias from the thread pointer to the beginning of the
532 thread descriptor. It has to be subtracted due to implementation
533 quirks in libthread_db. */
534 *base = (void *) ((char *)*base - idx);
539 const struct target_desc *
540 arm_linux_nat_target::read_description ()
542 CORE_ADDR arm_hwcap = 0;
544 if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
546 elf_gregset_t gpregs;
548 int tid = ptid_get_lwp (inferior_ptid);
550 iov.iov_base = &gpregs;
551 iov.iov_len = sizeof (gpregs);
553 /* Check if PTRACE_GETREGSET works. */
554 if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) < 0)
555 have_ptrace_getregset = TRIBOOL_FALSE;
557 have_ptrace_getregset = TRIBOOL_TRUE;
560 if (target_auxv_search (this, AT_HWCAP, &arm_hwcap) != 1)
562 return this->beneath->read_description ();
565 if (arm_hwcap & HWCAP_IWMMXT)
566 return tdesc_arm_with_iwmmxt;
568 if (arm_hwcap & HWCAP_VFP)
572 const struct target_desc * result = NULL;
574 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
575 Neon with VFPv3-D32. */
576 if (arm_hwcap & HWCAP_NEON)
577 result = tdesc_arm_with_neon;
578 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
579 result = tdesc_arm_with_vfpv3;
581 result = tdesc_arm_with_vfpv2;
583 /* Now make sure that the kernel supports reading these
584 registers. Support was added in 2.6.30. */
585 pid = ptid_get_lwp (inferior_ptid);
587 buf = (char *) alloca (VFP_REGS_SIZE);
588 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
595 return this->beneath->read_description ();
598 /* Information describing the hardware breakpoint capabilities. */
599 struct arm_linux_hwbp_cap
602 gdb_byte max_wp_length;
607 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
608 assume a maximum number of supported break-/watchpoints. */
612 /* Get hold of the Hardware Breakpoint information for the target we are
613 attached to. Returns NULL if the kernel doesn't support Hardware
614 breakpoints at all, or a pointer to the information structure. */
615 static const struct arm_linux_hwbp_cap *
616 arm_linux_get_hwbp_cap (void)
618 /* The info structure we return. */
619 static struct arm_linux_hwbp_cap info;
621 /* Is INFO in a good state? -1 means that no attempt has been made to
622 initialize INFO; 0 means an attempt has been made, but it failed; 1
623 means INFO is in an initialized state. */
624 static int available = -1;
631 tid = ptid_get_lwp (inferior_ptid);
632 if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
636 info.arch = (gdb_byte)((val >> 24) & 0xff);
637 info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
638 info.wp_count = (gdb_byte)((val >> 8) & 0xff);
639 info.bp_count = (gdb_byte)(val & 0xff);
641 if (info.wp_count > MAX_WPTS)
643 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
644 supports %d"), MAX_WPTS, info.wp_count);
645 info.wp_count = MAX_WPTS;
648 if (info.bp_count > MAX_BPTS)
650 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
651 supports %d"), MAX_BPTS, info.bp_count);
652 info.bp_count = MAX_BPTS;
654 available = (info.arch != 0);
658 return available == 1 ? &info : NULL;
661 /* How many hardware breakpoints are available? */
663 arm_linux_get_hw_breakpoint_count (void)
665 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
666 return cap != NULL ? cap->bp_count : 0;
669 /* How many hardware watchpoints are available? */
671 arm_linux_get_hw_watchpoint_count (void)
673 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
674 return cap != NULL ? cap->wp_count : 0;
677 /* Have we got a free break-/watch-point available for use? Returns -1 if
678 there is not an appropriate resource available, otherwise returns 1. */
680 arm_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
683 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
684 || type == bp_access_watchpoint || type == bp_watchpoint)
686 int count = arm_linux_get_hw_watchpoint_count ();
690 else if (cnt + ot > count)
693 else if (type == bp_hardware_breakpoint)
695 int count = arm_linux_get_hw_breakpoint_count ();
699 else if (cnt > count)
708 /* Enum describing the different types of ARM hardware break-/watch-points. */
717 /* Type describing an ARM Hardware Breakpoint Control register value. */
718 typedef unsigned int arm_hwbp_control_t;
720 /* Structure used to keep track of hardware break-/watch-points. */
721 struct arm_linux_hw_breakpoint
723 /* Address to break on, or being watched. */
724 unsigned int address;
725 /* Control register for break-/watch- point. */
726 arm_hwbp_control_t control;
729 /* Structure containing arrays of per process hardware break-/watchpoints
730 for caching address and control information.
732 The Linux ptrace interface to hardware break-/watch-points presents the
733 values in a vector centred around 0 (which is used fo generic information).
734 Positive indicies refer to breakpoint addresses/control registers, negative
735 indices to watchpoint addresses/control registers.
737 The Linux vector is indexed as follows:
738 -((i << 1) + 2): Control register for watchpoint i.
739 -((i << 1) + 1): Address register for watchpoint i.
740 0: Information register.
741 ((i << 1) + 1): Address register for breakpoint i.
742 ((i << 1) + 2): Control register for breakpoint i.
744 This structure is used as a per-thread cache of the state stored by the
745 kernel, so that we don't need to keep calling into the kernel to find a
748 We treat break-/watch-points with their enable bit clear as being deleted.
750 struct arm_linux_debug_reg_state
752 /* Hardware breakpoints for this process. */
753 struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
754 /* Hardware watchpoints for this process. */
755 struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
758 /* Per-process arch-specific data we want to keep. */
759 struct arm_linux_process_info
762 struct arm_linux_process_info *next;
763 /* The process identifier. */
765 /* Hardware break-/watchpoints state information. */
766 struct arm_linux_debug_reg_state state;
770 /* Per-thread arch-specific data we want to keep. */
773 /* Non-zero if our copy differs from what's recorded in the thread. */
774 char bpts_changed[MAX_BPTS];
775 char wpts_changed[MAX_WPTS];
778 static struct arm_linux_process_info *arm_linux_process_list = NULL;
780 /* Find process data for process PID. */
782 static struct arm_linux_process_info *
783 arm_linux_find_process_pid (pid_t pid)
785 struct arm_linux_process_info *proc;
787 for (proc = arm_linux_process_list; proc; proc = proc->next)
788 if (proc->pid == pid)
794 /* Add process data for process PID. Returns newly allocated info
797 static struct arm_linux_process_info *
798 arm_linux_add_process (pid_t pid)
800 struct arm_linux_process_info *proc;
802 proc = XCNEW (struct arm_linux_process_info);
805 proc->next = arm_linux_process_list;
806 arm_linux_process_list = proc;
811 /* Get data specific info for process PID, creating it if necessary.
812 Never returns NULL. */
814 static struct arm_linux_process_info *
815 arm_linux_process_info_get (pid_t pid)
817 struct arm_linux_process_info *proc;
819 proc = arm_linux_find_process_pid (pid);
821 proc = arm_linux_add_process (pid);
826 /* Called whenever GDB is no longer debugging process PID. It deletes
827 data structures that keep track of debug register state. */
830 arm_linux_nat_target::low_forget_process (pid_t pid)
832 struct arm_linux_process_info *proc, **proc_link;
834 proc = arm_linux_process_list;
835 proc_link = &arm_linux_process_list;
839 if (proc->pid == pid)
841 *proc_link = proc->next;
847 proc_link = &proc->next;
852 /* Get hardware break-/watchpoint state for process PID. */
854 static struct arm_linux_debug_reg_state *
855 arm_linux_get_debug_reg_state (pid_t pid)
857 return &arm_linux_process_info_get (pid)->state;
860 /* Initialize an ARM hardware break-/watch-point control register value.
861 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
862 type of break-/watch-point; ENABLE indicates whether the point is enabled.
864 static arm_hwbp_control_t
865 arm_hwbp_control_initialize (unsigned byte_address_select,
866 arm_hwbp_type hwbp_type,
869 gdb_assert ((byte_address_select & ~0xffU) == 0);
870 gdb_assert (hwbp_type != arm_hwbp_break
871 || ((byte_address_select & 0xfU) != 0));
873 return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
876 /* Does the breakpoint control value CONTROL have the enable bit set? */
878 arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
880 return control & 0x1;
883 /* Change a breakpoint control word so that it is in the disabled state. */
884 static arm_hwbp_control_t
885 arm_hwbp_control_disable (arm_hwbp_control_t control)
887 return control & ~0x1;
890 /* Initialise the hardware breakpoint structure P. The breakpoint will be
891 enabled, and will point to the placed address of BP_TGT. */
893 arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
894 struct bp_target_info *bp_tgt,
895 struct arm_linux_hw_breakpoint *p)
898 CORE_ADDR address = bp_tgt->placed_address = bp_tgt->reqstd_address;
900 /* We have to create a mask for the control register which says which bits
901 of the word pointed to by address to break on. */
902 if (arm_pc_is_thumb (gdbarch, address))
913 p->address = (unsigned int) address;
914 p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
917 /* Get the ARM hardware breakpoint type from the TYPE value we're
918 given when asked to set a watchpoint. */
920 arm_linux_get_hwbp_type (enum target_hw_bp_type type)
923 return arm_hwbp_load;
924 else if (type == hw_write)
925 return arm_hwbp_store;
927 return arm_hwbp_access;
930 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
931 to LEN. The type of watchpoint is given in RW. */
933 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len,
934 enum target_hw_bp_type type,
935 struct arm_linux_hw_breakpoint *p)
937 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
940 gdb_assert (cap != NULL);
941 gdb_assert (cap->max_wp_length != 0);
943 mask = (1 << len) - 1;
945 p->address = (unsigned int) addr;
946 p->control = arm_hwbp_control_initialize (mask,
947 arm_linux_get_hwbp_type (type), 1);
950 /* Are two break-/watch-points equal? */
952 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
953 const struct arm_linux_hw_breakpoint *p2)
955 return p1->address == p2->address && p1->control == p2->control;
958 /* Callback to mark a watch-/breakpoint to be updated in all threads of
959 the current process. */
961 struct update_registers_data
968 update_registers_callback (struct lwp_info *lwp, void *arg)
970 struct update_registers_data *data = (struct update_registers_data *) arg;
972 if (lwp->arch_private == NULL)
973 lwp->arch_private = XCNEW (struct arch_lwp_info);
975 /* The actual update is done later just before resuming the lwp,
976 we just mark that the registers need updating. */
978 lwp->arch_private->wpts_changed[data->index] = 1;
980 lwp->arch_private->bpts_changed[data->index] = 1;
982 /* If the lwp isn't stopped, force it to momentarily pause, so
983 we can update its breakpoint registers. */
985 linux_stop_lwp (lwp);
990 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
991 =1) BPT for thread TID. */
993 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt,
999 struct arm_linux_hw_breakpoint* bpts;
1000 struct update_registers_data data;
1002 pid = ptid_get_pid (inferior_ptid);
1003 pid_ptid = pid_to_ptid (pid);
1007 count = arm_linux_get_hw_watchpoint_count ();
1008 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1012 count = arm_linux_get_hw_breakpoint_count ();
1013 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1016 for (i = 0; i < count; ++i)
1017 if (!arm_hwbp_control_is_enabled (bpts[i].control))
1019 data.watch = watchpoint;
1022 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1026 gdb_assert (i != count);
1029 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1030 (WATCHPOINT = 1) BPT for thread TID. */
1032 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt,
1038 struct arm_linux_hw_breakpoint* bpts;
1039 struct update_registers_data data;
1041 pid = ptid_get_pid (inferior_ptid);
1042 pid_ptid = pid_to_ptid (pid);
1046 count = arm_linux_get_hw_watchpoint_count ();
1047 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1051 count = arm_linux_get_hw_breakpoint_count ();
1052 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1055 for (i = 0; i < count; ++i)
1056 if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
1058 data.watch = watchpoint;
1060 bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
1061 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1065 gdb_assert (i != count);
1068 /* Insert a Hardware breakpoint. */
1070 arm_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
1071 struct bp_target_info *bp_tgt)
1073 struct lwp_info *lp;
1074 struct arm_linux_hw_breakpoint p;
1076 if (arm_linux_get_hw_breakpoint_count () == 0)
1079 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1081 arm_linux_insert_hw_breakpoint1 (&p, 0);
1086 /* Remove a hardware breakpoint. */
1088 arm_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
1089 struct bp_target_info *bp_tgt)
1091 struct lwp_info *lp;
1092 struct arm_linux_hw_breakpoint p;
1094 if (arm_linux_get_hw_breakpoint_count () == 0)
1097 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1099 arm_linux_remove_hw_breakpoint1 (&p, 0);
1104 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1107 arm_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
1109 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1110 CORE_ADDR max_wp_length, aligned_addr;
1112 /* Can not set watchpoints for zero or negative lengths. */
1116 /* Need to be able to use the ptrace interface. */
1117 if (cap == NULL || cap->wp_count == 0)
1120 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1121 range covered by a watchpoint. */
1122 max_wp_length = (CORE_ADDR)cap->max_wp_length;
1123 aligned_addr = addr & ~(max_wp_length - 1);
1125 if (aligned_addr + max_wp_length < addr + len)
1128 /* The current ptrace interface can only handle watchpoints that are a
1130 if ((len & (len - 1)) != 0)
1133 /* All tests passed so we must be able to set a watchpoint. */
1137 /* Insert a Hardware breakpoint. */
1139 arm_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
1140 enum target_hw_bp_type rw,
1141 struct expression *cond)
1143 struct lwp_info *lp;
1144 struct arm_linux_hw_breakpoint p;
1146 if (arm_linux_get_hw_watchpoint_count () == 0)
1149 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1151 arm_linux_insert_hw_breakpoint1 (&p, 1);
1156 /* Remove a hardware breakpoint. */
1158 arm_linux_nat_target::remove_watchpoint (CORE_ADDR addr,
1159 int len, enum target_hw_bp_type rw,
1160 struct expression *cond)
1162 struct lwp_info *lp;
1163 struct arm_linux_hw_breakpoint p;
1165 if (arm_linux_get_hw_watchpoint_count () == 0)
1168 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1170 arm_linux_remove_hw_breakpoint1 (&p, 1);
1175 /* What was the data address the target was stopped on accessing. */
1177 arm_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
1182 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
1185 /* This must be a hardware breakpoint. */
1186 if (siginfo.si_signo != SIGTRAP
1187 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1190 /* We must be able to set hardware watchpoints. */
1191 if (arm_linux_get_hw_watchpoint_count () == 0)
1194 slot = siginfo.si_errno;
1196 /* If we are in a positive slot then we're looking at a breakpoint and not
1201 *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
1205 /* Has the target been stopped by hitting a watchpoint? */
1207 arm_linux_nat_target::stopped_by_watchpoint ()
1210 return stopped_data_address (&addr);
1214 arm_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
1218 return start <= addr && start + length - 1 >= addr;
1221 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1222 in the parent thread to the child thread. */
1224 arm_linux_nat_target::low_new_thread (struct lwp_info *lp)
1227 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
1229 /* Mark that all the hardware breakpoint/watchpoint register pairs
1230 for this thread need to be initialized. */
1232 for (i = 0; i < MAX_BPTS; i++)
1234 info->bpts_changed[i] = 1;
1235 info->wpts_changed[i] = 1;
1238 lp->arch_private = info;
1241 /* Function to call when a thread is being deleted. */
1244 arm_linux_nat_target::low_delete_thread (struct arch_lwp_info *arch_lwp)
1249 /* Called when resuming a thread.
1250 The hardware debug registers are updated when there is any change. */
1253 arm_linux_nat_target::low_prepare_to_resume (struct lwp_info *lwp)
1256 struct arm_linux_hw_breakpoint *bpts, *wpts;
1257 struct arch_lwp_info *arm_lwp_info = lwp->arch_private;
1259 pid = ptid_get_lwp (lwp->ptid);
1260 bpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->bpts;
1261 wpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->wpts;
1263 /* NULL means this is the main thread still going through the shell,
1264 or, no watchpoint has been set yet. In that case, there's
1266 if (arm_lwp_info == NULL)
1269 for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
1270 if (arm_lwp_info->bpts_changed[i])
1273 if (arm_hwbp_control_is_enabled (bpts[i].control))
1274 if (ptrace (PTRACE_SETHBPREGS, pid,
1275 (PTRACE_TYPE_ARG3) ((i << 1) + 1), &bpts[i].address) < 0)
1276 perror_with_name (_("Unexpected error setting breakpoint"));
1278 if (bpts[i].control != 0)
1279 if (ptrace (PTRACE_SETHBPREGS, pid,
1280 (PTRACE_TYPE_ARG3) ((i << 1) + 2), &bpts[i].control) < 0)
1281 perror_with_name (_("Unexpected error setting breakpoint"));
1283 arm_lwp_info->bpts_changed[i] = 0;
1286 for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
1287 if (arm_lwp_info->wpts_changed[i])
1290 if (arm_hwbp_control_is_enabled (wpts[i].control))
1291 if (ptrace (PTRACE_SETHBPREGS, pid,
1292 (PTRACE_TYPE_ARG3) -((i << 1) + 1), &wpts[i].address) < 0)
1293 perror_with_name (_("Unexpected error setting watchpoint"));
1295 if (wpts[i].control != 0)
1296 if (ptrace (PTRACE_SETHBPREGS, pid,
1297 (PTRACE_TYPE_ARG3) -((i << 1) + 2), &wpts[i].control) < 0)
1298 perror_with_name (_("Unexpected error setting watchpoint"));
1300 arm_lwp_info->wpts_changed[i] = 0;
1304 /* linux_nat_new_fork hook. */
1307 arm_linux_nat_target::low_new_fork (struct lwp_info *parent, pid_t child_pid)
1310 struct arm_linux_debug_reg_state *parent_state;
1311 struct arm_linux_debug_reg_state *child_state;
1313 /* NULL means no watchpoint has ever been set in the parent. In
1314 that case, there's nothing to do. */
1315 if (parent->arch_private == NULL)
1318 /* GDB core assumes the child inherits the watchpoints/hw
1319 breakpoints of the parent, and will remove them all from the
1320 forked off process. Copy the debug registers mirrors into the
1321 new process so that all breakpoints and watchpoints can be
1322 removed together. */
1324 parent_pid = ptid_get_pid (parent->ptid);
1325 parent_state = arm_linux_get_debug_reg_state (parent_pid);
1326 child_state = arm_linux_get_debug_reg_state (child_pid);
1327 *child_state = *parent_state;
1331 _initialize_arm_linux_nat (void)
1333 /* Register the target. */
1334 linux_target = &the_arm_linux_nat_target;
1335 add_inf_child_target (&the_arm_linux_nat_target);