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