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