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