*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
CommitLineData
a4194092 1/* Native-dependent code for GNU/Linux i386.
a4b6fc86 2
6aba47ca 3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
31828840 4 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
UW
163 error (_("Couldn't read register %s (#%d): %s."),
164 gdbarch_register_name (current_gdbarch, 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
UW
191 error (_("Couldn't write register %s (#%d): %s."),
192 gdbarch_register_name (current_gdbarch, 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
f57d151a 212 if (I386_LINUX_ORIG_EAX_REGNUM < gdbarch_num_regs (current_gdbarch))
7f7fe91e 213 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM,
31828840 214 regp + ORIG_EAX);
917317f4
JM
215}
216
04cd15b6
MK
217/* Fill register REGNO (if it is a general-purpose register) in
218 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
219 do this for all registers. */
6ce2ac0b 220
917317f4 221void
7f7fe91e
UW
222fill_gregset (const struct regcache *regcache,
223 elf_gregset_t *gregsetp, int regno)
917317f4 224{
6ce2ac0b
MK
225 elf_greg_t *regp = (elf_greg_t *) gregsetp;
226 int i;
04cd15b6 227
98df6387 228 for (i = 0; i < I386_NUM_GREGS; i++)
099a9414 229 if (regno == -1 || regno == i)
7f7fe91e 230 regcache_raw_collect (regcache, i, regp + regmap[i]);
3fb1c838 231
82ea117a 232 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
f57d151a 233 && I386_LINUX_ORIG_EAX_REGNUM < gdbarch_num_regs (current_gdbarch))
7f7fe91e 234 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM,
31828840 235 regp + ORIG_EAX);
d4f3574e
SS
236}
237
f60300e7
MK
238#ifdef HAVE_PTRACE_GETREGS
239
04cd15b6
MK
240/* Fetch all general-purpose registers from process/thread TID and
241 store their values in GDB's register array. */
d4f3574e 242
5c44784c 243static void
56be3814 244fetch_regs (struct regcache *regcache, int tid)
5c44784c 245{
04cd15b6 246 elf_gregset_t regs;
2e024c20 247 elf_gregset_t *regs_p = &regs;
5c44784c 248
6ce2ac0b 249 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
5c44784c 250 {
f60300e7
MK
251 if (errno == EIO)
252 {
253 /* The kernel we're running on doesn't support the GETREGS
254 request. Reset `have_ptrace_getregs'. */
255 have_ptrace_getregs = 0;
256 return;
257 }
258
e2e0b3e5 259 perror_with_name (_("Couldn't get registers"));
5c44784c
JM
260 }
261
2e024c20 262 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
5c44784c
JM
263}
264
04cd15b6
MK
265/* Store all valid general-purpose registers in GDB's register array
266 into the process/thread specified by TID. */
5c44784c 267
5c44784c 268static void
56be3814 269store_regs (const struct regcache *regcache, int tid, int regno)
5c44784c 270{
04cd15b6 271 elf_gregset_t regs;
5c44784c 272
6ce2ac0b 273 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 274 perror_with_name (_("Couldn't get registers"));
5c44784c 275
56be3814 276 fill_gregset (regcache, &regs, regno);
6ce2ac0b
MK
277
278 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 279 perror_with_name (_("Couldn't write registers"));
5c44784c
JM
280}
281
f60300e7
MK
282#else
283
56be3814
UW
284static void fetch_regs (struct regcache *regcache, int tid) {}
285static void store_regs (const struct regcache *regcache, int tid, int regno) {}
f60300e7
MK
286
287#endif
5c44784c 288\f
5c44784c 289
6ce2ac0b 290/* Transfering floating-point registers between GDB, inferiors and cores. */
d4f3574e 291
04cd15b6 292/* Fill GDB's register array with the floating-point register values in
917317f4 293 *FPREGSETP. */
04cd15b6 294
d4f3574e 295void
7f7fe91e 296supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
d4f3574e 297{
7f7fe91e 298 i387_supply_fsave (regcache, -1, fpregsetp);
917317f4 299}
d4f3574e 300
04cd15b6
MK
301/* Fill register REGNO (if it is a floating-point register) in
302 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
303 do this for all registers. */
917317f4
JM
304
305void
7f7fe91e
UW
306fill_fpregset (const struct regcache *regcache,
307 elf_fpregset_t *fpregsetp, int regno)
917317f4 308{
7f7fe91e 309 i387_collect_fsave (regcache, regno, fpregsetp);
d4f3574e
SS
310}
311
f60300e7
MK
312#ifdef HAVE_PTRACE_GETREGS
313
04cd15b6
MK
314/* Fetch all floating-point registers from process/thread TID and store
315 thier values in GDB's register array. */
917317f4 316
d4f3574e 317static void
56be3814 318fetch_fpregs (struct regcache *regcache, int tid)
d4f3574e 319{
04cd15b6 320 elf_fpregset_t fpregs;
d4f3574e 321
6ce2ac0b 322 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 323 perror_with_name (_("Couldn't get floating point status"));
d4f3574e 324
56be3814 325 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
d4f3574e
SS
326}
327
04cd15b6
MK
328/* Store all valid floating-point registers in GDB's register array
329 into the process/thread specified by TID. */
d4f3574e 330
d4f3574e 331static void
56be3814 332store_fpregs (const struct regcache *regcache, int tid, int regno)
d4f3574e 333{
04cd15b6 334 elf_fpregset_t fpregs;
d4f3574e 335
6ce2ac0b 336 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 337 perror_with_name (_("Couldn't get floating point status"));
d4f3574e 338
56be3814 339 fill_fpregset (regcache, &fpregs, regno);
d4f3574e 340
6ce2ac0b 341 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 342 perror_with_name (_("Couldn't write floating point status"));
d4f3574e
SS
343}
344
f60300e7
MK
345#else
346
56be3814
UW
347static void fetch_fpregs (struct regcache *regcache, int tid) {}
348static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
f60300e7
MK
349
350#endif
5c44784c 351\f
d4f3574e 352
6ce2ac0b 353/* Transfering floating-point and SSE registers to and from GDB. */
11cf8741 354
6ce2ac0b 355#ifdef HAVE_PTRACE_GETFPXREGS
04cd15b6
MK
356
357/* Fill GDB's register array with the floating-point and SSE register
6ce2ac0b 358 values in *FPXREGSETP. */
04cd15b6 359
975aec09 360void
7f7fe91e
UW
361supply_fpxregset (struct regcache *regcache,
362 const elf_fpxregset_t *fpxregsetp)
d4f3574e 363{
7f7fe91e 364 i387_supply_fxsave (regcache, -1, fpxregsetp);
d4f3574e
SS
365}
366
6ce2ac0b
MK
367/* Fill register REGNO (if it is a floating-point or SSE register) in
368 *FPXREGSETP with the value in GDB's register array. If REGNO is
369 -1, do this for all registers. */
d4f3574e 370
975aec09 371void
7f7fe91e
UW
372fill_fpxregset (const struct regcache *regcache,
373 elf_fpxregset_t *fpxregsetp, int regno)
d4f3574e 374{
7f7fe91e 375 i387_collect_fxsave (regcache, regno, fpxregsetp);
5c44784c
JM
376}
377
6ce2ac0b 378/* Fetch all registers covered by the PTRACE_GETFPXREGS request from
04cd15b6
MK
379 process/thread TID and store their values in GDB's register array.
380 Return non-zero if successful, zero otherwise. */
5c44784c 381
5c44784c 382static int
56be3814 383fetch_fpxregs (struct regcache *regcache, int tid)
5c44784c 384{
6ce2ac0b 385 elf_fpxregset_t fpxregs;
5c44784c 386
6ce2ac0b 387 if (! have_ptrace_getfpxregs)
5c44784c
JM
388 return 0;
389
6ce2ac0b 390 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
d4f3574e 391 {
5c44784c
JM
392 if (errno == EIO)
393 {
6ce2ac0b 394 have_ptrace_getfpxregs = 0;
5c44784c
JM
395 return 0;
396 }
397
e2e0b3e5 398 perror_with_name (_("Couldn't read floating-point and SSE registers"));
d4f3574e
SS
399 }
400
56be3814 401 supply_fpxregset (regcache, (const elf_fpxregset_t *) &fpxregs);
5c44784c
JM
402 return 1;
403}
d4f3574e 404
04cd15b6 405/* Store all valid registers in GDB's register array covered by the
6ce2ac0b 406 PTRACE_SETFPXREGS request into the process/thread specified by TID.
04cd15b6 407 Return non-zero if successful, zero otherwise. */
5c44784c 408
5c44784c 409static int
56be3814 410store_fpxregs (const struct regcache *regcache, int tid, int regno)
5c44784c 411{
6ce2ac0b 412 elf_fpxregset_t fpxregs;
5c44784c 413
6ce2ac0b 414 if (! have_ptrace_getfpxregs)
5c44784c 415 return 0;
6ce2ac0b
MK
416
417 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
2866d305
MK
418 {
419 if (errno == EIO)
420 {
421 have_ptrace_getfpxregs = 0;
422 return 0;
423 }
424
e2e0b3e5 425 perror_with_name (_("Couldn't read floating-point and SSE registers"));
2866d305 426 }
5c44784c 427
56be3814 428 fill_fpxregset (regcache, &fpxregs, regno);
5c44784c 429
6ce2ac0b 430 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
e2e0b3e5 431 perror_with_name (_("Couldn't write floating-point and SSE registers"));
5c44784c
JM
432
433 return 1;
434}
435
5c44784c
JM
436#else
437
56be3814
UW
438static int fetch_fpxregs (struct regcache *regcache, int tid) { return 0; }
439static int store_fpxregs (const struct regcache *regcache, int tid, int regno) { return 0; }
5c44784c 440
6ce2ac0b 441#endif /* HAVE_PTRACE_GETFPXREGS */
5c44784c 442\f
6ce2ac0b 443
5c44784c 444/* Transferring arbitrary registers between GDB and inferior. */
d4f3574e 445
04cd15b6
MK
446/* Fetch register REGNO from the child process. If REGNO is -1, do
447 this for all registers (including the floating point and SSE
448 registers). */
d4f3574e 449
10d6c8cd 450static void
56be3814 451i386_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
d4f3574e 452{
ed9a39eb
JM
453 int tid;
454
f60300e7
MK
455 /* Use the old method of peeking around in `struct user' if the
456 GETREGS request isn't available. */
ce556f85 457 if (!have_ptrace_getregs)
f60300e7 458 {
ce556f85
MK
459 int i;
460
f57d151a 461 for (i = 0; i < gdbarch_num_regs (current_gdbarch); i++)
ce556f85 462 if (regno == -1 || regno == i)
56be3814 463 fetch_register (regcache, i);
ce556f85 464
f60300e7
MK
465 return;
466 }
467
a4b6fc86 468 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
469 tid = TIDGET (inferior_ptid);
470 if (tid == 0)
471 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 472
6ce2ac0b 473 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
04cd15b6 474 transfers more registers in one system call, and we'll cache the
6ce2ac0b 475 results. But remember that fetch_fpxregs can fail, and return
04cd15b6 476 zero. */
5c44784c
JM
477 if (regno == -1)
478 {
56be3814 479 fetch_regs (regcache, tid);
f60300e7
MK
480
481 /* The call above might reset `have_ptrace_getregs'. */
ce556f85 482 if (!have_ptrace_getregs)
f60300e7 483 {
56be3814 484 i386_linux_fetch_inferior_registers (regcache, regno);
f60300e7
MK
485 return;
486 }
487
56be3814 488 if (fetch_fpxregs (regcache, tid))
5c44784c 489 return;
56be3814 490 fetch_fpregs (regcache, tid);
5c44784c
JM
491 return;
492 }
d4f3574e 493
5c44784c
JM
494 if (GETREGS_SUPPLIES (regno))
495 {
56be3814 496 fetch_regs (regcache, tid);
5c44784c
JM
497 return;
498 }
499
6ce2ac0b 500 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 501 {
56be3814 502 if (fetch_fpxregs (regcache, tid))
5c44784c
JM
503 return;
504
505 /* Either our processor or our kernel doesn't support the SSE
506 registers, so read the FP registers in the traditional way,
507 and fill the SSE registers with dummy values. It would be
508 more graceful to handle differences in the register set using
509 gdbarch. Until then, this will at least make things work
510 plausibly. */
56be3814 511 fetch_fpregs (regcache, tid);
5c44784c
JM
512 return;
513 }
514
8e65ff28 515 internal_error (__FILE__, __LINE__,
e2e0b3e5 516 _("Got request for bad register number %d."), regno);
d4f3574e
SS
517}
518
04cd15b6
MK
519/* Store register REGNO back into the child process. If REGNO is -1,
520 do this for all registers (including the floating point and SSE
521 registers). */
10d6c8cd 522static void
56be3814 523i386_linux_store_inferior_registers (struct regcache *regcache, int regno)
d4f3574e 524{
ed9a39eb
JM
525 int tid;
526
f60300e7
MK
527 /* Use the old method of poking around in `struct user' if the
528 SETREGS request isn't available. */
ce556f85 529 if (!have_ptrace_getregs)
f60300e7 530 {
ce556f85
MK
531 int i;
532
f57d151a 533 for (i = 0; i < gdbarch_num_regs (current_gdbarch); i++)
ce556f85 534 if (regno == -1 || regno == i)
56be3814 535 store_register (regcache, i);
ce556f85 536
f60300e7
MK
537 return;
538 }
539
a4b6fc86 540 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
541 tid = TIDGET (inferior_ptid);
542 if (tid == 0)
543 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 544
6ce2ac0b 545 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
04cd15b6 546 transfers more registers in one system call. But remember that
6ce2ac0b 547 store_fpxregs can fail, and return zero. */
5c44784c
JM
548 if (regno == -1)
549 {
56be3814
UW
550 store_regs (regcache, tid, regno);
551 if (store_fpxregs (regcache, tid, regno))
5c44784c 552 return;
56be3814 553 store_fpregs (regcache, tid, regno);
5c44784c
JM
554 return;
555 }
d4f3574e 556
5c44784c
JM
557 if (GETREGS_SUPPLIES (regno))
558 {
56be3814 559 store_regs (regcache, tid, regno);
5c44784c
JM
560 return;
561 }
562
6ce2ac0b 563 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 564 {
56be3814 565 if (store_fpxregs (regcache, tid, regno))
5c44784c
JM
566 return;
567
568 /* Either our processor or our kernel doesn't support the SSE
04cd15b6
MK
569 registers, so just write the FP registers in the traditional
570 way. */
56be3814 571 store_fpregs (regcache, tid, regno);
5c44784c
JM
572 return;
573 }
574
8e65ff28 575 internal_error (__FILE__, __LINE__,
e2e0b3e5 576 _("Got request to store bad register number %d."), regno);
d4f3574e 577}
de57eccd 578\f
6ce2ac0b 579
4ffc8466
MK
580/* Support for debug registers. */
581
7bf0983e 582static unsigned long
84346e11
MK
583i386_linux_dr_get (int regnum)
584{
585 int tid;
7bf0983e 586 unsigned long value;
84346e11
MK
587
588 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
589 multi-threaded processes here. For now, pretend there is just
590 one thread. */
39f77062 591 tid = PIDGET (inferior_ptid);
84346e11 592
b9511b9a
MK
593 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
594 ptrace call fails breaks debugging remote targets. The correct
595 way to fix this is to add the hardware breakpoint and watchpoint
7532965f 596 stuff to the target vector. For now, just return zero if the
b9511b9a 597 ptrace call fails. */
84346e11 598 errno = 0;
ce556f85 599 value = ptrace (PTRACE_PEEKUSER, tid,
84346e11
MK
600 offsetof (struct user, u_debugreg[regnum]), 0);
601 if (errno != 0)
b9511b9a 602#if 0
e2e0b3e5 603 perror_with_name (_("Couldn't read debug register"));
b9511b9a
MK
604#else
605 return 0;
606#endif
84346e11
MK
607
608 return value;
609}
610
611static void
7bf0983e 612i386_linux_dr_set (int regnum, unsigned long value)
84346e11
MK
613{
614 int tid;
615
616 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
617 multi-threaded processes here. For now, pretend there is just
618 one thread. */
39f77062 619 tid = PIDGET (inferior_ptid);
84346e11
MK
620
621 errno = 0;
ce556f85 622 ptrace (PTRACE_POKEUSER, tid,
84346e11
MK
623 offsetof (struct user, u_debugreg[regnum]), value);
624 if (errno != 0)
e2e0b3e5 625 perror_with_name (_("Couldn't write debug register"));
84346e11
MK
626}
627
628void
7bf0983e 629i386_linux_dr_set_control (unsigned long control)
84346e11
MK
630{
631 i386_linux_dr_set (DR_CONTROL, control);
632}
633
634void
635i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
636{
637 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
638
639 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
640}
641
642void
643i386_linux_dr_reset_addr (int regnum)
644{
645 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
646
647 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
648}
649
7bf0983e 650unsigned long
84346e11
MK
651i386_linux_dr_get_status (void)
652{
653 return i386_linux_dr_get (DR_STATUS);
654}
655\f
656
5bca7895
MK
657/* Called by libthread_db. Returns a pointer to the thread local
658 storage (or its descriptor). */
659
660ps_err_e
661ps_get_thread_area (const struct ps_prochandle *ph,
662 lwpid_t lwpid, int idx, void **base)
663{
664 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
665 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
666 4 byte integers in size: `entry_number', `base_addr', `limit',
667 and a bunch of status bits.
668
669 The values returned by this ptrace call should be part of the
670 regcache buffer, and ps_get_thread_area should channel its
671 request through the regcache. That way remote targets could
672 provide the value using the remote protocol and not this direct
673 call.
674
675 Is this function needed? I'm guessing that the `base' is the
676 address of a a descriptor that libthread_db uses to find the
b2fa5097 677 thread local address base that GDB needs. Perhaps that
5bca7895
MK
678 descriptor is defined by the ABI. Anyway, given that
679 libthread_db calls this function without prompting (gdb
680 requesting tls base) I guess it needs info in there anyway. */
681 unsigned int desc[4];
682 gdb_assert (sizeof (int) == 4);
683
684#ifndef PTRACE_GET_THREAD_AREA
685#define PTRACE_GET_THREAD_AREA 25
686#endif
687
688 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
689 (void *) idx, (unsigned long) &desc) < 0)
690 return PS_ERR;
691
692 *(int *)base = desc[1];
693 return PS_OK;
694}
695\f
696
a4b6fc86 697/* The instruction for a GNU/Linux system call is:
a6abb2c0
MK
698 int $0x80
699 or 0xcd 0x80. */
700
701static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
702
703#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
704
705/* The system call number is stored in the %eax register. */
7532965f 706#define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
a6abb2c0
MK
707
708/* We are specifically interested in the sigreturn and rt_sigreturn
709 system calls. */
710
711#ifndef SYS_sigreturn
712#define SYS_sigreturn 0x77
713#endif
714#ifndef SYS_rt_sigreturn
715#define SYS_rt_sigreturn 0xad
716#endif
717
718/* Offset to saved processor flags, from <asm/sigcontext.h>. */
719#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
720
721/* Resume execution of the inferior process.
722 If STEP is nonzero, single-step it.
723 If SIGNAL is nonzero, give it that signal. */
724
10d6c8cd
DJ
725static void
726i386_linux_resume (ptid_t ptid, int step, enum target_signal signal)
a6abb2c0 727{
39f77062
KB
728 int pid = PIDGET (ptid);
729
a6abb2c0
MK
730 int request = PTRACE_CONT;
731
732 if (pid == -1)
733 /* Resume all threads. */
734 /* I think this only gets used in the non-threaded case, where "resume
39f77062
KB
735 all threads" and "resume inferior_ptid" are the same. */
736 pid = PIDGET (inferior_ptid);
a6abb2c0
MK
737
738 if (step)
739 {
594f7785 740 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
7b86a1b8 741 ULONGEST pc;
8e70166d 742 gdb_byte buf[LINUX_SYSCALL_LEN];
a6abb2c0
MK
743
744 request = PTRACE_SINGLESTEP;
745
3e8c568d
UW
746 regcache_cooked_read_unsigned (regcache,
747 gdbarch_pc_regnum (current_gdbarch), &pc);
7b86a1b8 748
a6abb2c0
MK
749 /* Returning from a signal trampoline is done by calling a
750 special system call (sigreturn or rt_sigreturn, see
751 i386-linux-tdep.c for more information). This system call
752 restores the registers that were saved when the signal was
753 raised, including %eflags. That means that single-stepping
754 won't work. Instead, we'll have to modify the signal context
755 that's about to be restored, and set the trace flag there. */
756
757 /* First check if PC is at a system call. */
359a9262 758 if (read_memory_nobpt (pc, buf, LINUX_SYSCALL_LEN) == 0
a6abb2c0
MK
759 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
760 {
7b86a1b8
UW
761 ULONGEST syscall;
762 regcache_cooked_read_unsigned (regcache,
763 LINUX_SYSCALL_REGNUM, &syscall);
a6abb2c0
MK
764
765 /* Then check the system call number. */
766 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
767 {
7b86a1b8 768 ULONGEST sp, addr;
a6abb2c0 769 unsigned long int eflags;
7bf0983e 770
7b86a1b8 771 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
a6abb2c0
MK
772 if (syscall == SYS_rt_sigreturn)
773 addr = read_memory_integer (sp + 8, 4) + 20;
7b86a1b8
UW
774 else
775 addr = sp;
a6abb2c0
MK
776
777 /* Set the trace flag in the context that's about to be
778 restored. */
779 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
8e70166d 780 read_memory (addr, (gdb_byte *) &eflags, 4);
a6abb2c0 781 eflags |= 0x0100;
8e70166d 782 write_memory (addr, (gdb_byte *) &eflags, 4);
a6abb2c0
MK
783 }
784 }
785 }
786
787 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
e2e0b3e5 788 perror_with_name (("ptrace"));
a6abb2c0 789}
4de4c07c 790
10d6c8cd
DJ
791static void (*super_post_startup_inferior) (ptid_t ptid);
792
793static void
794i386_linux_child_post_startup_inferior (ptid_t ptid)
4de4c07c
DJ
795{
796 i386_cleanup_dregs ();
10d6c8cd
DJ
797 super_post_startup_inferior (ptid);
798}
799
800void
801_initialize_i386_linux_nat (void)
802{
803 struct target_ops *t;
804
805 /* Fill in the generic GNU/Linux methods. */
806 t = linux_target ();
807
808 /* Override the default ptrace resume method. */
809 t->to_resume = i386_linux_resume;
810
811 /* Override the GNU/Linux inferior startup hook. */
812 super_post_startup_inferior = t->to_post_startup_inferior;
813 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
814
815 /* Add our register access methods. */
816 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
817 t->to_store_registers = i386_linux_store_inferior_registers;
818
819 /* Register the target. */
f973ed9c 820 linux_nat_add_target (t);
4de4c07c 821}
This page took 0.55383 seconds and 4 git commands to generate.