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