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