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