Re: nios2: Remove binutils support for Nios II target
[binutils-gdb.git] / gdb / i386-bsd-nat.c
blobe1db6c74f9c464a9346c69d5deddd3e3ab300b4d
1 /* Native-dependent code for modern i386 BSD's.
3 Copyright (C) 2000-2024 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/>. */
20 #include "inferior.h"
21 #include "regcache.h"
23 #include <signal.h>
24 #include <sys/types.h>
25 #include <sys/ptrace.h>
26 #include <machine/reg.h>
27 #include <machine/frame.h>
29 #include "i386-tdep.h"
30 #include "i387-tdep.h"
31 #include "x86-bsd-nat.h"
32 #include "i386-bsd-nat.h"
33 #include "inf-ptrace.h"
36 static PTRACE_TYPE_RET
37 gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
38 PTRACE_TYPE_ARG4 data)
40 #ifdef __NetBSD__
41 gdb_assert (data == 0);
42 /* Support for NetBSD threads: unlike other ptrace implementations in this
43 file, NetBSD requires that we pass both the pid and lwp. */
44 return ptrace (request, ptid.pid (), addr, ptid.lwp ());
45 #else
46 pid_t pid = get_ptrace_pid (ptid);
47 return ptrace (request, pid, addr, data);
48 #endif
51 /* In older BSD versions we cannot get at some of the segment
52 registers. FreeBSD for example didn't support the %fs and %gs
53 registers until the 3.0 release. We have autoconf checks for their
54 presence, and deal gracefully with their absence. */
56 /* Offset in `struct reg' where MEMBER is stored. */
57 #define REG_OFFSET(member) offsetof (struct reg, member)
59 /* At i386bsd_reg_offset[REGNUM] you'll find the offset in `struct
60 reg' where the GDB register REGNUM is stored. Unsupported
61 registers are marked with `-1'. */
62 static int i386bsd_r_reg_offset[] =
64 REG_OFFSET (r_eax),
65 REG_OFFSET (r_ecx),
66 REG_OFFSET (r_edx),
67 REG_OFFSET (r_ebx),
68 REG_OFFSET (r_esp),
69 REG_OFFSET (r_ebp),
70 REG_OFFSET (r_esi),
71 REG_OFFSET (r_edi),
72 REG_OFFSET (r_eip),
73 REG_OFFSET (r_eflags),
74 REG_OFFSET (r_cs),
75 REG_OFFSET (r_ss),
76 REG_OFFSET (r_ds),
77 REG_OFFSET (r_es),
78 #ifdef HAVE_STRUCT_REG_R_FS
79 REG_OFFSET (r_fs),
80 #else
81 -1,
82 #endif
83 #ifdef HAVE_STRUCT_REG_R_GS
84 REG_OFFSET (r_gs)
85 #else
87 #endif
90 /* Macro to determine if a register is fetched with PT_GETREGS. */
91 #define GETREGS_SUPPLIES(regnum) \
92 ((0 <= (regnum) && (regnum) <= 15))
94 /* Set to 1 if the kernel supports PT_GETXMMREGS. Initialized to -1
95 so that we try PT_GETXMMREGS the first time around. */
96 static int have_ptrace_xmmregs = -1;
99 /* Supply the general-purpose registers in GREGS, to REGCACHE. */
101 static void
102 i386bsd_supply_gregset (struct regcache *regcache, const void *gregs)
104 const char *regs = (const char *) gregs;
105 int regnum;
107 for (regnum = 0; regnum < ARRAY_SIZE (i386bsd_r_reg_offset); regnum++)
109 int offset = i386bsd_r_reg_offset[regnum];
111 if (offset != -1)
112 regcache->raw_supply (regnum, regs + offset);
116 /* Collect register REGNUM from REGCACHE and store its contents in
117 GREGS. If REGNUM is -1, collect and store all appropriate
118 registers. */
120 static void
121 i386bsd_collect_gregset (const struct regcache *regcache,
122 void *gregs, int regnum)
124 char *regs = (char *) gregs;
125 int i;
127 for (i = 0; i < ARRAY_SIZE (i386bsd_r_reg_offset); i++)
129 if (regnum == -1 || regnum == i)
131 int offset = i386bsd_r_reg_offset[i];
133 if (offset != -1)
134 regcache->raw_collect (i, regs + offset);
139 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
140 for all registers (including the floating point registers). */
142 void
143 i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
145 ptid_t ptid = regcache->ptid ();
147 if (regnum == -1 || GETREGS_SUPPLIES (regnum))
149 struct reg regs;
151 if (gdb_ptrace (PT_GETREGS, ptid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
152 perror_with_name (_("Couldn't get registers"));
154 i386bsd_supply_gregset (regcache, &regs);
155 if (regnum != -1)
156 return;
159 if (regnum == -1 || regnum >= I386_ST0_REGNUM)
161 struct fpreg fpregs;
162 char xmmregs[512];
164 if (have_ptrace_xmmregs != 0
165 && gdb_ptrace(PT_GETXMMREGS, ptid,
166 (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
168 have_ptrace_xmmregs = 1;
169 i387_supply_fxsave (regcache, -1, xmmregs);
171 else
173 have_ptrace_xmmregs = 0;
174 if (gdb_ptrace (PT_GETFPREGS, ptid,
175 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
176 perror_with_name (_("Couldn't get floating point status"));
178 i387_supply_fsave (regcache, -1, &fpregs);
183 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
184 this for all registers (including the floating point registers). */
186 void
187 i386bsd_store_inferior_registers (struct regcache *regcache, int regnum)
189 ptid_t ptid = regcache->ptid ();
191 if (regnum == -1 || GETREGS_SUPPLIES (regnum))
193 struct reg regs;
195 if (gdb_ptrace (PT_GETREGS, ptid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
196 perror_with_name (_("Couldn't get registers"));
198 i386bsd_collect_gregset (regcache, &regs, regnum);
200 if (gdb_ptrace (PT_SETREGS, ptid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
201 perror_with_name (_("Couldn't write registers"));
203 if (regnum != -1)
204 return;
207 if (regnum == -1 || regnum >= I386_ST0_REGNUM)
209 struct fpreg fpregs;
210 char xmmregs[512];
212 if (have_ptrace_xmmregs != 0
213 && gdb_ptrace(PT_GETXMMREGS, ptid,
214 (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
216 have_ptrace_xmmregs = 1;
218 i387_collect_fxsave (regcache, regnum, xmmregs);
220 if (gdb_ptrace (PT_SETXMMREGS, ptid,
221 (PTRACE_TYPE_ARG3) xmmregs, 0) == -1)
222 perror_with_name (_("Couldn't write XMM registers"));
224 else
226 have_ptrace_xmmregs = 0;
227 if (gdb_ptrace (PT_GETFPREGS, ptid,
228 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
229 perror_with_name (_("Couldn't get floating point status"));
231 i387_collect_fsave (regcache, regnum, &fpregs);
233 if (gdb_ptrace (PT_SETFPREGS, ptid,
234 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
235 perror_with_name (_("Couldn't write floating point status"));
240 void _initialize_i386bsd_nat ();
241 void
242 _initialize_i386bsd_nat ()
244 /* To support the recognition of signal handlers, i386-bsd-tdep.c
245 hardcodes some constants. Inclusion of this file means that we
246 are compiling a native debugger, which means that we can use the
247 system header files and sysctl(3) to get at the relevant
248 information. */
250 #if defined (OpenBSD)
251 #define SC_REG_OFFSET i386obsd_sc_reg_offset
252 #endif
254 #ifdef SC_REG_OFFSET
256 /* We only check the program counter, stack pointer and frame
257 pointer since these members of `struct sigcontext' are essential
258 for providing backtraces. More checks could be added, but would
259 involve adding configure checks for the appropriate structure
260 members, since older BSD's don't provide all of them. */
262 #define SC_PC_OFFSET SC_REG_OFFSET[I386_EIP_REGNUM]
263 #define SC_SP_OFFSET SC_REG_OFFSET[I386_ESP_REGNUM]
264 #define SC_FP_OFFSET SC_REG_OFFSET[I386_EBP_REGNUM]
266 /* Override the default value for the offset of the program counter
267 in the sigcontext structure. */
268 int offset = offsetof (struct sigcontext, sc_pc);
270 if (SC_PC_OFFSET != offset)
272 warning (_("\
273 offsetof (struct sigcontext, sc_pc) yields %d instead of %d.\n\
274 Please report this to <bug-gdb@gnu.org>."),
275 offset, SC_PC_OFFSET);
278 SC_PC_OFFSET = offset;
280 /* Likewise for the stack pointer. */
281 offset = offsetof (struct sigcontext, sc_sp);
283 if (SC_SP_OFFSET != offset)
285 warning (_("\
286 offsetof (struct sigcontext, sc_sp) yields %d instead of %d.\n\
287 Please report this to <bug-gdb@gnu.org>."),
288 offset, SC_SP_OFFSET);
291 SC_SP_OFFSET = offset;
293 /* And the frame pointer. */
294 offset = offsetof (struct sigcontext, sc_fp);
296 if (SC_FP_OFFSET != offset)
298 warning (_("\
299 offsetof (struct sigcontext, sc_fp) yields %d instead of %d.\n\
300 Please report this to <bug-gdb@gnu.org>."),
301 offset, SC_FP_OFFSET);
304 SC_FP_OFFSET = offset;
306 #endif /* SC_REG_OFFSET */