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