| 1 | /* Native-dependent code for GNU/Linux i386. |
| 2 | |
| 3 | Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
| 4 | 2009, 2010, 2011 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 3 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, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "i386-nat.h" |
| 23 | #include "inferior.h" |
| 24 | #include "gdbcore.h" |
| 25 | #include "regcache.h" |
| 26 | #include "regset.h" |
| 27 | #include "target.h" |
| 28 | #include "linux-nat.h" |
| 29 | |
| 30 | #include "gdb_assert.h" |
| 31 | #include "gdb_string.h" |
| 32 | #include "elf/common.h" |
| 33 | #include <sys/uio.h> |
| 34 | #include <sys/ptrace.h> |
| 35 | #include <sys/user.h> |
| 36 | #include <sys/procfs.h> |
| 37 | |
| 38 | #ifdef HAVE_SYS_REG_H |
| 39 | #include <sys/reg.h> |
| 40 | #endif |
| 41 | |
| 42 | #ifndef ORIG_EAX |
| 43 | #define ORIG_EAX -1 |
| 44 | #endif |
| 45 | |
| 46 | #ifdef HAVE_SYS_DEBUGREG_H |
| 47 | #include <sys/debugreg.h> |
| 48 | #endif |
| 49 | |
| 50 | #ifndef DR_FIRSTADDR |
| 51 | #define DR_FIRSTADDR 0 |
| 52 | #endif |
| 53 | |
| 54 | #ifndef DR_LASTADDR |
| 55 | #define DR_LASTADDR 3 |
| 56 | #endif |
| 57 | |
| 58 | #ifndef DR_STATUS |
| 59 | #define DR_STATUS 6 |
| 60 | #endif |
| 61 | |
| 62 | #ifndef DR_CONTROL |
| 63 | #define DR_CONTROL 7 |
| 64 | #endif |
| 65 | |
| 66 | /* Prototypes for supply_gregset etc. */ |
| 67 | #include "gregset.h" |
| 68 | |
| 69 | #include "i387-tdep.h" |
| 70 | #include "i386-tdep.h" |
| 71 | #include "i386-linux-tdep.h" |
| 72 | |
| 73 | /* Defines ps_err_e, struct ps_prochandle. */ |
| 74 | #include "gdb_proc_service.h" |
| 75 | |
| 76 | #include "i386-xstate.h" |
| 77 | |
| 78 | #ifndef PTRACE_GETREGSET |
| 79 | #define PTRACE_GETREGSET 0x4204 |
| 80 | #endif |
| 81 | |
| 82 | #ifndef PTRACE_SETREGSET |
| 83 | #define PTRACE_SETREGSET 0x4205 |
| 84 | #endif |
| 85 | |
| 86 | /* Does the current host support PTRACE_GETREGSET? */ |
| 87 | static int have_ptrace_getregset = -1; |
| 88 | \f |
| 89 | |
| 90 | /* The register sets used in GNU/Linux ELF core-dumps are identical to |
| 91 | the register sets in `struct user' that is used for a.out |
| 92 | core-dumps, and is also used by `ptrace'. The corresponding types |
| 93 | are `elf_gregset_t' for the general-purpose registers (with |
| 94 | `elf_greg_t' the type of a single GP register) and `elf_fpregset_t' |
| 95 | for the floating-point registers. |
| 96 | |
| 97 | Those types used to be available under the names `gregset_t' and |
| 98 | `fpregset_t' too, and this file used those names in the past. But |
| 99 | those names are now used for the register sets used in the |
| 100 | `mcontext_t' type, and have a different size and layout. */ |
| 101 | |
| 102 | /* Which ptrace request retrieves which registers? |
| 103 | These apply to the corresponding SET requests as well. */ |
| 104 | |
| 105 | #define GETREGS_SUPPLIES(regno) \ |
| 106 | ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM) |
| 107 | |
| 108 | #define GETFPXREGS_SUPPLIES(regno) \ |
| 109 | (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS) |
| 110 | |
| 111 | #define GETXSTATEREGS_SUPPLIES(regno) \ |
| 112 | (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX_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 | /* Accessing registers through the U area, one at a time. */ |
| 141 | |
| 142 | /* Fetch one register. */ |
| 143 | |
| 144 | static void |
| 145 | fetch_register (struct regcache *regcache, int regno) |
| 146 | { |
| 147 | int tid; |
| 148 | int val; |
| 149 | |
| 150 | gdb_assert (!have_ptrace_getregs); |
| 151 | if (i386_linux_gregset_reg_offset[regno] == -1) |
| 152 | { |
| 153 | regcache_raw_supply (regcache, regno, NULL); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | /* GNU/Linux LWP ID's are process ID's. */ |
| 158 | tid = TIDGET (inferior_ptid); |
| 159 | if (tid == 0) |
| 160 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ |
| 161 | |
| 162 | errno = 0; |
| 163 | val = ptrace (PTRACE_PEEKUSER, tid, |
| 164 | i386_linux_gregset_reg_offset[regno], 0); |
| 165 | if (errno != 0) |
| 166 | error (_("Couldn't read register %s (#%d): %s."), |
| 167 | gdbarch_register_name (get_regcache_arch (regcache), regno), |
| 168 | regno, safe_strerror (errno)); |
| 169 | |
| 170 | regcache_raw_supply (regcache, regno, &val); |
| 171 | } |
| 172 | |
| 173 | /* Store one register. */ |
| 174 | |
| 175 | static void |
| 176 | store_register (const struct regcache *regcache, int regno) |
| 177 | { |
| 178 | int tid; |
| 179 | int val; |
| 180 | |
| 181 | gdb_assert (!have_ptrace_getregs); |
| 182 | if (i386_linux_gregset_reg_offset[regno] == -1) |
| 183 | return; |
| 184 | |
| 185 | /* GNU/Linux LWP ID's are process ID's. */ |
| 186 | tid = TIDGET (inferior_ptid); |
| 187 | if (tid == 0) |
| 188 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ |
| 189 | |
| 190 | errno = 0; |
| 191 | regcache_raw_collect (regcache, regno, &val); |
| 192 | ptrace (PTRACE_POKEUSER, tid, |
| 193 | i386_linux_gregset_reg_offset[regno], val); |
| 194 | if (errno != 0) |
| 195 | error (_("Couldn't write register %s (#%d): %s."), |
| 196 | gdbarch_register_name (get_regcache_arch (regcache), regno), |
| 197 | regno, safe_strerror (errno)); |
| 198 | } |
| 199 | \f |
| 200 | |
| 201 | /* Transfering the general-purpose registers between GDB, inferiors |
| 202 | and core files. */ |
| 203 | |
| 204 | /* Fill GDB's register array with the general-purpose register values |
| 205 | in *GREGSETP. */ |
| 206 | |
| 207 | void |
| 208 | supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp) |
| 209 | { |
| 210 | const gdb_byte *regp = (const gdb_byte *) gregsetp; |
| 211 | int i; |
| 212 | |
| 213 | for (i = 0; i < I386_NUM_GREGS; i++) |
| 214 | regcache_raw_supply (regcache, i, |
| 215 | regp + i386_linux_gregset_reg_offset[i]); |
| 216 | |
| 217 | if (I386_LINUX_ORIG_EAX_REGNUM |
| 218 | < gdbarch_num_regs (get_regcache_arch (regcache))) |
| 219 | regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp |
| 220 | + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]); |
| 221 | } |
| 222 | |
| 223 | /* Fill register REGNO (if it is a general-purpose register) in |
| 224 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, |
| 225 | do this for all registers. */ |
| 226 | |
| 227 | void |
| 228 | fill_gregset (const struct regcache *regcache, |
| 229 | elf_gregset_t *gregsetp, int regno) |
| 230 | { |
| 231 | gdb_byte *regp = (gdb_byte *) gregsetp; |
| 232 | int i; |
| 233 | |
| 234 | for (i = 0; i < I386_NUM_GREGS; i++) |
| 235 | if (regno == -1 || regno == i) |
| 236 | regcache_raw_collect (regcache, i, |
| 237 | regp + i386_linux_gregset_reg_offset[i]); |
| 238 | |
| 239 | if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM) |
| 240 | && I386_LINUX_ORIG_EAX_REGNUM |
| 241 | < gdbarch_num_regs (get_regcache_arch (regcache))) |
| 242 | regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp |
| 243 | + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]); |
| 244 | } |
| 245 | |
| 246 | #ifdef HAVE_PTRACE_GETREGS |
| 247 | |
| 248 | /* Fetch all general-purpose registers from process/thread TID and |
| 249 | store their values in GDB's register array. */ |
| 250 | |
| 251 | static void |
| 252 | fetch_regs (struct regcache *regcache, int tid) |
| 253 | { |
| 254 | elf_gregset_t regs; |
| 255 | elf_gregset_t *regs_p = ®s; |
| 256 | |
| 257 | if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) |
| 258 | { |
| 259 | if (errno == EIO) |
| 260 | { |
| 261 | /* The kernel we're running on doesn't support the GETREGS |
| 262 | request. Reset `have_ptrace_getregs'. */ |
| 263 | have_ptrace_getregs = 0; |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | perror_with_name (_("Couldn't get registers")); |
| 268 | } |
| 269 | |
| 270 | supply_gregset (regcache, (const elf_gregset_t *) regs_p); |
| 271 | } |
| 272 | |
| 273 | /* Store all valid general-purpose registers in GDB's register array |
| 274 | into the process/thread specified by TID. */ |
| 275 | |
| 276 | static void |
| 277 | store_regs (const struct regcache *regcache, int tid, int regno) |
| 278 | { |
| 279 | elf_gregset_t regs; |
| 280 | |
| 281 | if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) |
| 282 | perror_with_name (_("Couldn't get registers")); |
| 283 | |
| 284 | fill_gregset (regcache, ®s, regno); |
| 285 | |
| 286 | if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0) |
| 287 | perror_with_name (_("Couldn't write registers")); |
| 288 | } |
| 289 | |
| 290 | #else |
| 291 | |
| 292 | static void fetch_regs (struct regcache *regcache, int tid) {} |
| 293 | static void store_regs (const struct regcache *regcache, int tid, int regno) {} |
| 294 | |
| 295 | #endif |
| 296 | \f |
| 297 | |
| 298 | /* Transfering floating-point registers between GDB, inferiors and cores. */ |
| 299 | |
| 300 | /* Fill GDB's register array with the floating-point register values in |
| 301 | *FPREGSETP. */ |
| 302 | |
| 303 | void |
| 304 | supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp) |
| 305 | { |
| 306 | i387_supply_fsave (regcache, -1, fpregsetp); |
| 307 | } |
| 308 | |
| 309 | /* Fill register REGNO (if it is a floating-point register) in |
| 310 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, |
| 311 | do this for all registers. */ |
| 312 | |
| 313 | void |
| 314 | fill_fpregset (const struct regcache *regcache, |
| 315 | elf_fpregset_t *fpregsetp, int regno) |
| 316 | { |
| 317 | i387_collect_fsave (regcache, regno, fpregsetp); |
| 318 | } |
| 319 | |
| 320 | #ifdef HAVE_PTRACE_GETREGS |
| 321 | |
| 322 | /* Fetch all floating-point registers from process/thread TID and store |
| 323 | thier values in GDB's register array. */ |
| 324 | |
| 325 | static void |
| 326 | fetch_fpregs (struct regcache *regcache, int tid) |
| 327 | { |
| 328 | elf_fpregset_t fpregs; |
| 329 | |
| 330 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) |
| 331 | perror_with_name (_("Couldn't get floating point status")); |
| 332 | |
| 333 | supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs); |
| 334 | } |
| 335 | |
| 336 | /* Store all valid floating-point registers in GDB's register array |
| 337 | into the process/thread specified by TID. */ |
| 338 | |
| 339 | static void |
| 340 | store_fpregs (const struct regcache *regcache, int tid, int regno) |
| 341 | { |
| 342 | elf_fpregset_t fpregs; |
| 343 | |
| 344 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) |
| 345 | perror_with_name (_("Couldn't get floating point status")); |
| 346 | |
| 347 | fill_fpregset (regcache, &fpregs, regno); |
| 348 | |
| 349 | if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0) |
| 350 | perror_with_name (_("Couldn't write floating point status")); |
| 351 | } |
| 352 | |
| 353 | #else |
| 354 | |
| 355 | static void |
| 356 | fetch_fpregs (struct regcache *regcache, int tid) |
| 357 | { |
| 358 | } |
| 359 | |
| 360 | static void |
| 361 | store_fpregs (const struct regcache *regcache, int tid, int regno) |
| 362 | { |
| 363 | } |
| 364 | |
| 365 | #endif |
| 366 | \f |
| 367 | |
| 368 | /* Transfering floating-point and SSE registers to and from GDB. */ |
| 369 | |
| 370 | /* Fetch all registers covered by the PTRACE_GETREGSET request from |
| 371 | process/thread TID and store their values in GDB's register array. |
| 372 | Return non-zero if successful, zero otherwise. */ |
| 373 | |
| 374 | static int |
| 375 | fetch_xstateregs (struct regcache *regcache, int tid) |
| 376 | { |
| 377 | char xstateregs[I386_XSTATE_MAX_SIZE]; |
| 378 | struct iovec iov; |
| 379 | |
| 380 | if (!have_ptrace_getregset) |
| 381 | return 0; |
| 382 | |
| 383 | iov.iov_base = xstateregs; |
| 384 | iov.iov_len = sizeof(xstateregs); |
| 385 | if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, |
| 386 | &iov) < 0) |
| 387 | perror_with_name (_("Couldn't read extended state status")); |
| 388 | |
| 389 | i387_supply_xsave (regcache, -1, xstateregs); |
| 390 | return 1; |
| 391 | } |
| 392 | |
| 393 | /* Store all valid registers in GDB's register array covered by the |
| 394 | PTRACE_SETREGSET request into the process/thread specified by TID. |
| 395 | Return non-zero if successful, zero otherwise. */ |
| 396 | |
| 397 | static int |
| 398 | store_xstateregs (const struct regcache *regcache, int tid, int regno) |
| 399 | { |
| 400 | char xstateregs[I386_XSTATE_MAX_SIZE]; |
| 401 | struct iovec iov; |
| 402 | |
| 403 | if (!have_ptrace_getregset) |
| 404 | return 0; |
| 405 | |
| 406 | iov.iov_base = xstateregs; |
| 407 | iov.iov_len = sizeof(xstateregs); |
| 408 | if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, |
| 409 | &iov) < 0) |
| 410 | perror_with_name (_("Couldn't read extended state status")); |
| 411 | |
| 412 | i387_collect_xsave (regcache, regno, xstateregs, 0); |
| 413 | |
| 414 | if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE, |
| 415 | (int) &iov) < 0) |
| 416 | perror_with_name (_("Couldn't write extended state status")); |
| 417 | |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | #ifdef HAVE_PTRACE_GETFPXREGS |
| 422 | |
| 423 | /* Fetch all registers covered by the PTRACE_GETFPXREGS request from |
| 424 | process/thread TID and store their values in GDB's register array. |
| 425 | Return non-zero if successful, zero otherwise. */ |
| 426 | |
| 427 | static int |
| 428 | fetch_fpxregs (struct regcache *regcache, int tid) |
| 429 | { |
| 430 | elf_fpxregset_t fpxregs; |
| 431 | |
| 432 | if (! have_ptrace_getfpxregs) |
| 433 | return 0; |
| 434 | |
| 435 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) |
| 436 | { |
| 437 | if (errno == EIO) |
| 438 | { |
| 439 | have_ptrace_getfpxregs = 0; |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | perror_with_name (_("Couldn't read floating-point and SSE registers")); |
| 444 | } |
| 445 | |
| 446 | i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs); |
| 447 | return 1; |
| 448 | } |
| 449 | |
| 450 | /* Store all valid registers in GDB's register array covered by the |
| 451 | PTRACE_SETFPXREGS request into the process/thread specified by TID. |
| 452 | Return non-zero if successful, zero otherwise. */ |
| 453 | |
| 454 | static int |
| 455 | store_fpxregs (const struct regcache *regcache, int tid, int regno) |
| 456 | { |
| 457 | elf_fpxregset_t fpxregs; |
| 458 | |
| 459 | if (! have_ptrace_getfpxregs) |
| 460 | return 0; |
| 461 | |
| 462 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1) |
| 463 | { |
| 464 | if (errno == EIO) |
| 465 | { |
| 466 | have_ptrace_getfpxregs = 0; |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | perror_with_name (_("Couldn't read floating-point and SSE registers")); |
| 471 | } |
| 472 | |
| 473 | i387_collect_fxsave (regcache, regno, &fpxregs); |
| 474 | |
| 475 | if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1) |
| 476 | perror_with_name (_("Couldn't write floating-point and SSE registers")); |
| 477 | |
| 478 | return 1; |
| 479 | } |
| 480 | |
| 481 | #else |
| 482 | |
| 483 | static int |
| 484 | fetch_fpxregs (struct regcache *regcache, int tid) |
| 485 | { |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int |
| 490 | store_fpxregs (const struct regcache *regcache, int tid, int regno) |
| 491 | { |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | #endif /* HAVE_PTRACE_GETFPXREGS */ |
| 496 | \f |
| 497 | |
| 498 | /* Transferring arbitrary registers between GDB and inferior. */ |
| 499 | |
| 500 | /* Fetch register REGNO from the child process. If REGNO is -1, do |
| 501 | this for all registers (including the floating point and SSE |
| 502 | registers). */ |
| 503 | |
| 504 | static void |
| 505 | i386_linux_fetch_inferior_registers (struct target_ops *ops, |
| 506 | struct regcache *regcache, int regno) |
| 507 | { |
| 508 | int tid; |
| 509 | |
| 510 | /* Use the old method of peeking around in `struct user' if the |
| 511 | GETREGS request isn't available. */ |
| 512 | if (!have_ptrace_getregs) |
| 513 | { |
| 514 | int i; |
| 515 | |
| 516 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
| 517 | if (regno == -1 || regno == i) |
| 518 | fetch_register (regcache, i); |
| 519 | |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | /* GNU/Linux LWP ID's are process ID's. */ |
| 524 | tid = TIDGET (inferior_ptid); |
| 525 | if (tid == 0) |
| 526 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ |
| 527 | |
| 528 | /* Use the PTRACE_GETFPXREGS request whenever possible, since it |
| 529 | transfers more registers in one system call, and we'll cache the |
| 530 | results. But remember that fetch_fpxregs can fail, and return |
| 531 | zero. */ |
| 532 | if (regno == -1) |
| 533 | { |
| 534 | fetch_regs (regcache, tid); |
| 535 | |
| 536 | /* The call above might reset `have_ptrace_getregs'. */ |
| 537 | if (!have_ptrace_getregs) |
| 538 | { |
| 539 | i386_linux_fetch_inferior_registers (ops, regcache, regno); |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | if (fetch_xstateregs (regcache, tid)) |
| 544 | return; |
| 545 | if (fetch_fpxregs (regcache, tid)) |
| 546 | return; |
| 547 | fetch_fpregs (regcache, tid); |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | if (GETREGS_SUPPLIES (regno)) |
| 552 | { |
| 553 | fetch_regs (regcache, tid); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | if (GETXSTATEREGS_SUPPLIES (regno)) |
| 558 | { |
| 559 | if (fetch_xstateregs (regcache, tid)) |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | if (GETFPXREGS_SUPPLIES (regno)) |
| 564 | { |
| 565 | if (fetch_fpxregs (regcache, tid)) |
| 566 | return; |
| 567 | |
| 568 | /* Either our processor or our kernel doesn't support the SSE |
| 569 | registers, so read the FP registers in the traditional way, |
| 570 | and fill the SSE registers with dummy values. It would be |
| 571 | more graceful to handle differences in the register set using |
| 572 | gdbarch. Until then, this will at least make things work |
| 573 | plausibly. */ |
| 574 | fetch_fpregs (regcache, tid); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | internal_error (__FILE__, __LINE__, |
| 579 | _("Got request for bad register number %d."), regno); |
| 580 | } |
| 581 | |
| 582 | /* Store register REGNO back into the child process. If REGNO is -1, |
| 583 | do this for all registers (including the floating point and SSE |
| 584 | registers). */ |
| 585 | static void |
| 586 | i386_linux_store_inferior_registers (struct target_ops *ops, |
| 587 | struct regcache *regcache, 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 < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
| 598 | if (regno == -1 || regno == i) |
| 599 | store_register (regcache, 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 (regcache, tid, regno); |
| 615 | if (store_xstateregs (regcache, tid, regno)) |
| 616 | return; |
| 617 | if (store_fpxregs (regcache, tid, regno)) |
| 618 | return; |
| 619 | store_fpregs (regcache, tid, regno); |
| 620 | return; |
| 621 | } |
| 622 | |
| 623 | if (GETREGS_SUPPLIES (regno)) |
| 624 | { |
| 625 | store_regs (regcache, tid, regno); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | if (GETXSTATEREGS_SUPPLIES (regno)) |
| 630 | { |
| 631 | if (store_xstateregs (regcache, tid, regno)) |
| 632 | return; |
| 633 | } |
| 634 | |
| 635 | if (GETFPXREGS_SUPPLIES (regno)) |
| 636 | { |
| 637 | if (store_fpxregs (regcache, tid, regno)) |
| 638 | return; |
| 639 | |
| 640 | /* Either our processor or our kernel doesn't support the SSE |
| 641 | registers, so just write the FP registers in the traditional |
| 642 | way. */ |
| 643 | store_fpregs (regcache, tid, regno); |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | internal_error (__FILE__, __LINE__, |
| 648 | _("Got request to store bad register number %d."), regno); |
| 649 | } |
| 650 | \f |
| 651 | |
| 652 | /* Support for debug registers. */ |
| 653 | |
| 654 | static unsigned long i386_linux_dr[DR_CONTROL + 1]; |
| 655 | |
| 656 | /* Get debug register REGNUM value from only the one LWP of PTID. */ |
| 657 | |
| 658 | static unsigned long |
| 659 | i386_linux_dr_get (ptid_t ptid, int regnum) |
| 660 | { |
| 661 | int tid; |
| 662 | unsigned long value; |
| 663 | |
| 664 | tid = TIDGET (ptid); |
| 665 | if (tid == 0) |
| 666 | tid = PIDGET (ptid); |
| 667 | |
| 668 | /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the |
| 669 | ptrace call fails breaks debugging remote targets. The correct |
| 670 | way to fix this is to add the hardware breakpoint and watchpoint |
| 671 | stuff to the target vector. For now, just return zero if the |
| 672 | ptrace call fails. */ |
| 673 | errno = 0; |
| 674 | value = ptrace (PTRACE_PEEKUSER, tid, |
| 675 | offsetof (struct user, u_debugreg[regnum]), 0); |
| 676 | if (errno != 0) |
| 677 | #if 0 |
| 678 | perror_with_name (_("Couldn't read debug register")); |
| 679 | #else |
| 680 | return 0; |
| 681 | #endif |
| 682 | |
| 683 | return value; |
| 684 | } |
| 685 | |
| 686 | /* Set debug register REGNUM to VALUE in only the one LWP of PTID. */ |
| 687 | |
| 688 | static void |
| 689 | i386_linux_dr_set (ptid_t ptid, int regnum, unsigned long value) |
| 690 | { |
| 691 | int tid; |
| 692 | |
| 693 | tid = TIDGET (ptid); |
| 694 | if (tid == 0) |
| 695 | tid = PIDGET (ptid); |
| 696 | |
| 697 | errno = 0; |
| 698 | ptrace (PTRACE_POKEUSER, tid, |
| 699 | offsetof (struct user, u_debugreg[regnum]), value); |
| 700 | if (errno != 0) |
| 701 | perror_with_name (_("Couldn't write debug register")); |
| 702 | } |
| 703 | |
| 704 | /* Set DR_CONTROL to ADDR in all LWPs of LWP_LIST. */ |
| 705 | |
| 706 | static void |
| 707 | i386_linux_dr_set_control (unsigned long control) |
| 708 | { |
| 709 | struct lwp_info *lp; |
| 710 | ptid_t ptid; |
| 711 | |
| 712 | i386_linux_dr[DR_CONTROL] = control; |
| 713 | ALL_LWPS (lp, ptid) |
| 714 | i386_linux_dr_set (ptid, DR_CONTROL, control); |
| 715 | } |
| 716 | |
| 717 | /* Set address REGNUM (zero based) to ADDR in all LWPs of LWP_LIST. */ |
| 718 | |
| 719 | static void |
| 720 | i386_linux_dr_set_addr (int regnum, CORE_ADDR addr) |
| 721 | { |
| 722 | struct lwp_info *lp; |
| 723 | ptid_t ptid; |
| 724 | |
| 725 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); |
| 726 | |
| 727 | i386_linux_dr[DR_FIRSTADDR + regnum] = addr; |
| 728 | ALL_LWPS (lp, ptid) |
| 729 | i386_linux_dr_set (ptid, DR_FIRSTADDR + regnum, addr); |
| 730 | } |
| 731 | |
| 732 | /* Set address REGNUM (zero based) to zero in all LWPs of LWP_LIST. */ |
| 733 | |
| 734 | static void |
| 735 | i386_linux_dr_reset_addr (int regnum) |
| 736 | { |
| 737 | i386_linux_dr_set_addr (regnum, 0); |
| 738 | } |
| 739 | |
| 740 | /* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */ |
| 741 | |
| 742 | static unsigned long |
| 743 | i386_linux_dr_get_status (void) |
| 744 | { |
| 745 | return i386_linux_dr_get (inferior_ptid, DR_STATUS); |
| 746 | } |
| 747 | |
| 748 | /* Unset MASK bits in DR_STATUS in all LWPs of LWP_LIST. */ |
| 749 | |
| 750 | static void |
| 751 | i386_linux_dr_unset_status (unsigned long mask) |
| 752 | { |
| 753 | struct lwp_info *lp; |
| 754 | ptid_t ptid; |
| 755 | |
| 756 | ALL_LWPS (lp, ptid) |
| 757 | { |
| 758 | unsigned long value; |
| 759 | |
| 760 | value = i386_linux_dr_get (ptid, DR_STATUS); |
| 761 | value &= ~mask; |
| 762 | i386_linux_dr_set (ptid, DR_STATUS, value); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | static void |
| 767 | i386_linux_new_thread (ptid_t ptid) |
| 768 | { |
| 769 | int i; |
| 770 | |
| 771 | for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++) |
| 772 | i386_linux_dr_set (ptid, i, i386_linux_dr[i]); |
| 773 | |
| 774 | i386_linux_dr_set (ptid, DR_CONTROL, i386_linux_dr[DR_CONTROL]); |
| 775 | } |
| 776 | \f |
| 777 | |
| 778 | /* Called by libthread_db. Returns a pointer to the thread local |
| 779 | storage (or its descriptor). */ |
| 780 | |
| 781 | ps_err_e |
| 782 | ps_get_thread_area (const struct ps_prochandle *ph, |
| 783 | lwpid_t lwpid, int idx, void **base) |
| 784 | { |
| 785 | /* NOTE: cagney/2003-08-26: The definition of this buffer is found |
| 786 | in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x |
| 787 | 4 byte integers in size: `entry_number', `base_addr', `limit', |
| 788 | and a bunch of status bits. |
| 789 | |
| 790 | The values returned by this ptrace call should be part of the |
| 791 | regcache buffer, and ps_get_thread_area should channel its |
| 792 | request through the regcache. That way remote targets could |
| 793 | provide the value using the remote protocol and not this direct |
| 794 | call. |
| 795 | |
| 796 | Is this function needed? I'm guessing that the `base' is the |
| 797 | address of a descriptor that libthread_db uses to find the |
| 798 | thread local address base that GDB needs. Perhaps that |
| 799 | descriptor is defined by the ABI. Anyway, given that |
| 800 | libthread_db calls this function without prompting (gdb |
| 801 | requesting tls base) I guess it needs info in there anyway. */ |
| 802 | unsigned int desc[4]; |
| 803 | gdb_assert (sizeof (int) == 4); |
| 804 | |
| 805 | #ifndef PTRACE_GET_THREAD_AREA |
| 806 | #define PTRACE_GET_THREAD_AREA 25 |
| 807 | #endif |
| 808 | |
| 809 | if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, |
| 810 | (void *) idx, (unsigned long) &desc) < 0) |
| 811 | return PS_ERR; |
| 812 | |
| 813 | *(int *)base = desc[1]; |
| 814 | return PS_OK; |
| 815 | } |
| 816 | \f |
| 817 | |
| 818 | /* The instruction for a GNU/Linux system call is: |
| 819 | int $0x80 |
| 820 | or 0xcd 0x80. */ |
| 821 | |
| 822 | static const unsigned char linux_syscall[] = { 0xcd, 0x80 }; |
| 823 | |
| 824 | #define LINUX_SYSCALL_LEN (sizeof linux_syscall) |
| 825 | |
| 826 | /* The system call number is stored in the %eax register. */ |
| 827 | #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM |
| 828 | |
| 829 | /* We are specifically interested in the sigreturn and rt_sigreturn |
| 830 | system calls. */ |
| 831 | |
| 832 | #ifndef SYS_sigreturn |
| 833 | #define SYS_sigreturn 0x77 |
| 834 | #endif |
| 835 | #ifndef SYS_rt_sigreturn |
| 836 | #define SYS_rt_sigreturn 0xad |
| 837 | #endif |
| 838 | |
| 839 | /* Offset to saved processor flags, from <asm/sigcontext.h>. */ |
| 840 | #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64) |
| 841 | |
| 842 | /* Resume execution of the inferior process. |
| 843 | If STEP is nonzero, single-step it. |
| 844 | If SIGNAL is nonzero, give it that signal. */ |
| 845 | |
| 846 | static void |
| 847 | i386_linux_resume (struct target_ops *ops, |
| 848 | ptid_t ptid, int step, enum target_signal signal) |
| 849 | { |
| 850 | int pid = PIDGET (ptid); |
| 851 | |
| 852 | int request; |
| 853 | |
| 854 | if (catch_syscall_enabled () > 0) |
| 855 | request = PTRACE_SYSCALL; |
| 856 | else |
| 857 | request = PTRACE_CONT; |
| 858 | |
| 859 | if (step) |
| 860 | { |
| 861 | struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid)); |
| 862 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
| 863 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
| 864 | ULONGEST pc; |
| 865 | gdb_byte buf[LINUX_SYSCALL_LEN]; |
| 866 | |
| 867 | request = PTRACE_SINGLESTEP; |
| 868 | |
| 869 | regcache_cooked_read_unsigned (regcache, |
| 870 | gdbarch_pc_regnum (gdbarch), &pc); |
| 871 | |
| 872 | /* Returning from a signal trampoline is done by calling a |
| 873 | special system call (sigreturn or rt_sigreturn, see |
| 874 | i386-linux-tdep.c for more information). This system call |
| 875 | restores the registers that were saved when the signal was |
| 876 | raised, including %eflags. That means that single-stepping |
| 877 | won't work. Instead, we'll have to modify the signal context |
| 878 | that's about to be restored, and set the trace flag there. */ |
| 879 | |
| 880 | /* First check if PC is at a system call. */ |
| 881 | if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0 |
| 882 | && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0) |
| 883 | { |
| 884 | ULONGEST syscall; |
| 885 | regcache_cooked_read_unsigned (regcache, |
| 886 | LINUX_SYSCALL_REGNUM, &syscall); |
| 887 | |
| 888 | /* Then check the system call number. */ |
| 889 | if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn) |
| 890 | { |
| 891 | ULONGEST sp, addr; |
| 892 | unsigned long int eflags; |
| 893 | |
| 894 | regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp); |
| 895 | if (syscall == SYS_rt_sigreturn) |
| 896 | addr = read_memory_integer (sp + 8, 4, byte_order) + 20; |
| 897 | else |
| 898 | addr = sp; |
| 899 | |
| 900 | /* Set the trace flag in the context that's about to be |
| 901 | restored. */ |
| 902 | addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET; |
| 903 | read_memory (addr, (gdb_byte *) &eflags, 4); |
| 904 | eflags |= 0x0100; |
| 905 | write_memory (addr, (gdb_byte *) &eflags, 4); |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1) |
| 911 | perror_with_name (("ptrace")); |
| 912 | } |
| 913 | |
| 914 | static void (*super_post_startup_inferior) (ptid_t ptid); |
| 915 | |
| 916 | static void |
| 917 | i386_linux_child_post_startup_inferior (ptid_t ptid) |
| 918 | { |
| 919 | i386_cleanup_dregs (); |
| 920 | super_post_startup_inferior (ptid); |
| 921 | } |
| 922 | |
| 923 | /* Get Linux/x86 target description from running target. */ |
| 924 | |
| 925 | static const struct target_desc * |
| 926 | i386_linux_read_description (struct target_ops *ops) |
| 927 | { |
| 928 | int tid; |
| 929 | static uint64_t xcr0; |
| 930 | |
| 931 | /* GNU/Linux LWP ID's are process ID's. */ |
| 932 | tid = TIDGET (inferior_ptid); |
| 933 | if (tid == 0) |
| 934 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ |
| 935 | |
| 936 | #ifdef HAVE_PTRACE_GETFPXREGS |
| 937 | if (have_ptrace_getfpxregs == -1) |
| 938 | { |
| 939 | elf_fpxregset_t fpxregs; |
| 940 | |
| 941 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) |
| 942 | { |
| 943 | have_ptrace_getfpxregs = 0; |
| 944 | have_ptrace_getregset = 0; |
| 945 | return tdesc_i386_mmx_linux; |
| 946 | } |
| 947 | } |
| 948 | #endif |
| 949 | |
| 950 | if (have_ptrace_getregset == -1) |
| 951 | { |
| 952 | uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))]; |
| 953 | struct iovec iov; |
| 954 | |
| 955 | iov.iov_base = xstateregs; |
| 956 | iov.iov_len = sizeof (xstateregs); |
| 957 | |
| 958 | /* Check if PTRACE_GETREGSET works. */ |
| 959 | if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, |
| 960 | &iov) < 0) |
| 961 | have_ptrace_getregset = 0; |
| 962 | else |
| 963 | { |
| 964 | have_ptrace_getregset = 1; |
| 965 | |
| 966 | /* Get XCR0 from XSAVE extended state. */ |
| 967 | xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET |
| 968 | / sizeof (long long))]; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | /* Check the native XCR0 only if PTRACE_GETREGSET is available. */ |
| 973 | if (have_ptrace_getregset |
| 974 | && (xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK) |
| 975 | return tdesc_i386_avx_linux; |
| 976 | else |
| 977 | return tdesc_i386_linux; |
| 978 | } |
| 979 | |
| 980 | void |
| 981 | _initialize_i386_linux_nat (void) |
| 982 | { |
| 983 | struct target_ops *t; |
| 984 | |
| 985 | /* Fill in the generic GNU/Linux methods. */ |
| 986 | t = linux_target (); |
| 987 | |
| 988 | i386_use_watchpoints (t); |
| 989 | |
| 990 | i386_dr_low.set_control = i386_linux_dr_set_control; |
| 991 | i386_dr_low.set_addr = i386_linux_dr_set_addr; |
| 992 | i386_dr_low.reset_addr = i386_linux_dr_reset_addr; |
| 993 | i386_dr_low.get_status = i386_linux_dr_get_status; |
| 994 | i386_dr_low.unset_status = i386_linux_dr_unset_status; |
| 995 | i386_set_debug_register_length (4); |
| 996 | |
| 997 | /* Override the default ptrace resume method. */ |
| 998 | t->to_resume = i386_linux_resume; |
| 999 | |
| 1000 | /* Override the GNU/Linux inferior startup hook. */ |
| 1001 | super_post_startup_inferior = t->to_post_startup_inferior; |
| 1002 | t->to_post_startup_inferior = i386_linux_child_post_startup_inferior; |
| 1003 | |
| 1004 | /* Add our register access methods. */ |
| 1005 | t->to_fetch_registers = i386_linux_fetch_inferior_registers; |
| 1006 | t->to_store_registers = i386_linux_store_inferior_registers; |
| 1007 | |
| 1008 | t->to_read_description = i386_linux_read_description; |
| 1009 | |
| 1010 | /* Register the target. */ |
| 1011 | linux_nat_add_target (t); |
| 1012 | linux_nat_set_new_thread (t, i386_linux_new_thread); |
| 1013 | } |