* MAINTAINERS: Add Jon Beniston to write after approval list.
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
CommitLineData
a4194092 1/* Native-dependent code for GNU/Linux i386.
a4b6fc86 2
0fb0cc75
JB
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009 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"
22#include "inferior.h"
23#include "gdbcore.h"
4e052eda 24#include "regcache.h"
10d6c8cd 25#include "target.h"
4de4c07c 26#include "linux-nat.h"
d4f3574e 27
84346e11 28#include "gdb_assert.h"
309367d4 29#include "gdb_string.h"
d4f3574e
SS
30#include <sys/ptrace.h>
31#include <sys/user.h>
32#include <sys/procfs.h>
33
34#ifdef HAVE_SYS_REG_H
35#include <sys/reg.h>
36#endif
37
ce556f85
MK
38#ifndef ORIG_EAX
39#define ORIG_EAX -1
40#endif
41
84346e11
MK
42#ifdef HAVE_SYS_DEBUGREG_H
43#include <sys/debugreg.h>
44#endif
45
46#ifndef DR_FIRSTADDR
47#define DR_FIRSTADDR 0
48#endif
49
50#ifndef DR_LASTADDR
51#define DR_LASTADDR 3
52#endif
53
54#ifndef DR_STATUS
55#define DR_STATUS 6
56#endif
57
58#ifndef DR_CONTROL
59#define DR_CONTROL 7
60#endif
61
6ce2ac0b 62/* Prototypes for supply_gregset etc. */
c60c0f5f
MS
63#include "gregset.h"
64
e750d25e 65#include "i387-tdep.h"
c3833324 66#include "i386-tdep.h"
5179e78f
AC
67#include "i386-linux-tdep.h"
68
b757528f
JJ
69/* Defines ps_err_e, struct ps_prochandle. */
70#include "gdb_proc_service.h"
6ce2ac0b 71\f
d4f3574e 72
a4b6fc86
AC
73/* The register sets used in GNU/Linux ELF core-dumps are identical to
74 the register sets in `struct user' that is used for a.out
75 core-dumps, and is also used by `ptrace'. The corresponding types
76 are `elf_gregset_t' for the general-purpose registers (with
04cd15b6
MK
77 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
78 for the floating-point registers.
79
80 Those types used to be available under the names `gregset_t' and
81 `fpregset_t' too, and this file used those names in the past. But
82 those names are now used for the register sets used in the
83 `mcontext_t' type, and have a different size and layout. */
84
85/* Mapping between the general-purpose registers in `struct user'
86 format and GDB's register array layout. */
d4f3574e
SS
87static int regmap[] =
88{
89 EAX, ECX, EDX, EBX,
90 UESP, EBP, ESI, EDI,
91 EIP, EFL, CS, SS,
ce556f85
MK
92 DS, ES, FS, GS,
93 -1, -1, -1, -1, /* st0, st1, st2, st3 */
94 -1, -1, -1, -1, /* st4, st5, st6, st7 */
95 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
96 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
97 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
98 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
99 -1, /* mxcsr */
100 ORIG_EAX
d4f3574e
SS
101};
102
5c44784c
JM
103/* Which ptrace request retrieves which registers?
104 These apply to the corresponding SET requests as well. */
e64a344c 105
5c44784c 106#define GETREGS_SUPPLIES(regno) \
3fb1c838 107 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
e64a344c 108
6ce2ac0b 109#define GETFPXREGS_SUPPLIES(regno) \
f6792ef4 110 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
5c44784c 111
f60300e7
MK
112/* Does the current host support the GETREGS request? */
113int have_ptrace_getregs =
114#ifdef HAVE_PTRACE_GETREGS
115 1
116#else
117 0
118#endif
119;
120
6ce2ac0b 121/* Does the current host support the GETFPXREGS request? The header
5c44784c
JM
122 file may or may not define it, and even if it is defined, the
123 kernel will return EIO if it's running on a pre-SSE processor.
124
125 My instinct is to attach this to some architecture- or
126 target-specific data structure, but really, a particular GDB
127 process can only run on top of one kernel at a time. So it's okay
128 for this to be a simple variable. */
6ce2ac0b
MK
129int have_ptrace_getfpxregs =
130#ifdef HAVE_PTRACE_GETFPXREGS
5c44784c
JM
131 1
132#else
133 0
134#endif
135;
f60300e7 136\f
6ce2ac0b 137
ce556f85 138/* Accessing registers through the U area, one at a time. */
f60300e7
MK
139
140/* Fetch one register. */
141
142static void
56be3814 143fetch_register (struct regcache *regcache, int regno)
f60300e7 144{
f60300e7 145 int tid;
ce556f85 146 int val;
f60300e7 147
ce556f85 148 gdb_assert (!have_ptrace_getregs);
de732108 149 if (regmap[regno] == -1)
f60300e7 150 {
56be3814 151 regcache_raw_supply (regcache, regno, NULL);
f60300e7
MK
152 return;
153 }
154
ce556f85 155 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
156 tid = TIDGET (inferior_ptid);
157 if (tid == 0)
158 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
f60300e7 159
ce556f85 160 errno = 0;
de732108 161 val = ptrace (PTRACE_PEEKUSER, tid, 4 * regmap[regno], 0);
ce556f85 162 if (errno != 0)
c9f4d572 163 error (_("Couldn't read register %s (#%d): %s."),
875f8d0e 164 gdbarch_register_name (get_regcache_arch (regcache), regno),
ce556f85 165 regno, safe_strerror (errno));
f60300e7 166
56be3814 167 regcache_raw_supply (regcache, regno, &val);
f60300e7
MK
168}
169
f60300e7
MK
170/* Store one register. */
171
172static void
56be3814 173store_register (const struct regcache *regcache, int regno)
f60300e7 174{
f60300e7 175 int tid;
ce556f85 176 int val;
f60300e7 177
ce556f85 178 gdb_assert (!have_ptrace_getregs);
de732108 179 if (regmap[regno] == -1)
ce556f85 180 return;
f60300e7 181
ce556f85 182 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
183 tid = TIDGET (inferior_ptid);
184 if (tid == 0)
185 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
f60300e7 186
ce556f85 187 errno = 0;
56be3814 188 regcache_raw_collect (regcache, regno, &val);
de732108 189 ptrace (PTRACE_POKEUSER, tid, 4 * regmap[regno], val);
ce556f85 190 if (errno != 0)
c9f4d572 191 error (_("Couldn't write register %s (#%d): %s."),
875f8d0e 192 gdbarch_register_name (get_regcache_arch (regcache), regno),
ce556f85 193 regno, safe_strerror (errno));
f60300e7 194}
5c44784c 195\f
6ce2ac0b 196
04cd15b6
MK
197/* Transfering the general-purpose registers between GDB, inferiors
198 and core files. */
199
ad2a4d09 200/* Fill GDB's register array with the general-purpose register values
04cd15b6 201 in *GREGSETP. */
5c44784c 202
d4f3574e 203void
7f7fe91e 204supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
d4f3574e 205{
7f7fe91e 206 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
6ce2ac0b 207 int i;
d4f3574e 208
98df6387 209 for (i = 0; i < I386_NUM_GREGS; i++)
7f7fe91e 210 regcache_raw_supply (regcache, i, regp + regmap[i]);
3fb1c838 211
875f8d0e
UW
212 if (I386_LINUX_ORIG_EAX_REGNUM
213 < gdbarch_num_regs (get_regcache_arch (regcache)))
7f7fe91e 214 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM,
31828840 215 regp + ORIG_EAX);
917317f4
JM
216}
217
04cd15b6
MK
218/* Fill register REGNO (if it is a general-purpose register) in
219 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
220 do this for all registers. */
6ce2ac0b 221
917317f4 222void
7f7fe91e
UW
223fill_gregset (const struct regcache *regcache,
224 elf_gregset_t *gregsetp, int regno)
917317f4 225{
6ce2ac0b
MK
226 elf_greg_t *regp = (elf_greg_t *) gregsetp;
227 int i;
04cd15b6 228
98df6387 229 for (i = 0; i < I386_NUM_GREGS; i++)
099a9414 230 if (regno == -1 || regno == i)
7f7fe91e 231 regcache_raw_collect (regcache, i, regp + regmap[i]);
3fb1c838 232
82ea117a 233 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
875f8d0e
UW
234 && I386_LINUX_ORIG_EAX_REGNUM
235 < gdbarch_num_regs (get_regcache_arch (regcache)))
7f7fe91e 236 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM,
31828840 237 regp + ORIG_EAX);
d4f3574e
SS
238}
239
f60300e7
MK
240#ifdef HAVE_PTRACE_GETREGS
241
04cd15b6
MK
242/* Fetch all general-purpose registers from process/thread TID and
243 store their values in GDB's register array. */
d4f3574e 244
5c44784c 245static void
56be3814 246fetch_regs (struct regcache *regcache, int tid)
5c44784c 247{
04cd15b6 248 elf_gregset_t regs;
2e024c20 249 elf_gregset_t *regs_p = &regs;
5c44784c 250
6ce2ac0b 251 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
5c44784c 252 {
f60300e7
MK
253 if (errno == EIO)
254 {
255 /* The kernel we're running on doesn't support the GETREGS
256 request. Reset `have_ptrace_getregs'. */
257 have_ptrace_getregs = 0;
258 return;
259 }
260
e2e0b3e5 261 perror_with_name (_("Couldn't get registers"));
5c44784c
JM
262 }
263
2e024c20 264 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
5c44784c
JM
265}
266
04cd15b6
MK
267/* Store all valid general-purpose registers in GDB's register array
268 into the process/thread specified by TID. */
5c44784c 269
5c44784c 270static void
56be3814 271store_regs (const struct regcache *regcache, int tid, int regno)
5c44784c 272{
04cd15b6 273 elf_gregset_t regs;
5c44784c 274
6ce2ac0b 275 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 276 perror_with_name (_("Couldn't get registers"));
5c44784c 277
56be3814 278 fill_gregset (regcache, &regs, regno);
6ce2ac0b
MK
279
280 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 281 perror_with_name (_("Couldn't write registers"));
5c44784c
JM
282}
283
f60300e7
MK
284#else
285
56be3814
UW
286static void fetch_regs (struct regcache *regcache, int tid) {}
287static void store_regs (const struct regcache *regcache, int tid, int regno) {}
f60300e7
MK
288
289#endif
5c44784c 290\f
5c44784c 291
6ce2ac0b 292/* Transfering floating-point registers between GDB, inferiors and cores. */
d4f3574e 293
04cd15b6 294/* Fill GDB's register array with the floating-point register values in
917317f4 295 *FPREGSETP. */
04cd15b6 296
d4f3574e 297void
7f7fe91e 298supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
d4f3574e 299{
7f7fe91e 300 i387_supply_fsave (regcache, -1, fpregsetp);
917317f4 301}
d4f3574e 302
04cd15b6
MK
303/* Fill register REGNO (if it is a floating-point register) in
304 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
305 do this for all registers. */
917317f4
JM
306
307void
7f7fe91e
UW
308fill_fpregset (const struct regcache *regcache,
309 elf_fpregset_t *fpregsetp, int regno)
917317f4 310{
7f7fe91e 311 i387_collect_fsave (regcache, regno, fpregsetp);
d4f3574e
SS
312}
313
f60300e7
MK
314#ifdef HAVE_PTRACE_GETREGS
315
04cd15b6
MK
316/* Fetch all floating-point registers from process/thread TID and store
317 thier values in GDB's register array. */
917317f4 318
d4f3574e 319static void
56be3814 320fetch_fpregs (struct regcache *regcache, int tid)
d4f3574e 321{
04cd15b6 322 elf_fpregset_t fpregs;
d4f3574e 323
6ce2ac0b 324 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 325 perror_with_name (_("Couldn't get floating point status"));
d4f3574e 326
56be3814 327 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
d4f3574e
SS
328}
329
04cd15b6
MK
330/* Store all valid floating-point registers in GDB's register array
331 into the process/thread specified by TID. */
d4f3574e 332
d4f3574e 333static void
56be3814 334store_fpregs (const struct regcache *regcache, int tid, int regno)
d4f3574e 335{
04cd15b6 336 elf_fpregset_t fpregs;
d4f3574e 337
6ce2ac0b 338 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 339 perror_with_name (_("Couldn't get floating point status"));
d4f3574e 340
56be3814 341 fill_fpregset (regcache, &fpregs, regno);
d4f3574e 342
6ce2ac0b 343 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 344 perror_with_name (_("Couldn't write floating point status"));
d4f3574e
SS
345}
346
f60300e7
MK
347#else
348
56be3814
UW
349static void fetch_fpregs (struct regcache *regcache, int tid) {}
350static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
f60300e7
MK
351
352#endif
5c44784c 353\f
d4f3574e 354
6ce2ac0b 355/* Transfering floating-point and SSE registers to and from GDB. */
11cf8741 356
6ce2ac0b 357#ifdef HAVE_PTRACE_GETFPXREGS
04cd15b6
MK
358
359/* Fill GDB's register array with the floating-point and SSE register
6ce2ac0b 360 values in *FPXREGSETP. */
04cd15b6 361
975aec09 362void
7f7fe91e
UW
363supply_fpxregset (struct regcache *regcache,
364 const elf_fpxregset_t *fpxregsetp)
d4f3574e 365{
7f7fe91e 366 i387_supply_fxsave (regcache, -1, fpxregsetp);
d4f3574e
SS
367}
368
6ce2ac0b
MK
369/* Fill register REGNO (if it is a floating-point or SSE register) in
370 *FPXREGSETP with the value in GDB's register array. If REGNO is
371 -1, do this for all registers. */
d4f3574e 372
975aec09 373void
7f7fe91e
UW
374fill_fpxregset (const struct regcache *regcache,
375 elf_fpxregset_t *fpxregsetp, int regno)
d4f3574e 376{
7f7fe91e 377 i387_collect_fxsave (regcache, regno, fpxregsetp);
5c44784c
JM
378}
379
6ce2ac0b 380/* Fetch all registers covered by the PTRACE_GETFPXREGS request from
04cd15b6
MK
381 process/thread TID and store their values in GDB's register array.
382 Return non-zero if successful, zero otherwise. */
5c44784c 383
5c44784c 384static int
56be3814 385fetch_fpxregs (struct regcache *regcache, int tid)
5c44784c 386{
6ce2ac0b 387 elf_fpxregset_t fpxregs;
5c44784c 388
6ce2ac0b 389 if (! have_ptrace_getfpxregs)
5c44784c
JM
390 return 0;
391
6ce2ac0b 392 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
d4f3574e 393 {
5c44784c
JM
394 if (errno == EIO)
395 {
6ce2ac0b 396 have_ptrace_getfpxregs = 0;
5c44784c
JM
397 return 0;
398 }
399
e2e0b3e5 400 perror_with_name (_("Couldn't read floating-point and SSE registers"));
d4f3574e
SS
401 }
402
56be3814 403 supply_fpxregset (regcache, (const elf_fpxregset_t *) &fpxregs);
5c44784c
JM
404 return 1;
405}
d4f3574e 406
04cd15b6 407/* Store all valid registers in GDB's register array covered by the
6ce2ac0b 408 PTRACE_SETFPXREGS request into the process/thread specified by TID.
04cd15b6 409 Return non-zero if successful, zero otherwise. */
5c44784c 410
5c44784c 411static int
56be3814 412store_fpxregs (const struct regcache *regcache, int tid, int regno)
5c44784c 413{
6ce2ac0b 414 elf_fpxregset_t fpxregs;
5c44784c 415
6ce2ac0b 416 if (! have_ptrace_getfpxregs)
5c44784c 417 return 0;
6ce2ac0b
MK
418
419 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
2866d305
MK
420 {
421 if (errno == EIO)
422 {
423 have_ptrace_getfpxregs = 0;
424 return 0;
425 }
426
e2e0b3e5 427 perror_with_name (_("Couldn't read floating-point and SSE registers"));
2866d305 428 }
5c44784c 429
56be3814 430 fill_fpxregset (regcache, &fpxregs, regno);
5c44784c 431
6ce2ac0b 432 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
e2e0b3e5 433 perror_with_name (_("Couldn't write floating-point and SSE registers"));
5c44784c
JM
434
435 return 1;
436}
437
5c44784c
JM
438#else
439
56be3814
UW
440static int fetch_fpxregs (struct regcache *regcache, int tid) { return 0; }
441static int store_fpxregs (const struct regcache *regcache, int tid, int regno) { return 0; }
5c44784c 442
6ce2ac0b 443#endif /* HAVE_PTRACE_GETFPXREGS */
5c44784c 444\f
6ce2ac0b 445
5c44784c 446/* Transferring arbitrary registers between GDB and inferior. */
d4f3574e 447
04cd15b6
MK
448/* Fetch register REGNO from the child process. If REGNO is -1, do
449 this for all registers (including the floating point and SSE
450 registers). */
d4f3574e 451
10d6c8cd 452static void
28439f5e
PA
453i386_linux_fetch_inferior_registers (struct target_ops *ops,
454 struct regcache *regcache, int regno)
d4f3574e 455{
ed9a39eb
JM
456 int tid;
457
f60300e7
MK
458 /* Use the old method of peeking around in `struct user' if the
459 GETREGS request isn't available. */
ce556f85 460 if (!have_ptrace_getregs)
f60300e7 461 {
ce556f85
MK
462 int i;
463
875f8d0e 464 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ce556f85 465 if (regno == -1 || regno == i)
56be3814 466 fetch_register (regcache, i);
ce556f85 467
f60300e7
MK
468 return;
469 }
470
a4b6fc86 471 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
472 tid = TIDGET (inferior_ptid);
473 if (tid == 0)
474 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 475
6ce2ac0b 476 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
04cd15b6 477 transfers more registers in one system call, and we'll cache the
6ce2ac0b 478 results. But remember that fetch_fpxregs can fail, and return
04cd15b6 479 zero. */
5c44784c
JM
480 if (regno == -1)
481 {
56be3814 482 fetch_regs (regcache, tid);
f60300e7
MK
483
484 /* The call above might reset `have_ptrace_getregs'. */
ce556f85 485 if (!have_ptrace_getregs)
f60300e7 486 {
84e473c8 487 i386_linux_fetch_inferior_registers (ops, regcache, regno);
f60300e7
MK
488 return;
489 }
490
56be3814 491 if (fetch_fpxregs (regcache, tid))
5c44784c 492 return;
56be3814 493 fetch_fpregs (regcache, tid);
5c44784c
JM
494 return;
495 }
d4f3574e 496
5c44784c
JM
497 if (GETREGS_SUPPLIES (regno))
498 {
56be3814 499 fetch_regs (regcache, tid);
5c44784c
JM
500 return;
501 }
502
6ce2ac0b 503 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 504 {
56be3814 505 if (fetch_fpxregs (regcache, tid))
5c44784c
JM
506 return;
507
508 /* Either our processor or our kernel doesn't support the SSE
509 registers, so read the FP registers in the traditional way,
510 and fill the SSE registers with dummy values. It would be
511 more graceful to handle differences in the register set using
512 gdbarch. Until then, this will at least make things work
513 plausibly. */
56be3814 514 fetch_fpregs (regcache, tid);
5c44784c
JM
515 return;
516 }
517
8e65ff28 518 internal_error (__FILE__, __LINE__,
e2e0b3e5 519 _("Got request for bad register number %d."), regno);
d4f3574e
SS
520}
521
04cd15b6
MK
522/* Store register REGNO back into the child process. If REGNO is -1,
523 do this for all registers (including the floating point and SSE
524 registers). */
10d6c8cd 525static void
28439f5e
PA
526i386_linux_store_inferior_registers (struct target_ops *ops,
527 struct regcache *regcache, int regno)
d4f3574e 528{
ed9a39eb
JM
529 int tid;
530
f60300e7
MK
531 /* Use the old method of poking around in `struct user' if the
532 SETREGS request isn't available. */
ce556f85 533 if (!have_ptrace_getregs)
f60300e7 534 {
ce556f85
MK
535 int i;
536
875f8d0e 537 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ce556f85 538 if (regno == -1 || regno == i)
56be3814 539 store_register (regcache, i);
ce556f85 540
f60300e7
MK
541 return;
542 }
543
a4b6fc86 544 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
545 tid = TIDGET (inferior_ptid);
546 if (tid == 0)
547 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 548
6ce2ac0b 549 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
04cd15b6 550 transfers more registers in one system call. But remember that
6ce2ac0b 551 store_fpxregs can fail, and return zero. */
5c44784c
JM
552 if (regno == -1)
553 {
56be3814
UW
554 store_regs (regcache, tid, regno);
555 if (store_fpxregs (regcache, tid, regno))
5c44784c 556 return;
56be3814 557 store_fpregs (regcache, tid, regno);
5c44784c
JM
558 return;
559 }
d4f3574e 560
5c44784c
JM
561 if (GETREGS_SUPPLIES (regno))
562 {
56be3814 563 store_regs (regcache, tid, regno);
5c44784c
JM
564 return;
565 }
566
6ce2ac0b 567 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 568 {
56be3814 569 if (store_fpxregs (regcache, tid, regno))
5c44784c
JM
570 return;
571
572 /* Either our processor or our kernel doesn't support the SSE
04cd15b6
MK
573 registers, so just write the FP registers in the traditional
574 way. */
56be3814 575 store_fpregs (regcache, tid, regno);
5c44784c
JM
576 return;
577 }
578
8e65ff28 579 internal_error (__FILE__, __LINE__,
e2e0b3e5 580 _("Got request to store bad register number %d."), regno);
d4f3574e 581}
de57eccd 582\f
6ce2ac0b 583
4ffc8466
MK
584/* Support for debug registers. */
585
9f0bdab8
DJ
586static unsigned long i386_linux_dr[DR_CONTROL + 1];
587
7bf0983e 588static unsigned long
9f0bdab8 589i386_linux_dr_get (ptid_t ptid, int regnum)
84346e11
MK
590{
591 int tid;
7bf0983e 592 unsigned long value;
84346e11 593
9f0bdab8
DJ
594 tid = TIDGET (ptid);
595 if (tid == 0)
596 tid = PIDGET (ptid);
84346e11 597
b9511b9a
MK
598 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
599 ptrace call fails breaks debugging remote targets. The correct
600 way to fix this is to add the hardware breakpoint and watchpoint
7532965f 601 stuff to the target vector. For now, just return zero if the
b9511b9a 602 ptrace call fails. */
84346e11 603 errno = 0;
ce556f85 604 value = ptrace (PTRACE_PEEKUSER, tid,
84346e11
MK
605 offsetof (struct user, u_debugreg[regnum]), 0);
606 if (errno != 0)
b9511b9a 607#if 0
e2e0b3e5 608 perror_with_name (_("Couldn't read debug register"));
b9511b9a
MK
609#else
610 return 0;
611#endif
84346e11
MK
612
613 return value;
614}
615
616static void
9f0bdab8 617i386_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
84346e11
MK
618{
619 int tid;
620
9f0bdab8
DJ
621 tid = TIDGET (ptid);
622 if (tid == 0)
623 tid = PIDGET (ptid);
84346e11
MK
624
625 errno = 0;
ce556f85 626 ptrace (PTRACE_POKEUSER, tid,
84346e11
MK
627 offsetof (struct user, u_debugreg[regnum]), value);
628 if (errno != 0)
e2e0b3e5 629 perror_with_name (_("Couldn't write debug register"));
84346e11
MK
630}
631
632void
7bf0983e 633i386_linux_dr_set_control (unsigned long control)
84346e11 634{
9f0bdab8
DJ
635 struct lwp_info *lp;
636 ptid_t ptid;
637
638 i386_linux_dr[DR_CONTROL] = control;
639 ALL_LWPS (lp, ptid)
640 i386_linux_dr_set (ptid, DR_CONTROL, control);
84346e11
MK
641}
642
643void
644i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
645{
9f0bdab8
DJ
646 struct lwp_info *lp;
647 ptid_t ptid;
648
84346e11
MK
649 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
650
9f0bdab8
DJ
651 i386_linux_dr[DR_FIRSTADDR + regnum] = addr;
652 ALL_LWPS (lp, ptid)
653 i386_linux_dr_set (ptid, DR_FIRSTADDR + regnum, addr);
84346e11
MK
654}
655
656void
657i386_linux_dr_reset_addr (int regnum)
658{
9f0bdab8 659 i386_linux_dr_set_addr (regnum, 0);
84346e11
MK
660}
661
7bf0983e 662unsigned long
84346e11
MK
663i386_linux_dr_get_status (void)
664{
9f0bdab8
DJ
665 return i386_linux_dr_get (inferior_ptid, DR_STATUS);
666}
667
668static void
669i386_linux_new_thread (ptid_t ptid)
670{
671 int i;
672
673 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
674 i386_linux_dr_set (ptid, i, i386_linux_dr[i]);
675
676 i386_linux_dr_set (ptid, DR_CONTROL, i386_linux_dr[DR_CONTROL]);
84346e11
MK
677}
678\f
679
5bca7895
MK
680/* Called by libthread_db. Returns a pointer to the thread local
681 storage (or its descriptor). */
682
683ps_err_e
684ps_get_thread_area (const struct ps_prochandle *ph,
685 lwpid_t lwpid, int idx, void **base)
686{
687 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
688 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
689 4 byte integers in size: `entry_number', `base_addr', `limit',
690 and a bunch of status bits.
691
692 The values returned by this ptrace call should be part of the
693 regcache buffer, and ps_get_thread_area should channel its
694 request through the regcache. That way remote targets could
695 provide the value using the remote protocol and not this direct
696 call.
697
698 Is this function needed? I'm guessing that the `base' is the
699 address of a a descriptor that libthread_db uses to find the
b2fa5097 700 thread local address base that GDB needs. Perhaps that
5bca7895
MK
701 descriptor is defined by the ABI. Anyway, given that
702 libthread_db calls this function without prompting (gdb
703 requesting tls base) I guess it needs info in there anyway. */
704 unsigned int desc[4];
705 gdb_assert (sizeof (int) == 4);
706
707#ifndef PTRACE_GET_THREAD_AREA
708#define PTRACE_GET_THREAD_AREA 25
709#endif
710
711 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
712 (void *) idx, (unsigned long) &desc) < 0)
713 return PS_ERR;
714
715 *(int *)base = desc[1];
716 return PS_OK;
717}
718\f
719
a4b6fc86 720/* The instruction for a GNU/Linux system call is:
a6abb2c0
MK
721 int $0x80
722 or 0xcd 0x80. */
723
724static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
725
726#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
727
728/* The system call number is stored in the %eax register. */
7532965f 729#define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
a6abb2c0
MK
730
731/* We are specifically interested in the sigreturn and rt_sigreturn
732 system calls. */
733
734#ifndef SYS_sigreturn
735#define SYS_sigreturn 0x77
736#endif
737#ifndef SYS_rt_sigreturn
738#define SYS_rt_sigreturn 0xad
739#endif
740
741/* Offset to saved processor flags, from <asm/sigcontext.h>. */
742#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
743
744/* Resume execution of the inferior process.
745 If STEP is nonzero, single-step it.
746 If SIGNAL is nonzero, give it that signal. */
747
10d6c8cd 748static void
28439f5e
PA
749i386_linux_resume (struct target_ops *ops,
750 ptid_t ptid, int step, enum target_signal signal)
a6abb2c0 751{
39f77062
KB
752 int pid = PIDGET (ptid);
753
a6abb2c0
MK
754 int request = PTRACE_CONT;
755
a6abb2c0
MK
756 if (step)
757 {
594f7785 758 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
7b86a1b8 759 ULONGEST pc;
8e70166d 760 gdb_byte buf[LINUX_SYSCALL_LEN];
a6abb2c0
MK
761
762 request = PTRACE_SINGLESTEP;
763
875f8d0e
UW
764 regcache_cooked_read_unsigned
765 (regcache, gdbarch_pc_regnum (get_regcache_arch (regcache)), &pc);
7b86a1b8 766
a6abb2c0
MK
767 /* Returning from a signal trampoline is done by calling a
768 special system call (sigreturn or rt_sigreturn, see
769 i386-linux-tdep.c for more information). This system call
770 restores the registers that were saved when the signal was
771 raised, including %eflags. That means that single-stepping
772 won't work. Instead, we'll have to modify the signal context
773 that's about to be restored, and set the trace flag there. */
774
775 /* First check if PC is at a system call. */
8defab1a 776 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
a6abb2c0
MK
777 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
778 {
7b86a1b8
UW
779 ULONGEST syscall;
780 regcache_cooked_read_unsigned (regcache,
781 LINUX_SYSCALL_REGNUM, &syscall);
a6abb2c0
MK
782
783 /* Then check the system call number. */
784 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
785 {
7b86a1b8 786 ULONGEST sp, addr;
a6abb2c0 787 unsigned long int eflags;
7bf0983e 788
7b86a1b8 789 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
a6abb2c0
MK
790 if (syscall == SYS_rt_sigreturn)
791 addr = read_memory_integer (sp + 8, 4) + 20;
7b86a1b8
UW
792 else
793 addr = sp;
a6abb2c0
MK
794
795 /* Set the trace flag in the context that's about to be
796 restored. */
797 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
8e70166d 798 read_memory (addr, (gdb_byte *) &eflags, 4);
a6abb2c0 799 eflags |= 0x0100;
8e70166d 800 write_memory (addr, (gdb_byte *) &eflags, 4);
a6abb2c0
MK
801 }
802 }
803 }
804
805 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
e2e0b3e5 806 perror_with_name (("ptrace"));
a6abb2c0 807}
4de4c07c 808
10d6c8cd
DJ
809static void (*super_post_startup_inferior) (ptid_t ptid);
810
811static void
812i386_linux_child_post_startup_inferior (ptid_t ptid)
4de4c07c
DJ
813{
814 i386_cleanup_dregs ();
10d6c8cd
DJ
815 super_post_startup_inferior (ptid);
816}
817
818void
819_initialize_i386_linux_nat (void)
820{
821 struct target_ops *t;
822
823 /* Fill in the generic GNU/Linux methods. */
824 t = linux_target ();
825
c03374d5
DJ
826 i386_use_watchpoints (t);
827
10d6c8cd
DJ
828 /* Override the default ptrace resume method. */
829 t->to_resume = i386_linux_resume;
830
831 /* Override the GNU/Linux inferior startup hook. */
832 super_post_startup_inferior = t->to_post_startup_inferior;
833 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
834
835 /* Add our register access methods. */
836 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
837 t->to_store_registers = i386_linux_store_inferior_registers;
838
839 /* Register the target. */
f973ed9c 840 linux_nat_add_target (t);
9f0bdab8 841 linux_nat_set_new_thread (t, i386_linux_new_thread);
4de4c07c 842}
This page took 0.758128 seconds and 4 git commands to generate.