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