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