| 1 | /* Native-dependent code for GNU/Linux i386. |
| 2 | |
| 3 | Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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., 51 Franklin Street, Fifth Floor, |
| 21 | Boston, MA 02110-1301, USA. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include "inferior.h" |
| 25 | #include "gdbcore.h" |
| 26 | #include "regcache.h" |
| 27 | #include "target.h" |
| 28 | #include "linux-nat.h" |
| 29 | |
| 30 | #include "gdb_assert.h" |
| 31 | #include "gdb_string.h" |
| 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 | |
| 40 | #ifndef ORIG_EAX |
| 41 | #define ORIG_EAX -1 |
| 42 | #endif |
| 43 | |
| 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 | |
| 64 | /* Prototypes for supply_gregset etc. */ |
| 65 | #include "gregset.h" |
| 66 | |
| 67 | #include "i387-tdep.h" |
| 68 | #include "i386-tdep.h" |
| 69 | #include "i386-linux-tdep.h" |
| 70 | |
| 71 | /* Defines ps_err_e, struct ps_prochandle. */ |
| 72 | #include "gdb_proc_service.h" |
| 73 | \f |
| 74 | |
| 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 |
| 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. */ |
| 89 | static int regmap[] = |
| 90 | { |
| 91 | EAX, ECX, EDX, EBX, |
| 92 | UESP, EBP, ESI, EDI, |
| 93 | EIP, EFL, CS, SS, |
| 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 |
| 103 | }; |
| 104 | |
| 105 | /* Which ptrace request retrieves which registers? |
| 106 | These apply to the corresponding SET requests as well. */ |
| 107 | |
| 108 | #define GETREGS_SUPPLIES(regno) \ |
| 109 | ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM) |
| 110 | |
| 111 | #define GETFPXREGS_SUPPLIES(regno) \ |
| 112 | (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS) |
| 113 | |
| 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 | |
| 123 | /* Does the current host support the GETFPXREGS request? The header |
| 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. */ |
| 131 | int have_ptrace_getfpxregs = |
| 132 | #ifdef HAVE_PTRACE_GETFPXREGS |
| 133 | 1 |
| 134 | #else |
| 135 | 0 |
| 136 | #endif |
| 137 | ; |
| 138 | \f |
| 139 | |
| 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 | |
| 160 | /* Accessing registers through the U area, one at a time. */ |
| 161 | |
| 162 | /* Fetch one register. */ |
| 163 | |
| 164 | static void |
| 165 | fetch_register (int regno) |
| 166 | { |
| 167 | int tid; |
| 168 | int val; |
| 169 | |
| 170 | gdb_assert (!have_ptrace_getregs); |
| 171 | if (cannot_fetch_register (regno)) |
| 172 | { |
| 173 | regcache_raw_supply (current_regcache, regno, NULL); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | /* GNU/Linux LWP ID's are process ID's. */ |
| 178 | tid = TIDGET (inferior_ptid); |
| 179 | if (tid == 0) |
| 180 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ |
| 181 | |
| 182 | errno = 0; |
| 183 | val = ptrace (PTRACE_PEEKUSER, tid, register_addr (regno, 0), 0); |
| 184 | if (errno != 0) |
| 185 | error (_("Couldn't read register %s (#%d): %s."), REGISTER_NAME (regno), |
| 186 | regno, safe_strerror (errno)); |
| 187 | |
| 188 | regcache_raw_supply (current_regcache, regno, &val); |
| 189 | } |
| 190 | |
| 191 | /* Store one register. */ |
| 192 | |
| 193 | static void |
| 194 | store_register (int regno) |
| 195 | { |
| 196 | int tid; |
| 197 | int val; |
| 198 | |
| 199 | gdb_assert (!have_ptrace_getregs); |
| 200 | if (cannot_store_register (regno)) |
| 201 | return; |
| 202 | |
| 203 | /* GNU/Linux LWP ID's are process ID's. */ |
| 204 | tid = TIDGET (inferior_ptid); |
| 205 | if (tid == 0) |
| 206 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ |
| 207 | |
| 208 | errno = 0; |
| 209 | regcache_raw_collect (current_regcache, regno, &val); |
| 210 | ptrace (PTRACE_POKEUSER, tid, register_addr (regno, 0), val); |
| 211 | if (errno != 0) |
| 212 | error (_("Couldn't write register %s (#%d): %s."), REGISTER_NAME (regno), |
| 213 | regno, safe_strerror (errno)); |
| 214 | } |
| 215 | \f |
| 216 | |
| 217 | /* Transfering the general-purpose registers between GDB, inferiors |
| 218 | and core files. */ |
| 219 | |
| 220 | /* Fill GDB's register array with the general-purpose register values |
| 221 | in *GREGSETP. */ |
| 222 | |
| 223 | void |
| 224 | supply_gregset (elf_gregset_t *gregsetp) |
| 225 | { |
| 226 | elf_greg_t *regp = (elf_greg_t *) gregsetp; |
| 227 | int i; |
| 228 | |
| 229 | for (i = 0; i < I386_NUM_GREGS; i++) |
| 230 | regcache_raw_supply (current_regcache, i, regp + regmap[i]); |
| 231 | |
| 232 | if (I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS) |
| 233 | regcache_raw_supply (current_regcache, I386_LINUX_ORIG_EAX_REGNUM, |
| 234 | regp + ORIG_EAX); |
| 235 | } |
| 236 | |
| 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. */ |
| 240 | |
| 241 | void |
| 242 | fill_gregset (elf_gregset_t *gregsetp, int regno) |
| 243 | { |
| 244 | elf_greg_t *regp = (elf_greg_t *) gregsetp; |
| 245 | int i; |
| 246 | |
| 247 | for (i = 0; i < I386_NUM_GREGS; i++) |
| 248 | if (regno == -1 || regno == i) |
| 249 | regcache_raw_collect (current_regcache, i, regp + regmap[i]); |
| 250 | |
| 251 | if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM) |
| 252 | && I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS) |
| 253 | regcache_raw_collect (current_regcache, I386_LINUX_ORIG_EAX_REGNUM, |
| 254 | regp + ORIG_EAX); |
| 255 | } |
| 256 | |
| 257 | #ifdef HAVE_PTRACE_GETREGS |
| 258 | |
| 259 | /* Fetch all general-purpose registers from process/thread TID and |
| 260 | store their values in GDB's register array. */ |
| 261 | |
| 262 | static void |
| 263 | fetch_regs (int tid) |
| 264 | { |
| 265 | elf_gregset_t regs; |
| 266 | |
| 267 | if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) |
| 268 | { |
| 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 | |
| 277 | perror_with_name (_("Couldn't get registers")); |
| 278 | } |
| 279 | |
| 280 | supply_gregset (®s); |
| 281 | } |
| 282 | |
| 283 | /* Store all valid general-purpose registers in GDB's register array |
| 284 | into the process/thread specified by TID. */ |
| 285 | |
| 286 | static void |
| 287 | store_regs (int tid, int regno) |
| 288 | { |
| 289 | elf_gregset_t regs; |
| 290 | |
| 291 | if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) |
| 292 | perror_with_name (_("Couldn't get registers")); |
| 293 | |
| 294 | fill_gregset (®s, regno); |
| 295 | |
| 296 | if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0) |
| 297 | perror_with_name (_("Couldn't write registers")); |
| 298 | } |
| 299 | |
| 300 | #else |
| 301 | |
| 302 | static void fetch_regs (int tid) {} |
| 303 | static void store_regs (int tid, int regno) {} |
| 304 | |
| 305 | #endif |
| 306 | \f |
| 307 | |
| 308 | /* Transfering floating-point registers between GDB, inferiors and cores. */ |
| 309 | |
| 310 | /* Fill GDB's register array with the floating-point register values in |
| 311 | *FPREGSETP. */ |
| 312 | |
| 313 | void |
| 314 | supply_fpregset (elf_fpregset_t *fpregsetp) |
| 315 | { |
| 316 | i387_supply_fsave (current_regcache, -1, fpregsetp); |
| 317 | } |
| 318 | |
| 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. */ |
| 322 | |
| 323 | void |
| 324 | fill_fpregset (elf_fpregset_t *fpregsetp, int regno) |
| 325 | { |
| 326 | i387_fill_fsave ((char *) fpregsetp, regno); |
| 327 | } |
| 328 | |
| 329 | #ifdef HAVE_PTRACE_GETREGS |
| 330 | |
| 331 | /* Fetch all floating-point registers from process/thread TID and store |
| 332 | thier values in GDB's register array. */ |
| 333 | |
| 334 | static void |
| 335 | fetch_fpregs (int tid) |
| 336 | { |
| 337 | elf_fpregset_t fpregs; |
| 338 | |
| 339 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) |
| 340 | perror_with_name (_("Couldn't get floating point status")); |
| 341 | |
| 342 | supply_fpregset (&fpregs); |
| 343 | } |
| 344 | |
| 345 | /* Store all valid floating-point registers in GDB's register array |
| 346 | into the process/thread specified by TID. */ |
| 347 | |
| 348 | static void |
| 349 | store_fpregs (int tid, int regno) |
| 350 | { |
| 351 | elf_fpregset_t fpregs; |
| 352 | |
| 353 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) |
| 354 | perror_with_name (_("Couldn't get floating point status")); |
| 355 | |
| 356 | fill_fpregset (&fpregs, regno); |
| 357 | |
| 358 | if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0) |
| 359 | perror_with_name (_("Couldn't write floating point status")); |
| 360 | } |
| 361 | |
| 362 | #else |
| 363 | |
| 364 | static void fetch_fpregs (int tid) {} |
| 365 | static void store_fpregs (int tid, int regno) {} |
| 366 | |
| 367 | #endif |
| 368 | \f |
| 369 | |
| 370 | /* Transfering floating-point and SSE registers to and from GDB. */ |
| 371 | |
| 372 | #ifdef HAVE_PTRACE_GETFPXREGS |
| 373 | |
| 374 | /* Fill GDB's register array with the floating-point and SSE register |
| 375 | values in *FPXREGSETP. */ |
| 376 | |
| 377 | void |
| 378 | supply_fpxregset (elf_fpxregset_t *fpxregsetp) |
| 379 | { |
| 380 | i387_supply_fxsave (current_regcache, -1, fpxregsetp); |
| 381 | } |
| 382 | |
| 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. */ |
| 386 | |
| 387 | void |
| 388 | fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno) |
| 389 | { |
| 390 | i387_fill_fxsave ((char *) fpxregsetp, regno); |
| 391 | } |
| 392 | |
| 393 | /* Fetch all registers covered by the PTRACE_GETFPXREGS request from |
| 394 | process/thread TID and store their values in GDB's register array. |
| 395 | Return non-zero if successful, zero otherwise. */ |
| 396 | |
| 397 | static int |
| 398 | fetch_fpxregs (int tid) |
| 399 | { |
| 400 | elf_fpxregset_t fpxregs; |
| 401 | |
| 402 | if (! have_ptrace_getfpxregs) |
| 403 | return 0; |
| 404 | |
| 405 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) |
| 406 | { |
| 407 | if (errno == EIO) |
| 408 | { |
| 409 | have_ptrace_getfpxregs = 0; |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | perror_with_name (_("Couldn't read floating-point and SSE registers")); |
| 414 | } |
| 415 | |
| 416 | supply_fpxregset (&fpxregs); |
| 417 | return 1; |
| 418 | } |
| 419 | |
| 420 | /* Store all valid registers in GDB's register array covered by the |
| 421 | PTRACE_SETFPXREGS request into the process/thread specified by TID. |
| 422 | Return non-zero if successful, zero otherwise. */ |
| 423 | |
| 424 | static int |
| 425 | store_fpxregs (int tid, int regno) |
| 426 | { |
| 427 | elf_fpxregset_t fpxregs; |
| 428 | |
| 429 | if (! have_ptrace_getfpxregs) |
| 430 | return 0; |
| 431 | |
| 432 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1) |
| 433 | { |
| 434 | if (errno == EIO) |
| 435 | { |
| 436 | have_ptrace_getfpxregs = 0; |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | perror_with_name (_("Couldn't read floating-point and SSE registers")); |
| 441 | } |
| 442 | |
| 443 | fill_fpxregset (&fpxregs, regno); |
| 444 | |
| 445 | if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1) |
| 446 | perror_with_name (_("Couldn't write floating-point and SSE registers")); |
| 447 | |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | #else |
| 452 | |
| 453 | static int fetch_fpxregs (int tid) { return 0; } |
| 454 | static int store_fpxregs (int tid, int regno) { return 0; } |
| 455 | |
| 456 | #endif /* HAVE_PTRACE_GETFPXREGS */ |
| 457 | \f |
| 458 | |
| 459 | /* Transferring arbitrary registers between GDB and inferior. */ |
| 460 | |
| 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 | { |
| 469 | gdb_assert (regno >= 0 && regno < NUM_REGS); |
| 470 | return (!have_ptrace_getregs && regmap[regno] == -1); |
| 471 | } |
| 472 | |
| 473 | int |
| 474 | cannot_store_register (int regno) |
| 475 | { |
| 476 | gdb_assert (regno >= 0 && regno < NUM_REGS); |
| 477 | return (!have_ptrace_getregs && regmap[regno] == -1); |
| 478 | } |
| 479 | |
| 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). */ |
| 483 | |
| 484 | static void |
| 485 | i386_linux_fetch_inferior_registers (int regno) |
| 486 | { |
| 487 | int tid; |
| 488 | |
| 489 | /* Use the old method of peeking around in `struct user' if the |
| 490 | GETREGS request isn't available. */ |
| 491 | if (!have_ptrace_getregs) |
| 492 | { |
| 493 | int i; |
| 494 | |
| 495 | for (i = 0; i < NUM_REGS; i++) |
| 496 | if (regno == -1 || regno == i) |
| 497 | fetch_register (i); |
| 498 | |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | /* GNU/Linux LWP ID's are process ID's. */ |
| 503 | tid = TIDGET (inferior_ptid); |
| 504 | if (tid == 0) |
| 505 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ |
| 506 | |
| 507 | /* Use the PTRACE_GETFPXREGS request whenever possible, since it |
| 508 | transfers more registers in one system call, and we'll cache the |
| 509 | results. But remember that fetch_fpxregs can fail, and return |
| 510 | zero. */ |
| 511 | if (regno == -1) |
| 512 | { |
| 513 | fetch_regs (tid); |
| 514 | |
| 515 | /* The call above might reset `have_ptrace_getregs'. */ |
| 516 | if (!have_ptrace_getregs) |
| 517 | { |
| 518 | i386_linux_fetch_inferior_registers (regno); |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | if (fetch_fpxregs (tid)) |
| 523 | return; |
| 524 | fetch_fpregs (tid); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | if (GETREGS_SUPPLIES (regno)) |
| 529 | { |
| 530 | fetch_regs (tid); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | if (GETFPXREGS_SUPPLIES (regno)) |
| 535 | { |
| 536 | if (fetch_fpxregs (tid)) |
| 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. */ |
| 545 | fetch_fpregs (tid); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | internal_error (__FILE__, __LINE__, |
| 550 | _("Got request for bad register number %d."), regno); |
| 551 | } |
| 552 | |
| 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). */ |
| 556 | static void |
| 557 | i386_linux_store_inferior_registers (int regno) |
| 558 | { |
| 559 | int tid; |
| 560 | |
| 561 | /* Use the old method of poking around in `struct user' if the |
| 562 | SETREGS request isn't available. */ |
| 563 | if (!have_ptrace_getregs) |
| 564 | { |
| 565 | int i; |
| 566 | |
| 567 | for (i = 0; i < NUM_REGS; i++) |
| 568 | if (regno == -1 || regno == i) |
| 569 | store_register (i); |
| 570 | |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | /* GNU/Linux LWP ID's are process ID's. */ |
| 575 | tid = TIDGET (inferior_ptid); |
| 576 | if (tid == 0) |
| 577 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ |
| 578 | |
| 579 | /* Use the PTRACE_SETFPXREGS requests whenever possible, since it |
| 580 | transfers more registers in one system call. But remember that |
| 581 | store_fpxregs can fail, and return zero. */ |
| 582 | if (regno == -1) |
| 583 | { |
| 584 | store_regs (tid, regno); |
| 585 | if (store_fpxregs (tid, regno)) |
| 586 | return; |
| 587 | store_fpregs (tid, regno); |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | if (GETREGS_SUPPLIES (regno)) |
| 592 | { |
| 593 | store_regs (tid, regno); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | if (GETFPXREGS_SUPPLIES (regno)) |
| 598 | { |
| 599 | if (store_fpxregs (tid, regno)) |
| 600 | return; |
| 601 | |
| 602 | /* Either our processor or our kernel doesn't support the SSE |
| 603 | registers, so just write the FP registers in the traditional |
| 604 | way. */ |
| 605 | store_fpregs (tid, regno); |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | internal_error (__FILE__, __LINE__, |
| 610 | _("Got request to store bad register number %d."), regno); |
| 611 | } |
| 612 | \f |
| 613 | |
| 614 | /* Support for debug registers. */ |
| 615 | |
| 616 | static unsigned long |
| 617 | i386_linux_dr_get (int regnum) |
| 618 | { |
| 619 | int tid; |
| 620 | unsigned long value; |
| 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. */ |
| 625 | tid = PIDGET (inferior_ptid); |
| 626 | |
| 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 |
| 630 | stuff to the target vector. For now, just return zero if the |
| 631 | ptrace call fails. */ |
| 632 | errno = 0; |
| 633 | value = ptrace (PTRACE_PEEKUSER, tid, |
| 634 | offsetof (struct user, u_debugreg[regnum]), 0); |
| 635 | if (errno != 0) |
| 636 | #if 0 |
| 637 | perror_with_name (_("Couldn't read debug register")); |
| 638 | #else |
| 639 | return 0; |
| 640 | #endif |
| 641 | |
| 642 | return value; |
| 643 | } |
| 644 | |
| 645 | static void |
| 646 | i386_linux_dr_set (int regnum, unsigned long value) |
| 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. */ |
| 653 | tid = PIDGET (inferior_ptid); |
| 654 | |
| 655 | errno = 0; |
| 656 | ptrace (PTRACE_POKEUSER, tid, |
| 657 | offsetof (struct user, u_debugreg[regnum]), value); |
| 658 | if (errno != 0) |
| 659 | perror_with_name (_("Couldn't write debug register")); |
| 660 | } |
| 661 | |
| 662 | void |
| 663 | i386_linux_dr_set_control (unsigned long control) |
| 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 | |
| 684 | unsigned long |
| 685 | i386_linux_dr_get_status (void) |
| 686 | { |
| 687 | return i386_linux_dr_get (DR_STATUS); |
| 688 | } |
| 689 | \f |
| 690 | |
| 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 |
| 711 | thread local address base that GDB needs. Perhaps that |
| 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 | |
| 731 | /* The instruction for a GNU/Linux system call is: |
| 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. */ |
| 740 | #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM |
| 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 | |
| 759 | static void |
| 760 | i386_linux_resume (ptid_t ptid, int step, enum target_signal signal) |
| 761 | { |
| 762 | int pid = PIDGET (ptid); |
| 763 | |
| 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 |
| 769 | all threads" and "resume inferior_ptid" are the same. */ |
| 770 | pid = PIDGET (inferior_ptid); |
| 771 | |
| 772 | if (step) |
| 773 | { |
| 774 | CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid)); |
| 775 | gdb_byte buf[LINUX_SYSCALL_LEN]; |
| 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. */ |
| 788 | if (deprecated_read_memory_nobpt (pc, buf, LINUX_SYSCALL_LEN) == 0 |
| 789 | && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0) |
| 790 | { |
| 791 | int syscall = read_register_pid (LINUX_SYSCALL_REGNUM, |
| 792 | pid_to_ptid (pid)); |
| 793 | |
| 794 | /* Then check the system call number. */ |
| 795 | if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn) |
| 796 | { |
| 797 | CORE_ADDR sp = read_register (I386_ESP_REGNUM); |
| 798 | CORE_ADDR addr = sp; |
| 799 | unsigned long int eflags; |
| 800 | |
| 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; |
| 807 | read_memory (addr, (gdb_byte *) &eflags, 4); |
| 808 | eflags |= 0x0100; |
| 809 | write_memory (addr, (gdb_byte *) &eflags, 4); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1) |
| 815 | perror_with_name (("ptrace")); |
| 816 | } |
| 817 | |
| 818 | static void (*super_post_startup_inferior) (ptid_t ptid); |
| 819 | |
| 820 | static void |
| 821 | i386_linux_child_post_startup_inferior (ptid_t ptid) |
| 822 | { |
| 823 | i386_cleanup_dregs (); |
| 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. */ |
| 847 | linux_nat_add_target (t); |
| 848 | } |