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