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