Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/binutils-gdb.git] / gdb / amd64-bsd-nat.c
1 /* Native-dependent code for AMD64 BSD's.
2
3 Copyright (C) 2003-2019 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
22 /* Local non-gdb includes. */
23 #include "inferior.h"
24 #include "regcache.h"
25 #include "target.h"
26
27 /* We include <signal.h> to make sure `struct fxsave64' is defined on
28 NetBSD, since NetBSD's <machine/reg.h> needs it. */
29 #include <signal.h>
30 #include <sys/types.h>
31 #include <sys/ptrace.h>
32 #include <machine/reg.h>
33
34 #include "amd64-tdep.h"
35 #include "amd64-nat.h"
36 #include "x86-bsd-nat.h"
37 #include "inf-ptrace.h"
38 \f
39
40 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
41 for all registers (including the floating-point registers). */
42
43 void
44 amd64bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
45 {
46 struct gdbarch *gdbarch = regcache->arch ();
47 pid_t pid = get_ptrace_pid (regcache->ptid ());
48 #if defined(PT_GETFSBASE) || defined(PT_GETGSBASE)
49 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
50 #endif
51
52 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
53 {
54 struct reg regs;
55
56 if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
57 perror_with_name (_("Couldn't get registers"));
58
59 amd64_supply_native_gregset (regcache, &regs, -1);
60 if (regnum != -1)
61 return;
62 }
63
64 #ifdef PT_GETFSBASE
65 if (regnum == -1 || regnum == tdep->fsbase_regnum)
66 {
67 register_t base;
68
69 if (ptrace (PT_GETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
70 perror_with_name (_("Couldn't get segment register fs_base"));
71
72 regcache->raw_supply (tdep->fsbase_regnum, &base);
73 if (regnum != -1)
74 return;
75 }
76 #endif
77 #ifdef PT_GETGSBASE
78 if (regnum == -1 || regnum == tdep->fsbase_regnum + 1)
79 {
80 register_t base;
81
82 if (ptrace (PT_GETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
83 perror_with_name (_("Couldn't get segment register gs_base"));
84
85 regcache->raw_supply (tdep->fsbase_regnum + 1, &base);
86 if (regnum != -1)
87 return;
88 }
89 #endif
90
91 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
92 {
93 struct fpreg fpregs;
94 #ifdef PT_GETXSTATE_INFO
95 void *xstateregs;
96
97 if (x86bsd_xsave_len != 0)
98 {
99 xstateregs = alloca (x86bsd_xsave_len);
100 if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0)
101 == -1)
102 perror_with_name (_("Couldn't get extended state status"));
103
104 amd64_supply_xsave (regcache, -1, xstateregs);
105 return;
106 }
107 #endif
108
109 if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
110 perror_with_name (_("Couldn't get floating point status"));
111
112 amd64_supply_fxsave (regcache, -1, &fpregs);
113 }
114 }
115
116 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
117 this for all registers (including the floating-point registers). */
118
119 void
120 amd64bsd_store_inferior_registers (struct regcache *regcache, int regnum)
121 {
122 struct gdbarch *gdbarch = regcache->arch ();
123 pid_t pid = get_ptrace_pid (regcache->ptid ());
124 #if defined(PT_SETFSBASE) || defined(PT_SETGSBASE)
125 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
126 #endif
127
128 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
129 {
130 struct reg regs;
131
132 if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
133 perror_with_name (_("Couldn't get registers"));
134
135 amd64_collect_native_gregset (regcache, &regs, regnum);
136
137 if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
138 perror_with_name (_("Couldn't write registers"));
139
140 if (regnum != -1)
141 return;
142 }
143
144 #ifdef PT_SETFSBASE
145 if (regnum == -1 || regnum == tdep->fsbase_regnum)
146 {
147 register_t base;
148
149 /* Clear the full base value to support 32-bit targets. */
150 base = 0;
151 regcache->raw_collect (tdep->fsbase_regnum, &base);
152
153 if (ptrace (PT_SETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
154 perror_with_name (_("Couldn't write segment register fs_base"));
155 if (regnum != -1)
156 return;
157 }
158 #endif
159 #ifdef PT_SETGSBASE
160 if (regnum == -1 || regnum == tdep->fsbase_regnum + 1)
161 {
162 register_t base;
163
164 /* Clear the full base value to support 32-bit targets. */
165 base = 0;
166 regcache->raw_collect (tdep->fsbase_regnum + 1, &base);
167
168 if (ptrace (PT_SETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
169 perror_with_name (_("Couldn't write segment register gs_base"));
170 if (regnum != -1)
171 return;
172 }
173 #endif
174
175 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
176 {
177 struct fpreg fpregs;
178 #ifdef PT_GETXSTATE_INFO
179 void *xstateregs;
180
181 if (x86bsd_xsave_len != 0)
182 {
183 xstateregs = alloca (x86bsd_xsave_len);
184 if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0)
185 == -1)
186 perror_with_name (_("Couldn't get extended state status"));
187
188 amd64_collect_xsave (regcache, regnum, xstateregs, 0);
189
190 if (ptrace (PT_SETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs,
191 x86bsd_xsave_len) == -1)
192 perror_with_name (_("Couldn't write extended state status"));
193 return;
194 }
195 #endif
196
197 if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
198 perror_with_name (_("Couldn't get floating point status"));
199
200 amd64_collect_fxsave (regcache, regnum, &fpregs);
201
202 if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
203 perror_with_name (_("Couldn't write floating point status"));
204 }
205 }
This page took 0.035344 seconds and 5 git commands to generate.