]>
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 | ||
82 | /* Does the current host support the GETXFPREGS request? The header | |
83 | file may or may not define it, and even if it is defined, the | |
84 | kernel will return EIO if it's running on a pre-SSE processor. | |
85 | ||
c2d11a7d JM |
86 | PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own |
87 | Linux kernel patch for SSE support. That patch may or may not | |
88 | actually make it into the official distribution. If you find that | |
89 | years have gone by since this stuff was added, and Linux isn't | |
90 | using PTRACE_GETXFPREGS, that means that our patch didn't make it, | |
91 | and you can delete this, and the related code. | |
92 | ||
5c44784c JM |
93 | My instinct is to attach this to some architecture- or |
94 | target-specific data structure, but really, a particular GDB | |
95 | process can only run on top of one kernel at a time. So it's okay | |
96 | for this to be a simple variable. */ | |
97 | int have_ptrace_getxfpregs = | |
98 | #ifdef HAVE_PTRACE_GETXFPREGS | |
99 | 1 | |
100 | #else | |
101 | 0 | |
102 | #endif | |
103 | ; | |
104 | ||
5c44784c | 105 | \f |
04cd15b6 MK |
106 | /* Transfering the general-purpose registers between GDB, inferiors |
107 | and core files. */ | |
108 | ||
109 | /* Fill GDB's register array with the genereal-purpose register values | |
110 | in *GREGSETP. */ | |
5c44784c | 111 | |
d4f3574e | 112 | void |
04cd15b6 | 113 | supply_gregset (elf_gregset_t *gregsetp) |
d4f3574e | 114 | { |
04cd15b6 MK |
115 | elf_greg_t *regp = (elf_greg_t *) gregsetp; |
116 | int regi; | |
d4f3574e | 117 | |
917317f4 | 118 | for (regi = 0; regi < NUM_GREGS; regi++) |
04cd15b6 | 119 | supply_register (regi, (char *) (regp + regmap[regi])); |
d4f3574e SS |
120 | } |
121 | ||
04cd15b6 MK |
122 | /* Convert the valid general-purpose register values in GDB's register |
123 | array to `struct user' format and store them in *GREGSETP. The | |
124 | array VALID indicates which register values are valid. If VALID is | |
125 | NULL, all registers are assumed to be valid. */ | |
5c44784c | 126 | |
04cd15b6 MK |
127 | static void |
128 | convert_to_gregset (elf_gregset_t *gregsetp, signed char *valid) | |
d4f3574e | 129 | { |
04cd15b6 | 130 | elf_greg_t *regp = (elf_greg_t *) gregsetp; |
d4f3574e | 131 | int regi; |
d4f3574e | 132 | |
917317f4 JM |
133 | for (regi = 0; regi < NUM_GREGS; regi++) |
134 | if (! valid || valid[regi]) | |
135 | *(regp + regmap[regi]) = * (int *) ®isters[REGISTER_BYTE (regi)]; | |
136 | } | |
137 | ||
04cd15b6 MK |
138 | /* Fill register REGNO (if it is a general-purpose register) in |
139 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, | |
140 | do this for all registers. */ | |
917317f4 | 141 | void |
04cd15b6 | 142 | fill_gregset (elf_gregset_t *gregsetp, int regno) |
917317f4 JM |
143 | { |
144 | if (regno == -1) | |
04cd15b6 MK |
145 | { |
146 | convert_to_gregset (gregsetp, NULL); | |
147 | return; | |
148 | } | |
149 | ||
150 | if (GETREGS_SUPPLIES (regno)) | |
d4f3574e | 151 | { |
917317f4 | 152 | signed char valid[NUM_GREGS]; |
04cd15b6 | 153 | |
917317f4 JM |
154 | memset (valid, 0, sizeof (valid)); |
155 | valid[regno] = 1; | |
04cd15b6 MK |
156 | |
157 | convert_to_gregset (gregsetp, valid); | |
d4f3574e SS |
158 | } |
159 | } | |
160 | ||
04cd15b6 MK |
161 | /* Fetch all general-purpose registers from process/thread TID and |
162 | store their values in GDB's register array. */ | |
d4f3574e | 163 | |
5c44784c | 164 | static void |
ed9a39eb | 165 | fetch_regs (int tid) |
5c44784c | 166 | { |
04cd15b6 MK |
167 | elf_gregset_t regs; |
168 | int ret; | |
5c44784c | 169 | |
04cd15b6 | 170 | ret = ptrace (PTRACE_GETREGS, tid, 0, (int) ®s); |
5c44784c JM |
171 | if (ret < 0) |
172 | { | |
04cd15b6 | 173 | warning ("Couldn't get registers."); |
5c44784c JM |
174 | return; |
175 | } | |
176 | ||
04cd15b6 | 177 | supply_gregset (®s); |
5c44784c JM |
178 | } |
179 | ||
04cd15b6 MK |
180 | /* Store all valid general-purpose registers in GDB's register array |
181 | into the process/thread specified by TID. */ | |
5c44784c | 182 | |
5c44784c | 183 | static void |
ed9a39eb | 184 | store_regs (int tid) |
5c44784c | 185 | { |
04cd15b6 MK |
186 | elf_gregset_t regs; |
187 | int ret; | |
5c44784c | 188 | |
04cd15b6 | 189 | ret = ptrace (PTRACE_GETREGS, tid, 0, (int) ®s); |
5c44784c JM |
190 | if (ret < 0) |
191 | { | |
04cd15b6 | 192 | warning ("Couldn't get registers."); |
5c44784c JM |
193 | return; |
194 | } | |
195 | ||
04cd15b6 | 196 | convert_to_gregset (®s, register_valid); |
5c44784c | 197 | |
04cd15b6 | 198 | ret = ptrace (PTRACE_SETREGS, tid, 0, (int) ®s); |
5c44784c JM |
199 | if (ret < 0) |
200 | { | |
04cd15b6 | 201 | warning ("Couldn't write registers."); |
5c44784c JM |
202 | return; |
203 | } | |
204 | } | |
205 | ||
5c44784c JM |
206 | \f |
207 | /* Transfering floating-point registers between GDB, inferiors and cores. */ | |
208 | ||
04cd15b6 MK |
209 | /* What is the address of st(N) within the floating-point register set F? */ |
210 | #define FPREG_ADDR(f, n) ((char *) &(f)->st_space + (n) * 10) | |
d4f3574e | 211 | |
04cd15b6 | 212 | /* Fill GDB's register array with the floating-point register values in |
917317f4 | 213 | *FPREGSETP. */ |
04cd15b6 | 214 | |
d4f3574e | 215 | void |
04cd15b6 | 216 | supply_fpregset (elf_fpregset_t *fpregsetp) |
d4f3574e | 217 | { |
04cd15b6 | 218 | int reg; |
917317f4 JM |
219 | |
220 | /* Supply the floating-point registers. */ | |
04cd15b6 MK |
221 | for (reg = 0; reg < 8; reg++) |
222 | supply_register (FP0_REGNUM + reg, FPREG_ADDR (fpregsetp, reg)); | |
917317f4 JM |
223 | |
224 | supply_register (FCTRL_REGNUM, (char *) &fpregsetp->cwd); | |
225 | supply_register (FSTAT_REGNUM, (char *) &fpregsetp->swd); | |
226 | supply_register (FTAG_REGNUM, (char *) &fpregsetp->twd); | |
227 | supply_register (FCOFF_REGNUM, (char *) &fpregsetp->fip); | |
228 | supply_register (FDS_REGNUM, (char *) &fpregsetp->fos); | |
229 | supply_register (FDOFF_REGNUM, (char *) &fpregsetp->foo); | |
230 | ||
231 | /* Extract the code segment and opcode from the "fcs" member. */ | |
232 | { | |
233 | long l; | |
234 | ||
235 | l = fpregsetp->fcs & 0xffff; | |
236 | supply_register (FCS_REGNUM, (char *) &l); | |
237 | ||
238 | l = (fpregsetp->fcs >> 16) & ((1 << 11) - 1); | |
239 | supply_register (FOP_REGNUM, (char *) &l); | |
240 | } | |
d4f3574e SS |
241 | } |
242 | ||
04cd15b6 MK |
243 | /* Convert the valid floating-point register values in GDB's register |
244 | array to `struct user' format and store them in *FPREGSETP. The | |
245 | array VALID indicates which register values are valid. If VALID is | |
246 | NULL, all registers are assumed to be valid. */ | |
d4f3574e | 247 | |
04cd15b6 MK |
248 | static void |
249 | convert_to_fpregset (elf_fpregset_t *fpregsetp, signed char *valid) | |
d4f3574e | 250 | { |
04cd15b6 | 251 | int reg; |
917317f4 JM |
252 | |
253 | /* Fill in the floating-point registers. */ | |
04cd15b6 MK |
254 | for (reg = 0; reg < 8; reg++) |
255 | if (!valid || valid[reg]) | |
256 | memcpy (FPREG_ADDR (fpregsetp, reg), | |
257 | ®isters[REGISTER_BYTE (FP0_REGNUM + reg)], | |
258 | REGISTER_RAW_SIZE(FP0_REGNUM + reg)); | |
917317f4 JM |
259 | |
260 | #define fill(MEMBER, REGNO) \ | |
261 | if (! valid || valid[(REGNO)]) \ | |
262 | memcpy (&fpregsetp->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \ | |
263 | sizeof (fpregsetp->MEMBER)) | |
264 | ||
265 | fill (cwd, FCTRL_REGNUM); | |
266 | fill (swd, FSTAT_REGNUM); | |
267 | fill (twd, FTAG_REGNUM); | |
268 | fill (fip, FCOFF_REGNUM); | |
269 | fill (foo, FDOFF_REGNUM); | |
270 | fill (fos, FDS_REGNUM); | |
271 | ||
272 | #undef fill | |
273 | ||
274 | if (! valid || valid[FCS_REGNUM]) | |
275 | fpregsetp->fcs | |
276 | = ((fpregsetp->fcs & ~0xffff) | |
277 | | (* (int *) ®isters[REGISTER_BYTE (FCS_REGNUM)] & 0xffff)); | |
278 | ||
279 | if (! valid || valid[FOP_REGNUM]) | |
280 | fpregsetp->fcs | |
281 | = ((fpregsetp->fcs & 0xffff) | |
282 | | ((*(int *) ®isters[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1)) | |
283 | << 16)); | |
284 | } | |
d4f3574e | 285 | |
04cd15b6 MK |
286 | /* Fill register REGNO (if it is a floating-point register) in |
287 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, | |
288 | do this for all registers. */ | |
917317f4 JM |
289 | |
290 | void | |
04cd15b6 | 291 | fill_fpregset (elf_fpregset_t *fpregsetp, int regno) |
917317f4 | 292 | { |
04cd15b6 MK |
293 | if (regno == -1) |
294 | { | |
295 | convert_to_fpregset (fpregsetp, NULL); | |
296 | return; | |
297 | } | |
298 | ||
299 | if (GETFPREGS_SUPPLIES(regno)) | |
300 | { | |
301 | signed char valid[MAX_NUM_REGS]; | |
302 | ||
303 | memset (valid, 0, sizeof (valid)); | |
304 | valid[regno] = 1; | |
305 | ||
306 | convert_to_fpregset (fpregsetp, valid); | |
307 | } | |
d4f3574e SS |
308 | } |
309 | ||
04cd15b6 MK |
310 | /* Fetch all floating-point registers from process/thread TID and store |
311 | thier values in GDB's register array. */ | |
917317f4 | 312 | |
d4f3574e | 313 | static void |
ed9a39eb | 314 | fetch_fpregs (int tid) |
d4f3574e | 315 | { |
04cd15b6 MK |
316 | elf_fpregset_t fpregs; |
317 | int ret; | |
d4f3574e | 318 | |
04cd15b6 | 319 | ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs); |
917317f4 | 320 | if (ret < 0) |
d4f3574e | 321 | { |
04cd15b6 | 322 | warning ("Couldn't get floating point status."); |
d4f3574e SS |
323 | return; |
324 | } | |
325 | ||
04cd15b6 | 326 | supply_fpregset (&fpregs); |
d4f3574e SS |
327 | } |
328 | ||
04cd15b6 MK |
329 | /* Store all valid floating-point registers in GDB's register array |
330 | into the process/thread specified by TID. */ | |
d4f3574e | 331 | |
d4f3574e | 332 | static void |
ed9a39eb | 333 | store_fpregs (int tid) |
d4f3574e | 334 | { |
04cd15b6 | 335 | elf_fpregset_t fpregs; |
917317f4 | 336 | int ret; |
d4f3574e | 337 | |
04cd15b6 | 338 | ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs); |
917317f4 | 339 | if (ret < 0) |
d4f3574e | 340 | { |
04cd15b6 | 341 | warning ("Couldn't get floating point status."); |
d4f3574e SS |
342 | return; |
343 | } | |
344 | ||
04cd15b6 | 345 | convert_to_fpregset (&fpregs, register_valid); |
d4f3574e | 346 | |
04cd15b6 | 347 | ret = ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs); |
917317f4 | 348 | if (ret < 0) |
d4f3574e | 349 | { |
04cd15b6 | 350 | warning ("Couldn't write floating point status."); |
d4f3574e SS |
351 | return; |
352 | } | |
d4f3574e SS |
353 | } |
354 | ||
5c44784c JM |
355 | \f |
356 | /* Transfering floating-point and SSE registers to and from GDB. */ | |
d4f3574e | 357 | |
11cf8741 JM |
358 | /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own |
359 | Linux kernel patch for SSE support. That patch may or may not | |
360 | actually make it into the official distribution. If you find that | |
361 | years have gone by since this code was added, and Linux isn't using | |
362 | PTRACE_GETXFPREGS, that means that our patch didn't make it, and | |
363 | you can delete this code. */ | |
364 | ||
5c44784c | 365 | #ifdef HAVE_PTRACE_GETXFPREGS |
04cd15b6 MK |
366 | |
367 | /* Fill GDB's register array with the floating-point and SSE register | |
368 | values in *XFPREGS. */ | |
369 | ||
d4f3574e | 370 | static void |
5c44784c | 371 | supply_xfpregset (struct user_xfpregs_struct *xfpregs) |
d4f3574e | 372 | { |
5c44784c | 373 | int reg; |
d4f3574e | 374 | |
5c44784c JM |
375 | /* Supply the floating-point registers. */ |
376 | for (reg = 0; reg < 8; reg++) | |
377 | supply_register (FP0_REGNUM + reg, (char *) &xfpregs->st_space[reg]); | |
378 | ||
379 | { | |
380 | supply_register (FCTRL_REGNUM, (char *) &xfpregs->cwd); | |
381 | supply_register (FSTAT_REGNUM, (char *) &xfpregs->swd); | |
382 | supply_register (FTAG_REGNUM, (char *) &xfpregs->twd); | |
383 | supply_register (FCOFF_REGNUM, (char *) &xfpregs->fip); | |
384 | supply_register (FDS_REGNUM, (char *) &xfpregs->fos); | |
385 | supply_register (FDOFF_REGNUM, (char *) &xfpregs->foo); | |
386 | ||
387 | /* Extract the code segment and opcode from the "fcs" member. */ | |
d4f3574e | 388 | { |
5c44784c JM |
389 | long l; |
390 | ||
391 | l = xfpregs->fcs & 0xffff; | |
392 | supply_register (FCS_REGNUM, (char *) &l); | |
393 | ||
394 | l = (xfpregs->fcs >> 16) & ((1 << 11) - 1); | |
395 | supply_register (FOP_REGNUM, (char *) &l); | |
d4f3574e | 396 | } |
5c44784c | 397 | } |
d4f3574e | 398 | |
5c44784c JM |
399 | /* Supply the SSE registers. */ |
400 | for (reg = 0; reg < 8; reg++) | |
401 | supply_register (XMM0_REGNUM + reg, (char *) &xfpregs->xmm_space[reg]); | |
402 | supply_register (MXCSR_REGNUM, (char *) &xfpregs->mxcsr); | |
d4f3574e SS |
403 | } |
404 | ||
04cd15b6 MK |
405 | /* Convert the valid floating-point and SSE registers in GDB's |
406 | register array to `struct user' format and store them in *XFPREGS. | |
407 | The array VALID indicates which registers are valid. If VALID is | |
408 | NULL, all registers are assumed to be valid. */ | |
d4f3574e | 409 | |
d4f3574e | 410 | static void |
5c44784c | 411 | convert_to_xfpregset (struct user_xfpregs_struct *xfpregs, |
5c44784c | 412 | signed char *valid) |
d4f3574e | 413 | { |
5c44784c | 414 | int reg; |
d4f3574e | 415 | |
5c44784c JM |
416 | /* Fill in the floating-point registers. */ |
417 | for (reg = 0; reg < 8; reg++) | |
418 | if (!valid || valid[reg]) | |
419 | memcpy (&xfpregs->st_space[reg], | |
420 | ®isters[REGISTER_BYTE (FP0_REGNUM + reg)], | |
421 | REGISTER_RAW_SIZE(FP0_REGNUM + reg)); | |
422 | ||
423 | #define fill(MEMBER, REGNO) \ | |
424 | if (! valid || valid[(REGNO)]) \ | |
425 | memcpy (&xfpregs->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \ | |
426 | sizeof (xfpregs->MEMBER)) | |
427 | ||
428 | fill (cwd, FCTRL_REGNUM); | |
429 | fill (swd, FSTAT_REGNUM); | |
430 | fill (twd, FTAG_REGNUM); | |
431 | fill (fip, FCOFF_REGNUM); | |
432 | fill (foo, FDOFF_REGNUM); | |
433 | fill (fos, FDS_REGNUM); | |
434 | ||
435 | #undef fill | |
436 | ||
437 | if (! valid || valid[FCS_REGNUM]) | |
438 | xfpregs->fcs | |
439 | = ((xfpregs->fcs & ~0xffff) | |
440 | | (* (int *) ®isters[REGISTER_BYTE (FCS_REGNUM)] & 0xffff)); | |
441 | ||
442 | if (! valid || valid[FOP_REGNUM]) | |
443 | xfpregs->fcs | |
444 | = ((xfpregs->fcs & 0xffff) | |
445 | | ((*(int *) ®isters[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1)) | |
446 | << 16)); | |
447 | ||
448 | /* Fill in the XMM registers. */ | |
449 | for (reg = 0; reg < 8; reg++) | |
450 | if (! valid || valid[reg]) | |
451 | memcpy (&xfpregs->xmm_space[reg], | |
452 | ®isters[REGISTER_BYTE (XMM0_REGNUM + reg)], | |
453 | REGISTER_RAW_SIZE (XMM0_REGNUM + reg)); | |
454 | } | |
455 | ||
04cd15b6 MK |
456 | /* Fetch all registers covered by the PTRACE_SETXFPREGS request from |
457 | process/thread TID and store their values in GDB's register array. | |
458 | Return non-zero if successful, zero otherwise. */ | |
5c44784c | 459 | |
5c44784c | 460 | static int |
ed9a39eb | 461 | fetch_xfpregs (int tid) |
5c44784c | 462 | { |
5c44784c | 463 | struct user_xfpregs_struct xfpregs; |
04cd15b6 | 464 | int ret; |
5c44784c JM |
465 | |
466 | if (! have_ptrace_getxfpregs) | |
467 | return 0; | |
468 | ||
ed9a39eb | 469 | ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs); |
5c44784c | 470 | if (ret == -1) |
d4f3574e | 471 | { |
5c44784c JM |
472 | if (errno == EIO) |
473 | { | |
474 | have_ptrace_getxfpregs = 0; | |
475 | return 0; | |
476 | } | |
477 | ||
04cd15b6 | 478 | warning ("Couldn't read floating-point and SSE registers."); |
5c44784c | 479 | return 0; |
d4f3574e SS |
480 | } |
481 | ||
5c44784c JM |
482 | supply_xfpregset (&xfpregs); |
483 | return 1; | |
484 | } | |
d4f3574e | 485 | |
04cd15b6 MK |
486 | /* Store all valid registers in GDB's register array covered by the |
487 | PTRACE_SETXFPREGS request into the process/thread specified by TID. | |
488 | Return non-zero if successful, zero otherwise. */ | |
5c44784c | 489 | |
5c44784c | 490 | static int |
ed9a39eb | 491 | store_xfpregs (int tid) |
5c44784c | 492 | { |
5c44784c | 493 | struct user_xfpregs_struct xfpregs; |
04cd15b6 | 494 | int ret; |
5c44784c JM |
495 | |
496 | if (! have_ptrace_getxfpregs) | |
497 | return 0; | |
498 | ||
ed9a39eb | 499 | ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs); |
5c44784c | 500 | if (ret == -1) |
d4f3574e | 501 | { |
5c44784c JM |
502 | if (errno == EIO) |
503 | { | |
504 | have_ptrace_getxfpregs = 0; | |
505 | return 0; | |
506 | } | |
507 | ||
04cd15b6 | 508 | warning ("Couldn't read floating-point and SSE registers."); |
5c44784c JM |
509 | return 0; |
510 | } | |
511 | ||
04cd15b6 | 512 | convert_to_xfpregset (&xfpregs, register_valid); |
5c44784c | 513 | |
ed9a39eb | 514 | if (ptrace (PTRACE_SETXFPREGS, tid, 0, &xfpregs) < 0) |
5c44784c JM |
515 | { |
516 | warning ("Couldn't write floating-point and SSE registers."); | |
517 | return 0; | |
d4f3574e | 518 | } |
5c44784c JM |
519 | |
520 | return 1; | |
521 | } | |
522 | ||
04cd15b6 | 523 | /* Fill the XMM registers in the register array with dummy values. For |
5c44784c JM |
524 | cases where we don't have access to the XMM registers. I think |
525 | this is cleaner than printing a warning. For a cleaner solution, | |
526 | we should gdbarchify the i386 family. */ | |
04cd15b6 | 527 | |
5c44784c | 528 | static void |
04cd15b6 | 529 | dummy_sse_values (void) |
5c44784c JM |
530 | { |
531 | /* C doesn't have a syntax for NaN's, so write it out as an array of | |
532 | longs. */ | |
533 | static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }; | |
534 | static long mxcsr = 0x1f80; | |
535 | int reg; | |
536 | ||
537 | for (reg = 0; reg < 8; reg++) | |
538 | supply_register (XMM0_REGNUM + reg, (char *) dummy); | |
539 | supply_register (MXCSR_REGNUM, (char *) &mxcsr); | |
d4f3574e SS |
540 | } |
541 | ||
5c44784c JM |
542 | #else |
543 | ||
544 | /* Stub versions of the above routines, for systems that don't have | |
545 | PTRACE_GETXFPREGS. */ | |
ed9a39eb JM |
546 | static int store_xfpregs (int tid) { return 0; } |
547 | static int fetch_xfpregs (int tid) { return 0; } | |
04cd15b6 | 548 | static void dummy_sse_values (void) {} |
5c44784c JM |
549 | |
550 | #endif | |
551 | ||
552 | \f | |
553 | /* Transferring arbitrary registers between GDB and inferior. */ | |
d4f3574e | 554 | |
04cd15b6 MK |
555 | /* Fetch register REGNO from the child process. If REGNO is -1, do |
556 | this for all registers (including the floating point and SSE | |
557 | registers). */ | |
d4f3574e SS |
558 | |
559 | void | |
917317f4 | 560 | fetch_inferior_registers (int regno) |
d4f3574e | 561 | { |
ed9a39eb JM |
562 | int tid; |
563 | ||
04cd15b6 | 564 | /* Linux LWP ID's are process ID's. */ |
ed9a39eb | 565 | if ((tid = TIDGET (inferior_pid)) == 0) |
04cd15b6 | 566 | tid = inferior_pid; /* Not a threaded program. */ |
ed9a39eb | 567 | |
04cd15b6 MK |
568 | /* Use the PTRACE_GETXFPREGS request whenever possible, since it |
569 | transfers more registers in one system call, and we'll cache the | |
570 | results. But remember that fetch_xfpregs can fail, and return | |
571 | zero. */ | |
5c44784c JM |
572 | if (regno == -1) |
573 | { | |
ed9a39eb JM |
574 | fetch_regs (tid); |
575 | if (fetch_xfpregs (tid)) | |
5c44784c | 576 | return; |
ed9a39eb | 577 | fetch_fpregs (tid); |
5c44784c JM |
578 | return; |
579 | } | |
d4f3574e | 580 | |
5c44784c JM |
581 | if (GETREGS_SUPPLIES (regno)) |
582 | { | |
ed9a39eb | 583 | fetch_regs (tid); |
5c44784c JM |
584 | return; |
585 | } | |
586 | ||
587 | if (GETXFPREGS_SUPPLIES (regno)) | |
588 | { | |
ed9a39eb | 589 | if (fetch_xfpregs (tid)) |
5c44784c JM |
590 | return; |
591 | ||
592 | /* Either our processor or our kernel doesn't support the SSE | |
593 | registers, so read the FP registers in the traditional way, | |
594 | and fill the SSE registers with dummy values. It would be | |
595 | more graceful to handle differences in the register set using | |
596 | gdbarch. Until then, this will at least make things work | |
597 | plausibly. */ | |
ed9a39eb | 598 | fetch_fpregs (tid); |
5c44784c JM |
599 | dummy_sse_values (); |
600 | return; | |
601 | } | |
602 | ||
603 | internal_error ("i386-linux-nat.c (fetch_inferior_registers): " | |
604 | "got request for bad register number %d", regno); | |
d4f3574e SS |
605 | } |
606 | ||
04cd15b6 MK |
607 | /* Store register REGNO back into the child process. If REGNO is -1, |
608 | do this for all registers (including the floating point and SSE | |
609 | registers). */ | |
d4f3574e | 610 | void |
04cd15b6 | 611 | store_inferior_registers (int regno) |
d4f3574e | 612 | { |
ed9a39eb JM |
613 | int tid; |
614 | ||
04cd15b6 | 615 | /* Linux LWP ID's are process ID's. */ |
ed9a39eb | 616 | if ((tid = TIDGET (inferior_pid)) == 0) |
04cd15b6 | 617 | tid = inferior_pid; /* Not a threaded program. */ |
ed9a39eb | 618 | |
04cd15b6 MK |
619 | /* Use the PTRACE_SETXFPREGS requests whenever possibl, since it |
620 | transfers more registers in one system call. But remember that | |
ed9a39eb | 621 | store_xfpregs can fail, and return zero. */ |
5c44784c JM |
622 | if (regno == -1) |
623 | { | |
ed9a39eb JM |
624 | store_regs (tid); |
625 | if (store_xfpregs (tid)) | |
5c44784c | 626 | return; |
ed9a39eb | 627 | store_fpregs (tid); |
5c44784c JM |
628 | return; |
629 | } | |
d4f3574e | 630 | |
5c44784c JM |
631 | if (GETREGS_SUPPLIES (regno)) |
632 | { | |
ed9a39eb | 633 | store_regs (tid); |
5c44784c JM |
634 | return; |
635 | } | |
636 | ||
637 | if (GETXFPREGS_SUPPLIES (regno)) | |
638 | { | |
ed9a39eb | 639 | if (store_xfpregs (tid)) |
5c44784c JM |
640 | return; |
641 | ||
642 | /* Either our processor or our kernel doesn't support the SSE | |
04cd15b6 MK |
643 | registers, so just write the FP registers in the traditional |
644 | way. */ | |
ed9a39eb | 645 | store_fpregs (tid); |
5c44784c JM |
646 | return; |
647 | } | |
648 | ||
04cd15b6 | 649 | internal_error ("Got request to store bad register number %d.", regno); |
d4f3574e SS |
650 | } |
651 | ||
de57eccd JM |
652 | \f |
653 | /* Interpreting register set info found in core files. */ | |
654 | ||
655 | /* Provide registers to GDB from a core file. | |
656 | ||
657 | (We can't use the generic version of this function in | |
658 | core-regset.c, because Linux has *three* different kinds of | |
659 | register set notes. core-regset.c would have to call | |
660 | supply_xfpregset, which most platforms don't have.) | |
661 | ||
662 | CORE_REG_SECT points to an array of bytes, which are the contents | |
663 | of a `note' from a core file which BFD thinks might contain | |
664 | register contents. CORE_REG_SIZE is its size. | |
665 | ||
666 | WHICH says which register set corelow suspects this is: | |
04cd15b6 MK |
667 | 0 --- the general-purpose register set, in elf_gregset_t format |
668 | 2 --- the floating-point register set, in elf_fpregset_t format | |
669 | 3 --- the extended floating-point register set, in struct | |
670 | user_xfpregs_struct format | |
671 | ||
672 | REG_ADDR isn't used on Linux. */ | |
de57eccd | 673 | |
de57eccd | 674 | static void |
04cd15b6 MK |
675 | fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, |
676 | int which, CORE_ADDR reg_addr) | |
de57eccd | 677 | { |
04cd15b6 MK |
678 | elf_gregset_t gregset; |
679 | elf_fpregset_t fpregset; | |
de57eccd JM |
680 | |
681 | switch (which) | |
682 | { | |
683 | case 0: | |
684 | if (core_reg_size != sizeof (gregset)) | |
04cd15b6 | 685 | warning ("Wrong size gregset in core file."); |
de57eccd JM |
686 | else |
687 | { | |
688 | memcpy (&gregset, core_reg_sect, sizeof (gregset)); | |
689 | supply_gregset (&gregset); | |
690 | } | |
691 | break; | |
692 | ||
693 | case 2: | |
694 | if (core_reg_size != sizeof (fpregset)) | |
04cd15b6 | 695 | warning ("Wrong size fpregset in core file."); |
de57eccd JM |
696 | else |
697 | { | |
698 | memcpy (&fpregset, core_reg_sect, sizeof (fpregset)); | |
699 | supply_fpregset (&fpregset); | |
700 | } | |
701 | break; | |
702 | ||
703 | #ifdef HAVE_PTRACE_GETXFPREGS | |
704 | { | |
705 | struct user_xfpregs_struct xfpregset; | |
04cd15b6 | 706 | |
de57eccd | 707 | case 3: |
04cd15b6 MK |
708 | if (core_reg_size != sizeof (xfpregset)) |
709 | warning ("Wrong size user_xfpregs_struct in core file."); | |
de57eccd JM |
710 | else |
711 | { | |
712 | memcpy (&xfpregset, core_reg_sect, sizeof (xfpregset)); | |
713 | supply_xfpregset (&xfpregset); | |
714 | } | |
715 | break; | |
716 | } | |
717 | #endif | |
718 | ||
719 | default: | |
720 | /* We've covered all the kinds of registers we know about here, | |
721 | so this must be something we wouldn't know what to do with | |
722 | anyway. Just ignore it. */ | |
723 | break; | |
724 | } | |
725 | } | |
726 | ||
5c44784c JM |
727 | \f |
728 | /* Calling functions in shared libraries. */ | |
04cd15b6 MK |
729 | /* FIXME: kettenis/2000-03-05: Doesn't this belong in a |
730 | target-dependent file? The function | |
731 | `i386_linux_skip_solib_resolver' is mentioned in | |
732 | `config/i386/tm-linux.h'. */ | |
5c44784c | 733 | |
d4f3574e SS |
734 | /* Find the minimal symbol named NAME, and return both the minsym |
735 | struct and its objfile. This probably ought to be in minsym.c, but | |
736 | everything there is trying to deal with things like C++ and | |
737 | SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may | |
738 | be considered too special-purpose for general consumption. */ | |
739 | ||
740 | static struct minimal_symbol * | |
741 | find_minsym_and_objfile (char *name, struct objfile **objfile_p) | |
742 | { | |
743 | struct objfile *objfile; | |
744 | ||
745 | ALL_OBJFILES (objfile) | |
746 | { | |
747 | struct minimal_symbol *msym; | |
748 | ||
749 | ALL_OBJFILE_MSYMBOLS (objfile, msym) | |
750 | { | |
751 | if (SYMBOL_NAME (msym) | |
752 | && STREQ (SYMBOL_NAME (msym), name)) | |
753 | { | |
754 | *objfile_p = objfile; | |
755 | return msym; | |
756 | } | |
757 | } | |
758 | } | |
759 | ||
760 | return 0; | |
761 | } | |
762 | ||
763 | ||
764 | static CORE_ADDR | |
765 | skip_hurd_resolver (CORE_ADDR pc) | |
766 | { | |
767 | /* The HURD dynamic linker is part of the GNU C library, so many | |
768 | GNU/Linux distributions use it. (All ELF versions, as far as I | |
769 | know.) An unresolved PLT entry points to "_dl_runtime_resolve", | |
770 | which calls "fixup" to patch the PLT, and then passes control to | |
771 | the function. | |
772 | ||
773 | We look for the symbol `_dl_runtime_resolve', and find `fixup' in | |
774 | the same objfile. If we are at the entry point of `fixup', then | |
775 | we set a breakpoint at the return address (at the top of the | |
776 | stack), and continue. | |
777 | ||
778 | It's kind of gross to do all these checks every time we're | |
779 | called, since they don't change once the executable has gotten | |
780 | started. But this is only a temporary hack --- upcoming versions | |
781 | of Linux will provide a portable, efficient interface for | |
782 | debugging programs that use shared libraries. */ | |
783 | ||
784 | struct objfile *objfile; | |
785 | struct minimal_symbol *resolver | |
786 | = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile); | |
787 | ||
788 | if (resolver) | |
789 | { | |
790 | struct minimal_symbol *fixup | |
791 | = lookup_minimal_symbol ("fixup", 0, objfile); | |
792 | ||
793 | if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc) | |
794 | return (SAVED_PC_AFTER_CALL (get_current_frame ())); | |
795 | } | |
796 | ||
797 | return 0; | |
798 | } | |
799 | ||
d4f3574e SS |
800 | /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c. |
801 | This function: | |
802 | 1) decides whether a PLT has sent us into the linker to resolve | |
803 | a function reference, and | |
804 | 2) if so, tells us where to set a temporary breakpoint that will | |
805 | trigger when the dynamic linker is done. */ | |
806 | ||
807 | CORE_ADDR | |
808 | i386_linux_skip_solib_resolver (CORE_ADDR pc) | |
809 | { | |
810 | CORE_ADDR result; | |
811 | ||
812 | /* Plug in functions for other kinds of resolvers here. */ | |
813 | result = skip_hurd_resolver (pc); | |
814 | if (result) | |
815 | return result; | |
816 | ||
817 | return 0; | |
818 | } | |
de57eccd | 819 | |
de57eccd | 820 | \f |
04cd15b6 MK |
821 | /* Register that we are able to handle Linux ELF core file formats. */ |
822 | ||
823 | static struct core_fns linux_elf_core_fns = | |
824 | { | |
825 | bfd_target_elf_flavour, /* core_flavour */ | |
826 | default_check_format, /* check_format */ | |
827 | default_core_sniffer, /* core_sniffer */ | |
828 | fetch_core_registers, /* core_read_registers */ | |
829 | NULL /* next */ | |
830 | }; | |
de57eccd JM |
831 | |
832 | void | |
833 | _initialize_i386_linux_nat () | |
834 | { | |
04cd15b6 | 835 | add_core_fns (&linux_elf_core_fns); |
de57eccd | 836 | } |