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