Commit | Line | Data |
---|---|---|
a4194092 | 1 | /* Native-dependent code for GNU/Linux i386. |
a4b6fc86 | 2 | |
ecd75fc8 | 3 | Copyright (C) 1999-2014 Free Software Foundation, Inc. |
d4f3574e | 4 | |
04cd15b6 | 5 | This file is part of GDB. |
d4f3574e | 6 | |
04cd15b6 MK |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
04cd15b6 | 10 | (at your option) any later version. |
d4f3574e | 11 | |
04cd15b6 MK |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
d4f3574e | 16 | |
04cd15b6 | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
d4f3574e SS |
19 | |
20 | #include "defs.h" | |
21 | #include "inferior.h" | |
22 | #include "gdbcore.h" | |
4e052eda | 23 | #include "regcache.h" |
c131fcee | 24 | #include "elf/common.h" |
d4f3574e | 25 | #include <sys/ptrace.h> |
72fde3df | 26 | #include <sys/uio.h> |
c60c0f5f | 27 | #include "gregset.h" |
3116063b | 28 | #include "gdb_proc_service.h" |
c60c0f5f | 29 | |
3116063b | 30 | #include "i386-linux-nat.h" |
e750d25e | 31 | #include "i387-tdep.h" |
c3833324 | 32 | #include "i386-tdep.h" |
5179e78f | 33 | #include "i386-linux-tdep.h" |
df7e5265 | 34 | #include "x86-xstate.h" |
c131fcee | 35 | |
040baaf6 | 36 | #include "x86-linux-nat.h" |
d4f3574e | 37 | |
a4b6fc86 AC |
38 | /* The register sets used in GNU/Linux ELF core-dumps are identical to |
39 | the register sets in `struct user' that is used for a.out | |
40 | core-dumps, and is also used by `ptrace'. The corresponding types | |
41 | are `elf_gregset_t' for the general-purpose registers (with | |
04cd15b6 MK |
42 | `elf_greg_t' the type of a single GP register) and `elf_fpregset_t' |
43 | for the floating-point registers. | |
44 | ||
45 | Those types used to be available under the names `gregset_t' and | |
46 | `fpregset_t' too, and this file used those names in the past. But | |
47 | those names are now used for the register sets used in the | |
48 | `mcontext_t' type, and have a different size and layout. */ | |
49 | ||
5c44784c JM |
50 | /* Which ptrace request retrieves which registers? |
51 | These apply to the corresponding SET requests as well. */ | |
e64a344c | 52 | |
5c44784c | 53 | #define GETREGS_SUPPLIES(regno) \ |
3fb1c838 | 54 | ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM) |
e64a344c | 55 | |
6ce2ac0b | 56 | #define GETFPXREGS_SUPPLIES(regno) \ |
f6792ef4 | 57 | (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS) |
5c44784c | 58 | |
c131fcee | 59 | #define GETXSTATEREGS_SUPPLIES(regno) \ |
01f9f808 | 60 | (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX512_NUM_REGS) |
c131fcee | 61 | |
f60300e7 MK |
62 | /* Does the current host support the GETREGS request? */ |
63 | int have_ptrace_getregs = | |
64 | #ifdef HAVE_PTRACE_GETREGS | |
65 | 1 | |
66 | #else | |
67 | 0 | |
68 | #endif | |
69 | ; | |
70 | ||
6ce2ac0b | 71 | /* Does the current host support the GETFPXREGS request? The header |
5c44784c JM |
72 | file may or may not define it, and even if it is defined, the |
73 | kernel will return EIO if it's running on a pre-SSE processor. | |
74 | ||
75 | My instinct is to attach this to some architecture- or | |
76 | target-specific data structure, but really, a particular GDB | |
77 | process can only run on top of one kernel at a time. So it's okay | |
78 | for this to be a simple variable. */ | |
6ce2ac0b MK |
79 | int have_ptrace_getfpxregs = |
80 | #ifdef HAVE_PTRACE_GETFPXREGS | |
3a13a53b | 81 | -1 |
5c44784c JM |
82 | #else |
83 | 0 | |
84 | #endif | |
85 | ; | |
f60300e7 | 86 | \f |
6ce2ac0b | 87 | |
ce556f85 | 88 | /* Accessing registers through the U area, one at a time. */ |
f60300e7 MK |
89 | |
90 | /* Fetch one register. */ | |
91 | ||
92 | static void | |
56be3814 | 93 | fetch_register (struct regcache *regcache, int regno) |
f60300e7 | 94 | { |
f60300e7 | 95 | int tid; |
ce556f85 | 96 | int val; |
f60300e7 | 97 | |
ce556f85 | 98 | gdb_assert (!have_ptrace_getregs); |
be0d2954 | 99 | if (i386_linux_gregset_reg_offset[regno] == -1) |
f60300e7 | 100 | { |
56be3814 | 101 | regcache_raw_supply (regcache, regno, NULL); |
f60300e7 MK |
102 | return; |
103 | } | |
104 | ||
ce556f85 | 105 | /* GNU/Linux LWP ID's are process ID's. */ |
dfd4cc63 | 106 | tid = ptid_get_lwp (inferior_ptid); |
e64a344c | 107 | if (tid == 0) |
dfd4cc63 | 108 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
f60300e7 | 109 | |
ce556f85 | 110 | errno = 0; |
be0d2954 L |
111 | val = ptrace (PTRACE_PEEKUSER, tid, |
112 | i386_linux_gregset_reg_offset[regno], 0); | |
ce556f85 | 113 | if (errno != 0) |
c9f4d572 | 114 | error (_("Couldn't read register %s (#%d): %s."), |
875f8d0e | 115 | gdbarch_register_name (get_regcache_arch (regcache), regno), |
ce556f85 | 116 | regno, safe_strerror (errno)); |
f60300e7 | 117 | |
56be3814 | 118 | regcache_raw_supply (regcache, regno, &val); |
f60300e7 MK |
119 | } |
120 | ||
1777feb0 | 121 | /* Store one register. */ |
f60300e7 MK |
122 | |
123 | static void | |
56be3814 | 124 | store_register (const struct regcache *regcache, int regno) |
f60300e7 | 125 | { |
f60300e7 | 126 | int tid; |
ce556f85 | 127 | int val; |
f60300e7 | 128 | |
ce556f85 | 129 | gdb_assert (!have_ptrace_getregs); |
be0d2954 | 130 | if (i386_linux_gregset_reg_offset[regno] == -1) |
ce556f85 | 131 | return; |
f60300e7 | 132 | |
ce556f85 | 133 | /* GNU/Linux LWP ID's are process ID's. */ |
dfd4cc63 | 134 | tid = ptid_get_lwp (inferior_ptid); |
e64a344c | 135 | if (tid == 0) |
dfd4cc63 | 136 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
f60300e7 | 137 | |
ce556f85 | 138 | errno = 0; |
56be3814 | 139 | regcache_raw_collect (regcache, regno, &val); |
be0d2954 L |
140 | ptrace (PTRACE_POKEUSER, tid, |
141 | i386_linux_gregset_reg_offset[regno], val); | |
ce556f85 | 142 | if (errno != 0) |
c9f4d572 | 143 | error (_("Couldn't write register %s (#%d): %s."), |
875f8d0e | 144 | gdbarch_register_name (get_regcache_arch (regcache), regno), |
ce556f85 | 145 | regno, safe_strerror (errno)); |
f60300e7 | 146 | } |
5c44784c | 147 | \f |
6ce2ac0b | 148 | |
04cd15b6 MK |
149 | /* Transfering the general-purpose registers between GDB, inferiors |
150 | and core files. */ | |
151 | ||
ad2a4d09 | 152 | /* Fill GDB's register array with the general-purpose register values |
04cd15b6 | 153 | in *GREGSETP. */ |
5c44784c | 154 | |
d4f3574e | 155 | void |
7f7fe91e | 156 | supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp) |
d4f3574e | 157 | { |
be0d2954 | 158 | const gdb_byte *regp = (const gdb_byte *) gregsetp; |
6ce2ac0b | 159 | int i; |
d4f3574e | 160 | |
98df6387 | 161 | for (i = 0; i < I386_NUM_GREGS; i++) |
be0d2954 L |
162 | regcache_raw_supply (regcache, i, |
163 | regp + i386_linux_gregset_reg_offset[i]); | |
3fb1c838 | 164 | |
875f8d0e UW |
165 | if (I386_LINUX_ORIG_EAX_REGNUM |
166 | < gdbarch_num_regs (get_regcache_arch (regcache))) | |
1777feb0 MS |
167 | regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp |
168 | + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]); | |
917317f4 JM |
169 | } |
170 | ||
04cd15b6 MK |
171 | /* Fill register REGNO (if it is a general-purpose register) in |
172 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, | |
173 | do this for all registers. */ | |
6ce2ac0b | 174 | |
917317f4 | 175 | void |
7f7fe91e UW |
176 | fill_gregset (const struct regcache *regcache, |
177 | elf_gregset_t *gregsetp, int regno) | |
917317f4 | 178 | { |
be0d2954 | 179 | gdb_byte *regp = (gdb_byte *) gregsetp; |
6ce2ac0b | 180 | int i; |
04cd15b6 | 181 | |
98df6387 | 182 | for (i = 0; i < I386_NUM_GREGS; i++) |
099a9414 | 183 | if (regno == -1 || regno == i) |
be0d2954 L |
184 | regcache_raw_collect (regcache, i, |
185 | regp + i386_linux_gregset_reg_offset[i]); | |
3fb1c838 | 186 | |
82ea117a | 187 | if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM) |
875f8d0e UW |
188 | && I386_LINUX_ORIG_EAX_REGNUM |
189 | < gdbarch_num_regs (get_regcache_arch (regcache))) | |
1777feb0 MS |
190 | regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp |
191 | + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]); | |
d4f3574e SS |
192 | } |
193 | ||
f60300e7 MK |
194 | #ifdef HAVE_PTRACE_GETREGS |
195 | ||
04cd15b6 MK |
196 | /* Fetch all general-purpose registers from process/thread TID and |
197 | store their values in GDB's register array. */ | |
d4f3574e | 198 | |
5c44784c | 199 | static void |
56be3814 | 200 | fetch_regs (struct regcache *regcache, int tid) |
5c44784c | 201 | { |
04cd15b6 | 202 | elf_gregset_t regs; |
2e024c20 | 203 | elf_gregset_t *regs_p = ®s; |
5c44784c | 204 | |
6ce2ac0b | 205 | if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) |
5c44784c | 206 | { |
f60300e7 MK |
207 | if (errno == EIO) |
208 | { | |
209 | /* The kernel we're running on doesn't support the GETREGS | |
210 | request. Reset `have_ptrace_getregs'. */ | |
211 | have_ptrace_getregs = 0; | |
212 | return; | |
213 | } | |
214 | ||
e2e0b3e5 | 215 | perror_with_name (_("Couldn't get registers")); |
5c44784c JM |
216 | } |
217 | ||
2e024c20 | 218 | supply_gregset (regcache, (const elf_gregset_t *) regs_p); |
5c44784c JM |
219 | } |
220 | ||
04cd15b6 MK |
221 | /* Store all valid general-purpose registers in GDB's register array |
222 | into the process/thread specified by TID. */ | |
5c44784c | 223 | |
5c44784c | 224 | static void |
56be3814 | 225 | store_regs (const struct regcache *regcache, int tid, int regno) |
5c44784c | 226 | { |
04cd15b6 | 227 | elf_gregset_t regs; |
5c44784c | 228 | |
6ce2ac0b | 229 | if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) |
e2e0b3e5 | 230 | perror_with_name (_("Couldn't get registers")); |
5c44784c | 231 | |
56be3814 | 232 | fill_gregset (regcache, ®s, regno); |
6ce2ac0b MK |
233 | |
234 | if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0) | |
e2e0b3e5 | 235 | perror_with_name (_("Couldn't write registers")); |
5c44784c JM |
236 | } |
237 | ||
f60300e7 MK |
238 | #else |
239 | ||
56be3814 UW |
240 | static void fetch_regs (struct regcache *regcache, int tid) {} |
241 | static void store_regs (const struct regcache *regcache, int tid, int regno) {} | |
f60300e7 MK |
242 | |
243 | #endif | |
5c44784c | 244 | \f |
5c44784c | 245 | |
6ce2ac0b | 246 | /* Transfering floating-point registers between GDB, inferiors and cores. */ |
d4f3574e | 247 | |
04cd15b6 | 248 | /* Fill GDB's register array with the floating-point register values in |
917317f4 | 249 | *FPREGSETP. */ |
04cd15b6 | 250 | |
d4f3574e | 251 | void |
7f7fe91e | 252 | supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp) |
d4f3574e | 253 | { |
7f7fe91e | 254 | i387_supply_fsave (regcache, -1, fpregsetp); |
917317f4 | 255 | } |
d4f3574e | 256 | |
04cd15b6 MK |
257 | /* Fill register REGNO (if it is a floating-point register) in |
258 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, | |
259 | do this for all registers. */ | |
917317f4 JM |
260 | |
261 | void | |
7f7fe91e UW |
262 | fill_fpregset (const struct regcache *regcache, |
263 | elf_fpregset_t *fpregsetp, int regno) | |
917317f4 | 264 | { |
7f7fe91e | 265 | i387_collect_fsave (regcache, regno, fpregsetp); |
d4f3574e SS |
266 | } |
267 | ||
f60300e7 MK |
268 | #ifdef HAVE_PTRACE_GETREGS |
269 | ||
04cd15b6 MK |
270 | /* Fetch all floating-point registers from process/thread TID and store |
271 | thier values in GDB's register array. */ | |
917317f4 | 272 | |
d4f3574e | 273 | static void |
56be3814 | 274 | fetch_fpregs (struct regcache *regcache, int tid) |
d4f3574e | 275 | { |
04cd15b6 | 276 | elf_fpregset_t fpregs; |
d4f3574e | 277 | |
6ce2ac0b | 278 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) |
e2e0b3e5 | 279 | perror_with_name (_("Couldn't get floating point status")); |
d4f3574e | 280 | |
56be3814 | 281 | supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs); |
d4f3574e SS |
282 | } |
283 | ||
04cd15b6 MK |
284 | /* Store all valid floating-point registers in GDB's register array |
285 | into the process/thread specified by TID. */ | |
d4f3574e | 286 | |
d4f3574e | 287 | static void |
56be3814 | 288 | store_fpregs (const struct regcache *regcache, int tid, int regno) |
d4f3574e | 289 | { |
04cd15b6 | 290 | elf_fpregset_t fpregs; |
d4f3574e | 291 | |
6ce2ac0b | 292 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) |
e2e0b3e5 | 293 | perror_with_name (_("Couldn't get floating point status")); |
d4f3574e | 294 | |
56be3814 | 295 | fill_fpregset (regcache, &fpregs, regno); |
d4f3574e | 296 | |
6ce2ac0b | 297 | if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0) |
e2e0b3e5 | 298 | perror_with_name (_("Couldn't write floating point status")); |
d4f3574e SS |
299 | } |
300 | ||
f60300e7 MK |
301 | #else |
302 | ||
1777feb0 MS |
303 | static void |
304 | fetch_fpregs (struct regcache *regcache, int tid) | |
305 | { | |
306 | } | |
307 | ||
308 | static void | |
309 | store_fpregs (const struct regcache *regcache, int tid, int regno) | |
310 | { | |
311 | } | |
f60300e7 MK |
312 | |
313 | #endif | |
5c44784c | 314 | \f |
d4f3574e | 315 | |
6ce2ac0b | 316 | /* Transfering floating-point and SSE registers to and from GDB. */ |
11cf8741 | 317 | |
c131fcee L |
318 | /* Fetch all registers covered by the PTRACE_GETREGSET request from |
319 | process/thread TID and store their values in GDB's register array. | |
320 | Return non-zero if successful, zero otherwise. */ | |
321 | ||
322 | static int | |
323 | fetch_xstateregs (struct regcache *regcache, int tid) | |
324 | { | |
df7e5265 | 325 | char xstateregs[X86_XSTATE_MAX_SIZE]; |
c131fcee L |
326 | struct iovec iov; |
327 | ||
328 | if (!have_ptrace_getregset) | |
329 | return 0; | |
330 | ||
331 | iov.iov_base = xstateregs; | |
332 | iov.iov_len = sizeof(xstateregs); | |
333 | if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, | |
334 | &iov) < 0) | |
335 | perror_with_name (_("Couldn't read extended state status")); | |
336 | ||
337 | i387_supply_xsave (regcache, -1, xstateregs); | |
338 | return 1; | |
339 | } | |
340 | ||
341 | /* Store all valid registers in GDB's register array covered by the | |
342 | PTRACE_SETREGSET request into the process/thread specified by TID. | |
343 | Return non-zero if successful, zero otherwise. */ | |
344 | ||
345 | static int | |
346 | store_xstateregs (const struct regcache *regcache, int tid, int regno) | |
347 | { | |
df7e5265 | 348 | char xstateregs[X86_XSTATE_MAX_SIZE]; |
c131fcee L |
349 | struct iovec iov; |
350 | ||
351 | if (!have_ptrace_getregset) | |
352 | return 0; | |
353 | ||
354 | iov.iov_base = xstateregs; | |
355 | iov.iov_len = sizeof(xstateregs); | |
356 | if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, | |
357 | &iov) < 0) | |
358 | perror_with_name (_("Couldn't read extended state status")); | |
359 | ||
360 | i387_collect_xsave (regcache, regno, xstateregs, 0); | |
361 | ||
362 | if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE, | |
363 | (int) &iov) < 0) | |
364 | perror_with_name (_("Couldn't write extended state status")); | |
365 | ||
366 | return 1; | |
367 | } | |
368 | ||
6ce2ac0b | 369 | #ifdef HAVE_PTRACE_GETFPXREGS |
04cd15b6 | 370 | |
6ce2ac0b | 371 | /* Fetch all registers covered by the PTRACE_GETFPXREGS request from |
04cd15b6 MK |
372 | process/thread TID and store their values in GDB's register array. |
373 | Return non-zero if successful, zero otherwise. */ | |
5c44784c | 374 | |
5c44784c | 375 | static int |
56be3814 | 376 | fetch_fpxregs (struct regcache *regcache, int tid) |
5c44784c | 377 | { |
6ce2ac0b | 378 | elf_fpxregset_t fpxregs; |
5c44784c | 379 | |
6ce2ac0b | 380 | if (! have_ptrace_getfpxregs) |
5c44784c JM |
381 | return 0; |
382 | ||
6ce2ac0b | 383 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) |
d4f3574e | 384 | { |
5c44784c JM |
385 | if (errno == EIO) |
386 | { | |
6ce2ac0b | 387 | have_ptrace_getfpxregs = 0; |
5c44784c JM |
388 | return 0; |
389 | } | |
390 | ||
e2e0b3e5 | 391 | perror_with_name (_("Couldn't read floating-point and SSE registers")); |
d4f3574e SS |
392 | } |
393 | ||
b7a8b4ef | 394 | i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs); |
5c44784c JM |
395 | return 1; |
396 | } | |
d4f3574e | 397 | |
04cd15b6 | 398 | /* Store all valid registers in GDB's register array covered by the |
6ce2ac0b | 399 | PTRACE_SETFPXREGS request into the process/thread specified by TID. |
04cd15b6 | 400 | Return non-zero if successful, zero otherwise. */ |
5c44784c | 401 | |
5c44784c | 402 | static int |
56be3814 | 403 | store_fpxregs (const struct regcache *regcache, int tid, int regno) |
5c44784c | 404 | { |
6ce2ac0b | 405 | elf_fpxregset_t fpxregs; |
5c44784c | 406 | |
6ce2ac0b | 407 | if (! have_ptrace_getfpxregs) |
5c44784c | 408 | return 0; |
6ce2ac0b MK |
409 | |
410 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1) | |
2866d305 MK |
411 | { |
412 | if (errno == EIO) | |
413 | { | |
414 | have_ptrace_getfpxregs = 0; | |
415 | return 0; | |
416 | } | |
417 | ||
e2e0b3e5 | 418 | perror_with_name (_("Couldn't read floating-point and SSE registers")); |
2866d305 | 419 | } |
5c44784c | 420 | |
b7a8b4ef | 421 | i387_collect_fxsave (regcache, regno, &fpxregs); |
5c44784c | 422 | |
6ce2ac0b | 423 | if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1) |
e2e0b3e5 | 424 | perror_with_name (_("Couldn't write floating-point and SSE registers")); |
5c44784c JM |
425 | |
426 | return 1; | |
427 | } | |
428 | ||
5c44784c JM |
429 | #else |
430 | ||
1777feb0 MS |
431 | static int |
432 | fetch_fpxregs (struct regcache *regcache, int tid) | |
433 | { | |
434 | return 0; | |
435 | } | |
436 | ||
437 | static int | |
438 | store_fpxregs (const struct regcache *regcache, int tid, int regno) | |
439 | { | |
440 | return 0; | |
441 | } | |
5c44784c | 442 | |
6ce2ac0b | 443 | #endif /* HAVE_PTRACE_GETFPXREGS */ |
5c44784c | 444 | \f |
6ce2ac0b | 445 | |
5c44784c | 446 | /* Transferring arbitrary registers between GDB and inferior. */ |
d4f3574e | 447 | |
04cd15b6 MK |
448 | /* Fetch register REGNO from the child process. If REGNO is -1, do |
449 | this for all registers (including the floating point and SSE | |
450 | registers). */ | |
d4f3574e | 451 | |
10d6c8cd | 452 | static void |
28439f5e PA |
453 | i386_linux_fetch_inferior_registers (struct target_ops *ops, |
454 | struct regcache *regcache, int regno) | |
d4f3574e | 455 | { |
ed9a39eb JM |
456 | int tid; |
457 | ||
f60300e7 MK |
458 | /* Use the old method of peeking around in `struct user' if the |
459 | GETREGS request isn't available. */ | |
ce556f85 | 460 | if (!have_ptrace_getregs) |
f60300e7 | 461 | { |
ce556f85 MK |
462 | int i; |
463 | ||
875f8d0e | 464 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
ce556f85 | 465 | if (regno == -1 || regno == i) |
56be3814 | 466 | fetch_register (regcache, i); |
ce556f85 | 467 | |
f60300e7 MK |
468 | return; |
469 | } | |
470 | ||
a4b6fc86 | 471 | /* GNU/Linux LWP ID's are process ID's. */ |
dfd4cc63 | 472 | tid = ptid_get_lwp (inferior_ptid); |
e64a344c | 473 | if (tid == 0) |
dfd4cc63 | 474 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
ed9a39eb | 475 | |
6ce2ac0b | 476 | /* Use the PTRACE_GETFPXREGS request whenever possible, since it |
04cd15b6 | 477 | transfers more registers in one system call, and we'll cache the |
6ce2ac0b | 478 | results. But remember that fetch_fpxregs can fail, and return |
04cd15b6 | 479 | zero. */ |
5c44784c JM |
480 | if (regno == -1) |
481 | { | |
56be3814 | 482 | fetch_regs (regcache, tid); |
f60300e7 MK |
483 | |
484 | /* The call above might reset `have_ptrace_getregs'. */ | |
ce556f85 | 485 | if (!have_ptrace_getregs) |
f60300e7 | 486 | { |
84e473c8 | 487 | i386_linux_fetch_inferior_registers (ops, regcache, regno); |
f60300e7 MK |
488 | return; |
489 | } | |
490 | ||
c131fcee L |
491 | if (fetch_xstateregs (regcache, tid)) |
492 | return; | |
56be3814 | 493 | if (fetch_fpxregs (regcache, tid)) |
5c44784c | 494 | return; |
56be3814 | 495 | fetch_fpregs (regcache, tid); |
5c44784c JM |
496 | return; |
497 | } | |
d4f3574e | 498 | |
5c44784c JM |
499 | if (GETREGS_SUPPLIES (regno)) |
500 | { | |
56be3814 | 501 | fetch_regs (regcache, tid); |
5c44784c JM |
502 | return; |
503 | } | |
504 | ||
c131fcee L |
505 | if (GETXSTATEREGS_SUPPLIES (regno)) |
506 | { | |
507 | if (fetch_xstateregs (regcache, tid)) | |
508 | return; | |
509 | } | |
510 | ||
6ce2ac0b | 511 | if (GETFPXREGS_SUPPLIES (regno)) |
5c44784c | 512 | { |
56be3814 | 513 | if (fetch_fpxregs (regcache, tid)) |
5c44784c JM |
514 | return; |
515 | ||
516 | /* Either our processor or our kernel doesn't support the SSE | |
517 | registers, so read the FP registers in the traditional way, | |
518 | and fill the SSE registers with dummy values. It would be | |
519 | more graceful to handle differences in the register set using | |
520 | gdbarch. Until then, this will at least make things work | |
521 | plausibly. */ | |
56be3814 | 522 | fetch_fpregs (regcache, tid); |
5c44784c JM |
523 | return; |
524 | } | |
525 | ||
8e65ff28 | 526 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 527 | _("Got request for bad register number %d."), regno); |
d4f3574e SS |
528 | } |
529 | ||
04cd15b6 MK |
530 | /* Store register REGNO back into the child process. If REGNO is -1, |
531 | do this for all registers (including the floating point and SSE | |
532 | registers). */ | |
10d6c8cd | 533 | static void |
28439f5e PA |
534 | i386_linux_store_inferior_registers (struct target_ops *ops, |
535 | struct regcache *regcache, int regno) | |
d4f3574e | 536 | { |
ed9a39eb JM |
537 | int tid; |
538 | ||
f60300e7 MK |
539 | /* Use the old method of poking around in `struct user' if the |
540 | SETREGS request isn't available. */ | |
ce556f85 | 541 | if (!have_ptrace_getregs) |
f60300e7 | 542 | { |
ce556f85 MK |
543 | int i; |
544 | ||
875f8d0e | 545 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
ce556f85 | 546 | if (regno == -1 || regno == i) |
56be3814 | 547 | store_register (regcache, i); |
ce556f85 | 548 | |
f60300e7 MK |
549 | return; |
550 | } | |
551 | ||
a4b6fc86 | 552 | /* GNU/Linux LWP ID's are process ID's. */ |
dfd4cc63 | 553 | tid = ptid_get_lwp (inferior_ptid); |
e64a344c | 554 | if (tid == 0) |
dfd4cc63 | 555 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
ed9a39eb | 556 | |
6ce2ac0b | 557 | /* Use the PTRACE_SETFPXREGS requests whenever possible, since it |
04cd15b6 | 558 | transfers more registers in one system call. But remember that |
6ce2ac0b | 559 | store_fpxregs can fail, and return zero. */ |
5c44784c JM |
560 | if (regno == -1) |
561 | { | |
56be3814 | 562 | store_regs (regcache, tid, regno); |
c131fcee L |
563 | if (store_xstateregs (regcache, tid, regno)) |
564 | return; | |
56be3814 | 565 | if (store_fpxregs (regcache, tid, regno)) |
5c44784c | 566 | return; |
56be3814 | 567 | store_fpregs (regcache, tid, regno); |
5c44784c JM |
568 | return; |
569 | } | |
d4f3574e | 570 | |
5c44784c JM |
571 | if (GETREGS_SUPPLIES (regno)) |
572 | { | |
56be3814 | 573 | store_regs (regcache, tid, regno); |
5c44784c JM |
574 | return; |
575 | } | |
576 | ||
c131fcee L |
577 | if (GETXSTATEREGS_SUPPLIES (regno)) |
578 | { | |
579 | if (store_xstateregs (regcache, tid, regno)) | |
580 | return; | |
581 | } | |
582 | ||
6ce2ac0b | 583 | if (GETFPXREGS_SUPPLIES (regno)) |
5c44784c | 584 | { |
56be3814 | 585 | if (store_fpxregs (regcache, tid, regno)) |
5c44784c JM |
586 | return; |
587 | ||
588 | /* Either our processor or our kernel doesn't support the SSE | |
04cd15b6 MK |
589 | registers, so just write the FP registers in the traditional |
590 | way. */ | |
56be3814 | 591 | store_fpregs (regcache, tid, regno); |
5c44784c JM |
592 | return; |
593 | } | |
594 | ||
8e65ff28 | 595 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 596 | _("Got request to store bad register number %d."), regno); |
d4f3574e | 597 | } |
de57eccd | 598 | \f |
6ce2ac0b | 599 | |
8c420b8d GB |
600 | /* Called by libthread_db. Returns a pointer to the thread local |
601 | storage (or its descriptor). */ | |
602 | ||
603 | ps_err_e | |
604 | ps_get_thread_area (const struct ps_prochandle *ph, | |
605 | lwpid_t lwpid, int idx, void **base) | |
606 | { | |
607 | unsigned int base_addr; | |
608 | ps_err_e result; | |
609 | ||
610 | result = x86_linux_get_thread_area (lwpid, (void *) idx, &base_addr); | |
611 | ||
612 | if (result == PS_OK) | |
613 | *(int *) base = base_addr; | |
614 | ||
615 | return result; | |
616 | } | |
5bca7895 MK |
617 | \f |
618 | ||
a4b6fc86 | 619 | /* The instruction for a GNU/Linux system call is: |
a6abb2c0 MK |
620 | int $0x80 |
621 | or 0xcd 0x80. */ | |
622 | ||
623 | static const unsigned char linux_syscall[] = { 0xcd, 0x80 }; | |
624 | ||
625 | #define LINUX_SYSCALL_LEN (sizeof linux_syscall) | |
626 | ||
627 | /* The system call number is stored in the %eax register. */ | |
7532965f | 628 | #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM |
a6abb2c0 MK |
629 | |
630 | /* We are specifically interested in the sigreturn and rt_sigreturn | |
631 | system calls. */ | |
632 | ||
633 | #ifndef SYS_sigreturn | |
634 | #define SYS_sigreturn 0x77 | |
635 | #endif | |
636 | #ifndef SYS_rt_sigreturn | |
637 | #define SYS_rt_sigreturn 0xad | |
638 | #endif | |
639 | ||
640 | /* Offset to saved processor flags, from <asm/sigcontext.h>. */ | |
641 | #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64) | |
642 | ||
643 | /* Resume execution of the inferior process. | |
644 | If STEP is nonzero, single-step it. | |
645 | If SIGNAL is nonzero, give it that signal. */ | |
646 | ||
10d6c8cd | 647 | static void |
28439f5e | 648 | i386_linux_resume (struct target_ops *ops, |
2ea28649 | 649 | ptid_t ptid, int step, enum gdb_signal signal) |
a6abb2c0 | 650 | { |
dfd4cc63 | 651 | int pid = ptid_get_pid (ptid); |
39f77062 | 652 | |
a96d9b2e SDJ |
653 | int request; |
654 | ||
655 | if (catch_syscall_enabled () > 0) | |
656 | request = PTRACE_SYSCALL; | |
657 | else | |
658 | request = PTRACE_CONT; | |
a6abb2c0 | 659 | |
a6abb2c0 MK |
660 | if (step) |
661 | { | |
594f7785 | 662 | struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid)); |
e17a4113 UW |
663 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
664 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
7b86a1b8 | 665 | ULONGEST pc; |
8e70166d | 666 | gdb_byte buf[LINUX_SYSCALL_LEN]; |
a6abb2c0 MK |
667 | |
668 | request = PTRACE_SINGLESTEP; | |
669 | ||
e17a4113 UW |
670 | regcache_cooked_read_unsigned (regcache, |
671 | gdbarch_pc_regnum (gdbarch), &pc); | |
7b86a1b8 | 672 | |
a6abb2c0 MK |
673 | /* Returning from a signal trampoline is done by calling a |
674 | special system call (sigreturn or rt_sigreturn, see | |
675 | i386-linux-tdep.c for more information). This system call | |
676 | restores the registers that were saved when the signal was | |
677 | raised, including %eflags. That means that single-stepping | |
678 | won't work. Instead, we'll have to modify the signal context | |
679 | that's about to be restored, and set the trace flag there. */ | |
680 | ||
681 | /* First check if PC is at a system call. */ | |
8defab1a | 682 | if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0 |
a6abb2c0 MK |
683 | && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0) |
684 | { | |
7b86a1b8 UW |
685 | ULONGEST syscall; |
686 | regcache_cooked_read_unsigned (regcache, | |
687 | LINUX_SYSCALL_REGNUM, &syscall); | |
a6abb2c0 MK |
688 | |
689 | /* Then check the system call number. */ | |
690 | if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn) | |
691 | { | |
7b86a1b8 | 692 | ULONGEST sp, addr; |
a6abb2c0 | 693 | unsigned long int eflags; |
7bf0983e | 694 | |
7b86a1b8 | 695 | regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp); |
a6abb2c0 | 696 | if (syscall == SYS_rt_sigreturn) |
f3d6df6d YQ |
697 | addr = read_memory_unsigned_integer (sp + 8, 4, byte_order) |
698 | + 20; | |
7b86a1b8 UW |
699 | else |
700 | addr = sp; | |
a6abb2c0 MK |
701 | |
702 | /* Set the trace flag in the context that's about to be | |
703 | restored. */ | |
704 | addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET; | |
8e70166d | 705 | read_memory (addr, (gdb_byte *) &eflags, 4); |
a6abb2c0 | 706 | eflags |= 0x0100; |
8e70166d | 707 | write_memory (addr, (gdb_byte *) &eflags, 4); |
a6abb2c0 MK |
708 | } |
709 | } | |
710 | } | |
711 | ||
2ea28649 | 712 | if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1) |
e2e0b3e5 | 713 | perror_with_name (("ptrace")); |
a6abb2c0 | 714 | } |
040baaf6 | 715 | \f |
c1e246a0 GB |
716 | |
717 | /* -Wmissing-prototypes */ | |
718 | extern initialize_file_ftype _initialize_i386_linux_nat; | |
719 | ||
720 | void | |
721 | _initialize_i386_linux_nat (void) | |
722 | { | |
723 | /* Create a generic x86 GNU/Linux target. */ | |
724 | struct target_ops *t = x86_linux_create_target (); | |
725 | ||
726 | /* Override the default ptrace resume method. */ | |
727 | t->to_resume = i386_linux_resume; | |
728 | ||
729 | /* Add our register access methods. */ | |
730 | t->to_fetch_registers = i386_linux_fetch_inferior_registers; | |
731 | t->to_store_registers = i386_linux_store_inferior_registers; | |
732 | ||
733 | /* Add the target. */ | |
734 | x86_linux_add_target (t); | |
735 | } |