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
->get_register_status (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
->get_register_status (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
->get_register_status (regno
+ ARM_WR0_REGNUM
))
322 regcache_raw_collect (regcache
, regno
+ ARM_WR0_REGNUM
,
325 for (regno
= 0; regno
< 2; regno
++)
326 if (REG_VALID
== regcache
->get_register_status (regno
+ ARM_WCSSF_REGNUM
))
327 regcache_raw_collect (regcache
, regno
+ ARM_WCSSF_REGNUM
,
328 ®buf
[16 * 8 + regno
* 4]);
330 for (regno
= 0; regno
< 4; regno
++)
331 if (REG_VALID
== regcache
->get_register_status (regno
+ ARM_WCGR0_REGNUM
))
332 regcache_raw_collect (regcache
, regno
+ ARM_WCGR0_REGNUM
,
333 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
335 ret
= ptrace (PTRACE_SETWMMXREGS
, tid
, 0, regbuf
);
338 perror_with_name (_("Unable to store WMMX registers."));
342 fetch_vfp_regs (struct regcache
*regcache
)
344 gdb_byte regbuf
[VFP_REGS_SIZE
];
346 struct gdbarch
*gdbarch
= regcache
->arch ();
347 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
349 /* Get the thread id for the ptrace call. */
350 tid
= ptid_get_lwp (regcache
->ptid ());
352 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
356 iov
.iov_base
= regbuf
;
357 iov
.iov_len
= VFP_REGS_SIZE
;
358 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
361 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
364 perror_with_name (_("Unable to fetch VFP registers."));
366 aarch32_vfp_regcache_supply (regcache
, regbuf
,
367 tdep
->vfp_register_count
);
371 store_vfp_regs (const struct regcache
*regcache
)
373 gdb_byte regbuf
[VFP_REGS_SIZE
];
375 struct gdbarch
*gdbarch
= regcache
->arch ();
376 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
378 /* Get the thread id for the ptrace call. */
379 tid
= ptid_get_lwp (regcache
->ptid ());
381 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
385 iov
.iov_base
= regbuf
;
386 iov
.iov_len
= VFP_REGS_SIZE
;
387 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
390 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
393 perror_with_name (_("Unable to fetch VFP registers (for update)."));
395 aarch32_vfp_regcache_collect (regcache
, regbuf
,
396 tdep
->vfp_register_count
);
398 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
402 iov
.iov_base
= regbuf
;
403 iov
.iov_len
= VFP_REGS_SIZE
;
404 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iov
);
407 ret
= ptrace (PTRACE_SETVFPREGS
, tid
, 0, regbuf
);
410 perror_with_name (_("Unable to store VFP registers."));
413 /* Fetch registers from the child process. Fetch all registers if
414 regno == -1, otherwise fetch all general registers or all floating
415 point registers depending upon the value of regno. */
418 arm_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
420 struct gdbarch
*gdbarch
= regcache
->arch ();
421 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
425 fetch_regs (regcache
);
426 if (tdep
->have_wmmx_registers
)
427 fetch_wmmx_regs (regcache
);
428 if (tdep
->vfp_register_count
> 0)
429 fetch_vfp_regs (regcache
);
430 if (tdep
->have_fpa_registers
)
431 fetch_fpregs (regcache
);
435 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
436 fetch_regs (regcache
);
437 else if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
438 fetch_fpregs (regcache
);
439 else if (tdep
->have_wmmx_registers
440 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
441 fetch_wmmx_regs (regcache
);
442 else if (tdep
->vfp_register_count
> 0
443 && regno
>= ARM_D0_REGNUM
444 && (regno
< ARM_D0_REGNUM
+ tdep
->vfp_register_count
445 || regno
== ARM_FPSCR_REGNUM
))
446 fetch_vfp_regs (regcache
);
450 /* Store registers back into the inferior. Store all registers if
451 regno == -1, otherwise store all general registers or all floating
452 point registers depending upon the value of regno. */
455 arm_linux_nat_target::store_registers (struct regcache
*regcache
, int regno
)
457 struct gdbarch
*gdbarch
= regcache
->arch ();
458 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
462 store_regs (regcache
);
463 if (tdep
->have_wmmx_registers
)
464 store_wmmx_regs (regcache
);
465 if (tdep
->vfp_register_count
> 0)
466 store_vfp_regs (regcache
);
467 if (tdep
->have_fpa_registers
)
468 store_fpregs (regcache
);
472 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
473 store_regs (regcache
);
474 else if ((regno
>= ARM_F0_REGNUM
) && (regno
<= ARM_FPS_REGNUM
))
475 store_fpregs (regcache
);
476 else if (tdep
->have_wmmx_registers
477 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
478 store_wmmx_regs (regcache
);
479 else if (tdep
->vfp_register_count
> 0
480 && regno
>= ARM_D0_REGNUM
481 && (regno
< ARM_D0_REGNUM
+ tdep
->vfp_register_count
482 || regno
== ARM_FPSCR_REGNUM
))
483 store_vfp_regs (regcache
);
487 /* Wrapper functions for the standard regset handling, used by
491 fill_gregset (const struct regcache
*regcache
,
492 gdb_gregset_t
*gregsetp
, int regno
)
494 arm_linux_collect_gregset (NULL
, regcache
, regno
, gregsetp
, 0);
498 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
500 arm_linux_supply_gregset (NULL
, regcache
, -1, gregsetp
, 0);
504 fill_fpregset (const struct regcache
*regcache
,
505 gdb_fpregset_t
*fpregsetp
, int regno
)
507 arm_linux_collect_nwfpe (NULL
, regcache
, regno
, fpregsetp
, 0);
510 /* Fill GDB's register array with the floating-point register values
514 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
516 arm_linux_supply_nwfpe (NULL
, regcache
, -1, fpregsetp
, 0);
519 /* Fetch the thread-local storage pointer for libthread_db. */
522 ps_get_thread_area (struct ps_prochandle
*ph
,
523 lwpid_t lwpid
, int idx
, void **base
)
525 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
528 /* IDX is the bias from the thread pointer to the beginning of the
529 thread descriptor. It has to be subtracted due to implementation
530 quirks in libthread_db. */
531 *base
= (void *) ((char *)*base
- idx
);
536 const struct target_desc
*
537 arm_linux_nat_target::read_description ()
539 CORE_ADDR arm_hwcap
= 0;
541 if (have_ptrace_getregset
== TRIBOOL_UNKNOWN
)
543 elf_gregset_t gpregs
;
545 int tid
= ptid_get_lwp (inferior_ptid
);
547 iov
.iov_base
= &gpregs
;
548 iov
.iov_len
= sizeof (gpregs
);
550 /* Check if PTRACE_GETREGSET works. */
551 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
) < 0)
552 have_ptrace_getregset
= TRIBOOL_FALSE
;
554 have_ptrace_getregset
= TRIBOOL_TRUE
;
557 if (target_auxv_search (this, AT_HWCAP
, &arm_hwcap
) != 1)
559 return this->beneath
->read_description ();
562 if (arm_hwcap
& HWCAP_IWMMXT
)
563 return tdesc_arm_with_iwmmxt
;
565 if (arm_hwcap
& HWCAP_VFP
)
569 const struct target_desc
* result
= NULL
;
571 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
572 Neon with VFPv3-D32. */
573 if (arm_hwcap
& HWCAP_NEON
)
574 result
= tdesc_arm_with_neon
;
575 else if ((arm_hwcap
& (HWCAP_VFPv3
| HWCAP_VFPv3D16
)) == HWCAP_VFPv3
)
576 result
= tdesc_arm_with_vfpv3
;
578 result
= tdesc_arm_with_vfpv2
;
580 /* Now make sure that the kernel supports reading these
581 registers. Support was added in 2.6.30. */
582 pid
= ptid_get_lwp (inferior_ptid
);
584 buf
= (char *) alloca (VFP_REGS_SIZE
);
585 if (ptrace (PTRACE_GETVFPREGS
, pid
, 0, buf
) < 0
592 return this->beneath
->read_description ();
595 /* Information describing the hardware breakpoint capabilities. */
596 struct arm_linux_hwbp_cap
599 gdb_byte max_wp_length
;
604 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
605 assume a maximum number of supported break-/watchpoints. */
609 /* Get hold of the Hardware Breakpoint information for the target we are
610 attached to. Returns NULL if the kernel doesn't support Hardware
611 breakpoints at all, or a pointer to the information structure. */
612 static const struct arm_linux_hwbp_cap
*
613 arm_linux_get_hwbp_cap (void)
615 /* The info structure we return. */
616 static struct arm_linux_hwbp_cap info
;
618 /* Is INFO in a good state? -1 means that no attempt has been made to
619 initialize INFO; 0 means an attempt has been made, but it failed; 1
620 means INFO is in an initialized state. */
621 static int available
= -1;
628 tid
= ptid_get_lwp (inferior_ptid
);
629 if (ptrace (PTRACE_GETHBPREGS
, tid
, 0, &val
) < 0)
633 info
.arch
= (gdb_byte
)((val
>> 24) & 0xff);
634 info
.max_wp_length
= (gdb_byte
)((val
>> 16) & 0xff);
635 info
.wp_count
= (gdb_byte
)((val
>> 8) & 0xff);
636 info
.bp_count
= (gdb_byte
)(val
& 0xff);
638 if (info
.wp_count
> MAX_WPTS
)
640 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
641 supports %d"), MAX_WPTS
, info
.wp_count
);
642 info
.wp_count
= MAX_WPTS
;
645 if (info
.bp_count
> MAX_BPTS
)
647 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
648 supports %d"), MAX_BPTS
, info
.bp_count
);
649 info
.bp_count
= MAX_BPTS
;
651 available
= (info
.arch
!= 0);
655 return available
== 1 ? &info
: NULL
;
658 /* How many hardware breakpoints are available? */
660 arm_linux_get_hw_breakpoint_count (void)
662 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
663 return cap
!= NULL
? cap
->bp_count
: 0;
666 /* How many hardware watchpoints are available? */
668 arm_linux_get_hw_watchpoint_count (void)
670 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
671 return cap
!= NULL
? cap
->wp_count
: 0;
674 /* Have we got a free break-/watch-point available for use? Returns -1 if
675 there is not an appropriate resource available, otherwise returns 1. */
677 arm_linux_nat_target::can_use_hw_breakpoint (enum bptype type
,
680 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
681 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
683 int count
= arm_linux_get_hw_watchpoint_count ();
687 else if (cnt
+ ot
> count
)
690 else if (type
== bp_hardware_breakpoint
)
692 int count
= arm_linux_get_hw_breakpoint_count ();
696 else if (cnt
> count
)
705 /* Enum describing the different types of ARM hardware break-/watch-points. */
714 /* Type describing an ARM Hardware Breakpoint Control register value. */
715 typedef unsigned int arm_hwbp_control_t
;
717 /* Structure used to keep track of hardware break-/watch-points. */
718 struct arm_linux_hw_breakpoint
720 /* Address to break on, or being watched. */
721 unsigned int address
;
722 /* Control register for break-/watch- point. */
723 arm_hwbp_control_t control
;
726 /* Structure containing arrays of per process hardware break-/watchpoints
727 for caching address and control information.
729 The Linux ptrace interface to hardware break-/watch-points presents the
730 values in a vector centred around 0 (which is used fo generic information).
731 Positive indicies refer to breakpoint addresses/control registers, negative
732 indices to watchpoint addresses/control registers.
734 The Linux vector is indexed as follows:
735 -((i << 1) + 2): Control register for watchpoint i.
736 -((i << 1) + 1): Address register for watchpoint i.
737 0: Information register.
738 ((i << 1) + 1): Address register for breakpoint i.
739 ((i << 1) + 2): Control register for breakpoint i.
741 This structure is used as a per-thread cache of the state stored by the
742 kernel, so that we don't need to keep calling into the kernel to find a
745 We treat break-/watch-points with their enable bit clear as being deleted.
747 struct arm_linux_debug_reg_state
749 /* Hardware breakpoints for this process. */
750 struct arm_linux_hw_breakpoint bpts
[MAX_BPTS
];
751 /* Hardware watchpoints for this process. */
752 struct arm_linux_hw_breakpoint wpts
[MAX_WPTS
];
755 /* Per-process arch-specific data we want to keep. */
756 struct arm_linux_process_info
759 struct arm_linux_process_info
*next
;
760 /* The process identifier. */
762 /* Hardware break-/watchpoints state information. */
763 struct arm_linux_debug_reg_state state
;
767 /* Per-thread arch-specific data we want to keep. */
770 /* Non-zero if our copy differs from what's recorded in the thread. */
771 char bpts_changed
[MAX_BPTS
];
772 char wpts_changed
[MAX_WPTS
];
775 static struct arm_linux_process_info
*arm_linux_process_list
= NULL
;
777 /* Find process data for process PID. */
779 static struct arm_linux_process_info
*
780 arm_linux_find_process_pid (pid_t pid
)
782 struct arm_linux_process_info
*proc
;
784 for (proc
= arm_linux_process_list
; proc
; proc
= proc
->next
)
785 if (proc
->pid
== pid
)
791 /* Add process data for process PID. Returns newly allocated info
794 static struct arm_linux_process_info
*
795 arm_linux_add_process (pid_t pid
)
797 struct arm_linux_process_info
*proc
;
799 proc
= XCNEW (struct arm_linux_process_info
);
802 proc
->next
= arm_linux_process_list
;
803 arm_linux_process_list
= proc
;
808 /* Get data specific info for process PID, creating it if necessary.
809 Never returns NULL. */
811 static struct arm_linux_process_info
*
812 arm_linux_process_info_get (pid_t pid
)
814 struct arm_linux_process_info
*proc
;
816 proc
= arm_linux_find_process_pid (pid
);
818 proc
= arm_linux_add_process (pid
);
823 /* Called whenever GDB is no longer debugging process PID. It deletes
824 data structures that keep track of debug register state. */
827 arm_linux_nat_target::low_forget_process (pid_t pid
)
829 struct arm_linux_process_info
*proc
, **proc_link
;
831 proc
= arm_linux_process_list
;
832 proc_link
= &arm_linux_process_list
;
836 if (proc
->pid
== pid
)
838 *proc_link
= proc
->next
;
844 proc_link
= &proc
->next
;
849 /* Get hardware break-/watchpoint state for process PID. */
851 static struct arm_linux_debug_reg_state
*
852 arm_linux_get_debug_reg_state (pid_t pid
)
854 return &arm_linux_process_info_get (pid
)->state
;
857 /* Initialize an ARM hardware break-/watch-point control register value.
858 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
859 type of break-/watch-point; ENABLE indicates whether the point is enabled.
861 static arm_hwbp_control_t
862 arm_hwbp_control_initialize (unsigned byte_address_select
,
863 arm_hwbp_type hwbp_type
,
866 gdb_assert ((byte_address_select
& ~0xffU
) == 0);
867 gdb_assert (hwbp_type
!= arm_hwbp_break
868 || ((byte_address_select
& 0xfU
) != 0));
870 return (byte_address_select
<< 5) | (hwbp_type
<< 3) | (3 << 1) | enable
;
873 /* Does the breakpoint control value CONTROL have the enable bit set? */
875 arm_hwbp_control_is_enabled (arm_hwbp_control_t control
)
877 return control
& 0x1;
880 /* Change a breakpoint control word so that it is in the disabled state. */
881 static arm_hwbp_control_t
882 arm_hwbp_control_disable (arm_hwbp_control_t control
)
884 return control
& ~0x1;
887 /* Initialise the hardware breakpoint structure P. The breakpoint will be
888 enabled, and will point to the placed address of BP_TGT. */
890 arm_linux_hw_breakpoint_initialize (struct gdbarch
*gdbarch
,
891 struct bp_target_info
*bp_tgt
,
892 struct arm_linux_hw_breakpoint
*p
)
895 CORE_ADDR address
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
897 /* We have to create a mask for the control register which says which bits
898 of the word pointed to by address to break on. */
899 if (arm_pc_is_thumb (gdbarch
, address
))
910 p
->address
= (unsigned int) address
;
911 p
->control
= arm_hwbp_control_initialize (mask
, arm_hwbp_break
, 1);
914 /* Get the ARM hardware breakpoint type from the TYPE value we're
915 given when asked to set a watchpoint. */
917 arm_linux_get_hwbp_type (enum target_hw_bp_type type
)
920 return arm_hwbp_load
;
921 else if (type
== hw_write
)
922 return arm_hwbp_store
;
924 return arm_hwbp_access
;
927 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
928 to LEN. The type of watchpoint is given in RW. */
930 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr
, int len
,
931 enum target_hw_bp_type type
,
932 struct arm_linux_hw_breakpoint
*p
)
934 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
937 gdb_assert (cap
!= NULL
);
938 gdb_assert (cap
->max_wp_length
!= 0);
940 mask
= (1 << len
) - 1;
942 p
->address
= (unsigned int) addr
;
943 p
->control
= arm_hwbp_control_initialize (mask
,
944 arm_linux_get_hwbp_type (type
), 1);
947 /* Are two break-/watch-points equal? */
949 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint
*p1
,
950 const struct arm_linux_hw_breakpoint
*p2
)
952 return p1
->address
== p2
->address
&& p1
->control
== p2
->control
;
955 /* Callback to mark a watch-/breakpoint to be updated in all threads of
956 the current process. */
958 struct update_registers_data
965 update_registers_callback (struct lwp_info
*lwp
, void *arg
)
967 struct update_registers_data
*data
= (struct update_registers_data
*) arg
;
969 if (lwp
->arch_private
== NULL
)
970 lwp
->arch_private
= XCNEW (struct arch_lwp_info
);
972 /* The actual update is done later just before resuming the lwp,
973 we just mark that the registers need updating. */
975 lwp
->arch_private
->wpts_changed
[data
->index
] = 1;
977 lwp
->arch_private
->bpts_changed
[data
->index
] = 1;
979 /* If the lwp isn't stopped, force it to momentarily pause, so
980 we can update its breakpoint registers. */
982 linux_stop_lwp (lwp
);
987 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
988 =1) BPT for thread TID. */
990 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
* bpt
,
996 struct arm_linux_hw_breakpoint
* bpts
;
997 struct update_registers_data data
;
999 pid
= ptid_get_pid (inferior_ptid
);
1000 pid_ptid
= pid_to_ptid (pid
);
1004 count
= arm_linux_get_hw_watchpoint_count ();
1005 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1009 count
= arm_linux_get_hw_breakpoint_count ();
1010 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1013 for (i
= 0; i
< count
; ++i
)
1014 if (!arm_hwbp_control_is_enabled (bpts
[i
].control
))
1016 data
.watch
= watchpoint
;
1019 iterate_over_lwps (pid_ptid
, update_registers_callback
, &data
);
1023 gdb_assert (i
!= count
);
1026 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1027 (WATCHPOINT = 1) BPT for thread TID. */
1029 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
*bpt
,
1035 struct arm_linux_hw_breakpoint
* bpts
;
1036 struct update_registers_data data
;
1038 pid
= ptid_get_pid (inferior_ptid
);
1039 pid_ptid
= pid_to_ptid (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 data
.watch
= watchpoint
;
1057 bpts
[i
].control
= arm_hwbp_control_disable (bpts
[i
].control
);
1058 iterate_over_lwps (pid_ptid
, update_registers_callback
, &data
);
1062 gdb_assert (i
!= count
);
1065 /* Insert a Hardware breakpoint. */
1067 arm_linux_nat_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
1068 struct bp_target_info
*bp_tgt
)
1070 struct lwp_info
*lp
;
1071 struct arm_linux_hw_breakpoint p
;
1073 if (arm_linux_get_hw_breakpoint_count () == 0)
1076 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1078 arm_linux_insert_hw_breakpoint1 (&p
, 0);
1083 /* Remove a hardware breakpoint. */
1085 arm_linux_nat_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
1086 struct bp_target_info
*bp_tgt
)
1088 struct lwp_info
*lp
;
1089 struct arm_linux_hw_breakpoint p
;
1091 if (arm_linux_get_hw_breakpoint_count () == 0)
1094 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1096 arm_linux_remove_hw_breakpoint1 (&p
, 0);
1101 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1104 arm_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
1106 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
1107 CORE_ADDR max_wp_length
, aligned_addr
;
1109 /* Can not set watchpoints for zero or negative lengths. */
1113 /* Need to be able to use the ptrace interface. */
1114 if (cap
== NULL
|| cap
->wp_count
== 0)
1117 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1118 range covered by a watchpoint. */
1119 max_wp_length
= (CORE_ADDR
)cap
->max_wp_length
;
1120 aligned_addr
= addr
& ~(max_wp_length
- 1);
1122 if (aligned_addr
+ max_wp_length
< addr
+ len
)
1125 /* The current ptrace interface can only handle watchpoints that are a
1127 if ((len
& (len
- 1)) != 0)
1130 /* All tests passed so we must be able to set a watchpoint. */
1134 /* Insert a Hardware breakpoint. */
1136 arm_linux_nat_target::insert_watchpoint (CORE_ADDR addr
, int len
,
1137 enum target_hw_bp_type rw
,
1138 struct expression
*cond
)
1140 struct lwp_info
*lp
;
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 lwp_info
*lp
;
1160 struct arm_linux_hw_breakpoint p
;
1162 if (arm_linux_get_hw_watchpoint_count () == 0)
1165 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1167 arm_linux_remove_hw_breakpoint1 (&p
, 1);
1172 /* What was the data address the target was stopped on accessing. */
1174 arm_linux_nat_target::stopped_data_address (CORE_ADDR
*addr_p
)
1179 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
1182 /* This must be a hardware breakpoint. */
1183 if (siginfo
.si_signo
!= SIGTRAP
1184 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1187 /* We must be able to set hardware watchpoints. */
1188 if (arm_linux_get_hw_watchpoint_count () == 0)
1191 slot
= siginfo
.si_errno
;
1193 /* If we are in a positive slot then we're looking at a breakpoint and not
1198 *addr_p
= (CORE_ADDR
) (uintptr_t) siginfo
.si_addr
;
1202 /* Has the target been stopped by hitting a watchpoint? */
1204 arm_linux_nat_target::stopped_by_watchpoint ()
1207 return stopped_data_address (&addr
);
1211 arm_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr
,
1215 return start
<= addr
&& start
+ length
- 1 >= addr
;
1218 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1219 in the parent thread to the child thread. */
1221 arm_linux_nat_target::low_new_thread (struct lwp_info
*lp
)
1224 struct arch_lwp_info
*info
= XCNEW (struct arch_lwp_info
);
1226 /* Mark that all the hardware breakpoint/watchpoint register pairs
1227 for this thread need to be initialized. */
1229 for (i
= 0; i
< MAX_BPTS
; i
++)
1231 info
->bpts_changed
[i
] = 1;
1232 info
->wpts_changed
[i
] = 1;
1235 lp
->arch_private
= info
;
1238 /* Function to call when a thread is being deleted. */
1241 arm_linux_nat_target::low_delete_thread (struct arch_lwp_info
*arch_lwp
)
1246 /* Called when resuming a thread.
1247 The hardware debug registers are updated when there is any change. */
1250 arm_linux_nat_target::low_prepare_to_resume (struct lwp_info
*lwp
)
1253 struct arm_linux_hw_breakpoint
*bpts
, *wpts
;
1254 struct arch_lwp_info
*arm_lwp_info
= lwp
->arch_private
;
1256 pid
= ptid_get_lwp (lwp
->ptid
);
1257 bpts
= arm_linux_get_debug_reg_state (ptid_get_pid (lwp
->ptid
))->bpts
;
1258 wpts
= arm_linux_get_debug_reg_state (ptid_get_pid (lwp
->ptid
))->wpts
;
1260 /* NULL means this is the main thread still going through the shell,
1261 or, no watchpoint has been set yet. In that case, there's
1263 if (arm_lwp_info
== NULL
)
1266 for (i
= 0; i
< arm_linux_get_hw_breakpoint_count (); i
++)
1267 if (arm_lwp_info
->bpts_changed
[i
])
1270 if (arm_hwbp_control_is_enabled (bpts
[i
].control
))
1271 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1272 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 1), &bpts
[i
].address
) < 0)
1273 perror_with_name (_("Unexpected error setting breakpoint"));
1275 if (bpts
[i
].control
!= 0)
1276 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1277 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 2), &bpts
[i
].control
) < 0)
1278 perror_with_name (_("Unexpected error setting breakpoint"));
1280 arm_lwp_info
->bpts_changed
[i
] = 0;
1283 for (i
= 0; i
< arm_linux_get_hw_watchpoint_count (); i
++)
1284 if (arm_lwp_info
->wpts_changed
[i
])
1287 if (arm_hwbp_control_is_enabled (wpts
[i
].control
))
1288 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1289 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 1), &wpts
[i
].address
) < 0)
1290 perror_with_name (_("Unexpected error setting watchpoint"));
1292 if (wpts
[i
].control
!= 0)
1293 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1294 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 2), &wpts
[i
].control
) < 0)
1295 perror_with_name (_("Unexpected error setting watchpoint"));
1297 arm_lwp_info
->wpts_changed
[i
] = 0;
1301 /* linux_nat_new_fork hook. */
1304 arm_linux_nat_target::low_new_fork (struct lwp_info
*parent
, pid_t child_pid
)
1307 struct arm_linux_debug_reg_state
*parent_state
;
1308 struct arm_linux_debug_reg_state
*child_state
;
1310 /* NULL means no watchpoint has ever been set in the parent. In
1311 that case, there's nothing to do. */
1312 if (parent
->arch_private
== NULL
)
1315 /* GDB core assumes the child inherits the watchpoints/hw
1316 breakpoints of the parent, and will remove them all from the
1317 forked off process. Copy the debug registers mirrors into the
1318 new process so that all breakpoints and watchpoints can be
1319 removed together. */
1321 parent_pid
= ptid_get_pid (parent
->ptid
);
1322 parent_state
= arm_linux_get_debug_reg_state (parent_pid
);
1323 child_state
= arm_linux_get_debug_reg_state (child_pid
);
1324 *child_state
= *parent_state
;
1328 _initialize_arm_linux_nat (void)
1330 /* Register the target. */
1331 linux_target
= &the_arm_linux_nat_target
;
1332 add_inf_child_target (&the_arm_linux_nat_target
);