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/>. */
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
)
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 ());
46 pid_t pid
= get_ptrace_pid (ptid
);
47 return ptrace (request
, pid
, addr
, data
);
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
[] =
73 REG_OFFSET (r_eflags
),
78 #ifdef HAVE_STRUCT_REG_R_FS
83 #ifdef HAVE_STRUCT_REG_R_GS
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. */
102 i386bsd_supply_gregset (struct regcache
*regcache
, const void *gregs
)
104 const char *regs
= (const char *) gregs
;
107 for (regnum
= 0; regnum
< ARRAY_SIZE (i386bsd_r_reg_offset
); regnum
++)
109 int offset
= i386bsd_r_reg_offset
[regnum
];
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
121 i386bsd_collect_gregset (const struct regcache
*regcache
,
122 void *gregs
, int regnum
)
124 char *regs
= (char *) gregs
;
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
];
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). */
143 i386bsd_fetch_inferior_registers (struct regcache
*regcache
, int regnum
)
145 ptid_t ptid
= regcache
->ptid ();
147 if (regnum
== -1 || GETREGS_SUPPLIES (regnum
))
151 if (gdb_ptrace (PT_GETREGS
, ptid
, (PTRACE_TYPE_ARG3
) ®s
, 0) == -1)
152 perror_with_name (_("Couldn't get registers"));
154 i386bsd_supply_gregset (regcache
, ®s
);
159 if (regnum
== -1 || regnum
>= I386_ST0_REGNUM
)
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
);
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). */
187 i386bsd_store_inferior_registers (struct regcache
*regcache
, int regnum
)
189 ptid_t ptid
= regcache
->ptid ();
191 if (regnum
== -1 || GETREGS_SUPPLIES (regnum
))
195 if (gdb_ptrace (PT_GETREGS
, ptid
, (PTRACE_TYPE_ARG3
) ®s
, 0) == -1)
196 perror_with_name (_("Couldn't get registers"));
198 i386bsd_collect_gregset (regcache
, ®s
, regnum
);
200 if (gdb_ptrace (PT_SETREGS
, ptid
, (PTRACE_TYPE_ARG3
) ®s
, 0) == -1)
201 perror_with_name (_("Couldn't write registers"));
207 if (regnum
== -1 || regnum
>= I386_ST0_REGNUM
)
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"));
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 ();
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
250 #if defined (OpenBSD)
251 #define SC_REG_OFFSET i386obsd_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
)
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
)
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
)
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 */