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