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