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