* i386-linux-nat.c (GETREGS_SUPPLIES, supply_gregset,
[deliverable/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) \
3fb1c838 91 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
5c44784c
JM
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]));
3fb1c838
MK
309
310 supply_register (I386_LINUX_ORIG_EAX_REGNUM, (char *) (regp + ORIG_EAX));
917317f4
JM
311}
312
04cd15b6
MK
313/* Fill register REGNO (if it is a general-purpose register) in
314 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
315 do this for all registers. */
6ce2ac0b 316
917317f4 317void
04cd15b6 318fill_gregset (elf_gregset_t *gregsetp, int regno)
917317f4 319{
6ce2ac0b
MK
320 elf_greg_t *regp = (elf_greg_t *) gregsetp;
321 int i;
04cd15b6 322
6ce2ac0b
MK
323 for (i = 0; i < NUM_GREGS; i++)
324 if ((regno == -1 || regno == i))
325 *(regp + regmap[i]) = *(elf_greg_t *) &registers[REGISTER_BYTE (i)];
3fb1c838
MK
326
327 if (regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
328 read_register_gen (I386_LINUX_ORIG_EAX_REGNUM, (char *) (regp + ORIG_EAX));
d4f3574e
SS
329}
330
f60300e7
MK
331#ifdef HAVE_PTRACE_GETREGS
332
04cd15b6
MK
333/* Fetch all general-purpose registers from process/thread TID and
334 store their values in GDB's register array. */
d4f3574e 335
5c44784c 336static void
ed9a39eb 337fetch_regs (int tid)
5c44784c 338{
04cd15b6 339 elf_gregset_t regs;
5c44784c 340
6ce2ac0b 341 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
5c44784c 342 {
f60300e7
MK
343 if (errno == EIO)
344 {
345 /* The kernel we're running on doesn't support the GETREGS
346 request. Reset `have_ptrace_getregs'. */
347 have_ptrace_getregs = 0;
348 return;
349 }
350
6ce2ac0b 351 perror_with_name ("Couldn't get registers");
5c44784c
JM
352 }
353
04cd15b6 354 supply_gregset (&regs);
5c44784c
JM
355}
356
04cd15b6
MK
357/* Store all valid general-purpose registers in GDB's register array
358 into the process/thread specified by TID. */
5c44784c 359
5c44784c 360static void
6ce2ac0b 361store_regs (int tid, int regno)
5c44784c 362{
04cd15b6 363 elf_gregset_t regs;
5c44784c 364
6ce2ac0b
MK
365 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
366 perror_with_name ("Couldn't get registers");
5c44784c 367
6ce2ac0b
MK
368 fill_gregset (&regs, regno);
369
370 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
371 perror_with_name ("Couldn't write registers");
5c44784c
JM
372}
373
f60300e7
MK
374#else
375
376static void fetch_regs (int tid) {}
6ce2ac0b 377static void store_regs (int tid, int regno) {}
f60300e7
MK
378
379#endif
5c44784c 380\f
5c44784c 381
6ce2ac0b 382/* Transfering floating-point registers between GDB, inferiors and cores. */
d4f3574e 383
04cd15b6 384/* Fill GDB's register array with the floating-point register values in
917317f4 385 *FPREGSETP. */
04cd15b6 386
d4f3574e 387void
04cd15b6 388supply_fpregset (elf_fpregset_t *fpregsetp)
d4f3574e 389{
6ce2ac0b 390 i387_supply_fsave ((char *) fpregsetp);
756ed206 391 dummy_sse_values ();
917317f4 392}
d4f3574e 393
04cd15b6
MK
394/* Fill register REGNO (if it is a floating-point register) in
395 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
396 do this for all registers. */
917317f4
JM
397
398void
04cd15b6 399fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
917317f4 400{
6ce2ac0b 401 i387_fill_fsave ((char *) fpregsetp, regno);
d4f3574e
SS
402}
403
f60300e7
MK
404#ifdef HAVE_PTRACE_GETREGS
405
04cd15b6
MK
406/* Fetch all floating-point registers from process/thread TID and store
407 thier values in GDB's register array. */
917317f4 408
d4f3574e 409static void
ed9a39eb 410fetch_fpregs (int tid)
d4f3574e 411{
04cd15b6 412 elf_fpregset_t fpregs;
d4f3574e 413
6ce2ac0b
MK
414 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
415 perror_with_name ("Couldn't get floating point status");
d4f3574e 416
04cd15b6 417 supply_fpregset (&fpregs);
d4f3574e
SS
418}
419
04cd15b6
MK
420/* Store all valid floating-point registers in GDB's register array
421 into the process/thread specified by TID. */
d4f3574e 422
d4f3574e 423static void
6ce2ac0b 424store_fpregs (int tid, int regno)
d4f3574e 425{
04cd15b6 426 elf_fpregset_t fpregs;
d4f3574e 427
6ce2ac0b
MK
428 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
429 perror_with_name ("Couldn't get floating point status");
d4f3574e 430
6ce2ac0b 431 fill_fpregset (&fpregs, regno);
d4f3574e 432
6ce2ac0b
MK
433 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
434 perror_with_name ("Couldn't write floating point status");
d4f3574e
SS
435}
436
f60300e7
MK
437#else
438
439static void fetch_fpregs (int tid) {}
6ce2ac0b 440static void store_fpregs (int tid, int regno) {}
f60300e7
MK
441
442#endif
5c44784c 443\f
d4f3574e 444
6ce2ac0b 445/* Transfering floating-point and SSE registers to and from GDB. */
11cf8741 446
6ce2ac0b 447#ifdef HAVE_PTRACE_GETFPXREGS
04cd15b6
MK
448
449/* Fill GDB's register array with the floating-point and SSE register
6ce2ac0b 450 values in *FPXREGSETP. */
04cd15b6 451
d4f3574e 452static void
6ce2ac0b 453supply_fpxregset (elf_fpxregset_t *fpxregsetp)
d4f3574e 454{
6ce2ac0b 455 i387_supply_fxsave ((char *) fpxregsetp);
d4f3574e
SS
456}
457
6ce2ac0b
MK
458/* Fill register REGNO (if it is a floating-point or SSE register) in
459 *FPXREGSETP with the value in GDB's register array. If REGNO is
460 -1, do this for all registers. */
d4f3574e 461
d4f3574e 462static void
6ce2ac0b 463fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
d4f3574e 464{
6ce2ac0b 465 i387_fill_fxsave ((char *) fpxregsetp, regno);
5c44784c
JM
466}
467
6ce2ac0b 468/* Fetch all registers covered by the PTRACE_GETFPXREGS request from
04cd15b6
MK
469 process/thread TID and store their values in GDB's register array.
470 Return non-zero if successful, zero otherwise. */
5c44784c 471
5c44784c 472static int
6ce2ac0b 473fetch_fpxregs (int tid)
5c44784c 474{
6ce2ac0b 475 elf_fpxregset_t fpxregs;
5c44784c 476
6ce2ac0b 477 if (! have_ptrace_getfpxregs)
5c44784c
JM
478 return 0;
479
6ce2ac0b 480 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
d4f3574e 481 {
5c44784c
JM
482 if (errno == EIO)
483 {
6ce2ac0b 484 have_ptrace_getfpxregs = 0;
5c44784c
JM
485 return 0;
486 }
487
6ce2ac0b 488 perror_with_name ("Couldn't read floating-point and SSE registers");
d4f3574e
SS
489 }
490
6ce2ac0b 491 supply_fpxregset (&fpxregs);
5c44784c
JM
492 return 1;
493}
d4f3574e 494
04cd15b6 495/* Store all valid registers in GDB's register array covered by the
6ce2ac0b 496 PTRACE_SETFPXREGS request into the process/thread specified by TID.
04cd15b6 497 Return non-zero if successful, zero otherwise. */
5c44784c 498
5c44784c 499static int
6ce2ac0b 500store_fpxregs (int tid, int regno)
5c44784c 501{
6ce2ac0b 502 elf_fpxregset_t fpxregs;
5c44784c 503
6ce2ac0b 504 if (! have_ptrace_getfpxregs)
5c44784c 505 return 0;
6ce2ac0b
MK
506
507 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
2866d305
MK
508 {
509 if (errno == EIO)
510 {
511 have_ptrace_getfpxregs = 0;
512 return 0;
513 }
514
515 perror_with_name ("Couldn't read floating-point and SSE registers");
516 }
5c44784c 517
6ce2ac0b 518 fill_fpxregset (&fpxregs, regno);
5c44784c 519
6ce2ac0b
MK
520 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
521 perror_with_name ("Couldn't write floating-point and SSE registers");
5c44784c
JM
522
523 return 1;
524}
525
04cd15b6 526/* Fill the XMM registers in the register array with dummy values. For
5c44784c
JM
527 cases where we don't have access to the XMM registers. I think
528 this is cleaner than printing a warning. For a cleaner solution,
529 we should gdbarchify the i386 family. */
04cd15b6 530
5c44784c 531static void
04cd15b6 532dummy_sse_values (void)
5c44784c
JM
533{
534 /* C doesn't have a syntax for NaN's, so write it out as an array of
535 longs. */
536 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
537 static long mxcsr = 0x1f80;
538 int reg;
539
540 for (reg = 0; reg < 8; reg++)
541 supply_register (XMM0_REGNUM + reg, (char *) dummy);
542 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
d4f3574e
SS
543}
544
5c44784c
JM
545#else
546
f0373401
MK
547static int fetch_fpxregs (int tid) { return 0; }
548static int store_fpxregs (int tid, int regno) { return 0; }
04cd15b6 549static void dummy_sse_values (void) {}
5c44784c 550
6ce2ac0b 551#endif /* HAVE_PTRACE_GETFPXREGS */
5c44784c 552\f
6ce2ac0b 553
5c44784c 554/* Transferring arbitrary registers between GDB and inferior. */
d4f3574e 555
d5d65353
PS
556/* Check if register REGNO in the child process is accessible.
557 If we are accessing registers directly via the U area, only the
558 general-purpose registers are available.
559 All registers should be accessible if we have GETREGS support. */
560
561int
562cannot_fetch_register (int regno)
563{
564 if (! have_ptrace_getregs)
565 return OLD_CANNOT_FETCH_REGISTER (regno);
566 return 0;
567}
568int
569cannot_store_register (int regno)
570{
571 if (! have_ptrace_getregs)
572 return OLD_CANNOT_STORE_REGISTER (regno);
573 return 0;
574}
575
04cd15b6
MK
576/* Fetch register REGNO from the child process. If REGNO is -1, do
577 this for all registers (including the floating point and SSE
578 registers). */
d4f3574e
SS
579
580void
917317f4 581fetch_inferior_registers (int regno)
d4f3574e 582{
ed9a39eb
JM
583 int tid;
584
f60300e7
MK
585 /* Use the old method of peeking around in `struct user' if the
586 GETREGS request isn't available. */
587 if (! have_ptrace_getregs)
588 {
589 old_fetch_inferior_registers (regno);
590 return;
591 }
592
04cd15b6 593 /* Linux LWP ID's are process ID's. */
39f77062
KB
594 if ((tid = TIDGET (inferior_ptid)) == 0)
595 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 596
6ce2ac0b 597 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
04cd15b6 598 transfers more registers in one system call, and we'll cache the
6ce2ac0b 599 results. But remember that fetch_fpxregs can fail, and return
04cd15b6 600 zero. */
5c44784c
JM
601 if (regno == -1)
602 {
ed9a39eb 603 fetch_regs (tid);
f60300e7
MK
604
605 /* The call above might reset `have_ptrace_getregs'. */
606 if (! have_ptrace_getregs)
607 {
608 old_fetch_inferior_registers (-1);
609 return;
610 }
611
6ce2ac0b 612 if (fetch_fpxregs (tid))
5c44784c 613 return;
ed9a39eb 614 fetch_fpregs (tid);
5c44784c
JM
615 return;
616 }
d4f3574e 617
5c44784c
JM
618 if (GETREGS_SUPPLIES (regno))
619 {
ed9a39eb 620 fetch_regs (tid);
5c44784c
JM
621 return;
622 }
623
6ce2ac0b 624 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 625 {
6ce2ac0b 626 if (fetch_fpxregs (tid))
5c44784c
JM
627 return;
628
629 /* Either our processor or our kernel doesn't support the SSE
630 registers, so read the FP registers in the traditional way,
631 and fill the SSE registers with dummy values. It would be
632 more graceful to handle differences in the register set using
633 gdbarch. Until then, this will at least make things work
634 plausibly. */
ed9a39eb 635 fetch_fpregs (tid);
5c44784c
JM
636 return;
637 }
638
8e65ff28
AC
639 internal_error (__FILE__, __LINE__,
640 "Got request for bad register number %d.", regno);
d4f3574e
SS
641}
642
04cd15b6
MK
643/* Store register REGNO back into the child process. If REGNO is -1,
644 do this for all registers (including the floating point and SSE
645 registers). */
d4f3574e 646void
04cd15b6 647store_inferior_registers (int regno)
d4f3574e 648{
ed9a39eb
JM
649 int tid;
650
f60300e7
MK
651 /* Use the old method of poking around in `struct user' if the
652 SETREGS request isn't available. */
653 if (! have_ptrace_getregs)
654 {
655 old_store_inferior_registers (regno);
656 return;
657 }
658
04cd15b6 659 /* Linux LWP ID's are process ID's. */
39f77062
KB
660 if ((tid = TIDGET (inferior_ptid)) == 0)
661 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 662
6ce2ac0b 663 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
04cd15b6 664 transfers more registers in one system call. But remember that
6ce2ac0b 665 store_fpxregs can fail, and return zero. */
5c44784c
JM
666 if (regno == -1)
667 {
6ce2ac0b
MK
668 store_regs (tid, regno);
669 if (store_fpxregs (tid, regno))
5c44784c 670 return;
6ce2ac0b 671 store_fpregs (tid, regno);
5c44784c
JM
672 return;
673 }
d4f3574e 674
5c44784c
JM
675 if (GETREGS_SUPPLIES (regno))
676 {
6ce2ac0b 677 store_regs (tid, regno);
5c44784c
JM
678 return;
679 }
680
6ce2ac0b 681 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 682 {
6ce2ac0b 683 if (store_fpxregs (tid, regno))
5c44784c
JM
684 return;
685
686 /* Either our processor or our kernel doesn't support the SSE
04cd15b6
MK
687 registers, so just write the FP registers in the traditional
688 way. */
6ce2ac0b 689 store_fpregs (tid, regno);
5c44784c
JM
690 return;
691 }
692
8e65ff28
AC
693 internal_error (__FILE__, __LINE__,
694 "Got request to store bad register number %d.", regno);
d4f3574e 695}
de57eccd 696\f
6ce2ac0b 697
7bf0983e 698static unsigned long
84346e11
MK
699i386_linux_dr_get (int regnum)
700{
701 int tid;
7bf0983e 702 unsigned long value;
84346e11
MK
703
704 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
705 multi-threaded processes here. For now, pretend there is just
706 one thread. */
39f77062 707 tid = PIDGET (inferior_ptid);
84346e11 708
b9511b9a
MK
709 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
710 ptrace call fails breaks debugging remote targets. The correct
711 way to fix this is to add the hardware breakpoint and watchpoint
712 stuff to the target vectore. For now, just return zero if the
713 ptrace call fails. */
84346e11
MK
714 errno = 0;
715 value = ptrace (PT_READ_U, tid,
716 offsetof (struct user, u_debugreg[regnum]), 0);
717 if (errno != 0)
b9511b9a 718#if 0
84346e11 719 perror_with_name ("Couldn't read debug register");
b9511b9a
MK
720#else
721 return 0;
722#endif
84346e11
MK
723
724 return value;
725}
726
727static void
7bf0983e 728i386_linux_dr_set (int regnum, unsigned long value)
84346e11
MK
729{
730 int tid;
731
732 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
733 multi-threaded processes here. For now, pretend there is just
734 one thread. */
39f77062 735 tid = PIDGET (inferior_ptid);
84346e11
MK
736
737 errno = 0;
738 ptrace (PT_WRITE_U, tid,
739 offsetof (struct user, u_debugreg[regnum]), value);
740 if (errno != 0)
741 perror_with_name ("Couldn't write debug register");
742}
743
744void
7bf0983e 745i386_linux_dr_set_control (unsigned long control)
84346e11
MK
746{
747 i386_linux_dr_set (DR_CONTROL, control);
748}
749
750void
751i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
752{
753 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
754
755 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
756}
757
758void
759i386_linux_dr_reset_addr (int regnum)
760{
761 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
762
763 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
764}
765
7bf0983e 766unsigned long
84346e11
MK
767i386_linux_dr_get_status (void)
768{
769 return i386_linux_dr_get (DR_STATUS);
770}
771\f
772
de57eccd
JM
773/* Interpreting register set info found in core files. */
774
775/* Provide registers to GDB from a core file.
776
777 (We can't use the generic version of this function in
778 core-regset.c, because Linux has *three* different kinds of
779 register set notes. core-regset.c would have to call
6ce2ac0b 780 supply_fpxregset, which most platforms don't have.)
de57eccd
JM
781
782 CORE_REG_SECT points to an array of bytes, which are the contents
783 of a `note' from a core file which BFD thinks might contain
784 register contents. CORE_REG_SIZE is its size.
785
786 WHICH says which register set corelow suspects this is:
04cd15b6
MK
787 0 --- the general-purpose register set, in elf_gregset_t format
788 2 --- the floating-point register set, in elf_fpregset_t format
6ce2ac0b 789 3 --- the extended floating-point register set, in elf_fpxregset_t format
04cd15b6
MK
790
791 REG_ADDR isn't used on Linux. */
de57eccd 792
de57eccd 793static void
04cd15b6
MK
794fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
795 int which, CORE_ADDR reg_addr)
de57eccd 796{
04cd15b6
MK
797 elf_gregset_t gregset;
798 elf_fpregset_t fpregset;
de57eccd
JM
799
800 switch (which)
801 {
802 case 0:
803 if (core_reg_size != sizeof (gregset))
04cd15b6 804 warning ("Wrong size gregset in core file.");
de57eccd
JM
805 else
806 {
807 memcpy (&gregset, core_reg_sect, sizeof (gregset));
808 supply_gregset (&gregset);
809 }
810 break;
811
812 case 2:
813 if (core_reg_size != sizeof (fpregset))
04cd15b6 814 warning ("Wrong size fpregset in core file.");
de57eccd
JM
815 else
816 {
817 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
818 supply_fpregset (&fpregset);
819 }
820 break;
821
6ce2ac0b 822#ifdef HAVE_PTRACE_GETFPXREGS
de57eccd 823 {
6ce2ac0b 824 elf_fpxregset_t fpxregset;
04cd15b6 825
de57eccd 826 case 3:
6ce2ac0b
MK
827 if (core_reg_size != sizeof (fpxregset))
828 warning ("Wrong size fpxregset in core file.");
de57eccd
JM
829 else
830 {
6ce2ac0b
MK
831 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
832 supply_fpxregset (&fpxregset);
de57eccd
JM
833 }
834 break;
835 }
836#endif
837
838 default:
839 /* We've covered all the kinds of registers we know about here,
840 so this must be something we wouldn't know what to do with
841 anyway. Just ignore it. */
842 break;
843 }
844}
a6abb2c0 845\f
6ce2ac0b 846
a6abb2c0
MK
847/* The instruction for a Linux system call is:
848 int $0x80
849 or 0xcd 0x80. */
850
851static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
852
853#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
854
855/* The system call number is stored in the %eax register. */
856#define LINUX_SYSCALL_REGNUM 0 /* %eax */
857
858/* We are specifically interested in the sigreturn and rt_sigreturn
859 system calls. */
860
861#ifndef SYS_sigreturn
862#define SYS_sigreturn 0x77
863#endif
864#ifndef SYS_rt_sigreturn
865#define SYS_rt_sigreturn 0xad
866#endif
867
868/* Offset to saved processor flags, from <asm/sigcontext.h>. */
869#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
870
871/* Resume execution of the inferior process.
872 If STEP is nonzero, single-step it.
873 If SIGNAL is nonzero, give it that signal. */
874
875void
39f77062 876child_resume (ptid_t ptid, int step, enum target_signal signal)
a6abb2c0 877{
39f77062
KB
878 int pid = PIDGET (ptid);
879
a6abb2c0
MK
880 int request = PTRACE_CONT;
881
882 if (pid == -1)
883 /* Resume all threads. */
884 /* I think this only gets used in the non-threaded case, where "resume
39f77062
KB
885 all threads" and "resume inferior_ptid" are the same. */
886 pid = PIDGET (inferior_ptid);
a6abb2c0
MK
887
888 if (step)
889 {
39f77062 890 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
a6abb2c0
MK
891 unsigned char buf[LINUX_SYSCALL_LEN];
892
893 request = PTRACE_SINGLESTEP;
894
895 /* Returning from a signal trampoline is done by calling a
896 special system call (sigreturn or rt_sigreturn, see
897 i386-linux-tdep.c for more information). This system call
898 restores the registers that were saved when the signal was
899 raised, including %eflags. That means that single-stepping
900 won't work. Instead, we'll have to modify the signal context
901 that's about to be restored, and set the trace flag there. */
902
903 /* First check if PC is at a system call. */
904 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
905 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
906 {
39f77062
KB
907 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
908 pid_to_ptid (pid));
a6abb2c0
MK
909
910 /* Then check the system call number. */
911 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
912 {
913 CORE_ADDR sp = read_register (SP_REGNUM);
914 CORE_ADDR addr = sp;
915 unsigned long int eflags;
7bf0983e 916
a6abb2c0
MK
917 if (syscall == SYS_rt_sigreturn)
918 addr = read_memory_integer (sp + 8, 4) + 20;
919
920 /* Set the trace flag in the context that's about to be
921 restored. */
922 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
923 read_memory (addr, (char *) &eflags, 4);
924 eflags |= 0x0100;
925 write_memory (addr, (char *) &eflags, 4);
926 }
927 }
928 }
929
930 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
931 perror_with_name ("ptrace");
932}
5c44784c 933\f
6ce2ac0b 934
04cd15b6
MK
935/* Register that we are able to handle Linux ELF core file formats. */
936
937static struct core_fns linux_elf_core_fns =
938{
939 bfd_target_elf_flavour, /* core_flavour */
940 default_check_format, /* check_format */
941 default_core_sniffer, /* core_sniffer */
942 fetch_core_registers, /* core_read_registers */
943 NULL /* next */
944};
de57eccd
JM
945
946void
fba45db2 947_initialize_i386_linux_nat (void)
de57eccd 948{
04cd15b6 949 add_core_fns (&linux_elf_core_fns);
de57eccd 950}
This page took 0.183087 seconds and 4 git commands to generate.