]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/i386-linux-nat.c
Add support for "orig_eax" pseudo register on Linux/x86.
[thirdparty/binutils-gdb.git] / gdb / i386-linux-nat.c
CommitLineData
6ce2ac0b 1/* Native-dependent code for Linux/x86.
8e65ff28 2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
d4f3574e 3
04cd15b6 4 This file is part of GDB.
d4f3574e 5
04cd15b6
MK
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
d4f3574e 10
04cd15b6
MK
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
d4f3574e 15
04cd15b6
MK
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
d4f3574e
SS
20
21#include "defs.h"
22#include "inferior.h"
23#include "gdbcore.h"
4e052eda 24#include "regcache.h"
d4f3574e 25
84346e11 26#include "gdb_assert.h"
d4f3574e
SS
27#include <sys/ptrace.h>
28#include <sys/user.h>
29#include <sys/procfs.h>
30
31#ifdef HAVE_SYS_REG_H
32#include <sys/reg.h>
33#endif
34
84346e11
MK
35#ifdef HAVE_SYS_DEBUGREG_H
36#include <sys/debugreg.h>
37#endif
38
39#ifndef DR_FIRSTADDR
40#define DR_FIRSTADDR 0
41#endif
42
43#ifndef DR_LASTADDR
44#define DR_LASTADDR 3
45#endif
46
47#ifndef DR_STATUS
48#define DR_STATUS 6
49#endif
50
51#ifndef DR_CONTROL
52#define DR_CONTROL 7
53#endif
54
6ce2ac0b 55/* Prototypes for supply_gregset etc. */
c60c0f5f
MS
56#include "gregset.h"
57
6ce2ac0b
MK
58/* Prototypes for i387_supply_fsave etc. */
59#include "i387-nat.h"
60
756ed206
MK
61/* Prototypes for local functions. */
62static void dummy_sse_values (void);
63
6ce2ac0b 64\f
d4f3574e 65
04cd15b6
MK
66/* The register sets used in Linux ELF core-dumps are identical to the
67 register sets in `struct user' that is used for a.out core-dumps,
68 and is also used by `ptrace'. The corresponding types are
69 `elf_gregset_t' for the general-purpose registers (with
70 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
71 for the floating-point registers.
72
73 Those types used to be available under the names `gregset_t' and
74 `fpregset_t' too, and this file used those names in the past. But
75 those names are now used for the register sets used in the
76 `mcontext_t' type, and have a different size and layout. */
77
78/* Mapping between the general-purpose registers in `struct user'
79 format and GDB's register array layout. */
d4f3574e
SS
80static int regmap[] =
81{
82 EAX, ECX, EDX, EBX,
83 UESP, EBP, ESI, EDI,
84 EIP, EFL, CS, SS,
04cd15b6 85 DS, ES, FS, GS
d4f3574e
SS
86};
87
5c44784c
JM
88/* Which ptrace request retrieves which registers?
89 These apply to the corresponding SET requests as well. */
90#define GETREGS_SUPPLIES(regno) \
91 (0 <= (regno) && (regno) <= 15)
92#define GETFPREGS_SUPPLIES(regno) \
93 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
6ce2ac0b 94#define GETFPXREGS_SUPPLIES(regno) \
5c44784c
JM
95 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
96
f60300e7
MK
97/* Does the current host support the GETREGS request? */
98int have_ptrace_getregs =
99#ifdef HAVE_PTRACE_GETREGS
100 1
101#else
102 0
103#endif
104;
105
6ce2ac0b 106/* Does the current host support the GETFPXREGS request? The header
5c44784c
JM
107 file may or may not define it, and even if it is defined, the
108 kernel will return EIO if it's running on a pre-SSE processor.
109
110 My instinct is to attach this to some architecture- or
111 target-specific data structure, but really, a particular GDB
112 process can only run on top of one kernel at a time. So it's okay
113 for this to be a simple variable. */
6ce2ac0b
MK
114int have_ptrace_getfpxregs =
115#ifdef HAVE_PTRACE_GETFPXREGS
5c44784c
JM
116 1
117#else
118 0
119#endif
120;
f60300e7 121\f
6ce2ac0b 122
84346e11
MK
123/* Support for the user struct. */
124
125/* Return the address of register REGNUM. BLOCKEND is the value of
126 u.u_ar0, which should point to the registers. */
127
128CORE_ADDR
129register_u_addr (CORE_ADDR blockend, int regnum)
130{
131 return (blockend + 4 * regmap[regnum]);
132}
133
134/* Return the size of the user struct. */
135
136int
137kernel_u_size (void)
138{
139 return (sizeof (struct user));
140}
141\f
142
97780f5f
JB
143/* Fetching registers directly from the U area, one at a time. */
144
f60300e7
MK
145/* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
146 The problem is that we define FETCH_INFERIOR_REGISTERS since we
147 want to use our own versions of {fetch,store}_inferior_registers
148 that use the GETREGS request. This means that the code in
149 `infptrace.c' is #ifdef'd out. But we need to fall back on that
150 code when GDB is running on top of a kernel that doesn't support
151 the GETREGS request. I want to avoid changing `infptrace.c' right
152 now. */
153
318b21ef
MK
154#ifndef PT_READ_U
155#define PT_READ_U PTRACE_PEEKUSR
156#endif
157#ifndef PT_WRITE_U
158#define PT_WRITE_U PTRACE_POKEUSR
159#endif
160
f60300e7
MK
161/* Default the type of the ptrace transfer to int. */
162#ifndef PTRACE_XFER_TYPE
163#define PTRACE_XFER_TYPE int
164#endif
165
166/* Registers we shouldn't try to fetch. */
d5d65353 167#define OLD_CANNOT_FETCH_REGISTER(regno) ((regno) >= NUM_GREGS)
f60300e7
MK
168
169/* Fetch one register. */
170
171static void
fba45db2 172fetch_register (int regno)
f60300e7
MK
173{
174 /* This isn't really an address. But ptrace thinks of it as one. */
175 CORE_ADDR regaddr;
176 char mess[128]; /* For messages */
177 register int i;
178 unsigned int offset; /* Offset of registers within the u area. */
179 char buf[MAX_REGISTER_RAW_SIZE];
180 int tid;
181
d5d65353 182 if (OLD_CANNOT_FETCH_REGISTER (regno))
f60300e7
MK
183 {
184 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
185 supply_register (regno, buf);
186 return;
187 }
188
189 /* Overload thread id onto process id */
39f77062
KB
190 if ((tid = TIDGET (inferior_ptid)) == 0)
191 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
f60300e7
MK
192
193 offset = U_REGS_OFFSET;
194
195 regaddr = register_addr (regno, offset);
196 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
197 {
198 errno = 0;
199 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
200 (PTRACE_ARG3_TYPE) regaddr, 0);
201 regaddr += sizeof (PTRACE_XFER_TYPE);
202 if (errno != 0)
203 {
204 sprintf (mess, "reading register %s (#%d)",
205 REGISTER_NAME (regno), regno);
206 perror_with_name (mess);
207 }
208 }
209 supply_register (regno, buf);
210}
211
212/* Fetch register values from the inferior.
213 If REGNO is negative, do this for all registers.
214 Otherwise, REGNO specifies which register (so we can save time). */
215
216void
fba45db2 217old_fetch_inferior_registers (int regno)
f60300e7
MK
218{
219 if (regno >= 0)
220 {
221 fetch_register (regno);
222 }
223 else
224 {
a728f042 225 for (regno = 0; regno < NUM_REGS; regno++)
f60300e7
MK
226 {
227 fetch_register (regno);
228 }
229 }
230}
231
232/* Registers we shouldn't try to store. */
d5d65353 233#define OLD_CANNOT_STORE_REGISTER(regno) ((regno) >= NUM_GREGS)
f60300e7
MK
234
235/* Store one register. */
236
237static void
fba45db2 238store_register (int regno)
f60300e7
MK
239{
240 /* This isn't really an address. But ptrace thinks of it as one. */
241 CORE_ADDR regaddr;
242 char mess[128]; /* For messages */
243 register int i;
244 unsigned int offset; /* Offset of registers within the u area. */
245 int tid;
246
d5d65353 247 if (OLD_CANNOT_STORE_REGISTER (regno))
f60300e7
MK
248 {
249 return;
250 }
251
252 /* Overload thread id onto process id */
39f77062
KB
253 if ((tid = TIDGET (inferior_ptid)) == 0)
254 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
f60300e7
MK
255
256 offset = U_REGS_OFFSET;
257
258 regaddr = register_addr (regno, offset);
259 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
260 {
261 errno = 0;
262 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
263 *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
264 regaddr += sizeof (PTRACE_XFER_TYPE);
265 if (errno != 0)
266 {
267 sprintf (mess, "writing register %s (#%d)",
268 REGISTER_NAME (regno), regno);
269 perror_with_name (mess);
270 }
271 }
272}
273
274/* Store our register values back into the inferior.
275 If REGNO is negative, do this for all registers.
276 Otherwise, REGNO specifies which register (so we can save time). */
277
278void
fba45db2 279old_store_inferior_registers (int regno)
f60300e7
MK
280{
281 if (regno >= 0)
282 {
283 store_register (regno);
284 }
285 else
286 {
a728f042 287 for (regno = 0; regno < NUM_REGS; regno++)
f60300e7
MK
288 {
289 store_register (regno);
290 }
291 }
292}
5c44784c 293\f
6ce2ac0b 294
04cd15b6
MK
295/* Transfering the general-purpose registers between GDB, inferiors
296 and core files. */
297
ad2a4d09 298/* Fill GDB's register array with the general-purpose register values
04cd15b6 299 in *GREGSETP. */
5c44784c 300
d4f3574e 301void
04cd15b6 302supply_gregset (elf_gregset_t *gregsetp)
d4f3574e 303{
04cd15b6 304 elf_greg_t *regp = (elf_greg_t *) gregsetp;
6ce2ac0b 305 int i;
d4f3574e 306
6ce2ac0b
MK
307 for (i = 0; i < NUM_GREGS; i++)
308 supply_register (i, (char *) (regp + regmap[i]));
917317f4
JM
309}
310
04cd15b6
MK
311/* Fill register REGNO (if it is a general-purpose register) in
312 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
313 do this for all registers. */
6ce2ac0b 314
917317f4 315void
04cd15b6 316fill_gregset (elf_gregset_t *gregsetp, int regno)
917317f4 317{
6ce2ac0b
MK
318 elf_greg_t *regp = (elf_greg_t *) gregsetp;
319 int i;
04cd15b6 320
6ce2ac0b
MK
321 for (i = 0; i < NUM_GREGS; i++)
322 if ((regno == -1 || regno == i))
323 *(regp + regmap[i]) = *(elf_greg_t *) &registers[REGISTER_BYTE (i)];
d4f3574e
SS
324}
325
f60300e7
MK
326#ifdef HAVE_PTRACE_GETREGS
327
04cd15b6
MK
328/* Fetch all general-purpose registers from process/thread TID and
329 store their values in GDB's register array. */
d4f3574e 330
5c44784c 331static void
ed9a39eb 332fetch_regs (int tid)
5c44784c 333{
04cd15b6 334 elf_gregset_t regs;
5c44784c 335
6ce2ac0b 336 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
5c44784c 337 {
f60300e7
MK
338 if (errno == EIO)
339 {
340 /* The kernel we're running on doesn't support the GETREGS
341 request. Reset `have_ptrace_getregs'. */
342 have_ptrace_getregs = 0;
343 return;
344 }
345
6ce2ac0b 346 perror_with_name ("Couldn't get registers");
5c44784c
JM
347 }
348
04cd15b6 349 supply_gregset (&regs);
5c44784c
JM
350}
351
04cd15b6
MK
352/* Store all valid general-purpose registers in GDB's register array
353 into the process/thread specified by TID. */
5c44784c 354
5c44784c 355static void
6ce2ac0b 356store_regs (int tid, int regno)
5c44784c 357{
04cd15b6 358 elf_gregset_t regs;
5c44784c 359
6ce2ac0b
MK
360 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
361 perror_with_name ("Couldn't get registers");
5c44784c 362
6ce2ac0b
MK
363 fill_gregset (&regs, regno);
364
365 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
366 perror_with_name ("Couldn't write registers");
5c44784c
JM
367}
368
f60300e7
MK
369#else
370
371static void fetch_regs (int tid) {}
6ce2ac0b 372static void store_regs (int tid, int regno) {}
f60300e7
MK
373
374#endif
5c44784c 375\f
5c44784c 376
6ce2ac0b 377/* Transfering floating-point registers between GDB, inferiors and cores. */
d4f3574e 378
04cd15b6 379/* Fill GDB's register array with the floating-point register values in
917317f4 380 *FPREGSETP. */
04cd15b6 381
d4f3574e 382void
04cd15b6 383supply_fpregset (elf_fpregset_t *fpregsetp)
d4f3574e 384{
6ce2ac0b 385 i387_supply_fsave ((char *) fpregsetp);
756ed206 386 dummy_sse_values ();
917317f4 387}
d4f3574e 388
04cd15b6
MK
389/* Fill register REGNO (if it is a floating-point register) in
390 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
391 do this for all registers. */
917317f4
JM
392
393void
04cd15b6 394fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
917317f4 395{
6ce2ac0b 396 i387_fill_fsave ((char *) fpregsetp, regno);
d4f3574e
SS
397}
398
f60300e7
MK
399#ifdef HAVE_PTRACE_GETREGS
400
04cd15b6
MK
401/* Fetch all floating-point registers from process/thread TID and store
402 thier values in GDB's register array. */
917317f4 403
d4f3574e 404static void
ed9a39eb 405fetch_fpregs (int tid)
d4f3574e 406{
04cd15b6 407 elf_fpregset_t fpregs;
d4f3574e 408
6ce2ac0b
MK
409 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
410 perror_with_name ("Couldn't get floating point status");
d4f3574e 411
04cd15b6 412 supply_fpregset (&fpregs);
d4f3574e
SS
413}
414
04cd15b6
MK
415/* Store all valid floating-point registers in GDB's register array
416 into the process/thread specified by TID. */
d4f3574e 417
d4f3574e 418static void
6ce2ac0b 419store_fpregs (int tid, int regno)
d4f3574e 420{
04cd15b6 421 elf_fpregset_t fpregs;
d4f3574e 422
6ce2ac0b
MK
423 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
424 perror_with_name ("Couldn't get floating point status");
d4f3574e 425
6ce2ac0b 426 fill_fpregset (&fpregs, regno);
d4f3574e 427
6ce2ac0b
MK
428 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
429 perror_with_name ("Couldn't write floating point status");
d4f3574e
SS
430}
431
f60300e7
MK
432#else
433
434static void fetch_fpregs (int tid) {}
6ce2ac0b 435static void store_fpregs (int tid, int regno) {}
f60300e7
MK
436
437#endif
5c44784c 438\f
d4f3574e 439
6ce2ac0b 440/* Transfering floating-point and SSE registers to and from GDB. */
11cf8741 441
6ce2ac0b 442#ifdef HAVE_PTRACE_GETFPXREGS
04cd15b6
MK
443
444/* Fill GDB's register array with the floating-point and SSE register
6ce2ac0b 445 values in *FPXREGSETP. */
04cd15b6 446
d4f3574e 447static void
6ce2ac0b 448supply_fpxregset (elf_fpxregset_t *fpxregsetp)
d4f3574e 449{
6ce2ac0b 450 i387_supply_fxsave ((char *) fpxregsetp);
d4f3574e
SS
451}
452
6ce2ac0b
MK
453/* Fill register REGNO (if it is a floating-point or SSE register) in
454 *FPXREGSETP with the value in GDB's register array. If REGNO is
455 -1, do this for all registers. */
d4f3574e 456
d4f3574e 457static void
6ce2ac0b 458fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
d4f3574e 459{
6ce2ac0b 460 i387_fill_fxsave ((char *) fpxregsetp, regno);
5c44784c
JM
461}
462
6ce2ac0b 463/* Fetch all registers covered by the PTRACE_GETFPXREGS request from
04cd15b6
MK
464 process/thread TID and store their values in GDB's register array.
465 Return non-zero if successful, zero otherwise. */
5c44784c 466
5c44784c 467static int
6ce2ac0b 468fetch_fpxregs (int tid)
5c44784c 469{
6ce2ac0b 470 elf_fpxregset_t fpxregs;
5c44784c 471
6ce2ac0b 472 if (! have_ptrace_getfpxregs)
5c44784c
JM
473 return 0;
474
6ce2ac0b 475 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
d4f3574e 476 {
5c44784c
JM
477 if (errno == EIO)
478 {
6ce2ac0b 479 have_ptrace_getfpxregs = 0;
5c44784c
JM
480 return 0;
481 }
482
6ce2ac0b 483 perror_with_name ("Couldn't read floating-point and SSE registers");
d4f3574e
SS
484 }
485
6ce2ac0b 486 supply_fpxregset (&fpxregs);
5c44784c
JM
487 return 1;
488}
d4f3574e 489
04cd15b6 490/* Store all valid registers in GDB's register array covered by the
6ce2ac0b 491 PTRACE_SETFPXREGS request into the process/thread specified by TID.
04cd15b6 492 Return non-zero if successful, zero otherwise. */
5c44784c 493
5c44784c 494static int
6ce2ac0b 495store_fpxregs (int tid, int regno)
5c44784c 496{
6ce2ac0b 497 elf_fpxregset_t fpxregs;
5c44784c 498
6ce2ac0b 499 if (! have_ptrace_getfpxregs)
5c44784c 500 return 0;
6ce2ac0b
MK
501
502 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
2866d305
MK
503 {
504 if (errno == EIO)
505 {
506 have_ptrace_getfpxregs = 0;
507 return 0;
508 }
509
510 perror_with_name ("Couldn't read floating-point and SSE registers");
511 }
5c44784c 512
6ce2ac0b 513 fill_fpxregset (&fpxregs, regno);
5c44784c 514
6ce2ac0b
MK
515 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
516 perror_with_name ("Couldn't write floating-point and SSE registers");
5c44784c
JM
517
518 return 1;
519}
520
04cd15b6 521/* Fill the XMM registers in the register array with dummy values. For
5c44784c
JM
522 cases where we don't have access to the XMM registers. I think
523 this is cleaner than printing a warning. For a cleaner solution,
524 we should gdbarchify the i386 family. */
04cd15b6 525
5c44784c 526static void
04cd15b6 527dummy_sse_values (void)
5c44784c
JM
528{
529 /* C doesn't have a syntax for NaN's, so write it out as an array of
530 longs. */
531 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
532 static long mxcsr = 0x1f80;
533 int reg;
534
535 for (reg = 0; reg < 8; reg++)
536 supply_register (XMM0_REGNUM + reg, (char *) dummy);
537 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
d4f3574e
SS
538}
539
5c44784c
JM
540#else
541
f0373401
MK
542static int fetch_fpxregs (int tid) { return 0; }
543static int store_fpxregs (int tid, int regno) { return 0; }
04cd15b6 544static void dummy_sse_values (void) {}
5c44784c 545
6ce2ac0b 546#endif /* HAVE_PTRACE_GETFPXREGS */
5c44784c 547\f
6ce2ac0b 548
5c44784c 549/* Transferring arbitrary registers between GDB and inferior. */
d4f3574e 550
d5d65353
PS
551/* Check if register REGNO in the child process is accessible.
552 If we are accessing registers directly via the U area, only the
553 general-purpose registers are available.
554 All registers should be accessible if we have GETREGS support. */
555
556int
557cannot_fetch_register (int regno)
558{
559 if (! have_ptrace_getregs)
560 return OLD_CANNOT_FETCH_REGISTER (regno);
561 return 0;
562}
563int
564cannot_store_register (int regno)
565{
566 if (! have_ptrace_getregs)
567 return OLD_CANNOT_STORE_REGISTER (regno);
568 return 0;
569}
570
04cd15b6
MK
571/* Fetch register REGNO from the child process. If REGNO is -1, do
572 this for all registers (including the floating point and SSE
573 registers). */
d4f3574e
SS
574
575void
917317f4 576fetch_inferior_registers (int regno)
d4f3574e 577{
ed9a39eb
JM
578 int tid;
579
f60300e7
MK
580 /* Use the old method of peeking around in `struct user' if the
581 GETREGS request isn't available. */
582 if (! have_ptrace_getregs)
583 {
584 old_fetch_inferior_registers (regno);
585 return;
586 }
587
04cd15b6 588 /* Linux LWP ID's are process ID's. */
39f77062
KB
589 if ((tid = TIDGET (inferior_ptid)) == 0)
590 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 591
6ce2ac0b 592 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
04cd15b6 593 transfers more registers in one system call, and we'll cache the
6ce2ac0b 594 results. But remember that fetch_fpxregs can fail, and return
04cd15b6 595 zero. */
5c44784c
JM
596 if (regno == -1)
597 {
ed9a39eb 598 fetch_regs (tid);
f60300e7
MK
599
600 /* The call above might reset `have_ptrace_getregs'. */
601 if (! have_ptrace_getregs)
602 {
603 old_fetch_inferior_registers (-1);
604 return;
605 }
606
6ce2ac0b 607 if (fetch_fpxregs (tid))
5c44784c 608 return;
ed9a39eb 609 fetch_fpregs (tid);
5c44784c
JM
610 return;
611 }
d4f3574e 612
5c44784c
JM
613 if (GETREGS_SUPPLIES (regno))
614 {
ed9a39eb 615 fetch_regs (tid);
5c44784c
JM
616 return;
617 }
618
6ce2ac0b 619 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 620 {
6ce2ac0b 621 if (fetch_fpxregs (tid))
5c44784c
JM
622 return;
623
624 /* Either our processor or our kernel doesn't support the SSE
625 registers, so read the FP registers in the traditional way,
626 and fill the SSE registers with dummy values. It would be
627 more graceful to handle differences in the register set using
628 gdbarch. Until then, this will at least make things work
629 plausibly. */
ed9a39eb 630 fetch_fpregs (tid);
5c44784c
JM
631 return;
632 }
633
8e65ff28
AC
634 internal_error (__FILE__, __LINE__,
635 "Got request for bad register number %d.", regno);
d4f3574e
SS
636}
637
04cd15b6
MK
638/* Store register REGNO back into the child process. If REGNO is -1,
639 do this for all registers (including the floating point and SSE
640 registers). */
d4f3574e 641void
04cd15b6 642store_inferior_registers (int regno)
d4f3574e 643{
ed9a39eb
JM
644 int tid;
645
f60300e7
MK
646 /* Use the old method of poking around in `struct user' if the
647 SETREGS request isn't available. */
648 if (! have_ptrace_getregs)
649 {
650 old_store_inferior_registers (regno);
651 return;
652 }
653
04cd15b6 654 /* Linux LWP ID's are process ID's. */
39f77062
KB
655 if ((tid = TIDGET (inferior_ptid)) == 0)
656 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 657
6ce2ac0b 658 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
04cd15b6 659 transfers more registers in one system call. But remember that
6ce2ac0b 660 store_fpxregs can fail, and return zero. */
5c44784c
JM
661 if (regno == -1)
662 {
6ce2ac0b
MK
663 store_regs (tid, regno);
664 if (store_fpxregs (tid, regno))
5c44784c 665 return;
6ce2ac0b 666 store_fpregs (tid, regno);
5c44784c
JM
667 return;
668 }
d4f3574e 669
5c44784c
JM
670 if (GETREGS_SUPPLIES (regno))
671 {
6ce2ac0b 672 store_regs (tid, regno);
5c44784c
JM
673 return;
674 }
675
6ce2ac0b 676 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 677 {
6ce2ac0b 678 if (store_fpxregs (tid, regno))
5c44784c
JM
679 return;
680
681 /* Either our processor or our kernel doesn't support the SSE
04cd15b6
MK
682 registers, so just write the FP registers in the traditional
683 way. */
6ce2ac0b 684 store_fpregs (tid, regno);
5c44784c
JM
685 return;
686 }
687
8e65ff28
AC
688 internal_error (__FILE__, __LINE__,
689 "Got request to store bad register number %d.", regno);
d4f3574e 690}
de57eccd 691\f
6ce2ac0b 692
7bf0983e 693static unsigned long
84346e11
MK
694i386_linux_dr_get (int regnum)
695{
696 int tid;
7bf0983e 697 unsigned long value;
84346e11
MK
698
699 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
700 multi-threaded processes here. For now, pretend there is just
701 one thread. */
39f77062 702 tid = PIDGET (inferior_ptid);
84346e11 703
b9511b9a
MK
704 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
705 ptrace call fails breaks debugging remote targets. The correct
706 way to fix this is to add the hardware breakpoint and watchpoint
707 stuff to the target vectore. For now, just return zero if the
708 ptrace call fails. */
84346e11
MK
709 errno = 0;
710 value = ptrace (PT_READ_U, tid,
711 offsetof (struct user, u_debugreg[regnum]), 0);
712 if (errno != 0)
b9511b9a 713#if 0
84346e11 714 perror_with_name ("Couldn't read debug register");
b9511b9a
MK
715#else
716 return 0;
717#endif
84346e11
MK
718
719 return value;
720}
721
722static void
7bf0983e 723i386_linux_dr_set (int regnum, unsigned long value)
84346e11
MK
724{
725 int tid;
726
727 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
728 multi-threaded processes here. For now, pretend there is just
729 one thread. */
39f77062 730 tid = PIDGET (inferior_ptid);
84346e11
MK
731
732 errno = 0;
733 ptrace (PT_WRITE_U, tid,
734 offsetof (struct user, u_debugreg[regnum]), value);
735 if (errno != 0)
736 perror_with_name ("Couldn't write debug register");
737}
738
739void
7bf0983e 740i386_linux_dr_set_control (unsigned long control)
84346e11
MK
741{
742 i386_linux_dr_set (DR_CONTROL, control);
743}
744
745void
746i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
747{
748 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
749
750 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
751}
752
753void
754i386_linux_dr_reset_addr (int regnum)
755{
756 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
757
758 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
759}
760
7bf0983e 761unsigned long
84346e11
MK
762i386_linux_dr_get_status (void)
763{
764 return i386_linux_dr_get (DR_STATUS);
765}
766\f
767
de57eccd
JM
768/* Interpreting register set info found in core files. */
769
770/* Provide registers to GDB from a core file.
771
772 (We can't use the generic version of this function in
773 core-regset.c, because Linux has *three* different kinds of
774 register set notes. core-regset.c would have to call
6ce2ac0b 775 supply_fpxregset, which most platforms don't have.)
de57eccd
JM
776
777 CORE_REG_SECT points to an array of bytes, which are the contents
778 of a `note' from a core file which BFD thinks might contain
779 register contents. CORE_REG_SIZE is its size.
780
781 WHICH says which register set corelow suspects this is:
04cd15b6
MK
782 0 --- the general-purpose register set, in elf_gregset_t format
783 2 --- the floating-point register set, in elf_fpregset_t format
6ce2ac0b 784 3 --- the extended floating-point register set, in elf_fpxregset_t format
04cd15b6
MK
785
786 REG_ADDR isn't used on Linux. */
de57eccd 787
de57eccd 788static void
04cd15b6
MK
789fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
790 int which, CORE_ADDR reg_addr)
de57eccd 791{
04cd15b6
MK
792 elf_gregset_t gregset;
793 elf_fpregset_t fpregset;
de57eccd
JM
794
795 switch (which)
796 {
797 case 0:
798 if (core_reg_size != sizeof (gregset))
04cd15b6 799 warning ("Wrong size gregset in core file.");
de57eccd
JM
800 else
801 {
802 memcpy (&gregset, core_reg_sect, sizeof (gregset));
803 supply_gregset (&gregset);
804 }
805 break;
806
807 case 2:
808 if (core_reg_size != sizeof (fpregset))
04cd15b6 809 warning ("Wrong size fpregset in core file.");
de57eccd
JM
810 else
811 {
812 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
813 supply_fpregset (&fpregset);
814 }
815 break;
816
6ce2ac0b 817#ifdef HAVE_PTRACE_GETFPXREGS
de57eccd 818 {
6ce2ac0b 819 elf_fpxregset_t fpxregset;
04cd15b6 820
de57eccd 821 case 3:
6ce2ac0b
MK
822 if (core_reg_size != sizeof (fpxregset))
823 warning ("Wrong size fpxregset in core file.");
de57eccd
JM
824 else
825 {
6ce2ac0b
MK
826 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
827 supply_fpxregset (&fpxregset);
de57eccd
JM
828 }
829 break;
830 }
831#endif
832
833 default:
834 /* We've covered all the kinds of registers we know about here,
835 so this must be something we wouldn't know what to do with
836 anyway. Just ignore it. */
837 break;
838 }
839}
a6abb2c0 840\f
6ce2ac0b 841
a6abb2c0
MK
842/* The instruction for a Linux system call is:
843 int $0x80
844 or 0xcd 0x80. */
845
846static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
847
848#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
849
850/* The system call number is stored in the %eax register. */
851#define LINUX_SYSCALL_REGNUM 0 /* %eax */
852
853/* We are specifically interested in the sigreturn and rt_sigreturn
854 system calls. */
855
856#ifndef SYS_sigreturn
857#define SYS_sigreturn 0x77
858#endif
859#ifndef SYS_rt_sigreturn
860#define SYS_rt_sigreturn 0xad
861#endif
862
863/* Offset to saved processor flags, from <asm/sigcontext.h>. */
864#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
865
866/* Resume execution of the inferior process.
867 If STEP is nonzero, single-step it.
868 If SIGNAL is nonzero, give it that signal. */
869
870void
39f77062 871child_resume (ptid_t ptid, int step, enum target_signal signal)
a6abb2c0 872{
39f77062
KB
873 int pid = PIDGET (ptid);
874
a6abb2c0
MK
875 int request = PTRACE_CONT;
876
877 if (pid == -1)
878 /* Resume all threads. */
879 /* I think this only gets used in the non-threaded case, where "resume
39f77062
KB
880 all threads" and "resume inferior_ptid" are the same. */
881 pid = PIDGET (inferior_ptid);
a6abb2c0
MK
882
883 if (step)
884 {
39f77062 885 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
a6abb2c0
MK
886 unsigned char buf[LINUX_SYSCALL_LEN];
887
888 request = PTRACE_SINGLESTEP;
889
890 /* Returning from a signal trampoline is done by calling a
891 special system call (sigreturn or rt_sigreturn, see
892 i386-linux-tdep.c for more information). This system call
893 restores the registers that were saved when the signal was
894 raised, including %eflags. That means that single-stepping
895 won't work. Instead, we'll have to modify the signal context
896 that's about to be restored, and set the trace flag there. */
897
898 /* First check if PC is at a system call. */
899 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
900 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
901 {
39f77062
KB
902 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
903 pid_to_ptid (pid));
a6abb2c0
MK
904
905 /* Then check the system call number. */
906 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
907 {
908 CORE_ADDR sp = read_register (SP_REGNUM);
909 CORE_ADDR addr = sp;
910 unsigned long int eflags;
7bf0983e 911
a6abb2c0
MK
912 if (syscall == SYS_rt_sigreturn)
913 addr = read_memory_integer (sp + 8, 4) + 20;
914
915 /* Set the trace flag in the context that's about to be
916 restored. */
917 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
918 read_memory (addr, (char *) &eflags, 4);
919 eflags |= 0x0100;
920 write_memory (addr, (char *) &eflags, 4);
921 }
922 }
923 }
924
925 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
926 perror_with_name ("ptrace");
927}
5c44784c 928\f
6ce2ac0b 929
04cd15b6
MK
930/* Register that we are able to handle Linux ELF core file formats. */
931
932static struct core_fns linux_elf_core_fns =
933{
934 bfd_target_elf_flavour, /* core_flavour */
935 default_check_format, /* check_format */
936 default_core_sniffer, /* core_sniffer */
937 fetch_core_registers, /* core_read_registers */
938 NULL /* next */
939};
de57eccd
JM
940
941void
fba45db2 942_initialize_i386_linux_nat (void)
de57eccd 943{
04cd15b6 944 add_core_fns (&linux_elf_core_fns);
de57eccd 945}