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"
33 #include <elf/common.h>
35 #include <sys/ptrace.h>
36 #include <sys/utsname.h>
37 #include <sys/procfs.h>
39 /* Prototypes for supply_gregset etc. */
42 /* Defines ps_err_e, struct ps_prochandle. */
43 #include "gdb_proc_service.h"
45 #ifndef PTRACE_GET_THREAD_AREA
46 #define PTRACE_GET_THREAD_AREA 22
49 #ifndef PTRACE_GETWMMXREGS
50 #define PTRACE_GETWMMXREGS 18
51 #define PTRACE_SETWMMXREGS 19
54 #ifndef PTRACE_GETVFPREGS
55 #define PTRACE_GETVFPREGS 27
56 #define PTRACE_SETVFPREGS 28
59 #ifndef PTRACE_GETHBPREGS
60 #define PTRACE_GETHBPREGS 29
61 #define PTRACE_SETHBPREGS 30
64 extern int arm_apcs_32
;
66 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
67 case we may be tracing more than one process at a time. In that
68 case, inferior_ptid will contain the main process ID and the
69 individual thread (process) ID. get_thread_id () is used to get
70 the thread id if it's available, and the process id otherwise. */
73 get_thread_id (ptid_t ptid
)
75 int tid
= ptid_get_lwp (ptid
);
77 tid
= ptid_get_pid (ptid
);
81 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
83 /* Get the value of a particular register from the floating point
84 state of the process and store it into regcache. */
87 fetch_fpregister (struct regcache
*regcache
, int regno
)
90 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
92 /* Get the thread id for the ptrace call. */
93 tid
= GET_THREAD_ID (inferior_ptid
);
95 /* Read the floating point state. */
96 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
99 warning (_("Unable to fetch floating point register."));
104 if (ARM_FPS_REGNUM
== regno
)
105 regcache_raw_supply (regcache
, ARM_FPS_REGNUM
,
106 fp
+ NWFPE_FPSR_OFFSET
);
108 /* Fetch the floating point register. */
109 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
110 supply_nwfpe_register (regcache
, regno
, fp
);
113 /* Get the whole floating point state of the process and store it
117 fetch_fpregs (struct regcache
*regcache
)
120 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
122 /* Get the thread id for the ptrace call. */
123 tid
= GET_THREAD_ID (inferior_ptid
);
125 /* Read the floating point state. */
126 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
129 warning (_("Unable to fetch the floating point registers."));
134 regcache_raw_supply (regcache
, ARM_FPS_REGNUM
,
135 fp
+ NWFPE_FPSR_OFFSET
);
137 /* Fetch the floating point registers. */
138 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
139 supply_nwfpe_register (regcache
, regno
, fp
);
142 /* Save a particular register into the floating point state of the
143 process using the contents from regcache. */
146 store_fpregister (const struct regcache
*regcache
, int regno
)
149 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
151 /* Get the thread id for the ptrace call. */
152 tid
= GET_THREAD_ID (inferior_ptid
);
154 /* Read the floating point state. */
155 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
158 warning (_("Unable to fetch the floating point registers."));
163 if (ARM_FPS_REGNUM
== regno
164 && REG_VALID
== regcache_register_status (regcache
, ARM_FPS_REGNUM
))
165 regcache_raw_collect (regcache
, ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
167 /* Store the floating point register. */
168 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
169 collect_nwfpe_register (regcache
, regno
, fp
);
171 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
174 warning (_("Unable to store floating point register."));
179 /* Save the whole floating point state of the process using
180 the contents from regcache. */
183 store_fpregs (const struct regcache
*regcache
)
186 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
188 /* Get the thread id for the ptrace call. */
189 tid
= GET_THREAD_ID (inferior_ptid
);
191 /* Read the floating point state. */
192 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
195 warning (_("Unable to fetch the floating point registers."));
200 if (REG_VALID
== regcache_register_status (regcache
, ARM_FPS_REGNUM
))
201 regcache_raw_collect (regcache
, ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
203 /* Store the floating point registers. */
204 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
205 if (REG_VALID
== regcache_register_status (regcache
, regno
))
206 collect_nwfpe_register (regcache
, regno
, fp
);
208 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
211 warning (_("Unable to store floating point registers."));
216 /* Fetch a general register of the process and store into
220 fetch_register (struct regcache
*regcache
, int regno
)
225 /* Get the thread id for the ptrace call. */
226 tid
= GET_THREAD_ID (inferior_ptid
);
228 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
231 warning (_("Unable to fetch general register."));
235 if (regno
>= ARM_A1_REGNUM
&& regno
< ARM_PC_REGNUM
)
236 regcache_raw_supply (regcache
, regno
, (char *) ®s
[regno
]);
238 if (ARM_PS_REGNUM
== regno
)
241 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
242 (char *) ®s
[ARM_CPSR_GREGNUM
]);
244 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
245 (char *) ®s
[ARM_PC_REGNUM
]);
248 if (ARM_PC_REGNUM
== regno
)
250 regs
[ARM_PC_REGNUM
] = gdbarch_addr_bits_remove
251 (get_regcache_arch (regcache
),
252 regs
[ARM_PC_REGNUM
]);
253 regcache_raw_supply (regcache
, ARM_PC_REGNUM
,
254 (char *) ®s
[ARM_PC_REGNUM
]);
258 /* Fetch all general registers of the process and store into
262 fetch_regs (struct regcache
*regcache
)
267 /* Get the thread id for the ptrace call. */
268 tid
= GET_THREAD_ID (inferior_ptid
);
270 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
273 warning (_("Unable to fetch general registers."));
277 for (regno
= ARM_A1_REGNUM
; regno
< ARM_PC_REGNUM
; regno
++)
278 regcache_raw_supply (regcache
, regno
, (char *) ®s
[regno
]);
281 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
282 (char *) ®s
[ARM_CPSR_GREGNUM
]);
284 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
285 (char *) ®s
[ARM_PC_REGNUM
]);
287 regs
[ARM_PC_REGNUM
] = gdbarch_addr_bits_remove
288 (get_regcache_arch (regcache
), regs
[ARM_PC_REGNUM
]);
289 regcache_raw_supply (regcache
, ARM_PC_REGNUM
,
290 (char *) ®s
[ARM_PC_REGNUM
]);
293 /* Store all general registers of the process from the values in
297 store_register (const struct regcache
*regcache
, int regno
)
302 if (REG_VALID
!= regcache_register_status (regcache
, regno
))
305 /* Get the thread id for the ptrace call. */
306 tid
= GET_THREAD_ID (inferior_ptid
);
308 /* Get the general registers from the process. */
309 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
312 warning (_("Unable to fetch general registers."));
316 if (regno
>= ARM_A1_REGNUM
&& regno
<= ARM_PC_REGNUM
)
317 regcache_raw_collect (regcache
, regno
, (char *) ®s
[regno
]);
318 else if (arm_apcs_32
&& regno
== ARM_PS_REGNUM
)
319 regcache_raw_collect (regcache
, regno
,
320 (char *) ®s
[ARM_CPSR_GREGNUM
]);
321 else if (!arm_apcs_32
&& regno
== ARM_PS_REGNUM
)
322 regcache_raw_collect (regcache
, ARM_PC_REGNUM
,
323 (char *) ®s
[ARM_PC_REGNUM
]);
325 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
328 warning (_("Unable to store general register."));
334 store_regs (const struct regcache
*regcache
)
339 /* Get the thread id for the ptrace call. */
340 tid
= GET_THREAD_ID (inferior_ptid
);
342 /* Fetch the general registers. */
343 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
346 warning (_("Unable to fetch general registers."));
350 for (regno
= ARM_A1_REGNUM
; regno
<= ARM_PC_REGNUM
; regno
++)
352 if (REG_VALID
== regcache_register_status (regcache
, regno
))
353 regcache_raw_collect (regcache
, regno
, (char *) ®s
[regno
]);
356 if (arm_apcs_32
&& REG_VALID
== regcache_register_status (regcache
, ARM_PS_REGNUM
))
357 regcache_raw_collect (regcache
, ARM_PS_REGNUM
,
358 (char *) ®s
[ARM_CPSR_GREGNUM
]);
360 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
364 warning (_("Unable to store general registers."));
369 /* Fetch all WMMX registers of the process and store into
372 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
375 fetch_wmmx_regs (struct regcache
*regcache
)
377 char regbuf
[IWMMXT_REGS_SIZE
];
380 /* Get the thread id for the ptrace call. */
381 tid
= GET_THREAD_ID (inferior_ptid
);
383 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
386 warning (_("Unable to fetch WMMX registers."));
390 for (regno
= 0; regno
< 16; regno
++)
391 regcache_raw_supply (regcache
, regno
+ ARM_WR0_REGNUM
,
394 for (regno
= 0; regno
< 2; regno
++)
395 regcache_raw_supply (regcache
, regno
+ ARM_WCSSF_REGNUM
,
396 ®buf
[16 * 8 + regno
* 4]);
398 for (regno
= 0; regno
< 4; regno
++)
399 regcache_raw_supply (regcache
, regno
+ ARM_WCGR0_REGNUM
,
400 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
404 store_wmmx_regs (const struct regcache
*regcache
)
406 char regbuf
[IWMMXT_REGS_SIZE
];
409 /* Get the thread id for the ptrace call. */
410 tid
= GET_THREAD_ID (inferior_ptid
);
412 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
415 warning (_("Unable to fetch WMMX registers."));
419 for (regno
= 0; regno
< 16; regno
++)
420 if (REG_VALID
== regcache_register_status (regcache
,
421 regno
+ ARM_WR0_REGNUM
))
422 regcache_raw_collect (regcache
, regno
+ ARM_WR0_REGNUM
,
425 for (regno
= 0; regno
< 2; regno
++)
426 if (REG_VALID
== regcache_register_status (regcache
,
427 regno
+ ARM_WCSSF_REGNUM
))
428 regcache_raw_collect (regcache
, regno
+ ARM_WCSSF_REGNUM
,
429 ®buf
[16 * 8 + regno
* 4]);
431 for (regno
= 0; regno
< 4; regno
++)
432 if (REG_VALID
== regcache_register_status (regcache
,
433 regno
+ ARM_WCGR0_REGNUM
))
434 regcache_raw_collect (regcache
, regno
+ ARM_WCGR0_REGNUM
,
435 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
437 ret
= ptrace (PTRACE_SETWMMXREGS
, tid
, 0, regbuf
);
441 warning (_("Unable to store WMMX registers."));
446 /* Fetch and store VFP Registers. The kernel object has space for 32
447 64-bit registers, and the FPSCR. This is even when on a VFPv2 or
449 #define VFP_REGS_SIZE (32 * 8 + 4)
452 fetch_vfp_regs (struct regcache
*regcache
)
454 char regbuf
[VFP_REGS_SIZE
];
456 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
457 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
459 /* Get the thread id for the ptrace call. */
460 tid
= GET_THREAD_ID (inferior_ptid
);
462 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
465 warning (_("Unable to fetch VFP registers."));
469 for (regno
= 0; regno
< tdep
->vfp_register_count
; regno
++)
470 regcache_raw_supply (regcache
, regno
+ ARM_D0_REGNUM
,
471 (char *) regbuf
+ regno
* 8);
473 regcache_raw_supply (regcache
, ARM_FPSCR_REGNUM
,
474 (char *) regbuf
+ 32 * 8);
478 store_vfp_regs (const struct regcache
*regcache
)
480 char regbuf
[VFP_REGS_SIZE
];
482 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
483 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
485 /* Get the thread id for the ptrace call. */
486 tid
= GET_THREAD_ID (inferior_ptid
);
488 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
491 warning (_("Unable to fetch VFP registers (for update)."));
495 for (regno
= 0; regno
< tdep
->vfp_register_count
; regno
++)
496 regcache_raw_collect (regcache
, regno
+ ARM_D0_REGNUM
,
497 (char *) regbuf
+ regno
* 8);
499 regcache_raw_collect (regcache
, ARM_FPSCR_REGNUM
,
500 (char *) regbuf
+ 32 * 8);
502 ret
= ptrace (PTRACE_SETVFPREGS
, tid
, 0, regbuf
);
506 warning (_("Unable to store VFP registers."));
511 /* Fetch registers from the child process. Fetch all registers if
512 regno == -1, otherwise fetch all general registers or all floating
513 point registers depending upon the value of regno. */
516 arm_linux_fetch_inferior_registers (struct target_ops
*ops
,
517 struct regcache
*regcache
, int regno
)
519 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
520 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
524 fetch_regs (regcache
);
525 fetch_fpregs (regcache
);
526 if (tdep
->have_wmmx_registers
)
527 fetch_wmmx_regs (regcache
);
528 if (tdep
->vfp_register_count
> 0)
529 fetch_vfp_regs (regcache
);
533 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
534 fetch_register (regcache
, regno
);
535 else if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
536 fetch_fpregister (regcache
, regno
);
537 else if (tdep
->have_wmmx_registers
538 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
539 fetch_wmmx_regs (regcache
);
540 else if (tdep
->vfp_register_count
> 0
541 && regno
>= ARM_D0_REGNUM
542 && regno
<= ARM_D0_REGNUM
+ tdep
->vfp_register_count
)
543 fetch_vfp_regs (regcache
);
547 /* Store registers back into the inferior. Store all registers if
548 regno == -1, otherwise store all general registers or all floating
549 point registers depending upon the value of regno. */
552 arm_linux_store_inferior_registers (struct target_ops
*ops
,
553 struct regcache
*regcache
, int regno
)
555 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
556 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
560 store_regs (regcache
);
561 store_fpregs (regcache
);
562 if (tdep
->have_wmmx_registers
)
563 store_wmmx_regs (regcache
);
564 if (tdep
->vfp_register_count
> 0)
565 store_vfp_regs (regcache
);
569 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
570 store_register (regcache
, regno
);
571 else if ((regno
>= ARM_F0_REGNUM
) && (regno
<= ARM_FPS_REGNUM
))
572 store_fpregister (regcache
, regno
);
573 else if (tdep
->have_wmmx_registers
574 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
575 store_wmmx_regs (regcache
);
576 else if (tdep
->vfp_register_count
> 0
577 && regno
>= ARM_D0_REGNUM
578 && regno
<= ARM_D0_REGNUM
+ tdep
->vfp_register_count
)
579 store_vfp_regs (regcache
);
583 /* Wrapper functions for the standard regset handling, used by
587 fill_gregset (const struct regcache
*regcache
,
588 gdb_gregset_t
*gregsetp
, int regno
)
590 arm_linux_collect_gregset (NULL
, regcache
, regno
, gregsetp
, 0);
594 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
596 arm_linux_supply_gregset (NULL
, regcache
, -1, gregsetp
, 0);
600 fill_fpregset (const struct regcache
*regcache
,
601 gdb_fpregset_t
*fpregsetp
, int regno
)
603 arm_linux_collect_nwfpe (NULL
, regcache
, regno
, fpregsetp
, 0);
606 /* Fill GDB's register array with the floating-point register values
610 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
612 arm_linux_supply_nwfpe (NULL
, regcache
, -1, fpregsetp
, 0);
615 /* Fetch the thread-local storage pointer for libthread_db. */
618 ps_get_thread_area (const struct ps_prochandle
*ph
,
619 lwpid_t lwpid
, int idx
, void **base
)
621 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
624 /* IDX is the bias from the thread pointer to the beginning of the
625 thread descriptor. It has to be subtracted due to implementation
626 quirks in libthread_db. */
627 *base
= (void *) ((char *)*base
- idx
);
632 static const struct target_desc
*
633 arm_linux_read_description (struct target_ops
*ops
)
635 CORE_ADDR arm_hwcap
= 0;
637 if (have_ptrace_getregset
== -1)
639 elf_gregset_t gpregs
;
641 int tid
= GET_THREAD_ID (inferior_ptid
);
643 iov
.iov_base
= &gpregs
;
644 iov
.iov_len
= sizeof (gpregs
);
646 /* Check if PTRACE_GETREGSET works. */
647 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
) < 0)
648 have_ptrace_getregset
= 0;
650 have_ptrace_getregset
= 1;
653 if (target_auxv_search (ops
, AT_HWCAP
, &arm_hwcap
) != 1)
655 return ops
->beneath
->to_read_description (ops
->beneath
);
658 if (arm_hwcap
& HWCAP_IWMMXT
)
659 return tdesc_arm_with_iwmmxt
;
661 if (arm_hwcap
& HWCAP_VFP
)
665 const struct target_desc
* result
= NULL
;
667 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
668 Neon with VFPv3-D32. */
669 if (arm_hwcap
& HWCAP_NEON
)
670 result
= tdesc_arm_with_neon
;
671 else if ((arm_hwcap
& (HWCAP_VFPv3
| HWCAP_VFPv3D16
)) == HWCAP_VFPv3
)
672 result
= tdesc_arm_with_vfpv3
;
674 result
= tdesc_arm_with_vfpv2
;
676 /* Now make sure that the kernel supports reading these
677 registers. Support was added in 2.6.30. */
678 pid
= ptid_get_lwp (inferior_ptid
);
680 buf
= alloca (VFP_REGS_SIZE
);
681 if (ptrace (PTRACE_GETVFPREGS
, pid
, 0, buf
) < 0
688 return ops
->beneath
->to_read_description (ops
->beneath
);
691 /* Information describing the hardware breakpoint capabilities. */
692 struct arm_linux_hwbp_cap
695 gdb_byte max_wp_length
;
700 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
701 assume a maximum number of supported break-/watchpoints. */
705 /* Get hold of the Hardware Breakpoint information for the target we are
706 attached to. Returns NULL if the kernel doesn't support Hardware
707 breakpoints at all, or a pointer to the information structure. */
708 static const struct arm_linux_hwbp_cap
*
709 arm_linux_get_hwbp_cap (void)
711 /* The info structure we return. */
712 static struct arm_linux_hwbp_cap info
;
714 /* Is INFO in a good state? -1 means that no attempt has been made to
715 initialize INFO; 0 means an attempt has been made, but it failed; 1
716 means INFO is in an initialized state. */
717 static int available
= -1;
724 tid
= GET_THREAD_ID (inferior_ptid
);
725 if (ptrace (PTRACE_GETHBPREGS
, tid
, 0, &val
) < 0)
729 info
.arch
= (gdb_byte
)((val
>> 24) & 0xff);
730 info
.max_wp_length
= (gdb_byte
)((val
>> 16) & 0xff);
731 info
.wp_count
= (gdb_byte
)((val
>> 8) & 0xff);
732 info
.bp_count
= (gdb_byte
)(val
& 0xff);
734 if (info
.wp_count
> MAX_WPTS
)
736 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
737 supports %d"), MAX_WPTS
, info
.wp_count
);
738 info
.wp_count
= MAX_WPTS
;
741 if (info
.bp_count
> MAX_BPTS
)
743 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
744 supports %d"), MAX_BPTS
, info
.bp_count
);
745 info
.bp_count
= MAX_BPTS
;
747 available
= (info
.arch
!= 0);
751 return available
== 1 ? &info
: NULL
;
754 /* How many hardware breakpoints are available? */
756 arm_linux_get_hw_breakpoint_count (void)
758 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
759 return cap
!= NULL
? cap
->bp_count
: 0;
762 /* How many hardware watchpoints are available? */
764 arm_linux_get_hw_watchpoint_count (void)
766 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
767 return cap
!= NULL
? cap
->wp_count
: 0;
770 /* Have we got a free break-/watch-point available for use? Returns -1 if
771 there is not an appropriate resource available, otherwise returns 1. */
773 arm_linux_can_use_hw_breakpoint (struct target_ops
*self
,
774 int type
, int cnt
, int ot
)
776 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
777 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
779 int count
= arm_linux_get_hw_watchpoint_count ();
783 else if (cnt
+ ot
> count
)
786 else if (type
== bp_hardware_breakpoint
)
788 int count
= arm_linux_get_hw_breakpoint_count ();
792 else if (cnt
> count
)
801 /* Enum describing the different types of ARM hardware break-/watch-points. */
810 /* Type describing an ARM Hardware Breakpoint Control register value. */
811 typedef unsigned int arm_hwbp_control_t
;
813 /* Structure used to keep track of hardware break-/watch-points. */
814 struct arm_linux_hw_breakpoint
816 /* Address to break on, or being watched. */
817 unsigned int address
;
818 /* Control register for break-/watch- point. */
819 arm_hwbp_control_t control
;
822 /* Structure containing arrays of per process hardware break-/watchpoints
823 for caching address and control information.
825 The Linux ptrace interface to hardware break-/watch-points presents the
826 values in a vector centred around 0 (which is used fo generic information).
827 Positive indicies refer to breakpoint addresses/control registers, negative
828 indices to watchpoint addresses/control registers.
830 The Linux vector is indexed as follows:
831 -((i << 1) + 2): Control register for watchpoint i.
832 -((i << 1) + 1): Address register for watchpoint i.
833 0: Information register.
834 ((i << 1) + 1): Address register for breakpoint i.
835 ((i << 1) + 2): Control register for breakpoint i.
837 This structure is used as a per-thread cache of the state stored by the
838 kernel, so that we don't need to keep calling into the kernel to find a
841 We treat break-/watch-points with their enable bit clear as being deleted.
843 struct arm_linux_debug_reg_state
845 /* Hardware breakpoints for this process. */
846 struct arm_linux_hw_breakpoint bpts
[MAX_BPTS
];
847 /* Hardware watchpoints for this process. */
848 struct arm_linux_hw_breakpoint wpts
[MAX_WPTS
];
851 /* Per-process arch-specific data we want to keep. */
852 struct arm_linux_process_info
855 struct arm_linux_process_info
*next
;
856 /* The process identifier. */
858 /* Hardware break-/watchpoints state information. */
859 struct arm_linux_debug_reg_state state
;
863 /* Per-thread arch-specific data we want to keep. */
866 /* Non-zero if our copy differs from what's recorded in the thread. */
867 char bpts_changed
[MAX_BPTS
];
868 char wpts_changed
[MAX_WPTS
];
871 static struct arm_linux_process_info
*arm_linux_process_list
= NULL
;
873 /* Find process data for process PID. */
875 static struct arm_linux_process_info
*
876 arm_linux_find_process_pid (pid_t pid
)
878 struct arm_linux_process_info
*proc
;
880 for (proc
= arm_linux_process_list
; proc
; proc
= proc
->next
)
881 if (proc
->pid
== pid
)
887 /* Add process data for process PID. Returns newly allocated info
890 static struct arm_linux_process_info
*
891 arm_linux_add_process (pid_t pid
)
893 struct arm_linux_process_info
*proc
;
895 proc
= xcalloc (1, sizeof (*proc
));
898 proc
->next
= arm_linux_process_list
;
899 arm_linux_process_list
= proc
;
904 /* Get data specific info for process PID, creating it if necessary.
905 Never returns NULL. */
907 static struct arm_linux_process_info
*
908 arm_linux_process_info_get (pid_t pid
)
910 struct arm_linux_process_info
*proc
;
912 proc
= arm_linux_find_process_pid (pid
);
914 proc
= arm_linux_add_process (pid
);
919 /* Called whenever GDB is no longer debugging process PID. It deletes
920 data structures that keep track of debug register state. */
923 arm_linux_forget_process (pid_t pid
)
925 struct arm_linux_process_info
*proc
, **proc_link
;
927 proc
= arm_linux_process_list
;
928 proc_link
= &arm_linux_process_list
;
932 if (proc
->pid
== pid
)
934 *proc_link
= proc
->next
;
940 proc_link
= &proc
->next
;
945 /* Get hardware break-/watchpoint state for process PID. */
947 static struct arm_linux_debug_reg_state
*
948 arm_linux_get_debug_reg_state (pid_t pid
)
950 return &arm_linux_process_info_get (pid
)->state
;
953 /* Initialize an ARM hardware break-/watch-point control register value.
954 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
955 type of break-/watch-point; ENABLE indicates whether the point is enabled.
957 static arm_hwbp_control_t
958 arm_hwbp_control_initialize (unsigned byte_address_select
,
959 arm_hwbp_type hwbp_type
,
962 gdb_assert ((byte_address_select
& ~0xffU
) == 0);
963 gdb_assert (hwbp_type
!= arm_hwbp_break
964 || ((byte_address_select
& 0xfU
) != 0));
966 return (byte_address_select
<< 5) | (hwbp_type
<< 3) | (3 << 1) | enable
;
969 /* Does the breakpoint control value CONTROL have the enable bit set? */
971 arm_hwbp_control_is_enabled (arm_hwbp_control_t control
)
973 return control
& 0x1;
976 /* Change a breakpoint control word so that it is in the disabled state. */
977 static arm_hwbp_control_t
978 arm_hwbp_control_disable (arm_hwbp_control_t control
)
980 return control
& ~0x1;
983 /* Initialise the hardware breakpoint structure P. The breakpoint will be
984 enabled, and will point to the placed address of BP_TGT. */
986 arm_linux_hw_breakpoint_initialize (struct gdbarch
*gdbarch
,
987 struct bp_target_info
*bp_tgt
,
988 struct arm_linux_hw_breakpoint
*p
)
991 CORE_ADDR address
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
993 /* We have to create a mask for the control register which says which bits
994 of the word pointed to by address to break on. */
995 if (arm_pc_is_thumb (gdbarch
, address
))
1006 p
->address
= (unsigned int) address
;
1007 p
->control
= arm_hwbp_control_initialize (mask
, arm_hwbp_break
, 1);
1010 /* Get the ARM hardware breakpoint type from the RW value we're given when
1011 asked to set a watchpoint. */
1012 static arm_hwbp_type
1013 arm_linux_get_hwbp_type (int rw
)
1016 return arm_hwbp_load
;
1017 else if (rw
== hw_write
)
1018 return arm_hwbp_store
;
1020 return arm_hwbp_access
;
1023 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
1024 to LEN. The type of watchpoint is given in RW. */
1026 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr
, int len
, int rw
,
1027 struct arm_linux_hw_breakpoint
*p
)
1029 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
1032 gdb_assert (cap
!= NULL
);
1033 gdb_assert (cap
->max_wp_length
!= 0);
1035 mask
= (1 << len
) - 1;
1037 p
->address
= (unsigned int) addr
;
1038 p
->control
= arm_hwbp_control_initialize (mask
,
1039 arm_linux_get_hwbp_type (rw
), 1);
1042 /* Are two break-/watch-points equal? */
1044 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint
*p1
,
1045 const struct arm_linux_hw_breakpoint
*p2
)
1047 return p1
->address
== p2
->address
&& p1
->control
== p2
->control
;
1050 /* Callback to mark a watch-/breakpoint to be updated in all threads of
1051 the current process. */
1053 struct update_registers_data
1060 update_registers_callback (struct lwp_info
*lwp
, void *arg
)
1062 struct update_registers_data
*data
= (struct update_registers_data
*) arg
;
1064 if (lwp
->arch_private
== NULL
)
1065 lwp
->arch_private
= XCNEW (struct arch_lwp_info
);
1067 /* The actual update is done later just before resuming the lwp,
1068 we just mark that the registers need updating. */
1070 lwp
->arch_private
->wpts_changed
[data
->index
] = 1;
1072 lwp
->arch_private
->bpts_changed
[data
->index
] = 1;
1074 /* If the lwp isn't stopped, force it to momentarily pause, so
1075 we can update its breakpoint registers. */
1077 linux_stop_lwp (lwp
);
1082 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
1083 =1) BPT for thread TID. */
1085 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
* bpt
,
1091 struct arm_linux_hw_breakpoint
* bpts
;
1092 struct update_registers_data data
;
1094 pid
= ptid_get_pid (inferior_ptid
);
1095 pid_ptid
= pid_to_ptid (pid
);
1099 count
= arm_linux_get_hw_watchpoint_count ();
1100 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1104 count
= arm_linux_get_hw_breakpoint_count ();
1105 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1108 for (i
= 0; i
< count
; ++i
)
1109 if (!arm_hwbp_control_is_enabled (bpts
[i
].control
))
1111 data
.watch
= watchpoint
;
1114 iterate_over_lwps (pid_ptid
, update_registers_callback
, &data
);
1118 gdb_assert (i
!= count
);
1121 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1122 (WATCHPOINT = 1) BPT for thread TID. */
1124 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
*bpt
,
1130 struct arm_linux_hw_breakpoint
* bpts
;
1131 struct update_registers_data data
;
1133 pid
= ptid_get_pid (inferior_ptid
);
1134 pid_ptid
= pid_to_ptid (pid
);
1138 count
= arm_linux_get_hw_watchpoint_count ();
1139 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1143 count
= arm_linux_get_hw_breakpoint_count ();
1144 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1147 for (i
= 0; i
< count
; ++i
)
1148 if (arm_linux_hw_breakpoint_equal (bpt
, bpts
+ i
))
1150 data
.watch
= watchpoint
;
1152 bpts
[i
].control
= arm_hwbp_control_disable (bpts
[i
].control
);
1153 iterate_over_lwps (pid_ptid
, update_registers_callback
, &data
);
1157 gdb_assert (i
!= count
);
1160 /* Insert a Hardware breakpoint. */
1162 arm_linux_insert_hw_breakpoint (struct target_ops
*self
,
1163 struct gdbarch
*gdbarch
,
1164 struct bp_target_info
*bp_tgt
)
1166 struct lwp_info
*lp
;
1167 struct arm_linux_hw_breakpoint p
;
1169 if (arm_linux_get_hw_breakpoint_count () == 0)
1172 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1174 arm_linux_insert_hw_breakpoint1 (&p
, 0);
1179 /* Remove a hardware breakpoint. */
1181 arm_linux_remove_hw_breakpoint (struct target_ops
*self
,
1182 struct gdbarch
*gdbarch
,
1183 struct bp_target_info
*bp_tgt
)
1185 struct lwp_info
*lp
;
1186 struct arm_linux_hw_breakpoint p
;
1188 if (arm_linux_get_hw_breakpoint_count () == 0)
1191 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1193 arm_linux_remove_hw_breakpoint1 (&p
, 0);
1198 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1201 arm_linux_region_ok_for_hw_watchpoint (struct target_ops
*self
,
1202 CORE_ADDR addr
, int len
)
1204 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
1205 CORE_ADDR max_wp_length
, aligned_addr
;
1207 /* Can not set watchpoints for zero or negative lengths. */
1211 /* Need to be able to use the ptrace interface. */
1212 if (cap
== NULL
|| cap
->wp_count
== 0)
1215 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1216 range covered by a watchpoint. */
1217 max_wp_length
= (CORE_ADDR
)cap
->max_wp_length
;
1218 aligned_addr
= addr
& ~(max_wp_length
- 1);
1220 if (aligned_addr
+ max_wp_length
< addr
+ len
)
1223 /* The current ptrace interface can only handle watchpoints that are a
1225 if ((len
& (len
- 1)) != 0)
1228 /* All tests passed so we must be able to set a watchpoint. */
1232 /* Insert a Hardware breakpoint. */
1234 arm_linux_insert_watchpoint (struct target_ops
*self
,
1235 CORE_ADDR addr
, int len
, int rw
,
1236 struct expression
*cond
)
1238 struct lwp_info
*lp
;
1239 struct arm_linux_hw_breakpoint p
;
1241 if (arm_linux_get_hw_watchpoint_count () == 0)
1244 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1246 arm_linux_insert_hw_breakpoint1 (&p
, 1);
1251 /* Remove a hardware breakpoint. */
1253 arm_linux_remove_watchpoint (struct target_ops
*self
,
1254 CORE_ADDR addr
, int len
, int rw
,
1255 struct expression
*cond
)
1257 struct lwp_info
*lp
;
1258 struct arm_linux_hw_breakpoint p
;
1260 if (arm_linux_get_hw_watchpoint_count () == 0)
1263 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1265 arm_linux_remove_hw_breakpoint1 (&p
, 1);
1270 /* What was the data address the target was stopped on accessing. */
1272 arm_linux_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
1277 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
1280 /* This must be a hardware breakpoint. */
1281 if (siginfo
.si_signo
!= SIGTRAP
1282 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1285 /* We must be able to set hardware watchpoints. */
1286 if (arm_linux_get_hw_watchpoint_count () == 0)
1289 slot
= siginfo
.si_errno
;
1291 /* If we are in a positive slot then we're looking at a breakpoint and not
1296 *addr_p
= (CORE_ADDR
) (uintptr_t) siginfo
.si_addr
;
1300 /* Has the target been stopped by hitting a watchpoint? */
1302 arm_linux_stopped_by_watchpoint (struct target_ops
*ops
)
1305 return arm_linux_stopped_data_address (ops
, &addr
);
1309 arm_linux_watchpoint_addr_within_range (struct target_ops
*target
,
1311 CORE_ADDR start
, int length
)
1313 return start
<= addr
&& start
+ length
- 1 >= addr
;
1316 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1317 in the parent thread to the child thread. */
1319 arm_linux_new_thread (struct lwp_info
*lp
)
1322 struct arch_lwp_info
*info
= XCNEW (struct arch_lwp_info
);
1324 /* Mark that all the hardware breakpoint/watchpoint register pairs
1325 for this thread need to be initialized. */
1327 for (i
= 0; i
< MAX_BPTS
; i
++)
1329 info
->bpts_changed
[i
] = 1;
1330 info
->wpts_changed
[i
] = 1;
1333 lp
->arch_private
= info
;
1336 /* Called when resuming a thread.
1337 The hardware debug registers are updated when there is any change. */
1340 arm_linux_prepare_to_resume (struct lwp_info
*lwp
)
1343 struct arm_linux_hw_breakpoint
*bpts
, *wpts
;
1344 struct arch_lwp_info
*arm_lwp_info
= lwp
->arch_private
;
1346 pid
= ptid_get_lwp (lwp
->ptid
);
1347 bpts
= arm_linux_get_debug_reg_state (ptid_get_pid (lwp
->ptid
))->bpts
;
1348 wpts
= arm_linux_get_debug_reg_state (ptid_get_pid (lwp
->ptid
))->wpts
;
1350 /* NULL means this is the main thread still going through the shell,
1351 or, no watchpoint has been set yet. In that case, there's
1353 if (arm_lwp_info
== NULL
)
1356 for (i
= 0; i
< arm_linux_get_hw_breakpoint_count (); i
++)
1357 if (arm_lwp_info
->bpts_changed
[i
])
1360 if (arm_hwbp_control_is_enabled (bpts
[i
].control
))
1361 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1362 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 1), &bpts
[i
].address
) < 0)
1363 perror_with_name (_("Unexpected error setting breakpoint"));
1365 if (bpts
[i
].control
!= 0)
1366 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1367 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 2), &bpts
[i
].control
) < 0)
1368 perror_with_name (_("Unexpected error setting breakpoint"));
1370 arm_lwp_info
->bpts_changed
[i
] = 0;
1373 for (i
= 0; i
< arm_linux_get_hw_watchpoint_count (); i
++)
1374 if (arm_lwp_info
->wpts_changed
[i
])
1377 if (arm_hwbp_control_is_enabled (wpts
[i
].control
))
1378 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1379 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 1), &wpts
[i
].address
) < 0)
1380 perror_with_name (_("Unexpected error setting watchpoint"));
1382 if (wpts
[i
].control
!= 0)
1383 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1384 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 2), &wpts
[i
].control
) < 0)
1385 perror_with_name (_("Unexpected error setting watchpoint"));
1387 arm_lwp_info
->wpts_changed
[i
] = 0;
1391 /* linux_nat_new_fork hook. */
1394 arm_linux_new_fork (struct lwp_info
*parent
, pid_t child_pid
)
1397 struct arm_linux_debug_reg_state
*parent_state
;
1398 struct arm_linux_debug_reg_state
*child_state
;
1400 /* NULL means no watchpoint has ever been set in the parent. In
1401 that case, there's nothing to do. */
1402 if (parent
->arch_private
== NULL
)
1405 /* GDB core assumes the child inherits the watchpoints/hw
1406 breakpoints of the parent, and will remove them all from the
1407 forked off process. Copy the debug registers mirrors into the
1408 new process so that all breakpoints and watchpoints can be
1409 removed together. */
1411 parent_pid
= ptid_get_pid (parent
->ptid
);
1412 parent_state
= arm_linux_get_debug_reg_state (parent_pid
);
1413 child_state
= arm_linux_get_debug_reg_state (child_pid
);
1414 *child_state
= *parent_state
;
1417 void _initialize_arm_linux_nat (void);
1420 _initialize_arm_linux_nat (void)
1422 struct target_ops
*t
;
1424 /* Fill in the generic GNU/Linux methods. */
1425 t
= linux_target ();
1427 /* Add our register access methods. */
1428 t
->to_fetch_registers
= arm_linux_fetch_inferior_registers
;
1429 t
->to_store_registers
= arm_linux_store_inferior_registers
;
1431 /* Add our hardware breakpoint and watchpoint implementation. */
1432 t
->to_can_use_hw_breakpoint
= arm_linux_can_use_hw_breakpoint
;
1433 t
->to_insert_hw_breakpoint
= arm_linux_insert_hw_breakpoint
;
1434 t
->to_remove_hw_breakpoint
= arm_linux_remove_hw_breakpoint
;
1435 t
->to_region_ok_for_hw_watchpoint
= arm_linux_region_ok_for_hw_watchpoint
;
1436 t
->to_insert_watchpoint
= arm_linux_insert_watchpoint
;
1437 t
->to_remove_watchpoint
= arm_linux_remove_watchpoint
;
1438 t
->to_stopped_by_watchpoint
= arm_linux_stopped_by_watchpoint
;
1439 t
->to_stopped_data_address
= arm_linux_stopped_data_address
;
1440 t
->to_watchpoint_addr_within_range
= arm_linux_watchpoint_addr_within_range
;
1442 t
->to_read_description
= arm_linux_read_description
;
1444 /* Register the target. */
1445 linux_nat_add_target (t
);
1447 /* Handle thread creation and exit. */
1448 linux_nat_set_new_thread (t
, arm_linux_new_thread
);
1449 linux_nat_set_prepare_to_resume (t
, arm_linux_prepare_to_resume
);
1451 /* Handle process creation and exit. */
1452 linux_nat_set_new_fork (t
, arm_linux_new_fork
);
1453 linux_nat_set_forget_process (t
, arm_linux_forget_process
);