]>
Commit | Line | Data |
---|---|---|
d4f3574e SS |
1 | /* Native-dependent code for Linux running on i386's, for GDB. |
2 | ||
04cd15b6 | 3 | This file is part of GDB. |
d4f3574e | 4 | |
04cd15b6 MK |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
d4f3574e | 9 | |
04cd15b6 MK |
10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
d4f3574e | 14 | |
04cd15b6 MK |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
d4f3574e SS |
19 | |
20 | #include "defs.h" | |
21 | #include "inferior.h" | |
22 | #include "gdbcore.h" | |
23 | ||
04cd15b6 | 24 | /* For i386_linux_skip_solib_resolver. */ |
d4f3574e SS |
25 | #include "symtab.h" |
26 | #include "frame.h" | |
27 | #include "symfile.h" | |
28 | #include "objfiles.h" | |
29 | ||
30 | #include <sys/ptrace.h> | |
31 | #include <sys/user.h> | |
32 | #include <sys/procfs.h> | |
33 | ||
34 | #ifdef HAVE_SYS_REG_H | |
35 | #include <sys/reg.h> | |
36 | #endif | |
37 | ||
04cd15b6 MK |
38 | /* On Linux, threads are implemented as pseudo-processes, in which |
39 | case we may be tracing more than one process at a time. In that | |
40 | case, inferior_pid will contain the main process ID and the | |
41 | individual thread (process) ID mashed together. These macros are | |
42 | used to separate them out. These definitions should be overridden | |
43 | if thread support is included. */ | |
ed9a39eb JM |
44 | |
45 | #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */ | |
46 | #define PIDGET(PID) PID | |
47 | #define TIDGET(PID) 0 | |
48 | #endif | |
49 | ||
d4f3574e | 50 | |
04cd15b6 MK |
51 | /* The register sets used in Linux ELF core-dumps are identical to the |
52 | register sets in `struct user' that is used for a.out core-dumps, | |
53 | and is also used by `ptrace'. The corresponding types are | |
54 | `elf_gregset_t' for the general-purpose registers (with | |
55 | `elf_greg_t' the type of a single GP register) and `elf_fpregset_t' | |
56 | for the floating-point registers. | |
57 | ||
58 | Those types used to be available under the names `gregset_t' and | |
59 | `fpregset_t' too, and this file used those names in the past. But | |
60 | those names are now used for the register sets used in the | |
61 | `mcontext_t' type, and have a different size and layout. */ | |
62 | ||
63 | /* Mapping between the general-purpose registers in `struct user' | |
64 | format and GDB's register array layout. */ | |
d4f3574e SS |
65 | static int regmap[] = |
66 | { | |
67 | EAX, ECX, EDX, EBX, | |
68 | UESP, EBP, ESI, EDI, | |
69 | EIP, EFL, CS, SS, | |
04cd15b6 | 70 | DS, ES, FS, GS |
d4f3574e SS |
71 | }; |
72 | ||
5c44784c JM |
73 | /* Which ptrace request retrieves which registers? |
74 | These apply to the corresponding SET requests as well. */ | |
75 | #define GETREGS_SUPPLIES(regno) \ | |
76 | (0 <= (regno) && (regno) <= 15) | |
77 | #define GETFPREGS_SUPPLIES(regno) \ | |
78 | (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM) | |
79 | #define GETXFPREGS_SUPPLIES(regno) \ | |
80 | (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM) | |
81 | ||
f60300e7 MK |
82 | /* Does the current host support the GETREGS request? */ |
83 | int have_ptrace_getregs = | |
84 | #ifdef HAVE_PTRACE_GETREGS | |
85 | 1 | |
86 | #else | |
87 | 0 | |
88 | #endif | |
89 | ; | |
90 | ||
5c44784c JM |
91 | /* Does the current host support the GETXFPREGS request? The header |
92 | file may or may not define it, and even if it is defined, the | |
93 | kernel will return EIO if it's running on a pre-SSE processor. | |
94 | ||
c2d11a7d JM |
95 | PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own |
96 | Linux kernel patch for SSE support. That patch may or may not | |
97 | actually make it into the official distribution. If you find that | |
98 | years have gone by since this stuff was added, and Linux isn't | |
99 | using PTRACE_GETXFPREGS, that means that our patch didn't make it, | |
100 | and you can delete this, and the related code. | |
101 | ||
5c44784c JM |
102 | My instinct is to attach this to some architecture- or |
103 | target-specific data structure, but really, a particular GDB | |
104 | process can only run on top of one kernel at a time. So it's okay | |
105 | for this to be a simple variable. */ | |
106 | int have_ptrace_getxfpregs = | |
107 | #ifdef HAVE_PTRACE_GETXFPREGS | |
108 | 1 | |
109 | #else | |
110 | 0 | |
111 | #endif | |
112 | ; | |
113 | ||
f60300e7 MK |
114 | \f |
115 | /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'. | |
116 | The problem is that we define FETCH_INFERIOR_REGISTERS since we | |
117 | want to use our own versions of {fetch,store}_inferior_registers | |
118 | that use the GETREGS request. This means that the code in | |
119 | `infptrace.c' is #ifdef'd out. But we need to fall back on that | |
120 | code when GDB is running on top of a kernel that doesn't support | |
121 | the GETREGS request. I want to avoid changing `infptrace.c' right | |
122 | now. */ | |
123 | ||
124 | /* Default the type of the ptrace transfer to int. */ | |
125 | #ifndef PTRACE_XFER_TYPE | |
126 | #define PTRACE_XFER_TYPE int | |
127 | #endif | |
128 | ||
129 | /* Registers we shouldn't try to fetch. */ | |
130 | #if !defined (CANNOT_FETCH_REGISTER) | |
131 | #define CANNOT_FETCH_REGISTER(regno) 0 | |
132 | #endif | |
133 | ||
134 | /* Fetch one register. */ | |
135 | ||
136 | static void | |
137 | fetch_register (regno) | |
138 | int regno; | |
139 | { | |
140 | /* This isn't really an address. But ptrace thinks of it as one. */ | |
141 | CORE_ADDR regaddr; | |
142 | char mess[128]; /* For messages */ | |
143 | register int i; | |
144 | unsigned int offset; /* Offset of registers within the u area. */ | |
145 | char buf[MAX_REGISTER_RAW_SIZE]; | |
146 | int tid; | |
147 | ||
148 | if (CANNOT_FETCH_REGISTER (regno)) | |
149 | { | |
150 | memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */ | |
151 | supply_register (regno, buf); | |
152 | return; | |
153 | } | |
154 | ||
155 | /* Overload thread id onto process id */ | |
156 | if ((tid = TIDGET (inferior_pid)) == 0) | |
157 | tid = inferior_pid; /* no thread id, just use process id */ | |
158 | ||
159 | offset = U_REGS_OFFSET; | |
160 | ||
161 | regaddr = register_addr (regno, offset); | |
162 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) | |
163 | { | |
164 | errno = 0; | |
165 | *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid, | |
166 | (PTRACE_ARG3_TYPE) regaddr, 0); | |
167 | regaddr += sizeof (PTRACE_XFER_TYPE); | |
168 | if (errno != 0) | |
169 | { | |
170 | sprintf (mess, "reading register %s (#%d)", | |
171 | REGISTER_NAME (regno), regno); | |
172 | perror_with_name (mess); | |
173 | } | |
174 | } | |
175 | supply_register (regno, buf); | |
176 | } | |
177 | ||
178 | /* Fetch register values from the inferior. | |
179 | If REGNO is negative, do this for all registers. | |
180 | Otherwise, REGNO specifies which register (so we can save time). */ | |
181 | ||
182 | void | |
183 | old_fetch_inferior_registers (regno) | |
184 | int regno; | |
185 | { | |
186 | if (regno >= 0) | |
187 | { | |
188 | fetch_register (regno); | |
189 | } | |
190 | else | |
191 | { | |
192 | for (regno = 0; regno < ARCH_NUM_REGS; regno++) | |
193 | { | |
194 | fetch_register (regno); | |
195 | } | |
196 | } | |
197 | } | |
198 | ||
199 | /* Registers we shouldn't try to store. */ | |
200 | #if !defined (CANNOT_STORE_REGISTER) | |
201 | #define CANNOT_STORE_REGISTER(regno) 0 | |
202 | #endif | |
203 | ||
204 | /* Store one register. */ | |
205 | ||
206 | static void | |
207 | store_register (regno) | |
208 | int regno; | |
209 | { | |
210 | /* This isn't really an address. But ptrace thinks of it as one. */ | |
211 | CORE_ADDR regaddr; | |
212 | char mess[128]; /* For messages */ | |
213 | register int i; | |
214 | unsigned int offset; /* Offset of registers within the u area. */ | |
215 | int tid; | |
216 | ||
217 | if (CANNOT_STORE_REGISTER (regno)) | |
218 | { | |
219 | return; | |
220 | } | |
221 | ||
222 | /* Overload thread id onto process id */ | |
223 | if ((tid = TIDGET (inferior_pid)) == 0) | |
224 | tid = inferior_pid; /* no thread id, just use process id */ | |
225 | ||
226 | offset = U_REGS_OFFSET; | |
227 | ||
228 | regaddr = register_addr (regno, offset); | |
229 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) | |
230 | { | |
231 | errno = 0; | |
232 | ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr, | |
233 | *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]); | |
234 | regaddr += sizeof (PTRACE_XFER_TYPE); | |
235 | if (errno != 0) | |
236 | { | |
237 | sprintf (mess, "writing register %s (#%d)", | |
238 | REGISTER_NAME (regno), regno); | |
239 | perror_with_name (mess); | |
240 | } | |
241 | } | |
242 | } | |
243 | ||
244 | /* Store our register values back into the inferior. | |
245 | If REGNO is negative, do this for all registers. | |
246 | Otherwise, REGNO specifies which register (so we can save time). */ | |
247 | ||
248 | void | |
249 | old_store_inferior_registers (regno) | |
250 | int regno; | |
251 | { | |
252 | if (regno >= 0) | |
253 | { | |
254 | store_register (regno); | |
255 | } | |
256 | else | |
257 | { | |
258 | for (regno = 0; regno < ARCH_NUM_REGS; regno++) | |
259 | { | |
260 | store_register (regno); | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
5c44784c | 265 | \f |
04cd15b6 MK |
266 | /* Transfering the general-purpose registers between GDB, inferiors |
267 | and core files. */ | |
268 | ||
269 | /* Fill GDB's register array with the genereal-purpose register values | |
270 | in *GREGSETP. */ | |
5c44784c | 271 | |
d4f3574e | 272 | void |
04cd15b6 | 273 | supply_gregset (elf_gregset_t *gregsetp) |
d4f3574e | 274 | { |
04cd15b6 MK |
275 | elf_greg_t *regp = (elf_greg_t *) gregsetp; |
276 | int regi; | |
d4f3574e | 277 | |
917317f4 | 278 | for (regi = 0; regi < NUM_GREGS; regi++) |
04cd15b6 | 279 | supply_register (regi, (char *) (regp + regmap[regi])); |
d4f3574e SS |
280 | } |
281 | ||
04cd15b6 MK |
282 | /* Convert the valid general-purpose register values in GDB's register |
283 | array to `struct user' format and store them in *GREGSETP. The | |
284 | array VALID indicates which register values are valid. If VALID is | |
285 | NULL, all registers are assumed to be valid. */ | |
5c44784c | 286 | |
04cd15b6 MK |
287 | static void |
288 | convert_to_gregset (elf_gregset_t *gregsetp, signed char *valid) | |
d4f3574e | 289 | { |
04cd15b6 | 290 | elf_greg_t *regp = (elf_greg_t *) gregsetp; |
d4f3574e | 291 | int regi; |
d4f3574e | 292 | |
917317f4 JM |
293 | for (regi = 0; regi < NUM_GREGS; regi++) |
294 | if (! valid || valid[regi]) | |
295 | *(regp + regmap[regi]) = * (int *) ®isters[REGISTER_BYTE (regi)]; | |
296 | } | |
297 | ||
04cd15b6 MK |
298 | /* Fill register REGNO (if it is a general-purpose register) in |
299 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, | |
300 | do this for all registers. */ | |
917317f4 | 301 | void |
04cd15b6 | 302 | fill_gregset (elf_gregset_t *gregsetp, int regno) |
917317f4 JM |
303 | { |
304 | if (regno == -1) | |
04cd15b6 MK |
305 | { |
306 | convert_to_gregset (gregsetp, NULL); | |
307 | return; | |
308 | } | |
309 | ||
310 | if (GETREGS_SUPPLIES (regno)) | |
d4f3574e | 311 | { |
917317f4 | 312 | signed char valid[NUM_GREGS]; |
04cd15b6 | 313 | |
917317f4 JM |
314 | memset (valid, 0, sizeof (valid)); |
315 | valid[regno] = 1; | |
04cd15b6 MK |
316 | |
317 | convert_to_gregset (gregsetp, valid); | |
d4f3574e SS |
318 | } |
319 | } | |
320 | ||
f60300e7 MK |
321 | #ifdef HAVE_PTRACE_GETREGS |
322 | ||
04cd15b6 MK |
323 | /* Fetch all general-purpose registers from process/thread TID and |
324 | store their values in GDB's register array. */ | |
d4f3574e | 325 | |
5c44784c | 326 | static void |
ed9a39eb | 327 | fetch_regs (int tid) |
5c44784c | 328 | { |
04cd15b6 MK |
329 | elf_gregset_t regs; |
330 | int ret; | |
5c44784c | 331 | |
04cd15b6 | 332 | ret = ptrace (PTRACE_GETREGS, tid, 0, (int) ®s); |
5c44784c JM |
333 | if (ret < 0) |
334 | { | |
f60300e7 MK |
335 | if (errno == EIO) |
336 | { | |
337 | /* The kernel we're running on doesn't support the GETREGS | |
338 | request. Reset `have_ptrace_getregs'. */ | |
339 | have_ptrace_getregs = 0; | |
340 | return; | |
341 | } | |
342 | ||
04cd15b6 | 343 | warning ("Couldn't get registers."); |
5c44784c JM |
344 | return; |
345 | } | |
346 | ||
04cd15b6 | 347 | supply_gregset (®s); |
5c44784c JM |
348 | } |
349 | ||
04cd15b6 MK |
350 | /* Store all valid general-purpose registers in GDB's register array |
351 | into the process/thread specified by TID. */ | |
5c44784c | 352 | |
5c44784c | 353 | static void |
ed9a39eb | 354 | store_regs (int tid) |
5c44784c | 355 | { |
04cd15b6 MK |
356 | elf_gregset_t regs; |
357 | int ret; | |
5c44784c | 358 | |
04cd15b6 | 359 | ret = ptrace (PTRACE_GETREGS, tid, 0, (int) ®s); |
5c44784c JM |
360 | if (ret < 0) |
361 | { | |
04cd15b6 | 362 | warning ("Couldn't get registers."); |
5c44784c JM |
363 | return; |
364 | } | |
365 | ||
04cd15b6 | 366 | convert_to_gregset (®s, register_valid); |
5c44784c | 367 | |
04cd15b6 | 368 | ret = ptrace (PTRACE_SETREGS, tid, 0, (int) ®s); |
5c44784c JM |
369 | if (ret < 0) |
370 | { | |
04cd15b6 | 371 | warning ("Couldn't write registers."); |
5c44784c JM |
372 | return; |
373 | } | |
374 | } | |
375 | ||
f60300e7 MK |
376 | #else |
377 | ||
378 | static void fetch_regs (int tid) {} | |
379 | static void store_regs (int tid) {} | |
380 | ||
381 | #endif | |
382 | ||
5c44784c JM |
383 | \f |
384 | /* Transfering floating-point registers between GDB, inferiors and cores. */ | |
385 | ||
04cd15b6 MK |
386 | /* What is the address of st(N) within the floating-point register set F? */ |
387 | #define FPREG_ADDR(f, n) ((char *) &(f)->st_space + (n) * 10) | |
d4f3574e | 388 | |
04cd15b6 | 389 | /* Fill GDB's register array with the floating-point register values in |
917317f4 | 390 | *FPREGSETP. */ |
04cd15b6 | 391 | |
d4f3574e | 392 | void |
04cd15b6 | 393 | supply_fpregset (elf_fpregset_t *fpregsetp) |
d4f3574e | 394 | { |
04cd15b6 | 395 | int reg; |
b948cda9 | 396 | long l; |
917317f4 JM |
397 | |
398 | /* Supply the floating-point registers. */ | |
04cd15b6 MK |
399 | for (reg = 0; reg < 8; reg++) |
400 | supply_register (FP0_REGNUM + reg, FPREG_ADDR (fpregsetp, reg)); | |
917317f4 | 401 | |
b948cda9 MK |
402 | /* We have to mask off the reserved bits in *FPREGSETP before |
403 | storing the values in GDB's register file. */ | |
404 | #define supply(REGNO, MEMBER) \ | |
405 | l = fpregsetp->MEMBER & 0xffff; \ | |
406 | supply_register (REGNO, (char *) &l) | |
407 | ||
408 | supply (FCTRL_REGNUM, cwd); | |
409 | supply (FSTAT_REGNUM, swd); | |
410 | supply (FTAG_REGNUM, twd); | |
917317f4 | 411 | supply_register (FCOFF_REGNUM, (char *) &fpregsetp->fip); |
b948cda9 | 412 | supply (FDS_REGNUM, fos); |
917317f4 | 413 | supply_register (FDOFF_REGNUM, (char *) &fpregsetp->foo); |
917317f4 | 414 | |
b948cda9 MK |
415 | #undef supply |
416 | ||
417 | /* Extract the code segment and opcode from the "fcs" member. */ | |
418 | l = fpregsetp->fcs & 0xffff; | |
419 | supply_register (FCS_REGNUM, (char *) &l); | |
917317f4 | 420 | |
b948cda9 MK |
421 | l = (fpregsetp->fcs >> 16) & ((1 << 11) - 1); |
422 | supply_register (FOP_REGNUM, (char *) &l); | |
d4f3574e SS |
423 | } |
424 | ||
04cd15b6 MK |
425 | /* Convert the valid floating-point register values in GDB's register |
426 | array to `struct user' format and store them in *FPREGSETP. The | |
427 | array VALID indicates which register values are valid. If VALID is | |
428 | NULL, all registers are assumed to be valid. */ | |
d4f3574e | 429 | |
04cd15b6 MK |
430 | static void |
431 | convert_to_fpregset (elf_fpregset_t *fpregsetp, signed char *valid) | |
d4f3574e | 432 | { |
04cd15b6 | 433 | int reg; |
917317f4 JM |
434 | |
435 | /* Fill in the floating-point registers. */ | |
04cd15b6 MK |
436 | for (reg = 0; reg < 8; reg++) |
437 | if (!valid || valid[reg]) | |
438 | memcpy (FPREG_ADDR (fpregsetp, reg), | |
439 | ®isters[REGISTER_BYTE (FP0_REGNUM + reg)], | |
440 | REGISTER_RAW_SIZE(FP0_REGNUM + reg)); | |
917317f4 | 441 | |
b948cda9 MK |
442 | /* We're not supposed to touch the reserved bits in *FPREGSETP. */ |
443 | ||
917317f4 JM |
444 | #define fill(MEMBER, REGNO) \ |
445 | if (! valid || valid[(REGNO)]) \ | |
b948cda9 MK |
446 | fpregsetp->MEMBER \ |
447 | = ((fpregsetp->MEMBER & ~0xffff) \ | |
448 | | (* (int *) ®isters[REGISTER_BYTE (REGNO)] & 0xffff)) | |
449 | ||
450 | #define fill_register(MEMBER, REGNO) \ | |
451 | if (! valid || valid[(REGNO)]) \ | |
452 | memcpy (&fpregsetp->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \ | |
453 | sizeof (fpregsetp->MEMBER)) | |
917317f4 JM |
454 | |
455 | fill (cwd, FCTRL_REGNUM); | |
456 | fill (swd, FSTAT_REGNUM); | |
457 | fill (twd, FTAG_REGNUM); | |
b948cda9 | 458 | fill_register (fip, FCOFF_REGNUM); |
917317f4 | 459 | fill (foo, FDOFF_REGNUM); |
b948cda9 | 460 | fill_register (fos, FDS_REGNUM); |
917317f4 JM |
461 | |
462 | #undef fill | |
b948cda9 | 463 | #undef fill_register |
917317f4 JM |
464 | |
465 | if (! valid || valid[FCS_REGNUM]) | |
466 | fpregsetp->fcs | |
467 | = ((fpregsetp->fcs & ~0xffff) | |
468 | | (* (int *) ®isters[REGISTER_BYTE (FCS_REGNUM)] & 0xffff)); | |
469 | ||
470 | if (! valid || valid[FOP_REGNUM]) | |
471 | fpregsetp->fcs | |
472 | = ((fpregsetp->fcs & 0xffff) | |
473 | | ((*(int *) ®isters[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1)) | |
474 | << 16)); | |
475 | } | |
d4f3574e | 476 | |
04cd15b6 MK |
477 | /* Fill register REGNO (if it is a floating-point register) in |
478 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, | |
479 | do this for all registers. */ | |
917317f4 JM |
480 | |
481 | void | |
04cd15b6 | 482 | fill_fpregset (elf_fpregset_t *fpregsetp, int regno) |
917317f4 | 483 | { |
04cd15b6 MK |
484 | if (regno == -1) |
485 | { | |
486 | convert_to_fpregset (fpregsetp, NULL); | |
487 | return; | |
488 | } | |
489 | ||
490 | if (GETFPREGS_SUPPLIES(regno)) | |
491 | { | |
492 | signed char valid[MAX_NUM_REGS]; | |
493 | ||
494 | memset (valid, 0, sizeof (valid)); | |
495 | valid[regno] = 1; | |
496 | ||
497 | convert_to_fpregset (fpregsetp, valid); | |
498 | } | |
d4f3574e SS |
499 | } |
500 | ||
f60300e7 MK |
501 | #ifdef HAVE_PTRACE_GETREGS |
502 | ||
04cd15b6 MK |
503 | /* Fetch all floating-point registers from process/thread TID and store |
504 | thier values in GDB's register array. */ | |
917317f4 | 505 | |
d4f3574e | 506 | static void |
ed9a39eb | 507 | fetch_fpregs (int tid) |
d4f3574e | 508 | { |
04cd15b6 MK |
509 | elf_fpregset_t fpregs; |
510 | int ret; | |
d4f3574e | 511 | |
04cd15b6 | 512 | ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs); |
917317f4 | 513 | if (ret < 0) |
d4f3574e | 514 | { |
04cd15b6 | 515 | warning ("Couldn't get floating point status."); |
d4f3574e SS |
516 | return; |
517 | } | |
518 | ||
04cd15b6 | 519 | supply_fpregset (&fpregs); |
d4f3574e SS |
520 | } |
521 | ||
04cd15b6 MK |
522 | /* Store all valid floating-point registers in GDB's register array |
523 | into the process/thread specified by TID. */ | |
d4f3574e | 524 | |
d4f3574e | 525 | static void |
ed9a39eb | 526 | store_fpregs (int tid) |
d4f3574e | 527 | { |
04cd15b6 | 528 | elf_fpregset_t fpregs; |
917317f4 | 529 | int ret; |
d4f3574e | 530 | |
04cd15b6 | 531 | ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs); |
917317f4 | 532 | if (ret < 0) |
d4f3574e | 533 | { |
04cd15b6 | 534 | warning ("Couldn't get floating point status."); |
d4f3574e SS |
535 | return; |
536 | } | |
537 | ||
04cd15b6 | 538 | convert_to_fpregset (&fpregs, register_valid); |
d4f3574e | 539 | |
04cd15b6 | 540 | ret = ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs); |
917317f4 | 541 | if (ret < 0) |
d4f3574e | 542 | { |
04cd15b6 | 543 | warning ("Couldn't write floating point status."); |
d4f3574e SS |
544 | return; |
545 | } | |
d4f3574e SS |
546 | } |
547 | ||
f60300e7 MK |
548 | #else |
549 | ||
550 | static void fetch_fpregs (int tid) {} | |
551 | static void store_fpregs (int tid) {} | |
552 | ||
553 | #endif | |
554 | ||
5c44784c JM |
555 | \f |
556 | /* Transfering floating-point and SSE registers to and from GDB. */ | |
d4f3574e | 557 | |
11cf8741 JM |
558 | /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own |
559 | Linux kernel patch for SSE support. That patch may or may not | |
560 | actually make it into the official distribution. If you find that | |
561 | years have gone by since this code was added, and Linux isn't using | |
562 | PTRACE_GETXFPREGS, that means that our patch didn't make it, and | |
563 | you can delete this code. */ | |
564 | ||
5c44784c | 565 | #ifdef HAVE_PTRACE_GETXFPREGS |
04cd15b6 MK |
566 | |
567 | /* Fill GDB's register array with the floating-point and SSE register | |
568 | values in *XFPREGS. */ | |
569 | ||
d4f3574e | 570 | static void |
5c44784c | 571 | supply_xfpregset (struct user_xfpregs_struct *xfpregs) |
d4f3574e | 572 | { |
5c44784c | 573 | int reg; |
d4f3574e | 574 | |
5c44784c JM |
575 | /* Supply the floating-point registers. */ |
576 | for (reg = 0; reg < 8; reg++) | |
577 | supply_register (FP0_REGNUM + reg, (char *) &xfpregs->st_space[reg]); | |
578 | ||
579 | { | |
580 | supply_register (FCTRL_REGNUM, (char *) &xfpregs->cwd); | |
581 | supply_register (FSTAT_REGNUM, (char *) &xfpregs->swd); | |
582 | supply_register (FTAG_REGNUM, (char *) &xfpregs->twd); | |
583 | supply_register (FCOFF_REGNUM, (char *) &xfpregs->fip); | |
584 | supply_register (FDS_REGNUM, (char *) &xfpregs->fos); | |
585 | supply_register (FDOFF_REGNUM, (char *) &xfpregs->foo); | |
586 | ||
587 | /* Extract the code segment and opcode from the "fcs" member. */ | |
d4f3574e | 588 | { |
5c44784c JM |
589 | long l; |
590 | ||
591 | l = xfpregs->fcs & 0xffff; | |
592 | supply_register (FCS_REGNUM, (char *) &l); | |
593 | ||
594 | l = (xfpregs->fcs >> 16) & ((1 << 11) - 1); | |
595 | supply_register (FOP_REGNUM, (char *) &l); | |
d4f3574e | 596 | } |
5c44784c | 597 | } |
d4f3574e | 598 | |
5c44784c JM |
599 | /* Supply the SSE registers. */ |
600 | for (reg = 0; reg < 8; reg++) | |
601 | supply_register (XMM0_REGNUM + reg, (char *) &xfpregs->xmm_space[reg]); | |
602 | supply_register (MXCSR_REGNUM, (char *) &xfpregs->mxcsr); | |
d4f3574e SS |
603 | } |
604 | ||
04cd15b6 MK |
605 | /* Convert the valid floating-point and SSE registers in GDB's |
606 | register array to `struct user' format and store them in *XFPREGS. | |
607 | The array VALID indicates which registers are valid. If VALID is | |
608 | NULL, all registers are assumed to be valid. */ | |
d4f3574e | 609 | |
d4f3574e | 610 | static void |
5c44784c | 611 | convert_to_xfpregset (struct user_xfpregs_struct *xfpregs, |
5c44784c | 612 | signed char *valid) |
d4f3574e | 613 | { |
5c44784c | 614 | int reg; |
d4f3574e | 615 | |
5c44784c JM |
616 | /* Fill in the floating-point registers. */ |
617 | for (reg = 0; reg < 8; reg++) | |
618 | if (!valid || valid[reg]) | |
619 | memcpy (&xfpregs->st_space[reg], | |
620 | ®isters[REGISTER_BYTE (FP0_REGNUM + reg)], | |
621 | REGISTER_RAW_SIZE(FP0_REGNUM + reg)); | |
622 | ||
623 | #define fill(MEMBER, REGNO) \ | |
624 | if (! valid || valid[(REGNO)]) \ | |
625 | memcpy (&xfpregs->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \ | |
626 | sizeof (xfpregs->MEMBER)) | |
627 | ||
628 | fill (cwd, FCTRL_REGNUM); | |
629 | fill (swd, FSTAT_REGNUM); | |
630 | fill (twd, FTAG_REGNUM); | |
631 | fill (fip, FCOFF_REGNUM); | |
632 | fill (foo, FDOFF_REGNUM); | |
633 | fill (fos, FDS_REGNUM); | |
634 | ||
635 | #undef fill | |
636 | ||
637 | if (! valid || valid[FCS_REGNUM]) | |
638 | xfpregs->fcs | |
639 | = ((xfpregs->fcs & ~0xffff) | |
640 | | (* (int *) ®isters[REGISTER_BYTE (FCS_REGNUM)] & 0xffff)); | |
641 | ||
642 | if (! valid || valid[FOP_REGNUM]) | |
643 | xfpregs->fcs | |
644 | = ((xfpregs->fcs & 0xffff) | |
645 | | ((*(int *) ®isters[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1)) | |
646 | << 16)); | |
647 | ||
648 | /* Fill in the XMM registers. */ | |
649 | for (reg = 0; reg < 8; reg++) | |
650 | if (! valid || valid[reg]) | |
651 | memcpy (&xfpregs->xmm_space[reg], | |
652 | ®isters[REGISTER_BYTE (XMM0_REGNUM + reg)], | |
653 | REGISTER_RAW_SIZE (XMM0_REGNUM + reg)); | |
654 | } | |
655 | ||
04cd15b6 MK |
656 | /* Fetch all registers covered by the PTRACE_SETXFPREGS request from |
657 | process/thread TID and store their values in GDB's register array. | |
658 | Return non-zero if successful, zero otherwise. */ | |
5c44784c | 659 | |
5c44784c | 660 | static int |
ed9a39eb | 661 | fetch_xfpregs (int tid) |
5c44784c | 662 | { |
5c44784c | 663 | struct user_xfpregs_struct xfpregs; |
04cd15b6 | 664 | int ret; |
5c44784c JM |
665 | |
666 | if (! have_ptrace_getxfpregs) | |
667 | return 0; | |
668 | ||
ed9a39eb | 669 | ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs); |
5c44784c | 670 | if (ret == -1) |
d4f3574e | 671 | { |
5c44784c JM |
672 | if (errno == EIO) |
673 | { | |
674 | have_ptrace_getxfpregs = 0; | |
675 | return 0; | |
676 | } | |
677 | ||
04cd15b6 | 678 | warning ("Couldn't read floating-point and SSE registers."); |
5c44784c | 679 | return 0; |
d4f3574e SS |
680 | } |
681 | ||
5c44784c JM |
682 | supply_xfpregset (&xfpregs); |
683 | return 1; | |
684 | } | |
d4f3574e | 685 | |
04cd15b6 MK |
686 | /* Store all valid registers in GDB's register array covered by the |
687 | PTRACE_SETXFPREGS request into the process/thread specified by TID. | |
688 | Return non-zero if successful, zero otherwise. */ | |
5c44784c | 689 | |
5c44784c | 690 | static int |
ed9a39eb | 691 | store_xfpregs (int tid) |
5c44784c | 692 | { |
5c44784c | 693 | struct user_xfpregs_struct xfpregs; |
04cd15b6 | 694 | int ret; |
5c44784c JM |
695 | |
696 | if (! have_ptrace_getxfpregs) | |
697 | return 0; | |
698 | ||
ed9a39eb | 699 | ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs); |
5c44784c | 700 | if (ret == -1) |
d4f3574e | 701 | { |
5c44784c JM |
702 | if (errno == EIO) |
703 | { | |
704 | have_ptrace_getxfpregs = 0; | |
705 | return 0; | |
706 | } | |
707 | ||
04cd15b6 | 708 | warning ("Couldn't read floating-point and SSE registers."); |
5c44784c JM |
709 | return 0; |
710 | } | |
711 | ||
04cd15b6 | 712 | convert_to_xfpregset (&xfpregs, register_valid); |
5c44784c | 713 | |
ed9a39eb | 714 | if (ptrace (PTRACE_SETXFPREGS, tid, 0, &xfpregs) < 0) |
5c44784c JM |
715 | { |
716 | warning ("Couldn't write floating-point and SSE registers."); | |
717 | return 0; | |
d4f3574e | 718 | } |
5c44784c JM |
719 | |
720 | return 1; | |
721 | } | |
722 | ||
04cd15b6 | 723 | /* Fill the XMM registers in the register array with dummy values. For |
5c44784c JM |
724 | cases where we don't have access to the XMM registers. I think |
725 | this is cleaner than printing a warning. For a cleaner solution, | |
726 | we should gdbarchify the i386 family. */ | |
04cd15b6 | 727 | |
5c44784c | 728 | static void |
04cd15b6 | 729 | dummy_sse_values (void) |
5c44784c JM |
730 | { |
731 | /* C doesn't have a syntax for NaN's, so write it out as an array of | |
732 | longs. */ | |
733 | static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }; | |
734 | static long mxcsr = 0x1f80; | |
735 | int reg; | |
736 | ||
737 | for (reg = 0; reg < 8; reg++) | |
738 | supply_register (XMM0_REGNUM + reg, (char *) dummy); | |
739 | supply_register (MXCSR_REGNUM, (char *) &mxcsr); | |
d4f3574e SS |
740 | } |
741 | ||
5c44784c JM |
742 | #else |
743 | ||
744 | /* Stub versions of the above routines, for systems that don't have | |
745 | PTRACE_GETXFPREGS. */ | |
ed9a39eb JM |
746 | static int store_xfpregs (int tid) { return 0; } |
747 | static int fetch_xfpregs (int tid) { return 0; } | |
04cd15b6 | 748 | static void dummy_sse_values (void) {} |
5c44784c JM |
749 | |
750 | #endif | |
751 | ||
752 | \f | |
753 | /* Transferring arbitrary registers between GDB and inferior. */ | |
d4f3574e | 754 | |
04cd15b6 MK |
755 | /* Fetch register REGNO from the child process. If REGNO is -1, do |
756 | this for all registers (including the floating point and SSE | |
757 | registers). */ | |
d4f3574e SS |
758 | |
759 | void | |
917317f4 | 760 | fetch_inferior_registers (int regno) |
d4f3574e | 761 | { |
ed9a39eb JM |
762 | int tid; |
763 | ||
f60300e7 MK |
764 | /* Use the old method of peeking around in `struct user' if the |
765 | GETREGS request isn't available. */ | |
766 | if (! have_ptrace_getregs) | |
767 | { | |
768 | old_fetch_inferior_registers (regno); | |
769 | return; | |
770 | } | |
771 | ||
04cd15b6 | 772 | /* Linux LWP ID's are process ID's. */ |
ed9a39eb | 773 | if ((tid = TIDGET (inferior_pid)) == 0) |
04cd15b6 | 774 | tid = inferior_pid; /* Not a threaded program. */ |
ed9a39eb | 775 | |
04cd15b6 MK |
776 | /* Use the PTRACE_GETXFPREGS request whenever possible, since it |
777 | transfers more registers in one system call, and we'll cache the | |
778 | results. But remember that fetch_xfpregs can fail, and return | |
779 | zero. */ | |
5c44784c JM |
780 | if (regno == -1) |
781 | { | |
ed9a39eb | 782 | fetch_regs (tid); |
f60300e7 MK |
783 | |
784 | /* The call above might reset `have_ptrace_getregs'. */ | |
785 | if (! have_ptrace_getregs) | |
786 | { | |
787 | old_fetch_inferior_registers (-1); | |
788 | return; | |
789 | } | |
790 | ||
ed9a39eb | 791 | if (fetch_xfpregs (tid)) |
5c44784c | 792 | return; |
ed9a39eb | 793 | fetch_fpregs (tid); |
5c44784c JM |
794 | return; |
795 | } | |
d4f3574e | 796 | |
5c44784c JM |
797 | if (GETREGS_SUPPLIES (regno)) |
798 | { | |
ed9a39eb | 799 | fetch_regs (tid); |
5c44784c JM |
800 | return; |
801 | } | |
802 | ||
803 | if (GETXFPREGS_SUPPLIES (regno)) | |
804 | { | |
ed9a39eb | 805 | if (fetch_xfpregs (tid)) |
5c44784c JM |
806 | return; |
807 | ||
808 | /* Either our processor or our kernel doesn't support the SSE | |
809 | registers, so read the FP registers in the traditional way, | |
810 | and fill the SSE registers with dummy values. It would be | |
811 | more graceful to handle differences in the register set using | |
812 | gdbarch. Until then, this will at least make things work | |
813 | plausibly. */ | |
ed9a39eb | 814 | fetch_fpregs (tid); |
5c44784c JM |
815 | dummy_sse_values (); |
816 | return; | |
817 | } | |
818 | ||
819 | internal_error ("i386-linux-nat.c (fetch_inferior_registers): " | |
820 | "got request for bad register number %d", regno); | |
d4f3574e SS |
821 | } |
822 | ||
04cd15b6 MK |
823 | /* Store register REGNO back into the child process. If REGNO is -1, |
824 | do this for all registers (including the floating point and SSE | |
825 | registers). */ | |
d4f3574e | 826 | void |
04cd15b6 | 827 | store_inferior_registers (int regno) |
d4f3574e | 828 | { |
ed9a39eb JM |
829 | int tid; |
830 | ||
f60300e7 MK |
831 | /* Use the old method of poking around in `struct user' if the |
832 | SETREGS request isn't available. */ | |
833 | if (! have_ptrace_getregs) | |
834 | { | |
835 | old_store_inferior_registers (regno); | |
836 | return; | |
837 | } | |
838 | ||
04cd15b6 | 839 | /* Linux LWP ID's are process ID's. */ |
ed9a39eb | 840 | if ((tid = TIDGET (inferior_pid)) == 0) |
04cd15b6 | 841 | tid = inferior_pid; /* Not a threaded program. */ |
ed9a39eb | 842 | |
04cd15b6 MK |
843 | /* Use the PTRACE_SETXFPREGS requests whenever possibl, since it |
844 | transfers more registers in one system call. But remember that | |
ed9a39eb | 845 | store_xfpregs can fail, and return zero. */ |
5c44784c JM |
846 | if (regno == -1) |
847 | { | |
ed9a39eb JM |
848 | store_regs (tid); |
849 | if (store_xfpregs (tid)) | |
5c44784c | 850 | return; |
ed9a39eb | 851 | store_fpregs (tid); |
5c44784c JM |
852 | return; |
853 | } | |
d4f3574e | 854 | |
5c44784c JM |
855 | if (GETREGS_SUPPLIES (regno)) |
856 | { | |
ed9a39eb | 857 | store_regs (tid); |
5c44784c JM |
858 | return; |
859 | } | |
860 | ||
861 | if (GETXFPREGS_SUPPLIES (regno)) | |
862 | { | |
ed9a39eb | 863 | if (store_xfpregs (tid)) |
5c44784c JM |
864 | return; |
865 | ||
866 | /* Either our processor or our kernel doesn't support the SSE | |
04cd15b6 MK |
867 | registers, so just write the FP registers in the traditional |
868 | way. */ | |
ed9a39eb | 869 | store_fpregs (tid); |
5c44784c JM |
870 | return; |
871 | } | |
872 | ||
04cd15b6 | 873 | internal_error ("Got request to store bad register number %d.", regno); |
d4f3574e SS |
874 | } |
875 | ||
de57eccd JM |
876 | \f |
877 | /* Interpreting register set info found in core files. */ | |
878 | ||
879 | /* Provide registers to GDB from a core file. | |
880 | ||
881 | (We can't use the generic version of this function in | |
882 | core-regset.c, because Linux has *three* different kinds of | |
883 | register set notes. core-regset.c would have to call | |
884 | supply_xfpregset, which most platforms don't have.) | |
885 | ||
886 | CORE_REG_SECT points to an array of bytes, which are the contents | |
887 | of a `note' from a core file which BFD thinks might contain | |
888 | register contents. CORE_REG_SIZE is its size. | |
889 | ||
890 | WHICH says which register set corelow suspects this is: | |
04cd15b6 MK |
891 | 0 --- the general-purpose register set, in elf_gregset_t format |
892 | 2 --- the floating-point register set, in elf_fpregset_t format | |
893 | 3 --- the extended floating-point register set, in struct | |
894 | user_xfpregs_struct format | |
895 | ||
896 | REG_ADDR isn't used on Linux. */ | |
de57eccd | 897 | |
de57eccd | 898 | static void |
04cd15b6 MK |
899 | fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, |
900 | int which, CORE_ADDR reg_addr) | |
de57eccd | 901 | { |
04cd15b6 MK |
902 | elf_gregset_t gregset; |
903 | elf_fpregset_t fpregset; | |
de57eccd JM |
904 | |
905 | switch (which) | |
906 | { | |
907 | case 0: | |
908 | if (core_reg_size != sizeof (gregset)) | |
04cd15b6 | 909 | warning ("Wrong size gregset in core file."); |
de57eccd JM |
910 | else |
911 | { | |
912 | memcpy (&gregset, core_reg_sect, sizeof (gregset)); | |
913 | supply_gregset (&gregset); | |
914 | } | |
915 | break; | |
916 | ||
917 | case 2: | |
918 | if (core_reg_size != sizeof (fpregset)) | |
04cd15b6 | 919 | warning ("Wrong size fpregset in core file."); |
de57eccd JM |
920 | else |
921 | { | |
922 | memcpy (&fpregset, core_reg_sect, sizeof (fpregset)); | |
923 | supply_fpregset (&fpregset); | |
924 | } | |
925 | break; | |
926 | ||
927 | #ifdef HAVE_PTRACE_GETXFPREGS | |
928 | { | |
929 | struct user_xfpregs_struct xfpregset; | |
04cd15b6 | 930 | |
de57eccd | 931 | case 3: |
04cd15b6 MK |
932 | if (core_reg_size != sizeof (xfpregset)) |
933 | warning ("Wrong size user_xfpregs_struct in core file."); | |
de57eccd JM |
934 | else |
935 | { | |
936 | memcpy (&xfpregset, core_reg_sect, sizeof (xfpregset)); | |
937 | supply_xfpregset (&xfpregset); | |
938 | } | |
939 | break; | |
940 | } | |
941 | #endif | |
942 | ||
943 | default: | |
944 | /* We've covered all the kinds of registers we know about here, | |
945 | so this must be something we wouldn't know what to do with | |
946 | anyway. Just ignore it. */ | |
947 | break; | |
948 | } | |
949 | } | |
950 | ||
5c44784c JM |
951 | \f |
952 | /* Calling functions in shared libraries. */ | |
04cd15b6 MK |
953 | /* FIXME: kettenis/2000-03-05: Doesn't this belong in a |
954 | target-dependent file? The function | |
955 | `i386_linux_skip_solib_resolver' is mentioned in | |
956 | `config/i386/tm-linux.h'. */ | |
5c44784c | 957 | |
d4f3574e SS |
958 | /* Find the minimal symbol named NAME, and return both the minsym |
959 | struct and its objfile. This probably ought to be in minsym.c, but | |
960 | everything there is trying to deal with things like C++ and | |
961 | SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may | |
962 | be considered too special-purpose for general consumption. */ | |
963 | ||
964 | static struct minimal_symbol * | |
965 | find_minsym_and_objfile (char *name, struct objfile **objfile_p) | |
966 | { | |
967 | struct objfile *objfile; | |
968 | ||
969 | ALL_OBJFILES (objfile) | |
970 | { | |
971 | struct minimal_symbol *msym; | |
972 | ||
973 | ALL_OBJFILE_MSYMBOLS (objfile, msym) | |
974 | { | |
975 | if (SYMBOL_NAME (msym) | |
976 | && STREQ (SYMBOL_NAME (msym), name)) | |
977 | { | |
978 | *objfile_p = objfile; | |
979 | return msym; | |
980 | } | |
981 | } | |
982 | } | |
983 | ||
984 | return 0; | |
985 | } | |
986 | ||
987 | ||
988 | static CORE_ADDR | |
989 | skip_hurd_resolver (CORE_ADDR pc) | |
990 | { | |
991 | /* The HURD dynamic linker is part of the GNU C library, so many | |
992 | GNU/Linux distributions use it. (All ELF versions, as far as I | |
993 | know.) An unresolved PLT entry points to "_dl_runtime_resolve", | |
994 | which calls "fixup" to patch the PLT, and then passes control to | |
995 | the function. | |
996 | ||
997 | We look for the symbol `_dl_runtime_resolve', and find `fixup' in | |
998 | the same objfile. If we are at the entry point of `fixup', then | |
999 | we set a breakpoint at the return address (at the top of the | |
1000 | stack), and continue. | |
1001 | ||
1002 | It's kind of gross to do all these checks every time we're | |
1003 | called, since they don't change once the executable has gotten | |
1004 | started. But this is only a temporary hack --- upcoming versions | |
1005 | of Linux will provide a portable, efficient interface for | |
1006 | debugging programs that use shared libraries. */ | |
1007 | ||
1008 | struct objfile *objfile; | |
1009 | struct minimal_symbol *resolver | |
1010 | = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile); | |
1011 | ||
1012 | if (resolver) | |
1013 | { | |
1014 | struct minimal_symbol *fixup | |
1015 | = lookup_minimal_symbol ("fixup", 0, objfile); | |
1016 | ||
1017 | if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc) | |
1018 | return (SAVED_PC_AFTER_CALL (get_current_frame ())); | |
1019 | } | |
1020 | ||
1021 | return 0; | |
1022 | } | |
1023 | ||
d4f3574e SS |
1024 | /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c. |
1025 | This function: | |
1026 | 1) decides whether a PLT has sent us into the linker to resolve | |
1027 | a function reference, and | |
1028 | 2) if so, tells us where to set a temporary breakpoint that will | |
1029 | trigger when the dynamic linker is done. */ | |
1030 | ||
1031 | CORE_ADDR | |
1032 | i386_linux_skip_solib_resolver (CORE_ADDR pc) | |
1033 | { | |
1034 | CORE_ADDR result; | |
1035 | ||
1036 | /* Plug in functions for other kinds of resolvers here. */ | |
1037 | result = skip_hurd_resolver (pc); | |
1038 | if (result) | |
1039 | return result; | |
1040 | ||
1041 | return 0; | |
1042 | } | |
de57eccd | 1043 | |
11708b95 JB |
1044 | \f |
1045 | /* Recognizing signal handler frames. */ | |
1046 | ||
1047 | /* Linux has two flavors of signals. Normal signal handlers, and | |
1048 | "realtime" (RT) signals. The RT signals can provide additional | |
1049 | information to the signal handler if the SA_SIGINFO flag is set | |
1050 | when establishing a signal handler using `sigaction'. It is not | |
1051 | unlikely that future versions of Linux will support SA_SIGINFO for | |
1052 | normal signals too. */ | |
1053 | ||
1054 | /* When the i386 Linux kernel calls a signal handler and the | |
1055 | SA_RESTORER flag isn't set, the return address points to a bit of | |
1056 | code on the stack. This function returns whether the PC appears to | |
1057 | be within this bit of code. | |
1058 | ||
1059 | The instruction sequence for normal signals is | |
1060 | pop %eax | |
1061 | mov $0x77,%eax | |
1062 | int $0x80 | |
1063 | or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80. | |
1064 | ||
1065 | Checking for the code sequence should be somewhat reliable, because | |
1066 | the effect is to call the system call sigreturn. This is unlikely | |
1067 | to occur anywhere other than a signal trampoline. | |
1068 | ||
1069 | It kind of sucks that we have to read memory from the process in | |
1070 | order to identify a signal trampoline, but there doesn't seem to be | |
1071 | any other way. The IN_SIGTRAMP macro in tm-linux.h arranges to | |
1072 | only call us if no function name could be identified, which should | |
1073 | be the case since the code is on the stack. | |
1074 | ||
1075 | Detection of signal trampolines for handlers that set the | |
1076 | SA_RESTORER flag is in general not possible. Unfortunately this is | |
1077 | what the GNU C Library has been doing for quite some time now. | |
1078 | However, as of version 2.1.2, the GNU C Library uses signal | |
1079 | trampolines (named __restore and __restore_rt) that are identical | |
1080 | to the ones used by the kernel. Therefore, these trampolines are | |
1081 | supported too. */ | |
1082 | ||
1083 | #define LINUX_SIGTRAMP_INSN0 (0x58) /* pop %eax */ | |
1084 | #define LINUX_SIGTRAMP_OFFSET0 (0) | |
1085 | #define LINUX_SIGTRAMP_INSN1 (0xb8) /* mov $NNNN,%eax */ | |
1086 | #define LINUX_SIGTRAMP_OFFSET1 (1) | |
1087 | #define LINUX_SIGTRAMP_INSN2 (0xcd) /* int */ | |
1088 | #define LINUX_SIGTRAMP_OFFSET2 (6) | |
1089 | ||
1090 | static const unsigned char linux_sigtramp_code[] = | |
1091 | { | |
1092 | LINUX_SIGTRAMP_INSN0, /* pop %eax */ | |
1093 | LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77,%eax */ | |
1094 | LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */ | |
1095 | }; | |
1096 | ||
1097 | #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code) | |
1098 | ||
1099 | /* If PC is in a sigtramp routine, return the address of the start of | |
1100 | the routine. Otherwise, return 0. */ | |
1101 | ||
1102 | static CORE_ADDR | |
1103 | i386_linux_sigtramp_start (CORE_ADDR pc) | |
1104 | { | |
1105 | unsigned char buf[LINUX_SIGTRAMP_LEN]; | |
1106 | ||
1107 | /* We only recognize a signal trampoline if PC is at the start of | |
1108 | one of the three instructions. We optimize for finding the PC at | |
1109 | the start, as will be the case when the trampoline is not the | |
1110 | first frame on the stack. We assume that in the case where the | |
1111 | PC is not at the start of the instruction sequence, there will be | |
1112 | a few trailing readable bytes on the stack. */ | |
1113 | ||
1114 | if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0) | |
1115 | return 0; | |
1116 | ||
1117 | if (buf[0] != LINUX_SIGTRAMP_INSN0) | |
1118 | { | |
1119 | int adjust; | |
1120 | ||
1121 | switch (buf[0]) | |
1122 | { | |
1123 | case LINUX_SIGTRAMP_INSN1: | |
1124 | adjust = LINUX_SIGTRAMP_OFFSET1; | |
1125 | break; | |
1126 | case LINUX_SIGTRAMP_INSN2: | |
1127 | adjust = LINUX_SIGTRAMP_OFFSET2; | |
1128 | break; | |
1129 | default: | |
1130 | return 0; | |
1131 | } | |
1132 | ||
1133 | pc -= adjust; | |
1134 | ||
1135 | if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0) | |
1136 | return 0; | |
1137 | } | |
1138 | ||
1139 | if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0) | |
1140 | return 0; | |
1141 | ||
1142 | return pc; | |
1143 | } | |
1144 | ||
1145 | /* This function does the same for RT signals. Here the instruction | |
1146 | sequence is | |
1147 | mov $0xad,%eax | |
1148 | int $0x80 | |
1149 | or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80. | |
1150 | ||
1151 | The effect is to call the system call rt_sigreturn. */ | |
1152 | ||
1153 | #define LINUX_RT_SIGTRAMP_INSN0 (0xb8) /* mov $NNNN,%eax */ | |
1154 | #define LINUX_RT_SIGTRAMP_OFFSET0 (0) | |
1155 | #define LINUX_RT_SIGTRAMP_INSN1 (0xcd) /* int */ | |
1156 | #define LINUX_RT_SIGTRAMP_OFFSET1 (5) | |
1157 | ||
1158 | static const unsigned char linux_rt_sigtramp_code[] = | |
1159 | { | |
1160 | LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad,%eax */ | |
1161 | LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */ | |
1162 | }; | |
1163 | ||
1164 | #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code) | |
1165 | ||
1166 | /* If PC is in a RT sigtramp routine, return the address of the start | |
1167 | of the routine. Otherwise, return 0. */ | |
1168 | ||
1169 | static CORE_ADDR | |
1170 | i386_linux_rt_sigtramp_start (CORE_ADDR pc) | |
1171 | { | |
1172 | unsigned char buf[LINUX_RT_SIGTRAMP_LEN]; | |
1173 | ||
1174 | /* We only recognize a signal trampoline if PC is at the start of | |
1175 | one of the two instructions. We optimize for finding the PC at | |
1176 | the start, as will be the case when the trampoline is not the | |
1177 | first frame on the stack. We assume that in the case where the | |
1178 | PC is not at the start of the instruction sequence, there will be | |
1179 | a few trailing readable bytes on the stack. */ | |
1180 | ||
1181 | if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0) | |
1182 | return 0; | |
1183 | ||
1184 | if (buf[0] != LINUX_RT_SIGTRAMP_INSN0) | |
1185 | { | |
1186 | if (buf[0] != LINUX_RT_SIGTRAMP_INSN1) | |
1187 | return 0; | |
1188 | ||
1189 | pc -= LINUX_RT_SIGTRAMP_OFFSET1; | |
1190 | ||
1191 | if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0) | |
1192 | return 0; | |
1193 | } | |
1194 | ||
1195 | if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0) | |
1196 | return 0; | |
1197 | ||
1198 | return pc; | |
1199 | } | |
1200 | ||
1201 | /* Return whether PC is in a Linux sigtramp routine. */ | |
1202 | ||
1203 | int | |
1204 | i386_linux_in_sigtramp (CORE_ADDR pc, char *name) | |
1205 | { | |
1206 | if (name) | |
1207 | return STREQ ("__restore", name) || STREQ ("__restore_rt", name); | |
1208 | ||
1209 | return (i386_linux_sigtramp_start (pc) != 0 | |
1210 | || i386_linux_rt_sigtramp_start (pc) != 0); | |
1211 | } | |
1212 | ||
1213 | /* Assuming FRAME is for a Linux sigtramp routine, return the address | |
1214 | of the associated sigcontext structure. */ | |
1215 | ||
1216 | CORE_ADDR | |
1217 | i386_linux_sigcontext_addr (struct frame_info *frame) | |
1218 | { | |
1219 | CORE_ADDR pc; | |
1220 | ||
1221 | pc = i386_linux_sigtramp_start (frame->pc); | |
1222 | if (pc) | |
1223 | { | |
1224 | CORE_ADDR sp; | |
1225 | ||
1226 | if (frame->next) | |
1227 | /* If this isn't the top frame, the next frame must be for the | |
1228 | signal handler itself. The sigcontext structure lives on | |
1229 | the stack, right after the signum argument. */ | |
1230 | return frame->next->frame + 12; | |
1231 | ||
1232 | /* This is the top frame. We'll have to find the address of the | |
1233 | sigcontext structure by looking at the stack pointer. Keep | |
1234 | in mind that the first instruction of the sigtramp code is | |
1235 | "pop %eax". If the PC is at this instruction, adjust the | |
1236 | returned value accordingly. */ | |
1237 | sp = read_register (SP_REGNUM); | |
1238 | if (pc == frame->pc) | |
1239 | return sp + 4; | |
1240 | return sp; | |
1241 | } | |
1242 | ||
1243 | pc = i386_linux_rt_sigtramp_start (frame->pc); | |
1244 | if (pc) | |
1245 | { | |
1246 | if (frame->next) | |
1247 | /* If this isn't the top frame, the next frame must be for the | |
1248 | signal handler itself. The sigcontext structure is part of | |
1249 | the user context. A pointer to the user context is passed | |
1250 | as the third argument to the signal handler. */ | |
1251 | return read_memory_integer (frame->next->frame + 16, 4) + 20; | |
1252 | ||
1253 | /* This is the top frame. Again, use the stack pointer to find | |
1254 | the address of the sigcontext structure. */ | |
1255 | return read_memory_integer (read_register (SP_REGNUM) + 8, 4) + 20; | |
1256 | } | |
1257 | ||
1258 | error ("Couldn't recognize signal trampoline."); | |
1259 | return 0; | |
1260 | } | |
1261 | ||
1262 | /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */ | |
1263 | #define LINUX_SIGCONTEXT_PC_OFFSET (56) | |
1264 | ||
1265 | /* Assuming FRAME is for a Linux sigtramp routine, return the saved | |
1266 | program counter. */ | |
1267 | ||
1268 | CORE_ADDR | |
1269 | i386_linux_sigtramp_saved_pc (struct frame_info *frame) | |
1270 | { | |
1271 | CORE_ADDR addr; | |
1272 | addr = i386_linux_sigcontext_addr (frame); | |
1273 | return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 4); | |
1274 | } | |
1275 | ||
1276 | /* Offset to saved SP in sigcontext, from <asm/sigcontext.h>. */ | |
1277 | #define LINUX_SIGCONTEXT_SP_OFFSET (28) | |
1278 | ||
1279 | /* Assuming FRAME is for a Linux sigtramp routine, return the saved | |
1280 | stack pointer. */ | |
1281 | ||
1282 | CORE_ADDR | |
1283 | i386_linux_sigtramp_saved_sp (struct frame_info *frame) | |
1284 | { | |
1285 | CORE_ADDR addr; | |
1286 | addr = i386_linux_sigcontext_addr (frame); | |
1287 | return read_memory_integer (addr + LINUX_SIGCONTEXT_SP_OFFSET, 4); | |
1288 | } | |
1289 | ||
de57eccd | 1290 | \f |
04cd15b6 MK |
1291 | /* Register that we are able to handle Linux ELF core file formats. */ |
1292 | ||
1293 | static struct core_fns linux_elf_core_fns = | |
1294 | { | |
1295 | bfd_target_elf_flavour, /* core_flavour */ | |
1296 | default_check_format, /* check_format */ | |
1297 | default_core_sniffer, /* core_sniffer */ | |
1298 | fetch_core_registers, /* core_read_registers */ | |
1299 | NULL /* next */ | |
1300 | }; | |
de57eccd JM |
1301 | |
1302 | void | |
1303 | _initialize_i386_linux_nat () | |
1304 | { | |
04cd15b6 | 1305 | add_core_fns (&linux_elf_core_fns); |
de57eccd | 1306 | } |