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 <sys/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 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
70 case we may be tracing more than one process at a time. In that
71 case, inferior_ptid will contain the main process ID and the
72 individual thread (process) ID. get_thread_id () is used to get
73 the thread id if it's available, and the process id otherwise. */
76 get_thread_id (ptid_t ptid
)
78 int tid
= ptid_get_lwp (ptid
);
80 tid
= ptid_get_pid (ptid
);
84 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
86 /* Get the whole floating point state of the process and store it
90 fetch_fpregs (struct regcache
*regcache
)
93 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
95 /* Get the thread id for the ptrace call. */
96 tid
= GET_THREAD_ID (inferior_ptid
);
98 /* Read the floating point state. */
99 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
104 iov
.iov_len
= ARM_LINUX_SIZEOF_NWFPE
;
106 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iov
);
109 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
113 warning (_("Unable to fetch the floating point registers."));
118 regcache_raw_supply (regcache
, ARM_FPS_REGNUM
,
119 fp
+ NWFPE_FPSR_OFFSET
);
121 /* Fetch the floating point registers. */
122 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
123 supply_nwfpe_register (regcache
, regno
, fp
);
126 /* Save the whole floating point state of the process using
127 the contents from regcache. */
130 store_fpregs (const struct regcache
*regcache
)
133 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
135 /* Get the thread id for the ptrace call. */
136 tid
= GET_THREAD_ID (inferior_ptid
);
138 /* Read the floating point state. */
139 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
141 elf_fpregset_t fpregs
;
144 iov
.iov_base
= &fpregs
;
145 iov
.iov_len
= sizeof (fpregs
);
147 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iov
);
150 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
154 warning (_("Unable to fetch the floating point registers."));
159 if (REG_VALID
== regcache_register_status (regcache
, ARM_FPS_REGNUM
))
160 regcache_raw_collect (regcache
, ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
162 /* Store the floating point registers. */
163 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
164 if (REG_VALID
== regcache_register_status (regcache
, regno
))
165 collect_nwfpe_register (regcache
, regno
, fp
);
167 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
172 iov
.iov_len
= ARM_LINUX_SIZEOF_NWFPE
;
174 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_FPREGSET
, &iov
);
177 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
181 warning (_("Unable to store floating point registers."));
186 /* Fetch all general registers of the process and store into
190 fetch_regs (struct regcache
*regcache
)
195 /* Get the thread id for the ptrace call. */
196 tid
= GET_THREAD_ID (inferior_ptid
);
198 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
202 iov
.iov_base
= ®s
;
203 iov
.iov_len
= sizeof (regs
);
205 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
);
208 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
212 warning (_("Unable to fetch general registers."));
216 aarch32_gp_regcache_supply (regcache
, (uint32_t *) regs
, arm_apcs_32
);
220 store_regs (const struct regcache
*regcache
)
225 /* Get the thread id for the ptrace call. */
226 tid
= GET_THREAD_ID (inferior_ptid
);
228 /* Fetch the general registers. */
229 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
233 iov
.iov_base
= ®s
;
234 iov
.iov_len
= sizeof (regs
);
236 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
);
239 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
243 warning (_("Unable to fetch general registers."));
247 aarch32_gp_regcache_collect (regcache
, (uint32_t *) regs
, arm_apcs_32
);
249 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
253 iov
.iov_base
= ®s
;
254 iov
.iov_len
= sizeof (regs
);
256 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, &iov
);
259 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
263 warning (_("Unable to store general registers."));
268 /* Fetch all WMMX registers of the process and store into
271 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
274 fetch_wmmx_regs (struct regcache
*regcache
)
276 char regbuf
[IWMMXT_REGS_SIZE
];
279 /* Get the thread id for the ptrace call. */
280 tid
= GET_THREAD_ID (inferior_ptid
);
282 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
285 warning (_("Unable to fetch WMMX registers."));
289 for (regno
= 0; regno
< 16; regno
++)
290 regcache_raw_supply (regcache
, regno
+ ARM_WR0_REGNUM
,
293 for (regno
= 0; regno
< 2; regno
++)
294 regcache_raw_supply (regcache
, regno
+ ARM_WCSSF_REGNUM
,
295 ®buf
[16 * 8 + regno
* 4]);
297 for (regno
= 0; regno
< 4; regno
++)
298 regcache_raw_supply (regcache
, regno
+ ARM_WCGR0_REGNUM
,
299 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
303 store_wmmx_regs (const struct regcache
*regcache
)
305 char regbuf
[IWMMXT_REGS_SIZE
];
308 /* Get the thread id for the ptrace call. */
309 tid
= GET_THREAD_ID (inferior_ptid
);
311 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
314 warning (_("Unable to fetch WMMX registers."));
318 for (regno
= 0; regno
< 16; regno
++)
319 if (REG_VALID
== regcache_register_status (regcache
,
320 regno
+ ARM_WR0_REGNUM
))
321 regcache_raw_collect (regcache
, regno
+ ARM_WR0_REGNUM
,
324 for (regno
= 0; regno
< 2; regno
++)
325 if (REG_VALID
== regcache_register_status (regcache
,
326 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_register_status (regcache
,
332 regno
+ ARM_WCGR0_REGNUM
))
333 regcache_raw_collect (regcache
, regno
+ ARM_WCGR0_REGNUM
,
334 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
336 ret
= ptrace (PTRACE_SETWMMXREGS
, tid
, 0, regbuf
);
340 warning (_("Unable to store WMMX registers."));
345 /* Fetch and store VFP Registers. The kernel object has space for 32
346 64-bit registers, and the FPSCR. This is even when on a VFPv2 or
348 #define VFP_REGS_SIZE (32 * 8 + 4)
351 fetch_vfp_regs (struct regcache
*regcache
)
353 gdb_byte regbuf
[VFP_REGS_SIZE
];
355 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
356 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
358 /* Get the thread id for the ptrace call. */
359 tid
= GET_THREAD_ID (inferior_ptid
);
361 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
365 iov
.iov_base
= regbuf
;
366 iov
.iov_len
= VFP_REGS_SIZE
;
367 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
370 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
374 warning (_("Unable to fetch VFP registers."));
378 aarch32_vfp_regcache_supply (regcache
, regbuf
,
379 tdep
->vfp_register_count
);
383 store_vfp_regs (const struct regcache
*regcache
)
385 gdb_byte regbuf
[VFP_REGS_SIZE
];
387 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
388 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
390 /* Get the thread id for the ptrace call. */
391 tid
= GET_THREAD_ID (inferior_ptid
);
393 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
397 iov
.iov_base
= regbuf
;
398 iov
.iov_len
= VFP_REGS_SIZE
;
399 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
402 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
406 warning (_("Unable to fetch VFP registers (for update)."));
410 aarch32_vfp_regcache_collect (regcache
, regbuf
,
411 tdep
->vfp_register_count
);
413 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
417 iov
.iov_base
= regbuf
;
418 iov
.iov_len
= VFP_REGS_SIZE
;
419 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iov
);
422 ret
= ptrace (PTRACE_SETVFPREGS
, tid
, 0, regbuf
);
426 warning (_("Unable to store VFP registers."));
431 /* Fetch registers from the child process. Fetch all registers if
432 regno == -1, otherwise fetch all general registers or all floating
433 point registers depending upon the value of regno. */
436 arm_linux_fetch_inferior_registers (struct target_ops
*ops
,
437 struct regcache
*regcache
, int regno
)
439 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
440 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
444 fetch_regs (regcache
);
445 fetch_fpregs (regcache
);
446 if (tdep
->have_wmmx_registers
)
447 fetch_wmmx_regs (regcache
);
448 if (tdep
->vfp_register_count
> 0)
449 fetch_vfp_regs (regcache
);
453 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
454 fetch_regs (regcache
);
455 else if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
456 fetch_fpregs (regcache
);
457 else if (tdep
->have_wmmx_registers
458 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
459 fetch_wmmx_regs (regcache
);
460 else if (tdep
->vfp_register_count
> 0
461 && regno
>= ARM_D0_REGNUM
462 && regno
<= ARM_D0_REGNUM
+ tdep
->vfp_register_count
)
463 fetch_vfp_regs (regcache
);
467 /* Store registers back into the inferior. Store all registers if
468 regno == -1, otherwise store all general registers or all floating
469 point registers depending upon the value of regno. */
472 arm_linux_store_inferior_registers (struct target_ops
*ops
,
473 struct regcache
*regcache
, int regno
)
475 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
476 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
480 store_regs (regcache
);
481 store_fpregs (regcache
);
482 if (tdep
->have_wmmx_registers
)
483 store_wmmx_regs (regcache
);
484 if (tdep
->vfp_register_count
> 0)
485 store_vfp_regs (regcache
);
489 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
490 store_regs (regcache
);
491 else if ((regno
>= ARM_F0_REGNUM
) && (regno
<= ARM_FPS_REGNUM
))
492 store_fpregs (regcache
);
493 else if (tdep
->have_wmmx_registers
494 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
495 store_wmmx_regs (regcache
);
496 else if (tdep
->vfp_register_count
> 0
497 && regno
>= ARM_D0_REGNUM
498 && regno
<= ARM_D0_REGNUM
+ tdep
->vfp_register_count
)
499 store_vfp_regs (regcache
);
503 /* Wrapper functions for the standard regset handling, used by
507 fill_gregset (const struct regcache
*regcache
,
508 gdb_gregset_t
*gregsetp
, int regno
)
510 arm_linux_collect_gregset (NULL
, regcache
, regno
, gregsetp
, 0);
514 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
516 arm_linux_supply_gregset (NULL
, regcache
, -1, gregsetp
, 0);
520 fill_fpregset (const struct regcache
*regcache
,
521 gdb_fpregset_t
*fpregsetp
, int regno
)
523 arm_linux_collect_nwfpe (NULL
, regcache
, regno
, fpregsetp
, 0);
526 /* Fill GDB's register array with the floating-point register values
530 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
532 arm_linux_supply_nwfpe (NULL
, regcache
, -1, fpregsetp
, 0);
535 /* Fetch the thread-local storage pointer for libthread_db. */
538 ps_get_thread_area (const struct ps_prochandle
*ph
,
539 lwpid_t lwpid
, int idx
, void **base
)
541 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
544 /* IDX is the bias from the thread pointer to the beginning of the
545 thread descriptor. It has to be subtracted due to implementation
546 quirks in libthread_db. */
547 *base
= (void *) ((char *)*base
- idx
);
552 static const struct target_desc
*
553 arm_linux_read_description (struct target_ops
*ops
)
555 CORE_ADDR arm_hwcap
= 0;
557 if (have_ptrace_getregset
== TRIBOOL_UNKNOWN
)
559 elf_gregset_t gpregs
;
561 int tid
= GET_THREAD_ID (inferior_ptid
);
563 iov
.iov_base
= &gpregs
;
564 iov
.iov_len
= sizeof (gpregs
);
566 /* Check if PTRACE_GETREGSET works. */
567 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
) < 0)
568 have_ptrace_getregset
= TRIBOOL_FALSE
;
570 have_ptrace_getregset
= TRIBOOL_TRUE
;
573 if (target_auxv_search (ops
, AT_HWCAP
, &arm_hwcap
) != 1)
575 return ops
->beneath
->to_read_description (ops
->beneath
);
578 if (arm_hwcap
& HWCAP_IWMMXT
)
579 return tdesc_arm_with_iwmmxt
;
581 if (arm_hwcap
& HWCAP_VFP
)
585 const struct target_desc
* result
= NULL
;
587 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
588 Neon with VFPv3-D32. */
589 if (arm_hwcap
& HWCAP_NEON
)
590 result
= tdesc_arm_with_neon
;
591 else if ((arm_hwcap
& (HWCAP_VFPv3
| HWCAP_VFPv3D16
)) == HWCAP_VFPv3
)
592 result
= tdesc_arm_with_vfpv3
;
594 result
= tdesc_arm_with_vfpv2
;
596 /* Now make sure that the kernel supports reading these
597 registers. Support was added in 2.6.30. */
598 pid
= ptid_get_lwp (inferior_ptid
);
600 buf
= alloca (VFP_REGS_SIZE
);
601 if (ptrace (PTRACE_GETVFPREGS
, pid
, 0, buf
) < 0
608 return ops
->beneath
->to_read_description (ops
->beneath
);
611 /* Information describing the hardware breakpoint capabilities. */
612 struct arm_linux_hwbp_cap
615 gdb_byte max_wp_length
;
620 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
621 assume a maximum number of supported break-/watchpoints. */
625 /* Get hold of the Hardware Breakpoint information for the target we are
626 attached to. Returns NULL if the kernel doesn't support Hardware
627 breakpoints at all, or a pointer to the information structure. */
628 static const struct arm_linux_hwbp_cap
*
629 arm_linux_get_hwbp_cap (void)
631 /* The info structure we return. */
632 static struct arm_linux_hwbp_cap info
;
634 /* Is INFO in a good state? -1 means that no attempt has been made to
635 initialize INFO; 0 means an attempt has been made, but it failed; 1
636 means INFO is in an initialized state. */
637 static int available
= -1;
644 tid
= GET_THREAD_ID (inferior_ptid
);
645 if (ptrace (PTRACE_GETHBPREGS
, tid
, 0, &val
) < 0)
649 info
.arch
= (gdb_byte
)((val
>> 24) & 0xff);
650 info
.max_wp_length
= (gdb_byte
)((val
>> 16) & 0xff);
651 info
.wp_count
= (gdb_byte
)((val
>> 8) & 0xff);
652 info
.bp_count
= (gdb_byte
)(val
& 0xff);
654 if (info
.wp_count
> MAX_WPTS
)
656 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
657 supports %d"), MAX_WPTS
, info
.wp_count
);
658 info
.wp_count
= MAX_WPTS
;
661 if (info
.bp_count
> MAX_BPTS
)
663 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
664 supports %d"), MAX_BPTS
, info
.bp_count
);
665 info
.bp_count
= MAX_BPTS
;
667 available
= (info
.arch
!= 0);
671 return available
== 1 ? &info
: NULL
;
674 /* How many hardware breakpoints are available? */
676 arm_linux_get_hw_breakpoint_count (void)
678 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
679 return cap
!= NULL
? cap
->bp_count
: 0;
682 /* How many hardware watchpoints are available? */
684 arm_linux_get_hw_watchpoint_count (void)
686 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
687 return cap
!= NULL
? cap
->wp_count
: 0;
690 /* Have we got a free break-/watch-point available for use? Returns -1 if
691 there is not an appropriate resource available, otherwise returns 1. */
693 arm_linux_can_use_hw_breakpoint (struct target_ops
*self
,
694 int type
, int cnt
, int ot
)
696 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
697 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
699 int count
= arm_linux_get_hw_watchpoint_count ();
703 else if (cnt
+ ot
> count
)
706 else if (type
== bp_hardware_breakpoint
)
708 int count
= arm_linux_get_hw_breakpoint_count ();
712 else if (cnt
> count
)
721 /* Enum describing the different types of ARM hardware break-/watch-points. */
730 /* Type describing an ARM Hardware Breakpoint Control register value. */
731 typedef unsigned int arm_hwbp_control_t
;
733 /* Structure used to keep track of hardware break-/watch-points. */
734 struct arm_linux_hw_breakpoint
736 /* Address to break on, or being watched. */
737 unsigned int address
;
738 /* Control register for break-/watch- point. */
739 arm_hwbp_control_t control
;
742 /* Structure containing arrays of per process hardware break-/watchpoints
743 for caching address and control information.
745 The Linux ptrace interface to hardware break-/watch-points presents the
746 values in a vector centred around 0 (which is used fo generic information).
747 Positive indicies refer to breakpoint addresses/control registers, negative
748 indices to watchpoint addresses/control registers.
750 The Linux vector is indexed as follows:
751 -((i << 1) + 2): Control register for watchpoint i.
752 -((i << 1) + 1): Address register for watchpoint i.
753 0: Information register.
754 ((i << 1) + 1): Address register for breakpoint i.
755 ((i << 1) + 2): Control register for breakpoint i.
757 This structure is used as a per-thread cache of the state stored by the
758 kernel, so that we don't need to keep calling into the kernel to find a
761 We treat break-/watch-points with their enable bit clear as being deleted.
763 struct arm_linux_debug_reg_state
765 /* Hardware breakpoints for this process. */
766 struct arm_linux_hw_breakpoint bpts
[MAX_BPTS
];
767 /* Hardware watchpoints for this process. */
768 struct arm_linux_hw_breakpoint wpts
[MAX_WPTS
];
771 /* Per-process arch-specific data we want to keep. */
772 struct arm_linux_process_info
775 struct arm_linux_process_info
*next
;
776 /* The process identifier. */
778 /* Hardware break-/watchpoints state information. */
779 struct arm_linux_debug_reg_state state
;
783 /* Per-thread arch-specific data we want to keep. */
786 /* Non-zero if our copy differs from what's recorded in the thread. */
787 char bpts_changed
[MAX_BPTS
];
788 char wpts_changed
[MAX_WPTS
];
791 static struct arm_linux_process_info
*arm_linux_process_list
= NULL
;
793 /* Find process data for process PID. */
795 static struct arm_linux_process_info
*
796 arm_linux_find_process_pid (pid_t pid
)
798 struct arm_linux_process_info
*proc
;
800 for (proc
= arm_linux_process_list
; proc
; proc
= proc
->next
)
801 if (proc
->pid
== pid
)
807 /* Add process data for process PID. Returns newly allocated info
810 static struct arm_linux_process_info
*
811 arm_linux_add_process (pid_t pid
)
813 struct arm_linux_process_info
*proc
;
815 proc
= xcalloc (1, sizeof (*proc
));
818 proc
->next
= arm_linux_process_list
;
819 arm_linux_process_list
= proc
;
824 /* Get data specific info for process PID, creating it if necessary.
825 Never returns NULL. */
827 static struct arm_linux_process_info
*
828 arm_linux_process_info_get (pid_t pid
)
830 struct arm_linux_process_info
*proc
;
832 proc
= arm_linux_find_process_pid (pid
);
834 proc
= arm_linux_add_process (pid
);
839 /* Called whenever GDB is no longer debugging process PID. It deletes
840 data structures that keep track of debug register state. */
843 arm_linux_forget_process (pid_t pid
)
845 struct arm_linux_process_info
*proc
, **proc_link
;
847 proc
= arm_linux_process_list
;
848 proc_link
= &arm_linux_process_list
;
852 if (proc
->pid
== pid
)
854 *proc_link
= proc
->next
;
860 proc_link
= &proc
->next
;
865 /* Get hardware break-/watchpoint state for process PID. */
867 static struct arm_linux_debug_reg_state
*
868 arm_linux_get_debug_reg_state (pid_t pid
)
870 return &arm_linux_process_info_get (pid
)->state
;
873 /* Initialize an ARM hardware break-/watch-point control register value.
874 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
875 type of break-/watch-point; ENABLE indicates whether the point is enabled.
877 static arm_hwbp_control_t
878 arm_hwbp_control_initialize (unsigned byte_address_select
,
879 arm_hwbp_type hwbp_type
,
882 gdb_assert ((byte_address_select
& ~0xffU
) == 0);
883 gdb_assert (hwbp_type
!= arm_hwbp_break
884 || ((byte_address_select
& 0xfU
) != 0));
886 return (byte_address_select
<< 5) | (hwbp_type
<< 3) | (3 << 1) | enable
;
889 /* Does the breakpoint control value CONTROL have the enable bit set? */
891 arm_hwbp_control_is_enabled (arm_hwbp_control_t control
)
893 return control
& 0x1;
896 /* Change a breakpoint control word so that it is in the disabled state. */
897 static arm_hwbp_control_t
898 arm_hwbp_control_disable (arm_hwbp_control_t control
)
900 return control
& ~0x1;
903 /* Initialise the hardware breakpoint structure P. The breakpoint will be
904 enabled, and will point to the placed address of BP_TGT. */
906 arm_linux_hw_breakpoint_initialize (struct gdbarch
*gdbarch
,
907 struct bp_target_info
*bp_tgt
,
908 struct arm_linux_hw_breakpoint
*p
)
911 CORE_ADDR address
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
913 /* We have to create a mask for the control register which says which bits
914 of the word pointed to by address to break on. */
915 if (arm_pc_is_thumb (gdbarch
, address
))
926 p
->address
= (unsigned int) address
;
927 p
->control
= arm_hwbp_control_initialize (mask
, arm_hwbp_break
, 1);
930 /* Get the ARM hardware breakpoint type from the RW value we're given when
931 asked to set a watchpoint. */
933 arm_linux_get_hwbp_type (int rw
)
936 return arm_hwbp_load
;
937 else if (rw
== hw_write
)
938 return arm_hwbp_store
;
940 return arm_hwbp_access
;
943 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
944 to LEN. The type of watchpoint is given in RW. */
946 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr
, int len
, int rw
,
947 struct arm_linux_hw_breakpoint
*p
)
949 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
952 gdb_assert (cap
!= NULL
);
953 gdb_assert (cap
->max_wp_length
!= 0);
955 mask
= (1 << len
) - 1;
957 p
->address
= (unsigned int) addr
;
958 p
->control
= arm_hwbp_control_initialize (mask
,
959 arm_linux_get_hwbp_type (rw
), 1);
962 /* Are two break-/watch-points equal? */
964 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint
*p1
,
965 const struct arm_linux_hw_breakpoint
*p2
)
967 return p1
->address
== p2
->address
&& p1
->control
== p2
->control
;
970 /* Callback to mark a watch-/breakpoint to be updated in all threads of
971 the current process. */
973 struct update_registers_data
980 update_registers_callback (struct lwp_info
*lwp
, void *arg
)
982 struct update_registers_data
*data
= (struct update_registers_data
*) arg
;
984 if (lwp
->arch_private
== NULL
)
985 lwp
->arch_private
= XCNEW (struct arch_lwp_info
);
987 /* The actual update is done later just before resuming the lwp,
988 we just mark that the registers need updating. */
990 lwp
->arch_private
->wpts_changed
[data
->index
] = 1;
992 lwp
->arch_private
->bpts_changed
[data
->index
] = 1;
994 /* If the lwp isn't stopped, force it to momentarily pause, so
995 we can update its breakpoint registers. */
997 linux_stop_lwp (lwp
);
1002 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
1003 =1) BPT for thread TID. */
1005 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
* bpt
,
1011 struct arm_linux_hw_breakpoint
* bpts
;
1012 struct update_registers_data data
;
1014 pid
= ptid_get_pid (inferior_ptid
);
1015 pid_ptid
= pid_to_ptid (pid
);
1019 count
= arm_linux_get_hw_watchpoint_count ();
1020 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1024 count
= arm_linux_get_hw_breakpoint_count ();
1025 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1028 for (i
= 0; i
< count
; ++i
)
1029 if (!arm_hwbp_control_is_enabled (bpts
[i
].control
))
1031 data
.watch
= watchpoint
;
1034 iterate_over_lwps (pid_ptid
, update_registers_callback
, &data
);
1038 gdb_assert (i
!= count
);
1041 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1042 (WATCHPOINT = 1) BPT for thread TID. */
1044 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
*bpt
,
1050 struct arm_linux_hw_breakpoint
* bpts
;
1051 struct update_registers_data data
;
1053 pid
= ptid_get_pid (inferior_ptid
);
1054 pid_ptid
= pid_to_ptid (pid
);
1058 count
= arm_linux_get_hw_watchpoint_count ();
1059 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1063 count
= arm_linux_get_hw_breakpoint_count ();
1064 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1067 for (i
= 0; i
< count
; ++i
)
1068 if (arm_linux_hw_breakpoint_equal (bpt
, bpts
+ i
))
1070 data
.watch
= watchpoint
;
1072 bpts
[i
].control
= arm_hwbp_control_disable (bpts
[i
].control
);
1073 iterate_over_lwps (pid_ptid
, update_registers_callback
, &data
);
1077 gdb_assert (i
!= count
);
1080 /* Insert a Hardware breakpoint. */
1082 arm_linux_insert_hw_breakpoint (struct target_ops
*self
,
1083 struct gdbarch
*gdbarch
,
1084 struct bp_target_info
*bp_tgt
)
1086 struct lwp_info
*lp
;
1087 struct arm_linux_hw_breakpoint p
;
1089 if (arm_linux_get_hw_breakpoint_count () == 0)
1092 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1094 arm_linux_insert_hw_breakpoint1 (&p
, 0);
1099 /* Remove a hardware breakpoint. */
1101 arm_linux_remove_hw_breakpoint (struct target_ops
*self
,
1102 struct gdbarch
*gdbarch
,
1103 struct bp_target_info
*bp_tgt
)
1105 struct lwp_info
*lp
;
1106 struct arm_linux_hw_breakpoint p
;
1108 if (arm_linux_get_hw_breakpoint_count () == 0)
1111 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1113 arm_linux_remove_hw_breakpoint1 (&p
, 0);
1118 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1121 arm_linux_region_ok_for_hw_watchpoint (struct target_ops
*self
,
1122 CORE_ADDR addr
, int len
)
1124 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
1125 CORE_ADDR max_wp_length
, aligned_addr
;
1127 /* Can not set watchpoints for zero or negative lengths. */
1131 /* Need to be able to use the ptrace interface. */
1132 if (cap
== NULL
|| cap
->wp_count
== 0)
1135 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1136 range covered by a watchpoint. */
1137 max_wp_length
= (CORE_ADDR
)cap
->max_wp_length
;
1138 aligned_addr
= addr
& ~(max_wp_length
- 1);
1140 if (aligned_addr
+ max_wp_length
< addr
+ len
)
1143 /* The current ptrace interface can only handle watchpoints that are a
1145 if ((len
& (len
- 1)) != 0)
1148 /* All tests passed so we must be able to set a watchpoint. */
1152 /* Insert a Hardware breakpoint. */
1154 arm_linux_insert_watchpoint (struct target_ops
*self
,
1155 CORE_ADDR addr
, int len
, int 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_insert_hw_breakpoint1 (&p
, 1);
1171 /* Remove a hardware breakpoint. */
1173 arm_linux_remove_watchpoint (struct target_ops
*self
,
1174 CORE_ADDR addr
, int len
, int rw
,
1175 struct expression
*cond
)
1177 struct lwp_info
*lp
;
1178 struct arm_linux_hw_breakpoint p
;
1180 if (arm_linux_get_hw_watchpoint_count () == 0)
1183 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1185 arm_linux_remove_hw_breakpoint1 (&p
, 1);
1190 /* What was the data address the target was stopped on accessing. */
1192 arm_linux_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
1197 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
1200 /* This must be a hardware breakpoint. */
1201 if (siginfo
.si_signo
!= SIGTRAP
1202 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1205 /* We must be able to set hardware watchpoints. */
1206 if (arm_linux_get_hw_watchpoint_count () == 0)
1209 slot
= siginfo
.si_errno
;
1211 /* If we are in a positive slot then we're looking at a breakpoint and not
1216 *addr_p
= (CORE_ADDR
) (uintptr_t) siginfo
.si_addr
;
1220 /* Has the target been stopped by hitting a watchpoint? */
1222 arm_linux_stopped_by_watchpoint (struct target_ops
*ops
)
1225 return arm_linux_stopped_data_address (ops
, &addr
);
1229 arm_linux_watchpoint_addr_within_range (struct target_ops
*target
,
1231 CORE_ADDR start
, int length
)
1233 return start
<= addr
&& start
+ length
- 1 >= addr
;
1236 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1237 in the parent thread to the child thread. */
1239 arm_linux_new_thread (struct lwp_info
*lp
)
1242 struct arch_lwp_info
*info
= XCNEW (struct arch_lwp_info
);
1244 /* Mark that all the hardware breakpoint/watchpoint register pairs
1245 for this thread need to be initialized. */
1247 for (i
= 0; i
< MAX_BPTS
; i
++)
1249 info
->bpts_changed
[i
] = 1;
1250 info
->wpts_changed
[i
] = 1;
1253 lp
->arch_private
= info
;
1256 /* Called when resuming a thread.
1257 The hardware debug registers are updated when there is any change. */
1260 arm_linux_prepare_to_resume (struct lwp_info
*lwp
)
1263 struct arm_linux_hw_breakpoint
*bpts
, *wpts
;
1264 struct arch_lwp_info
*arm_lwp_info
= lwp
->arch_private
;
1266 pid
= ptid_get_lwp (lwp
->ptid
);
1267 bpts
= arm_linux_get_debug_reg_state (ptid_get_pid (lwp
->ptid
))->bpts
;
1268 wpts
= arm_linux_get_debug_reg_state (ptid_get_pid (lwp
->ptid
))->wpts
;
1270 /* NULL means this is the main thread still going through the shell,
1271 or, no watchpoint has been set yet. In that case, there's
1273 if (arm_lwp_info
== NULL
)
1276 for (i
= 0; i
< arm_linux_get_hw_breakpoint_count (); i
++)
1277 if (arm_lwp_info
->bpts_changed
[i
])
1280 if (arm_hwbp_control_is_enabled (bpts
[i
].control
))
1281 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1282 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 1), &bpts
[i
].address
) < 0)
1283 perror_with_name (_("Unexpected error setting breakpoint"));
1285 if (bpts
[i
].control
!= 0)
1286 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1287 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 2), &bpts
[i
].control
) < 0)
1288 perror_with_name (_("Unexpected error setting breakpoint"));
1290 arm_lwp_info
->bpts_changed
[i
] = 0;
1293 for (i
= 0; i
< arm_linux_get_hw_watchpoint_count (); i
++)
1294 if (arm_lwp_info
->wpts_changed
[i
])
1297 if (arm_hwbp_control_is_enabled (wpts
[i
].control
))
1298 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1299 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 1), &wpts
[i
].address
) < 0)
1300 perror_with_name (_("Unexpected error setting watchpoint"));
1302 if (wpts
[i
].control
!= 0)
1303 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1304 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 2), &wpts
[i
].control
) < 0)
1305 perror_with_name (_("Unexpected error setting watchpoint"));
1307 arm_lwp_info
->wpts_changed
[i
] = 0;
1311 /* linux_nat_new_fork hook. */
1314 arm_linux_new_fork (struct lwp_info
*parent
, pid_t child_pid
)
1317 struct arm_linux_debug_reg_state
*parent_state
;
1318 struct arm_linux_debug_reg_state
*child_state
;
1320 /* NULL means no watchpoint has ever been set in the parent. In
1321 that case, there's nothing to do. */
1322 if (parent
->arch_private
== NULL
)
1325 /* GDB core assumes the child inherits the watchpoints/hw
1326 breakpoints of the parent, and will remove them all from the
1327 forked off process. Copy the debug registers mirrors into the
1328 new process so that all breakpoints and watchpoints can be
1329 removed together. */
1331 parent_pid
= ptid_get_pid (parent
->ptid
);
1332 parent_state
= arm_linux_get_debug_reg_state (parent_pid
);
1333 child_state
= arm_linux_get_debug_reg_state (child_pid
);
1334 *child_state
= *parent_state
;
1337 void _initialize_arm_linux_nat (void);
1340 _initialize_arm_linux_nat (void)
1342 struct target_ops
*t
;
1344 /* Fill in the generic GNU/Linux methods. */
1345 t
= linux_target ();
1347 /* Add our register access methods. */
1348 t
->to_fetch_registers
= arm_linux_fetch_inferior_registers
;
1349 t
->to_store_registers
= arm_linux_store_inferior_registers
;
1351 /* Add our hardware breakpoint and watchpoint implementation. */
1352 t
->to_can_use_hw_breakpoint
= arm_linux_can_use_hw_breakpoint
;
1353 t
->to_insert_hw_breakpoint
= arm_linux_insert_hw_breakpoint
;
1354 t
->to_remove_hw_breakpoint
= arm_linux_remove_hw_breakpoint
;
1355 t
->to_region_ok_for_hw_watchpoint
= arm_linux_region_ok_for_hw_watchpoint
;
1356 t
->to_insert_watchpoint
= arm_linux_insert_watchpoint
;
1357 t
->to_remove_watchpoint
= arm_linux_remove_watchpoint
;
1358 t
->to_stopped_by_watchpoint
= arm_linux_stopped_by_watchpoint
;
1359 t
->to_stopped_data_address
= arm_linux_stopped_data_address
;
1360 t
->to_watchpoint_addr_within_range
= arm_linux_watchpoint_addr_within_range
;
1362 t
->to_read_description
= arm_linux_read_description
;
1364 /* Register the target. */
1365 linux_nat_add_target (t
);
1367 /* Handle thread creation and exit. */
1368 linux_nat_set_new_thread (t
, arm_linux_new_thread
);
1369 linux_nat_set_prepare_to_resume (t
, arm_linux_prepare_to_resume
);
1371 /* Handle process creation and exit. */
1372 linux_nat_set_new_fork (t
, arm_linux_new_fork
);
1373 linux_nat_set_forget_process (t
, arm_linux_forget_process
);