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