1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999-2019 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/>. */
21 /* Standard C includes. */
22 #include <elf/common.h>
23 #include <sys/procfs.h>
25 #include <sys/utsname.h>
27 /* Local non-gdb includes. */
28 #include "aarch32-linux-nat.h"
29 #include "arm-linux-tdep.h"
33 #include "gdbthread.h"
35 #include "linux-nat.h"
36 #include "linux-tdep.h"
37 #include "nat/gdb_ptrace.h"
38 #include "nat/linux-ptrace.h"
39 #include "observable.h"
41 #include "target-descriptions.h"
44 /* Prototypes for supply_gregset etc. */
47 /* Defines ps_err_e, struct ps_prochandle. */
48 #include "gdb_proc_service.h"
50 #ifndef PTRACE_GET_THREAD_AREA
51 #define PTRACE_GET_THREAD_AREA 22
54 #ifndef PTRACE_GETWMMXREGS
55 #define PTRACE_GETWMMXREGS 18
56 #define PTRACE_SETWMMXREGS 19
59 #ifndef PTRACE_GETVFPREGS
60 #define PTRACE_GETVFPREGS 27
61 #define PTRACE_SETVFPREGS 28
64 #ifndef PTRACE_GETHBPREGS
65 #define PTRACE_GETHBPREGS 29
66 #define PTRACE_SETHBPREGS 30
69 extern int arm_apcs_32
;
71 class arm_linux_nat_target final
: public linux_nat_target
74 /* Add our register access methods. */
75 void fetch_registers (struct regcache
*, int) override
;
76 void store_registers (struct regcache
*, int) override
;
78 /* Add our hardware breakpoint and watchpoint implementation. */
79 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
81 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
83 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
85 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
87 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
88 struct expression
*) override
;
90 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
91 struct expression
*) override
;
92 bool stopped_by_watchpoint () override
;
94 bool stopped_data_address (CORE_ADDR
*) override
;
96 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
98 const struct target_desc
*read_description () override
;
100 /* Override linux_nat_target low methods. */
102 /* Handle thread creation and exit. */
103 void low_new_thread (struct lwp_info
*lp
) override
;
104 void low_delete_thread (struct arch_lwp_info
*lp
) override
;
105 void low_prepare_to_resume (struct lwp_info
*lp
) override
;
107 /* Handle process creation and exit. */
108 void low_new_fork (struct lwp_info
*parent
, pid_t child_pid
) override
;
109 void low_forget_process (pid_t pid
) override
;
112 static arm_linux_nat_target the_arm_linux_nat_target
;
114 /* Get the whole floating point state of the process and store it
118 fetch_fpregs (struct regcache
*regcache
)
121 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
123 /* Get the thread id for the ptrace call. */
124 tid
= regcache
->ptid ().lwp ();
126 /* Read the floating point state. */
127 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
132 iov
.iov_len
= ARM_LINUX_SIZEOF_NWFPE
;
134 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iov
);
137 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
140 perror_with_name (_("Unable to fetch the floating point registers."));
143 regcache
->raw_supply (ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
145 /* Fetch the floating point registers. */
146 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
147 supply_nwfpe_register (regcache
, regno
, fp
);
150 /* Save the whole floating point state of the process using
151 the contents from regcache. */
154 store_fpregs (const struct regcache
*regcache
)
157 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
159 /* Get the thread id for the ptrace call. */
160 tid
= regcache
->ptid ().lwp ();
162 /* Read the floating point state. */
163 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
165 elf_fpregset_t fpregs
;
168 iov
.iov_base
= &fpregs
;
169 iov
.iov_len
= sizeof (fpregs
);
171 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iov
);
174 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
177 perror_with_name (_("Unable to fetch the floating point registers."));
180 if (REG_VALID
== regcache
->get_register_status (ARM_FPS_REGNUM
))
181 regcache
->raw_collect (ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
183 /* Store the floating point registers. */
184 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
185 if (REG_VALID
== regcache
->get_register_status (regno
))
186 collect_nwfpe_register (regcache
, regno
, fp
);
188 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
193 iov
.iov_len
= ARM_LINUX_SIZEOF_NWFPE
;
195 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_FPREGSET
, &iov
);
198 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
201 perror_with_name (_("Unable to store floating point registers."));
204 /* Fetch all general registers of the process and store into
208 fetch_regs (struct regcache
*regcache
)
213 /* Get the thread id for the ptrace call. */
214 tid
= regcache
->ptid ().lwp ();
216 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
220 iov
.iov_base
= ®s
;
221 iov
.iov_len
= sizeof (regs
);
223 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
);
226 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
229 perror_with_name (_("Unable to fetch general registers."));
231 aarch32_gp_regcache_supply (regcache
, (uint32_t *) regs
, arm_apcs_32
);
235 store_regs (const struct regcache
*regcache
)
240 /* Get the thread id for the ptrace call. */
241 tid
= regcache
->ptid ().lwp ();
243 /* Fetch the general registers. */
244 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
248 iov
.iov_base
= ®s
;
249 iov
.iov_len
= sizeof (regs
);
251 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
);
254 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
257 perror_with_name (_("Unable to fetch general registers."));
259 aarch32_gp_regcache_collect (regcache
, (uint32_t *) regs
, arm_apcs_32
);
261 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
265 iov
.iov_base
= ®s
;
266 iov
.iov_len
= sizeof (regs
);
268 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, &iov
);
271 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
274 perror_with_name (_("Unable to store general registers."));
277 /* Fetch all WMMX registers of the process and store into
280 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
283 fetch_wmmx_regs (struct regcache
*regcache
)
285 char regbuf
[IWMMXT_REGS_SIZE
];
288 /* Get the thread id for the ptrace call. */
289 tid
= regcache
->ptid ().lwp ();
291 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
293 perror_with_name (_("Unable to fetch WMMX registers."));
295 for (regno
= 0; regno
< 16; regno
++)
296 regcache
->raw_supply (regno
+ ARM_WR0_REGNUM
, ®buf
[regno
* 8]);
298 for (regno
= 0; regno
< 2; regno
++)
299 regcache
->raw_supply (regno
+ ARM_WCSSF_REGNUM
,
300 ®buf
[16 * 8 + regno
* 4]);
302 for (regno
= 0; regno
< 4; regno
++)
303 regcache
->raw_supply (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
= regcache
->ptid ().lwp ();
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
->get_register_status (regno
+ ARM_WR0_REGNUM
))
322 regcache
->raw_collect (regno
+ ARM_WR0_REGNUM
, ®buf
[regno
* 8]);
324 for (regno
= 0; regno
< 2; regno
++)
325 if (REG_VALID
== regcache
->get_register_status (regno
+ ARM_WCSSF_REGNUM
))
326 regcache
->raw_collect (regno
+ ARM_WCSSF_REGNUM
,
327 ®buf
[16 * 8 + regno
* 4]);
329 for (regno
= 0; regno
< 4; regno
++)
330 if (REG_VALID
== regcache
->get_register_status (regno
+ ARM_WCGR0_REGNUM
))
331 regcache
->raw_collect (regno
+ ARM_WCGR0_REGNUM
,
332 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
334 ret
= ptrace (PTRACE_SETWMMXREGS
, tid
, 0, regbuf
);
337 perror_with_name (_("Unable to store WMMX registers."));
341 fetch_vfp_regs (struct regcache
*regcache
)
343 gdb_byte regbuf
[VFP_REGS_SIZE
];
345 struct gdbarch
*gdbarch
= regcache
->arch ();
346 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
348 /* Get the thread id for the ptrace call. */
349 tid
= regcache
->ptid ().lwp ();
351 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
355 iov
.iov_base
= regbuf
;
356 iov
.iov_len
= VFP_REGS_SIZE
;
357 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
360 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
363 perror_with_name (_("Unable to fetch VFP registers."));
365 aarch32_vfp_regcache_supply (regcache
, regbuf
,
366 tdep
->vfp_register_count
);
370 store_vfp_regs (const struct regcache
*regcache
)
372 gdb_byte regbuf
[VFP_REGS_SIZE
];
374 struct gdbarch
*gdbarch
= regcache
->arch ();
375 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
377 /* Get the thread id for the ptrace call. */
378 tid
= regcache
->ptid ().lwp ();
380 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
384 iov
.iov_base
= regbuf
;
385 iov
.iov_len
= VFP_REGS_SIZE
;
386 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
389 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
392 perror_with_name (_("Unable to fetch VFP registers (for update)."));
394 aarch32_vfp_regcache_collect (regcache
, regbuf
,
395 tdep
->vfp_register_count
);
397 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
401 iov
.iov_base
= regbuf
;
402 iov
.iov_len
= VFP_REGS_SIZE
;
403 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iov
);
406 ret
= ptrace (PTRACE_SETVFPREGS
, tid
, 0, regbuf
);
409 perror_with_name (_("Unable to store VFP registers."));
412 /* Fetch registers from the child process. Fetch all registers if
413 regno == -1, otherwise fetch all general registers or all floating
414 point registers depending upon the value of regno. */
417 arm_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
419 struct gdbarch
*gdbarch
= regcache
->arch ();
420 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
424 fetch_regs (regcache
);
425 if (tdep
->have_wmmx_registers
)
426 fetch_wmmx_regs (regcache
);
427 if (tdep
->vfp_register_count
> 0)
428 fetch_vfp_regs (regcache
);
429 if (tdep
->have_fpa_registers
)
430 fetch_fpregs (regcache
);
434 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
435 fetch_regs (regcache
);
436 else if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
437 fetch_fpregs (regcache
);
438 else if (tdep
->have_wmmx_registers
439 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
440 fetch_wmmx_regs (regcache
);
441 else if (tdep
->vfp_register_count
> 0
442 && regno
>= ARM_D0_REGNUM
443 && (regno
< ARM_D0_REGNUM
+ tdep
->vfp_register_count
444 || regno
== ARM_FPSCR_REGNUM
))
445 fetch_vfp_regs (regcache
);
449 /* Store registers back into the inferior. Store all registers if
450 regno == -1, otherwise store all general registers or all floating
451 point registers depending upon the value of regno. */
454 arm_linux_nat_target::store_registers (struct regcache
*regcache
, int regno
)
456 struct gdbarch
*gdbarch
= regcache
->arch ();
457 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
461 store_regs (regcache
);
462 if (tdep
->have_wmmx_registers
)
463 store_wmmx_regs (regcache
);
464 if (tdep
->vfp_register_count
> 0)
465 store_vfp_regs (regcache
);
466 if (tdep
->have_fpa_registers
)
467 store_fpregs (regcache
);
471 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
472 store_regs (regcache
);
473 else if ((regno
>= ARM_F0_REGNUM
) && (regno
<= ARM_FPS_REGNUM
))
474 store_fpregs (regcache
);
475 else if (tdep
->have_wmmx_registers
476 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
477 store_wmmx_regs (regcache
);
478 else if (tdep
->vfp_register_count
> 0
479 && regno
>= ARM_D0_REGNUM
480 && (regno
< ARM_D0_REGNUM
+ tdep
->vfp_register_count
481 || regno
== ARM_FPSCR_REGNUM
))
482 store_vfp_regs (regcache
);
486 /* Wrapper functions for the standard regset handling, used by
490 fill_gregset (const struct regcache
*regcache
,
491 gdb_gregset_t
*gregsetp
, int regno
)
493 arm_linux_collect_gregset (NULL
, regcache
, regno
, gregsetp
, 0);
497 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
499 arm_linux_supply_gregset (NULL
, regcache
, -1, gregsetp
, 0);
503 fill_fpregset (const struct regcache
*regcache
,
504 gdb_fpregset_t
*fpregsetp
, int regno
)
506 arm_linux_collect_nwfpe (NULL
, regcache
, regno
, fpregsetp
, 0);
509 /* Fill GDB's register array with the floating-point register values
513 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
515 arm_linux_supply_nwfpe (NULL
, regcache
, -1, fpregsetp
, 0);
518 /* Fetch the thread-local storage pointer for libthread_db. */
521 ps_get_thread_area (struct ps_prochandle
*ph
,
522 lwpid_t lwpid
, int idx
, void **base
)
524 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
527 /* IDX is the bias from the thread pointer to the beginning of the
528 thread descriptor. It has to be subtracted due to implementation
529 quirks in libthread_db. */
530 *base
= (void *) ((char *)*base
- idx
);
535 const struct target_desc
*
536 arm_linux_nat_target::read_description ()
538 CORE_ADDR arm_hwcap
= linux_get_hwcap (this);
540 if (have_ptrace_getregset
== TRIBOOL_UNKNOWN
)
542 elf_gregset_t gpregs
;
544 int tid
= inferior_ptid
.lwp ();
546 iov
.iov_base
= &gpregs
;
547 iov
.iov_len
= sizeof (gpregs
);
549 /* Check if PTRACE_GETREGSET works. */
550 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
) < 0)
551 have_ptrace_getregset
= TRIBOOL_FALSE
;
553 have_ptrace_getregset
= TRIBOOL_TRUE
;
556 if (arm_hwcap
& HWCAP_IWMMXT
)
557 return tdesc_arm_with_iwmmxt
;
559 if (arm_hwcap
& HWCAP_VFP
)
563 const struct target_desc
* result
= NULL
;
565 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
566 Neon with VFPv3-D32. */
567 if (arm_hwcap
& HWCAP_NEON
)
568 result
= tdesc_arm_with_neon
;
569 else if ((arm_hwcap
& (HWCAP_VFPv3
| HWCAP_VFPv3D16
)) == HWCAP_VFPv3
)
570 result
= tdesc_arm_with_vfpv3
;
572 result
= tdesc_arm_with_vfpv2
;
574 /* Now make sure that the kernel supports reading these
575 registers. Support was added in 2.6.30. */
576 pid
= inferior_ptid
.lwp ();
578 buf
= (char *) alloca (VFP_REGS_SIZE
);
579 if (ptrace (PTRACE_GETVFPREGS
, pid
, 0, buf
) < 0
586 return this->beneath ()->read_description ();
589 /* Information describing the hardware breakpoint capabilities. */
590 struct arm_linux_hwbp_cap
593 gdb_byte max_wp_length
;
598 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
599 assume a maximum number of supported break-/watchpoints. */
603 /* Get hold of the Hardware Breakpoint information for the target we are
604 attached to. Returns NULL if the kernel doesn't support Hardware
605 breakpoints at all, or a pointer to the information structure. */
606 static const struct arm_linux_hwbp_cap
*
607 arm_linux_get_hwbp_cap (void)
609 /* The info structure we return. */
610 static struct arm_linux_hwbp_cap info
;
612 /* Is INFO in a good state? -1 means that no attempt has been made to
613 initialize INFO; 0 means an attempt has been made, but it failed; 1
614 means INFO is in an initialized state. */
615 static int available
= -1;
622 tid
= inferior_ptid
.lwp ();
623 if (ptrace (PTRACE_GETHBPREGS
, tid
, 0, &val
) < 0)
627 info
.arch
= (gdb_byte
)((val
>> 24) & 0xff);
628 info
.max_wp_length
= (gdb_byte
)((val
>> 16) & 0xff);
629 info
.wp_count
= (gdb_byte
)((val
>> 8) & 0xff);
630 info
.bp_count
= (gdb_byte
)(val
& 0xff);
632 if (info
.wp_count
> MAX_WPTS
)
634 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
635 supports %d"), MAX_WPTS
, info
.wp_count
);
636 info
.wp_count
= MAX_WPTS
;
639 if (info
.bp_count
> MAX_BPTS
)
641 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
642 supports %d"), MAX_BPTS
, info
.bp_count
);
643 info
.bp_count
= MAX_BPTS
;
645 available
= (info
.arch
!= 0);
649 return available
== 1 ? &info
: NULL
;
652 /* How many hardware breakpoints are available? */
654 arm_linux_get_hw_breakpoint_count (void)
656 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
657 return cap
!= NULL
? cap
->bp_count
: 0;
660 /* How many hardware watchpoints are available? */
662 arm_linux_get_hw_watchpoint_count (void)
664 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
665 return cap
!= NULL
? cap
->wp_count
: 0;
668 /* Have we got a free break-/watch-point available for use? Returns -1 if
669 there is not an appropriate resource available, otherwise returns 1. */
671 arm_linux_nat_target::can_use_hw_breakpoint (enum bptype type
,
674 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
675 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
677 int count
= arm_linux_get_hw_watchpoint_count ();
681 else if (cnt
+ ot
> count
)
684 else if (type
== bp_hardware_breakpoint
)
686 int count
= arm_linux_get_hw_breakpoint_count ();
690 else if (cnt
> count
)
699 /* Enum describing the different types of ARM hardware break-/watch-points. */
708 /* Type describing an ARM Hardware Breakpoint Control register value. */
709 typedef unsigned int arm_hwbp_control_t
;
711 /* Structure used to keep track of hardware break-/watch-points. */
712 struct arm_linux_hw_breakpoint
714 /* Address to break on, or being watched. */
715 unsigned int address
;
716 /* Control register for break-/watch- point. */
717 arm_hwbp_control_t control
;
720 /* Structure containing arrays of per process hardware break-/watchpoints
721 for caching address and control information.
723 The Linux ptrace interface to hardware break-/watch-points presents the
724 values in a vector centred around 0 (which is used fo generic information).
725 Positive indicies refer to breakpoint addresses/control registers, negative
726 indices to watchpoint addresses/control registers.
728 The Linux vector is indexed as follows:
729 -((i << 1) + 2): Control register for watchpoint i.
730 -((i << 1) + 1): Address register for watchpoint i.
731 0: Information register.
732 ((i << 1) + 1): Address register for breakpoint i.
733 ((i << 1) + 2): Control register for breakpoint i.
735 This structure is used as a per-thread cache of the state stored by the
736 kernel, so that we don't need to keep calling into the kernel to find a
739 We treat break-/watch-points with their enable bit clear as being deleted.
741 struct arm_linux_debug_reg_state
743 /* Hardware breakpoints for this process. */
744 struct arm_linux_hw_breakpoint bpts
[MAX_BPTS
];
745 /* Hardware watchpoints for this process. */
746 struct arm_linux_hw_breakpoint wpts
[MAX_WPTS
];
749 /* Per-process arch-specific data we want to keep. */
750 struct arm_linux_process_info
753 struct arm_linux_process_info
*next
;
754 /* The process identifier. */
756 /* Hardware break-/watchpoints state information. */
757 struct arm_linux_debug_reg_state state
;
761 /* Per-thread arch-specific data we want to keep. */
764 /* Non-zero if our copy differs from what's recorded in the thread. */
765 char bpts_changed
[MAX_BPTS
];
766 char wpts_changed
[MAX_WPTS
];
769 static struct arm_linux_process_info
*arm_linux_process_list
= NULL
;
771 /* Find process data for process PID. */
773 static struct arm_linux_process_info
*
774 arm_linux_find_process_pid (pid_t pid
)
776 struct arm_linux_process_info
*proc
;
778 for (proc
= arm_linux_process_list
; proc
; proc
= proc
->next
)
779 if (proc
->pid
== pid
)
785 /* Add process data for process PID. Returns newly allocated info
788 static struct arm_linux_process_info
*
789 arm_linux_add_process (pid_t pid
)
791 struct arm_linux_process_info
*proc
;
793 proc
= XCNEW (struct arm_linux_process_info
);
796 proc
->next
= arm_linux_process_list
;
797 arm_linux_process_list
= proc
;
802 /* Get data specific info for process PID, creating it if necessary.
803 Never returns NULL. */
805 static struct arm_linux_process_info
*
806 arm_linux_process_info_get (pid_t pid
)
808 struct arm_linux_process_info
*proc
;
810 proc
= arm_linux_find_process_pid (pid
);
812 proc
= arm_linux_add_process (pid
);
817 /* Called whenever GDB is no longer debugging process PID. It deletes
818 data structures that keep track of debug register state. */
821 arm_linux_nat_target::low_forget_process (pid_t pid
)
823 struct arm_linux_process_info
*proc
, **proc_link
;
825 proc
= arm_linux_process_list
;
826 proc_link
= &arm_linux_process_list
;
830 if (proc
->pid
== pid
)
832 *proc_link
= proc
->next
;
838 proc_link
= &proc
->next
;
843 /* Get hardware break-/watchpoint state for process PID. */
845 static struct arm_linux_debug_reg_state
*
846 arm_linux_get_debug_reg_state (pid_t pid
)
848 return &arm_linux_process_info_get (pid
)->state
;
851 /* Initialize an ARM hardware break-/watch-point control register value.
852 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
853 type of break-/watch-point; ENABLE indicates whether the point is enabled.
855 static arm_hwbp_control_t
856 arm_hwbp_control_initialize (unsigned byte_address_select
,
857 arm_hwbp_type hwbp_type
,
860 gdb_assert ((byte_address_select
& ~0xffU
) == 0);
861 gdb_assert (hwbp_type
!= arm_hwbp_break
862 || ((byte_address_select
& 0xfU
) != 0));
864 return (byte_address_select
<< 5) | (hwbp_type
<< 3) | (3 << 1) | enable
;
867 /* Does the breakpoint control value CONTROL have the enable bit set? */
869 arm_hwbp_control_is_enabled (arm_hwbp_control_t control
)
871 return control
& 0x1;
874 /* Change a breakpoint control word so that it is in the disabled state. */
875 static arm_hwbp_control_t
876 arm_hwbp_control_disable (arm_hwbp_control_t control
)
878 return control
& ~0x1;
881 /* Initialise the hardware breakpoint structure P. The breakpoint will be
882 enabled, and will point to the placed address of BP_TGT. */
884 arm_linux_hw_breakpoint_initialize (struct gdbarch
*gdbarch
,
885 struct bp_target_info
*bp_tgt
,
886 struct arm_linux_hw_breakpoint
*p
)
889 CORE_ADDR address
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
891 /* We have to create a mask for the control register which says which bits
892 of the word pointed to by address to break on. */
893 if (arm_pc_is_thumb (gdbarch
, address
))
904 p
->address
= (unsigned int) address
;
905 p
->control
= arm_hwbp_control_initialize (mask
, arm_hwbp_break
, 1);
908 /* Get the ARM hardware breakpoint type from the TYPE value we're
909 given when asked to set a watchpoint. */
911 arm_linux_get_hwbp_type (enum target_hw_bp_type type
)
914 return arm_hwbp_load
;
915 else if (type
== hw_write
)
916 return arm_hwbp_store
;
918 return arm_hwbp_access
;
921 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
922 to LEN. The type of watchpoint is given in RW. */
924 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr
, int len
,
925 enum target_hw_bp_type type
,
926 struct arm_linux_hw_breakpoint
*p
)
928 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
931 gdb_assert (cap
!= NULL
);
932 gdb_assert (cap
->max_wp_length
!= 0);
934 mask
= (1 << len
) - 1;
936 p
->address
= (unsigned int) addr
;
937 p
->control
= arm_hwbp_control_initialize (mask
,
938 arm_linux_get_hwbp_type (type
), 1);
941 /* Are two break-/watch-points equal? */
943 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint
*p1
,
944 const struct arm_linux_hw_breakpoint
*p2
)
946 return p1
->address
== p2
->address
&& p1
->control
== p2
->control
;
949 /* Callback to mark a watch-/breakpoint to be updated in all threads of
950 the current process. */
953 update_registers_callback (struct lwp_info
*lwp
, int watch
, int index
)
955 if (lwp
->arch_private
== NULL
)
956 lwp
->arch_private
= XCNEW (struct arch_lwp_info
);
958 /* The actual update is done later just before resuming the lwp,
959 we just mark that the registers need updating. */
961 lwp
->arch_private
->wpts_changed
[index
] = 1;
963 lwp
->arch_private
->bpts_changed
[index
] = 1;
965 /* If the lwp isn't stopped, force it to momentarily pause, so
966 we can update its breakpoint registers. */
968 linux_stop_lwp (lwp
);
973 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
974 =1) BPT for thread TID. */
976 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
* bpt
,
982 struct arm_linux_hw_breakpoint
* bpts
;
984 pid
= inferior_ptid
.pid ();
985 pid_ptid
= ptid_t (pid
);
989 count
= arm_linux_get_hw_watchpoint_count ();
990 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
994 count
= arm_linux_get_hw_breakpoint_count ();
995 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
998 for (i
= 0; i
< count
; ++i
)
999 if (!arm_hwbp_control_is_enabled (bpts
[i
].control
))
1002 iterate_over_lwps (pid_ptid
,
1003 [=] (struct lwp_info
*info
)
1005 return update_registers_callback (info
, watchpoint
,
1011 gdb_assert (i
!= count
);
1014 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1015 (WATCHPOINT = 1) BPT for thread TID. */
1017 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
*bpt
,
1023 struct arm_linux_hw_breakpoint
* bpts
;
1025 pid
= inferior_ptid
.pid ();
1026 pid_ptid
= ptid_t (pid
);
1030 count
= arm_linux_get_hw_watchpoint_count ();
1031 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1035 count
= arm_linux_get_hw_breakpoint_count ();
1036 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1039 for (i
= 0; i
< count
; ++i
)
1040 if (arm_linux_hw_breakpoint_equal (bpt
, bpts
+ i
))
1042 bpts
[i
].control
= arm_hwbp_control_disable (bpts
[i
].control
);
1043 iterate_over_lwps (pid_ptid
,
1044 [=] (struct lwp_info
*info
)
1046 return update_registers_callback (info
, watchpoint
,
1052 gdb_assert (i
!= count
);
1055 /* Insert a Hardware breakpoint. */
1057 arm_linux_nat_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
1058 struct bp_target_info
*bp_tgt
)
1060 struct arm_linux_hw_breakpoint p
;
1062 if (arm_linux_get_hw_breakpoint_count () == 0)
1065 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1067 arm_linux_insert_hw_breakpoint1 (&p
, 0);
1072 /* Remove a hardware breakpoint. */
1074 arm_linux_nat_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
1075 struct bp_target_info
*bp_tgt
)
1077 struct arm_linux_hw_breakpoint p
;
1079 if (arm_linux_get_hw_breakpoint_count () == 0)
1082 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1084 arm_linux_remove_hw_breakpoint1 (&p
, 0);
1089 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1092 arm_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
1094 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
1095 CORE_ADDR max_wp_length
, aligned_addr
;
1097 /* Can not set watchpoints for zero or negative lengths. */
1101 /* Need to be able to use the ptrace interface. */
1102 if (cap
== NULL
|| cap
->wp_count
== 0)
1105 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1106 range covered by a watchpoint. */
1107 max_wp_length
= (CORE_ADDR
)cap
->max_wp_length
;
1108 aligned_addr
= addr
& ~(max_wp_length
- 1);
1110 if (aligned_addr
+ max_wp_length
< addr
+ len
)
1113 /* The current ptrace interface can only handle watchpoints that are a
1115 if ((len
& (len
- 1)) != 0)
1118 /* All tests passed so we must be able to set a watchpoint. */
1122 /* Insert a Hardware breakpoint. */
1124 arm_linux_nat_target::insert_watchpoint (CORE_ADDR addr
, int len
,
1125 enum target_hw_bp_type rw
,
1126 struct expression
*cond
)
1128 struct arm_linux_hw_breakpoint p
;
1130 if (arm_linux_get_hw_watchpoint_count () == 0)
1133 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1135 arm_linux_insert_hw_breakpoint1 (&p
, 1);
1140 /* Remove a hardware breakpoint. */
1142 arm_linux_nat_target::remove_watchpoint (CORE_ADDR addr
,
1143 int len
, enum target_hw_bp_type rw
,
1144 struct expression
*cond
)
1146 struct arm_linux_hw_breakpoint p
;
1148 if (arm_linux_get_hw_watchpoint_count () == 0)
1151 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1153 arm_linux_remove_hw_breakpoint1 (&p
, 1);
1158 /* What was the data address the target was stopped on accessing. */
1160 arm_linux_nat_target::stopped_data_address (CORE_ADDR
*addr_p
)
1165 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
1168 /* This must be a hardware breakpoint. */
1169 if (siginfo
.si_signo
!= SIGTRAP
1170 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1173 /* We must be able to set hardware watchpoints. */
1174 if (arm_linux_get_hw_watchpoint_count () == 0)
1177 slot
= siginfo
.si_errno
;
1179 /* If we are in a positive slot then we're looking at a breakpoint and not
1184 *addr_p
= (CORE_ADDR
) (uintptr_t) siginfo
.si_addr
;
1188 /* Has the target been stopped by hitting a watchpoint? */
1190 arm_linux_nat_target::stopped_by_watchpoint ()
1193 return stopped_data_address (&addr
);
1197 arm_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr
,
1201 return start
<= addr
&& start
+ length
- 1 >= addr
;
1204 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1205 in the parent thread to the child thread. */
1207 arm_linux_nat_target::low_new_thread (struct lwp_info
*lp
)
1210 struct arch_lwp_info
*info
= XCNEW (struct arch_lwp_info
);
1212 /* Mark that all the hardware breakpoint/watchpoint register pairs
1213 for this thread need to be initialized. */
1215 for (i
= 0; i
< MAX_BPTS
; i
++)
1217 info
->bpts_changed
[i
] = 1;
1218 info
->wpts_changed
[i
] = 1;
1221 lp
->arch_private
= info
;
1224 /* Function to call when a thread is being deleted. */
1227 arm_linux_nat_target::low_delete_thread (struct arch_lwp_info
*arch_lwp
)
1232 /* Called when resuming a thread.
1233 The hardware debug registers are updated when there is any change. */
1236 arm_linux_nat_target::low_prepare_to_resume (struct lwp_info
*lwp
)
1239 struct arm_linux_hw_breakpoint
*bpts
, *wpts
;
1240 struct arch_lwp_info
*arm_lwp_info
= lwp
->arch_private
;
1242 pid
= lwp
->ptid
.lwp ();
1243 bpts
= arm_linux_get_debug_reg_state (lwp
->ptid
.pid ())->bpts
;
1244 wpts
= arm_linux_get_debug_reg_state (lwp
->ptid
.pid ())->wpts
;
1246 /* NULL means this is the main thread still going through the shell,
1247 or, no watchpoint has been set yet. In that case, there's
1249 if (arm_lwp_info
== NULL
)
1252 for (i
= 0; i
< arm_linux_get_hw_breakpoint_count (); i
++)
1253 if (arm_lwp_info
->bpts_changed
[i
])
1256 if (arm_hwbp_control_is_enabled (bpts
[i
].control
))
1257 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1258 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 1), &bpts
[i
].address
) < 0)
1259 perror_with_name (_("Unexpected error setting breakpoint"));
1261 if (bpts
[i
].control
!= 0)
1262 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1263 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 2), &bpts
[i
].control
) < 0)
1264 perror_with_name (_("Unexpected error setting breakpoint"));
1266 arm_lwp_info
->bpts_changed
[i
] = 0;
1269 for (i
= 0; i
< arm_linux_get_hw_watchpoint_count (); i
++)
1270 if (arm_lwp_info
->wpts_changed
[i
])
1273 if (arm_hwbp_control_is_enabled (wpts
[i
].control
))
1274 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1275 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 1), &wpts
[i
].address
) < 0)
1276 perror_with_name (_("Unexpected error setting watchpoint"));
1278 if (wpts
[i
].control
!= 0)
1279 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1280 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 2), &wpts
[i
].control
) < 0)
1281 perror_with_name (_("Unexpected error setting watchpoint"));
1283 arm_lwp_info
->wpts_changed
[i
] = 0;
1287 /* linux_nat_new_fork hook. */
1290 arm_linux_nat_target::low_new_fork (struct lwp_info
*parent
, pid_t child_pid
)
1293 struct arm_linux_debug_reg_state
*parent_state
;
1294 struct arm_linux_debug_reg_state
*child_state
;
1296 /* NULL means no watchpoint has ever been set in the parent. In
1297 that case, there's nothing to do. */
1298 if (parent
->arch_private
== NULL
)
1301 /* GDB core assumes the child inherits the watchpoints/hw
1302 breakpoints of the parent, and will remove them all from the
1303 forked off process. Copy the debug registers mirrors into the
1304 new process so that all breakpoints and watchpoints can be
1305 removed together. */
1307 parent_pid
= parent
->ptid
.pid ();
1308 parent_state
= arm_linux_get_debug_reg_state (parent_pid
);
1309 child_state
= arm_linux_get_debug_reg_state (child_pid
);
1310 *child_state
= *parent_state
;
1314 _initialize_arm_linux_nat (void)
1316 /* Register the target. */
1317 linux_target
= &the_arm_linux_nat_target
;
1318 add_inf_child_target (&the_arm_linux_nat_target
);