2011-12-05 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
CommitLineData
a4194092 1/* Native-dependent code for GNU/Linux i386.
a4b6fc86 2
0fb0cc75 3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7b6bb8da 4 2009, 2010, 2011 Free Software Foundation, Inc.
d4f3574e 5
04cd15b6 6 This file is part of GDB.
d4f3574e 7
04cd15b6
MK
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
04cd15b6 11 (at your option) any later version.
d4f3574e 12
04cd15b6
MK
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
d4f3574e 17
04cd15b6 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d4f3574e
SS
20
21#include "defs.h"
9bb9e8ad 22#include "i386-nat.h"
d4f3574e
SS
23#include "inferior.h"
24#include "gdbcore.h"
4e052eda 25#include "regcache.h"
c131fcee 26#include "regset.h"
10d6c8cd 27#include "target.h"
4de4c07c 28#include "linux-nat.h"
d4f3574e 29
84346e11 30#include "gdb_assert.h"
309367d4 31#include "gdb_string.h"
c131fcee
L
32#include "elf/common.h"
33#include <sys/uio.h>
d4f3574e
SS
34#include <sys/ptrace.h>
35#include <sys/user.h>
36#include <sys/procfs.h>
37
38#ifdef HAVE_SYS_REG_H
39#include <sys/reg.h>
40#endif
41
ce556f85
MK
42#ifndef ORIG_EAX
43#define ORIG_EAX -1
44#endif
45
84346e11
MK
46#ifdef HAVE_SYS_DEBUGREG_H
47#include <sys/debugreg.h>
48#endif
49
50#ifndef DR_FIRSTADDR
51#define DR_FIRSTADDR 0
52#endif
53
54#ifndef DR_LASTADDR
55#define DR_LASTADDR 3
56#endif
57
58#ifndef DR_STATUS
59#define DR_STATUS 6
60#endif
61
62#ifndef DR_CONTROL
63#define DR_CONTROL 7
64#endif
65
6ce2ac0b 66/* Prototypes for supply_gregset etc. */
c60c0f5f
MS
67#include "gregset.h"
68
e750d25e 69#include "i387-tdep.h"
c3833324 70#include "i386-tdep.h"
5179e78f
AC
71#include "i386-linux-tdep.h"
72
b757528f
JJ
73/* Defines ps_err_e, struct ps_prochandle. */
74#include "gdb_proc_service.h"
c131fcee
L
75
76#include "i386-xstate.h"
77
78#ifndef PTRACE_GETREGSET
79#define PTRACE_GETREGSET 0x4204
80#endif
81
82#ifndef PTRACE_SETREGSET
83#define PTRACE_SETREGSET 0x4205
84#endif
85
86/* Does the current host support PTRACE_GETREGSET? */
87static int have_ptrace_getregset = -1;
6ce2ac0b 88\f
d4f3574e 89
a4b6fc86
AC
90/* The register sets used in GNU/Linux ELF core-dumps are identical to
91 the register sets in `struct user' that is used for a.out
92 core-dumps, and is also used by `ptrace'. The corresponding types
93 are `elf_gregset_t' for the general-purpose registers (with
04cd15b6
MK
94 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
95 for the floating-point registers.
96
97 Those types used to be available under the names `gregset_t' and
98 `fpregset_t' too, and this file used those names in the past. But
99 those names are now used for the register sets used in the
100 `mcontext_t' type, and have a different size and layout. */
101
5c44784c
JM
102/* Which ptrace request retrieves which registers?
103 These apply to the corresponding SET requests as well. */
e64a344c 104
5c44784c 105#define GETREGS_SUPPLIES(regno) \
3fb1c838 106 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
e64a344c 107
6ce2ac0b 108#define GETFPXREGS_SUPPLIES(regno) \
f6792ef4 109 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
5c44784c 110
c131fcee
L
111#define GETXSTATEREGS_SUPPLIES(regno) \
112 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX_NUM_REGS)
113
f60300e7
MK
114/* Does the current host support the GETREGS request? */
115int have_ptrace_getregs =
116#ifdef HAVE_PTRACE_GETREGS
117 1
118#else
119 0
120#endif
121;
122
6ce2ac0b 123/* Does the current host support the GETFPXREGS request? The header
5c44784c
JM
124 file may or may not define it, and even if it is defined, the
125 kernel will return EIO if it's running on a pre-SSE processor.
126
127 My instinct is to attach this to some architecture- or
128 target-specific data structure, but really, a particular GDB
129 process can only run on top of one kernel at a time. So it's okay
130 for this to be a simple variable. */
6ce2ac0b
MK
131int have_ptrace_getfpxregs =
132#ifdef HAVE_PTRACE_GETFPXREGS
3a13a53b 133 -1
5c44784c
JM
134#else
135 0
136#endif
137;
f60300e7 138\f
6ce2ac0b 139
ce556f85 140/* Accessing registers through the U area, one at a time. */
f60300e7
MK
141
142/* Fetch one register. */
143
144static void
56be3814 145fetch_register (struct regcache *regcache, int regno)
f60300e7 146{
f60300e7 147 int tid;
ce556f85 148 int val;
f60300e7 149
ce556f85 150 gdb_assert (!have_ptrace_getregs);
be0d2954 151 if (i386_linux_gregset_reg_offset[regno] == -1)
f60300e7 152 {
56be3814 153 regcache_raw_supply (regcache, regno, NULL);
f60300e7
MK
154 return;
155 }
156
ce556f85 157 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
158 tid = TIDGET (inferior_ptid);
159 if (tid == 0)
160 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
f60300e7 161
ce556f85 162 errno = 0;
be0d2954
L
163 val = ptrace (PTRACE_PEEKUSER, tid,
164 i386_linux_gregset_reg_offset[regno], 0);
ce556f85 165 if (errno != 0)
c9f4d572 166 error (_("Couldn't read register %s (#%d): %s."),
875f8d0e 167 gdbarch_register_name (get_regcache_arch (regcache), regno),
ce556f85 168 regno, safe_strerror (errno));
f60300e7 169
56be3814 170 regcache_raw_supply (regcache, regno, &val);
f60300e7
MK
171}
172
1777feb0 173/* Store one register. */
f60300e7
MK
174
175static void
56be3814 176store_register (const struct regcache *regcache, int regno)
f60300e7 177{
f60300e7 178 int tid;
ce556f85 179 int val;
f60300e7 180
ce556f85 181 gdb_assert (!have_ptrace_getregs);
be0d2954 182 if (i386_linux_gregset_reg_offset[regno] == -1)
ce556f85 183 return;
f60300e7 184
ce556f85 185 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
186 tid = TIDGET (inferior_ptid);
187 if (tid == 0)
188 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
f60300e7 189
ce556f85 190 errno = 0;
56be3814 191 regcache_raw_collect (regcache, regno, &val);
be0d2954
L
192 ptrace (PTRACE_POKEUSER, tid,
193 i386_linux_gregset_reg_offset[regno], val);
ce556f85 194 if (errno != 0)
c9f4d572 195 error (_("Couldn't write register %s (#%d): %s."),
875f8d0e 196 gdbarch_register_name (get_regcache_arch (regcache), regno),
ce556f85 197 regno, safe_strerror (errno));
f60300e7 198}
5c44784c 199\f
6ce2ac0b 200
04cd15b6
MK
201/* Transfering the general-purpose registers between GDB, inferiors
202 and core files. */
203
ad2a4d09 204/* Fill GDB's register array with the general-purpose register values
04cd15b6 205 in *GREGSETP. */
5c44784c 206
d4f3574e 207void
7f7fe91e 208supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
d4f3574e 209{
be0d2954 210 const gdb_byte *regp = (const gdb_byte *) gregsetp;
6ce2ac0b 211 int i;
d4f3574e 212
98df6387 213 for (i = 0; i < I386_NUM_GREGS; i++)
be0d2954
L
214 regcache_raw_supply (regcache, i,
215 regp + i386_linux_gregset_reg_offset[i]);
3fb1c838 216
875f8d0e
UW
217 if (I386_LINUX_ORIG_EAX_REGNUM
218 < gdbarch_num_regs (get_regcache_arch (regcache)))
1777feb0
MS
219 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
220 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
917317f4
JM
221}
222
04cd15b6
MK
223/* Fill register REGNO (if it is a general-purpose register) in
224 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
225 do this for all registers. */
6ce2ac0b 226
917317f4 227void
7f7fe91e
UW
228fill_gregset (const struct regcache *regcache,
229 elf_gregset_t *gregsetp, int regno)
917317f4 230{
be0d2954 231 gdb_byte *regp = (gdb_byte *) gregsetp;
6ce2ac0b 232 int i;
04cd15b6 233
98df6387 234 for (i = 0; i < I386_NUM_GREGS; i++)
099a9414 235 if (regno == -1 || regno == i)
be0d2954
L
236 regcache_raw_collect (regcache, i,
237 regp + i386_linux_gregset_reg_offset[i]);
3fb1c838 238
82ea117a 239 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
875f8d0e
UW
240 && I386_LINUX_ORIG_EAX_REGNUM
241 < gdbarch_num_regs (get_regcache_arch (regcache)))
1777feb0
MS
242 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
243 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
d4f3574e
SS
244}
245
f60300e7
MK
246#ifdef HAVE_PTRACE_GETREGS
247
04cd15b6
MK
248/* Fetch all general-purpose registers from process/thread TID and
249 store their values in GDB's register array. */
d4f3574e 250
5c44784c 251static void
56be3814 252fetch_regs (struct regcache *regcache, int tid)
5c44784c 253{
04cd15b6 254 elf_gregset_t regs;
2e024c20 255 elf_gregset_t *regs_p = &regs;
5c44784c 256
6ce2ac0b 257 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
5c44784c 258 {
f60300e7
MK
259 if (errno == EIO)
260 {
261 /* The kernel we're running on doesn't support the GETREGS
262 request. Reset `have_ptrace_getregs'. */
263 have_ptrace_getregs = 0;
264 return;
265 }
266
e2e0b3e5 267 perror_with_name (_("Couldn't get registers"));
5c44784c
JM
268 }
269
2e024c20 270 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
5c44784c
JM
271}
272
04cd15b6
MK
273/* Store all valid general-purpose registers in GDB's register array
274 into the process/thread specified by TID. */
5c44784c 275
5c44784c 276static void
56be3814 277store_regs (const struct regcache *regcache, int tid, int regno)
5c44784c 278{
04cd15b6 279 elf_gregset_t regs;
5c44784c 280
6ce2ac0b 281 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 282 perror_with_name (_("Couldn't get registers"));
5c44784c 283
56be3814 284 fill_gregset (regcache, &regs, regno);
6ce2ac0b
MK
285
286 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 287 perror_with_name (_("Couldn't write registers"));
5c44784c
JM
288}
289
f60300e7
MK
290#else
291
56be3814
UW
292static void fetch_regs (struct regcache *regcache, int tid) {}
293static void store_regs (const struct regcache *regcache, int tid, int regno) {}
f60300e7
MK
294
295#endif
5c44784c 296\f
5c44784c 297
6ce2ac0b 298/* Transfering floating-point registers between GDB, inferiors and cores. */
d4f3574e 299
04cd15b6 300/* Fill GDB's register array with the floating-point register values in
917317f4 301 *FPREGSETP. */
04cd15b6 302
d4f3574e 303void
7f7fe91e 304supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
d4f3574e 305{
7f7fe91e 306 i387_supply_fsave (regcache, -1, fpregsetp);
917317f4 307}
d4f3574e 308
04cd15b6
MK
309/* Fill register REGNO (if it is a floating-point register) in
310 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
311 do this for all registers. */
917317f4
JM
312
313void
7f7fe91e
UW
314fill_fpregset (const struct regcache *regcache,
315 elf_fpregset_t *fpregsetp, int regno)
917317f4 316{
7f7fe91e 317 i387_collect_fsave (regcache, regno, fpregsetp);
d4f3574e
SS
318}
319
f60300e7
MK
320#ifdef HAVE_PTRACE_GETREGS
321
04cd15b6
MK
322/* Fetch all floating-point registers from process/thread TID and store
323 thier values in GDB's register array. */
917317f4 324
d4f3574e 325static void
56be3814 326fetch_fpregs (struct regcache *regcache, int tid)
d4f3574e 327{
04cd15b6 328 elf_fpregset_t fpregs;
d4f3574e 329
6ce2ac0b 330 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 331 perror_with_name (_("Couldn't get floating point status"));
d4f3574e 332
56be3814 333 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
d4f3574e
SS
334}
335
04cd15b6
MK
336/* Store all valid floating-point registers in GDB's register array
337 into the process/thread specified by TID. */
d4f3574e 338
d4f3574e 339static void
56be3814 340store_fpregs (const struct regcache *regcache, int tid, int regno)
d4f3574e 341{
04cd15b6 342 elf_fpregset_t fpregs;
d4f3574e 343
6ce2ac0b 344 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 345 perror_with_name (_("Couldn't get floating point status"));
d4f3574e 346
56be3814 347 fill_fpregset (regcache, &fpregs, regno);
d4f3574e 348
6ce2ac0b 349 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 350 perror_with_name (_("Couldn't write floating point status"));
d4f3574e
SS
351}
352
f60300e7
MK
353#else
354
1777feb0
MS
355static void
356fetch_fpregs (struct regcache *regcache, int tid)
357{
358}
359
360static void
361store_fpregs (const struct regcache *regcache, int tid, int regno)
362{
363}
f60300e7
MK
364
365#endif
5c44784c 366\f
d4f3574e 367
6ce2ac0b 368/* Transfering floating-point and SSE registers to and from GDB. */
11cf8741 369
c131fcee
L
370/* Fetch all registers covered by the PTRACE_GETREGSET request from
371 process/thread TID and store their values in GDB's register array.
372 Return non-zero if successful, zero otherwise. */
373
374static int
375fetch_xstateregs (struct regcache *regcache, int tid)
376{
377 char xstateregs[I386_XSTATE_MAX_SIZE];
378 struct iovec iov;
379
380 if (!have_ptrace_getregset)
381 return 0;
382
383 iov.iov_base = xstateregs;
384 iov.iov_len = sizeof(xstateregs);
385 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
386 &iov) < 0)
387 perror_with_name (_("Couldn't read extended state status"));
388
389 i387_supply_xsave (regcache, -1, xstateregs);
390 return 1;
391}
392
393/* Store all valid registers in GDB's register array covered by the
394 PTRACE_SETREGSET request into the process/thread specified by TID.
395 Return non-zero if successful, zero otherwise. */
396
397static int
398store_xstateregs (const struct regcache *regcache, int tid, int regno)
399{
400 char xstateregs[I386_XSTATE_MAX_SIZE];
401 struct iovec iov;
402
403 if (!have_ptrace_getregset)
404 return 0;
405
406 iov.iov_base = xstateregs;
407 iov.iov_len = sizeof(xstateregs);
408 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
409 &iov) < 0)
410 perror_with_name (_("Couldn't read extended state status"));
411
412 i387_collect_xsave (regcache, regno, xstateregs, 0);
413
414 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
415 (int) &iov) < 0)
416 perror_with_name (_("Couldn't write extended state status"));
417
418 return 1;
419}
420
6ce2ac0b 421#ifdef HAVE_PTRACE_GETFPXREGS
04cd15b6 422
6ce2ac0b 423/* Fetch all registers covered by the PTRACE_GETFPXREGS request from
04cd15b6
MK
424 process/thread TID and store their values in GDB's register array.
425 Return non-zero if successful, zero otherwise. */
5c44784c 426
5c44784c 427static int
56be3814 428fetch_fpxregs (struct regcache *regcache, int tid)
5c44784c 429{
6ce2ac0b 430 elf_fpxregset_t fpxregs;
5c44784c 431
6ce2ac0b 432 if (! have_ptrace_getfpxregs)
5c44784c
JM
433 return 0;
434
6ce2ac0b 435 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
d4f3574e 436 {
5c44784c
JM
437 if (errno == EIO)
438 {
6ce2ac0b 439 have_ptrace_getfpxregs = 0;
5c44784c
JM
440 return 0;
441 }
442
e2e0b3e5 443 perror_with_name (_("Couldn't read floating-point and SSE registers"));
d4f3574e
SS
444 }
445
b7a8b4ef 446 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
5c44784c
JM
447 return 1;
448}
d4f3574e 449
04cd15b6 450/* Store all valid registers in GDB's register array covered by the
6ce2ac0b 451 PTRACE_SETFPXREGS request into the process/thread specified by TID.
04cd15b6 452 Return non-zero if successful, zero otherwise. */
5c44784c 453
5c44784c 454static int
56be3814 455store_fpxregs (const struct regcache *regcache, int tid, int regno)
5c44784c 456{
6ce2ac0b 457 elf_fpxregset_t fpxregs;
5c44784c 458
6ce2ac0b 459 if (! have_ptrace_getfpxregs)
5c44784c 460 return 0;
6ce2ac0b
MK
461
462 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
2866d305
MK
463 {
464 if (errno == EIO)
465 {
466 have_ptrace_getfpxregs = 0;
467 return 0;
468 }
469
e2e0b3e5 470 perror_with_name (_("Couldn't read floating-point and SSE registers"));
2866d305 471 }
5c44784c 472
b7a8b4ef 473 i387_collect_fxsave (regcache, regno, &fpxregs);
5c44784c 474
6ce2ac0b 475 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
e2e0b3e5 476 perror_with_name (_("Couldn't write floating-point and SSE registers"));
5c44784c
JM
477
478 return 1;
479}
480
5c44784c
JM
481#else
482
1777feb0
MS
483static int
484fetch_fpxregs (struct regcache *regcache, int tid)
485{
486 return 0;
487}
488
489static int
490store_fpxregs (const struct regcache *regcache, int tid, int regno)
491{
492 return 0;
493}
5c44784c 494
6ce2ac0b 495#endif /* HAVE_PTRACE_GETFPXREGS */
5c44784c 496\f
6ce2ac0b 497
5c44784c 498/* Transferring arbitrary registers between GDB and inferior. */
d4f3574e 499
04cd15b6
MK
500/* Fetch register REGNO from the child process. If REGNO is -1, do
501 this for all registers (including the floating point and SSE
502 registers). */
d4f3574e 503
10d6c8cd 504static void
28439f5e
PA
505i386_linux_fetch_inferior_registers (struct target_ops *ops,
506 struct regcache *regcache, int regno)
d4f3574e 507{
ed9a39eb
JM
508 int tid;
509
f60300e7
MK
510 /* Use the old method of peeking around in `struct user' if the
511 GETREGS request isn't available. */
ce556f85 512 if (!have_ptrace_getregs)
f60300e7 513 {
ce556f85
MK
514 int i;
515
875f8d0e 516 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ce556f85 517 if (regno == -1 || regno == i)
56be3814 518 fetch_register (regcache, i);
ce556f85 519
f60300e7
MK
520 return;
521 }
522
a4b6fc86 523 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
524 tid = TIDGET (inferior_ptid);
525 if (tid == 0)
526 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 527
6ce2ac0b 528 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
04cd15b6 529 transfers more registers in one system call, and we'll cache the
6ce2ac0b 530 results. But remember that fetch_fpxregs can fail, and return
04cd15b6 531 zero. */
5c44784c
JM
532 if (regno == -1)
533 {
56be3814 534 fetch_regs (regcache, tid);
f60300e7
MK
535
536 /* The call above might reset `have_ptrace_getregs'. */
ce556f85 537 if (!have_ptrace_getregs)
f60300e7 538 {
84e473c8 539 i386_linux_fetch_inferior_registers (ops, regcache, regno);
f60300e7
MK
540 return;
541 }
542
c131fcee
L
543 if (fetch_xstateregs (regcache, tid))
544 return;
56be3814 545 if (fetch_fpxregs (regcache, tid))
5c44784c 546 return;
56be3814 547 fetch_fpregs (regcache, tid);
5c44784c
JM
548 return;
549 }
d4f3574e 550
5c44784c
JM
551 if (GETREGS_SUPPLIES (regno))
552 {
56be3814 553 fetch_regs (regcache, tid);
5c44784c
JM
554 return;
555 }
556
c131fcee
L
557 if (GETXSTATEREGS_SUPPLIES (regno))
558 {
559 if (fetch_xstateregs (regcache, tid))
560 return;
561 }
562
6ce2ac0b 563 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 564 {
56be3814 565 if (fetch_fpxregs (regcache, tid))
5c44784c
JM
566 return;
567
568 /* Either our processor or our kernel doesn't support the SSE
569 registers, so read the FP registers in the traditional way,
570 and fill the SSE registers with dummy values. It would be
571 more graceful to handle differences in the register set using
572 gdbarch. Until then, this will at least make things work
573 plausibly. */
56be3814 574 fetch_fpregs (regcache, tid);
5c44784c
JM
575 return;
576 }
577
8e65ff28 578 internal_error (__FILE__, __LINE__,
e2e0b3e5 579 _("Got request for bad register number %d."), regno);
d4f3574e
SS
580}
581
04cd15b6
MK
582/* Store register REGNO back into the child process. If REGNO is -1,
583 do this for all registers (including the floating point and SSE
584 registers). */
10d6c8cd 585static void
28439f5e
PA
586i386_linux_store_inferior_registers (struct target_ops *ops,
587 struct regcache *regcache, int regno)
d4f3574e 588{
ed9a39eb
JM
589 int tid;
590
f60300e7
MK
591 /* Use the old method of poking around in `struct user' if the
592 SETREGS request isn't available. */
ce556f85 593 if (!have_ptrace_getregs)
f60300e7 594 {
ce556f85
MK
595 int i;
596
875f8d0e 597 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ce556f85 598 if (regno == -1 || regno == i)
56be3814 599 store_register (regcache, i);
ce556f85 600
f60300e7
MK
601 return;
602 }
603
a4b6fc86 604 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
605 tid = TIDGET (inferior_ptid);
606 if (tid == 0)
607 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 608
6ce2ac0b 609 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
04cd15b6 610 transfers more registers in one system call. But remember that
6ce2ac0b 611 store_fpxregs can fail, and return zero. */
5c44784c
JM
612 if (regno == -1)
613 {
56be3814 614 store_regs (regcache, tid, regno);
c131fcee
L
615 if (store_xstateregs (regcache, tid, regno))
616 return;
56be3814 617 if (store_fpxregs (regcache, tid, regno))
5c44784c 618 return;
56be3814 619 store_fpregs (regcache, tid, regno);
5c44784c
JM
620 return;
621 }
d4f3574e 622
5c44784c
JM
623 if (GETREGS_SUPPLIES (regno))
624 {
56be3814 625 store_regs (regcache, tid, regno);
5c44784c
JM
626 return;
627 }
628
c131fcee
L
629 if (GETXSTATEREGS_SUPPLIES (regno))
630 {
631 if (store_xstateregs (regcache, tid, regno))
632 return;
633 }
634
6ce2ac0b 635 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 636 {
56be3814 637 if (store_fpxregs (regcache, tid, regno))
5c44784c
JM
638 return;
639
640 /* Either our processor or our kernel doesn't support the SSE
04cd15b6
MK
641 registers, so just write the FP registers in the traditional
642 way. */
56be3814 643 store_fpregs (regcache, tid, regno);
5c44784c
JM
644 return;
645 }
646
8e65ff28 647 internal_error (__FILE__, __LINE__,
e2e0b3e5 648 _("Got request to store bad register number %d."), regno);
d4f3574e 649}
de57eccd 650\f
6ce2ac0b 651
4ffc8466
MK
652/* Support for debug registers. */
653
9f0bdab8
DJ
654static unsigned long i386_linux_dr[DR_CONTROL + 1];
655
a79d3c27
JK
656/* Get debug register REGNUM value from only the one LWP of PTID. */
657
7bf0983e 658static unsigned long
9f0bdab8 659i386_linux_dr_get (ptid_t ptid, int regnum)
84346e11
MK
660{
661 int tid;
7bf0983e 662 unsigned long value;
84346e11 663
9f0bdab8
DJ
664 tid = TIDGET (ptid);
665 if (tid == 0)
666 tid = PIDGET (ptid);
84346e11
MK
667
668 errno = 0;
ce556f85 669 value = ptrace (PTRACE_PEEKUSER, tid,
84346e11
MK
670 offsetof (struct user, u_debugreg[regnum]), 0);
671 if (errno != 0)
e2e0b3e5 672 perror_with_name (_("Couldn't read debug register"));
84346e11
MK
673
674 return value;
675}
676
a79d3c27
JK
677/* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
678
84346e11 679static void
9f0bdab8 680i386_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
84346e11
MK
681{
682 int tid;
683
9f0bdab8
DJ
684 tid = TIDGET (ptid);
685 if (tid == 0)
686 tid = PIDGET (ptid);
84346e11
MK
687
688 errno = 0;
ce556f85 689 ptrace (PTRACE_POKEUSER, tid,
84346e11
MK
690 offsetof (struct user, u_debugreg[regnum]), value);
691 if (errno != 0)
e2e0b3e5 692 perror_with_name (_("Couldn't write debug register"));
84346e11
MK
693}
694
a79d3c27
JK
695/* Set DR_CONTROL to ADDR in all LWPs of LWP_LIST. */
696
9bb9e8ad 697static void
7bf0983e 698i386_linux_dr_set_control (unsigned long control)
84346e11 699{
9f0bdab8 700 struct lwp_info *lp;
9f0bdab8
DJ
701
702 i386_linux_dr[DR_CONTROL] = control;
4c38200f
PA
703 ALL_LWPS (lp)
704 i386_linux_dr_set (lp->ptid, DR_CONTROL, control);
84346e11
MK
705}
706
a79d3c27
JK
707/* Set address REGNUM (zero based) to ADDR in all LWPs of LWP_LIST. */
708
9bb9e8ad 709static void
84346e11
MK
710i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
711{
9f0bdab8 712 struct lwp_info *lp;
9f0bdab8 713
84346e11
MK
714 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
715
9f0bdab8 716 i386_linux_dr[DR_FIRSTADDR + regnum] = addr;
4c38200f
PA
717 ALL_LWPS (lp)
718 i386_linux_dr_set (lp->ptid, DR_FIRSTADDR + regnum, addr);
84346e11
MK
719}
720
a79d3c27
JK
721/* Set address REGNUM (zero based) to zero in all LWPs of LWP_LIST. */
722
9bb9e8ad 723static void
84346e11
MK
724i386_linux_dr_reset_addr (int regnum)
725{
9f0bdab8 726 i386_linux_dr_set_addr (regnum, 0);
84346e11
MK
727}
728
a79d3c27
JK
729/* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
730
9bb9e8ad 731static unsigned long
84346e11
MK
732i386_linux_dr_get_status (void)
733{
9f0bdab8
DJ
734 return i386_linux_dr_get (inferior_ptid, DR_STATUS);
735}
736
a79d3c27
JK
737/* Unset MASK bits in DR_STATUS in all LWPs of LWP_LIST. */
738
739static void
740i386_linux_dr_unset_status (unsigned long mask)
741{
742 struct lwp_info *lp;
a79d3c27 743
4c38200f 744 ALL_LWPS (lp)
a79d3c27
JK
745 {
746 unsigned long value;
4c38200f
PA
747
748 value = i386_linux_dr_get (lp->ptid, DR_STATUS);
a79d3c27 749 value &= ~mask;
4c38200f 750 i386_linux_dr_set (lp->ptid, DR_STATUS, value);
a79d3c27
JK
751 }
752}
753
9f0bdab8
DJ
754static void
755i386_linux_new_thread (ptid_t ptid)
756{
757 int i;
758
759 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
760 i386_linux_dr_set (ptid, i, i386_linux_dr[i]);
761
762 i386_linux_dr_set (ptid, DR_CONTROL, i386_linux_dr[DR_CONTROL]);
84346e11
MK
763}
764\f
765
5bca7895
MK
766/* Called by libthread_db. Returns a pointer to the thread local
767 storage (or its descriptor). */
768
769ps_err_e
770ps_get_thread_area (const struct ps_prochandle *ph,
771 lwpid_t lwpid, int idx, void **base)
772{
773 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
774 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
775 4 byte integers in size: `entry_number', `base_addr', `limit',
776 and a bunch of status bits.
777
778 The values returned by this ptrace call should be part of the
779 regcache buffer, and ps_get_thread_area should channel its
780 request through the regcache. That way remote targets could
781 provide the value using the remote protocol and not this direct
782 call.
783
784 Is this function needed? I'm guessing that the `base' is the
766062f6 785 address of a descriptor that libthread_db uses to find the
b2fa5097 786 thread local address base that GDB needs. Perhaps that
5bca7895
MK
787 descriptor is defined by the ABI. Anyway, given that
788 libthread_db calls this function without prompting (gdb
789 requesting tls base) I guess it needs info in there anyway. */
790 unsigned int desc[4];
791 gdb_assert (sizeof (int) == 4);
792
793#ifndef PTRACE_GET_THREAD_AREA
794#define PTRACE_GET_THREAD_AREA 25
795#endif
796
797 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
798 (void *) idx, (unsigned long) &desc) < 0)
799 return PS_ERR;
800
801 *(int *)base = desc[1];
802 return PS_OK;
803}
804\f
805
a4b6fc86 806/* The instruction for a GNU/Linux system call is:
a6abb2c0
MK
807 int $0x80
808 or 0xcd 0x80. */
809
810static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
811
812#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
813
814/* The system call number is stored in the %eax register. */
7532965f 815#define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
a6abb2c0
MK
816
817/* We are specifically interested in the sigreturn and rt_sigreturn
818 system calls. */
819
820#ifndef SYS_sigreturn
821#define SYS_sigreturn 0x77
822#endif
823#ifndef SYS_rt_sigreturn
824#define SYS_rt_sigreturn 0xad
825#endif
826
827/* Offset to saved processor flags, from <asm/sigcontext.h>. */
828#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
829
830/* Resume execution of the inferior process.
831 If STEP is nonzero, single-step it.
832 If SIGNAL is nonzero, give it that signal. */
833
10d6c8cd 834static void
28439f5e
PA
835i386_linux_resume (struct target_ops *ops,
836 ptid_t ptid, int step, enum target_signal signal)
a6abb2c0 837{
39f77062
KB
838 int pid = PIDGET (ptid);
839
a96d9b2e
SDJ
840 int request;
841
842 if (catch_syscall_enabled () > 0)
843 request = PTRACE_SYSCALL;
844 else
845 request = PTRACE_CONT;
a6abb2c0 846
a6abb2c0
MK
847 if (step)
848 {
594f7785 849 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
e17a4113
UW
850 struct gdbarch *gdbarch = get_regcache_arch (regcache);
851 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7b86a1b8 852 ULONGEST pc;
8e70166d 853 gdb_byte buf[LINUX_SYSCALL_LEN];
a6abb2c0
MK
854
855 request = PTRACE_SINGLESTEP;
856
e17a4113
UW
857 regcache_cooked_read_unsigned (regcache,
858 gdbarch_pc_regnum (gdbarch), &pc);
7b86a1b8 859
a6abb2c0
MK
860 /* Returning from a signal trampoline is done by calling a
861 special system call (sigreturn or rt_sigreturn, see
862 i386-linux-tdep.c for more information). This system call
863 restores the registers that were saved when the signal was
864 raised, including %eflags. That means that single-stepping
865 won't work. Instead, we'll have to modify the signal context
866 that's about to be restored, and set the trace flag there. */
867
868 /* First check if PC is at a system call. */
8defab1a 869 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
a6abb2c0
MK
870 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
871 {
7b86a1b8
UW
872 ULONGEST syscall;
873 regcache_cooked_read_unsigned (regcache,
874 LINUX_SYSCALL_REGNUM, &syscall);
a6abb2c0
MK
875
876 /* Then check the system call number. */
877 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
878 {
7b86a1b8 879 ULONGEST sp, addr;
a6abb2c0 880 unsigned long int eflags;
7bf0983e 881
7b86a1b8 882 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
a6abb2c0 883 if (syscall == SYS_rt_sigreturn)
f3d6df6d
YQ
884 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order)
885 + 20;
7b86a1b8
UW
886 else
887 addr = sp;
a6abb2c0
MK
888
889 /* Set the trace flag in the context that's about to be
890 restored. */
891 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
8e70166d 892 read_memory (addr, (gdb_byte *) &eflags, 4);
a6abb2c0 893 eflags |= 0x0100;
8e70166d 894 write_memory (addr, (gdb_byte *) &eflags, 4);
a6abb2c0
MK
895 }
896 }
897 }
898
899 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
e2e0b3e5 900 perror_with_name (("ptrace"));
a6abb2c0 901}
4de4c07c 902
10d6c8cd
DJ
903static void (*super_post_startup_inferior) (ptid_t ptid);
904
905static void
906i386_linux_child_post_startup_inferior (ptid_t ptid)
4de4c07c
DJ
907{
908 i386_cleanup_dregs ();
10d6c8cd
DJ
909 super_post_startup_inferior (ptid);
910}
911
90884b2b
L
912/* Get Linux/x86 target description from running target. */
913
914static const struct target_desc *
915i386_linux_read_description (struct target_ops *ops)
916{
3a13a53b 917 int tid;
c131fcee
L
918 static uint64_t xcr0;
919
3a13a53b
L
920 /* GNU/Linux LWP ID's are process ID's. */
921 tid = TIDGET (inferior_ptid);
922 if (tid == 0)
923 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
924
925#ifdef HAVE_PTRACE_GETFPXREGS
926 if (have_ptrace_getfpxregs == -1)
927 {
928 elf_fpxregset_t fpxregs;
929
930 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
931 {
932 have_ptrace_getfpxregs = 0;
933 have_ptrace_getregset = 0;
934 return tdesc_i386_mmx_linux;
935 }
936 }
937#endif
938
c131fcee
L
939 if (have_ptrace_getregset == -1)
940 {
c131fcee
L
941 uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
942 struct iovec iov;
943
c131fcee
L
944 iov.iov_base = xstateregs;
945 iov.iov_len = sizeof (xstateregs);
946
947 /* Check if PTRACE_GETREGSET works. */
948 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
949 &iov) < 0)
950 have_ptrace_getregset = 0;
951 else
952 {
953 have_ptrace_getregset = 1;
954
955 /* Get XCR0 from XSAVE extended state. */
956 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
957 / sizeof (long long))];
958 }
959 }
960
961 /* Check the native XCR0 only if PTRACE_GETREGSET is available. */
962 if (have_ptrace_getregset
963 && (xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK)
964 return tdesc_i386_avx_linux;
965 else
966 return tdesc_i386_linux;
90884b2b
L
967}
968
10d6c8cd
DJ
969void
970_initialize_i386_linux_nat (void)
971{
972 struct target_ops *t;
973
974 /* Fill in the generic GNU/Linux methods. */
975 t = linux_target ();
976
c03374d5
DJ
977 i386_use_watchpoints (t);
978
9bb9e8ad
PM
979 i386_dr_low.set_control = i386_linux_dr_set_control;
980 i386_dr_low.set_addr = i386_linux_dr_set_addr;
981 i386_dr_low.reset_addr = i386_linux_dr_reset_addr;
982 i386_dr_low.get_status = i386_linux_dr_get_status;
a79d3c27 983 i386_dr_low.unset_status = i386_linux_dr_unset_status;
9bb9e8ad
PM
984 i386_set_debug_register_length (4);
985
10d6c8cd
DJ
986 /* Override the default ptrace resume method. */
987 t->to_resume = i386_linux_resume;
988
989 /* Override the GNU/Linux inferior startup hook. */
990 super_post_startup_inferior = t->to_post_startup_inferior;
991 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
992
993 /* Add our register access methods. */
994 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
995 t->to_store_registers = i386_linux_store_inferior_registers;
996
90884b2b
L
997 t->to_read_description = i386_linux_read_description;
998
10d6c8cd 999 /* Register the target. */
f973ed9c 1000 linux_nat_add_target (t);
9f0bdab8 1001 linux_nat_set_new_thread (t, i386_linux_new_thread);
4de4c07c 1002}
This page took 0.95518 seconds and 4 git commands to generate.