1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999-2015 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"
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 /* Get the whole floating point state of the process and store it
73 fetch_fpregs (struct regcache
*regcache
)
76 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
78 /* Get the thread id for the ptrace call. */
79 tid
= ptid_get_lwp (inferior_ptid
);
81 /* Read the floating point state. */
82 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
87 iov
.iov_len
= ARM_LINUX_SIZEOF_NWFPE
;
89 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iov
);
92 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
96 warning (_("Unable to fetch the floating point registers."));
101 regcache_raw_supply (regcache
, ARM_FPS_REGNUM
,
102 fp
+ NWFPE_FPSR_OFFSET
);
104 /* Fetch the floating point registers. */
105 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
106 supply_nwfpe_register (regcache
, regno
, fp
);
109 /* Save the whole floating point state of the process using
110 the contents from regcache. */
113 store_fpregs (const struct regcache
*regcache
)
116 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
118 /* Get the thread id for the ptrace call. */
119 tid
= ptid_get_lwp (inferior_ptid
);
121 /* Read the floating point state. */
122 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
124 elf_fpregset_t fpregs
;
127 iov
.iov_base
= &fpregs
;
128 iov
.iov_len
= sizeof (fpregs
);
130 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iov
);
133 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
137 warning (_("Unable to fetch the floating point registers."));
142 if (REG_VALID
== regcache_register_status (regcache
, ARM_FPS_REGNUM
))
143 regcache_raw_collect (regcache
, ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
145 /* Store the floating point registers. */
146 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
147 if (REG_VALID
== regcache_register_status (regcache
, regno
))
148 collect_nwfpe_register (regcache
, regno
, fp
);
150 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
155 iov
.iov_len
= ARM_LINUX_SIZEOF_NWFPE
;
157 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_FPREGSET
, &iov
);
160 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
164 warning (_("Unable to store floating point registers."));
169 /* Fetch all general registers of the process and store into
173 fetch_regs (struct regcache
*regcache
)
178 /* Get the thread id for the ptrace call. */
179 tid
= ptid_get_lwp (inferior_ptid
);
181 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
185 iov
.iov_base
= ®s
;
186 iov
.iov_len
= sizeof (regs
);
188 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
);
191 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
195 warning (_("Unable to fetch general registers."));
199 aarch32_gp_regcache_supply (regcache
, (uint32_t *) regs
, arm_apcs_32
);
203 store_regs (const struct regcache
*regcache
)
208 /* Get the thread id for the ptrace call. */
209 tid
= ptid_get_lwp (inferior_ptid
);
211 /* Fetch the general registers. */
212 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
216 iov
.iov_base
= ®s
;
217 iov
.iov_len
= sizeof (regs
);
219 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
);
222 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
226 warning (_("Unable to fetch general registers."));
230 aarch32_gp_regcache_collect (regcache
, (uint32_t *) regs
, arm_apcs_32
);
232 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
236 iov
.iov_base
= ®s
;
237 iov
.iov_len
= sizeof (regs
);
239 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, &iov
);
242 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
246 warning (_("Unable to store general registers."));
251 /* Fetch all WMMX registers of the process and store into
254 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
257 fetch_wmmx_regs (struct regcache
*regcache
)
259 char regbuf
[IWMMXT_REGS_SIZE
];
262 /* Get the thread id for the ptrace call. */
263 tid
= ptid_get_lwp (inferior_ptid
);
265 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
268 warning (_("Unable to fetch WMMX registers."));
272 for (regno
= 0; regno
< 16; regno
++)
273 regcache_raw_supply (regcache
, regno
+ ARM_WR0_REGNUM
,
276 for (regno
= 0; regno
< 2; regno
++)
277 regcache_raw_supply (regcache
, regno
+ ARM_WCSSF_REGNUM
,
278 ®buf
[16 * 8 + regno
* 4]);
280 for (regno
= 0; regno
< 4; regno
++)
281 regcache_raw_supply (regcache
, regno
+ ARM_WCGR0_REGNUM
,
282 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
286 store_wmmx_regs (const struct regcache
*regcache
)
288 char regbuf
[IWMMXT_REGS_SIZE
];
291 /* Get the thread id for the ptrace call. */
292 tid
= ptid_get_lwp (inferior_ptid
);
294 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
297 warning (_("Unable to fetch WMMX registers."));
301 for (regno
= 0; regno
< 16; regno
++)
302 if (REG_VALID
== regcache_register_status (regcache
,
303 regno
+ ARM_WR0_REGNUM
))
304 regcache_raw_collect (regcache
, regno
+ ARM_WR0_REGNUM
,
307 for (regno
= 0; regno
< 2; regno
++)
308 if (REG_VALID
== regcache_register_status (regcache
,
309 regno
+ ARM_WCSSF_REGNUM
))
310 regcache_raw_collect (regcache
, regno
+ ARM_WCSSF_REGNUM
,
311 ®buf
[16 * 8 + regno
* 4]);
313 for (regno
= 0; regno
< 4; regno
++)
314 if (REG_VALID
== regcache_register_status (regcache
,
315 regno
+ ARM_WCGR0_REGNUM
))
316 regcache_raw_collect (regcache
, regno
+ ARM_WCGR0_REGNUM
,
317 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
319 ret
= ptrace (PTRACE_SETWMMXREGS
, tid
, 0, regbuf
);
323 warning (_("Unable to store WMMX registers."));
329 fetch_vfp_regs (struct regcache
*regcache
)
331 gdb_byte regbuf
[VFP_REGS_SIZE
];
333 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
334 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
336 /* Get the thread id for the ptrace call. */
337 tid
= ptid_get_lwp (inferior_ptid
);
339 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
343 iov
.iov_base
= regbuf
;
344 iov
.iov_len
= VFP_REGS_SIZE
;
345 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
348 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
352 warning (_("Unable to fetch VFP registers."));
356 aarch32_vfp_regcache_supply (regcache
, regbuf
,
357 tdep
->vfp_register_count
);
361 store_vfp_regs (const struct regcache
*regcache
)
363 gdb_byte regbuf
[VFP_REGS_SIZE
];
365 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
366 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
368 /* Get the thread id for the ptrace call. */
369 tid
= ptid_get_lwp (inferior_ptid
);
371 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
375 iov
.iov_base
= regbuf
;
376 iov
.iov_len
= VFP_REGS_SIZE
;
377 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
380 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
384 warning (_("Unable to fetch VFP registers (for update)."));
388 aarch32_vfp_regcache_collect (regcache
, regbuf
,
389 tdep
->vfp_register_count
);
391 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
395 iov
.iov_base
= regbuf
;
396 iov
.iov_len
= VFP_REGS_SIZE
;
397 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iov
);
400 ret
= ptrace (PTRACE_SETVFPREGS
, tid
, 0, regbuf
);
404 warning (_("Unable to store VFP registers."));
409 /* Fetch registers from the child process. Fetch all registers if
410 regno == -1, otherwise fetch all general registers or all floating
411 point registers depending upon the value of regno. */
414 arm_linux_fetch_inferior_registers (struct target_ops
*ops
,
415 struct regcache
*regcache
, int regno
)
417 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
418 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
422 fetch_regs (regcache
);
423 fetch_fpregs (regcache
);
424 if (tdep
->have_wmmx_registers
)
425 fetch_wmmx_regs (regcache
);
426 if (tdep
->vfp_register_count
> 0)
427 fetch_vfp_regs (regcache
);
431 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
432 fetch_regs (regcache
);
433 else if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
434 fetch_fpregs (regcache
);
435 else if (tdep
->have_wmmx_registers
436 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
437 fetch_wmmx_regs (regcache
);
438 else if (tdep
->vfp_register_count
> 0
439 && regno
>= ARM_D0_REGNUM
440 && regno
<= ARM_D0_REGNUM
+ tdep
->vfp_register_count
)
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_store_inferior_registers (struct target_ops
*ops
,
451 struct regcache
*regcache
, int regno
)
453 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
454 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
458 store_regs (regcache
);
459 store_fpregs (regcache
);
460 if (tdep
->have_wmmx_registers
)
461 store_wmmx_regs (regcache
);
462 if (tdep
->vfp_register_count
> 0)
463 store_vfp_regs (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 store_vfp_regs (regcache
);
481 /* Wrapper functions for the standard regset handling, used by
485 fill_gregset (const struct regcache
*regcache
,
486 gdb_gregset_t
*gregsetp
, int regno
)
488 arm_linux_collect_gregset (NULL
, regcache
, regno
, gregsetp
, 0);
492 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
494 arm_linux_supply_gregset (NULL
, regcache
, -1, gregsetp
, 0);
498 fill_fpregset (const struct regcache
*regcache
,
499 gdb_fpregset_t
*fpregsetp
, int regno
)
501 arm_linux_collect_nwfpe (NULL
, regcache
, regno
, fpregsetp
, 0);
504 /* Fill GDB's register array with the floating-point register values
508 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
510 arm_linux_supply_nwfpe (NULL
, regcache
, -1, fpregsetp
, 0);
513 /* Fetch the thread-local storage pointer for libthread_db. */
516 ps_get_thread_area (const struct ps_prochandle
*ph
,
517 lwpid_t lwpid
, int idx
, void **base
)
519 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
522 /* IDX is the bias from the thread pointer to the beginning of the
523 thread descriptor. It has to be subtracted due to implementation
524 quirks in libthread_db. */
525 *base
= (void *) ((char *)*base
- idx
);
530 static const struct target_desc
*
531 arm_linux_read_description (struct target_ops
*ops
)
533 CORE_ADDR arm_hwcap
= 0;
535 if (have_ptrace_getregset
== TRIBOOL_UNKNOWN
)
537 elf_gregset_t gpregs
;
539 int tid
= ptid_get_lwp (inferior_ptid
);
541 iov
.iov_base
= &gpregs
;
542 iov
.iov_len
= sizeof (gpregs
);
544 /* Check if PTRACE_GETREGSET works. */
545 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
) < 0)
546 have_ptrace_getregset
= TRIBOOL_FALSE
;
548 have_ptrace_getregset
= TRIBOOL_TRUE
;
551 if (target_auxv_search (ops
, AT_HWCAP
, &arm_hwcap
) != 1)
553 return ops
->beneath
->to_read_description (ops
->beneath
);
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
= ptid_get_lwp (inferior_ptid
);
578 buf
= alloca (VFP_REGS_SIZE
);
579 if (ptrace (PTRACE_GETVFPREGS
, pid
, 0, buf
) < 0
586 return ops
->beneath
->to_read_description (ops
->beneath
);
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
= ptid_get_lwp (inferior_ptid
);
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_can_use_hw_breakpoint (struct target_ops
*self
,
675 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
676 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
678 int count
= arm_linux_get_hw_watchpoint_count ();
682 else if (cnt
+ ot
> count
)
685 else if (type
== bp_hardware_breakpoint
)
687 int count
= arm_linux_get_hw_breakpoint_count ();
691 else if (cnt
> count
)
700 /* Enum describing the different types of ARM hardware break-/watch-points. */
709 /* Type describing an ARM Hardware Breakpoint Control register value. */
710 typedef unsigned int arm_hwbp_control_t
;
712 /* Structure used to keep track of hardware break-/watch-points. */
713 struct arm_linux_hw_breakpoint
715 /* Address to break on, or being watched. */
716 unsigned int address
;
717 /* Control register for break-/watch- point. */
718 arm_hwbp_control_t control
;
721 /* Structure containing arrays of per process hardware break-/watchpoints
722 for caching address and control information.
724 The Linux ptrace interface to hardware break-/watch-points presents the
725 values in a vector centred around 0 (which is used fo generic information).
726 Positive indicies refer to breakpoint addresses/control registers, negative
727 indices to watchpoint addresses/control registers.
729 The Linux vector is indexed as follows:
730 -((i << 1) + 2): Control register for watchpoint i.
731 -((i << 1) + 1): Address register for watchpoint i.
732 0: Information register.
733 ((i << 1) + 1): Address register for breakpoint i.
734 ((i << 1) + 2): Control register for breakpoint i.
736 This structure is used as a per-thread cache of the state stored by the
737 kernel, so that we don't need to keep calling into the kernel to find a
740 We treat break-/watch-points with their enable bit clear as being deleted.
742 struct arm_linux_debug_reg_state
744 /* Hardware breakpoints for this process. */
745 struct arm_linux_hw_breakpoint bpts
[MAX_BPTS
];
746 /* Hardware watchpoints for this process. */
747 struct arm_linux_hw_breakpoint wpts
[MAX_WPTS
];
750 /* Per-process arch-specific data we want to keep. */
751 struct arm_linux_process_info
754 struct arm_linux_process_info
*next
;
755 /* The process identifier. */
757 /* Hardware break-/watchpoints state information. */
758 struct arm_linux_debug_reg_state state
;
762 /* Per-thread arch-specific data we want to keep. */
765 /* Non-zero if our copy differs from what's recorded in the thread. */
766 char bpts_changed
[MAX_BPTS
];
767 char wpts_changed
[MAX_WPTS
];
770 static struct arm_linux_process_info
*arm_linux_process_list
= NULL
;
772 /* Find process data for process PID. */
774 static struct arm_linux_process_info
*
775 arm_linux_find_process_pid (pid_t pid
)
777 struct arm_linux_process_info
*proc
;
779 for (proc
= arm_linux_process_list
; proc
; proc
= proc
->next
)
780 if (proc
->pid
== pid
)
786 /* Add process data for process PID. Returns newly allocated info
789 static struct arm_linux_process_info
*
790 arm_linux_add_process (pid_t pid
)
792 struct arm_linux_process_info
*proc
;
794 proc
= xcalloc (1, sizeof (*proc
));
797 proc
->next
= arm_linux_process_list
;
798 arm_linux_process_list
= proc
;
803 /* Get data specific info for process PID, creating it if necessary.
804 Never returns NULL. */
806 static struct arm_linux_process_info
*
807 arm_linux_process_info_get (pid_t pid
)
809 struct arm_linux_process_info
*proc
;
811 proc
= arm_linux_find_process_pid (pid
);
813 proc
= arm_linux_add_process (pid
);
818 /* Called whenever GDB is no longer debugging process PID. It deletes
819 data structures that keep track of debug register state. */
822 arm_linux_forget_process (pid_t pid
)
824 struct arm_linux_process_info
*proc
, **proc_link
;
826 proc
= arm_linux_process_list
;
827 proc_link
= &arm_linux_process_list
;
831 if (proc
->pid
== pid
)
833 *proc_link
= proc
->next
;
839 proc_link
= &proc
->next
;
844 /* Get hardware break-/watchpoint state for process PID. */
846 static struct arm_linux_debug_reg_state
*
847 arm_linux_get_debug_reg_state (pid_t pid
)
849 return &arm_linux_process_info_get (pid
)->state
;
852 /* Initialize an ARM hardware break-/watch-point control register value.
853 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
854 type of break-/watch-point; ENABLE indicates whether the point is enabled.
856 static arm_hwbp_control_t
857 arm_hwbp_control_initialize (unsigned byte_address_select
,
858 arm_hwbp_type hwbp_type
,
861 gdb_assert ((byte_address_select
& ~0xffU
) == 0);
862 gdb_assert (hwbp_type
!= arm_hwbp_break
863 || ((byte_address_select
& 0xfU
) != 0));
865 return (byte_address_select
<< 5) | (hwbp_type
<< 3) | (3 << 1) | enable
;
868 /* Does the breakpoint control value CONTROL have the enable bit set? */
870 arm_hwbp_control_is_enabled (arm_hwbp_control_t control
)
872 return control
& 0x1;
875 /* Change a breakpoint control word so that it is in the disabled state. */
876 static arm_hwbp_control_t
877 arm_hwbp_control_disable (arm_hwbp_control_t control
)
879 return control
& ~0x1;
882 /* Initialise the hardware breakpoint structure P. The breakpoint will be
883 enabled, and will point to the placed address of BP_TGT. */
885 arm_linux_hw_breakpoint_initialize (struct gdbarch
*gdbarch
,
886 struct bp_target_info
*bp_tgt
,
887 struct arm_linux_hw_breakpoint
*p
)
890 CORE_ADDR address
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
892 /* We have to create a mask for the control register which says which bits
893 of the word pointed to by address to break on. */
894 if (arm_pc_is_thumb (gdbarch
, address
))
905 p
->address
= (unsigned int) address
;
906 p
->control
= arm_hwbp_control_initialize (mask
, arm_hwbp_break
, 1);
909 /* Get the ARM hardware breakpoint type from the TYPE value we're
910 given when asked to set a watchpoint. */
912 arm_linux_get_hwbp_type (enum target_hw_bp_type type
)
915 return arm_hwbp_load
;
916 else if (type
== hw_write
)
917 return arm_hwbp_store
;
919 return arm_hwbp_access
;
922 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
923 to LEN. The type of watchpoint is given in RW. */
925 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr
, int len
,
926 enum target_hw_bp_type type
,
927 struct arm_linux_hw_breakpoint
*p
)
929 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
932 gdb_assert (cap
!= NULL
);
933 gdb_assert (cap
->max_wp_length
!= 0);
935 mask
= (1 << len
) - 1;
937 p
->address
= (unsigned int) addr
;
938 p
->control
= arm_hwbp_control_initialize (mask
,
939 arm_linux_get_hwbp_type (type
), 1);
942 /* Are two break-/watch-points equal? */
944 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint
*p1
,
945 const struct arm_linux_hw_breakpoint
*p2
)
947 return p1
->address
== p2
->address
&& p1
->control
== p2
->control
;
950 /* Callback to mark a watch-/breakpoint to be updated in all threads of
951 the current process. */
953 struct update_registers_data
960 update_registers_callback (struct lwp_info
*lwp
, void *arg
)
962 struct update_registers_data
*data
= (struct update_registers_data
*) arg
;
964 if (lwp
->arch_private
== NULL
)
965 lwp
->arch_private
= XCNEW (struct arch_lwp_info
);
967 /* The actual update is done later just before resuming the lwp,
968 we just mark that the registers need updating. */
970 lwp
->arch_private
->wpts_changed
[data
->index
] = 1;
972 lwp
->arch_private
->bpts_changed
[data
->index
] = 1;
974 /* If the lwp isn't stopped, force it to momentarily pause, so
975 we can update its breakpoint registers. */
977 linux_stop_lwp (lwp
);
982 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
983 =1) BPT for thread TID. */
985 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
* bpt
,
991 struct arm_linux_hw_breakpoint
* bpts
;
992 struct update_registers_data data
;
994 pid
= ptid_get_pid (inferior_ptid
);
995 pid_ptid
= pid_to_ptid (pid
);
999 count
= arm_linux_get_hw_watchpoint_count ();
1000 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1004 count
= arm_linux_get_hw_breakpoint_count ();
1005 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1008 for (i
= 0; i
< count
; ++i
)
1009 if (!arm_hwbp_control_is_enabled (bpts
[i
].control
))
1011 data
.watch
= watchpoint
;
1014 iterate_over_lwps (pid_ptid
, update_registers_callback
, &data
);
1018 gdb_assert (i
!= count
);
1021 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1022 (WATCHPOINT = 1) BPT for thread TID. */
1024 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
*bpt
,
1030 struct arm_linux_hw_breakpoint
* bpts
;
1031 struct update_registers_data data
;
1033 pid
= ptid_get_pid (inferior_ptid
);
1034 pid_ptid
= pid_to_ptid (pid
);
1038 count
= arm_linux_get_hw_watchpoint_count ();
1039 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1043 count
= arm_linux_get_hw_breakpoint_count ();
1044 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1047 for (i
= 0; i
< count
; ++i
)
1048 if (arm_linux_hw_breakpoint_equal (bpt
, bpts
+ i
))
1050 data
.watch
= watchpoint
;
1052 bpts
[i
].control
= arm_hwbp_control_disable (bpts
[i
].control
);
1053 iterate_over_lwps (pid_ptid
, update_registers_callback
, &data
);
1057 gdb_assert (i
!= count
);
1060 /* Insert a Hardware breakpoint. */
1062 arm_linux_insert_hw_breakpoint (struct target_ops
*self
,
1063 struct gdbarch
*gdbarch
,
1064 struct bp_target_info
*bp_tgt
)
1066 struct lwp_info
*lp
;
1067 struct arm_linux_hw_breakpoint p
;
1069 if (arm_linux_get_hw_breakpoint_count () == 0)
1072 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1074 arm_linux_insert_hw_breakpoint1 (&p
, 0);
1079 /* Remove a hardware breakpoint. */
1081 arm_linux_remove_hw_breakpoint (struct target_ops
*self
,
1082 struct gdbarch
*gdbarch
,
1083 struct bp_target_info
*bp_tgt
)
1085 struct lwp_info
*lp
;
1086 struct arm_linux_hw_breakpoint p
;
1088 if (arm_linux_get_hw_breakpoint_count () == 0)
1091 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1093 arm_linux_remove_hw_breakpoint1 (&p
, 0);
1098 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1101 arm_linux_region_ok_for_hw_watchpoint (struct target_ops
*self
,
1102 CORE_ADDR addr
, int len
)
1104 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
1105 CORE_ADDR max_wp_length
, aligned_addr
;
1107 /* Can not set watchpoints for zero or negative lengths. */
1111 /* Need to be able to use the ptrace interface. */
1112 if (cap
== NULL
|| cap
->wp_count
== 0)
1115 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1116 range covered by a watchpoint. */
1117 max_wp_length
= (CORE_ADDR
)cap
->max_wp_length
;
1118 aligned_addr
= addr
& ~(max_wp_length
- 1);
1120 if (aligned_addr
+ max_wp_length
< addr
+ len
)
1123 /* The current ptrace interface can only handle watchpoints that are a
1125 if ((len
& (len
- 1)) != 0)
1128 /* All tests passed so we must be able to set a watchpoint. */
1132 /* Insert a Hardware breakpoint. */
1134 arm_linux_insert_watchpoint (struct target_ops
*self
,
1135 CORE_ADDR addr
, int len
,
1136 enum target_hw_bp_type rw
,
1137 struct expression
*cond
)
1139 struct lwp_info
*lp
;
1140 struct arm_linux_hw_breakpoint p
;
1142 if (arm_linux_get_hw_watchpoint_count () == 0)
1145 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1147 arm_linux_insert_hw_breakpoint1 (&p
, 1);
1152 /* Remove a hardware breakpoint. */
1154 arm_linux_remove_watchpoint (struct target_ops
*self
, CORE_ADDR addr
,
1155 int len
, enum target_hw_bp_type rw
,
1156 struct expression
*cond
)
1158 struct lwp_info
*lp
;
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_stopped_data_address (struct target_ops
*target
, 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_stopped_by_watchpoint (struct target_ops
*ops
)
1206 return arm_linux_stopped_data_address (ops
, &addr
);
1210 arm_linux_watchpoint_addr_within_range (struct target_ops
*target
,
1212 CORE_ADDR start
, int length
)
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_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 /* Called when resuming a thread.
1238 The hardware debug registers are updated when there is any change. */
1241 arm_linux_prepare_to_resume (struct lwp_info
*lwp
)
1244 struct arm_linux_hw_breakpoint
*bpts
, *wpts
;
1245 struct arch_lwp_info
*arm_lwp_info
= lwp
->arch_private
;
1247 pid
= ptid_get_lwp (lwp
->ptid
);
1248 bpts
= arm_linux_get_debug_reg_state (ptid_get_pid (lwp
->ptid
))->bpts
;
1249 wpts
= arm_linux_get_debug_reg_state (ptid_get_pid (lwp
->ptid
))->wpts
;
1251 /* NULL means this is the main thread still going through the shell,
1252 or, no watchpoint has been set yet. In that case, there's
1254 if (arm_lwp_info
== NULL
)
1257 for (i
= 0; i
< arm_linux_get_hw_breakpoint_count (); i
++)
1258 if (arm_lwp_info
->bpts_changed
[i
])
1261 if (arm_hwbp_control_is_enabled (bpts
[i
].control
))
1262 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1263 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 1), &bpts
[i
].address
) < 0)
1264 perror_with_name (_("Unexpected error setting breakpoint"));
1266 if (bpts
[i
].control
!= 0)
1267 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1268 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 2), &bpts
[i
].control
) < 0)
1269 perror_with_name (_("Unexpected error setting breakpoint"));
1271 arm_lwp_info
->bpts_changed
[i
] = 0;
1274 for (i
= 0; i
< arm_linux_get_hw_watchpoint_count (); i
++)
1275 if (arm_lwp_info
->wpts_changed
[i
])
1278 if (arm_hwbp_control_is_enabled (wpts
[i
].control
))
1279 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1280 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 1), &wpts
[i
].address
) < 0)
1281 perror_with_name (_("Unexpected error setting watchpoint"));
1283 if (wpts
[i
].control
!= 0)
1284 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1285 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 2), &wpts
[i
].control
) < 0)
1286 perror_with_name (_("Unexpected error setting watchpoint"));
1288 arm_lwp_info
->wpts_changed
[i
] = 0;
1292 /* linux_nat_new_fork hook. */
1295 arm_linux_new_fork (struct lwp_info
*parent
, pid_t child_pid
)
1298 struct arm_linux_debug_reg_state
*parent_state
;
1299 struct arm_linux_debug_reg_state
*child_state
;
1301 /* NULL means no watchpoint has ever been set in the parent. In
1302 that case, there's nothing to do. */
1303 if (parent
->arch_private
== NULL
)
1306 /* GDB core assumes the child inherits the watchpoints/hw
1307 breakpoints of the parent, and will remove them all from the
1308 forked off process. Copy the debug registers mirrors into the
1309 new process so that all breakpoints and watchpoints can be
1310 removed together. */
1312 parent_pid
= ptid_get_pid (parent
->ptid
);
1313 parent_state
= arm_linux_get_debug_reg_state (parent_pid
);
1314 child_state
= arm_linux_get_debug_reg_state (child_pid
);
1315 *child_state
= *parent_state
;
1318 void _initialize_arm_linux_nat (void);
1321 _initialize_arm_linux_nat (void)
1323 struct target_ops
*t
;
1325 /* Fill in the generic GNU/Linux methods. */
1326 t
= linux_target ();
1328 /* Add our register access methods. */
1329 t
->to_fetch_registers
= arm_linux_fetch_inferior_registers
;
1330 t
->to_store_registers
= arm_linux_store_inferior_registers
;
1332 /* Add our hardware breakpoint and watchpoint implementation. */
1333 t
->to_can_use_hw_breakpoint
= arm_linux_can_use_hw_breakpoint
;
1334 t
->to_insert_hw_breakpoint
= arm_linux_insert_hw_breakpoint
;
1335 t
->to_remove_hw_breakpoint
= arm_linux_remove_hw_breakpoint
;
1336 t
->to_region_ok_for_hw_watchpoint
= arm_linux_region_ok_for_hw_watchpoint
;
1337 t
->to_insert_watchpoint
= arm_linux_insert_watchpoint
;
1338 t
->to_remove_watchpoint
= arm_linux_remove_watchpoint
;
1339 t
->to_stopped_by_watchpoint
= arm_linux_stopped_by_watchpoint
;
1340 t
->to_stopped_data_address
= arm_linux_stopped_data_address
;
1341 t
->to_watchpoint_addr_within_range
= arm_linux_watchpoint_addr_within_range
;
1343 t
->to_read_description
= arm_linux_read_description
;
1345 /* Register the target. */
1346 linux_nat_add_target (t
);
1348 /* Handle thread creation and exit. */
1349 linux_nat_set_new_thread (t
, arm_linux_new_thread
);
1350 linux_nat_set_prepare_to_resume (t
, arm_linux_prepare_to_resume
);
1352 /* Handle process creation and exit. */
1353 linux_nat_set_new_fork (t
, arm_linux_new_fork
);
1354 linux_nat_set_forget_process (t
, arm_linux_forget_process
);