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