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