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