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