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