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