1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999-2014 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/>. */
25 #include "linux-nat.h"
26 #include "target-descriptions.h"
29 #include "gdbthread.h"
32 #include "arm-linux-tdep.h"
34 #include <elf/common.h>
36 #include <sys/ptrace.h>
37 #include <sys/utsname.h>
38 #include <sys/procfs.h>
40 /* Prototypes for supply_gregset etc. */
43 /* Defines ps_err_e, struct ps_prochandle. */
44 #include "gdb_proc_service.h"
46 #ifndef PTRACE_GET_THREAD_AREA
47 #define PTRACE_GET_THREAD_AREA 22
50 #ifndef PTRACE_GETWMMXREGS
51 #define PTRACE_GETWMMXREGS 18
52 #define PTRACE_SETWMMXREGS 19
55 #ifndef PTRACE_GETVFPREGS
56 #define PTRACE_GETVFPREGS 27
57 #define PTRACE_SETVFPREGS 28
60 #ifndef PTRACE_GETHBPREGS
61 #define PTRACE_GETHBPREGS 29
62 #define PTRACE_SETHBPREGS 30
65 /* A flag for whether the WMMX registers are available. */
66 static int arm_linux_has_wmmx_registers
;
68 /* The number of 64-bit VFP registers we have (expect this to be 0,
70 static int arm_linux_vfp_register_count
;
72 extern int arm_apcs_32
;
74 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
75 case we may be tracing more than one process at a time. In that
76 case, inferior_ptid will contain the main process ID and the
77 individual thread (process) ID. get_thread_id () is used to get
78 the thread id if it's available, and the process id otherwise. */
81 get_thread_id (ptid_t ptid
)
83 int tid
= ptid_get_lwp (ptid
);
85 tid
= ptid_get_pid (ptid
);
89 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
91 /* Get the value of a particular register from the floating point
92 state of the process and store it into regcache. */
95 fetch_fpregister (struct regcache
*regcache
, int regno
)
98 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
100 /* Get the thread id for the ptrace call. */
101 tid
= GET_THREAD_ID (inferior_ptid
);
103 /* Read the floating point state. */
104 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
107 warning (_("Unable to fetch floating point register."));
112 if (ARM_FPS_REGNUM
== regno
)
113 regcache_raw_supply (regcache
, ARM_FPS_REGNUM
,
114 fp
+ NWFPE_FPSR_OFFSET
);
116 /* Fetch the floating point register. */
117 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
118 supply_nwfpe_register (regcache
, regno
, fp
);
121 /* Get the whole floating point state of the process and store it
125 fetch_fpregs (struct regcache
*regcache
)
128 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
130 /* Get the thread id for the ptrace call. */
131 tid
= GET_THREAD_ID (inferior_ptid
);
133 /* Read the floating point state. */
134 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
137 warning (_("Unable to fetch the floating point registers."));
142 regcache_raw_supply (regcache
, ARM_FPS_REGNUM
,
143 fp
+ NWFPE_FPSR_OFFSET
);
145 /* Fetch the floating point registers. */
146 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
147 supply_nwfpe_register (regcache
, regno
, fp
);
150 /* Save a particular register into the floating point state of the
151 process using the contents from regcache. */
154 store_fpregister (const struct regcache
*regcache
, int regno
)
157 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
159 /* Get the thread id for the ptrace call. */
160 tid
= GET_THREAD_ID (inferior_ptid
);
162 /* Read the floating point state. */
163 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
166 warning (_("Unable to fetch the floating point registers."));
171 if (ARM_FPS_REGNUM
== regno
172 && REG_VALID
== regcache_register_status (regcache
, ARM_FPS_REGNUM
))
173 regcache_raw_collect (regcache
, ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
175 /* Store the floating point register. */
176 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
177 collect_nwfpe_register (regcache
, regno
, fp
);
179 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
182 warning (_("Unable to store floating point register."));
187 /* Save the whole floating point state of the process using
188 the contents from regcache. */
191 store_fpregs (const struct regcache
*regcache
)
194 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
196 /* Get the thread id for the ptrace call. */
197 tid
= GET_THREAD_ID (inferior_ptid
);
199 /* Read the floating point state. */
200 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
203 warning (_("Unable to fetch the floating point registers."));
208 if (REG_VALID
== regcache_register_status (regcache
, ARM_FPS_REGNUM
))
209 regcache_raw_collect (regcache
, ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
211 /* Store the floating point registers. */
212 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
213 if (REG_VALID
== regcache_register_status (regcache
, regno
))
214 collect_nwfpe_register (regcache
, regno
, fp
);
216 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
219 warning (_("Unable to store floating point registers."));
224 /* Fetch a general register of the process and store into
228 fetch_register (struct regcache
*regcache
, int regno
)
233 /* Get the thread id for the ptrace call. */
234 tid
= GET_THREAD_ID (inferior_ptid
);
236 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
239 warning (_("Unable to fetch general register."));
243 if (regno
>= ARM_A1_REGNUM
&& regno
< ARM_PC_REGNUM
)
244 regcache_raw_supply (regcache
, regno
, (char *) ®s
[regno
]);
246 if (ARM_PS_REGNUM
== regno
)
249 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
250 (char *) ®s
[ARM_CPSR_GREGNUM
]);
252 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
253 (char *) ®s
[ARM_PC_REGNUM
]);
256 if (ARM_PC_REGNUM
== regno
)
258 regs
[ARM_PC_REGNUM
] = gdbarch_addr_bits_remove
259 (get_regcache_arch (regcache
),
260 regs
[ARM_PC_REGNUM
]);
261 regcache_raw_supply (regcache
, ARM_PC_REGNUM
,
262 (char *) ®s
[ARM_PC_REGNUM
]);
266 /* Fetch all general registers of the process and store into
270 fetch_regs (struct regcache
*regcache
)
275 /* Get the thread id for the ptrace call. */
276 tid
= GET_THREAD_ID (inferior_ptid
);
278 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
281 warning (_("Unable to fetch general registers."));
285 for (regno
= ARM_A1_REGNUM
; regno
< ARM_PC_REGNUM
; regno
++)
286 regcache_raw_supply (regcache
, regno
, (char *) ®s
[regno
]);
289 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
290 (char *) ®s
[ARM_CPSR_GREGNUM
]);
292 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
293 (char *) ®s
[ARM_PC_REGNUM
]);
295 regs
[ARM_PC_REGNUM
] = gdbarch_addr_bits_remove
296 (get_regcache_arch (regcache
), regs
[ARM_PC_REGNUM
]);
297 regcache_raw_supply (regcache
, ARM_PC_REGNUM
,
298 (char *) ®s
[ARM_PC_REGNUM
]);
301 /* Store all general registers of the process from the values in
305 store_register (const struct regcache
*regcache
, int regno
)
310 if (REG_VALID
!= regcache_register_status (regcache
, regno
))
313 /* Get the thread id for the ptrace call. */
314 tid
= GET_THREAD_ID (inferior_ptid
);
316 /* Get the general registers from the process. */
317 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
320 warning (_("Unable to fetch general registers."));
324 if (regno
>= ARM_A1_REGNUM
&& regno
<= ARM_PC_REGNUM
)
325 regcache_raw_collect (regcache
, regno
, (char *) ®s
[regno
]);
326 else if (arm_apcs_32
&& regno
== ARM_PS_REGNUM
)
327 regcache_raw_collect (regcache
, regno
,
328 (char *) ®s
[ARM_CPSR_GREGNUM
]);
329 else if (!arm_apcs_32
&& regno
== ARM_PS_REGNUM
)
330 regcache_raw_collect (regcache
, ARM_PC_REGNUM
,
331 (char *) ®s
[ARM_PC_REGNUM
]);
333 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
336 warning (_("Unable to store general register."));
342 store_regs (const struct regcache
*regcache
)
347 /* Get the thread id for the ptrace call. */
348 tid
= GET_THREAD_ID (inferior_ptid
);
350 /* Fetch the general registers. */
351 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
354 warning (_("Unable to fetch general registers."));
358 for (regno
= ARM_A1_REGNUM
; regno
<= ARM_PC_REGNUM
; regno
++)
360 if (REG_VALID
== regcache_register_status (regcache
, regno
))
361 regcache_raw_collect (regcache
, regno
, (char *) ®s
[regno
]);
364 if (arm_apcs_32
&& REG_VALID
== regcache_register_status (regcache
, ARM_PS_REGNUM
))
365 regcache_raw_collect (regcache
, ARM_PS_REGNUM
,
366 (char *) ®s
[ARM_CPSR_GREGNUM
]);
368 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
372 warning (_("Unable to store general registers."));
377 /* Fetch all WMMX registers of the process and store into
380 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
383 fetch_wmmx_regs (struct regcache
*regcache
)
385 char regbuf
[IWMMXT_REGS_SIZE
];
388 /* Get the thread id for the ptrace call. */
389 tid
= GET_THREAD_ID (inferior_ptid
);
391 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
394 warning (_("Unable to fetch WMMX registers."));
398 for (regno
= 0; regno
< 16; regno
++)
399 regcache_raw_supply (regcache
, regno
+ ARM_WR0_REGNUM
,
402 for (regno
= 0; regno
< 2; regno
++)
403 regcache_raw_supply (regcache
, regno
+ ARM_WCSSF_REGNUM
,
404 ®buf
[16 * 8 + regno
* 4]);
406 for (regno
= 0; regno
< 4; regno
++)
407 regcache_raw_supply (regcache
, regno
+ ARM_WCGR0_REGNUM
,
408 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
412 store_wmmx_regs (const struct regcache
*regcache
)
414 char regbuf
[IWMMXT_REGS_SIZE
];
417 /* Get the thread id for the ptrace call. */
418 tid
= GET_THREAD_ID (inferior_ptid
);
420 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
423 warning (_("Unable to fetch WMMX registers."));
427 for (regno
= 0; regno
< 16; regno
++)
428 if (REG_VALID
== regcache_register_status (regcache
,
429 regno
+ ARM_WR0_REGNUM
))
430 regcache_raw_collect (regcache
, regno
+ ARM_WR0_REGNUM
,
433 for (regno
= 0; regno
< 2; regno
++)
434 if (REG_VALID
== regcache_register_status (regcache
,
435 regno
+ ARM_WCSSF_REGNUM
))
436 regcache_raw_collect (regcache
, regno
+ ARM_WCSSF_REGNUM
,
437 ®buf
[16 * 8 + regno
* 4]);
439 for (regno
= 0; regno
< 4; regno
++)
440 if (REG_VALID
== regcache_register_status (regcache
,
441 regno
+ ARM_WCGR0_REGNUM
))
442 regcache_raw_collect (regcache
, regno
+ ARM_WCGR0_REGNUM
,
443 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
445 ret
= ptrace (PTRACE_SETWMMXREGS
, tid
, 0, regbuf
);
449 warning (_("Unable to store WMMX registers."));
454 /* Fetch and store VFP Registers. The kernel object has space for 32
455 64-bit registers, and the FPSCR. This is even when on a VFPv2 or
457 #define VFP_REGS_SIZE (32 * 8 + 4)
460 fetch_vfp_regs (struct regcache
*regcache
)
462 char regbuf
[VFP_REGS_SIZE
];
465 /* Get the thread id for the ptrace call. */
466 tid
= GET_THREAD_ID (inferior_ptid
);
468 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
471 warning (_("Unable to fetch VFP registers."));
475 for (regno
= 0; regno
< arm_linux_vfp_register_count
; regno
++)
476 regcache_raw_supply (regcache
, regno
+ ARM_D0_REGNUM
,
477 (char *) regbuf
+ regno
* 8);
479 regcache_raw_supply (regcache
, ARM_FPSCR_REGNUM
,
480 (char *) regbuf
+ 32 * 8);
484 store_vfp_regs (const struct regcache
*regcache
)
486 char regbuf
[VFP_REGS_SIZE
];
489 /* Get the thread id for the ptrace call. */
490 tid
= GET_THREAD_ID (inferior_ptid
);
492 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
495 warning (_("Unable to fetch VFP registers (for update)."));
499 for (regno
= 0; regno
< arm_linux_vfp_register_count
; regno
++)
500 regcache_raw_collect (regcache
, regno
+ ARM_D0_REGNUM
,
501 (char *) regbuf
+ regno
* 8);
503 regcache_raw_collect (regcache
, ARM_FPSCR_REGNUM
,
504 (char *) regbuf
+ 32 * 8);
506 ret
= ptrace (PTRACE_SETVFPREGS
, tid
, 0, regbuf
);
510 warning (_("Unable to store VFP registers."));
515 /* Fetch registers from the child process. Fetch all registers if
516 regno == -1, otherwise fetch all general registers or all floating
517 point registers depending upon the value of regno. */
520 arm_linux_fetch_inferior_registers (struct target_ops
*ops
,
521 struct regcache
*regcache
, int regno
)
525 fetch_regs (regcache
);
526 fetch_fpregs (regcache
);
527 if (arm_linux_has_wmmx_registers
)
528 fetch_wmmx_regs (regcache
);
529 if (arm_linux_vfp_register_count
> 0)
530 fetch_vfp_regs (regcache
);
534 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
535 fetch_register (regcache
, regno
);
536 else if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
537 fetch_fpregister (regcache
, regno
);
538 else if (arm_linux_has_wmmx_registers
539 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
540 fetch_wmmx_regs (regcache
);
541 else if (arm_linux_vfp_register_count
> 0
542 && regno
>= ARM_D0_REGNUM
543 && regno
<= ARM_D0_REGNUM
+ arm_linux_vfp_register_count
)
544 fetch_vfp_regs (regcache
);
548 /* Store registers back into the inferior. Store all registers if
549 regno == -1, otherwise store all general registers or all floating
550 point registers depending upon the value of regno. */
553 arm_linux_store_inferior_registers (struct target_ops
*ops
,
554 struct regcache
*regcache
, int regno
)
558 store_regs (regcache
);
559 store_fpregs (regcache
);
560 if (arm_linux_has_wmmx_registers
)
561 store_wmmx_regs (regcache
);
562 if (arm_linux_vfp_register_count
> 0)
563 store_vfp_regs (regcache
);
567 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
568 store_register (regcache
, regno
);
569 else if ((regno
>= ARM_F0_REGNUM
) && (regno
<= ARM_FPS_REGNUM
))
570 store_fpregister (regcache
, regno
);
571 else if (arm_linux_has_wmmx_registers
572 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
573 store_wmmx_regs (regcache
);
574 else if (arm_linux_vfp_register_count
> 0
575 && regno
>= ARM_D0_REGNUM
576 && regno
<= ARM_D0_REGNUM
+ arm_linux_vfp_register_count
)
577 store_vfp_regs (regcache
);
581 /* Wrapper functions for the standard regset handling, used by
585 fill_gregset (const struct regcache
*regcache
,
586 gdb_gregset_t
*gregsetp
, int regno
)
588 arm_linux_collect_gregset (NULL
, regcache
, regno
, gregsetp
, 0);
592 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
594 arm_linux_supply_gregset (NULL
, regcache
, -1, gregsetp
, 0);
598 fill_fpregset (const struct regcache
*regcache
,
599 gdb_fpregset_t
*fpregsetp
, int regno
)
601 arm_linux_collect_nwfpe (NULL
, regcache
, regno
, fpregsetp
, 0);
604 /* Fill GDB's register array with the floating-point register values
608 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
610 arm_linux_supply_nwfpe (NULL
, regcache
, -1, fpregsetp
, 0);
613 /* Fetch the thread-local storage pointer for libthread_db. */
616 ps_get_thread_area (const struct ps_prochandle
*ph
,
617 lwpid_t lwpid
, int idx
, void **base
)
619 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
622 /* IDX is the bias from the thread pointer to the beginning of the
623 thread descriptor. It has to be subtracted due to implementation
624 quirks in libthread_db. */
625 *base
= (void *) ((char *)*base
- idx
);
630 static const struct target_desc
*
631 arm_linux_read_description (struct target_ops
*ops
)
633 CORE_ADDR arm_hwcap
= 0;
634 arm_linux_has_wmmx_registers
= 0;
635 arm_linux_vfp_register_count
= 0;
637 if (target_auxv_search (ops
, AT_HWCAP
, &arm_hwcap
) != 1)
639 return ops
->beneath
->to_read_description (ops
->beneath
);
642 if (arm_hwcap
& HWCAP_IWMMXT
)
644 arm_linux_has_wmmx_registers
= 1;
645 return tdesc_arm_with_iwmmxt
;
648 if (arm_hwcap
& HWCAP_VFP
)
652 const struct target_desc
* result
= NULL
;
654 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
655 Neon with VFPv3-D32. */
656 if (arm_hwcap
& HWCAP_NEON
)
658 arm_linux_vfp_register_count
= 32;
659 result
= tdesc_arm_with_neon
;
661 else if ((arm_hwcap
& (HWCAP_VFPv3
| HWCAP_VFPv3D16
)) == HWCAP_VFPv3
)
663 arm_linux_vfp_register_count
= 32;
664 result
= tdesc_arm_with_vfpv3
;
668 arm_linux_vfp_register_count
= 16;
669 result
= tdesc_arm_with_vfpv2
;
672 /* Now make sure that the kernel supports reading these
673 registers. Support was added in 2.6.30. */
674 pid
= ptid_get_lwp (inferior_ptid
);
676 buf
= alloca (VFP_REGS_SIZE
);
677 if (ptrace (PTRACE_GETVFPREGS
, pid
, 0, buf
) < 0
684 return ops
->beneath
->to_read_description (ops
->beneath
);
687 /* Information describing the hardware breakpoint capabilities. */
688 struct arm_linux_hwbp_cap
691 gdb_byte max_wp_length
;
696 /* Get hold of the Hardware Breakpoint information for the target we are
697 attached to. Returns NULL if the kernel doesn't support Hardware
698 breakpoints at all, or a pointer to the information structure. */
699 static const struct arm_linux_hwbp_cap
*
700 arm_linux_get_hwbp_cap (void)
702 /* The info structure we return. */
703 static struct arm_linux_hwbp_cap info
;
705 /* Is INFO in a good state? -1 means that no attempt has been made to
706 initialize INFO; 0 means an attempt has been made, but it failed; 1
707 means INFO is in an initialized state. */
708 static int available
= -1;
715 tid
= GET_THREAD_ID (inferior_ptid
);
716 if (ptrace (PTRACE_GETHBPREGS
, tid
, 0, &val
) < 0)
720 info
.arch
= (gdb_byte
)((val
>> 24) & 0xff);
721 info
.max_wp_length
= (gdb_byte
)((val
>> 16) & 0xff);
722 info
.wp_count
= (gdb_byte
)((val
>> 8) & 0xff);
723 info
.bp_count
= (gdb_byte
)(val
& 0xff);
724 available
= (info
.arch
!= 0);
728 return available
== 1 ? &info
: NULL
;
731 /* How many hardware breakpoints are available? */
733 arm_linux_get_hw_breakpoint_count (void)
735 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
736 return cap
!= NULL
? cap
->bp_count
: 0;
739 /* How many hardware watchpoints are available? */
741 arm_linux_get_hw_watchpoint_count (void)
743 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
744 return cap
!= NULL
? cap
->wp_count
: 0;
747 /* Have we got a free break-/watch-point available for use? Returns -1 if
748 there is not an appropriate resource available, otherwise returns 1. */
750 arm_linux_can_use_hw_breakpoint (struct target_ops
*self
,
751 int type
, int cnt
, int ot
)
753 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
754 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
756 if (cnt
+ ot
> arm_linux_get_hw_watchpoint_count ())
759 else if (type
== bp_hardware_breakpoint
)
761 if (cnt
> arm_linux_get_hw_breakpoint_count ())
770 /* Enum describing the different types of ARM hardware break-/watch-points. */
779 /* Type describing an ARM Hardware Breakpoint Control register value. */
780 typedef unsigned int arm_hwbp_control_t
;
782 /* Structure used to keep track of hardware break-/watch-points. */
783 struct arm_linux_hw_breakpoint
785 /* Address to break on, or being watched. */
786 unsigned int address
;
787 /* Control register for break-/watch- point. */
788 arm_hwbp_control_t control
;
791 /* Structure containing arrays of the break and watch points which are have
792 active in each thread.
794 The Linux ptrace interface to hardware break-/watch-points presents the
795 values in a vector centred around 0 (which is used fo generic information).
796 Positive indicies refer to breakpoint addresses/control registers, negative
797 indices to watchpoint addresses/control registers.
799 The Linux vector is indexed as follows:
800 -((i << 1) + 2): Control register for watchpoint i.
801 -((i << 1) + 1): Address register for watchpoint i.
802 0: Information register.
803 ((i << 1) + 1): Address register for breakpoint i.
804 ((i << 1) + 2): Control register for breakpoint i.
806 This structure is used as a per-thread cache of the state stored by the
807 kernel, so that we don't need to keep calling into the kernel to find a
810 We treat break-/watch-points with their enable bit clear as being deleted.
812 typedef struct arm_linux_thread_points
816 /* Breakpoints for thread. */
817 struct arm_linux_hw_breakpoint
*bpts
;
818 /* Watchpoint for threads. */
819 struct arm_linux_hw_breakpoint
*wpts
;
820 } *arm_linux_thread_points_p
;
821 DEF_VEC_P (arm_linux_thread_points_p
);
823 /* Vector of hardware breakpoints for each thread. */
824 VEC(arm_linux_thread_points_p
) *arm_threads
= NULL
;
826 /* Find the list of hardware break-/watch-points for a thread with id TID.
827 If no list exists for TID we return NULL if ALLOC_NEW is 0, otherwise we
828 create a new list and return that. */
829 static struct arm_linux_thread_points
*
830 arm_linux_find_breakpoints_by_tid (int tid
, int alloc_new
)
833 struct arm_linux_thread_points
*t
;
835 for (i
= 0; VEC_iterate (arm_linux_thread_points_p
, arm_threads
, i
, t
); ++i
)
845 t
= xmalloc (sizeof (struct arm_linux_thread_points
));
847 t
->bpts
= xzalloc (arm_linux_get_hw_breakpoint_count ()
848 * sizeof (struct arm_linux_hw_breakpoint
));
849 t
->wpts
= xzalloc (arm_linux_get_hw_watchpoint_count ()
850 * sizeof (struct arm_linux_hw_breakpoint
));
851 VEC_safe_push (arm_linux_thread_points_p
, arm_threads
, t
);
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
;
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 RW value we're given when
915 asked to set a watchpoint. */
917 arm_linux_get_hwbp_type (int rw
)
920 return arm_hwbp_load
;
921 else if (rw
== 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
, int rw
,
931 struct arm_linux_hw_breakpoint
*p
)
933 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
936 gdb_assert (cap
!= NULL
);
937 gdb_assert (cap
->max_wp_length
!= 0);
939 mask
= (1 << len
) - 1;
941 p
->address
= (unsigned int) addr
;
942 p
->control
= arm_hwbp_control_initialize (mask
,
943 arm_linux_get_hwbp_type (rw
), 1);
946 /* Are two break-/watch-points equal? */
948 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint
*p1
,
949 const struct arm_linux_hw_breakpoint
*p2
)
951 return p1
->address
== p2
->address
&& p1
->control
== p2
->control
;
954 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
955 =1) BPT for thread TID. */
957 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
* bpt
,
958 int tid
, int watchpoint
)
960 struct arm_linux_thread_points
*t
= arm_linux_find_breakpoints_by_tid (tid
, 1);
962 struct arm_linux_hw_breakpoint
* bpts
;
965 gdb_assert (t
!= NULL
);
969 count
= arm_linux_get_hw_watchpoint_count ();
975 count
= arm_linux_get_hw_breakpoint_count ();
980 for (i
= 0; i
< count
; ++i
)
981 if (!arm_hwbp_control_is_enabled (bpts
[i
].control
))
984 if (ptrace (PTRACE_SETHBPREGS
, tid
, dir
* ((i
<< 1) + 1),
986 perror_with_name (_("Unexpected error setting breakpoint address"));
987 if (ptrace (PTRACE_SETHBPREGS
, tid
, dir
* ((i
<< 1) + 2),
989 perror_with_name (_("Unexpected error setting breakpoint"));
991 memcpy (bpts
+ i
, bpt
, sizeof (struct arm_linux_hw_breakpoint
));
995 gdb_assert (i
!= count
);
998 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
999 (WATCHPOINT = 1) BPT for thread TID. */
1001 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
*bpt
,
1002 int tid
, int watchpoint
)
1004 struct arm_linux_thread_points
*t
= arm_linux_find_breakpoints_by_tid (tid
, 0);
1006 struct arm_linux_hw_breakpoint
*bpts
;
1009 gdb_assert (t
!= NULL
);
1013 count
= arm_linux_get_hw_watchpoint_count ();
1019 count
= arm_linux_get_hw_breakpoint_count ();
1024 for (i
= 0; i
< count
; ++i
)
1025 if (arm_linux_hw_breakpoint_equal (bpt
, bpts
+ i
))
1028 bpts
[i
].control
= arm_hwbp_control_disable (bpts
[i
].control
);
1029 if (ptrace (PTRACE_SETHBPREGS
, tid
, dir
* ((i
<< 1) + 2),
1030 &bpts
[i
].control
) < 0)
1031 perror_with_name (_("Unexpected error clearing breakpoint"));
1035 gdb_assert (i
!= count
);
1038 /* Insert a Hardware breakpoint. */
1040 arm_linux_insert_hw_breakpoint (struct target_ops
*self
,
1041 struct gdbarch
*gdbarch
,
1042 struct bp_target_info
*bp_tgt
)
1044 struct lwp_info
*lp
;
1045 struct arm_linux_hw_breakpoint p
;
1047 if (arm_linux_get_hw_breakpoint_count () == 0)
1050 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1052 arm_linux_insert_hw_breakpoint1 (&p
, ptid_get_lwp (lp
->ptid
), 0);
1057 /* Remove a hardware breakpoint. */
1059 arm_linux_remove_hw_breakpoint (struct target_ops
*self
,
1060 struct gdbarch
*gdbarch
,
1061 struct bp_target_info
*bp_tgt
)
1063 struct lwp_info
*lp
;
1064 struct arm_linux_hw_breakpoint p
;
1066 if (arm_linux_get_hw_breakpoint_count () == 0)
1069 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1071 arm_linux_remove_hw_breakpoint1 (&p
, ptid_get_lwp (lp
->ptid
), 0);
1076 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1079 arm_linux_region_ok_for_hw_watchpoint (struct target_ops
*self
,
1080 CORE_ADDR addr
, int len
)
1082 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
1083 CORE_ADDR max_wp_length
, aligned_addr
;
1085 /* Can not set watchpoints for zero or negative lengths. */
1089 /* Need to be able to use the ptrace interface. */
1090 if (cap
== NULL
|| cap
->wp_count
== 0)
1093 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1094 range covered by a watchpoint. */
1095 max_wp_length
= (CORE_ADDR
)cap
->max_wp_length
;
1096 aligned_addr
= addr
& ~(max_wp_length
- 1);
1098 if (aligned_addr
+ max_wp_length
< addr
+ len
)
1101 /* The current ptrace interface can only handle watchpoints that are a
1103 if ((len
& (len
- 1)) != 0)
1106 /* All tests passed so we must be able to set a watchpoint. */
1110 /* Insert a Hardware breakpoint. */
1112 arm_linux_insert_watchpoint (struct target_ops
*self
,
1113 CORE_ADDR addr
, int len
, int rw
,
1114 struct expression
*cond
)
1116 struct lwp_info
*lp
;
1117 struct arm_linux_hw_breakpoint p
;
1119 if (arm_linux_get_hw_watchpoint_count () == 0)
1122 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1124 arm_linux_insert_hw_breakpoint1 (&p
, ptid_get_lwp (lp
->ptid
), 1);
1129 /* Remove a hardware breakpoint. */
1131 arm_linux_remove_watchpoint (struct target_ops
*self
,
1132 CORE_ADDR addr
, int len
, int rw
,
1133 struct expression
*cond
)
1135 struct lwp_info
*lp
;
1136 struct arm_linux_hw_breakpoint p
;
1138 if (arm_linux_get_hw_watchpoint_count () == 0)
1141 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1143 arm_linux_remove_hw_breakpoint1 (&p
, ptid_get_lwp (lp
->ptid
), 1);
1148 /* What was the data address the target was stopped on accessing. */
1150 arm_linux_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
1155 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
1158 /* This must be a hardware breakpoint. */
1159 if (siginfo
.si_signo
!= SIGTRAP
1160 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1163 /* We must be able to set hardware watchpoints. */
1164 if (arm_linux_get_hw_watchpoint_count () == 0)
1167 slot
= siginfo
.si_errno
;
1169 /* If we are in a positive slot then we're looking at a breakpoint and not
1174 *addr_p
= (CORE_ADDR
) (uintptr_t) siginfo
.si_addr
;
1178 /* Has the target been stopped by hitting a watchpoint? */
1180 arm_linux_stopped_by_watchpoint (struct target_ops
*ops
)
1183 return arm_linux_stopped_data_address (ops
, &addr
);
1187 arm_linux_watchpoint_addr_within_range (struct target_ops
*target
,
1189 CORE_ADDR start
, int length
)
1191 return start
<= addr
&& start
+ length
- 1 >= addr
;
1194 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1195 in the parent thread to the child thread. */
1197 arm_linux_new_thread (struct lwp_info
*lp
)
1199 int tid
= ptid_get_lwp (lp
->ptid
);
1200 const struct arm_linux_hwbp_cap
*info
= arm_linux_get_hwbp_cap ();
1205 struct arm_linux_thread_points
*p
;
1206 struct arm_linux_hw_breakpoint
*bpts
;
1208 if (VEC_empty (arm_linux_thread_points_p
, arm_threads
))
1211 /* Get a list of breakpoints from any thread. */
1212 p
= VEC_last (arm_linux_thread_points_p
, arm_threads
);
1214 /* Copy that thread's breakpoints and watchpoints to the new thread. */
1215 for (i
= 0; i
< info
->bp_count
; i
++)
1216 if (arm_hwbp_control_is_enabled (p
->bpts
[i
].control
))
1217 arm_linux_insert_hw_breakpoint1 (p
->bpts
+ i
, tid
, 0);
1218 for (i
= 0; i
< info
->wp_count
; i
++)
1219 if (arm_hwbp_control_is_enabled (p
->wpts
[i
].control
))
1220 arm_linux_insert_hw_breakpoint1 (p
->wpts
+ i
, tid
, 1);
1224 /* Handle thread exit. Tidy up the memory that has been allocated for the
1227 arm_linux_thread_exit (struct thread_info
*tp
, int silent
)
1229 const struct arm_linux_hwbp_cap
*info
= arm_linux_get_hwbp_cap ();
1234 int tid
= ptid_get_lwp (tp
->ptid
);
1235 struct arm_linux_thread_points
*t
= NULL
, *p
;
1238 VEC_iterate (arm_linux_thread_points_p
, arm_threads
, i
, p
); i
++)
1250 VEC_unordered_remove (arm_linux_thread_points_p
, arm_threads
, i
);
1258 void _initialize_arm_linux_nat (void);
1261 _initialize_arm_linux_nat (void)
1263 struct target_ops
*t
;
1265 /* Fill in the generic GNU/Linux methods. */
1266 t
= linux_target ();
1268 /* Add our register access methods. */
1269 t
->to_fetch_registers
= arm_linux_fetch_inferior_registers
;
1270 t
->to_store_registers
= arm_linux_store_inferior_registers
;
1272 /* Add our hardware breakpoint and watchpoint implementation. */
1273 t
->to_can_use_hw_breakpoint
= arm_linux_can_use_hw_breakpoint
;
1274 t
->to_insert_hw_breakpoint
= arm_linux_insert_hw_breakpoint
;
1275 t
->to_remove_hw_breakpoint
= arm_linux_remove_hw_breakpoint
;
1276 t
->to_region_ok_for_hw_watchpoint
= arm_linux_region_ok_for_hw_watchpoint
;
1277 t
->to_insert_watchpoint
= arm_linux_insert_watchpoint
;
1278 t
->to_remove_watchpoint
= arm_linux_remove_watchpoint
;
1279 t
->to_stopped_by_watchpoint
= arm_linux_stopped_by_watchpoint
;
1280 t
->to_stopped_data_address
= arm_linux_stopped_data_address
;
1281 t
->to_watchpoint_addr_within_range
= arm_linux_watchpoint_addr_within_range
;
1283 t
->to_read_description
= arm_linux_read_description
;
1285 /* Register the target. */
1286 linux_nat_add_target (t
);
1288 /* Handle thread creation and exit */
1289 observer_attach_thread_exit (arm_linux_thread_exit
);
1290 linux_nat_set_new_thread (t
, arm_linux_new_thread
);