* coffcode.h (coff_write_object_contents): Enclose all occurrences
[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,
4c38e0a4 4 2009, 2010 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
f60300e7
MK
173/* Store one register. */
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)))
7f7fe91e 219 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM,
be0d2954 220 regp + 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)))
7f7fe91e 242 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM,
be0d2954 243 regp + 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
56be3814
UW
355static void fetch_fpregs (struct regcache *regcache, int tid) {}
356static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
f60300e7
MK
357
358#endif
5c44784c 359\f
d4f3574e 360
6ce2ac0b 361/* Transfering floating-point and SSE registers to and from GDB. */
11cf8741 362
c131fcee
L
363/* Fetch all registers covered by the PTRACE_GETREGSET request from
364 process/thread TID and store their values in GDB's register array.
365 Return non-zero if successful, zero otherwise. */
366
367static int
368fetch_xstateregs (struct regcache *regcache, int tid)
369{
370 char xstateregs[I386_XSTATE_MAX_SIZE];
371 struct iovec iov;
372
373 if (!have_ptrace_getregset)
374 return 0;
375
376 iov.iov_base = xstateregs;
377 iov.iov_len = sizeof(xstateregs);
378 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
379 &iov) < 0)
380 perror_with_name (_("Couldn't read extended state status"));
381
382 i387_supply_xsave (regcache, -1, xstateregs);
383 return 1;
384}
385
386/* Store all valid registers in GDB's register array covered by the
387 PTRACE_SETREGSET request into the process/thread specified by TID.
388 Return non-zero if successful, zero otherwise. */
389
390static int
391store_xstateregs (const struct regcache *regcache, int tid, int regno)
392{
393 char xstateregs[I386_XSTATE_MAX_SIZE];
394 struct iovec iov;
395
396 if (!have_ptrace_getregset)
397 return 0;
398
399 iov.iov_base = xstateregs;
400 iov.iov_len = sizeof(xstateregs);
401 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
402 &iov) < 0)
403 perror_with_name (_("Couldn't read extended state status"));
404
405 i387_collect_xsave (regcache, regno, xstateregs, 0);
406
407 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
408 (int) &iov) < 0)
409 perror_with_name (_("Couldn't write extended state status"));
410
411 return 1;
412}
413
6ce2ac0b 414#ifdef HAVE_PTRACE_GETFPXREGS
04cd15b6
MK
415
416/* Fill GDB's register array with the floating-point and SSE register
6ce2ac0b 417 values in *FPXREGSETP. */
04cd15b6 418
975aec09 419void
7f7fe91e
UW
420supply_fpxregset (struct regcache *regcache,
421 const elf_fpxregset_t *fpxregsetp)
d4f3574e 422{
7f7fe91e 423 i387_supply_fxsave (regcache, -1, fpxregsetp);
d4f3574e
SS
424}
425
6ce2ac0b
MK
426/* Fill register REGNO (if it is a floating-point or SSE register) in
427 *FPXREGSETP with the value in GDB's register array. If REGNO is
428 -1, do this for all registers. */
d4f3574e 429
975aec09 430void
7f7fe91e
UW
431fill_fpxregset (const struct regcache *regcache,
432 elf_fpxregset_t *fpxregsetp, int regno)
d4f3574e 433{
7f7fe91e 434 i387_collect_fxsave (regcache, regno, fpxregsetp);
5c44784c
JM
435}
436
6ce2ac0b 437/* Fetch all registers covered by the PTRACE_GETFPXREGS request from
04cd15b6
MK
438 process/thread TID and store their values in GDB's register array.
439 Return non-zero if successful, zero otherwise. */
5c44784c 440
5c44784c 441static int
56be3814 442fetch_fpxregs (struct regcache *regcache, int tid)
5c44784c 443{
6ce2ac0b 444 elf_fpxregset_t fpxregs;
5c44784c 445
6ce2ac0b 446 if (! have_ptrace_getfpxregs)
5c44784c
JM
447 return 0;
448
6ce2ac0b 449 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
d4f3574e 450 {
5c44784c
JM
451 if (errno == EIO)
452 {
6ce2ac0b 453 have_ptrace_getfpxregs = 0;
5c44784c
JM
454 return 0;
455 }
456
e2e0b3e5 457 perror_with_name (_("Couldn't read floating-point and SSE registers"));
d4f3574e
SS
458 }
459
56be3814 460 supply_fpxregset (regcache, (const elf_fpxregset_t *) &fpxregs);
5c44784c
JM
461 return 1;
462}
d4f3574e 463
04cd15b6 464/* Store all valid registers in GDB's register array covered by the
6ce2ac0b 465 PTRACE_SETFPXREGS request into the process/thread specified by TID.
04cd15b6 466 Return non-zero if successful, zero otherwise. */
5c44784c 467
5c44784c 468static int
56be3814 469store_fpxregs (const struct regcache *regcache, int tid, int regno)
5c44784c 470{
6ce2ac0b 471 elf_fpxregset_t fpxregs;
5c44784c 472
6ce2ac0b 473 if (! have_ptrace_getfpxregs)
5c44784c 474 return 0;
6ce2ac0b
MK
475
476 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
2866d305
MK
477 {
478 if (errno == EIO)
479 {
480 have_ptrace_getfpxregs = 0;
481 return 0;
482 }
483
e2e0b3e5 484 perror_with_name (_("Couldn't read floating-point and SSE registers"));
2866d305 485 }
5c44784c 486
56be3814 487 fill_fpxregset (regcache, &fpxregs, regno);
5c44784c 488
6ce2ac0b 489 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
e2e0b3e5 490 perror_with_name (_("Couldn't write floating-point and SSE registers"));
5c44784c
JM
491
492 return 1;
493}
494
5c44784c
JM
495#else
496
56be3814
UW
497static int fetch_fpxregs (struct regcache *regcache, int tid) { return 0; }
498static int store_fpxregs (const struct regcache *regcache, int tid, int regno) { return 0; }
5c44784c 499
6ce2ac0b 500#endif /* HAVE_PTRACE_GETFPXREGS */
5c44784c 501\f
6ce2ac0b 502
5c44784c 503/* Transferring arbitrary registers between GDB and inferior. */
d4f3574e 504
04cd15b6
MK
505/* Fetch register REGNO from the child process. If REGNO is -1, do
506 this for all registers (including the floating point and SSE
507 registers). */
d4f3574e 508
10d6c8cd 509static void
28439f5e
PA
510i386_linux_fetch_inferior_registers (struct target_ops *ops,
511 struct regcache *regcache, int regno)
d4f3574e 512{
ed9a39eb
JM
513 int tid;
514
f60300e7
MK
515 /* Use the old method of peeking around in `struct user' if the
516 GETREGS request isn't available. */
ce556f85 517 if (!have_ptrace_getregs)
f60300e7 518 {
ce556f85
MK
519 int i;
520
875f8d0e 521 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ce556f85 522 if (regno == -1 || regno == i)
56be3814 523 fetch_register (regcache, i);
ce556f85 524
f60300e7
MK
525 return;
526 }
527
a4b6fc86 528 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
529 tid = TIDGET (inferior_ptid);
530 if (tid == 0)
531 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 532
6ce2ac0b 533 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
04cd15b6 534 transfers more registers in one system call, and we'll cache the
6ce2ac0b 535 results. But remember that fetch_fpxregs can fail, and return
04cd15b6 536 zero. */
5c44784c
JM
537 if (regno == -1)
538 {
56be3814 539 fetch_regs (regcache, tid);
f60300e7
MK
540
541 /* The call above might reset `have_ptrace_getregs'. */
ce556f85 542 if (!have_ptrace_getregs)
f60300e7 543 {
84e473c8 544 i386_linux_fetch_inferior_registers (ops, regcache, regno);
f60300e7
MK
545 return;
546 }
547
c131fcee
L
548 if (fetch_xstateregs (regcache, tid))
549 return;
56be3814 550 if (fetch_fpxregs (regcache, tid))
5c44784c 551 return;
56be3814 552 fetch_fpregs (regcache, tid);
5c44784c
JM
553 return;
554 }
d4f3574e 555
5c44784c
JM
556 if (GETREGS_SUPPLIES (regno))
557 {
56be3814 558 fetch_regs (regcache, tid);
5c44784c
JM
559 return;
560 }
561
c131fcee
L
562 if (GETXSTATEREGS_SUPPLIES (regno))
563 {
564 if (fetch_xstateregs (regcache, tid))
565 return;
566 }
567
6ce2ac0b 568 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 569 {
56be3814 570 if (fetch_fpxregs (regcache, tid))
5c44784c
JM
571 return;
572
573 /* Either our processor or our kernel doesn't support the SSE
574 registers, so read the FP registers in the traditional way,
575 and fill the SSE registers with dummy values. It would be
576 more graceful to handle differences in the register set using
577 gdbarch. Until then, this will at least make things work
578 plausibly. */
56be3814 579 fetch_fpregs (regcache, tid);
5c44784c
JM
580 return;
581 }
582
8e65ff28 583 internal_error (__FILE__, __LINE__,
e2e0b3e5 584 _("Got request for bad register number %d."), regno);
d4f3574e
SS
585}
586
04cd15b6
MK
587/* Store register REGNO back into the child process. If REGNO is -1,
588 do this for all registers (including the floating point and SSE
589 registers). */
10d6c8cd 590static void
28439f5e
PA
591i386_linux_store_inferior_registers (struct target_ops *ops,
592 struct regcache *regcache, int regno)
d4f3574e 593{
ed9a39eb
JM
594 int tid;
595
f60300e7
MK
596 /* Use the old method of poking around in `struct user' if the
597 SETREGS request isn't available. */
ce556f85 598 if (!have_ptrace_getregs)
f60300e7 599 {
ce556f85
MK
600 int i;
601
875f8d0e 602 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ce556f85 603 if (regno == -1 || regno == i)
56be3814 604 store_register (regcache, i);
ce556f85 605
f60300e7
MK
606 return;
607 }
608
a4b6fc86 609 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
610 tid = TIDGET (inferior_ptid);
611 if (tid == 0)
612 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 613
6ce2ac0b 614 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
04cd15b6 615 transfers more registers in one system call. But remember that
6ce2ac0b 616 store_fpxregs can fail, and return zero. */
5c44784c
JM
617 if (regno == -1)
618 {
56be3814 619 store_regs (regcache, tid, regno);
c131fcee
L
620 if (store_xstateregs (regcache, tid, regno))
621 return;
56be3814 622 if (store_fpxregs (regcache, tid, regno))
5c44784c 623 return;
56be3814 624 store_fpregs (regcache, tid, regno);
5c44784c
JM
625 return;
626 }
d4f3574e 627
5c44784c
JM
628 if (GETREGS_SUPPLIES (regno))
629 {
56be3814 630 store_regs (regcache, tid, regno);
5c44784c
JM
631 return;
632 }
633
c131fcee
L
634 if (GETXSTATEREGS_SUPPLIES (regno))
635 {
636 if (store_xstateregs (regcache, tid, regno))
637 return;
638 }
639
6ce2ac0b 640 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 641 {
56be3814 642 if (store_fpxregs (regcache, tid, regno))
5c44784c
JM
643 return;
644
645 /* Either our processor or our kernel doesn't support the SSE
04cd15b6
MK
646 registers, so just write the FP registers in the traditional
647 way. */
56be3814 648 store_fpregs (regcache, tid, regno);
5c44784c
JM
649 return;
650 }
651
8e65ff28 652 internal_error (__FILE__, __LINE__,
e2e0b3e5 653 _("Got request to store bad register number %d."), regno);
d4f3574e 654}
de57eccd 655\f
6ce2ac0b 656
4ffc8466
MK
657/* Support for debug registers. */
658
9f0bdab8
DJ
659static unsigned long i386_linux_dr[DR_CONTROL + 1];
660
a79d3c27
JK
661/* Get debug register REGNUM value from only the one LWP of PTID. */
662
7bf0983e 663static unsigned long
9f0bdab8 664i386_linux_dr_get (ptid_t ptid, int regnum)
84346e11
MK
665{
666 int tid;
7bf0983e 667 unsigned long value;
84346e11 668
9f0bdab8
DJ
669 tid = TIDGET (ptid);
670 if (tid == 0)
671 tid = PIDGET (ptid);
84346e11 672
b9511b9a
MK
673 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
674 ptrace call fails breaks debugging remote targets. The correct
675 way to fix this is to add the hardware breakpoint and watchpoint
7532965f 676 stuff to the target vector. For now, just return zero if the
b9511b9a 677 ptrace call fails. */
84346e11 678 errno = 0;
ce556f85 679 value = ptrace (PTRACE_PEEKUSER, tid,
84346e11
MK
680 offsetof (struct user, u_debugreg[regnum]), 0);
681 if (errno != 0)
b9511b9a 682#if 0
e2e0b3e5 683 perror_with_name (_("Couldn't read debug register"));
b9511b9a
MK
684#else
685 return 0;
686#endif
84346e11
MK
687
688 return value;
689}
690
a79d3c27
JK
691/* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
692
84346e11 693static void
9f0bdab8 694i386_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
84346e11
MK
695{
696 int tid;
697
9f0bdab8
DJ
698 tid = TIDGET (ptid);
699 if (tid == 0)
700 tid = PIDGET (ptid);
84346e11
MK
701
702 errno = 0;
ce556f85 703 ptrace (PTRACE_POKEUSER, tid,
84346e11
MK
704 offsetof (struct user, u_debugreg[regnum]), value);
705 if (errno != 0)
e2e0b3e5 706 perror_with_name (_("Couldn't write debug register"));
84346e11
MK
707}
708
a79d3c27
JK
709/* Set DR_CONTROL to ADDR in all LWPs of LWP_LIST. */
710
9bb9e8ad 711static void
7bf0983e 712i386_linux_dr_set_control (unsigned long control)
84346e11 713{
9f0bdab8
DJ
714 struct lwp_info *lp;
715 ptid_t ptid;
716
717 i386_linux_dr[DR_CONTROL] = control;
718 ALL_LWPS (lp, ptid)
719 i386_linux_dr_set (ptid, DR_CONTROL, control);
84346e11
MK
720}
721
a79d3c27
JK
722/* Set address REGNUM (zero based) to ADDR in all LWPs of LWP_LIST. */
723
9bb9e8ad 724static void
84346e11
MK
725i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
726{
9f0bdab8
DJ
727 struct lwp_info *lp;
728 ptid_t ptid;
729
84346e11
MK
730 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
731
9f0bdab8
DJ
732 i386_linux_dr[DR_FIRSTADDR + regnum] = addr;
733 ALL_LWPS (lp, ptid)
734 i386_linux_dr_set (ptid, DR_FIRSTADDR + regnum, addr);
84346e11
MK
735}
736
a79d3c27
JK
737/* Set address REGNUM (zero based) to zero in all LWPs of LWP_LIST. */
738
9bb9e8ad 739static void
84346e11
MK
740i386_linux_dr_reset_addr (int regnum)
741{
9f0bdab8 742 i386_linux_dr_set_addr (regnum, 0);
84346e11
MK
743}
744
a79d3c27
JK
745/* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
746
9bb9e8ad 747static unsigned long
84346e11
MK
748i386_linux_dr_get_status (void)
749{
9f0bdab8
DJ
750 return i386_linux_dr_get (inferior_ptid, DR_STATUS);
751}
752
a79d3c27
JK
753/* Unset MASK bits in DR_STATUS in all LWPs of LWP_LIST. */
754
755static void
756i386_linux_dr_unset_status (unsigned long mask)
757{
758 struct lwp_info *lp;
759 ptid_t ptid;
760
761 ALL_LWPS (lp, ptid)
762 {
763 unsigned long value;
764
765 value = i386_linux_dr_get (ptid, DR_STATUS);
766 value &= ~mask;
767 i386_linux_dr_set (ptid, DR_STATUS, value);
768 }
769}
770
9f0bdab8
DJ
771static void
772i386_linux_new_thread (ptid_t ptid)
773{
774 int i;
775
776 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
777 i386_linux_dr_set (ptid, i, i386_linux_dr[i]);
778
779 i386_linux_dr_set (ptid, DR_CONTROL, i386_linux_dr[DR_CONTROL]);
84346e11
MK
780}
781\f
782
5bca7895
MK
783/* Called by libthread_db. Returns a pointer to the thread local
784 storage (or its descriptor). */
785
786ps_err_e
787ps_get_thread_area (const struct ps_prochandle *ph,
788 lwpid_t lwpid, int idx, void **base)
789{
790 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
791 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
792 4 byte integers in size: `entry_number', `base_addr', `limit',
793 and a bunch of status bits.
794
795 The values returned by this ptrace call should be part of the
796 regcache buffer, and ps_get_thread_area should channel its
797 request through the regcache. That way remote targets could
798 provide the value using the remote protocol and not this direct
799 call.
800
801 Is this function needed? I'm guessing that the `base' is the
802 address of a a descriptor that libthread_db uses to find the
b2fa5097 803 thread local address base that GDB needs. Perhaps that
5bca7895
MK
804 descriptor is defined by the ABI. Anyway, given that
805 libthread_db calls this function without prompting (gdb
806 requesting tls base) I guess it needs info in there anyway. */
807 unsigned int desc[4];
808 gdb_assert (sizeof (int) == 4);
809
810#ifndef PTRACE_GET_THREAD_AREA
811#define PTRACE_GET_THREAD_AREA 25
812#endif
813
814 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
815 (void *) idx, (unsigned long) &desc) < 0)
816 return PS_ERR;
817
818 *(int *)base = desc[1];
819 return PS_OK;
820}
821\f
822
a4b6fc86 823/* The instruction for a GNU/Linux system call is:
a6abb2c0
MK
824 int $0x80
825 or 0xcd 0x80. */
826
827static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
828
829#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
830
831/* The system call number is stored in the %eax register. */
7532965f 832#define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
a6abb2c0
MK
833
834/* We are specifically interested in the sigreturn and rt_sigreturn
835 system calls. */
836
837#ifndef SYS_sigreturn
838#define SYS_sigreturn 0x77
839#endif
840#ifndef SYS_rt_sigreturn
841#define SYS_rt_sigreturn 0xad
842#endif
843
844/* Offset to saved processor flags, from <asm/sigcontext.h>. */
845#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
846
847/* Resume execution of the inferior process.
848 If STEP is nonzero, single-step it.
849 If SIGNAL is nonzero, give it that signal. */
850
10d6c8cd 851static void
28439f5e
PA
852i386_linux_resume (struct target_ops *ops,
853 ptid_t ptid, int step, enum target_signal signal)
a6abb2c0 854{
39f77062
KB
855 int pid = PIDGET (ptid);
856
a96d9b2e
SDJ
857 int request;
858
859 if (catch_syscall_enabled () > 0)
860 request = PTRACE_SYSCALL;
861 else
862 request = PTRACE_CONT;
a6abb2c0 863
a6abb2c0
MK
864 if (step)
865 {
594f7785 866 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
e17a4113
UW
867 struct gdbarch *gdbarch = get_regcache_arch (regcache);
868 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7b86a1b8 869 ULONGEST pc;
8e70166d 870 gdb_byte buf[LINUX_SYSCALL_LEN];
a6abb2c0
MK
871
872 request = PTRACE_SINGLESTEP;
873
e17a4113
UW
874 regcache_cooked_read_unsigned (regcache,
875 gdbarch_pc_regnum (gdbarch), &pc);
7b86a1b8 876
a6abb2c0
MK
877 /* Returning from a signal trampoline is done by calling a
878 special system call (sigreturn or rt_sigreturn, see
879 i386-linux-tdep.c for more information). This system call
880 restores the registers that were saved when the signal was
881 raised, including %eflags. That means that single-stepping
882 won't work. Instead, we'll have to modify the signal context
883 that's about to be restored, and set the trace flag there. */
884
885 /* First check if PC is at a system call. */
8defab1a 886 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
a6abb2c0
MK
887 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
888 {
7b86a1b8
UW
889 ULONGEST syscall;
890 regcache_cooked_read_unsigned (regcache,
891 LINUX_SYSCALL_REGNUM, &syscall);
a6abb2c0
MK
892
893 /* Then check the system call number. */
894 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
895 {
7b86a1b8 896 ULONGEST sp, addr;
a6abb2c0 897 unsigned long int eflags;
7bf0983e 898
7b86a1b8 899 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
a6abb2c0 900 if (syscall == SYS_rt_sigreturn)
e17a4113 901 addr = read_memory_integer (sp + 8, 4, byte_order) + 20;
7b86a1b8
UW
902 else
903 addr = sp;
a6abb2c0
MK
904
905 /* Set the trace flag in the context that's about to be
906 restored. */
907 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
8e70166d 908 read_memory (addr, (gdb_byte *) &eflags, 4);
a6abb2c0 909 eflags |= 0x0100;
8e70166d 910 write_memory (addr, (gdb_byte *) &eflags, 4);
a6abb2c0
MK
911 }
912 }
913 }
914
915 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
e2e0b3e5 916 perror_with_name (("ptrace"));
a6abb2c0 917}
4de4c07c 918
10d6c8cd
DJ
919static void (*super_post_startup_inferior) (ptid_t ptid);
920
921static void
922i386_linux_child_post_startup_inferior (ptid_t ptid)
4de4c07c
DJ
923{
924 i386_cleanup_dregs ();
10d6c8cd
DJ
925 super_post_startup_inferior (ptid);
926}
927
90884b2b
L
928/* Get Linux/x86 target description from running target. */
929
930static const struct target_desc *
931i386_linux_read_description (struct target_ops *ops)
932{
3a13a53b 933 int tid;
c131fcee
L
934 static uint64_t xcr0;
935
3a13a53b
L
936 /* GNU/Linux LWP ID's are process ID's. */
937 tid = TIDGET (inferior_ptid);
938 if (tid == 0)
939 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
940
941#ifdef HAVE_PTRACE_GETFPXREGS
942 if (have_ptrace_getfpxregs == -1)
943 {
944 elf_fpxregset_t fpxregs;
945
946 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
947 {
948 have_ptrace_getfpxregs = 0;
949 have_ptrace_getregset = 0;
950 return tdesc_i386_mmx_linux;
951 }
952 }
953#endif
954
c131fcee
L
955 if (have_ptrace_getregset == -1)
956 {
c131fcee
L
957 uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
958 struct iovec iov;
959
c131fcee
L
960 iov.iov_base = xstateregs;
961 iov.iov_len = sizeof (xstateregs);
962
963 /* Check if PTRACE_GETREGSET works. */
964 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
965 &iov) < 0)
966 have_ptrace_getregset = 0;
967 else
968 {
969 have_ptrace_getregset = 1;
970
971 /* Get XCR0 from XSAVE extended state. */
972 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
973 / sizeof (long long))];
974 }
975 }
976
977 /* Check the native XCR0 only if PTRACE_GETREGSET is available. */
978 if (have_ptrace_getregset
979 && (xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK)
980 return tdesc_i386_avx_linux;
981 else
982 return tdesc_i386_linux;
90884b2b
L
983}
984
10d6c8cd
DJ
985void
986_initialize_i386_linux_nat (void)
987{
988 struct target_ops *t;
989
990 /* Fill in the generic GNU/Linux methods. */
991 t = linux_target ();
992
c03374d5
DJ
993 i386_use_watchpoints (t);
994
9bb9e8ad
PM
995 i386_dr_low.set_control = i386_linux_dr_set_control;
996 i386_dr_low.set_addr = i386_linux_dr_set_addr;
997 i386_dr_low.reset_addr = i386_linux_dr_reset_addr;
998 i386_dr_low.get_status = i386_linux_dr_get_status;
a79d3c27 999 i386_dr_low.unset_status = i386_linux_dr_unset_status;
9bb9e8ad
PM
1000 i386_set_debug_register_length (4);
1001
10d6c8cd
DJ
1002 /* Override the default ptrace resume method. */
1003 t->to_resume = i386_linux_resume;
1004
1005 /* Override the GNU/Linux inferior startup hook. */
1006 super_post_startup_inferior = t->to_post_startup_inferior;
1007 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
1008
1009 /* Add our register access methods. */
1010 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
1011 t->to_store_registers = i386_linux_store_inferior_registers;
1012
90884b2b
L
1013 t->to_read_description = i386_linux_read_description;
1014
10d6c8cd 1015 /* Register the target. */
f973ed9c 1016 linux_nat_add_target (t);
9f0bdab8 1017 linux_nat_set_new_thread (t, i386_linux_new_thread);
4de4c07c 1018}
This page took 0.781545 seconds and 4 git commands to generate.