2004-07-21 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"
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
MK
217 errno = 0;
218 regcache_collect (regno, &val);
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)
8a406745 258 regcache_collect (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)
76fb44f4 262 regcache_collect (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
d4f3574e
SS
263}
264
f60300e7
MK
265#ifdef HAVE_PTRACE_GETREGS
266
04cd15b6
MK
267/* Fetch all general-purpose registers from process/thread TID and
268 store their values in GDB's register array. */
d4f3574e 269
5c44784c 270static void
ed9a39eb 271fetch_regs (int tid)
5c44784c 272{
04cd15b6 273 elf_gregset_t regs;
5c44784c 274
6ce2ac0b 275 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
5c44784c 276 {
f60300e7
MK
277 if (errno == EIO)
278 {
279 /* The kernel we're running on doesn't support the GETREGS
280 request. Reset `have_ptrace_getregs'. */
281 have_ptrace_getregs = 0;
282 return;
283 }
284
6ce2ac0b 285 perror_with_name ("Couldn't get registers");
5c44784c
JM
286 }
287
04cd15b6 288 supply_gregset (&regs);
5c44784c
JM
289}
290
04cd15b6
MK
291/* Store all valid general-purpose registers in GDB's register array
292 into the process/thread specified by TID. */
5c44784c 293
5c44784c 294static void
6ce2ac0b 295store_regs (int tid, int regno)
5c44784c 296{
04cd15b6 297 elf_gregset_t regs;
5c44784c 298
6ce2ac0b
MK
299 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
300 perror_with_name ("Couldn't get registers");
5c44784c 301
6ce2ac0b
MK
302 fill_gregset (&regs, regno);
303
304 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
305 perror_with_name ("Couldn't write registers");
5c44784c
JM
306}
307
f60300e7
MK
308#else
309
310static void fetch_regs (int tid) {}
6ce2ac0b 311static void store_regs (int tid, int regno) {}
f60300e7
MK
312
313#endif
5c44784c 314\f
5c44784c 315
6ce2ac0b 316/* Transfering floating-point registers between GDB, inferiors and cores. */
d4f3574e 317
04cd15b6 318/* Fill GDB's register array with the floating-point register values in
917317f4 319 *FPREGSETP. */
04cd15b6 320
d4f3574e 321void
04cd15b6 322supply_fpregset (elf_fpregset_t *fpregsetp)
d4f3574e 323{
41d041d6 324 i387_supply_fsave (current_regcache, -1, fpregsetp);
756ed206 325 dummy_sse_values ();
917317f4 326}
d4f3574e 327
04cd15b6
MK
328/* Fill register REGNO (if it is a floating-point register) in
329 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
330 do this for all registers. */
917317f4
JM
331
332void
04cd15b6 333fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
917317f4 334{
6ce2ac0b 335 i387_fill_fsave ((char *) fpregsetp, regno);
d4f3574e
SS
336}
337
f60300e7
MK
338#ifdef HAVE_PTRACE_GETREGS
339
04cd15b6
MK
340/* Fetch all floating-point registers from process/thread TID and store
341 thier values in GDB's register array. */
917317f4 342
d4f3574e 343static void
ed9a39eb 344fetch_fpregs (int tid)
d4f3574e 345{
04cd15b6 346 elf_fpregset_t fpregs;
d4f3574e 347
6ce2ac0b
MK
348 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
349 perror_with_name ("Couldn't get floating point status");
d4f3574e 350
04cd15b6 351 supply_fpregset (&fpregs);
d4f3574e
SS
352}
353
04cd15b6
MK
354/* Store all valid floating-point registers in GDB's register array
355 into the process/thread specified by TID. */
d4f3574e 356
d4f3574e 357static void
6ce2ac0b 358store_fpregs (int tid, int regno)
d4f3574e 359{
04cd15b6 360 elf_fpregset_t fpregs;
d4f3574e 361
6ce2ac0b
MK
362 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
363 perror_with_name ("Couldn't get floating point status");
d4f3574e 364
6ce2ac0b 365 fill_fpregset (&fpregs, regno);
d4f3574e 366
6ce2ac0b
MK
367 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
368 perror_with_name ("Couldn't write floating point status");
d4f3574e
SS
369}
370
f60300e7
MK
371#else
372
373static void fetch_fpregs (int tid) {}
6ce2ac0b 374static void store_fpregs (int tid, int regno) {}
f60300e7
MK
375
376#endif
5c44784c 377\f
d4f3574e 378
6ce2ac0b 379/* Transfering floating-point and SSE registers to and from GDB. */
11cf8741 380
6ce2ac0b 381#ifdef HAVE_PTRACE_GETFPXREGS
04cd15b6
MK
382
383/* Fill GDB's register array with the floating-point and SSE register
6ce2ac0b 384 values in *FPXREGSETP. */
04cd15b6 385
975aec09 386void
6ce2ac0b 387supply_fpxregset (elf_fpxregset_t *fpxregsetp)
d4f3574e 388{
41d041d6 389 i387_supply_fxsave (current_regcache, -1, fpxregsetp);
d4f3574e
SS
390}
391
6ce2ac0b
MK
392/* Fill register REGNO (if it is a floating-point or SSE register) in
393 *FPXREGSETP with the value in GDB's register array. If REGNO is
394 -1, do this for all registers. */
d4f3574e 395
975aec09 396void
6ce2ac0b 397fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
d4f3574e 398{
6ce2ac0b 399 i387_fill_fxsave ((char *) fpxregsetp, regno);
5c44784c
JM
400}
401
6ce2ac0b 402/* Fetch all registers covered by the PTRACE_GETFPXREGS request from
04cd15b6
MK
403 process/thread TID and store their values in GDB's register array.
404 Return non-zero if successful, zero otherwise. */
5c44784c 405
5c44784c 406static int
6ce2ac0b 407fetch_fpxregs (int tid)
5c44784c 408{
6ce2ac0b 409 elf_fpxregset_t fpxregs;
5c44784c 410
6ce2ac0b 411 if (! have_ptrace_getfpxregs)
5c44784c
JM
412 return 0;
413
6ce2ac0b 414 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
d4f3574e 415 {
5c44784c
JM
416 if (errno == EIO)
417 {
6ce2ac0b 418 have_ptrace_getfpxregs = 0;
5c44784c
JM
419 return 0;
420 }
421
6ce2ac0b 422 perror_with_name ("Couldn't read floating-point and SSE registers");
d4f3574e
SS
423 }
424
6ce2ac0b 425 supply_fpxregset (&fpxregs);
5c44784c
JM
426 return 1;
427}
d4f3574e 428
04cd15b6 429/* Store all valid registers in GDB's register array covered by the
6ce2ac0b 430 PTRACE_SETFPXREGS request into the process/thread specified by TID.
04cd15b6 431 Return non-zero if successful, zero otherwise. */
5c44784c 432
5c44784c 433static int
6ce2ac0b 434store_fpxregs (int tid, int regno)
5c44784c 435{
6ce2ac0b 436 elf_fpxregset_t fpxregs;
5c44784c 437
6ce2ac0b 438 if (! have_ptrace_getfpxregs)
5c44784c 439 return 0;
6ce2ac0b
MK
440
441 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
2866d305
MK
442 {
443 if (errno == EIO)
444 {
445 have_ptrace_getfpxregs = 0;
446 return 0;
447 }
448
449 perror_with_name ("Couldn't read floating-point and SSE registers");
450 }
5c44784c 451
6ce2ac0b 452 fill_fpxregset (&fpxregs, regno);
5c44784c 453
6ce2ac0b
MK
454 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
455 perror_with_name ("Couldn't write floating-point and SSE registers");
5c44784c
JM
456
457 return 1;
458}
459
04cd15b6 460/* Fill the XMM registers in the register array with dummy values. For
5c44784c
JM
461 cases where we don't have access to the XMM registers. I think
462 this is cleaner than printing a warning. For a cleaner solution,
463 we should gdbarchify the i386 family. */
04cd15b6 464
5c44784c 465static void
04cd15b6 466dummy_sse_values (void)
5c44784c 467{
7010ca0a 468 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
5c44784c
JM
469 /* C doesn't have a syntax for NaN's, so write it out as an array of
470 longs. */
471 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
472 static long mxcsr = 0x1f80;
473 int reg;
474
7010ca0a 475 for (reg = 0; reg < tdep->num_xmm_regs; reg++)
23a6d369 476 regcache_raw_supply (current_regcache, XMM0_REGNUM + reg, (char *) dummy);
7010ca0a 477 if (tdep->num_xmm_regs > 0)
23a6d369 478 regcache_raw_supply (current_regcache, MXCSR_REGNUM, (char *) &mxcsr);
d4f3574e
SS
479}
480
5c44784c
JM
481#else
482
f0373401
MK
483static int fetch_fpxregs (int tid) { return 0; }
484static int store_fpxregs (int tid, int regno) { return 0; }
04cd15b6 485static void dummy_sse_values (void) {}
5c44784c 486
6ce2ac0b 487#endif /* HAVE_PTRACE_GETFPXREGS */
5c44784c 488\f
6ce2ac0b 489
5c44784c 490/* Transferring arbitrary registers between GDB and inferior. */
d4f3574e 491
d5d65353
PS
492/* Check if register REGNO in the child process is accessible.
493 If we are accessing registers directly via the U area, only the
494 general-purpose registers are available.
495 All registers should be accessible if we have GETREGS support. */
496
497int
498cannot_fetch_register (int regno)
499{
ce556f85
MK
500 gdb_assert (regno >= 0 && regno < NUM_REGS);
501 return (!have_ptrace_getregs && regmap[regno] == -1);
d5d65353 502}
ce556f85 503
d5d65353
PS
504int
505cannot_store_register (int regno)
506{
ce556f85
MK
507 gdb_assert (regno >= 0 && regno < NUM_REGS);
508 return (!have_ptrace_getregs && regmap[regno] == -1);
d5d65353
PS
509}
510
04cd15b6
MK
511/* Fetch register REGNO from the child process. If REGNO is -1, do
512 this for all registers (including the floating point and SSE
513 registers). */
d4f3574e
SS
514
515void
917317f4 516fetch_inferior_registers (int regno)
d4f3574e 517{
ed9a39eb
JM
518 int tid;
519
f60300e7
MK
520 /* Use the old method of peeking around in `struct user' if the
521 GETREGS request isn't available. */
ce556f85 522 if (!have_ptrace_getregs)
f60300e7 523 {
ce556f85
MK
524 int i;
525
526 for (i = 0; i < NUM_REGS; i++)
527 if (regno == -1 || regno == i)
528 fetch_register (i);
529
f60300e7
MK
530 return;
531 }
532
a4b6fc86 533 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
534 tid = TIDGET (inferior_ptid);
535 if (tid == 0)
536 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 537
6ce2ac0b 538 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
04cd15b6 539 transfers more registers in one system call, and we'll cache the
6ce2ac0b 540 results. But remember that fetch_fpxregs can fail, and return
04cd15b6 541 zero. */
5c44784c
JM
542 if (regno == -1)
543 {
ed9a39eb 544 fetch_regs (tid);
f60300e7
MK
545
546 /* The call above might reset `have_ptrace_getregs'. */
ce556f85 547 if (!have_ptrace_getregs)
f60300e7 548 {
ce556f85 549 fetch_inferior_registers (regno);
f60300e7
MK
550 return;
551 }
552
6ce2ac0b 553 if (fetch_fpxregs (tid))
5c44784c 554 return;
ed9a39eb 555 fetch_fpregs (tid);
5c44784c
JM
556 return;
557 }
d4f3574e 558
5c44784c
JM
559 if (GETREGS_SUPPLIES (regno))
560 {
ed9a39eb 561 fetch_regs (tid);
5c44784c
JM
562 return;
563 }
564
6ce2ac0b 565 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 566 {
6ce2ac0b 567 if (fetch_fpxregs (tid))
5c44784c
JM
568 return;
569
570 /* Either our processor or our kernel doesn't support the SSE
571 registers, so read the FP registers in the traditional way,
572 and fill the SSE registers with dummy values. It would be
573 more graceful to handle differences in the register set using
574 gdbarch. Until then, this will at least make things work
575 plausibly. */
ed9a39eb 576 fetch_fpregs (tid);
5c44784c
JM
577 return;
578 }
579
8e65ff28
AC
580 internal_error (__FILE__, __LINE__,
581 "Got request for bad register number %d.", regno);
d4f3574e
SS
582}
583
04cd15b6
MK
584/* Store register REGNO back into the child process. If REGNO is -1,
585 do this for all registers (including the floating point and SSE
586 registers). */
d4f3574e 587void
04cd15b6 588store_inferior_registers (int regno)
d4f3574e 589{
ed9a39eb
JM
590 int tid;
591
f60300e7
MK
592 /* Use the old method of poking around in `struct user' if the
593 SETREGS request isn't available. */
ce556f85 594 if (!have_ptrace_getregs)
f60300e7 595 {
ce556f85
MK
596 int i;
597
598 for (i = 0; i < NUM_REGS; i++)
599 if (regno == -1 || regno == i)
600 store_register (i);
601
f60300e7
MK
602 return;
603 }
604
a4b6fc86 605 /* GNU/Linux LWP ID's are process ID's. */
e64a344c
MK
606 tid = TIDGET (inferior_ptid);
607 if (tid == 0)
608 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
ed9a39eb 609
6ce2ac0b 610 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
04cd15b6 611 transfers more registers in one system call. But remember that
6ce2ac0b 612 store_fpxregs can fail, and return zero. */
5c44784c
JM
613 if (regno == -1)
614 {
6ce2ac0b
MK
615 store_regs (tid, regno);
616 if (store_fpxregs (tid, regno))
5c44784c 617 return;
6ce2ac0b 618 store_fpregs (tid, regno);
5c44784c
JM
619 return;
620 }
d4f3574e 621
5c44784c
JM
622 if (GETREGS_SUPPLIES (regno))
623 {
6ce2ac0b 624 store_regs (tid, regno);
5c44784c
JM
625 return;
626 }
627
6ce2ac0b 628 if (GETFPXREGS_SUPPLIES (regno))
5c44784c 629 {
6ce2ac0b 630 if (store_fpxregs (tid, regno))
5c44784c
JM
631 return;
632
633 /* Either our processor or our kernel doesn't support the SSE
04cd15b6
MK
634 registers, so just write the FP registers in the traditional
635 way. */
6ce2ac0b 636 store_fpregs (tid, regno);
5c44784c
JM
637 return;
638 }
639
8e65ff28
AC
640 internal_error (__FILE__, __LINE__,
641 "Got request to store bad register number %d.", regno);
d4f3574e 642}
de57eccd 643\f
6ce2ac0b 644
4ffc8466
MK
645/* Support for debug registers. */
646
7bf0983e 647static unsigned long
84346e11
MK
648i386_linux_dr_get (int regnum)
649{
650 int tid;
7bf0983e 651 unsigned long value;
84346e11
MK
652
653 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
654 multi-threaded processes here. For now, pretend there is just
655 one thread. */
39f77062 656 tid = PIDGET (inferior_ptid);
84346e11 657
b9511b9a
MK
658 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
659 ptrace call fails breaks debugging remote targets. The correct
660 way to fix this is to add the hardware breakpoint and watchpoint
661 stuff to the target vectore. For now, just return zero if the
662 ptrace call fails. */
84346e11 663 errno = 0;
ce556f85 664 value = ptrace (PTRACE_PEEKUSER, tid,
84346e11
MK
665 offsetof (struct user, u_debugreg[regnum]), 0);
666 if (errno != 0)
b9511b9a 667#if 0
84346e11 668 perror_with_name ("Couldn't read debug register");
b9511b9a
MK
669#else
670 return 0;
671#endif
84346e11
MK
672
673 return value;
674}
675
676static void
7bf0983e 677i386_linux_dr_set (int regnum, unsigned long value)
84346e11
MK
678{
679 int tid;
680
681 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
682 multi-threaded processes here. For now, pretend there is just
683 one thread. */
39f77062 684 tid = PIDGET (inferior_ptid);
84346e11
MK
685
686 errno = 0;
ce556f85 687 ptrace (PTRACE_POKEUSER, tid,
84346e11
MK
688 offsetof (struct user, u_debugreg[regnum]), value);
689 if (errno != 0)
690 perror_with_name ("Couldn't write debug register");
691}
692
693void
7bf0983e 694i386_linux_dr_set_control (unsigned long control)
84346e11
MK
695{
696 i386_linux_dr_set (DR_CONTROL, control);
697}
698
699void
700i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
701{
702 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
703
704 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
705}
706
707void
708i386_linux_dr_reset_addr (int regnum)
709{
710 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
711
712 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
713}
714
7bf0983e 715unsigned long
84346e11
MK
716i386_linux_dr_get_status (void)
717{
718 return i386_linux_dr_get (DR_STATUS);
719}
720\f
721
5bca7895
MK
722/* Called by libthread_db. Returns a pointer to the thread local
723 storage (or its descriptor). */
724
725ps_err_e
726ps_get_thread_area (const struct ps_prochandle *ph,
727 lwpid_t lwpid, int idx, void **base)
728{
729 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
730 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
731 4 byte integers in size: `entry_number', `base_addr', `limit',
732 and a bunch of status bits.
733
734 The values returned by this ptrace call should be part of the
735 regcache buffer, and ps_get_thread_area should channel its
736 request through the regcache. That way remote targets could
737 provide the value using the remote protocol and not this direct
738 call.
739
740 Is this function needed? I'm guessing that the `base' is the
741 address of a a descriptor that libthread_db uses to find the
b2fa5097 742 thread local address base that GDB needs. Perhaps that
5bca7895
MK
743 descriptor is defined by the ABI. Anyway, given that
744 libthread_db calls this function without prompting (gdb
745 requesting tls base) I guess it needs info in there anyway. */
746 unsigned int desc[4];
747 gdb_assert (sizeof (int) == 4);
748
749#ifndef PTRACE_GET_THREAD_AREA
750#define PTRACE_GET_THREAD_AREA 25
751#endif
752
753 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
754 (void *) idx, (unsigned long) &desc) < 0)
755 return PS_ERR;
756
757 *(int *)base = desc[1];
758 return PS_OK;
759}
760\f
761
a4b6fc86 762/* The instruction for a GNU/Linux system call is:
a6abb2c0
MK
763 int $0x80
764 or 0xcd 0x80. */
765
766static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
767
768#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
769
770/* The system call number is stored in the %eax register. */
771#define LINUX_SYSCALL_REGNUM 0 /* %eax */
772
773/* We are specifically interested in the sigreturn and rt_sigreturn
774 system calls. */
775
776#ifndef SYS_sigreturn
777#define SYS_sigreturn 0x77
778#endif
779#ifndef SYS_rt_sigreturn
780#define SYS_rt_sigreturn 0xad
781#endif
782
783/* Offset to saved processor flags, from <asm/sigcontext.h>. */
784#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
785
786/* Resume execution of the inferior process.
787 If STEP is nonzero, single-step it.
788 If SIGNAL is nonzero, give it that signal. */
789
790void
39f77062 791child_resume (ptid_t ptid, int step, enum target_signal signal)
a6abb2c0 792{
39f77062
KB
793 int pid = PIDGET (ptid);
794
a6abb2c0
MK
795 int request = PTRACE_CONT;
796
797 if (pid == -1)
798 /* Resume all threads. */
799 /* I think this only gets used in the non-threaded case, where "resume
39f77062
KB
800 all threads" and "resume inferior_ptid" are the same. */
801 pid = PIDGET (inferior_ptid);
a6abb2c0
MK
802
803 if (step)
804 {
39f77062 805 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
a6abb2c0
MK
806 unsigned char buf[LINUX_SYSCALL_LEN];
807
808 request = PTRACE_SINGLESTEP;
809
810 /* Returning from a signal trampoline is done by calling a
811 special system call (sigreturn or rt_sigreturn, see
812 i386-linux-tdep.c for more information). This system call
813 restores the registers that were saved when the signal was
814 raised, including %eflags. That means that single-stepping
815 won't work. Instead, we'll have to modify the signal context
816 that's about to be restored, and set the trace flag there. */
817
818 /* First check if PC is at a system call. */
1f602b35 819 if (deprecated_read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
a6abb2c0
MK
820 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
821 {
39f77062
KB
822 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
823 pid_to_ptid (pid));
a6abb2c0
MK
824
825 /* Then check the system call number. */
826 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
827 {
c7f16359 828 CORE_ADDR sp = read_register (I386_ESP_REGNUM);
a6abb2c0
MK
829 CORE_ADDR addr = sp;
830 unsigned long int eflags;
7bf0983e 831
a6abb2c0
MK
832 if (syscall == SYS_rt_sigreturn)
833 addr = read_memory_integer (sp + 8, 4) + 20;
834
835 /* Set the trace flag in the context that's about to be
836 restored. */
837 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
838 read_memory (addr, (char *) &eflags, 4);
839 eflags |= 0x0100;
840 write_memory (addr, (char *) &eflags, 4);
841 }
842 }
843 }
844
845 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
846 perror_with_name ("ptrace");
847}
4de4c07c
DJ
848
849void
850child_post_startup_inferior (ptid_t ptid)
851{
852 i386_cleanup_dregs ();
853 linux_child_post_startup_inferior (ptid);
854}
This page took 0.391414 seconds and 4 git commands to generate.