1 /* Native-dependent code for X86 BSD's.
3 Copyright (C) 2003-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdbthread.h"
24 /* We include <signal.h> to make sure `struct fxsave64' is defined on
25 NetBSD, since NetBSD's <machine/reg.h> needs it. */
27 #include <sys/types.h>
28 #include <sys/ptrace.h>
29 #include <machine/reg.h>
32 #include "x86-bsd-nat.h"
33 #include "inf-ptrace.h"
36 #ifdef PT_GETXSTATE_INFO
37 size_t x86bsd_xsave_len;
40 /* Support for debug registers. */
42 #ifdef HAVE_PT_GETDBREGS
44 /* Helper macro to access debug register X. FreeBSD/amd64 and modern
45 versions of FreeBSD/i386 provide this macro in system headers. Define
46 a local version for systems that do not provide it. */
49 #define DBREG_DRX(d, x) ((d)->dr[x])
51 #define DBREG_DRX(d, x) ((&d->dr0)[x])
56 x86bsd_dr_get (ptid_t ptid, int regnum)
60 if (ptrace (PT_GETDBREGS, get_ptrace_pid (inferior_ptid),
61 (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
62 perror_with_name (_("Couldn't read debug registers"));
64 return DBREG_DRX ((&dbregs), regnum);
68 x86bsd_dr_set (int regnum, unsigned long value)
70 struct thread_info *thread;
73 if (ptrace (PT_GETDBREGS, get_ptrace_pid (inferior_ptid),
74 (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
75 perror_with_name (_("Couldn't get debug registers"));
77 /* For some mysterious reason, some of the reserved bits in the
78 debug control register get set. Mask these off, otherwise the
79 ptrace call below will fail. */
80 DBREG_DRX ((&dbregs), 7) &= ~(0xffffffff0000fc00);
82 DBREG_DRX ((&dbregs), regnum) = value;
84 ALL_NON_EXITED_THREADS (thread)
85 if (thread->inf == current_inferior ())
87 if (ptrace (PT_SETDBREGS, get_ptrace_pid (thread->ptid),
88 (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
89 perror_with_name (_("Couldn't write debug registers"));
94 x86bsd_dr_set_control (unsigned long control)
96 x86bsd_dr_set (7, control);
100 x86bsd_dr_set_addr (int regnum, CORE_ADDR addr)
102 gdb_assert (regnum >= 0 && regnum <= 4);
104 x86bsd_dr_set (regnum, addr);
108 x86bsd_dr_get_addr (int regnum)
110 return x86bsd_dr_get (inferior_ptid, regnum);
114 x86bsd_dr_get_status (void)
116 return x86bsd_dr_get (inferior_ptid, 6);
120 x86bsd_dr_get_control (void)
122 return x86bsd_dr_get (inferior_ptid, 7);
125 #endif /* PT_GETDBREGS */
128 _initialize_x86_bsd_nat ()
130 #ifdef HAVE_PT_GETDBREGS
131 x86_dr_low.set_control = x86bsd_dr_set_control;
132 x86_dr_low.set_addr = x86bsd_dr_set_addr;
133 x86_dr_low.get_addr = x86bsd_dr_get_addr;
134 x86_dr_low.get_status = x86bsd_dr_get_status;
135 x86_dr_low.get_control = x86bsd_dr_get_control;
136 x86_set_debug_register_length (sizeof (void *));
137 #endif /* HAVE_PT_GETDBREGS */