Move stddef.h to common-defs.h
[deliverable/binutils-gdb.git] / gdb / i386bsd-nat.c
1 /* Native-dependent code for modern i386 BSD's.
2
3 Copyright (C) 2000-2014 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
24 #include "gdb_assert.h"
25 #include <signal.h>
26 #include <sys/types.h>
27 #include <sys/ptrace.h>
28 #include <machine/reg.h>
29 #include <machine/frame.h>
30
31 #include "i386-tdep.h"
32 #include "i387-tdep.h"
33 #include "i386bsd-nat.h"
34 #include "inf-ptrace.h"
35 \f
36
37 /* In older BSD versions we cannot get at some of the segment
38 registers. FreeBSD for example didn't support the %fs and %gs
39 registers until the 3.0 release. We have autoconf checks for their
40 presence, and deal gracefully with their absence. */
41
42 /* Offset in `struct reg' where MEMBER is stored. */
43 #define REG_OFFSET(member) offsetof (struct reg, member)
44
45 /* At i386bsd_reg_offset[REGNUM] you'll find the offset in `struct
46 reg' where the GDB register REGNUM is stored. Unsupported
47 registers are marked with `-1'. */
48 static int i386bsd_r_reg_offset[] =
49 {
50 REG_OFFSET (r_eax),
51 REG_OFFSET (r_ecx),
52 REG_OFFSET (r_edx),
53 REG_OFFSET (r_ebx),
54 REG_OFFSET (r_esp),
55 REG_OFFSET (r_ebp),
56 REG_OFFSET (r_esi),
57 REG_OFFSET (r_edi),
58 REG_OFFSET (r_eip),
59 REG_OFFSET (r_eflags),
60 REG_OFFSET (r_cs),
61 REG_OFFSET (r_ss),
62 REG_OFFSET (r_ds),
63 REG_OFFSET (r_es),
64 #ifdef HAVE_STRUCT_REG_R_FS
65 REG_OFFSET (r_fs),
66 #else
67 -1,
68 #endif
69 #ifdef HAVE_STRUCT_REG_R_GS
70 REG_OFFSET (r_gs)
71 #else
72 -1
73 #endif
74 };
75
76 /* Macro to determine if a register is fetched with PT_GETREGS. */
77 #define GETREGS_SUPPLIES(regnum) \
78 ((0 <= (regnum) && (regnum) <= 15))
79
80 #ifdef HAVE_PT_GETXMMREGS
81 /* Set to 1 if the kernel supports PT_GETXMMREGS. Initialized to -1
82 so that we try PT_GETXMMREGS the first time around. */
83 static int have_ptrace_xmmregs = -1;
84 #endif
85 \f
86
87 /* Supply the general-purpose registers in GREGS, to REGCACHE. */
88
89 static void
90 i386bsd_supply_gregset (struct regcache *regcache, const void *gregs)
91 {
92 const char *regs = gregs;
93 int regnum;
94
95 for (regnum = 0; regnum < ARRAY_SIZE (i386bsd_r_reg_offset); regnum++)
96 {
97 int offset = i386bsd_r_reg_offset[regnum];
98
99 if (offset != -1)
100 regcache_raw_supply (regcache, regnum, regs + offset);
101 }
102 }
103
104 /* Collect register REGNUM from REGCACHE and store its contents in
105 GREGS. If REGNUM is -1, collect and store all appropriate
106 registers. */
107
108 static void
109 i386bsd_collect_gregset (const struct regcache *regcache,
110 void *gregs, int regnum)
111 {
112 char *regs = gregs;
113 int i;
114
115 for (i = 0; i < ARRAY_SIZE (i386bsd_r_reg_offset); i++)
116 {
117 if (regnum == -1 || regnum == i)
118 {
119 int offset = i386bsd_r_reg_offset[i];
120
121 if (offset != -1)
122 regcache_raw_collect (regcache, i, regs + offset);
123 }
124 }
125 }
126
127 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
128 for all registers (including the floating point registers). */
129
130 static void
131 i386bsd_fetch_inferior_registers (struct target_ops *ops,
132 struct regcache *regcache, int regnum)
133 {
134 if (regnum == -1 || GETREGS_SUPPLIES (regnum))
135 {
136 struct reg regs;
137
138 if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
139 (PTRACE_TYPE_ARG3) &regs, 0) == -1)
140 perror_with_name (_("Couldn't get registers"));
141
142 i386bsd_supply_gregset (regcache, &regs);
143 if (regnum != -1)
144 return;
145 }
146
147 if (regnum == -1 || regnum >= I386_ST0_REGNUM)
148 {
149 struct fpreg fpregs;
150 #ifdef HAVE_PT_GETXMMREGS
151 char xmmregs[512];
152
153 if (have_ptrace_xmmregs != 0
154 && ptrace(PT_GETXMMREGS, ptid_get_pid (inferior_ptid),
155 (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
156 {
157 have_ptrace_xmmregs = 1;
158 i387_supply_fxsave (regcache, -1, xmmregs);
159 }
160 else
161 {
162 if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
163 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
164 perror_with_name (_("Couldn't get floating point status"));
165
166 i387_supply_fsave (regcache, -1, &fpregs);
167 }
168 #else
169 if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
170 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
171 perror_with_name (_("Couldn't get floating point status"));
172
173 i387_supply_fsave (regcache, -1, &fpregs);
174 #endif
175 }
176 }
177
178 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
179 this for all registers (including the floating point registers). */
180
181 static void
182 i386bsd_store_inferior_registers (struct target_ops *ops,
183 struct regcache *regcache, int regnum)
184 {
185 if (regnum == -1 || GETREGS_SUPPLIES (regnum))
186 {
187 struct reg regs;
188
189 if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
190 (PTRACE_TYPE_ARG3) &regs, 0) == -1)
191 perror_with_name (_("Couldn't get registers"));
192
193 i386bsd_collect_gregset (regcache, &regs, regnum);
194
195 if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
196 (PTRACE_TYPE_ARG3) &regs, 0) == -1)
197 perror_with_name (_("Couldn't write registers"));
198
199 if (regnum != -1)
200 return;
201 }
202
203 if (regnum == -1 || regnum >= I386_ST0_REGNUM)
204 {
205 struct fpreg fpregs;
206 #ifdef HAVE_PT_GETXMMREGS
207 char xmmregs[512];
208
209 if (have_ptrace_xmmregs != 0
210 && ptrace(PT_GETXMMREGS, ptid_get_pid (inferior_ptid),
211 (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
212 {
213 have_ptrace_xmmregs = 1;
214
215 i387_collect_fxsave (regcache, regnum, xmmregs);
216
217 if (ptrace (PT_SETXMMREGS, ptid_get_pid (inferior_ptid),
218 (PTRACE_TYPE_ARG3) xmmregs, 0) == -1)
219 perror_with_name (_("Couldn't write XMM registers"));
220 }
221 else
222 {
223 have_ptrace_xmmregs = 0;
224 #endif
225 if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
226 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
227 perror_with_name (_("Couldn't get floating point status"));
228
229 i387_collect_fsave (regcache, regnum, &fpregs);
230
231 if (ptrace (PT_SETFPREGS, ptid_get_pid (inferior_ptid),
232 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
233 perror_with_name (_("Couldn't write floating point status"));
234 #ifdef HAVE_PT_GETXMMREGS
235 }
236 #endif
237 }
238 }
239
240 /* Create a prototype *BSD/i386 target. The client can override it
241 with local methods. */
242
243 struct target_ops *
244 i386bsd_target (void)
245 {
246 struct target_ops *t;
247
248 t = inf_ptrace_target ();
249 t->to_fetch_registers = i386bsd_fetch_inferior_registers;
250 t->to_store_registers = i386bsd_store_inferior_registers;
251 return t;
252 }
253 \f
254
255 /* Support for debug registers. */
256
257 #ifdef HAVE_PT_GETDBREGS
258
259 /* Not all versions of FreeBSD/i386 that support the debug registers
260 have this macro. */
261 #ifndef DBREG_DRX
262 #define DBREG_DRX(d, x) ((&d->dr0)[x])
263 #endif
264
265 static unsigned long
266 i386bsd_dr_get (ptid_t ptid, int regnum)
267 {
268 struct dbreg dbregs;
269
270 if (ptrace (PT_GETDBREGS, ptid_get_pid (inferior_ptid),
271 (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
272 perror_with_name (_("Couldn't read debug registers"));
273
274 return DBREG_DRX ((&dbregs), regnum);
275 }
276
277 static void
278 i386bsd_dr_set (int regnum, unsigned int value)
279 {
280 struct dbreg dbregs;
281
282 if (ptrace (PT_GETDBREGS, ptid_get_pid (inferior_ptid),
283 (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
284 perror_with_name (_("Couldn't get debug registers"));
285
286 /* For some mysterious reason, some of the reserved bits in the
287 debug control register get set. Mask these off, otherwise the
288 ptrace call below will fail. */
289 DBREG_DRX ((&dbregs), 7) &= ~(0x0000fc00);
290
291 DBREG_DRX ((&dbregs), regnum) = value;
292
293 if (ptrace (PT_SETDBREGS, ptid_get_pid (inferior_ptid),
294 (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
295 perror_with_name (_("Couldn't write debug registers"));
296 }
297
298 void
299 i386bsd_dr_set_control (unsigned long control)
300 {
301 i386bsd_dr_set (7, control);
302 }
303
304 void
305 i386bsd_dr_set_addr (int regnum, CORE_ADDR addr)
306 {
307 gdb_assert (regnum >= 0 && regnum <= 4);
308
309 i386bsd_dr_set (regnum, addr);
310 }
311
312 CORE_ADDR
313 i386bsd_dr_get_addr (int regnum)
314 {
315 return i386bsd_dr_get (inferior_ptid, regnum);
316 }
317
318 unsigned long
319 i386bsd_dr_get_status (void)
320 {
321 return i386bsd_dr_get (inferior_ptid, 6);
322 }
323
324 unsigned long
325 i386bsd_dr_get_control (void)
326 {
327 return i386bsd_dr_get (inferior_ptid, 7);
328 }
329
330 #endif /* PT_GETDBREGS */
331 \f
332
333 /* Provide a prototype to silence -Wmissing-prototypes. */
334 void _initialize_i386bsd_nat (void);
335
336 void
337 _initialize_i386bsd_nat (void)
338 {
339 int offset;
340
341 /* To support the recognition of signal handlers, i386bsd-tdep.c
342 hardcodes some constants. Inclusion of this file means that we
343 are compiling a native debugger, which means that we can use the
344 system header files and sysctl(3) to get at the relevant
345 information. */
346
347 #if defined (__FreeBSD_version) && __FreeBSD_version >= 400011
348 #define SC_REG_OFFSET i386fbsd4_sc_reg_offset
349 #elif defined (__FreeBSD_version) && __FreeBSD_version >= 300005
350 #define SC_REG_OFFSET i386fbsd_sc_reg_offset
351 #elif defined (NetBSD) || defined (__NetBSD_Version__)
352 #define SC_REG_OFFSET i386nbsd_sc_reg_offset
353 #elif defined (OpenBSD)
354 #define SC_REG_OFFSET i386obsd_sc_reg_offset
355 #endif
356
357 #ifdef SC_REG_OFFSET
358
359 /* We only check the program counter, stack pointer and frame
360 pointer since these members of `struct sigcontext' are essential
361 for providing backtraces. More checks could be added, but would
362 involve adding configure checks for the appropriate structure
363 members, since older BSD's don't provide all of them. */
364
365 #define SC_PC_OFFSET SC_REG_OFFSET[I386_EIP_REGNUM]
366 #define SC_SP_OFFSET SC_REG_OFFSET[I386_ESP_REGNUM]
367 #define SC_FP_OFFSET SC_REG_OFFSET[I386_EBP_REGNUM]
368
369 /* Override the default value for the offset of the program counter
370 in the sigcontext structure. */
371 offset = offsetof (struct sigcontext, sc_pc);
372
373 if (SC_PC_OFFSET != offset)
374 {
375 warning (_("\
376 offsetof (struct sigcontext, sc_pc) yields %d instead of %d.\n\
377 Please report this to <bug-gdb@gnu.org>."),
378 offset, SC_PC_OFFSET);
379 }
380
381 SC_PC_OFFSET = offset;
382
383 /* Likewise for the stack pointer. */
384 offset = offsetof (struct sigcontext, sc_sp);
385
386 if (SC_SP_OFFSET != offset)
387 {
388 warning (_("\
389 offsetof (struct sigcontext, sc_sp) yields %d instead of %d.\n\
390 Please report this to <bug-gdb@gnu.org>."),
391 offset, SC_SP_OFFSET);
392 }
393
394 SC_SP_OFFSET = offset;
395
396 /* And the frame pointer. */
397 offset = offsetof (struct sigcontext, sc_fp);
398
399 if (SC_FP_OFFSET != offset)
400 {
401 warning (_("\
402 offsetof (struct sigcontext, sc_fp) yields %d instead of %d.\n\
403 Please report this to <bug-gdb@gnu.org>."),
404 offset, SC_FP_OFFSET);
405 }
406
407 SC_FP_OFFSET = offset;
408
409 #endif /* SC_REG_OFFSET */
410 }
This page took 0.041576 seconds and 4 git commands to generate.