1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999-2024 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/>. */
23 #include "linux-nat.h"
24 #include "target-descriptions.h"
26 #include "observable.h"
27 #include "gdbthread.h"
29 #include "aarch32-tdep.h"
31 #include "arm-linux-tdep.h"
32 #include "aarch32-linux-nat.h"
34 #include <elf/common.h>
36 #include "nat/gdb_ptrace.h"
37 #include <sys/utsname.h>
38 #include <sys/procfs.h>
40 #include "nat/linux-ptrace.h"
41 #include "linux-tdep.h"
43 /* Prototypes for supply_gregset etc. */
46 /* Defines ps_err_e, struct ps_prochandle. */
47 #include "gdb_proc_service.h"
49 #ifndef PTRACE_GET_THREAD_AREA
50 #define PTRACE_GET_THREAD_AREA 22
53 #ifndef PTRACE_GETWMMXREGS
54 #define PTRACE_GETWMMXREGS 18
55 #define PTRACE_SETWMMXREGS 19
58 #ifndef PTRACE_GETVFPREGS
59 #define PTRACE_GETVFPREGS 27
60 #define PTRACE_SETVFPREGS 28
63 #ifndef PTRACE_GETHBPREGS
64 #define PTRACE_GETHBPREGS 29
65 #define PTRACE_SETHBPREGS 30
68 class arm_linux_nat_target final
: public linux_nat_target
71 /* Add our register access methods. */
72 void fetch_registers (struct regcache
*, int) override
;
73 void store_registers (struct regcache
*, int) override
;
75 /* Add our hardware breakpoint and watchpoint implementation. */
76 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
78 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
80 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
82 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
84 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
85 struct expression
*) override
;
87 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
88 struct expression
*) override
;
89 bool stopped_by_watchpoint () override
;
91 bool stopped_data_address (CORE_ADDR
*) override
;
93 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
95 const struct target_desc
*read_description () override
;
97 /* Override linux_nat_target low methods. */
99 /* Handle thread creation and exit. */
100 void low_new_thread (struct lwp_info
*lp
) override
;
101 void low_delete_thread (struct arch_lwp_info
*lp
) override
;
102 void low_prepare_to_resume (struct lwp_info
*lp
) override
;
104 /* Handle process creation and exit. */
105 void low_new_fork (struct lwp_info
*parent
, pid_t child_pid
) override
;
106 void low_forget_process (pid_t pid
) override
;
109 static arm_linux_nat_target the_arm_linux_nat_target
;
111 /* Get the whole floating point state of the process and store it
115 fetch_fpregs (struct regcache
*regcache
)
118 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
120 /* Get the thread id for the ptrace call. */
121 tid
= regcache
->ptid ().lwp ();
123 /* Read the floating point state. */
124 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
129 iov
.iov_len
= ARM_LINUX_SIZEOF_NWFPE
;
131 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iov
);
134 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
137 perror_with_name (_("Unable to fetch the floating point registers"));
140 regcache
->raw_supply (ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
142 /* Fetch the floating point registers. */
143 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
144 supply_nwfpe_register (regcache
, regno
, fp
);
147 /* Save the whole floating point state of the process using
148 the contents from regcache. */
151 store_fpregs (const struct regcache
*regcache
)
154 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
156 /* Get the thread id for the ptrace call. */
157 tid
= regcache
->ptid ().lwp ();
159 /* Read the floating point state. */
160 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
162 elf_fpregset_t fpregs
;
165 iov
.iov_base
= &fpregs
;
166 iov
.iov_len
= sizeof (fpregs
);
168 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_FPREGSET
, &iov
);
171 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
174 perror_with_name (_("Unable to fetch the floating point registers"));
177 if (REG_VALID
== regcache
->get_register_status (ARM_FPS_REGNUM
))
178 regcache
->raw_collect (ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
180 /* Store the floating point registers. */
181 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
182 if (REG_VALID
== regcache
->get_register_status (regno
))
183 collect_nwfpe_register (regcache
, regno
, fp
);
185 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
190 iov
.iov_len
= ARM_LINUX_SIZEOF_NWFPE
;
192 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_FPREGSET
, &iov
);
195 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
198 perror_with_name (_("Unable to store floating point registers"));
201 /* Fetch all general registers of the process and store into
205 fetch_regs (struct regcache
*regcache
)
210 /* Get the thread id for the ptrace call. */
211 tid
= regcache
->ptid ().lwp ();
213 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
217 iov
.iov_base
= ®s
;
218 iov
.iov_len
= sizeof (regs
);
220 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
);
223 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
226 perror_with_name (_("Unable to fetch general registers"));
228 aarch32_gp_regcache_supply (regcache
, (uint32_t *) regs
, arm_apcs_32
);
232 store_regs (const struct regcache
*regcache
)
237 /* Get the thread id for the ptrace call. */
238 tid
= regcache
->ptid ().lwp ();
240 /* Fetch the general registers. */
241 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
245 iov
.iov_base
= ®s
;
246 iov
.iov_len
= sizeof (regs
);
248 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
);
251 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
254 perror_with_name (_("Unable to fetch general registers"));
256 aarch32_gp_regcache_collect (regcache
, (uint32_t *) regs
, arm_apcs_32
);
258 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
262 iov
.iov_base
= ®s
;
263 iov
.iov_len
= sizeof (regs
);
265 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, &iov
);
268 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
271 perror_with_name (_("Unable to store general registers"));
274 /* Fetch all WMMX registers of the process and store into
278 fetch_wmmx_regs (struct regcache
*regcache
)
280 char regbuf
[IWMMXT_REGS_SIZE
];
283 /* Get the thread id for the ptrace call. */
284 tid
= regcache
->ptid ().lwp ();
286 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
288 perror_with_name (_("Unable to fetch WMMX registers"));
290 for (regno
= 0; regno
< 16; regno
++)
291 regcache
->raw_supply (regno
+ ARM_WR0_REGNUM
, ®buf
[regno
* 8]);
293 for (regno
= 0; regno
< 2; regno
++)
294 regcache
->raw_supply (regno
+ ARM_WCSSF_REGNUM
,
295 ®buf
[16 * 8 + regno
* 4]);
297 for (regno
= 0; regno
< 4; regno
++)
298 regcache
->raw_supply (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
= regcache
->ptid ().lwp ();
311 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
313 perror_with_name (_("Unable to fetch WMMX registers"));
315 for (regno
= 0; regno
< 16; regno
++)
316 if (REG_VALID
== regcache
->get_register_status (regno
+ ARM_WR0_REGNUM
))
317 regcache
->raw_collect (regno
+ ARM_WR0_REGNUM
, ®buf
[regno
* 8]);
319 for (regno
= 0; regno
< 2; regno
++)
320 if (REG_VALID
== regcache
->get_register_status (regno
+ ARM_WCSSF_REGNUM
))
321 regcache
->raw_collect (regno
+ ARM_WCSSF_REGNUM
,
322 ®buf
[16 * 8 + regno
* 4]);
324 for (regno
= 0; regno
< 4; regno
++)
325 if (REG_VALID
== regcache
->get_register_status (regno
+ ARM_WCGR0_REGNUM
))
326 regcache
->raw_collect (regno
+ ARM_WCGR0_REGNUM
,
327 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
329 ret
= ptrace (PTRACE_SETWMMXREGS
, tid
, 0, regbuf
);
332 perror_with_name (_("Unable to store WMMX registers"));
336 fetch_vfp_regs (struct regcache
*regcache
)
338 gdb_byte regbuf
[ARM_VFP3_REGS_SIZE
];
340 struct gdbarch
*gdbarch
= regcache
->arch ();
341 arm_gdbarch_tdep
*tdep
= gdbarch_tdep
<arm_gdbarch_tdep
> (gdbarch
);
343 /* Get the thread id for the ptrace call. */
344 tid
= regcache
->ptid ().lwp ();
346 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
350 iov
.iov_base
= regbuf
;
351 iov
.iov_len
= ARM_VFP3_REGS_SIZE
;
352 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
355 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
358 perror_with_name (_("Unable to fetch VFP registers"));
360 aarch32_vfp_regcache_supply (regcache
, regbuf
,
361 tdep
->vfp_register_count
);
365 store_vfp_regs (const struct regcache
*regcache
)
367 gdb_byte regbuf
[ARM_VFP3_REGS_SIZE
];
369 struct gdbarch
*gdbarch
= regcache
->arch ();
370 arm_gdbarch_tdep
*tdep
= gdbarch_tdep
<arm_gdbarch_tdep
> (gdbarch
);
372 /* Get the thread id for the ptrace call. */
373 tid
= regcache
->ptid ().lwp ();
375 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
379 iov
.iov_base
= regbuf
;
380 iov
.iov_len
= ARM_VFP3_REGS_SIZE
;
381 ret
= ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_VFP
, &iov
);
384 ret
= ptrace (PTRACE_GETVFPREGS
, tid
, 0, regbuf
);
387 perror_with_name (_("Unable to fetch VFP registers (for update)"));
389 aarch32_vfp_regcache_collect (regcache
, regbuf
,
390 tdep
->vfp_register_count
);
392 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
396 iov
.iov_base
= regbuf
;
397 iov
.iov_len
= ARM_VFP3_REGS_SIZE
;
398 ret
= ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_VFP
, &iov
);
401 ret
= ptrace (PTRACE_SETVFPREGS
, tid
, 0, regbuf
);
404 perror_with_name (_("Unable to store VFP registers"));
407 /* Fetch registers from the child process. Fetch all registers if
408 regno == -1, otherwise fetch all general registers or all floating
409 point registers depending upon the value of regno. */
412 arm_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
414 struct gdbarch
*gdbarch
= regcache
->arch ();
415 arm_gdbarch_tdep
*tdep
= gdbarch_tdep
<arm_gdbarch_tdep
> (gdbarch
);
419 fetch_regs (regcache
);
420 if (tdep
->have_wmmx_registers
)
421 fetch_wmmx_regs (regcache
);
422 if (tdep
->vfp_register_count
> 0)
423 fetch_vfp_regs (regcache
);
424 if (tdep
->have_fpa_registers
)
425 fetch_fpregs (regcache
);
429 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
430 fetch_regs (regcache
);
431 else if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
432 fetch_fpregs (regcache
);
433 else if (tdep
->have_wmmx_registers
434 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
435 fetch_wmmx_regs (regcache
);
436 else if (tdep
->vfp_register_count
> 0
437 && regno
>= ARM_D0_REGNUM
438 && (regno
< ARM_D0_REGNUM
+ tdep
->vfp_register_count
439 || regno
== ARM_FPSCR_REGNUM
))
440 fetch_vfp_regs (regcache
);
444 /* Store registers back into the inferior. Store all registers if
445 regno == -1, otherwise store all general registers or all floating
446 point registers depending upon the value of regno. */
449 arm_linux_nat_target::store_registers (struct regcache
*regcache
, int regno
)
451 struct gdbarch
*gdbarch
= regcache
->arch ();
452 arm_gdbarch_tdep
*tdep
= gdbarch_tdep
<arm_gdbarch_tdep
> (gdbarch
);
456 store_regs (regcache
);
457 if (tdep
->have_wmmx_registers
)
458 store_wmmx_regs (regcache
);
459 if (tdep
->vfp_register_count
> 0)
460 store_vfp_regs (regcache
);
461 if (tdep
->have_fpa_registers
)
462 store_fpregs (regcache
);
466 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
467 store_regs (regcache
);
468 else if ((regno
>= ARM_F0_REGNUM
) && (regno
<= ARM_FPS_REGNUM
))
469 store_fpregs (regcache
);
470 else if (tdep
->have_wmmx_registers
471 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
472 store_wmmx_regs (regcache
);
473 else if (tdep
->vfp_register_count
> 0
474 && regno
>= ARM_D0_REGNUM
475 && (regno
< ARM_D0_REGNUM
+ tdep
->vfp_register_count
476 || regno
== ARM_FPSCR_REGNUM
))
477 store_vfp_regs (regcache
);
481 /* Wrapper functions for the standard regset handling, used by
485 fill_gregset (const struct regcache
*regcache
,
486 gdb_gregset_t
*gregsetp
, int regno
)
488 arm_linux_collect_gregset (NULL
, regcache
, regno
, gregsetp
, 0);
492 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
494 arm_linux_supply_gregset (NULL
, regcache
, -1, gregsetp
, 0);
498 fill_fpregset (const struct regcache
*regcache
,
499 gdb_fpregset_t
*fpregsetp
, int regno
)
501 arm_linux_collect_nwfpe (NULL
, regcache
, regno
, fpregsetp
, 0);
504 /* Fill GDB's register array with the floating-point register values
508 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
510 arm_linux_supply_nwfpe (NULL
, regcache
, -1, fpregsetp
, 0);
513 /* Fetch the thread-local storage pointer for libthread_db. */
516 ps_get_thread_area (struct ps_prochandle
*ph
,
517 lwpid_t lwpid
, int idx
, void **base
)
519 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
522 /* IDX is the bias from the thread pointer to the beginning of the
523 thread descriptor. It has to be subtracted due to implementation
524 quirks in libthread_db. */
525 *base
= (void *) ((char *)*base
- idx
);
530 const struct target_desc
*
531 arm_linux_nat_target::read_description ()
533 if (inferior_ptid
== null_ptid
)
534 return this->beneath ()->read_description ();
536 CORE_ADDR arm_hwcap
= linux_get_hwcap ();
538 if (have_ptrace_getregset
== TRIBOOL_UNKNOWN
)
540 elf_gregset_t gpregs
;
542 int tid
= inferior_ptid
.pid ();
544 iov
.iov_base
= &gpregs
;
545 iov
.iov_len
= sizeof (gpregs
);
547 /* Check if PTRACE_GETREGSET works. */
548 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, &iov
) < 0)
549 have_ptrace_getregset
= TRIBOOL_FALSE
;
551 have_ptrace_getregset
= TRIBOOL_TRUE
;
554 if (arm_hwcap
& HWCAP_IWMMXT
)
555 return arm_read_description (ARM_FP_TYPE_IWMMXT
, false);
557 if (arm_hwcap
& HWCAP_VFP
)
559 /* Make sure that the kernel supports reading VFP registers. Support was
561 int pid
= inferior_ptid
.pid ();
563 char *buf
= (char *) alloca (ARM_VFP3_REGS_SIZE
);
564 if (ptrace (PTRACE_GETVFPREGS
, pid
, 0, buf
) < 0 && errno
== EIO
)
567 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
568 Neon with VFPv3-D32. */
569 if (arm_hwcap
& HWCAP_NEON
)
570 return aarch32_read_description (false);
571 else if ((arm_hwcap
& (HWCAP_VFPv3
| HWCAP_VFPv3D16
)) == HWCAP_VFPv3
)
572 return arm_read_description (ARM_FP_TYPE_VFPV3
, false);
574 return arm_read_description (ARM_FP_TYPE_VFPV2
, false);
577 return this->beneath ()->read_description ();
580 /* Information describing the hardware breakpoint capabilities. */
581 struct arm_linux_hwbp_cap
584 gdb_byte max_wp_length
;
589 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
590 assume a maximum number of supported break-/watchpoints. */
594 /* Get hold of the Hardware Breakpoint information for the target we are
595 attached to. Returns NULL if the kernel doesn't support Hardware
596 breakpoints at all, or a pointer to the information structure. */
597 static const struct arm_linux_hwbp_cap
*
598 arm_linux_get_hwbp_cap (void)
600 /* The info structure we return. */
601 static struct arm_linux_hwbp_cap info
;
603 /* Is INFO in a good state? -1 means that no attempt has been made to
604 initialize INFO; 0 means an attempt has been made, but it failed; 1
605 means INFO is in an initialized state. */
606 static int available
= -1;
613 tid
= inferior_ptid
.lwp ();
614 if (ptrace (PTRACE_GETHBPREGS
, tid
, 0, &val
) < 0)
618 info
.arch
= (gdb_byte
)((val
>> 24) & 0xff);
619 info
.max_wp_length
= (gdb_byte
)((val
>> 16) & 0xff);
620 info
.wp_count
= (gdb_byte
)((val
>> 8) & 0xff);
621 info
.bp_count
= (gdb_byte
)(val
& 0xff);
623 if (info
.wp_count
> MAX_WPTS
)
625 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
626 supports %d"), MAX_WPTS
, info
.wp_count
);
627 info
.wp_count
= MAX_WPTS
;
630 if (info
.bp_count
> MAX_BPTS
)
632 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
633 supports %d"), MAX_BPTS
, info
.bp_count
);
634 info
.bp_count
= MAX_BPTS
;
636 available
= (info
.arch
!= 0);
640 return available
== 1 ? &info
: NULL
;
643 /* How many hardware breakpoints are available? */
645 arm_linux_get_hw_breakpoint_count (void)
647 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
648 return cap
!= NULL
? cap
->bp_count
: 0;
651 /* How many hardware watchpoints are available? */
653 arm_linux_get_hw_watchpoint_count (void)
655 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
656 return cap
!= NULL
? cap
->wp_count
: 0;
659 /* Have we got a free break-/watch-point available for use? Returns -1 if
660 there is not an appropriate resource available, otherwise returns 1. */
662 arm_linux_nat_target::can_use_hw_breakpoint (enum bptype type
,
665 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
666 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
668 int count
= arm_linux_get_hw_watchpoint_count ();
672 else if (cnt
+ ot
> count
)
675 else if (type
== bp_hardware_breakpoint
)
677 int count
= arm_linux_get_hw_breakpoint_count ();
681 else if (cnt
> count
)
685 gdb_assert_not_reached ("unknown breakpoint type");
690 /* Enum describing the different types of ARM hardware break-/watch-points. */
699 /* Type describing an ARM Hardware Breakpoint Control register value. */
700 typedef unsigned int arm_hwbp_control_t
;
702 /* Structure used to keep track of hardware break-/watch-points. */
703 struct arm_linux_hw_breakpoint
705 /* Address to break on, or being watched. */
706 unsigned int address
;
707 /* Control register for break-/watch- point. */
708 arm_hwbp_control_t control
;
711 /* Structure containing arrays of per process hardware break-/watchpoints
712 for caching address and control information.
714 The Linux ptrace interface to hardware break-/watch-points presents the
715 values in a vector centred around 0 (which is used fo generic information).
716 Positive indicies refer to breakpoint addresses/control registers, negative
717 indices to watchpoint addresses/control registers.
719 The Linux vector is indexed as follows:
720 -((i << 1) + 2): Control register for watchpoint i.
721 -((i << 1) + 1): Address register for watchpoint i.
722 0: Information register.
723 ((i << 1) + 1): Address register for breakpoint i.
724 ((i << 1) + 2): Control register for breakpoint i.
726 This structure is used as a per-thread cache of the state stored by the
727 kernel, so that we don't need to keep calling into the kernel to find a
730 We treat break-/watch-points with their enable bit clear as being deleted.
732 struct arm_linux_debug_reg_state
734 /* Hardware breakpoints for this process. */
735 struct arm_linux_hw_breakpoint bpts
[MAX_BPTS
];
736 /* Hardware watchpoints for this process. */
737 struct arm_linux_hw_breakpoint wpts
[MAX_WPTS
];
740 /* Per-process arch-specific data we want to keep. */
741 struct arm_linux_process_info
744 struct arm_linux_process_info
*next
;
745 /* The process identifier. */
747 /* Hardware break-/watchpoints state information. */
748 struct arm_linux_debug_reg_state state
;
752 /* Per-thread arch-specific data we want to keep. */
755 /* Non-zero if our copy differs from what's recorded in the thread. */
756 char bpts_changed
[MAX_BPTS
];
757 char wpts_changed
[MAX_WPTS
];
760 static struct arm_linux_process_info
*arm_linux_process_list
= NULL
;
762 /* Find process data for process PID. */
764 static struct arm_linux_process_info
*
765 arm_linux_find_process_pid (pid_t pid
)
767 struct arm_linux_process_info
*proc
;
769 for (proc
= arm_linux_process_list
; proc
; proc
= proc
->next
)
770 if (proc
->pid
== pid
)
776 /* Add process data for process PID. Returns newly allocated info
779 static struct arm_linux_process_info
*
780 arm_linux_add_process (pid_t pid
)
782 struct arm_linux_process_info
*proc
;
784 proc
= XCNEW (struct arm_linux_process_info
);
787 proc
->next
= arm_linux_process_list
;
788 arm_linux_process_list
= proc
;
793 /* Get data specific info for process PID, creating it if necessary.
794 Never returns NULL. */
796 static struct arm_linux_process_info
*
797 arm_linux_process_info_get (pid_t pid
)
799 struct arm_linux_process_info
*proc
;
801 proc
= arm_linux_find_process_pid (pid
);
803 proc
= arm_linux_add_process (pid
);
808 /* Called whenever GDB is no longer debugging process PID. It deletes
809 data structures that keep track of debug register state. */
812 arm_linux_nat_target::low_forget_process (pid_t pid
)
814 struct arm_linux_process_info
*proc
, **proc_link
;
816 proc
= arm_linux_process_list
;
817 proc_link
= &arm_linux_process_list
;
821 if (proc
->pid
== pid
)
823 *proc_link
= proc
->next
;
829 proc_link
= &proc
->next
;
834 /* Get hardware break-/watchpoint state for process PID. */
836 static struct arm_linux_debug_reg_state
*
837 arm_linux_get_debug_reg_state (pid_t pid
)
839 return &arm_linux_process_info_get (pid
)->state
;
842 /* Initialize an ARM hardware break-/watch-point control register value.
843 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
844 type of break-/watch-point; ENABLE indicates whether the point is enabled.
846 static arm_hwbp_control_t
847 arm_hwbp_control_initialize (unsigned byte_address_select
,
848 arm_hwbp_type hwbp_type
,
851 gdb_assert ((byte_address_select
& ~0xffU
) == 0);
852 gdb_assert (hwbp_type
!= arm_hwbp_break
853 || ((byte_address_select
& 0xfU
) != 0));
855 return (byte_address_select
<< 5) | (hwbp_type
<< 3) | (3 << 1) | enable
;
858 /* Does the breakpoint control value CONTROL have the enable bit set? */
860 arm_hwbp_control_is_enabled (arm_hwbp_control_t control
)
862 return control
& 0x1;
865 /* Change a breakpoint control word so that it is in the disabled state. */
866 static arm_hwbp_control_t
867 arm_hwbp_control_disable (arm_hwbp_control_t control
)
869 return control
& ~0x1;
872 /* Initialise the hardware breakpoint structure P. The breakpoint will be
873 enabled, and will point to the placed address of BP_TGT. */
875 arm_linux_hw_breakpoint_initialize (struct gdbarch
*gdbarch
,
876 struct bp_target_info
*bp_tgt
,
877 struct arm_linux_hw_breakpoint
*p
)
880 CORE_ADDR address
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
882 /* We have to create a mask for the control register which says which bits
883 of the word pointed to by address to break on. */
884 if (arm_pc_is_thumb (gdbarch
, address
))
895 p
->address
= (unsigned int) address
;
896 p
->control
= arm_hwbp_control_initialize (mask
, arm_hwbp_break
, 1);
899 /* Get the ARM hardware breakpoint type from the TYPE value we're
900 given when asked to set a watchpoint. */
902 arm_linux_get_hwbp_type (enum target_hw_bp_type type
)
905 return arm_hwbp_load
;
906 else if (type
== hw_write
)
907 return arm_hwbp_store
;
909 return arm_hwbp_access
;
912 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
913 to LEN. The type of watchpoint is given in RW. */
915 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr
, int len
,
916 enum target_hw_bp_type type
,
917 struct arm_linux_hw_breakpoint
*p
)
919 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
922 gdb_assert (cap
!= NULL
);
923 gdb_assert (cap
->max_wp_length
!= 0);
925 mask
= (1 << len
) - 1;
927 p
->address
= (unsigned int) addr
;
928 p
->control
= arm_hwbp_control_initialize (mask
,
929 arm_linux_get_hwbp_type (type
), 1);
932 /* Are two break-/watch-points equal? */
934 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint
*p1
,
935 const struct arm_linux_hw_breakpoint
*p2
)
937 return p1
->address
== p2
->address
&& p1
->control
== p2
->control
;
940 /* Callback to mark a watch-/breakpoint to be updated in all threads of
941 the current process. */
944 update_registers_callback (struct lwp_info
*lwp
, int watch
, int index
)
946 if (lwp
->arch_private
== NULL
)
947 lwp
->arch_private
= XCNEW (struct arch_lwp_info
);
949 /* The actual update is done later just before resuming the lwp,
950 we just mark that the registers need updating. */
952 lwp
->arch_private
->wpts_changed
[index
] = 1;
954 lwp
->arch_private
->bpts_changed
[index
] = 1;
956 /* If the lwp isn't stopped, force it to momentarily pause, so
957 we can update its breakpoint registers. */
959 linux_stop_lwp (lwp
);
964 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
965 =1) BPT for thread TID. */
967 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
* bpt
,
973 struct arm_linux_hw_breakpoint
* bpts
;
975 pid
= inferior_ptid
.pid ();
976 pid_ptid
= ptid_t (pid
);
980 count
= arm_linux_get_hw_watchpoint_count ();
981 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
985 count
= arm_linux_get_hw_breakpoint_count ();
986 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
989 for (i
= 0; i
< count
; ++i
)
990 if (!arm_hwbp_control_is_enabled (bpts
[i
].control
))
993 iterate_over_lwps (pid_ptid
,
994 [=] (struct lwp_info
*info
)
996 return update_registers_callback (info
, watchpoint
,
1002 gdb_assert (i
!= count
);
1005 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1006 (WATCHPOINT = 1) BPT for thread TID. */
1008 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint
*bpt
,
1014 struct arm_linux_hw_breakpoint
* bpts
;
1016 pid
= inferior_ptid
.pid ();
1017 pid_ptid
= ptid_t (pid
);
1021 count
= arm_linux_get_hw_watchpoint_count ();
1022 bpts
= arm_linux_get_debug_reg_state (pid
)->wpts
;
1026 count
= arm_linux_get_hw_breakpoint_count ();
1027 bpts
= arm_linux_get_debug_reg_state (pid
)->bpts
;
1030 for (i
= 0; i
< count
; ++i
)
1031 if (arm_linux_hw_breakpoint_equal (bpt
, bpts
+ i
))
1033 bpts
[i
].control
= arm_hwbp_control_disable (bpts
[i
].control
);
1034 iterate_over_lwps (pid_ptid
,
1035 [=] (struct lwp_info
*info
)
1037 return update_registers_callback (info
, watchpoint
,
1043 gdb_assert (i
!= count
);
1046 /* Insert a Hardware breakpoint. */
1048 arm_linux_nat_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
1049 struct bp_target_info
*bp_tgt
)
1051 struct arm_linux_hw_breakpoint p
;
1053 if (arm_linux_get_hw_breakpoint_count () == 0)
1056 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1058 arm_linux_insert_hw_breakpoint1 (&p
, 0);
1063 /* Remove a hardware breakpoint. */
1065 arm_linux_nat_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
1066 struct bp_target_info
*bp_tgt
)
1068 struct arm_linux_hw_breakpoint p
;
1070 if (arm_linux_get_hw_breakpoint_count () == 0)
1073 arm_linux_hw_breakpoint_initialize (gdbarch
, bp_tgt
, &p
);
1075 arm_linux_remove_hw_breakpoint1 (&p
, 0);
1080 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1083 arm_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
1085 const struct arm_linux_hwbp_cap
*cap
= arm_linux_get_hwbp_cap ();
1086 CORE_ADDR max_wp_length
, aligned_addr
;
1088 /* Can not set watchpoints for zero or negative lengths. */
1092 /* Need to be able to use the ptrace interface. */
1093 if (cap
== NULL
|| cap
->wp_count
== 0)
1096 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1097 range covered by a watchpoint. */
1098 max_wp_length
= (CORE_ADDR
)cap
->max_wp_length
;
1099 aligned_addr
= addr
& ~(max_wp_length
- 1);
1101 if (aligned_addr
+ max_wp_length
< addr
+ len
)
1104 /* The current ptrace interface can only handle watchpoints that are a
1106 if ((len
& (len
- 1)) != 0)
1109 /* All tests passed so we must be able to set a watchpoint. */
1113 /* Insert a Hardware breakpoint. */
1115 arm_linux_nat_target::insert_watchpoint (CORE_ADDR addr
, int len
,
1116 enum target_hw_bp_type rw
,
1117 struct expression
*cond
)
1119 struct arm_linux_hw_breakpoint p
;
1121 if (arm_linux_get_hw_watchpoint_count () == 0)
1124 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1126 arm_linux_insert_hw_breakpoint1 (&p
, 1);
1131 /* Remove a hardware breakpoint. */
1133 arm_linux_nat_target::remove_watchpoint (CORE_ADDR addr
,
1134 int len
, enum target_hw_bp_type rw
,
1135 struct expression
*cond
)
1137 struct arm_linux_hw_breakpoint p
;
1139 if (arm_linux_get_hw_watchpoint_count () == 0)
1142 arm_linux_hw_watchpoint_initialize (addr
, len
, rw
, &p
);
1144 arm_linux_remove_hw_breakpoint1 (&p
, 1);
1149 /* What was the data address the target was stopped on accessing. */
1151 arm_linux_nat_target::stopped_data_address (CORE_ADDR
*addr_p
)
1156 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
1159 /* This must be a hardware breakpoint. */
1160 if (siginfo
.si_signo
!= SIGTRAP
1161 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1164 /* We must be able to set hardware watchpoints. */
1165 if (arm_linux_get_hw_watchpoint_count () == 0)
1168 slot
= siginfo
.si_errno
;
1170 /* If we are in a positive slot then we're looking at a breakpoint and not
1175 *addr_p
= (CORE_ADDR
) (uintptr_t) siginfo
.si_addr
;
1179 /* Has the target been stopped by hitting a watchpoint? */
1181 arm_linux_nat_target::stopped_by_watchpoint ()
1184 return stopped_data_address (&addr
);
1188 arm_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr
,
1192 return start
<= addr
&& start
+ length
- 1 >= addr
;
1195 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1196 in the parent thread to the child thread. */
1198 arm_linux_nat_target::low_new_thread (struct lwp_info
*lp
)
1201 struct arch_lwp_info
*info
= XCNEW (struct arch_lwp_info
);
1203 /* Mark that all the hardware breakpoint/watchpoint register pairs
1204 for this thread need to be initialized. */
1206 for (i
= 0; i
< MAX_BPTS
; i
++)
1208 info
->bpts_changed
[i
] = 1;
1209 info
->wpts_changed
[i
] = 1;
1212 lp
->arch_private
= info
;
1215 /* Function to call when a thread is being deleted. */
1218 arm_linux_nat_target::low_delete_thread (struct arch_lwp_info
*arch_lwp
)
1223 /* Called when resuming a thread.
1224 The hardware debug registers are updated when there is any change. */
1227 arm_linux_nat_target::low_prepare_to_resume (struct lwp_info
*lwp
)
1230 struct arm_linux_hw_breakpoint
*bpts
, *wpts
;
1231 struct arch_lwp_info
*arm_lwp_info
= lwp
->arch_private
;
1233 pid
= lwp
->ptid
.lwp ();
1234 bpts
= arm_linux_get_debug_reg_state (lwp
->ptid
.pid ())->bpts
;
1235 wpts
= arm_linux_get_debug_reg_state (lwp
->ptid
.pid ())->wpts
;
1237 /* NULL means this is the main thread still going through the shell,
1238 or, no watchpoint has been set yet. In that case, there's
1240 if (arm_lwp_info
== NULL
)
1243 for (i
= 0; i
< arm_linux_get_hw_breakpoint_count (); i
++)
1244 if (arm_lwp_info
->bpts_changed
[i
])
1247 if (arm_hwbp_control_is_enabled (bpts
[i
].control
))
1248 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1249 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 1), &bpts
[i
].address
) < 0)
1250 perror_with_name (_("Unexpected error setting breakpoint"));
1252 if (bpts
[i
].control
!= 0)
1253 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1254 (PTRACE_TYPE_ARG3
) ((i
<< 1) + 2), &bpts
[i
].control
) < 0)
1255 perror_with_name (_("Unexpected error setting breakpoint"));
1257 arm_lwp_info
->bpts_changed
[i
] = 0;
1260 for (i
= 0; i
< arm_linux_get_hw_watchpoint_count (); i
++)
1261 if (arm_lwp_info
->wpts_changed
[i
])
1264 if (arm_hwbp_control_is_enabled (wpts
[i
].control
))
1265 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1266 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 1), &wpts
[i
].address
) < 0)
1267 perror_with_name (_("Unexpected error setting watchpoint"));
1269 if (wpts
[i
].control
!= 0)
1270 if (ptrace (PTRACE_SETHBPREGS
, pid
,
1271 (PTRACE_TYPE_ARG3
) -((i
<< 1) + 2), &wpts
[i
].control
) < 0)
1272 perror_with_name (_("Unexpected error setting watchpoint"));
1274 arm_lwp_info
->wpts_changed
[i
] = 0;
1278 /* linux_nat_new_fork hook. */
1281 arm_linux_nat_target::low_new_fork (struct lwp_info
*parent
, pid_t child_pid
)
1284 struct arm_linux_debug_reg_state
*parent_state
;
1285 struct arm_linux_debug_reg_state
*child_state
;
1287 /* NULL means no watchpoint has ever been set in the parent. In
1288 that case, there's nothing to do. */
1289 if (parent
->arch_private
== NULL
)
1292 /* GDB core assumes the child inherits the watchpoints/hw
1293 breakpoints of the parent, and will remove them all from the
1294 forked off process. Copy the debug registers mirrors into the
1295 new process so that all breakpoints and watchpoints can be
1296 removed together. */
1298 parent_pid
= parent
->ptid
.pid ();
1299 parent_state
= arm_linux_get_debug_reg_state (parent_pid
);
1300 child_state
= arm_linux_get_debug_reg_state (child_pid
);
1301 *child_state
= *parent_state
;
1304 void _initialize_arm_linux_nat ();
1306 _initialize_arm_linux_nat ()
1308 /* Register the target. */
1309 linux_target
= &the_arm_linux_nat_target
;
1310 add_inf_child_target (&the_arm_linux_nat_target
);