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