* gdb.java/configure.in (AC_INIT): Use jmisc.exp.
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
CommitLineData
d4f3574e 1/* Native-dependent code for Linux running on i386's, for GDB.
ed40e7af 2 Copyright (C) 1999, 2000 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"
24
04cd15b6 25/* For i386_linux_skip_solib_resolver. */
d4f3574e 26#include "symtab.h"
d4f3574e
SS
27#include "symfile.h"
28#include "objfiles.h"
29
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
c60c0f5f
MS
38/* Prototypes for supply_gregset etc. */
39#include "gregset.h"
40
04cd15b6
MK
41/* On Linux, threads are implemented as pseudo-processes, in which
42 case we may be tracing more than one process at a time. In that
43 case, inferior_pid will contain the main process ID and the
44 individual thread (process) ID mashed together. These macros are
45 used to separate them out. These definitions should be overridden
46 if thread support is included. */
ed9a39eb
JM
47
48#if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
49#define PIDGET(PID) PID
50#define TIDGET(PID) 0
51#endif
52
d4f3574e 53
04cd15b6
MK
54/* The register sets used in Linux ELF core-dumps are identical to the
55 register sets in `struct user' that is used for a.out core-dumps,
56 and is also used by `ptrace'. The corresponding types are
57 `elf_gregset_t' for the general-purpose registers (with
58 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
59 for the floating-point registers.
60
61 Those types used to be available under the names `gregset_t' and
62 `fpregset_t' too, and this file used those names in the past. But
63 those names are now used for the register sets used in the
64 `mcontext_t' type, and have a different size and layout. */
65
66/* Mapping between the general-purpose registers in `struct user'
67 format and GDB's register array layout. */
d4f3574e
SS
68static int regmap[] =
69{
70 EAX, ECX, EDX, EBX,
71 UESP, EBP, ESI, EDI,
72 EIP, EFL, CS, SS,
04cd15b6 73 DS, ES, FS, GS
d4f3574e
SS
74};
75
5c44784c
JM
76/* Which ptrace request retrieves which registers?
77 These apply to the corresponding SET requests as well. */
78#define GETREGS_SUPPLIES(regno) \
79 (0 <= (regno) && (regno) <= 15)
80#define GETFPREGS_SUPPLIES(regno) \
81 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
82#define GETXFPREGS_SUPPLIES(regno) \
83 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
84
f60300e7
MK
85/* Does the current host support the GETREGS request? */
86int have_ptrace_getregs =
87#ifdef HAVE_PTRACE_GETREGS
88 1
89#else
90 0
91#endif
92;
93
5c44784c
JM
94/* Does the current host support the GETXFPREGS request? The header
95 file may or may not define it, and even if it is defined, the
96 kernel will return EIO if it's running on a pre-SSE processor.
97
c2d11a7d
JM
98 PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
99 Linux kernel patch for SSE support. That patch may or may not
100 actually make it into the official distribution. If you find that
101 years have gone by since this stuff was added, and Linux isn't
102 using PTRACE_GETXFPREGS, that means that our patch didn't make it,
103 and you can delete this, and the related code.
104
5c44784c
JM
105 My instinct is to attach this to some architecture- or
106 target-specific data structure, but really, a particular GDB
107 process can only run on top of one kernel at a time. So it's okay
108 for this to be a simple variable. */
109int have_ptrace_getxfpregs =
110#ifdef HAVE_PTRACE_GETXFPREGS
111 1
112#else
113 0
114#endif
115;
116
f60300e7 117\f
97780f5f
JB
118/* Fetching registers directly from the U area, one at a time. */
119
f60300e7
MK
120/* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
121 The problem is that we define FETCH_INFERIOR_REGISTERS since we
122 want to use our own versions of {fetch,store}_inferior_registers
123 that use the GETREGS request. This means that the code in
124 `infptrace.c' is #ifdef'd out. But we need to fall back on that
125 code when GDB is running on top of a kernel that doesn't support
126 the GETREGS request. I want to avoid changing `infptrace.c' right
127 now. */
128
318b21ef
MK
129#ifndef PT_READ_U
130#define PT_READ_U PTRACE_PEEKUSR
131#endif
132#ifndef PT_WRITE_U
133#define PT_WRITE_U PTRACE_POKEUSR
134#endif
135
f60300e7
MK
136/* Default the type of the ptrace transfer to int. */
137#ifndef PTRACE_XFER_TYPE
138#define PTRACE_XFER_TYPE int
139#endif
140
141/* Registers we shouldn't try to fetch. */
142#if !defined (CANNOT_FETCH_REGISTER)
143#define CANNOT_FETCH_REGISTER(regno) 0
144#endif
145
146/* Fetch one register. */
147
148static void
149fetch_register (regno)
150 int regno;
151{
152 /* This isn't really an address. But ptrace thinks of it as one. */
153 CORE_ADDR regaddr;
154 char mess[128]; /* For messages */
155 register int i;
156 unsigned int offset; /* Offset of registers within the u area. */
157 char buf[MAX_REGISTER_RAW_SIZE];
158 int tid;
159
160 if (CANNOT_FETCH_REGISTER (regno))
161 {
162 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
163 supply_register (regno, buf);
164 return;
165 }
166
167 /* Overload thread id onto process id */
168 if ((tid = TIDGET (inferior_pid)) == 0)
169 tid = inferior_pid; /* no thread id, just use process id */
170
171 offset = U_REGS_OFFSET;
172
173 regaddr = register_addr (regno, offset);
174 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
175 {
176 errno = 0;
177 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
178 (PTRACE_ARG3_TYPE) regaddr, 0);
179 regaddr += sizeof (PTRACE_XFER_TYPE);
180 if (errno != 0)
181 {
182 sprintf (mess, "reading register %s (#%d)",
183 REGISTER_NAME (regno), regno);
184 perror_with_name (mess);
185 }
186 }
187 supply_register (regno, buf);
188}
189
190/* Fetch register values from the inferior.
191 If REGNO is negative, do this for all registers.
192 Otherwise, REGNO specifies which register (so we can save time). */
193
194void
195old_fetch_inferior_registers (regno)
196 int regno;
197{
198 if (regno >= 0)
199 {
200 fetch_register (regno);
201 }
202 else
203 {
204 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
205 {
206 fetch_register (regno);
207 }
208 }
209}
210
211/* Registers we shouldn't try to store. */
212#if !defined (CANNOT_STORE_REGISTER)
213#define CANNOT_STORE_REGISTER(regno) 0
214#endif
215
216/* Store one register. */
217
218static void
219store_register (regno)
220 int regno;
221{
222 /* This isn't really an address. But ptrace thinks of it as one. */
223 CORE_ADDR regaddr;
224 char mess[128]; /* For messages */
225 register int i;
226 unsigned int offset; /* Offset of registers within the u area. */
227 int tid;
228
229 if (CANNOT_STORE_REGISTER (regno))
230 {
231 return;
232 }
233
234 /* Overload thread id onto process id */
235 if ((tid = TIDGET (inferior_pid)) == 0)
236 tid = inferior_pid; /* no thread id, just use process id */
237
238 offset = U_REGS_OFFSET;
239
240 regaddr = register_addr (regno, offset);
241 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
242 {
243 errno = 0;
244 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
245 *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
246 regaddr += sizeof (PTRACE_XFER_TYPE);
247 if (errno != 0)
248 {
249 sprintf (mess, "writing register %s (#%d)",
250 REGISTER_NAME (regno), regno);
251 perror_with_name (mess);
252 }
253 }
254}
255
256/* Store our register values back into the inferior.
257 If REGNO is negative, do this for all registers.
258 Otherwise, REGNO specifies which register (so we can save time). */
259
260void
261old_store_inferior_registers (regno)
262 int regno;
263{
264 if (regno >= 0)
265 {
266 store_register (regno);
267 }
268 else
269 {
270 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
271 {
272 store_register (regno);
273 }
274 }
275}
276
5c44784c 277\f
04cd15b6
MK
278/* Transfering the general-purpose registers between GDB, inferiors
279 and core files. */
280
281/* Fill GDB's register array with the genereal-purpose register values
282 in *GREGSETP. */
5c44784c 283
d4f3574e 284void
04cd15b6 285supply_gregset (elf_gregset_t *gregsetp)
d4f3574e 286{
04cd15b6
MK
287 elf_greg_t *regp = (elf_greg_t *) gregsetp;
288 int regi;
d4f3574e 289
917317f4 290 for (regi = 0; regi < NUM_GREGS; regi++)
04cd15b6 291 supply_register (regi, (char *) (regp + regmap[regi]));
d4f3574e
SS
292}
293
04cd15b6
MK
294/* Convert the valid general-purpose register values in GDB's register
295 array to `struct user' format and store them in *GREGSETP. The
296 array VALID indicates which register values are valid. If VALID is
297 NULL, all registers are assumed to be valid. */
5c44784c 298
04cd15b6
MK
299static void
300convert_to_gregset (elf_gregset_t *gregsetp, signed char *valid)
d4f3574e 301{
04cd15b6 302 elf_greg_t *regp = (elf_greg_t *) gregsetp;
d4f3574e 303 int regi;
d4f3574e 304
917317f4
JM
305 for (regi = 0; regi < NUM_GREGS; regi++)
306 if (! valid || valid[regi])
307 *(regp + regmap[regi]) = * (int *) &registers[REGISTER_BYTE (regi)];
308}
309
04cd15b6
MK
310/* Fill register REGNO (if it is a general-purpose register) in
311 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
312 do this for all registers. */
917317f4 313void
04cd15b6 314fill_gregset (elf_gregset_t *gregsetp, int regno)
917317f4
JM
315{
316 if (regno == -1)
04cd15b6
MK
317 {
318 convert_to_gregset (gregsetp, NULL);
319 return;
320 }
321
322 if (GETREGS_SUPPLIES (regno))
d4f3574e 323 {
917317f4 324 signed char valid[NUM_GREGS];
04cd15b6 325
917317f4
JM
326 memset (valid, 0, sizeof (valid));
327 valid[regno] = 1;
04cd15b6
MK
328
329 convert_to_gregset (gregsetp, valid);
d4f3574e
SS
330 }
331}
332
f60300e7
MK
333#ifdef HAVE_PTRACE_GETREGS
334
04cd15b6
MK
335/* Fetch all general-purpose registers from process/thread TID and
336 store their values in GDB's register array. */
d4f3574e 337
5c44784c 338static void
ed9a39eb 339fetch_regs (int tid)
5c44784c 340{
04cd15b6
MK
341 elf_gregset_t regs;
342 int ret;
5c44784c 343
04cd15b6 344 ret = ptrace (PTRACE_GETREGS, tid, 0, (int) &regs);
5c44784c
JM
345 if (ret < 0)
346 {
f60300e7
MK
347 if (errno == EIO)
348 {
349 /* The kernel we're running on doesn't support the GETREGS
350 request. Reset `have_ptrace_getregs'. */
351 have_ptrace_getregs = 0;
352 return;
353 }
354
04cd15b6 355 warning ("Couldn't get registers.");
5c44784c
JM
356 return;
357 }
358
04cd15b6 359 supply_gregset (&regs);
5c44784c
JM
360}
361
04cd15b6
MK
362/* Store all valid general-purpose registers in GDB's register array
363 into the process/thread specified by TID. */
5c44784c 364
5c44784c 365static void
ed9a39eb 366store_regs (int tid)
5c44784c 367{
04cd15b6
MK
368 elf_gregset_t regs;
369 int ret;
5c44784c 370
04cd15b6 371 ret = ptrace (PTRACE_GETREGS, tid, 0, (int) &regs);
5c44784c
JM
372 if (ret < 0)
373 {
04cd15b6 374 warning ("Couldn't get registers.");
5c44784c
JM
375 return;
376 }
377
04cd15b6 378 convert_to_gregset (&regs, register_valid);
5c44784c 379
04cd15b6 380 ret = ptrace (PTRACE_SETREGS, tid, 0, (int) &regs);
5c44784c
JM
381 if (ret < 0)
382 {
04cd15b6 383 warning ("Couldn't write registers.");
5c44784c
JM
384 return;
385 }
386}
387
f60300e7
MK
388#else
389
390static void fetch_regs (int tid) {}
391static void store_regs (int tid) {}
392
393#endif
394
5c44784c
JM
395\f
396/* Transfering floating-point registers between GDB, inferiors and cores. */
397
04cd15b6
MK
398/* What is the address of st(N) within the floating-point register set F? */
399#define FPREG_ADDR(f, n) ((char *) &(f)->st_space + (n) * 10)
d4f3574e 400
04cd15b6 401/* Fill GDB's register array with the floating-point register values in
917317f4 402 *FPREGSETP. */
04cd15b6 403
d4f3574e 404void
04cd15b6 405supply_fpregset (elf_fpregset_t *fpregsetp)
d4f3574e 406{
04cd15b6 407 int reg;
b948cda9 408 long l;
917317f4
JM
409
410 /* Supply the floating-point registers. */
04cd15b6
MK
411 for (reg = 0; reg < 8; reg++)
412 supply_register (FP0_REGNUM + reg, FPREG_ADDR (fpregsetp, reg));
917317f4 413
b948cda9
MK
414 /* We have to mask off the reserved bits in *FPREGSETP before
415 storing the values in GDB's register file. */
416#define supply(REGNO, MEMBER) \
417 l = fpregsetp->MEMBER & 0xffff; \
418 supply_register (REGNO, (char *) &l)
419
420 supply (FCTRL_REGNUM, cwd);
421 supply (FSTAT_REGNUM, swd);
422 supply (FTAG_REGNUM, twd);
917317f4 423 supply_register (FCOFF_REGNUM, (char *) &fpregsetp->fip);
b948cda9 424 supply (FDS_REGNUM, fos);
917317f4 425 supply_register (FDOFF_REGNUM, (char *) &fpregsetp->foo);
917317f4 426
b948cda9
MK
427#undef supply
428
429 /* Extract the code segment and opcode from the "fcs" member. */
430 l = fpregsetp->fcs & 0xffff;
431 supply_register (FCS_REGNUM, (char *) &l);
917317f4 432
b948cda9
MK
433 l = (fpregsetp->fcs >> 16) & ((1 << 11) - 1);
434 supply_register (FOP_REGNUM, (char *) &l);
d4f3574e
SS
435}
436
04cd15b6
MK
437/* Convert the valid floating-point register values in GDB's register
438 array to `struct user' format and store them in *FPREGSETP. The
439 array VALID indicates which register values are valid. If VALID is
440 NULL, all registers are assumed to be valid. */
d4f3574e 441
04cd15b6
MK
442static void
443convert_to_fpregset (elf_fpregset_t *fpregsetp, signed char *valid)
d4f3574e 444{
04cd15b6 445 int reg;
917317f4
JM
446
447 /* Fill in the floating-point registers. */
04cd15b6
MK
448 for (reg = 0; reg < 8; reg++)
449 if (!valid || valid[reg])
450 memcpy (FPREG_ADDR (fpregsetp, reg),
451 &registers[REGISTER_BYTE (FP0_REGNUM + reg)],
452 REGISTER_RAW_SIZE(FP0_REGNUM + reg));
917317f4 453
b948cda9
MK
454 /* We're not supposed to touch the reserved bits in *FPREGSETP. */
455
917317f4
JM
456#define fill(MEMBER, REGNO) \
457 if (! valid || valid[(REGNO)]) \
b948cda9
MK
458 fpregsetp->MEMBER \
459 = ((fpregsetp->MEMBER & ~0xffff) \
460 | (* (int *) &registers[REGISTER_BYTE (REGNO)] & 0xffff))
461
462#define fill_register(MEMBER, REGNO) \
463 if (! valid || valid[(REGNO)]) \
464 memcpy (&fpregsetp->MEMBER, &registers[REGISTER_BYTE (REGNO)], \
465 sizeof (fpregsetp->MEMBER))
917317f4
JM
466
467 fill (cwd, FCTRL_REGNUM);
468 fill (swd, FSTAT_REGNUM);
469 fill (twd, FTAG_REGNUM);
b948cda9 470 fill_register (fip, FCOFF_REGNUM);
917317f4 471 fill (foo, FDOFF_REGNUM);
b948cda9 472 fill_register (fos, FDS_REGNUM);
917317f4
JM
473
474#undef fill
b948cda9 475#undef fill_register
917317f4
JM
476
477 if (! valid || valid[FCS_REGNUM])
478 fpregsetp->fcs
479 = ((fpregsetp->fcs & ~0xffff)
480 | (* (int *) &registers[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
481
482 if (! valid || valid[FOP_REGNUM])
483 fpregsetp->fcs
484 = ((fpregsetp->fcs & 0xffff)
485 | ((*(int *) &registers[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
486 << 16));
487}
d4f3574e 488
04cd15b6
MK
489/* Fill register REGNO (if it is a floating-point register) in
490 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
491 do this for all registers. */
917317f4
JM
492
493void
04cd15b6 494fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
917317f4 495{
04cd15b6
MK
496 if (regno == -1)
497 {
498 convert_to_fpregset (fpregsetp, NULL);
499 return;
500 }
501
502 if (GETFPREGS_SUPPLIES(regno))
503 {
504 signed char valid[MAX_NUM_REGS];
505
506 memset (valid, 0, sizeof (valid));
507 valid[regno] = 1;
508
509 convert_to_fpregset (fpregsetp, valid);
510 }
d4f3574e
SS
511}
512
f60300e7
MK
513#ifdef HAVE_PTRACE_GETREGS
514
04cd15b6
MK
515/* Fetch all floating-point registers from process/thread TID and store
516 thier values in GDB's register array. */
917317f4 517
d4f3574e 518static void
ed9a39eb 519fetch_fpregs (int tid)
d4f3574e 520{
04cd15b6
MK
521 elf_fpregset_t fpregs;
522 int ret;
d4f3574e 523
04cd15b6 524 ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs);
917317f4 525 if (ret < 0)
d4f3574e 526 {
04cd15b6 527 warning ("Couldn't get floating point status.");
d4f3574e
SS
528 return;
529 }
530
04cd15b6 531 supply_fpregset (&fpregs);
d4f3574e
SS
532}
533
04cd15b6
MK
534/* Store all valid floating-point registers in GDB's register array
535 into the process/thread specified by TID. */
d4f3574e 536
d4f3574e 537static void
ed9a39eb 538store_fpregs (int tid)
d4f3574e 539{
04cd15b6 540 elf_fpregset_t fpregs;
917317f4 541 int ret;
d4f3574e 542
04cd15b6 543 ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs);
917317f4 544 if (ret < 0)
d4f3574e 545 {
04cd15b6 546 warning ("Couldn't get floating point status.");
d4f3574e
SS
547 return;
548 }
549
04cd15b6 550 convert_to_fpregset (&fpregs, register_valid);
d4f3574e 551
04cd15b6 552 ret = ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs);
917317f4 553 if (ret < 0)
d4f3574e 554 {
04cd15b6 555 warning ("Couldn't write floating point status.");
d4f3574e
SS
556 return;
557 }
d4f3574e
SS
558}
559
f60300e7
MK
560#else
561
562static void fetch_fpregs (int tid) {}
563static void store_fpregs (int tid) {}
564
565#endif
566
5c44784c
JM
567\f
568/* Transfering floating-point and SSE registers to and from GDB. */
d4f3574e 569
11cf8741
JM
570/* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
571 Linux kernel patch for SSE support. That patch may or may not
572 actually make it into the official distribution. If you find that
573 years have gone by since this code was added, and Linux isn't using
574 PTRACE_GETXFPREGS, that means that our patch didn't make it, and
575 you can delete this code. */
576
5c44784c 577#ifdef HAVE_PTRACE_GETXFPREGS
04cd15b6
MK
578
579/* Fill GDB's register array with the floating-point and SSE register
580 values in *XFPREGS. */
581
d4f3574e 582static void
5c44784c 583supply_xfpregset (struct user_xfpregs_struct *xfpregs)
d4f3574e 584{
5c44784c 585 int reg;
d4f3574e 586
5c44784c
JM
587 /* Supply the floating-point registers. */
588 for (reg = 0; reg < 8; reg++)
589 supply_register (FP0_REGNUM + reg, (char *) &xfpregs->st_space[reg]);
590
591 {
592 supply_register (FCTRL_REGNUM, (char *) &xfpregs->cwd);
593 supply_register (FSTAT_REGNUM, (char *) &xfpregs->swd);
594 supply_register (FTAG_REGNUM, (char *) &xfpregs->twd);
595 supply_register (FCOFF_REGNUM, (char *) &xfpregs->fip);
596 supply_register (FDS_REGNUM, (char *) &xfpregs->fos);
597 supply_register (FDOFF_REGNUM, (char *) &xfpregs->foo);
598
599 /* Extract the code segment and opcode from the "fcs" member. */
d4f3574e 600 {
5c44784c
JM
601 long l;
602
603 l = xfpregs->fcs & 0xffff;
604 supply_register (FCS_REGNUM, (char *) &l);
605
606 l = (xfpregs->fcs >> 16) & ((1 << 11) - 1);
607 supply_register (FOP_REGNUM, (char *) &l);
d4f3574e 608 }
5c44784c 609 }
d4f3574e 610
5c44784c
JM
611 /* Supply the SSE registers. */
612 for (reg = 0; reg < 8; reg++)
613 supply_register (XMM0_REGNUM + reg, (char *) &xfpregs->xmm_space[reg]);
614 supply_register (MXCSR_REGNUM, (char *) &xfpregs->mxcsr);
d4f3574e
SS
615}
616
04cd15b6
MK
617/* Convert the valid floating-point and SSE registers in GDB's
618 register array to `struct user' format and store them in *XFPREGS.
619 The array VALID indicates which registers are valid. If VALID is
620 NULL, all registers are assumed to be valid. */
d4f3574e 621
d4f3574e 622static void
5c44784c 623convert_to_xfpregset (struct user_xfpregs_struct *xfpregs,
5c44784c 624 signed char *valid)
d4f3574e 625{
5c44784c 626 int reg;
d4f3574e 627
5c44784c
JM
628 /* Fill in the floating-point registers. */
629 for (reg = 0; reg < 8; reg++)
630 if (!valid || valid[reg])
631 memcpy (&xfpregs->st_space[reg],
632 &registers[REGISTER_BYTE (FP0_REGNUM + reg)],
633 REGISTER_RAW_SIZE(FP0_REGNUM + reg));
634
635#define fill(MEMBER, REGNO) \
636 if (! valid || valid[(REGNO)]) \
637 memcpy (&xfpregs->MEMBER, &registers[REGISTER_BYTE (REGNO)], \
638 sizeof (xfpregs->MEMBER))
639
640 fill (cwd, FCTRL_REGNUM);
641 fill (swd, FSTAT_REGNUM);
642 fill (twd, FTAG_REGNUM);
643 fill (fip, FCOFF_REGNUM);
644 fill (foo, FDOFF_REGNUM);
645 fill (fos, FDS_REGNUM);
646
647#undef fill
648
649 if (! valid || valid[FCS_REGNUM])
650 xfpregs->fcs
651 = ((xfpregs->fcs & ~0xffff)
652 | (* (int *) &registers[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
653
654 if (! valid || valid[FOP_REGNUM])
655 xfpregs->fcs
656 = ((xfpregs->fcs & 0xffff)
657 | ((*(int *) &registers[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
658 << 16));
659
660 /* Fill in the XMM registers. */
661 for (reg = 0; reg < 8; reg++)
662 if (! valid || valid[reg])
663 memcpy (&xfpregs->xmm_space[reg],
664 &registers[REGISTER_BYTE (XMM0_REGNUM + reg)],
665 REGISTER_RAW_SIZE (XMM0_REGNUM + reg));
666}
667
04cd15b6
MK
668/* Fetch all registers covered by the PTRACE_SETXFPREGS request from
669 process/thread TID and store their values in GDB's register array.
670 Return non-zero if successful, zero otherwise. */
5c44784c 671
5c44784c 672static int
ed9a39eb 673fetch_xfpregs (int tid)
5c44784c 674{
5c44784c 675 struct user_xfpregs_struct xfpregs;
04cd15b6 676 int ret;
5c44784c
JM
677
678 if (! have_ptrace_getxfpregs)
679 return 0;
680
ed9a39eb 681 ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
5c44784c 682 if (ret == -1)
d4f3574e 683 {
5c44784c
JM
684 if (errno == EIO)
685 {
686 have_ptrace_getxfpregs = 0;
687 return 0;
688 }
689
04cd15b6 690 warning ("Couldn't read floating-point and SSE registers.");
5c44784c 691 return 0;
d4f3574e
SS
692 }
693
5c44784c
JM
694 supply_xfpregset (&xfpregs);
695 return 1;
696}
d4f3574e 697
04cd15b6
MK
698/* Store all valid registers in GDB's register array covered by the
699 PTRACE_SETXFPREGS request into the process/thread specified by TID.
700 Return non-zero if successful, zero otherwise. */
5c44784c 701
5c44784c 702static int
ed9a39eb 703store_xfpregs (int tid)
5c44784c 704{
5c44784c 705 struct user_xfpregs_struct xfpregs;
04cd15b6 706 int ret;
5c44784c
JM
707
708 if (! have_ptrace_getxfpregs)
709 return 0;
710
ed9a39eb 711 ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
5c44784c 712 if (ret == -1)
d4f3574e 713 {
5c44784c
JM
714 if (errno == EIO)
715 {
716 have_ptrace_getxfpregs = 0;
717 return 0;
718 }
719
04cd15b6 720 warning ("Couldn't read floating-point and SSE registers.");
5c44784c
JM
721 return 0;
722 }
723
04cd15b6 724 convert_to_xfpregset (&xfpregs, register_valid);
5c44784c 725
ed9a39eb 726 if (ptrace (PTRACE_SETXFPREGS, tid, 0, &xfpregs) < 0)
5c44784c
JM
727 {
728 warning ("Couldn't write floating-point and SSE registers.");
729 return 0;
d4f3574e 730 }
5c44784c
JM
731
732 return 1;
733}
734
04cd15b6 735/* Fill the XMM registers in the register array with dummy values. For
5c44784c
JM
736 cases where we don't have access to the XMM registers. I think
737 this is cleaner than printing a warning. For a cleaner solution,
738 we should gdbarchify the i386 family. */
04cd15b6 739
5c44784c 740static void
04cd15b6 741dummy_sse_values (void)
5c44784c
JM
742{
743 /* C doesn't have a syntax for NaN's, so write it out as an array of
744 longs. */
745 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
746 static long mxcsr = 0x1f80;
747 int reg;
748
749 for (reg = 0; reg < 8; reg++)
750 supply_register (XMM0_REGNUM + reg, (char *) dummy);
751 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
d4f3574e
SS
752}
753
5c44784c
JM
754#else
755
756/* Stub versions of the above routines, for systems that don't have
757 PTRACE_GETXFPREGS. */
ed9a39eb
JM
758static int store_xfpregs (int tid) { return 0; }
759static int fetch_xfpregs (int tid) { return 0; }
04cd15b6 760static void dummy_sse_values (void) {}
5c44784c
JM
761
762#endif
763
764\f
765/* Transferring arbitrary registers between GDB and inferior. */
d4f3574e 766
04cd15b6
MK
767/* Fetch register REGNO from the child process. If REGNO is -1, do
768 this for all registers (including the floating point and SSE
769 registers). */
d4f3574e
SS
770
771void
917317f4 772fetch_inferior_registers (int regno)
d4f3574e 773{
ed9a39eb
JM
774 int tid;
775
f60300e7
MK
776 /* Use the old method of peeking around in `struct user' if the
777 GETREGS request isn't available. */
778 if (! have_ptrace_getregs)
779 {
780 old_fetch_inferior_registers (regno);
781 return;
782 }
783
04cd15b6 784 /* Linux LWP ID's are process ID's. */
ed9a39eb 785 if ((tid = TIDGET (inferior_pid)) == 0)
04cd15b6 786 tid = inferior_pid; /* Not a threaded program. */
ed9a39eb 787
04cd15b6
MK
788 /* Use the PTRACE_GETXFPREGS request whenever possible, since it
789 transfers more registers in one system call, and we'll cache the
790 results. But remember that fetch_xfpregs can fail, and return
791 zero. */
5c44784c
JM
792 if (regno == -1)
793 {
ed9a39eb 794 fetch_regs (tid);
f60300e7
MK
795
796 /* The call above might reset `have_ptrace_getregs'. */
797 if (! have_ptrace_getregs)
798 {
799 old_fetch_inferior_registers (-1);
800 return;
801 }
802
ed9a39eb 803 if (fetch_xfpregs (tid))
5c44784c 804 return;
ed9a39eb 805 fetch_fpregs (tid);
5c44784c
JM
806 return;
807 }
d4f3574e 808
5c44784c
JM
809 if (GETREGS_SUPPLIES (regno))
810 {
ed9a39eb 811 fetch_regs (tid);
5c44784c
JM
812 return;
813 }
814
815 if (GETXFPREGS_SUPPLIES (regno))
816 {
ed9a39eb 817 if (fetch_xfpregs (tid))
5c44784c
JM
818 return;
819
820 /* Either our processor or our kernel doesn't support the SSE
821 registers, so read the FP registers in the traditional way,
822 and fill the SSE registers with dummy values. It would be
823 more graceful to handle differences in the register set using
824 gdbarch. Until then, this will at least make things work
825 plausibly. */
ed9a39eb 826 fetch_fpregs (tid);
5c44784c
JM
827 dummy_sse_values ();
828 return;
829 }
830
831 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
832 "got request for bad register number %d", regno);
d4f3574e
SS
833}
834
04cd15b6
MK
835/* Store register REGNO back into the child process. If REGNO is -1,
836 do this for all registers (including the floating point and SSE
837 registers). */
d4f3574e 838void
04cd15b6 839store_inferior_registers (int regno)
d4f3574e 840{
ed9a39eb
JM
841 int tid;
842
f60300e7
MK
843 /* Use the old method of poking around in `struct user' if the
844 SETREGS request isn't available. */
845 if (! have_ptrace_getregs)
846 {
847 old_store_inferior_registers (regno);
848 return;
849 }
850
04cd15b6 851 /* Linux LWP ID's are process ID's. */
ed9a39eb 852 if ((tid = TIDGET (inferior_pid)) == 0)
04cd15b6 853 tid = inferior_pid; /* Not a threaded program. */
ed9a39eb 854
04cd15b6
MK
855 /* Use the PTRACE_SETXFPREGS requests whenever possibl, since it
856 transfers more registers in one system call. But remember that
ed9a39eb 857 store_xfpregs can fail, and return zero. */
5c44784c
JM
858 if (regno == -1)
859 {
ed9a39eb
JM
860 store_regs (tid);
861 if (store_xfpregs (tid))
5c44784c 862 return;
ed9a39eb 863 store_fpregs (tid);
5c44784c
JM
864 return;
865 }
d4f3574e 866
5c44784c
JM
867 if (GETREGS_SUPPLIES (regno))
868 {
ed9a39eb 869 store_regs (tid);
5c44784c
JM
870 return;
871 }
872
873 if (GETXFPREGS_SUPPLIES (regno))
874 {
ed9a39eb 875 if (store_xfpregs (tid))
5c44784c
JM
876 return;
877
878 /* Either our processor or our kernel doesn't support the SSE
04cd15b6
MK
879 registers, so just write the FP registers in the traditional
880 way. */
ed9a39eb 881 store_fpregs (tid);
5c44784c
JM
882 return;
883 }
884
04cd15b6 885 internal_error ("Got request to store bad register number %d.", regno);
d4f3574e
SS
886}
887
de57eccd
JM
888\f
889/* Interpreting register set info found in core files. */
890
891/* Provide registers to GDB from a core file.
892
893 (We can't use the generic version of this function in
894 core-regset.c, because Linux has *three* different kinds of
895 register set notes. core-regset.c would have to call
896 supply_xfpregset, which most platforms don't have.)
897
898 CORE_REG_SECT points to an array of bytes, which are the contents
899 of a `note' from a core file which BFD thinks might contain
900 register contents. CORE_REG_SIZE is its size.
901
902 WHICH says which register set corelow suspects this is:
04cd15b6
MK
903 0 --- the general-purpose register set, in elf_gregset_t format
904 2 --- the floating-point register set, in elf_fpregset_t format
905 3 --- the extended floating-point register set, in struct
906 user_xfpregs_struct format
907
908 REG_ADDR isn't used on Linux. */
de57eccd 909
de57eccd 910static void
04cd15b6
MK
911fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
912 int which, CORE_ADDR reg_addr)
de57eccd 913{
04cd15b6
MK
914 elf_gregset_t gregset;
915 elf_fpregset_t fpregset;
de57eccd
JM
916
917 switch (which)
918 {
919 case 0:
920 if (core_reg_size != sizeof (gregset))
04cd15b6 921 warning ("Wrong size gregset in core file.");
de57eccd
JM
922 else
923 {
924 memcpy (&gregset, core_reg_sect, sizeof (gregset));
925 supply_gregset (&gregset);
926 }
927 break;
928
929 case 2:
930 if (core_reg_size != sizeof (fpregset))
04cd15b6 931 warning ("Wrong size fpregset in core file.");
de57eccd
JM
932 else
933 {
934 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
935 supply_fpregset (&fpregset);
936 }
937 break;
938
939#ifdef HAVE_PTRACE_GETXFPREGS
940 {
941 struct user_xfpregs_struct xfpregset;
04cd15b6 942
de57eccd 943 case 3:
04cd15b6
MK
944 if (core_reg_size != sizeof (xfpregset))
945 warning ("Wrong size user_xfpregs_struct in core file.");
de57eccd
JM
946 else
947 {
948 memcpy (&xfpregset, core_reg_sect, sizeof (xfpregset));
949 supply_xfpregset (&xfpregset);
950 }
951 break;
952 }
953#endif
954
955 default:
956 /* We've covered all the kinds of registers we know about here,
957 so this must be something we wouldn't know what to do with
958 anyway. Just ignore it. */
959 break;
960 }
961}
962
a6abb2c0
MK
963\f
964/* The instruction for a Linux system call is:
965 int $0x80
966 or 0xcd 0x80. */
967
968static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
969
970#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
971
972/* The system call number is stored in the %eax register. */
973#define LINUX_SYSCALL_REGNUM 0 /* %eax */
974
975/* We are specifically interested in the sigreturn and rt_sigreturn
976 system calls. */
977
978#ifndef SYS_sigreturn
979#define SYS_sigreturn 0x77
980#endif
981#ifndef SYS_rt_sigreturn
982#define SYS_rt_sigreturn 0xad
983#endif
984
985/* Offset to saved processor flags, from <asm/sigcontext.h>. */
986#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
987
988/* Resume execution of the inferior process.
989 If STEP is nonzero, single-step it.
990 If SIGNAL is nonzero, give it that signal. */
991
992void
993child_resume (int pid, int step, enum target_signal signal)
994{
995 int request = PTRACE_CONT;
996
997 if (pid == -1)
998 /* Resume all threads. */
999 /* I think this only gets used in the non-threaded case, where "resume
1000 all threads" and "resume inferior_pid" are the same. */
1001 pid = inferior_pid;
1002
1003 if (step)
1004 {
1005 CORE_ADDR pc = read_pc_pid (pid);
1006 unsigned char buf[LINUX_SYSCALL_LEN];
1007
1008 request = PTRACE_SINGLESTEP;
1009
1010 /* Returning from a signal trampoline is done by calling a
1011 special system call (sigreturn or rt_sigreturn, see
1012 i386-linux-tdep.c for more information). This system call
1013 restores the registers that were saved when the signal was
1014 raised, including %eflags. That means that single-stepping
1015 won't work. Instead, we'll have to modify the signal context
1016 that's about to be restored, and set the trace flag there. */
1017
1018 /* First check if PC is at a system call. */
1019 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
1020 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
1021 {
1022 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM, pid);
1023
1024 /* Then check the system call number. */
1025 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
1026 {
1027 CORE_ADDR sp = read_register (SP_REGNUM);
1028 CORE_ADDR addr = sp;
1029 unsigned long int eflags;
1030
1031 if (syscall == SYS_rt_sigreturn)
1032 addr = read_memory_integer (sp + 8, 4) + 20;
1033
1034 /* Set the trace flag in the context that's about to be
1035 restored. */
1036 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
1037 read_memory (addr, (char *) &eflags, 4);
1038 eflags |= 0x0100;
1039 write_memory (addr, (char *) &eflags, 4);
1040 }
1041 }
1042 }
1043
1044 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
1045 perror_with_name ("ptrace");
1046}
1047
5c44784c
JM
1048\f
1049/* Calling functions in shared libraries. */
04cd15b6
MK
1050/* FIXME: kettenis/2000-03-05: Doesn't this belong in a
1051 target-dependent file? The function
1052 `i386_linux_skip_solib_resolver' is mentioned in
1053 `config/i386/tm-linux.h'. */
5c44784c 1054
d4f3574e
SS
1055/* Find the minimal symbol named NAME, and return both the minsym
1056 struct and its objfile. This probably ought to be in minsym.c, but
1057 everything there is trying to deal with things like C++ and
1058 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
1059 be considered too special-purpose for general consumption. */
1060
1061static struct minimal_symbol *
1062find_minsym_and_objfile (char *name, struct objfile **objfile_p)
1063{
1064 struct objfile *objfile;
1065
1066 ALL_OBJFILES (objfile)
1067 {
1068 struct minimal_symbol *msym;
1069
1070 ALL_OBJFILE_MSYMBOLS (objfile, msym)
1071 {
1072 if (SYMBOL_NAME (msym)
1073 && STREQ (SYMBOL_NAME (msym), name))
1074 {
1075 *objfile_p = objfile;
1076 return msym;
1077 }
1078 }
1079 }
1080
1081 return 0;
1082}
1083
1084
1085static CORE_ADDR
1086skip_hurd_resolver (CORE_ADDR pc)
1087{
1088 /* The HURD dynamic linker is part of the GNU C library, so many
1089 GNU/Linux distributions use it. (All ELF versions, as far as I
1090 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
1091 which calls "fixup" to patch the PLT, and then passes control to
1092 the function.
1093
1094 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
1095 the same objfile. If we are at the entry point of `fixup', then
1096 we set a breakpoint at the return address (at the top of the
1097 stack), and continue.
1098
1099 It's kind of gross to do all these checks every time we're
1100 called, since they don't change once the executable has gotten
1101 started. But this is only a temporary hack --- upcoming versions
1102 of Linux will provide a portable, efficient interface for
1103 debugging programs that use shared libraries. */
1104
1105 struct objfile *objfile;
1106 struct minimal_symbol *resolver
1107 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
1108
1109 if (resolver)
1110 {
1111 struct minimal_symbol *fixup
1112 = lookup_minimal_symbol ("fixup", 0, objfile);
1113
1114 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
1115 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
1116 }
1117
1118 return 0;
1119}
1120
d4f3574e
SS
1121/* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
1122 This function:
1123 1) decides whether a PLT has sent us into the linker to resolve
1124 a function reference, and
1125 2) if so, tells us where to set a temporary breakpoint that will
1126 trigger when the dynamic linker is done. */
1127
1128CORE_ADDR
1129i386_linux_skip_solib_resolver (CORE_ADDR pc)
1130{
1131 CORE_ADDR result;
1132
1133 /* Plug in functions for other kinds of resolvers here. */
1134 result = skip_hurd_resolver (pc);
1135 if (result)
1136 return result;
1137
1138 return 0;
1139}
de57eccd 1140
de57eccd 1141\f
04cd15b6
MK
1142/* Register that we are able to handle Linux ELF core file formats. */
1143
1144static struct core_fns linux_elf_core_fns =
1145{
1146 bfd_target_elf_flavour, /* core_flavour */
1147 default_check_format, /* check_format */
1148 default_core_sniffer, /* core_sniffer */
1149 fetch_core_registers, /* core_read_registers */
1150 NULL /* next */
1151};
de57eccd
JM
1152
1153void
1154_initialize_i386_linux_nat ()
1155{
04cd15b6 1156 add_core_fns (&linux_elf_core_fns);
de57eccd 1157}
This page took 0.101999 seconds and 4 git commands to generate.