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