Commit | Line | Data |
---|---|---|
a4b6fc86 AC |
1 | /* Native-dependent code for GNU/Linux x86. |
2 | ||
975aec09 | 3 | Copyright 1999, 2000, 2001, 2002 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 | |
9 | the Free Software Foundation; either version 2 of the License, or | |
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 MK |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
d4f3574e SS |
21 | |
22 | #include "defs.h" | |
23 | #include "inferior.h" | |
24 | #include "gdbcore.h" | |
4e052eda | 25 | #include "regcache.h" |
d4f3574e | 26 | |
84346e11 | 27 | #include "gdb_assert.h" |
d4f3574e SS |
28 | #include <sys/ptrace.h> |
29 | #include <sys/user.h> | |
30 | #include <sys/procfs.h> | |
31 | ||
32 | #ifdef HAVE_SYS_REG_H | |
33 | #include <sys/reg.h> | |
34 | #endif | |
35 | ||
84346e11 MK |
36 | #ifdef HAVE_SYS_DEBUGREG_H |
37 | #include <sys/debugreg.h> | |
38 | #endif | |
39 | ||
40 | #ifndef DR_FIRSTADDR | |
41 | #define DR_FIRSTADDR 0 | |
42 | #endif | |
43 | ||
44 | #ifndef DR_LASTADDR | |
45 | #define DR_LASTADDR 3 | |
46 | #endif | |
47 | ||
48 | #ifndef DR_STATUS | |
49 | #define DR_STATUS 6 | |
50 | #endif | |
51 | ||
52 | #ifndef DR_CONTROL | |
53 | #define DR_CONTROL 7 | |
54 | #endif | |
55 | ||
6ce2ac0b | 56 | /* Prototypes for supply_gregset etc. */ |
c60c0f5f MS |
57 | #include "gregset.h" |
58 | ||
6ce2ac0b MK |
59 | /* Prototypes for i387_supply_fsave etc. */ |
60 | #include "i387-nat.h" | |
61 | ||
c3833324 MS |
62 | /* Defines for XMM0_REGNUM etc. */ |
63 | #include "i386-tdep.h" | |
64 | ||
756ed206 MK |
65 | /* Prototypes for local functions. */ |
66 | static void dummy_sse_values (void); | |
67 | ||
6ce2ac0b | 68 | \f |
d4f3574e | 69 | |
a4b6fc86 AC |
70 | /* The register sets used in GNU/Linux ELF core-dumps are identical to |
71 | the register sets in `struct user' that is used for a.out | |
72 | core-dumps, and is also used by `ptrace'. The corresponding types | |
73 | are `elf_gregset_t' for the general-purpose registers (with | |
04cd15b6 MK |
74 | `elf_greg_t' the type of a single GP register) and `elf_fpregset_t' |
75 | for the floating-point registers. | |
76 | ||
77 | Those types used to be available under the names `gregset_t' and | |
78 | `fpregset_t' too, and this file used those names in the past. But | |
79 | those names are now used for the register sets used in the | |
80 | `mcontext_t' type, and have a different size and layout. */ | |
81 | ||
82 | /* Mapping between the general-purpose registers in `struct user' | |
83 | format and GDB's register array layout. */ | |
d4f3574e SS |
84 | static int regmap[] = |
85 | { | |
86 | EAX, ECX, EDX, EBX, | |
87 | UESP, EBP, ESI, EDI, | |
88 | EIP, EFL, CS, SS, | |
04cd15b6 | 89 | DS, ES, FS, GS |
d4f3574e SS |
90 | }; |
91 | ||
5c44784c JM |
92 | /* Which ptrace request retrieves which registers? |
93 | These apply to the corresponding SET requests as well. */ | |
94 | #define GETREGS_SUPPLIES(regno) \ | |
3fb1c838 | 95 | ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM) |
5c44784c JM |
96 | #define GETFPREGS_SUPPLIES(regno) \ |
97 | (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM) | |
6ce2ac0b | 98 | #define GETFPXREGS_SUPPLIES(regno) \ |
5c44784c JM |
99 | (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM) |
100 | ||
f60300e7 MK |
101 | /* Does the current host support the GETREGS request? */ |
102 | int have_ptrace_getregs = | |
103 | #ifdef HAVE_PTRACE_GETREGS | |
104 | 1 | |
105 | #else | |
106 | 0 | |
107 | #endif | |
108 | ; | |
109 | ||
6ce2ac0b | 110 | /* Does the current host support the GETFPXREGS request? The header |
5c44784c JM |
111 | file may or may not define it, and even if it is defined, the |
112 | kernel will return EIO if it's running on a pre-SSE processor. | |
113 | ||
114 | My instinct is to attach this to some architecture- or | |
115 | target-specific data structure, but really, a particular GDB | |
116 | process can only run on top of one kernel at a time. So it's okay | |
117 | for this to be a simple variable. */ | |
6ce2ac0b MK |
118 | int have_ptrace_getfpxregs = |
119 | #ifdef HAVE_PTRACE_GETFPXREGS | |
5c44784c JM |
120 | 1 |
121 | #else | |
122 | 0 | |
123 | #endif | |
124 | ; | |
f60300e7 | 125 | \f |
6ce2ac0b | 126 | |
84346e11 MK |
127 | /* Support for the user struct. */ |
128 | ||
129 | /* Return the address of register REGNUM. BLOCKEND is the value of | |
130 | u.u_ar0, which should point to the registers. */ | |
131 | ||
132 | CORE_ADDR | |
133 | register_u_addr (CORE_ADDR blockend, int regnum) | |
134 | { | |
135 | return (blockend + 4 * regmap[regnum]); | |
136 | } | |
137 | ||
138 | /* Return the size of the user struct. */ | |
139 | ||
140 | int | |
141 | kernel_u_size (void) | |
142 | { | |
143 | return (sizeof (struct user)); | |
144 | } | |
145 | \f | |
146 | ||
97780f5f JB |
147 | /* Fetching registers directly from the U area, one at a time. */ |
148 | ||
f60300e7 MK |
149 | /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'. |
150 | The problem is that we define FETCH_INFERIOR_REGISTERS since we | |
151 | want to use our own versions of {fetch,store}_inferior_registers | |
152 | that use the GETREGS request. This means that the code in | |
153 | `infptrace.c' is #ifdef'd out. But we need to fall back on that | |
154 | code when GDB is running on top of a kernel that doesn't support | |
155 | the GETREGS request. I want to avoid changing `infptrace.c' right | |
156 | now. */ | |
157 | ||
318b21ef MK |
158 | #ifndef PT_READ_U |
159 | #define PT_READ_U PTRACE_PEEKUSR | |
160 | #endif | |
161 | #ifndef PT_WRITE_U | |
162 | #define PT_WRITE_U PTRACE_POKEUSR | |
163 | #endif | |
164 | ||
f60300e7 MK |
165 | /* Default the type of the ptrace transfer to int. */ |
166 | #ifndef PTRACE_XFER_TYPE | |
167 | #define PTRACE_XFER_TYPE int | |
168 | #endif | |
169 | ||
170 | /* Registers we shouldn't try to fetch. */ | |
d5d65353 | 171 | #define OLD_CANNOT_FETCH_REGISTER(regno) ((regno) >= NUM_GREGS) |
f60300e7 MK |
172 | |
173 | /* Fetch one register. */ | |
174 | ||
175 | static void | |
fba45db2 | 176 | fetch_register (int regno) |
f60300e7 MK |
177 | { |
178 | /* This isn't really an address. But ptrace thinks of it as one. */ | |
179 | CORE_ADDR regaddr; | |
180 | char mess[128]; /* For messages */ | |
181 | register int i; | |
182 | unsigned int offset; /* Offset of registers within the u area. */ | |
183 | char buf[MAX_REGISTER_RAW_SIZE]; | |
184 | int tid; | |
185 | ||
d5d65353 | 186 | if (OLD_CANNOT_FETCH_REGISTER (regno)) |
f60300e7 MK |
187 | { |
188 | memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */ | |
189 | supply_register (regno, buf); | |
190 | return; | |
191 | } | |
192 | ||
193 | /* Overload thread id onto process id */ | |
39f77062 KB |
194 | if ((tid = TIDGET (inferior_ptid)) == 0) |
195 | tid = PIDGET (inferior_ptid); /* no thread id, just use process id */ | |
f60300e7 MK |
196 | |
197 | offset = U_REGS_OFFSET; | |
198 | ||
199 | regaddr = register_addr (regno, offset); | |
200 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) | |
201 | { | |
202 | errno = 0; | |
203 | *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid, | |
204 | (PTRACE_ARG3_TYPE) regaddr, 0); | |
205 | regaddr += sizeof (PTRACE_XFER_TYPE); | |
206 | if (errno != 0) | |
207 | { | |
208 | sprintf (mess, "reading register %s (#%d)", | |
209 | REGISTER_NAME (regno), regno); | |
210 | perror_with_name (mess); | |
211 | } | |
212 | } | |
213 | supply_register (regno, buf); | |
214 | } | |
215 | ||
216 | /* Fetch register values from the inferior. | |
217 | If REGNO is negative, do this for all registers. | |
218 | Otherwise, REGNO specifies which register (so we can save time). */ | |
219 | ||
220 | void | |
fba45db2 | 221 | old_fetch_inferior_registers (int regno) |
f60300e7 MK |
222 | { |
223 | if (regno >= 0) | |
224 | { | |
225 | fetch_register (regno); | |
226 | } | |
227 | else | |
228 | { | |
a728f042 | 229 | for (regno = 0; regno < NUM_REGS; regno++) |
f60300e7 MK |
230 | { |
231 | fetch_register (regno); | |
232 | } | |
233 | } | |
234 | } | |
235 | ||
236 | /* Registers we shouldn't try to store. */ | |
d5d65353 | 237 | #define OLD_CANNOT_STORE_REGISTER(regno) ((regno) >= NUM_GREGS) |
f60300e7 MK |
238 | |
239 | /* Store one register. */ | |
240 | ||
241 | static void | |
fba45db2 | 242 | store_register (int regno) |
f60300e7 MK |
243 | { |
244 | /* This isn't really an address. But ptrace thinks of it as one. */ | |
245 | CORE_ADDR regaddr; | |
246 | char mess[128]; /* For messages */ | |
247 | register int i; | |
248 | unsigned int offset; /* Offset of registers within the u area. */ | |
249 | int tid; | |
250 | ||
d5d65353 | 251 | if (OLD_CANNOT_STORE_REGISTER (regno)) |
f60300e7 MK |
252 | { |
253 | return; | |
254 | } | |
255 | ||
256 | /* Overload thread id onto process id */ | |
39f77062 KB |
257 | if ((tid = TIDGET (inferior_ptid)) == 0) |
258 | tid = PIDGET (inferior_ptid); /* no thread id, just use process id */ | |
f60300e7 MK |
259 | |
260 | offset = U_REGS_OFFSET; | |
261 | ||
262 | regaddr = register_addr (regno, offset); | |
263 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) | |
264 | { | |
265 | errno = 0; | |
266 | ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr, | |
267 | *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]); | |
268 | regaddr += sizeof (PTRACE_XFER_TYPE); | |
269 | if (errno != 0) | |
270 | { | |
271 | sprintf (mess, "writing register %s (#%d)", | |
272 | REGISTER_NAME (regno), regno); | |
273 | perror_with_name (mess); | |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | /* Store our register values back into the inferior. | |
279 | If REGNO is negative, do this for all registers. | |
280 | Otherwise, REGNO specifies which register (so we can save time). */ | |
281 | ||
282 | void | |
fba45db2 | 283 | old_store_inferior_registers (int regno) |
f60300e7 MK |
284 | { |
285 | if (regno >= 0) | |
286 | { | |
287 | store_register (regno); | |
288 | } | |
289 | else | |
290 | { | |
a728f042 | 291 | for (regno = 0; regno < NUM_REGS; regno++) |
f60300e7 MK |
292 | { |
293 | store_register (regno); | |
294 | } | |
295 | } | |
296 | } | |
5c44784c | 297 | \f |
6ce2ac0b | 298 | |
04cd15b6 MK |
299 | /* Transfering the general-purpose registers between GDB, inferiors |
300 | and core files. */ | |
301 | ||
ad2a4d09 | 302 | /* Fill GDB's register array with the general-purpose register values |
04cd15b6 | 303 | in *GREGSETP. */ |
5c44784c | 304 | |
d4f3574e | 305 | void |
04cd15b6 | 306 | supply_gregset (elf_gregset_t *gregsetp) |
d4f3574e | 307 | { |
04cd15b6 | 308 | elf_greg_t *regp = (elf_greg_t *) gregsetp; |
6ce2ac0b | 309 | int i; |
d4f3574e | 310 | |
6ce2ac0b MK |
311 | for (i = 0; i < NUM_GREGS; i++) |
312 | supply_register (i, (char *) (regp + regmap[i])); | |
3fb1c838 MK |
313 | |
314 | supply_register (I386_LINUX_ORIG_EAX_REGNUM, (char *) (regp + ORIG_EAX)); | |
917317f4 JM |
315 | } |
316 | ||
04cd15b6 MK |
317 | /* Fill register REGNO (if it is a general-purpose register) in |
318 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, | |
319 | do this for all registers. */ | |
6ce2ac0b | 320 | |
917317f4 | 321 | void |
04cd15b6 | 322 | fill_gregset (elf_gregset_t *gregsetp, int regno) |
917317f4 | 323 | { |
6ce2ac0b MK |
324 | elf_greg_t *regp = (elf_greg_t *) gregsetp; |
325 | int i; | |
04cd15b6 | 326 | |
6ce2ac0b MK |
327 | for (i = 0; i < NUM_GREGS; i++) |
328 | if ((regno == -1 || regno == i)) | |
8a406745 | 329 | regcache_collect (i, regp + regmap[i]); |
3fb1c838 MK |
330 | |
331 | if (regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM) | |
76fb44f4 | 332 | regcache_collect (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX); |
d4f3574e SS |
333 | } |
334 | ||
f60300e7 MK |
335 | #ifdef HAVE_PTRACE_GETREGS |
336 | ||
04cd15b6 MK |
337 | /* Fetch all general-purpose registers from process/thread TID and |
338 | store their values in GDB's register array. */ | |
d4f3574e | 339 | |
5c44784c | 340 | static void |
ed9a39eb | 341 | fetch_regs (int tid) |
5c44784c | 342 | { |
04cd15b6 | 343 | elf_gregset_t regs; |
5c44784c | 344 | |
6ce2ac0b | 345 | if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) |
5c44784c | 346 | { |
f60300e7 MK |
347 | if (errno == EIO) |
348 | { | |
349 | /* The kernel we're running on doesn't support the GETREGS | |
350 | request. Reset `have_ptrace_getregs'. */ | |
351 | have_ptrace_getregs = 0; | |
352 | return; | |
353 | } | |
354 | ||
6ce2ac0b | 355 | perror_with_name ("Couldn't get registers"); |
5c44784c JM |
356 | } |
357 | ||
04cd15b6 | 358 | supply_gregset (®s); |
5c44784c JM |
359 | } |
360 | ||
04cd15b6 MK |
361 | /* Store all valid general-purpose registers in GDB's register array |
362 | into the process/thread specified by TID. */ | |
5c44784c | 363 | |
5c44784c | 364 | static void |
6ce2ac0b | 365 | store_regs (int tid, int regno) |
5c44784c | 366 | { |
04cd15b6 | 367 | elf_gregset_t regs; |
5c44784c | 368 | |
6ce2ac0b MK |
369 | if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) |
370 | perror_with_name ("Couldn't get registers"); | |
5c44784c | 371 | |
6ce2ac0b MK |
372 | fill_gregset (®s, regno); |
373 | ||
374 | if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0) | |
375 | perror_with_name ("Couldn't write registers"); | |
5c44784c JM |
376 | } |
377 | ||
f60300e7 MK |
378 | #else |
379 | ||
380 | static void fetch_regs (int tid) {} | |
6ce2ac0b | 381 | static void store_regs (int tid, int regno) {} |
f60300e7 MK |
382 | |
383 | #endif | |
5c44784c | 384 | \f |
5c44784c | 385 | |
6ce2ac0b | 386 | /* Transfering floating-point registers between GDB, inferiors and cores. */ |
d4f3574e | 387 | |
04cd15b6 | 388 | /* Fill GDB's register array with the floating-point register values in |
917317f4 | 389 | *FPREGSETP. */ |
04cd15b6 | 390 | |
d4f3574e | 391 | void |
04cd15b6 | 392 | supply_fpregset (elf_fpregset_t *fpregsetp) |
d4f3574e | 393 | { |
6ce2ac0b | 394 | i387_supply_fsave ((char *) fpregsetp); |
756ed206 | 395 | dummy_sse_values (); |
917317f4 | 396 | } |
d4f3574e | 397 | |
04cd15b6 MK |
398 | /* Fill register REGNO (if it is a floating-point register) in |
399 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, | |
400 | do this for all registers. */ | |
917317f4 JM |
401 | |
402 | void | |
04cd15b6 | 403 | fill_fpregset (elf_fpregset_t *fpregsetp, int regno) |
917317f4 | 404 | { |
6ce2ac0b | 405 | i387_fill_fsave ((char *) fpregsetp, regno); |
d4f3574e SS |
406 | } |
407 | ||
f60300e7 MK |
408 | #ifdef HAVE_PTRACE_GETREGS |
409 | ||
04cd15b6 MK |
410 | /* Fetch all floating-point registers from process/thread TID and store |
411 | thier values in GDB's register array. */ | |
917317f4 | 412 | |
d4f3574e | 413 | static void |
ed9a39eb | 414 | fetch_fpregs (int tid) |
d4f3574e | 415 | { |
04cd15b6 | 416 | elf_fpregset_t fpregs; |
d4f3574e | 417 | |
6ce2ac0b MK |
418 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) |
419 | perror_with_name ("Couldn't get floating point status"); | |
d4f3574e | 420 | |
04cd15b6 | 421 | supply_fpregset (&fpregs); |
d4f3574e SS |
422 | } |
423 | ||
04cd15b6 MK |
424 | /* Store all valid floating-point registers in GDB's register array |
425 | into the process/thread specified by TID. */ | |
d4f3574e | 426 | |
d4f3574e | 427 | static void |
6ce2ac0b | 428 | store_fpregs (int tid, int regno) |
d4f3574e | 429 | { |
04cd15b6 | 430 | elf_fpregset_t fpregs; |
d4f3574e | 431 | |
6ce2ac0b MK |
432 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) |
433 | perror_with_name ("Couldn't get floating point status"); | |
d4f3574e | 434 | |
6ce2ac0b | 435 | fill_fpregset (&fpregs, regno); |
d4f3574e | 436 | |
6ce2ac0b MK |
437 | if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0) |
438 | perror_with_name ("Couldn't write floating point status"); | |
d4f3574e SS |
439 | } |
440 | ||
f60300e7 MK |
441 | #else |
442 | ||
443 | static void fetch_fpregs (int tid) {} | |
6ce2ac0b | 444 | static void store_fpregs (int tid, int regno) {} |
f60300e7 MK |
445 | |
446 | #endif | |
5c44784c | 447 | \f |
d4f3574e | 448 | |
6ce2ac0b | 449 | /* Transfering floating-point and SSE registers to and from GDB. */ |
11cf8741 | 450 | |
6ce2ac0b | 451 | #ifdef HAVE_PTRACE_GETFPXREGS |
04cd15b6 MK |
452 | |
453 | /* Fill GDB's register array with the floating-point and SSE register | |
6ce2ac0b | 454 | values in *FPXREGSETP. */ |
04cd15b6 | 455 | |
975aec09 | 456 | void |
6ce2ac0b | 457 | supply_fpxregset (elf_fpxregset_t *fpxregsetp) |
d4f3574e | 458 | { |
6ce2ac0b | 459 | i387_supply_fxsave ((char *) fpxregsetp); |
d4f3574e SS |
460 | } |
461 | ||
6ce2ac0b MK |
462 | /* Fill register REGNO (if it is a floating-point or SSE register) in |
463 | *FPXREGSETP with the value in GDB's register array. If REGNO is | |
464 | -1, do this for all registers. */ | |
d4f3574e | 465 | |
975aec09 | 466 | void |
6ce2ac0b | 467 | fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno) |
d4f3574e | 468 | { |
6ce2ac0b | 469 | i387_fill_fxsave ((char *) fpxregsetp, regno); |
5c44784c JM |
470 | } |
471 | ||
6ce2ac0b | 472 | /* Fetch all registers covered by the PTRACE_GETFPXREGS request from |
04cd15b6 MK |
473 | process/thread TID and store their values in GDB's register array. |
474 | Return non-zero if successful, zero otherwise. */ | |
5c44784c | 475 | |
5c44784c | 476 | static int |
6ce2ac0b | 477 | fetch_fpxregs (int tid) |
5c44784c | 478 | { |
6ce2ac0b | 479 | elf_fpxregset_t fpxregs; |
5c44784c | 480 | |
6ce2ac0b | 481 | if (! have_ptrace_getfpxregs) |
5c44784c JM |
482 | return 0; |
483 | ||
6ce2ac0b | 484 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) |
d4f3574e | 485 | { |
5c44784c JM |
486 | if (errno == EIO) |
487 | { | |
6ce2ac0b | 488 | have_ptrace_getfpxregs = 0; |
5c44784c JM |
489 | return 0; |
490 | } | |
491 | ||
6ce2ac0b | 492 | perror_with_name ("Couldn't read floating-point and SSE registers"); |
d4f3574e SS |
493 | } |
494 | ||
6ce2ac0b | 495 | supply_fpxregset (&fpxregs); |
5c44784c JM |
496 | return 1; |
497 | } | |
d4f3574e | 498 | |
04cd15b6 | 499 | /* Store all valid registers in GDB's register array covered by the |
6ce2ac0b | 500 | PTRACE_SETFPXREGS request into the process/thread specified by TID. |
04cd15b6 | 501 | Return non-zero if successful, zero otherwise. */ |
5c44784c | 502 | |
5c44784c | 503 | static int |
6ce2ac0b | 504 | store_fpxregs (int tid, int regno) |
5c44784c | 505 | { |
6ce2ac0b | 506 | elf_fpxregset_t fpxregs; |
5c44784c | 507 | |
6ce2ac0b | 508 | if (! have_ptrace_getfpxregs) |
5c44784c | 509 | return 0; |
6ce2ac0b MK |
510 | |
511 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1) | |
2866d305 MK |
512 | { |
513 | if (errno == EIO) | |
514 | { | |
515 | have_ptrace_getfpxregs = 0; | |
516 | return 0; | |
517 | } | |
518 | ||
519 | perror_with_name ("Couldn't read floating-point and SSE registers"); | |
520 | } | |
5c44784c | 521 | |
6ce2ac0b | 522 | fill_fpxregset (&fpxregs, regno); |
5c44784c | 523 | |
6ce2ac0b MK |
524 | if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1) |
525 | perror_with_name ("Couldn't write floating-point and SSE registers"); | |
5c44784c JM |
526 | |
527 | return 1; | |
528 | } | |
529 | ||
04cd15b6 | 530 | /* Fill the XMM registers in the register array with dummy values. For |
5c44784c JM |
531 | cases where we don't have access to the XMM registers. I think |
532 | this is cleaner than printing a warning. For a cleaner solution, | |
533 | we should gdbarchify the i386 family. */ | |
04cd15b6 | 534 | |
5c44784c | 535 | static void |
04cd15b6 | 536 | dummy_sse_values (void) |
5c44784c JM |
537 | { |
538 | /* C doesn't have a syntax for NaN's, so write it out as an array of | |
539 | longs. */ | |
540 | static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }; | |
541 | static long mxcsr = 0x1f80; | |
542 | int reg; | |
543 | ||
544 | for (reg = 0; reg < 8; reg++) | |
545 | supply_register (XMM0_REGNUM + reg, (char *) dummy); | |
546 | supply_register (MXCSR_REGNUM, (char *) &mxcsr); | |
d4f3574e SS |
547 | } |
548 | ||
5c44784c JM |
549 | #else |
550 | ||
f0373401 MK |
551 | static int fetch_fpxregs (int tid) { return 0; } |
552 | static int store_fpxregs (int tid, int regno) { return 0; } | |
04cd15b6 | 553 | static void dummy_sse_values (void) {} |
5c44784c | 554 | |
6ce2ac0b | 555 | #endif /* HAVE_PTRACE_GETFPXREGS */ |
5c44784c | 556 | \f |
6ce2ac0b | 557 | |
5c44784c | 558 | /* Transferring arbitrary registers between GDB and inferior. */ |
d4f3574e | 559 | |
d5d65353 PS |
560 | /* Check if register REGNO in the child process is accessible. |
561 | If we are accessing registers directly via the U area, only the | |
562 | general-purpose registers are available. | |
563 | All registers should be accessible if we have GETREGS support. */ | |
564 | ||
565 | int | |
566 | cannot_fetch_register (int regno) | |
567 | { | |
568 | if (! have_ptrace_getregs) | |
569 | return OLD_CANNOT_FETCH_REGISTER (regno); | |
570 | return 0; | |
571 | } | |
572 | int | |
573 | cannot_store_register (int regno) | |
574 | { | |
575 | if (! have_ptrace_getregs) | |
576 | return OLD_CANNOT_STORE_REGISTER (regno); | |
577 | return 0; | |
578 | } | |
579 | ||
04cd15b6 MK |
580 | /* Fetch register REGNO from the child process. If REGNO is -1, do |
581 | this for all registers (including the floating point and SSE | |
582 | registers). */ | |
d4f3574e SS |
583 | |
584 | void | |
917317f4 | 585 | fetch_inferior_registers (int regno) |
d4f3574e | 586 | { |
ed9a39eb JM |
587 | int tid; |
588 | ||
f60300e7 MK |
589 | /* Use the old method of peeking around in `struct user' if the |
590 | GETREGS request isn't available. */ | |
591 | if (! have_ptrace_getregs) | |
592 | { | |
593 | old_fetch_inferior_registers (regno); | |
594 | return; | |
595 | } | |
596 | ||
a4b6fc86 | 597 | /* GNU/Linux LWP ID's are process ID's. */ |
39f77062 KB |
598 | if ((tid = TIDGET (inferior_ptid)) == 0) |
599 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ | |
ed9a39eb | 600 | |
6ce2ac0b | 601 | /* Use the PTRACE_GETFPXREGS request whenever possible, since it |
04cd15b6 | 602 | transfers more registers in one system call, and we'll cache the |
6ce2ac0b | 603 | results. But remember that fetch_fpxregs can fail, and return |
04cd15b6 | 604 | zero. */ |
5c44784c JM |
605 | if (regno == -1) |
606 | { | |
ed9a39eb | 607 | fetch_regs (tid); |
f60300e7 MK |
608 | |
609 | /* The call above might reset `have_ptrace_getregs'. */ | |
610 | if (! have_ptrace_getregs) | |
611 | { | |
612 | old_fetch_inferior_registers (-1); | |
613 | return; | |
614 | } | |
615 | ||
6ce2ac0b | 616 | if (fetch_fpxregs (tid)) |
5c44784c | 617 | return; |
ed9a39eb | 618 | fetch_fpregs (tid); |
5c44784c JM |
619 | return; |
620 | } | |
d4f3574e | 621 | |
5c44784c JM |
622 | if (GETREGS_SUPPLIES (regno)) |
623 | { | |
ed9a39eb | 624 | fetch_regs (tid); |
5c44784c JM |
625 | return; |
626 | } | |
627 | ||
6ce2ac0b | 628 | if (GETFPXREGS_SUPPLIES (regno)) |
5c44784c | 629 | { |
6ce2ac0b | 630 | if (fetch_fpxregs (tid)) |
5c44784c JM |
631 | return; |
632 | ||
633 | /* Either our processor or our kernel doesn't support the SSE | |
634 | registers, so read the FP registers in the traditional way, | |
635 | and fill the SSE registers with dummy values. It would be | |
636 | more graceful to handle differences in the register set using | |
637 | gdbarch. Until then, this will at least make things work | |
638 | plausibly. */ | |
ed9a39eb | 639 | fetch_fpregs (tid); |
5c44784c JM |
640 | return; |
641 | } | |
642 | ||
8e65ff28 AC |
643 | internal_error (__FILE__, __LINE__, |
644 | "Got request for bad register number %d.", regno); | |
d4f3574e SS |
645 | } |
646 | ||
04cd15b6 MK |
647 | /* Store register REGNO back into the child process. If REGNO is -1, |
648 | do this for all registers (including the floating point and SSE | |
649 | registers). */ | |
d4f3574e | 650 | void |
04cd15b6 | 651 | store_inferior_registers (int regno) |
d4f3574e | 652 | { |
ed9a39eb JM |
653 | int tid; |
654 | ||
f60300e7 MK |
655 | /* Use the old method of poking around in `struct user' if the |
656 | SETREGS request isn't available. */ | |
657 | if (! have_ptrace_getregs) | |
658 | { | |
659 | old_store_inferior_registers (regno); | |
660 | return; | |
661 | } | |
662 | ||
a4b6fc86 | 663 | /* GNU/Linux LWP ID's are process ID's. */ |
39f77062 KB |
664 | if ((tid = TIDGET (inferior_ptid)) == 0) |
665 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ | |
ed9a39eb | 666 | |
6ce2ac0b | 667 | /* Use the PTRACE_SETFPXREGS requests whenever possible, since it |
04cd15b6 | 668 | transfers more registers in one system call. But remember that |
6ce2ac0b | 669 | store_fpxregs can fail, and return zero. */ |
5c44784c JM |
670 | if (regno == -1) |
671 | { | |
6ce2ac0b MK |
672 | store_regs (tid, regno); |
673 | if (store_fpxregs (tid, regno)) | |
5c44784c | 674 | return; |
6ce2ac0b | 675 | store_fpregs (tid, regno); |
5c44784c JM |
676 | return; |
677 | } | |
d4f3574e | 678 | |
5c44784c JM |
679 | if (GETREGS_SUPPLIES (regno)) |
680 | { | |
6ce2ac0b | 681 | store_regs (tid, regno); |
5c44784c JM |
682 | return; |
683 | } | |
684 | ||
6ce2ac0b | 685 | if (GETFPXREGS_SUPPLIES (regno)) |
5c44784c | 686 | { |
6ce2ac0b | 687 | if (store_fpxregs (tid, regno)) |
5c44784c JM |
688 | return; |
689 | ||
690 | /* Either our processor or our kernel doesn't support the SSE | |
04cd15b6 MK |
691 | registers, so just write the FP registers in the traditional |
692 | way. */ | |
6ce2ac0b | 693 | store_fpregs (tid, regno); |
5c44784c JM |
694 | return; |
695 | } | |
696 | ||
8e65ff28 AC |
697 | internal_error (__FILE__, __LINE__, |
698 | "Got request to store bad register number %d.", regno); | |
d4f3574e | 699 | } |
de57eccd | 700 | \f |
6ce2ac0b | 701 | |
7bf0983e | 702 | static unsigned long |
84346e11 MK |
703 | i386_linux_dr_get (int regnum) |
704 | { | |
705 | int tid; | |
7bf0983e | 706 | unsigned long value; |
84346e11 MK |
707 | |
708 | /* FIXME: kettenis/2001-01-29: It's not clear what we should do with | |
709 | multi-threaded processes here. For now, pretend there is just | |
710 | one thread. */ | |
39f77062 | 711 | tid = PIDGET (inferior_ptid); |
84346e11 | 712 | |
b9511b9a MK |
713 | /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the |
714 | ptrace call fails breaks debugging remote targets. The correct | |
715 | way to fix this is to add the hardware breakpoint and watchpoint | |
716 | stuff to the target vectore. For now, just return zero if the | |
717 | ptrace call fails. */ | |
84346e11 MK |
718 | errno = 0; |
719 | value = ptrace (PT_READ_U, tid, | |
720 | offsetof (struct user, u_debugreg[regnum]), 0); | |
721 | if (errno != 0) | |
b9511b9a | 722 | #if 0 |
84346e11 | 723 | perror_with_name ("Couldn't read debug register"); |
b9511b9a MK |
724 | #else |
725 | return 0; | |
726 | #endif | |
84346e11 MK |
727 | |
728 | return value; | |
729 | } | |
730 | ||
731 | static void | |
7bf0983e | 732 | i386_linux_dr_set (int regnum, unsigned long value) |
84346e11 MK |
733 | { |
734 | int tid; | |
735 | ||
736 | /* FIXME: kettenis/2001-01-29: It's not clear what we should do with | |
737 | multi-threaded processes here. For now, pretend there is just | |
738 | one thread. */ | |
39f77062 | 739 | tid = PIDGET (inferior_ptid); |
84346e11 MK |
740 | |
741 | errno = 0; | |
742 | ptrace (PT_WRITE_U, tid, | |
743 | offsetof (struct user, u_debugreg[regnum]), value); | |
744 | if (errno != 0) | |
745 | perror_with_name ("Couldn't write debug register"); | |
746 | } | |
747 | ||
748 | void | |
7bf0983e | 749 | i386_linux_dr_set_control (unsigned long control) |
84346e11 MK |
750 | { |
751 | i386_linux_dr_set (DR_CONTROL, control); | |
752 | } | |
753 | ||
754 | void | |
755 | i386_linux_dr_set_addr (int regnum, CORE_ADDR addr) | |
756 | { | |
757 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); | |
758 | ||
759 | i386_linux_dr_set (DR_FIRSTADDR + regnum, addr); | |
760 | } | |
761 | ||
762 | void | |
763 | i386_linux_dr_reset_addr (int regnum) | |
764 | { | |
765 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); | |
766 | ||
767 | i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L); | |
768 | } | |
769 | ||
7bf0983e | 770 | unsigned long |
84346e11 MK |
771 | i386_linux_dr_get_status (void) |
772 | { | |
773 | return i386_linux_dr_get (DR_STATUS); | |
774 | } | |
775 | \f | |
776 | ||
de57eccd JM |
777 | /* Interpreting register set info found in core files. */ |
778 | ||
779 | /* Provide registers to GDB from a core file. | |
780 | ||
781 | (We can't use the generic version of this function in | |
a4b6fc86 | 782 | core-regset.c, because GNU/Linux has *three* different kinds of |
de57eccd | 783 | register set notes. core-regset.c would have to call |
6ce2ac0b | 784 | supply_fpxregset, which most platforms don't have.) |
de57eccd JM |
785 | |
786 | CORE_REG_SECT points to an array of bytes, which are the contents | |
787 | of a `note' from a core file which BFD thinks might contain | |
788 | register contents. CORE_REG_SIZE is its size. | |
789 | ||
790 | WHICH says which register set corelow suspects this is: | |
04cd15b6 MK |
791 | 0 --- the general-purpose register set, in elf_gregset_t format |
792 | 2 --- the floating-point register set, in elf_fpregset_t format | |
6ce2ac0b | 793 | 3 --- the extended floating-point register set, in elf_fpxregset_t format |
04cd15b6 | 794 | |
a4b6fc86 | 795 | REG_ADDR isn't used on GNU/Linux. */ |
de57eccd | 796 | |
de57eccd | 797 | static void |
04cd15b6 MK |
798 | fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, |
799 | int which, CORE_ADDR reg_addr) | |
de57eccd | 800 | { |
04cd15b6 MK |
801 | elf_gregset_t gregset; |
802 | elf_fpregset_t fpregset; | |
de57eccd JM |
803 | |
804 | switch (which) | |
805 | { | |
806 | case 0: | |
807 | if (core_reg_size != sizeof (gregset)) | |
04cd15b6 | 808 | warning ("Wrong size gregset in core file."); |
de57eccd JM |
809 | else |
810 | { | |
811 | memcpy (&gregset, core_reg_sect, sizeof (gregset)); | |
812 | supply_gregset (&gregset); | |
813 | } | |
814 | break; | |
815 | ||
816 | case 2: | |
817 | if (core_reg_size != sizeof (fpregset)) | |
04cd15b6 | 818 | warning ("Wrong size fpregset in core file."); |
de57eccd JM |
819 | else |
820 | { | |
821 | memcpy (&fpregset, core_reg_sect, sizeof (fpregset)); | |
822 | supply_fpregset (&fpregset); | |
823 | } | |
824 | break; | |
825 | ||
6ce2ac0b | 826 | #ifdef HAVE_PTRACE_GETFPXREGS |
de57eccd | 827 | { |
6ce2ac0b | 828 | elf_fpxregset_t fpxregset; |
04cd15b6 | 829 | |
de57eccd | 830 | case 3: |
6ce2ac0b MK |
831 | if (core_reg_size != sizeof (fpxregset)) |
832 | warning ("Wrong size fpxregset in core file."); | |
de57eccd JM |
833 | else |
834 | { | |
6ce2ac0b MK |
835 | memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset)); |
836 | supply_fpxregset (&fpxregset); | |
de57eccd JM |
837 | } |
838 | break; | |
839 | } | |
840 | #endif | |
841 | ||
842 | default: | |
843 | /* We've covered all the kinds of registers we know about here, | |
844 | so this must be something we wouldn't know what to do with | |
845 | anyway. Just ignore it. */ | |
846 | break; | |
847 | } | |
848 | } | |
a6abb2c0 | 849 | \f |
6ce2ac0b | 850 | |
a4b6fc86 | 851 | /* The instruction for a GNU/Linux system call is: |
a6abb2c0 MK |
852 | int $0x80 |
853 | or 0xcd 0x80. */ | |
854 | ||
855 | static const unsigned char linux_syscall[] = { 0xcd, 0x80 }; | |
856 | ||
857 | #define LINUX_SYSCALL_LEN (sizeof linux_syscall) | |
858 | ||
859 | /* The system call number is stored in the %eax register. */ | |
860 | #define LINUX_SYSCALL_REGNUM 0 /* %eax */ | |
861 | ||
862 | /* We are specifically interested in the sigreturn and rt_sigreturn | |
863 | system calls. */ | |
864 | ||
865 | #ifndef SYS_sigreturn | |
866 | #define SYS_sigreturn 0x77 | |
867 | #endif | |
868 | #ifndef SYS_rt_sigreturn | |
869 | #define SYS_rt_sigreturn 0xad | |
870 | #endif | |
871 | ||
872 | /* Offset to saved processor flags, from <asm/sigcontext.h>. */ | |
873 | #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64) | |
874 | ||
875 | /* Resume execution of the inferior process. | |
876 | If STEP is nonzero, single-step it. | |
877 | If SIGNAL is nonzero, give it that signal. */ | |
878 | ||
879 | void | |
39f77062 | 880 | child_resume (ptid_t ptid, int step, enum target_signal signal) |
a6abb2c0 | 881 | { |
39f77062 KB |
882 | int pid = PIDGET (ptid); |
883 | ||
a6abb2c0 MK |
884 | int request = PTRACE_CONT; |
885 | ||
886 | if (pid == -1) | |
887 | /* Resume all threads. */ | |
888 | /* I think this only gets used in the non-threaded case, where "resume | |
39f77062 KB |
889 | all threads" and "resume inferior_ptid" are the same. */ |
890 | pid = PIDGET (inferior_ptid); | |
a6abb2c0 MK |
891 | |
892 | if (step) | |
893 | { | |
39f77062 | 894 | CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid)); |
a6abb2c0 MK |
895 | unsigned char buf[LINUX_SYSCALL_LEN]; |
896 | ||
897 | request = PTRACE_SINGLESTEP; | |
898 | ||
899 | /* Returning from a signal trampoline is done by calling a | |
900 | special system call (sigreturn or rt_sigreturn, see | |
901 | i386-linux-tdep.c for more information). This system call | |
902 | restores the registers that were saved when the signal was | |
903 | raised, including %eflags. That means that single-stepping | |
904 | won't work. Instead, we'll have to modify the signal context | |
905 | that's about to be restored, and set the trace flag there. */ | |
906 | ||
907 | /* First check if PC is at a system call. */ | |
908 | if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0 | |
909 | && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0) | |
910 | { | |
39f77062 KB |
911 | int syscall = read_register_pid (LINUX_SYSCALL_REGNUM, |
912 | pid_to_ptid (pid)); | |
a6abb2c0 MK |
913 | |
914 | /* Then check the system call number. */ | |
915 | if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn) | |
916 | { | |
917 | CORE_ADDR sp = read_register (SP_REGNUM); | |
918 | CORE_ADDR addr = sp; | |
919 | unsigned long int eflags; | |
7bf0983e | 920 | |
a6abb2c0 MK |
921 | if (syscall == SYS_rt_sigreturn) |
922 | addr = read_memory_integer (sp + 8, 4) + 20; | |
923 | ||
924 | /* Set the trace flag in the context that's about to be | |
925 | restored. */ | |
926 | addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET; | |
927 | read_memory (addr, (char *) &eflags, 4); | |
928 | eflags |= 0x0100; | |
929 | write_memory (addr, (char *) &eflags, 4); | |
930 | } | |
931 | } | |
932 | } | |
933 | ||
934 | if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1) | |
935 | perror_with_name ("ptrace"); | |
936 | } | |
5c44784c | 937 | \f |
6ce2ac0b | 938 | |
a4b6fc86 AC |
939 | /* Register that we are able to handle GNU/Linux ELF core file |
940 | formats. */ | |
04cd15b6 MK |
941 | |
942 | static struct core_fns linux_elf_core_fns = | |
943 | { | |
944 | bfd_target_elf_flavour, /* core_flavour */ | |
945 | default_check_format, /* check_format */ | |
946 | default_core_sniffer, /* core_sniffer */ | |
947 | fetch_core_registers, /* core_read_registers */ | |
948 | NULL /* next */ | |
949 | }; | |
de57eccd JM |
950 | |
951 | void | |
fba45db2 | 952 | _initialize_i386_linux_nat (void) |
de57eccd | 953 | { |
04cd15b6 | 954 | add_core_fns (&linux_elf_core_fns); |
de57eccd | 955 | } |