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