Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / x86-bsd-nat.c
1 /* Native-dependent code for X86 BSD's.
2
3 Copyright (C) 2003-2020 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "gdbthread.h"
23
24 /* We include <signal.h> to make sure `struct fxsave64' is defined on
25 NetBSD, since NetBSD's <machine/reg.h> needs it. */
26 #include <signal.h>
27 #include <sys/types.h>
28 #include <sys/ptrace.h>
29 #include <machine/reg.h>
30
31 #include "x86-nat.h"
32 #include "x86-bsd-nat.h"
33 #include "inf-ptrace.h"
34 \f
35
36 #ifdef PT_GETXSTATE_INFO
37 size_t x86bsd_xsave_len;
38 #endif
39
40 /* Support for debug registers. */
41
42 #ifdef HAVE_PT_GETDBREGS
43
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. */
47 #ifndef DBREG_DRX
48 #ifdef __NetBSD__
49 #define DBREG_DRX(d, x) ((d)->dr[x])
50 #else
51 #define DBREG_DRX(d, x) ((&d->dr0)[x])
52 #endif
53 #endif
54
55 static unsigned long
56 x86bsd_dr_get (ptid_t ptid, int regnum)
57 {
58 struct dbreg dbregs;
59 #ifdef __NetBSD__
60 int lwp = inferior_ptid.lwp ();
61 #else
62 int lwp = 0;
63 #endif
64
65 if (ptrace (PT_GETDBREGS, get_ptrace_pid (inferior_ptid),
66 (PTRACE_TYPE_ARG3) &dbregs, lwp) == -1)
67 perror_with_name (_("Couldn't read debug registers"));
68
69 return DBREG_DRX ((&dbregs), regnum);
70 }
71
72 static void
73 x86bsd_dr_set (int regnum, unsigned long value)
74 {
75 struct dbreg dbregs;
76 #ifdef __NetBSD__
77 int lwp = inferior_ptid.lwp ();
78 #else
79 int lwp = 0;
80 #endif
81
82 if (ptrace (PT_GETDBREGS, get_ptrace_pid (inferior_ptid),
83 (PTRACE_TYPE_ARG3) &dbregs, lwp) == -1)
84 perror_with_name (_("Couldn't get debug registers"));
85
86 /* For some mysterious reason, some of the reserved bits in the
87 debug control register get set. Mask these off, otherwise the
88 ptrace call below will fail. */
89 DBREG_DRX ((&dbregs), 7) &= ~(0xffffffff0000fc00);
90
91 DBREG_DRX ((&dbregs), regnum) = value;
92
93 for (thread_info *thread : current_inferior ()->non_exited_threads ())
94 {
95 #ifdef __NetBSD__
96 lwp = thread->ptid.lwp ();
97 #endif
98
99 if (ptrace (PT_SETDBREGS, get_ptrace_pid (thread->ptid),
100 (PTRACE_TYPE_ARG3) &dbregs, lwp) == -1)
101 perror_with_name (_("Couldn't write debug registers"));
102 }
103 }
104
105 static void
106 x86bsd_dr_set_control (unsigned long control)
107 {
108 x86bsd_dr_set (7, control);
109 }
110
111 static void
112 x86bsd_dr_set_addr (int regnum, CORE_ADDR addr)
113 {
114 gdb_assert (regnum >= 0 && regnum <= 4);
115
116 x86bsd_dr_set (regnum, addr);
117 }
118
119 static CORE_ADDR
120 x86bsd_dr_get_addr (int regnum)
121 {
122 return x86bsd_dr_get (inferior_ptid, regnum);
123 }
124
125 static unsigned long
126 x86bsd_dr_get_status (void)
127 {
128 return x86bsd_dr_get (inferior_ptid, 6);
129 }
130
131 static unsigned long
132 x86bsd_dr_get_control (void)
133 {
134 return x86bsd_dr_get (inferior_ptid, 7);
135 }
136
137 #endif /* PT_GETDBREGS */
138
139 void _initialize_x86_bsd_nat ();
140 void
141 _initialize_x86_bsd_nat ()
142 {
143 #ifdef HAVE_PT_GETDBREGS
144 x86_dr_low.set_control = x86bsd_dr_set_control;
145 x86_dr_low.set_addr = x86bsd_dr_set_addr;
146 x86_dr_low.get_addr = x86bsd_dr_get_addr;
147 x86_dr_low.get_status = x86bsd_dr_get_status;
148 x86_dr_low.get_control = x86bsd_dr_get_control;
149 x86_set_debug_register_length (sizeof (void *));
150 #endif /* HAVE_PT_GETDBREGS */
151 }
This page took 0.035105 seconds and 4 git commands to generate.