Convert have_ptrace_getregset to a tri-state boolean
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
CommitLineData
a4194092 1/* Native-dependent code for GNU/Linux i386.
a4b6fc86 2
32d0add0 3 Copyright (C) 1999-2015 Free Software Foundation, Inc.
d4f3574e 4
04cd15b6 5 This file is part of GDB.
d4f3574e 6
04cd15b6
MK
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
04cd15b6 10 (at your option) any later version.
d4f3574e 11
04cd15b6
MK
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.
d4f3574e 16
04cd15b6 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d4f3574e
SS
19
20#include "defs.h"
21#include "inferior.h"
22#include "gdbcore.h"
4e052eda 23#include "regcache.h"
c131fcee 24#include "elf/common.h"
d4f3574e 25#include <sys/ptrace.h>
72fde3df 26#include <sys/uio.h>
c60c0f5f 27#include "gregset.h"
3116063b 28#include "gdb_proc_service.h"
c60c0f5f 29
3116063b 30#include "i386-linux-nat.h"
e750d25e 31#include "i387-tdep.h"
c3833324 32#include "i386-tdep.h"
5179e78f 33#include "i386-linux-tdep.h"
df7e5265 34#include "x86-xstate.h"
c131fcee 35
8d683210 36#include "linux-nat.h"
040baaf6 37#include "x86-linux-nat.h"
ca9b78ce 38#include "nat/linux-ptrace.h"
d4f3574e 39
a4b6fc86
AC
40/* The register sets used in GNU/Linux ELF core-dumps are identical to
41 the register sets in `struct user' that is used for a.out
42 core-dumps, and is also used by `ptrace'. The corresponding types
43 are `elf_gregset_t' for the general-purpose registers (with
04cd15b6
MK
44 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
45 for the floating-point registers.
46
47 Those types used to be available under the names `gregset_t' and
48 `fpregset_t' too, and this file used those names in the past. But
49 those names are now used for the register sets used in the
50 `mcontext_t' type, and have a different size and layout. */
51
5c44784c
JM
52/* Which ptrace request retrieves which registers?
53 These apply to the corresponding SET requests as well. */
e64a344c 54
5c44784c 55#define GETREGS_SUPPLIES(regno) \
3fb1c838 56 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
e64a344c 57
6ce2ac0b 58#define GETFPXREGS_SUPPLIES(regno) \
f6792ef4 59 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
5c44784c 60
c131fcee 61#define GETXSTATEREGS_SUPPLIES(regno) \
01f9f808 62 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX512_NUM_REGS)
c131fcee 63
f60300e7
MK
64/* Does the current host support the GETREGS request? */
65int have_ptrace_getregs =
66#ifdef HAVE_PTRACE_GETREGS
67 1
68#else
69 0
70#endif
71;
72
6ce2ac0b 73/* Does the current host support the GETFPXREGS request? The header
5c44784c
JM
74 file may or may not define it, and even if it is defined, the
75 kernel will return EIO if it's running on a pre-SSE processor.
76
77 My instinct is to attach this to some architecture- or
78 target-specific data structure, but really, a particular GDB
79 process can only run on top of one kernel at a time. So it's okay
80 for this to be a simple variable. */
6ce2ac0b
MK
81int have_ptrace_getfpxregs =
82#ifdef HAVE_PTRACE_GETFPXREGS
3a13a53b 83 -1
5c44784c
JM
84#else
85 0
86#endif
87;
f60300e7 88\f
6ce2ac0b 89
ce556f85 90/* Accessing registers through the U area, one at a time. */
f60300e7
MK
91
92/* Fetch one register. */
93
94static void
56be3814 95fetch_register (struct regcache *regcache, int regno)
f60300e7 96{
f60300e7 97 int tid;
ce556f85 98 int val;
f60300e7 99
ce556f85 100 gdb_assert (!have_ptrace_getregs);
be0d2954 101 if (i386_linux_gregset_reg_offset[regno] == -1)
f60300e7 102 {
56be3814 103 regcache_raw_supply (regcache, regno, NULL);
f60300e7
MK
104 return;
105 }
106
ce556f85 107 /* GNU/Linux LWP ID's are process ID's. */
dfd4cc63 108 tid = ptid_get_lwp (inferior_ptid);
e64a344c 109 if (tid == 0)
dfd4cc63 110 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
f60300e7 111
ce556f85 112 errno = 0;
be0d2954
L
113 val = ptrace (PTRACE_PEEKUSER, tid,
114 i386_linux_gregset_reg_offset[regno], 0);
ce556f85 115 if (errno != 0)
c9f4d572 116 error (_("Couldn't read register %s (#%d): %s."),
875f8d0e 117 gdbarch_register_name (get_regcache_arch (regcache), regno),
ce556f85 118 regno, safe_strerror (errno));
f60300e7 119
56be3814 120 regcache_raw_supply (regcache, regno, &val);
f60300e7
MK
121}
122
1777feb0 123/* Store one register. */
f60300e7
MK
124
125static void
56be3814 126store_register (const struct regcache *regcache, int regno)
f60300e7 127{
f60300e7 128 int tid;
ce556f85 129 int val;
f60300e7 130
ce556f85 131 gdb_assert (!have_ptrace_getregs);
be0d2954 132 if (i386_linux_gregset_reg_offset[regno] == -1)
ce556f85 133 return;
f60300e7 134
ce556f85 135 /* GNU/Linux LWP ID's are process ID's. */
dfd4cc63 136 tid = ptid_get_lwp (inferior_ptid);
e64a344c 137 if (tid == 0)
dfd4cc63 138 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
f60300e7 139
ce556f85 140 errno = 0;
56be3814 141 regcache_raw_collect (regcache, regno, &val);
be0d2954
L
142 ptrace (PTRACE_POKEUSER, tid,
143 i386_linux_gregset_reg_offset[regno], val);
ce556f85 144 if (errno != 0)
c9f4d572 145 error (_("Couldn't write register %s (#%d): %s."),
875f8d0e 146 gdbarch_register_name (get_regcache_arch (regcache), regno),
ce556f85 147 regno, safe_strerror (errno));
f60300e7 148}
5c44784c 149\f
6ce2ac0b 150
04cd15b6
MK
151/* Transfering the general-purpose registers between GDB, inferiors
152 and core files. */
153
ad2a4d09 154/* Fill GDB's register array with the general-purpose register values
04cd15b6 155 in *GREGSETP. */
5c44784c 156
d4f3574e 157void
7f7fe91e 158supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
d4f3574e 159{
be0d2954 160 const gdb_byte *regp = (const gdb_byte *) gregsetp;
6ce2ac0b 161 int i;
d4f3574e 162
98df6387 163 for (i = 0; i < I386_NUM_GREGS; i++)
be0d2954
L
164 regcache_raw_supply (regcache, i,
165 regp + i386_linux_gregset_reg_offset[i]);
3fb1c838 166
875f8d0e
UW
167 if (I386_LINUX_ORIG_EAX_REGNUM
168 < gdbarch_num_regs (get_regcache_arch (regcache)))
1777feb0
MS
169 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
170 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
917317f4
JM
171}
172
04cd15b6
MK
173/* Fill register REGNO (if it is a general-purpose register) in
174 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
175 do this for all registers. */
6ce2ac0b 176
917317f4 177void
7f7fe91e
UW
178fill_gregset (const struct regcache *regcache,
179 elf_gregset_t *gregsetp, int regno)
917317f4 180{
be0d2954 181 gdb_byte *regp = (gdb_byte *) gregsetp;
6ce2ac0b 182 int i;
04cd15b6 183
98df6387 184 for (i = 0; i < I386_NUM_GREGS; i++)
099a9414 185 if (regno == -1 || regno == i)
be0d2954
L
186 regcache_raw_collect (regcache, i,
187 regp + i386_linux_gregset_reg_offset[i]);
3fb1c838 188
82ea117a 189 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
875f8d0e
UW
190 && I386_LINUX_ORIG_EAX_REGNUM
191 < gdbarch_num_regs (get_regcache_arch (regcache)))
1777feb0
MS
192 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
193 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
d4f3574e
SS
194}
195
f60300e7
MK
196#ifdef HAVE_PTRACE_GETREGS
197
04cd15b6
MK
198/* Fetch all general-purpose registers from process/thread TID and
199 store their values in GDB's register array. */
d4f3574e 200
5c44784c 201static void
56be3814 202fetch_regs (struct regcache *regcache, int tid)
5c44784c 203{
04cd15b6 204 elf_gregset_t regs;
2e024c20 205 elf_gregset_t *regs_p = &regs;
5c44784c 206
6ce2ac0b 207 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
5c44784c 208 {
f60300e7
MK
209 if (errno == EIO)
210 {
211 /* The kernel we're running on doesn't support the GETREGS
212 request. Reset `have_ptrace_getregs'. */
213 have_ptrace_getregs = 0;
214 return;
215 }
216
e2e0b3e5 217 perror_with_name (_("Couldn't get registers"));
5c44784c
JM
218 }
219
2e024c20 220 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
5c44784c
JM
221}
222
04cd15b6
MK
223/* Store all valid general-purpose registers in GDB's register array
224 into the process/thread specified by TID. */
5c44784c 225
5c44784c 226static void
56be3814 227store_regs (const struct regcache *regcache, int tid, int regno)
5c44784c 228{
04cd15b6 229 elf_gregset_t regs;
5c44784c 230
6ce2ac0b 231 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 232 perror_with_name (_("Couldn't get registers"));
5c44784c 233
56be3814 234 fill_gregset (regcache, &regs, regno);
6ce2ac0b
MK
235
236 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 237 perror_with_name (_("Couldn't write registers"));
5c44784c
JM
238}
239
f60300e7
MK
240#else
241
56be3814
UW
242static void fetch_regs (struct regcache *regcache, int tid) {}
243static void store_regs (const struct regcache *regcache, int tid, int regno) {}
f60300e7
MK
244
245#endif
5c44784c 246\f
5c44784c 247
6ce2ac0b 248/* Transfering floating-point registers between GDB, inferiors and cores. */
d4f3574e 249
04cd15b6 250/* Fill GDB's register array with the floating-point register values in
917317f4 251 *FPREGSETP. */
04cd15b6 252
d4f3574e 253void
7f7fe91e 254supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
d4f3574e 255{
7f7fe91e 256 i387_supply_fsave (regcache, -1, fpregsetp);
917317f4 257}
d4f3574e 258
04cd15b6
MK
259/* Fill register REGNO (if it is a floating-point register) in
260 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
261 do this for all registers. */
917317f4
JM
262
263void
7f7fe91e
UW
264fill_fpregset (const struct regcache *regcache,
265 elf_fpregset_t *fpregsetp, int regno)
917317f4 266{
7f7fe91e 267 i387_collect_fsave (regcache, regno, fpregsetp);
d4f3574e
SS
268}
269
f60300e7
MK
270#ifdef HAVE_PTRACE_GETREGS
271
04cd15b6
MK
272/* Fetch all floating-point registers from process/thread TID and store
273 thier values in GDB's register array. */
917317f4 274
d4f3574e 275static void
56be3814 276fetch_fpregs (struct regcache *regcache, int tid)
d4f3574e 277{
04cd15b6 278 elf_fpregset_t fpregs;
d4f3574e 279
6ce2ac0b 280 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 281 perror_with_name (_("Couldn't get floating point status"));
d4f3574e 282
56be3814 283 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
d4f3574e
SS
284}
285
04cd15b6
MK
286/* Store all valid floating-point registers in GDB's register array
287 into the process/thread specified by TID. */
d4f3574e 288
d4f3574e 289static void
56be3814 290store_fpregs (const struct regcache *regcache, int tid, int regno)
d4f3574e 291{
04cd15b6 292 elf_fpregset_t fpregs;
d4f3574e 293
6ce2ac0b 294 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 295 perror_with_name (_("Couldn't get floating point status"));
d4f3574e 296
56be3814 297 fill_fpregset (regcache, &fpregs, regno);
d4f3574e 298
6ce2ac0b 299 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 300 perror_with_name (_("Couldn't write floating point status"));
d4f3574e
SS
301}
302
f60300e7
MK
303#else
304
1777feb0
MS
305static void
306fetch_fpregs (struct regcache *regcache, int tid)
307{
308}
309
310static void
311store_fpregs (const struct regcache *regcache, int tid, int regno)
312{
313}
f60300e7
MK
314
315#endif
5c44784c 316\f
d4f3574e 317
6ce2ac0b 318/* Transfering floating-point and SSE registers to and from GDB. */
11cf8741 319
c131fcee
L
320/* Fetch all registers covered by the PTRACE_GETREGSET request from
321 process/thread TID and store their values in GDB's register array.
322 Return non-zero if successful, zero otherwise. */
323
324static int
325fetch_xstateregs (struct regcache *regcache, int tid)
326{
df7e5265 327 char xstateregs[X86_XSTATE_MAX_SIZE];
c131fcee
L
328 struct iovec iov;
329
0bdb2f78 330 if (have_ptrace_getregset != TRIBOOL_TRUE)
c131fcee
L
331 return 0;
332
333 iov.iov_base = xstateregs;
334 iov.iov_len = sizeof(xstateregs);
335 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
336 &iov) < 0)
337 perror_with_name (_("Couldn't read extended state status"));
338
339 i387_supply_xsave (regcache, -1, xstateregs);
340 return 1;
341}
342
343/* Store all valid registers in GDB's register array covered by the
344 PTRACE_SETREGSET request into the process/thread specified by TID.
345 Return non-zero if successful, zero otherwise. */
346
347static int
348store_xstateregs (const struct regcache *regcache, int tid, int regno)
349{
df7e5265 350 char xstateregs[X86_XSTATE_MAX_SIZE];
c131fcee
L
351 struct iovec iov;
352
0bdb2f78 353 if (have_ptrace_getregset != TRIBOOL_TRUE)
c131fcee
L
354 return 0;
355
356 iov.iov_base = xstateregs;
357 iov.iov_len = sizeof(xstateregs);
358 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
359 &iov) < 0)
360 perror_with_name (_("Couldn't read extended state status"));
361
362 i387_collect_xsave (regcache, regno, xstateregs, 0);
363
364 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
365 (int) &iov) < 0)
366 perror_with_name (_("Couldn't write extended state status"));
367
368 return 1;
369}
370
6ce2ac0b 371#ifdef HAVE_PTRACE_GETFPXREGS
04cd15b6 372
6ce2ac0b 373/* Fetch all registers covered by the PTRACE_GETFPXREGS request from
04cd15b6
MK
374 process/thread TID and store their values in GDB's register array.
375 Return non-zero if successful, zero otherwise. */
5c44784c 376
5c44784c 377static int
56be3814 378fetch_fpxregs (struct regcache *regcache, int tid)
5c44784c 379{
6ce2ac0b 380 elf_fpxregset_t fpxregs;
5c44784c 381
6ce2ac0b 382 if (! have_ptrace_getfpxregs)
5c44784c
JM
383 return 0;
384
6ce2ac0b 385 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
d4f3574e 386 {
5c44784c
JM
387 if (errno == EIO)
388 {
6ce2ac0b 389 have_ptrace_getfpxregs = 0;
5c44784c
JM
390 return 0;
391 }
392
e2e0b3e5 393 perror_with_name (_("Couldn't read floating-point and SSE registers"));
d4f3574e
SS
394 }
395
b7a8b4ef 396 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
5c44784c
JM
397 return 1;
398}
d4f3574e 399
04cd15b6 400/* Store all valid registers in GDB's register array covered by the
6ce2ac0b 401 PTRACE_SETFPXREGS request into the process/thread specified by TID.
04cd15b6 402 Return non-zero if successful, zero otherwise. */
5c44784c 403
5c44784c 404static int
56be3814 405store_fpxregs (const struct regcache *regcache, int tid, int regno)
5c44784c 406{
6ce2ac0b 407 elf_fpxregset_t fpxregs;
5c44784c 408
6ce2ac0b 409 if (! have_ptrace_getfpxregs)
5c44784c 410 return 0;
6ce2ac0b
MK
411
412 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
2866d305
MK
413 {
414 if (errno == EIO)
415 {
416 have_ptrace_getfpxregs = 0;
417 return 0;
418 }
419
e2e0b3e5 420 perror_with_name (_("Couldn't read floating-point and SSE registers"));
2866d305 421 }
5c44784c 422
b7a8b4ef 423 i387_collect_fxsave (regcache, regno, &fpxregs);
5c44784c 424
6ce2ac0b 425 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
e2e0b3e5 426 perror_with_name (_("Couldn't write floating-point and SSE registers"));
5c44784c
JM
427
428 return 1;
429}
430
5c44784c
JM
431#else
432
1777feb0
MS
433static int
434fetch_fpxregs (struct regcache *regcache, int tid)
435{
436 return 0;
437}
438
439static int
440store_fpxregs (const struct regcache *regcache, int tid, int regno)
441{
442 return 0;
443}
5c44784c 444
6ce2ac0b 445#endif /* HAVE_PTRACE_GETFPXREGS */
5c44784c 446\f
6ce2ac0b 447
5c44784c 448/* Transferring arbitrary registers between GDB and inferior. */
d4f3574e 449
04cd15b6
MK
450/* Fetch register REGNO from the child process. If REGNO is -1, do
451 this for all registers (including the floating point and SSE
452 registers). */
d4f3574e 453
10d6c8cd 454static void
28439f5e
PA
455i386_linux_fetch_inferior_registers (struct target_ops *ops,
456 struct regcache *regcache, int regno)
d4f3574e 457{
ed9a39eb
JM
458 int tid;
459
f60300e7
MK
460 /* Use the old method of peeking around in `struct user' if the
461 GETREGS request isn't available. */
ce556f85 462 if (!have_ptrace_getregs)
f60300e7 463 {
ce556f85
MK
464 int i;
465
875f8d0e 466 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ce556f85 467 if (regno == -1 || regno == i)
56be3814 468 fetch_register (regcache, i);
ce556f85 469
f60300e7
MK
470 return;
471 }
472
a4b6fc86 473 /* GNU/Linux LWP ID's are process ID's. */
dfd4cc63 474 tid = ptid_get_lwp (inferior_ptid);
e64a344c 475 if (tid == 0)
dfd4cc63 476 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
ed9a39eb 477
6ce2ac0b 478 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
04cd15b6 479 transfers more registers in one system call, and we'll cache the
6ce2ac0b 480 results. But remember that fetch_fpxregs can fail, and return
04cd15b6 481 zero. */
5c44784c
JM
482 if (regno == -1)
483 {
56be3814 484 fetch_regs (regcache, tid);
f60300e7
MK
485
486 /* The call above might reset `have_ptrace_getregs'. */
ce556f85 487 if (!have_ptrace_getregs)
f60300e7 488 {
84e473c8 489 i386_linux_fetch_inferior_registers (ops, regcache, regno);
f60300e7
MK
490 return;
491 }
492
c131fcee
L
493 if (fetch_xstateregs (regcache, tid))
494 return;
56be3814 495 if (fetch_fpxregs (regcache, tid))
5c44784c 496 return;
56be3814 497 fetch_fpregs (regcache, tid);
5c44784c
JM
498 return;
499 }
d4f3574e 500
5c44784c
JM
501 if (GETREGS_SUPPLIES (regno))
502 {
56be3814 503 fetch_regs (regcache, tid);
5c44784c
JM
504 return;
505 }
506
c131fcee
L
507 if (GETXSTATEREGS_SUPPLIES (regno))
508 {
509 if (fetch_xstateregs (regcache, tid))
510 return;
511 }
512
6ce2ac0b 513 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 514 {
56be3814 515 if (fetch_fpxregs (regcache, tid))
5c44784c
JM
516 return;
517
518 /* Either our processor or our kernel doesn't support the SSE
519 registers, so read the FP registers in the traditional way,
520 and fill the SSE registers with dummy values. It would be
521 more graceful to handle differences in the register set using
522 gdbarch. Until then, this will at least make things work
523 plausibly. */
56be3814 524 fetch_fpregs (regcache, tid);
5c44784c
JM
525 return;
526 }
527
8e65ff28 528 internal_error (__FILE__, __LINE__,
e2e0b3e5 529 _("Got request for bad register number %d."), regno);
d4f3574e
SS
530}
531
04cd15b6
MK
532/* Store register REGNO back into the child process. If REGNO is -1,
533 do this for all registers (including the floating point and SSE
534 registers). */
10d6c8cd 535static void
28439f5e
PA
536i386_linux_store_inferior_registers (struct target_ops *ops,
537 struct regcache *regcache, int regno)
d4f3574e 538{
ed9a39eb
JM
539 int tid;
540
f60300e7
MK
541 /* Use the old method of poking around in `struct user' if the
542 SETREGS request isn't available. */
ce556f85 543 if (!have_ptrace_getregs)
f60300e7 544 {
ce556f85
MK
545 int i;
546
875f8d0e 547 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ce556f85 548 if (regno == -1 || regno == i)
56be3814 549 store_register (regcache, i);
ce556f85 550
f60300e7
MK
551 return;
552 }
553
a4b6fc86 554 /* GNU/Linux LWP ID's are process ID's. */
dfd4cc63 555 tid = ptid_get_lwp (inferior_ptid);
e64a344c 556 if (tid == 0)
dfd4cc63 557 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
ed9a39eb 558
6ce2ac0b 559 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
04cd15b6 560 transfers more registers in one system call. But remember that
6ce2ac0b 561 store_fpxregs can fail, and return zero. */
5c44784c
JM
562 if (regno == -1)
563 {
56be3814 564 store_regs (regcache, tid, regno);
c131fcee
L
565 if (store_xstateregs (regcache, tid, regno))
566 return;
56be3814 567 if (store_fpxregs (regcache, tid, regno))
5c44784c 568 return;
56be3814 569 store_fpregs (regcache, tid, regno);
5c44784c
JM
570 return;
571 }
d4f3574e 572
5c44784c
JM
573 if (GETREGS_SUPPLIES (regno))
574 {
56be3814 575 store_regs (regcache, tid, regno);
5c44784c
JM
576 return;
577 }
578
c131fcee
L
579 if (GETXSTATEREGS_SUPPLIES (regno))
580 {
581 if (store_xstateregs (regcache, tid, regno))
582 return;
583 }
584
6ce2ac0b 585 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 586 {
56be3814 587 if (store_fpxregs (regcache, tid, regno))
5c44784c
JM
588 return;
589
590 /* Either our processor or our kernel doesn't support the SSE
04cd15b6
MK
591 registers, so just write the FP registers in the traditional
592 way. */
56be3814 593 store_fpregs (regcache, tid, regno);
5c44784c
JM
594 return;
595 }
596
8e65ff28 597 internal_error (__FILE__, __LINE__,
e2e0b3e5 598 _("Got request to store bad register number %d."), regno);
d4f3574e 599}
de57eccd 600\f
6ce2ac0b 601
8c420b8d
GB
602/* Called by libthread_db. Returns a pointer to the thread local
603 storage (or its descriptor). */
604
605ps_err_e
606ps_get_thread_area (const struct ps_prochandle *ph,
607 lwpid_t lwpid, int idx, void **base)
608{
609 unsigned int base_addr;
610 ps_err_e result;
611
612 result = x86_linux_get_thread_area (lwpid, (void *) idx, &base_addr);
613
614 if (result == PS_OK)
615 *(int *) base = base_addr;
616
617 return result;
618}
5bca7895
MK
619\f
620
a4b6fc86 621/* The instruction for a GNU/Linux system call is:
a6abb2c0
MK
622 int $0x80
623 or 0xcd 0x80. */
624
625static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
626
627#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
628
629/* The system call number is stored in the %eax register. */
7532965f 630#define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
a6abb2c0
MK
631
632/* We are specifically interested in the sigreturn and rt_sigreturn
633 system calls. */
634
635#ifndef SYS_sigreturn
636#define SYS_sigreturn 0x77
637#endif
638#ifndef SYS_rt_sigreturn
639#define SYS_rt_sigreturn 0xad
640#endif
641
642/* Offset to saved processor flags, from <asm/sigcontext.h>. */
643#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
644
645/* Resume execution of the inferior process.
646 If STEP is nonzero, single-step it.
647 If SIGNAL is nonzero, give it that signal. */
648
10d6c8cd 649static void
28439f5e 650i386_linux_resume (struct target_ops *ops,
2ea28649 651 ptid_t ptid, int step, enum gdb_signal signal)
a6abb2c0 652{
90ad5e1d 653 int pid = ptid_get_lwp (ptid);
a96d9b2e
SDJ
654 int request;
655
656 if (catch_syscall_enabled () > 0)
657 request = PTRACE_SYSCALL;
658 else
659 request = PTRACE_CONT;
a6abb2c0 660
a6abb2c0
MK
661 if (step)
662 {
90ad5e1d 663 struct regcache *regcache = get_thread_regcache (ptid);
e17a4113
UW
664 struct gdbarch *gdbarch = get_regcache_arch (regcache);
665 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7b86a1b8 666 ULONGEST pc;
8e70166d 667 gdb_byte buf[LINUX_SYSCALL_LEN];
a6abb2c0
MK
668
669 request = PTRACE_SINGLESTEP;
670
e17a4113
UW
671 regcache_cooked_read_unsigned (regcache,
672 gdbarch_pc_regnum (gdbarch), &pc);
7b86a1b8 673
a6abb2c0
MK
674 /* Returning from a signal trampoline is done by calling a
675 special system call (sigreturn or rt_sigreturn, see
676 i386-linux-tdep.c for more information). This system call
677 restores the registers that were saved when the signal was
678 raised, including %eflags. That means that single-stepping
679 won't work. Instead, we'll have to modify the signal context
680 that's about to be restored, and set the trace flag there. */
681
682 /* First check if PC is at a system call. */
8defab1a 683 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
a6abb2c0
MK
684 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
685 {
7b86a1b8
UW
686 ULONGEST syscall;
687 regcache_cooked_read_unsigned (regcache,
688 LINUX_SYSCALL_REGNUM, &syscall);
a6abb2c0
MK
689
690 /* Then check the system call number. */
691 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
692 {
7b86a1b8 693 ULONGEST sp, addr;
a6abb2c0 694 unsigned long int eflags;
7bf0983e 695
7b86a1b8 696 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
a6abb2c0 697 if (syscall == SYS_rt_sigreturn)
f3d6df6d
YQ
698 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order)
699 + 20;
7b86a1b8
UW
700 else
701 addr = sp;
a6abb2c0
MK
702
703 /* Set the trace flag in the context that's about to be
704 restored. */
705 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
8e70166d 706 read_memory (addr, (gdb_byte *) &eflags, 4);
a6abb2c0 707 eflags |= 0x0100;
8e70166d 708 write_memory (addr, (gdb_byte *) &eflags, 4);
a6abb2c0
MK
709 }
710 }
711 }
712
2ea28649 713 if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1)
e2e0b3e5 714 perror_with_name (("ptrace"));
a6abb2c0 715}
040baaf6 716\f
c1e246a0
GB
717
718/* -Wmissing-prototypes */
719extern initialize_file_ftype _initialize_i386_linux_nat;
720
721void
722_initialize_i386_linux_nat (void)
723{
724 /* Create a generic x86 GNU/Linux target. */
725 struct target_ops *t = x86_linux_create_target ();
726
727 /* Override the default ptrace resume method. */
728 t->to_resume = i386_linux_resume;
729
730 /* Add our register access methods. */
731 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
732 t->to_store_registers = i386_linux_store_inferior_registers;
733
734 /* Add the target. */
735 x86_linux_add_target (t);
736}
This page took 1.921087 seconds and 4 git commands to generate.