1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999-2024 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/>. */
23 #include "linux-nat.h"
24 #include "target-descriptions.h"
26 #include "observable.h"
27 #include "gdbthread.h"
29 #include "aarch32-tdep.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"
41 #include "linux-tdep.h"
43 /* Prototypes for supply_gregset etc. */
46 /* Defines ps_err_e, struct ps_prochandle. */
47 #include "gdb_proc_service.h"
49 #ifndef PTRACE_GET_THREAD_AREA
50 #define PTRACE_GET_THREAD_AREA 22
53 #ifndef PTRACE_GETWMMXREGS
54 #define PTRACE_GETWMMXREGS 18
55 #define PTRACE_SETWMMXREGS 19
58 #ifndef PTRACE_GETVFPREGS
59 #define PTRACE_GETVFPREGS 27
60 #define PTRACE_SETVFPREGS 28
63 #ifndef PTRACE_GETHBPREGS
64 #define PTRACE_GETHBPREGS 29
65 #define PTRACE_SETHBPREGS 30
68 class arm_linux_nat_target final
: public linux_nat_target
71 /* Add our register access methods. */
72 void fetch_registers (struct regcache
*, int) override
;
73 void store_registers (struct regcache
*, int) override
;
75 /* Add our hardware breakpoint and watchpoint implementation. */
76 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
78 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
80 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
82 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
84 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
85 struct expression
*) override
;
87 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
88 struct expression
*) override
;
89 bool stopped_by_watchpoint () override
;
91 bool stopped_data_address (CORE_ADDR
*) override
;
93 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
95 const struct target_desc
*read_description () override
;
97 /* Override linux_nat_target low methods. */
99 /* Handle thread creation and exit. */
100 void low_new_thread (struct lwp_info
*lp
) override
;
101 void low_delete_thread (struct arch_lwp_info
*lp
) override
;
102 void low_prepare_to_resume (struct lwp_info
*lp
) override
;
104 /* Handle process creation and exit. */
105 void low_new_fork (struct lwp_info
*parent
, pid_t child_pid
) override
;
106 void low_init_process (pid_t 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
= regcache
->ptid ().lwp ();
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 (ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
143 /* Fetch the floating point registers. */
144 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
145 supply_nwfpe_register (regcache
, regno
, fp
);
148 /* Save the whole floating point state of the process using
149 the contents from regcache. */
152 store_fpregs (const struct regcache
*regcache
)
155 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
157 /* Get the thread id for the ptrace call. */
158 tid
= regcache
->ptid ().lwp ();
160 /* Read the floating point state. */
161 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
163 elf_fpregset_t fpregs
;
166 iov
.iov_base
= &fpregs
;
167 iov
.iov_len
= sizeof (fpregs
);
169 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iov
);
172 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
175 perror_with_name (_("Unable to fetch the floating point registers"));
178 if (REG_VALID
== regcache
->get_register_status (ARM_FPS_REGNUM
))
179 regcache
->raw_collect (ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
181 /* Store the floating point registers. */
182 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
183 if (REG_VALID
== regcache
->get_register_status (regno
))
184 collect_nwfpe_register (regcache
, regno
, fp
);
186 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
191 iov
.iov_len
= ARM_LINUX_SIZEOF_NWFPE
;
193 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_FPREGSET
, &iov
);
196 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
199 perror_with_name (_("Unable to store floating point registers"));
202 /* Fetch all general registers of the process and store into
206 fetch_regs (struct regcache
*regcache
)
211 /* Get the thread id for the ptrace call. */
212 tid
= regcache
->ptid ().lwp ();
214 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
218 iov
.iov_base
= ®s
;
219 iov
.iov_len
= sizeof (regs
);
221 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
);
224 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
227 perror_with_name (_("Unable to fetch general registers"));
229 aarch32_gp_regcache_supply (regcache
, (uint32_t *) regs
, arm_apcs_32
);
233 store_regs (const struct regcache
*regcache
)
238 /* Get the thread id for the ptrace call. */
239 tid
= regcache
->ptid ().lwp ();
241 /* Fetch the general registers. */
242 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
246 iov
.iov_base
= ®s
;
247 iov
.iov_len
= sizeof (regs
);
249 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
);
252 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
255 perror_with_name (_("Unable to fetch general registers"));
257 aarch32_gp_regcache_collect (regcache
, (uint32_t *) regs
, arm_apcs_32
);
259 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
263 iov
.iov_base
= ®s
;
264 iov
.iov_len
= sizeof (regs
);
266 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, &iov
);
269 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
272 perror_with_name (_("Unable to store general registers"));
275 /* Fetch all WMMX registers of the process and store into
279 fetch_wmmx_regs (struct regcache
*regcache
)
281 char regbuf
[IWMMXT_REGS_SIZE
];
284 /* Get the thread id for the ptrace call. */
285 tid
= regcache
->ptid ().lwp ();
287 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
289 perror_with_name (_("Unable to fetch WMMX registers"));
291 for (regno
= 0; regno
< 16; regno
++)
292 regcache
->raw_supply (regno
+ ARM_WR0_REGNUM
, ®buf
[regno
* 8]);
294 for (regno
= 0; regno
< 2; regno
++)
295 regcache
->raw_supply (regno
+ ARM_WCSSF_REGNUM
,
296 ®buf
[16 * 8 + regno
* 4]);
298 for (regno
= 0; regno
< 4; regno
++)
299 regcache
->raw_supply (regno
+ ARM_WCGR0_REGNUM
,
300 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
304 store_wmmx_regs (const struct regcache
*regcache
)
306 char regbuf
[IWMMXT_REGS_SIZE
];
309 /* Get the thread id for the ptrace call. */
310 tid
= regcache
->ptid ().lwp ();
312 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
314 perror_with_name (_("Unable to fetch WMMX registers"));
316 for (regno
= 0; regno
< 16; regno
++)
317 if (REG_VALID
== regcache
->get_register_status (regno
+ ARM_WR0_REGNUM
))
318 regcache
->raw_collect (regno
+ ARM_WR0_REGNUM
, ®buf
[regno
* 8]);
320 for (regno
= 0; regno
< 2; regno
++)
321 if (REG_VALID
== regcache
->get_register_status (regno
+ ARM_WCSSF_REGNUM
))
322 regcache
->raw_collect (regno
+ ARM_WCSSF_REGNUM
,
323 ®buf
[16 * 8 + regno
* 4]);
325 for (regno
= 0; regno
< 4; regno
++)
326 if (REG_VALID
== regcache
->get_register_status (regno
+ ARM_WCGR0_REGNUM
))
327 regcache
->raw_collect (regno
+ ARM_WCGR0_REGNUM
,
328 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
330 ret
= ptrace (PTRACE_SETWMMXREGS
, tid
, 0, regbuf
);
333 perror_with_name (_("Unable to store WMMX registers"));
337 fetch_vfp_regs (struct regcache
*regcache
)
339 gdb_byte regbuf
[ARM_VFP3_REGS_SIZE
];
341 struct gdbarch
*gdbarch
= regcache
->arch ();
342 arm_gdbarch_tdep
*tdep
= gdbarch_tdep
<arm_gdbarch_tdep
> (gdbarch
);
344 /* Get the thread id for the ptrace call. */
345 tid
= regcache
->ptid ().lwp ();
347 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
351 iov
.iov_base
= regbuf
;
352 iov
.iov_len
= ARM_VFP3_REGS_SIZE
;
353 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
356 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
359 perror_with_name (_("Unable to fetch VFP registers"));
361 aarch32_vfp_regcache_supply (regcache
, regbuf
,
362 tdep
->vfp_register_count
);
366 store_vfp_regs (const struct regcache
*regcache
)
368 gdb_byte regbuf
[ARM_VFP3_REGS_SIZE
];
370 struct gdbarch
*gdbarch
= regcache
->arch ();
371 arm_gdbarch_tdep
*tdep
= gdbarch_tdep
<arm_gdbarch_tdep
> (gdbarch
);
373 /* Get the thread id for the ptrace call. */
374 tid
= regcache
->ptid ().lwp ();
376 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
380 iov
.iov_base
= regbuf
;
381 iov
.iov_len
= ARM_VFP3_REGS_SIZE
;
382 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
385 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
388 perror_with_name (_("Unable to fetch VFP registers (for update)"));
390 aarch32_vfp_regcache_collect (regcache
, regbuf
,
391 tdep
->vfp_register_count
);
393 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
397 iov
.iov_base
= regbuf
;
398 iov
.iov_len
= ARM_VFP3_REGS_SIZE
;
399 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iov
);
402 ret
= ptrace (PTRACE_SETVFPREGS
, tid
, 0, regbuf
);
405 perror_with_name (_("Unable to store VFP registers"));
408 /* Fetch registers from the child process. Fetch all registers if
409 regno == -1, otherwise fetch all general registers or all floating
410 point registers depending upon the value of regno. */
413 arm_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
415 struct gdbarch
*gdbarch
= regcache
->arch ();
416 arm_gdbarch_tdep
*tdep
= gdbarch_tdep
<arm_gdbarch_tdep
> (gdbarch
);
420 fetch_regs (regcache
);
421 if (tdep
->have_wmmx_registers
)
422 fetch_wmmx_regs (regcache
);
423 if (tdep
->vfp_register_count
> 0)
424 fetch_vfp_regs (regcache
);
425 if (tdep
->have_fpa_registers
)
426 fetch_fpregs (regcache
);
430 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
431 fetch_regs (regcache
);
432 else if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
433 fetch_fpregs (regcache
);
434 else if (tdep
->have_wmmx_registers
435 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
436 fetch_wmmx_regs (regcache
);
437 else if (tdep
->vfp_register_count
> 0
438 && regno
>= ARM_D0_REGNUM
439 && (regno
< ARM_D0_REGNUM
+ tdep
->vfp_register_count
440 || regno
== ARM_FPSCR_REGNUM
))
441 fetch_vfp_regs (regcache
);
445 /* Store registers back into the inferior. Store all registers if
446 regno == -1, otherwise store all general registers or all floating
447 point registers depending upon the value of regno. */
450 arm_linux_nat_target::store_registers (struct regcache
*regcache
, int regno
)
452 struct gdbarch
*gdbarch
= regcache
->arch ();
453 arm_gdbarch_tdep
*tdep
= gdbarch_tdep
<arm_gdbarch_tdep
> (gdbarch
);
457 store_regs (regcache
);
458 if (tdep
->have_wmmx_registers
)
459 store_wmmx_regs (regcache
);
460 if (tdep
->vfp_register_count
> 0)
461 store_vfp_regs (regcache
);
462 if (tdep
->have_fpa_registers
)
463 store_fpregs (regcache
);
467 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
468 store_regs (regcache
);
469 else if ((regno
>= ARM_F0_REGNUM
) && (regno
<= ARM_FPS_REGNUM
))
470 store_fpregs (regcache
);
471 else if (tdep
->have_wmmx_registers
472 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
473 store_wmmx_regs (regcache
);
474 else if (tdep
->vfp_register_count
> 0
475 && regno
>= ARM_D0_REGNUM
476 && (regno
< ARM_D0_REGNUM
+ tdep
->vfp_register_count
477 || regno
== ARM_FPSCR_REGNUM
))
478 store_vfp_regs (regcache
);
482 /* Wrapper functions for the standard regset handling, used by
486 fill_gregset (const struct regcache
*regcache
,
487 gdb_gregset_t
*gregsetp
, int regno
)
489 arm_linux_collect_gregset (NULL
, regcache
, regno
, gregsetp
, 0);
493 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
495 arm_linux_supply_gregset (NULL
, regcache
, -1, gregsetp
, 0);
499 fill_fpregset (const struct regcache
*regcache
,
500 gdb_fpregset_t
*fpregsetp
, int regno
)
502 arm_linux_collect_nwfpe (NULL
, regcache
, regno
, fpregsetp
, 0);
505 /* Fill GDB's register array with the floating-point register values
509 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
511 arm_linux_supply_nwfpe (NULL
, regcache
, -1, fpregsetp
, 0);
514 /* Fetch the thread-local storage pointer for libthread_db. */
517 ps_get_thread_area (struct ps_prochandle
*ph
,
518 lwpid_t lwpid
, int idx
, void **base
)
520 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
523 /* IDX is the bias from the thread pointer to the beginning of the
524 thread descriptor. It has to be subtracted due to implementation
525 quirks in libthread_db. */
526 *base
= (void *) ((char *)*base
- idx
);
531 const struct target_desc
*
532 arm_linux_nat_target::read_description ()
534 if (inferior_ptid
== null_ptid
)
535 return this->beneath ()->read_description ();
537 CORE_ADDR arm_hwcap
= linux_get_hwcap ();
539 if (have_ptrace_getregset
== TRIBOOL_UNKNOWN
)
541 elf_gregset_t gpregs
;
543 int tid
= inferior_ptid
.pid ();
545 iov
.iov_base
= &gpregs
;
546 iov
.iov_len
= sizeof (gpregs
);
548 /* Check if PTRACE_GETREGSET works. */
549 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
) < 0)
550 have_ptrace_getregset
= TRIBOOL_FALSE
;
552 have_ptrace_getregset
= TRIBOOL_TRUE
;
555 if (arm_hwcap
& HWCAP_IWMMXT
)
556 return arm_read_description (ARM_FP_TYPE_IWMMXT
, false);
558 if (arm_hwcap
& HWCAP_VFP
)
560 /* Make sure that the kernel supports reading VFP registers. Support was
562 int pid
= inferior_ptid
.pid ();
564 char *buf
= (char *) alloca (ARM_VFP3_REGS_SIZE
);
565 if (ptrace (PTRACE_GETVFPREGS
, pid
, 0, buf
) < 0 && errno
== EIO
)
568 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
569 Neon with VFPv3-D32. */
570 if (arm_hwcap
& HWCAP_NEON
)
571 return aarch32_read_description (false);
572 else if ((arm_hwcap
& (HWCAP_VFPv3
| HWCAP_VFPv3D16
)) == HWCAP_VFPv3
)
573 return arm_read_description (ARM_FP_TYPE_VFPV3
, false);
575 return arm_read_description (ARM_FP_TYPE_VFPV2
, false);
578 return this->beneath ()->read_description ();
581 /* Information describing the hardware breakpoint capabilities. */
582 struct arm_linux_hwbp_cap
585 gdb_byte max_wp_length
;
590 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
591 assume a maximum number of supported break-/watchpoints. */
595 /* Get hold of the Hardware Breakpoint information for the target we are
596 attached to. Returns NULL if the kernel doesn't support Hardware
597 breakpoints at all, or a pointer to the information structure. */
598 static const struct arm_linux_hwbp_cap
*
599 arm_linux_get_hwbp_cap (void)
601 /* The info structure we return. */
602 static struct arm_linux_hwbp_cap info
;
604 /* Is INFO in a good state? -1 means that no attempt has been made to
605 initialize INFO; 0 means an attempt has been made, but it failed; 1
606 means INFO is in an initialized state. */
607 static int available
= -1;
614 tid
= inferior_ptid
.lwp ();
615 if (ptrace (PTRACE_GETHBPREGS
, tid
, 0, &val
) < 0)
619 info
.arch
= (gdb_byte
)((val
>> 24) & 0xff);
620 info
.max_wp_length
= (gdb_byte
)((val
>> 16) & 0xff);
621 info
.wp_count
= (gdb_byte
)((val
>> 8) & 0xff);
622 info
.bp_count
= (gdb_byte
)(val
& 0xff);
624 if (info
.wp_count
> MAX_WPTS
)
626 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
627 supports %d"), MAX_WPTS
, info
.wp_count
);
628 info
.wp_count
= MAX_WPTS
;
631 if (info
.bp_count
> MAX_BPTS
)
633 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
634 supports %d"), MAX_BPTS
, info
.bp_count
);
635 info
.bp_count
= MAX_BPTS
;
637 available
= (info
.arch
!= 0);
641 return available
== 1 ? &info
: NULL
;
644 /* How many hardware breakpoints are available? */
646 arm_linux_get_hw_breakpoint_count (void)
648 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
649 return cap
!= NULL
? cap
->bp_count
: 0;
652 /* How many hardware watchpoints are available? */
654 arm_linux_get_hw_watchpoint_count (void)
656 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
657 return cap
!= NULL
? cap
->wp_count
: 0;
660 /* Have we got a free break-/watch-point available for use? Returns -1 if
661 there is not an appropriate resource available, otherwise returns 1. */
663 arm_linux_nat_target::can_use_hw_breakpoint (enum bptype type
,
666 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
667 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
669 int count
= arm_linux_get_hw_watchpoint_count ();
673 else if (cnt
+ ot
> count
)
676 else if (type
== bp_hardware_breakpoint
)
678 int count
= arm_linux_get_hw_breakpoint_count ();
682 else if (cnt
> count
)
686 gdb_assert_not_reached ("unknown breakpoint type");
691 /* Enum describing the different types of ARM hardware break-/watch-points. */
700 /* Type describing an ARM Hardware Breakpoint Control register value. */
701 typedef unsigned int arm_hwbp_control_t
;
703 /* Structure used to keep track of hardware break-/watch-points. */
704 struct arm_linux_hw_breakpoint
706 /* Address to break on, or being watched. */
707 unsigned int address
;
708 /* Control register for break-/watch- point. */
709 arm_hwbp_control_t control
;
712 /* Structure containing arrays of per process hardware break-/watchpoints
713 for caching address and control information.
715 The Linux ptrace interface to hardware break-/watch-points presents the
716 values in a vector centred around 0 (which is used fo generic information).
717 Positive indicies refer to breakpoint addresses/control registers, negative
718 indices to watchpoint addresses/control registers.
720 The Linux vector is indexed as follows:
721 -((i << 1) + 2): Control register for watchpoint i.
722 -((i << 1) + 1): Address register for watchpoint i.
723 0: Information register.
724 ((i << 1) + 1): Address register for breakpoint i.
725 ((i << 1) + 2): Control register for breakpoint i.
727 This structure is used as a per-thread cache of the state stored by the
728 kernel, so that we don't need to keep calling into the kernel to find a
731 We treat break-/watch-points with their enable bit clear as being deleted.
733 struct arm_linux_debug_reg_state
735 /* Hardware breakpoints for this process. */
736 struct arm_linux_hw_breakpoint bpts
[MAX_BPTS
];
737 /* Hardware watchpoints for this process. */
738 struct arm_linux_hw_breakpoint wpts
[MAX_WPTS
];
741 /* Per-process arch-specific data we want to keep. */
742 struct arm_linux_process_info
745 struct arm_linux_process_info
*next
;
746 /* The process identifier. */
748 /* Hardware break-/watchpoints state information. */
749 struct arm_linux_debug_reg_state state
;
753 /* Per-thread arch-specific data we want to keep. */
756 /* Non-zero if our copy differs from what's recorded in the thread. */
757 char bpts_changed
[MAX_BPTS
];
758 char wpts_changed
[MAX_WPTS
];
761 static struct arm_linux_process_info
*arm_linux_process_list
= NULL
;
763 /* Find process data for process PID. */
765 static struct arm_linux_process_info
*
766 arm_linux_find_process_pid (pid_t pid
)
768 struct arm_linux_process_info
*proc
;
770 for (proc
= arm_linux_process_list
; proc
; proc
= proc
->next
)
771 if (proc
->pid
== pid
)
777 /* Add process data for process PID. Returns newly allocated info
780 static struct arm_linux_process_info
*
781 arm_linux_add_process (pid_t pid
)
783 struct arm_linux_process_info
*proc
;
785 proc
= XCNEW (struct arm_linux_process_info
);
788 proc
->next
= arm_linux_process_list
;
789 arm_linux_process_list
= proc
;
794 /* Get data specific info for process PID, creating it if necessary.
795 Never returns NULL. */
797 static struct arm_linux_process_info
*
798 arm_linux_process_info_get (pid_t pid
)
800 struct arm_linux_process_info
*proc
;
802 proc
= arm_linux_find_process_pid (pid
);
804 proc
= arm_linux_add_process (pid
);
809 /* Implement the "low_init_process" target_ops method. */
812 arm_linux_nat_target::low_init_process (pid_t pid
)
814 /* Set the hardware debug register capacity. This requires the process to be
815 ptrace-stopped, otherwise detection will fail and software watchpoints will
816 be used instead of hardware. If we allow this to be done lazily, we
817 cannot guarantee that it's called when the process is ptrace-stopped, so
819 arm_linux_get_hwbp_cap ();
822 /* Called whenever GDB is no longer debugging process PID. It deletes
823 data structures that keep track of debug register state. */
826 arm_linux_nat_target::low_forget_process (pid_t pid
)
828 struct arm_linux_process_info
*proc
, **proc_link
;
830 proc
= arm_linux_process_list
;
831 proc_link
= &arm_linux_process_list
;
835 if (proc
->pid
== pid
)
837 *proc_link
= proc
->next
;
843 proc_link
= &proc
->next
;
848 /* Get hardware break-/watchpoint state for process PID. */
850 static struct arm_linux_debug_reg_state
*
851 arm_linux_get_debug_reg_state (pid_t pid
)
853 return &arm_linux_process_info_get (pid
)->state
;
856 /* Initialize an ARM hardware break-/watch-point control register value.
857 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
858 type of break-/watch-point; ENABLE indicates whether the point is enabled.
860 static arm_hwbp_control_t
861 arm_hwbp_control_initialize (unsigned byte_address_select
,
862 arm_hwbp_type hwbp_type
,
865 gdb_assert ((byte_address_select
& ~0xffU
) == 0);
866 gdb_assert (hwbp_type
!= arm_hwbp_break
867 || ((byte_address_select
& 0xfU
) != 0));
869 return (byte_address_select
<< 5) | (hwbp_type
<< 3) | (3 << 1) | enable
;
872 /* Does the breakpoint control value CONTROL have the enable bit set? */
874 arm_hwbp_control_is_enabled (arm_hwbp_control_t control
)
876 return control
& 0x1;
879 /* Is the breakpoint control value CONTROL initialized? */
882 arm_hwbp_control_is_initialized (arm_hwbp_control_t control
)
887 /* Change a breakpoint control word so that it is in the disabled state. */
888 static arm_hwbp_control_t
889 arm_hwbp_control_disable (arm_hwbp_control_t control
)
891 return control
& ~0x1;
894 /* Initialise the hardware breakpoint structure P. The breakpoint will be
895 enabled, and will point to the placed address of BP_TGT. */
897 arm_linux_hw_breakpoint_initialize (struct gdbarch
*gdbarch
,
898 struct bp_target_info
*bp_tgt
,
899 struct arm_linux_hw_breakpoint
*p
)
902 CORE_ADDR address
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
904 /* We have to create a mask for the control register which says which bits
905 of the word pointed to by address to break on. */
906 if (arm_pc_is_thumb (gdbarch
, address
))
917 p
->address
= (unsigned int) address
;
918 p
->control
= arm_hwbp_control_initialize (mask
, arm_hwbp_break
, 1);
921 /* Get the ARM hardware breakpoint type from the TYPE value we're
922 given when asked to set a watchpoint. */
924 arm_linux_get_hwbp_type (enum target_hw_bp_type type
)
927 return arm_hwbp_load
;
928 else if (type
== hw_write
)
929 return arm_hwbp_store
;
931 return arm_hwbp_access
;
934 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
935 to LEN. The type of watchpoint is given in RW. */
937 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr
, int len
,
938 enum target_hw_bp_type type
,
939 struct arm_linux_hw_breakpoint
*p
)
941 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
944 gdb_assert (cap
!= NULL
);
945 gdb_assert (cap
->max_wp_length
!= 0);
947 mask
= (1 << len
) - 1;
949 p
->address
= (unsigned int) addr
;
950 p
->control
= arm_hwbp_control_initialize (mask
,
951 arm_linux_get_hwbp_type (type
), 1);
954 /* Are two break-/watch-points equal? */
956 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint
*p1
,
957 const struct arm_linux_hw_breakpoint
*p2
)
959 return p1
->address
== p2
->address
&& p1
->control
== p2
->control
;
962 /* Callback to mark a watch-/breakpoint to be updated in all threads of
963 the current process. */
966 update_registers_callback (struct lwp_info
*lwp
, int watch
, int index
)
968 if (lwp
->arch_private
== NULL
)
969 lwp
->arch_private
= XCNEW (struct arch_lwp_info
);
971 /* The actual update is done later just before resuming the lwp,
972 we just mark that the registers need updating. */
974 lwp
->arch_private
->wpts_changed
[index
] = 1;
976 lwp
->arch_private
->bpts_changed
[index
] = 1;
978 /* If the lwp isn't stopped, force it to momentarily pause, so
979 we can update its breakpoint registers. */
981 linux_stop_lwp (lwp
);
986 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
987 =1) BPT for thread TID. */
989 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
* bpt
,
995 struct arm_linux_hw_breakpoint
* bpts
;
997 pid
= inferior_ptid
.pid ();
998 pid_ptid
= ptid_t (pid
);
1002 count
= arm_linux_get_hw_watchpoint_count ();
1003 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1007 count
= arm_linux_get_hw_breakpoint_count ();
1008 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1011 for (i
= 0; i
< count
; ++i
)
1012 if (!arm_hwbp_control_is_enabled (bpts
[i
].control
))
1015 iterate_over_lwps (pid_ptid
,
1016 [=] (struct lwp_info
*info
)
1018 return update_registers_callback (info
, watchpoint
,
1024 gdb_assert (i
!= count
);
1027 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1028 (WATCHPOINT = 1) BPT for thread TID. */
1030 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
*bpt
,
1036 struct arm_linux_hw_breakpoint
* bpts
;
1038 pid
= inferior_ptid
.pid ();
1039 pid_ptid
= ptid_t (pid
);
1043 count
= arm_linux_get_hw_watchpoint_count ();
1044 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1048 count
= arm_linux_get_hw_breakpoint_count ();
1049 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1052 for (i
= 0; i
< count
; ++i
)
1053 if (arm_linux_hw_breakpoint_equal (bpt
, bpts
+ i
))
1055 bpts
[i
].control
= arm_hwbp_control_disable (bpts
[i
].control
);
1056 iterate_over_lwps (pid_ptid
,
1057 [=] (struct lwp_info
*info
)
1059 return update_registers_callback (info
, watchpoint
,
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 arm_linux_hw_breakpoint p
;
1075 if (arm_linux_get_hw_breakpoint_count () == 0)
1078 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1080 arm_linux_insert_hw_breakpoint1 (&p
, 0);
1085 /* Remove a hardware breakpoint. */
1087 arm_linux_nat_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
1088 struct bp_target_info
*bp_tgt
)
1090 struct arm_linux_hw_breakpoint p
;
1092 if (arm_linux_get_hw_breakpoint_count () == 0)
1095 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1097 arm_linux_remove_hw_breakpoint1 (&p
, 0);
1102 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1105 arm_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
1107 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
1108 CORE_ADDR max_wp_length
, aligned_addr
;
1110 /* Can not set watchpoints for zero or negative lengths. */
1114 /* Need to be able to use the ptrace interface. */
1115 if (cap
== NULL
|| cap
->wp_count
== 0)
1118 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1119 range covered by a watchpoint. */
1120 max_wp_length
= (CORE_ADDR
)cap
->max_wp_length
;
1121 aligned_addr
= addr
& ~(max_wp_length
- 1);
1123 if (aligned_addr
+ max_wp_length
< addr
+ len
)
1126 /* The current ptrace interface can only handle watchpoints that are a
1128 if ((len
& (len
- 1)) != 0)
1131 /* All tests passed so we must be able to set a watchpoint. */
1135 /* Insert a Hardware breakpoint. */
1137 arm_linux_nat_target::insert_watchpoint (CORE_ADDR addr
, int len
,
1138 enum target_hw_bp_type rw
,
1139 struct expression
*cond
)
1141 struct arm_linux_hw_breakpoint p
;
1143 if (arm_linux_get_hw_watchpoint_count () == 0)
1146 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1148 arm_linux_insert_hw_breakpoint1 (&p
, 1);
1153 /* Remove a hardware breakpoint. */
1155 arm_linux_nat_target::remove_watchpoint (CORE_ADDR addr
,
1156 int len
, enum target_hw_bp_type rw
,
1157 struct expression
*cond
)
1159 struct arm_linux_hw_breakpoint p
;
1161 if (arm_linux_get_hw_watchpoint_count () == 0)
1164 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1166 arm_linux_remove_hw_breakpoint1 (&p
, 1);
1171 /* What was the data address the target was stopped on accessing. */
1173 arm_linux_nat_target::stopped_data_address (CORE_ADDR
*addr_p
)
1178 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
1181 /* This must be a hardware breakpoint. */
1182 if (siginfo
.si_signo
!= SIGTRAP
1183 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1186 /* We must be able to set hardware watchpoints. */
1187 if (arm_linux_get_hw_watchpoint_count () == 0)
1190 slot
= siginfo
.si_errno
;
1192 /* If we are in a positive slot then we're looking at a breakpoint and not
1197 *addr_p
= (CORE_ADDR
) (uintptr_t) siginfo
.si_addr
;
1201 /* Has the target been stopped by hitting a watchpoint? */
1203 arm_linux_nat_target::stopped_by_watchpoint ()
1206 return stopped_data_address (&addr
);
1210 arm_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr
,
1214 return start
<= addr
&& start
+ length
- 1 >= addr
;
1217 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1218 in the parent thread to the child thread. */
1220 arm_linux_nat_target::low_new_thread (struct lwp_info
*lp
)
1223 struct arch_lwp_info
*info
= XCNEW (struct arch_lwp_info
);
1225 /* Mark that all the hardware breakpoint/watchpoint register pairs
1226 for this thread need to be initialized. */
1228 for (i
= 0; i
< MAX_BPTS
; i
++)
1230 info
->bpts_changed
[i
] = 1;
1231 info
->wpts_changed
[i
] = 1;
1234 lp
->arch_private
= info
;
1237 /* Function to call when a thread is being deleted. */
1240 arm_linux_nat_target::low_delete_thread (struct arch_lwp_info
*arch_lwp
)
1245 /* For PID, set the address register of hardware breakpoint pair I to
1249 sethbpregs_hwbp_address (int pid
, int i
, unsigned int address
)
1251 PTRACE_TYPE_ARG3 address_reg
= (PTRACE_TYPE_ARG3
) ((i
<< 1) + 1);
1255 if (ptrace (PTRACE_SETHBPREGS
, pid
, address_reg
, &address
) < 0)
1256 perror_with_name (_("Unexpected error updating breakpoint address"));
1259 /* For PID, set the control register of hardware breakpoint pair I to
1263 sethbpregs_hwbp_control (int pid
, int i
, arm_hwbp_control_t control
)
1265 PTRACE_TYPE_ARG3 control_reg
= (PTRACE_TYPE_ARG3
) ((i
<< 1) + 2);
1269 if (ptrace (PTRACE_SETHBPREGS
, pid
, control_reg
, &control
) < 0)
1270 perror_with_name (_("Unexpected error setting breakpoint control"));
1273 /* Called when resuming a thread.
1274 The hardware debug registers are updated when there is any change. */
1277 arm_linux_nat_target::low_prepare_to_resume (struct lwp_info
*lwp
)
1280 struct arm_linux_hw_breakpoint
*bpts
, *wpts
;
1281 struct arch_lwp_info
*arm_lwp_info
= lwp
->arch_private
;
1283 pid
= lwp
->ptid
.lwp ();
1284 bpts
= arm_linux_get_debug_reg_state (lwp
->ptid
.pid ())->bpts
;
1285 wpts
= arm_linux_get_debug_reg_state (lwp
->ptid
.pid ())->wpts
;
1287 /* NULL means this is the main thread still going through the shell,
1288 or, no watchpoint has been set yet. In that case, there's
1290 if (arm_lwp_info
== NULL
)
1293 for (i
= 0; i
< arm_linux_get_hw_breakpoint_count (); i
++)
1294 if (arm_lwp_info
->bpts_changed
[i
])
1296 unsigned int address
= bpts
[i
].address
;
1297 arm_hwbp_control_t control
= bpts
[i
].control
;
1299 if (!arm_hwbp_control_is_initialized (control
))
1301 /* Nothing to do. */
1303 else if (!arm_hwbp_control_is_enabled (control
))
1305 /* Disable hardware breakpoint, just write the control
1307 sethbpregs_hwbp_control (pid
, i
, control
);
1311 /* We used to do here simply:
1312 1. address_reg = address
1313 2. control_reg = control
1314 but the write to address_reg can fail for thumb2 instructions if
1315 the address is not 4-byte aligned.
1317 It's not clear whether this is a kernel bug or not, partly
1318 because PTRACE_SETHBPREGS is undocumented.
1320 The context is that we're using two ptrace calls to set the two
1321 halves of a register pair. For each ptrace call, the kernel must
1322 check the arguments, and return -1 and set errno appropriately if
1323 something is wrong. One of the aspects that needs validation is
1324 whether, in terms of hw_breakpoint_arch_parse, the breakpoint
1325 address matches the breakpoint length. This aspect can only be
1326 checked by looking in both registers, which only makes sense
1327 once a pair is written in full.
1329 The problem is that the kernel checks this aspect after each
1330 ptrace call, and consequently for the first call it may be
1331 checking this aspect using a default or previous value for the
1332 part of the pair not written by the call. A possible fix for
1333 this would be to only check this aspect when writing the
1336 Work around this by first using an inoffensive address, which is
1337 guaranteed to hit the offset == 0 case in
1338 hw_breakpoint_arch_parse. */
1339 unsigned int aligned_address
= address
& ~0x7U
;
1340 if (aligned_address
!= address
)
1342 sethbpregs_hwbp_address (pid
, i
, aligned_address
);
1343 sethbpregs_hwbp_control (pid
, i
, control
);
1345 sethbpregs_hwbp_address (pid
, i
, address
);
1346 sethbpregs_hwbp_control (pid
, i
, control
);
1349 arm_lwp_info
->bpts_changed
[i
] = 0;
1352 for (i
= 0; i
< arm_linux_get_hw_watchpoint_count (); i
++)
1353 if (arm_lwp_info
->wpts_changed
[i
])
1356 if (arm_hwbp_control_is_enabled (wpts
[i
].control
))
1357 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1358 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 1), &wpts
[i
].address
) < 0)
1359 perror_with_name (_("Unexpected error setting watchpoint"));
1361 if (wpts
[i
].control
!= 0)
1362 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1363 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 2), &wpts
[i
].control
) < 0)
1364 perror_with_name (_("Unexpected error setting watchpoint"));
1366 arm_lwp_info
->wpts_changed
[i
] = 0;
1370 /* linux_nat_new_fork hook. */
1373 arm_linux_nat_target::low_new_fork (struct lwp_info
*parent
, pid_t child_pid
)
1376 struct arm_linux_debug_reg_state
*parent_state
;
1377 struct arm_linux_debug_reg_state
*child_state
;
1379 /* NULL means no watchpoint has ever been set in the parent. In
1380 that case, there's nothing to do. */
1381 if (parent
->arch_private
== NULL
)
1384 /* GDB core assumes the child inherits the watchpoints/hw
1385 breakpoints of the parent, and will remove them all from the
1386 forked off process. Copy the debug registers mirrors into the
1387 new process so that all breakpoints and watchpoints can be
1388 removed together. */
1390 parent_pid
= parent
->ptid
.pid ();
1391 parent_state
= arm_linux_get_debug_reg_state (parent_pid
);
1392 child_state
= arm_linux_get_debug_reg_state (child_pid
);
1393 *child_state
= *parent_state
;
1396 void _initialize_arm_linux_nat ();
1398 _initialize_arm_linux_nat ()
1400 /* Register the target. */
1401 linux_target
= &the_arm_linux_nat_target
;
1402 add_inf_child_target (&the_arm_linux_nat_target
);