GDB (xrefs)
Loading...
Searching...
No Matches
i386-fbsd-nat.c
Go to the documentation of this file.
1/* Native-dependent code for FreeBSD/i386.
2
3 Copyright (C) 2001-2023 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 "regcache.h"
23#include "target.h"
24
25#include <sys/types.h>
26#include <sys/ptrace.h>
27#include <sys/sysctl.h>
28#include <sys/user.h>
29
30#include "i386-tdep.h"
31#include "i386-fbsd-tdep.h"
32#include "i387-tdep.h"
33#include "x86-nat.h"
34#include "gdbsupport/x86-xstate.h"
35#include "x86-fbsd-nat.h"
36
38{
39public:
40 void fetch_registers (struct regcache *, int) override;
41 void store_registers (struct regcache *, int) override;
42
43 const struct target_desc *read_description () override;
44
45 void resume (ptid_t, int, enum gdb_signal) override;
46};
47
49
50#ifdef PT_GETXSTATE_INFO
51static size_t xsave_len;
52#endif
53
55
56/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
57 for all registers. */
58
59void
61{
62 struct gdbarch *gdbarch = regcache->arch ();
63 pid_t pid = get_ptrace_pid (regcache->ptid ());
64
65 if (fetch_register_set<struct reg> (regcache, regnum, PT_GETREGS,
67 {
68 if (regnum != -1)
69 return;
70 }
71
72#ifdef PT_GETFSBASE
73 if (regnum == -1 || regnum == I386_FSBASE_REGNUM)
74 {
75 register_t base;
76
77 if (ptrace (PT_GETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
78 perror_with_name (_("Couldn't get segment register fs_base"));
79
81 if (regnum != -1)
82 return;
83 }
84#endif
85#ifdef PT_GETGSBASE
86 if (regnum == -1 || regnum == I386_GSBASE_REGNUM)
87 {
88 register_t base;
89
90 if (ptrace (PT_GETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
91 perror_with_name (_("Couldn't get segment register gs_base"));
92
94 if (regnum != -1)
95 return;
96 }
97#endif
98
99 /* There is no i386_fxsave_supplies or i386_xsave_supplies.
100 Instead, the earlier register sets return early if the request
101 was for a specific register that was already satisified to avoid
102 fetching the FPU/XSAVE state unnecessarily. */
103
104#ifdef PT_GETXSTATE_INFO
105 if (xsave_len != 0)
106 {
107 void *xstateregs = alloca (xsave_len);
108
109 if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
110 perror_with_name (_("Couldn't get extended state status"));
111
112 i387_supply_xsave (regcache, regnum, xstateregs);
113 return;
114 }
115#endif
116 if (have_ptrace_xmmregs != 0)
117 {
118 char xmmregs[I387_SIZEOF_FXSAVE];
119
120 if (ptrace(PT_GETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs, 0) == -1)
121 perror_with_name (_("Couldn't get XMM registers"));
122
124 return;
125 }
126
127 struct fpreg fpregs;
128
129 if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
130 perror_with_name (_("Couldn't get floating point status"));
131
133}
134
135/* Store register REGNUM back into the inferior. If REGNUM is -1, do
136 this for all registers. */
137
138void
140{
141 struct gdbarch *gdbarch = regcache->arch ();
142 pid_t pid = get_ptrace_pid (regcache->ptid ());
143
144 if (store_register_set<struct reg> (regcache, regnum, PT_GETREGS, PT_SETREGS,
146 {
147 if (regnum != -1)
148 return;
149 }
150
151#ifdef PT_SETFSBASE
152 if (regnum == -1 || regnum == I386_FSBASE_REGNUM)
153 {
154 register_t base;
155
157
158 if (ptrace (PT_SETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
159 perror_with_name (_("Couldn't write segment register fs_base"));
160 if (regnum != -1)
161 return;
162 }
163#endif
164#ifdef PT_SETGSBASE
165 if (regnum == -1 || regnum == I386_GSBASE_REGNUM)
166 {
167 register_t base;
168
170
171 if (ptrace (PT_SETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
172 perror_with_name (_("Couldn't write segment register gs_base"));
173 if (regnum != -1)
174 return;
175 }
176#endif
177
178 /* There is no i386_fxsave_supplies or i386_xsave_supplies.
179 Instead, the earlier register sets return early if the request
180 was for a specific register that was already satisified to avoid
181 fetching the FPU/XSAVE state unnecessarily. */
182
183#ifdef PT_GETXSTATE_INFO
184 if (xsave_len != 0)
185 {
186 void *xstateregs = alloca (xsave_len);
187
188 if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
189 perror_with_name (_("Couldn't get extended state status"));
190
191 i387_collect_xsave (regcache, regnum, xstateregs, 0);
192
193 if (ptrace (PT_SETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, xsave_len)
194 == -1)
195 perror_with_name (_("Couldn't write extended state status"));
196 return;
197 }
198#endif
199 if (have_ptrace_xmmregs != 0)
200 {
201 char xmmregs[I387_SIZEOF_FXSAVE];
202
203 if (ptrace(PT_GETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs, 0) == -1)
204 perror_with_name (_("Couldn't get XMM registers"));
205
207
208 if (ptrace (PT_SETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs, 0) == -1)
209 perror_with_name (_("Couldn't write XMM registers"));
210 return;
211 }
212
213 struct fpreg fpregs;
214
215 if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
216 perror_with_name (_("Couldn't get floating point status"));
217
219
220 if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
221 perror_with_name (_("Couldn't write floating point status"));
222}
223
224/* Resume execution of the inferior process. If STEP is nonzero,
225 single-step it. If SIGNAL is nonzero, give it that signal. */
226
227void
228i386_fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
229{
230 pid_t pid = ptid.pid ();
231 int request = PT_STEP;
232
233 if (pid == -1)
234 /* Resume all threads. This only gets used in the non-threaded
235 case, where "resume all threads" and "resume inferior_ptid" are
236 the same. */
237 pid = inferior_ptid.pid ();
238
239 if (!step)
240 {
242 ULONGEST eflags;
243
244 /* Workaround for a bug in FreeBSD. Make sure that the trace
245 flag is off when doing a continue. There is a code path
246 through the kernel which leaves the flag set when it should
247 have been cleared. If a process has a signal pending (such
248 as SIGALRM) and we do a PT_STEP, the process never really has
249 a chance to run because the kernel needs to notify the
250 debugger that a signal is being sent. Therefore, the process
251 never goes through the kernel's trap() function which would
252 normally clear it. */
253
255 &eflags);
256 if (eflags & 0x0100)
258 eflags & ~0x0100);
259
260 request = PT_CONTINUE;
261 }
262
263 /* An addres of (caddr_t) 1 tells ptrace to continue from where it
264 was. (If GDB wanted it to start some other way, we have already
265 written a new PC value to the child.) */
266 if (ptrace (request, pid, (caddr_t) 1,
267 gdb_signal_to_host (signal)) == -1)
268 perror_with_name (("ptrace"));
269}
270
271
272/* Support for debugging kernel virtual memory images. */
273
274#include <machine/pcb.h>
275
276#include "bsd-kvm.h"
277
278static int
279i386fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
280{
281 /* The following is true for FreeBSD 4.7:
282
283 The pcb contains %eip, %ebx, %esp, %ebp, %esi, %edi and %gs.
284 This accounts for all callee-saved registers specified by the
285 psABI and then some. Here %esp contains the stack pointer at the
286 point just after the call to cpu_switch(). From this information
287 we reconstruct the register state as it would look when we just
288 returned from cpu_switch(). */
289
290 /* The stack pointer shouldn't be zero. */
291 if (pcb->pcb_esp == 0)
292 return 0;
293
294 pcb->pcb_esp += 4;
295 regcache->raw_supply (I386_EDI_REGNUM, &pcb->pcb_edi);
296 regcache->raw_supply (I386_ESI_REGNUM, &pcb->pcb_esi);
297 regcache->raw_supply (I386_EBP_REGNUM, &pcb->pcb_ebp);
298 regcache->raw_supply (I386_ESP_REGNUM, &pcb->pcb_esp);
299 regcache->raw_supply (I386_EBX_REGNUM, &pcb->pcb_ebx);
300 regcache->raw_supply (I386_EIP_REGNUM, &pcb->pcb_eip);
301 regcache->raw_supply (I386_GS_REGNUM, &pcb->pcb_gs);
302
303 return 1;
304}
305
306
307/* Implement the read_description method. */
308
309const struct target_desc *
311{
312#ifdef PT_GETXSTATE_INFO
313 static int xsave_probed;
314 static uint64_t xcr0;
315#endif
316 static int xmm_probed;
317
318#ifdef PT_GETXSTATE_INFO
319 if (!xsave_probed)
320 {
321 struct ptrace_xstate_info info;
322
323 if (ptrace (PT_GETXSTATE_INFO, inferior_ptid.pid (),
324 (PTRACE_TYPE_ARG3) &info, sizeof (info)) == 0)
325 {
326 xsave_len = info.xsave_len;
327 xcr0 = info.xsave_mask;
328 }
329 xsave_probed = 1;
330 }
331
332 if (xsave_len != 0)
333 return i386_target_description (xcr0, true);
334#endif
335
336 if (!xmm_probed)
337 {
338 char xmmregs[I387_SIZEOF_FXSAVE];
339
340 if (ptrace (PT_GETXMMREGS, inferior_ptid.pid (),
341 (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
343 xmm_probed = 1;
344 }
345
347 return i386_target_description (X86_XSTATE_SSE_MASK, true);
348
349 return i386_target_description (X86_XSTATE_X87_MASK, true);
350}
351
353void
355{
357
358 /* Support debugging kernel virtual memory images. */
360}
int regnum
void bsd_kvm_add_target(int(*supply_pcb)(struct regcache *, struct pcb *))
Definition bsd-kvm.c:380
void store_registers(struct regcache *, int) override
void fetch_registers(struct regcache *, int) override
void resume(ptid_t, int, enum gdb_signal) override
const struct target_desc * read_description() override
const target_info & info() const override
Definition inf-child.c:49
gdbarch * arch() const
Definition regcache.c:230
void raw_collect(int regnum, void *buf) const override
Definition regcache.c:1118
void raw_supply(int regnum, const void *buf) override
Definition regcache.c:1053
ptid_t ptid() const
Definition regcache.h:407
#define PT_STEP
Definition gdb_ptrace.h:91
#define PT_CONTINUE
Definition gdb_ptrace.h:79
#define ptrace(request, pid, addr, data)
Definition gdb_ptrace.h:141
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition gnu-nat.c:1792
static i386_fbsd_nat_target the_i386_fbsd_nat_target
static int i386fbsd_supply_pcb(struct regcache *regcache, struct pcb *pcb)
void _initialize_i386fbsd_nat()
static int have_ptrace_xmmregs
const struct regset i386_fbsd_gregset
const struct target_desc * i386_target_description(uint64_t xcr0, bool segments)
Definition i386-tdep.c:8809
@ I386_GSBASE_REGNUM
Definition i386-tdep.h:307
@ I386_EFLAGS_REGNUM
Definition i386-tdep.h:286
@ I386_ESI_REGNUM
Definition i386-tdep.h:283
@ I386_GS_REGNUM
Definition i386-tdep.h:292
@ I386_EIP_REGNUM
Definition i386-tdep.h:285
@ I386_EBP_REGNUM
Definition i386-tdep.h:282
@ I386_ESP_REGNUM
Definition i386-tdep.h:281
@ I386_EBX_REGNUM
Definition i386-tdep.h:280
@ I386_EDI_REGNUM
Definition i386-tdep.h:284
@ I386_FSBASE_REGNUM
Definition i386-tdep.h:306
void i387_supply_xsave(struct regcache *regcache, int regnum, const void *xsave)
Definition i387-tdep.c:924
void i387_supply_fxsave(struct regcache *regcache, int regnum, const void *fxsave)
Definition i387-tdep.c:589
void i387_collect_fxsave(const struct regcache *regcache, int regnum, void *fxsave)
Definition i387-tdep.c:673
void i387_collect_xsave(const struct regcache *regcache, int regnum, void *xsave, int gcore)
Definition i387-tdep.c:1347
void i387_supply_fsave(struct regcache *regcache, int regnum, const void *fsave)
Definition i387-tdep.c:440
void i387_collect_fsave(const struct regcache *regcache, int regnum, void *fsave)
Definition i387-tdep.c:495
#define I387_SIZEOF_FXSAVE
Definition i387-tdep.h:117
void add_inf_child_target(inf_child_target *target)
Definition inf-child.c:418
pid_t get_ptrace_pid(ptid_t ptid)
Definition inf-ptrace.c:238
ptid_t inferior_ptid
Definition infcmd.c:91
#define PTRACE_TYPE_ARG3
enum register_status regcache_cooked_read_unsigned(struct regcache *regcache, int regnum, ULONGEST *val)
Definition regcache.c:790
struct regcache * get_current_regcache(void)
Definition regcache.c:426
void regcache_cooked_write_unsigned(struct regcache *regcache, int regnum, ULONGEST val)
Definition regcache.c:819
void perror_with_name(const char *string)
Definition utils.c:643