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