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