| 1 | /* Native-dependent code for GNU/Linux i386. |
| 2 | |
| 3 | Copyright (C) 1999-2015 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 3 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, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "inferior.h" |
| 22 | #include "gdbcore.h" |
| 23 | #include "regcache.h" |
| 24 | #include "elf/common.h" |
| 25 | #include "nat/gdb_ptrace.h" |
| 26 | #include <sys/uio.h> |
| 27 | #include "gregset.h" |
| 28 | #include "gdb_proc_service.h" |
| 29 | |
| 30 | #include "i386-linux-nat.h" |
| 31 | #include "i387-tdep.h" |
| 32 | #include "i386-tdep.h" |
| 33 | #include "i386-linux-tdep.h" |
| 34 | #include "x86-xstate.h" |
| 35 | |
| 36 | #include "linux-nat.h" |
| 37 | #include "x86-linux-nat.h" |
| 38 | #include "nat/linux-ptrace.h" |
| 39 | |
| 40 | /* The register sets used in GNU/Linux ELF core-dumps are identical to |
| 41 | the register sets in `struct user' that is used for a.out |
| 42 | core-dumps, and is also used by `ptrace'. The corresponding types |
| 43 | are `elf_gregset_t' for the general-purpose registers (with |
| 44 | `elf_greg_t' the type of a single GP register) and `elf_fpregset_t' |
| 45 | for the floating-point registers. |
| 46 | |
| 47 | Those types used to be available under the names `gregset_t' and |
| 48 | `fpregset_t' too, and this file used those names in the past. But |
| 49 | those names are now used for the register sets used in the |
| 50 | `mcontext_t' type, and have a different size and layout. */ |
| 51 | |
| 52 | /* Which ptrace request retrieves which registers? |
| 53 | These apply to the corresponding SET requests as well. */ |
| 54 | |
| 55 | #define GETREGS_SUPPLIES(regno) \ |
| 56 | ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM) |
| 57 | |
| 58 | #define GETFPXREGS_SUPPLIES(regno) \ |
| 59 | (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS) |
| 60 | |
| 61 | #define GETXSTATEREGS_SUPPLIES(regno) \ |
| 62 | (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX512_NUM_REGS) |
| 63 | |
| 64 | /* Does the current host support the GETREGS request? */ |
| 65 | int have_ptrace_getregs = |
| 66 | #ifdef HAVE_PTRACE_GETREGS |
| 67 | 1 |
| 68 | #else |
| 69 | 0 |
| 70 | #endif |
| 71 | ; |
| 72 | |
| 73 | /* Does the current host support the GETFPXREGS request? The header |
| 74 | file may or may not define it, and even if it is defined, the |
| 75 | kernel will return EIO if it's running on a pre-SSE processor. |
| 76 | |
| 77 | My instinct is to attach this to some architecture- or |
| 78 | target-specific data structure, but really, a particular GDB |
| 79 | process can only run on top of one kernel at a time. So it's okay |
| 80 | for this to be a simple variable. */ |
| 81 | int have_ptrace_getfpxregs = |
| 82 | #ifdef HAVE_PTRACE_GETFPXREGS |
| 83 | -1 |
| 84 | #else |
| 85 | 0 |
| 86 | #endif |
| 87 | ; |
| 88 | \f |
| 89 | |
| 90 | /* Accessing registers through the U area, one at a time. */ |
| 91 | |
| 92 | /* Fetch one register. */ |
| 93 | |
| 94 | static void |
| 95 | fetch_register (struct regcache *regcache, int regno) |
| 96 | { |
| 97 | int tid; |
| 98 | int val; |
| 99 | |
| 100 | gdb_assert (!have_ptrace_getregs); |
| 101 | if (i386_linux_gregset_reg_offset[regno] == -1) |
| 102 | { |
| 103 | regcache_raw_supply (regcache, regno, NULL); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | /* GNU/Linux LWP ID's are process ID's. */ |
| 108 | tid = ptid_get_lwp (inferior_ptid); |
| 109 | if (tid == 0) |
| 110 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
| 111 | |
| 112 | errno = 0; |
| 113 | val = ptrace (PTRACE_PEEKUSER, tid, |
| 114 | i386_linux_gregset_reg_offset[regno], 0); |
| 115 | if (errno != 0) |
| 116 | error (_("Couldn't read register %s (#%d): %s."), |
| 117 | gdbarch_register_name (get_regcache_arch (regcache), regno), |
| 118 | regno, safe_strerror (errno)); |
| 119 | |
| 120 | regcache_raw_supply (regcache, regno, &val); |
| 121 | } |
| 122 | |
| 123 | /* Store one register. */ |
| 124 | |
| 125 | static void |
| 126 | store_register (const struct regcache *regcache, int regno) |
| 127 | { |
| 128 | int tid; |
| 129 | int val; |
| 130 | |
| 131 | gdb_assert (!have_ptrace_getregs); |
| 132 | if (i386_linux_gregset_reg_offset[regno] == -1) |
| 133 | return; |
| 134 | |
| 135 | /* GNU/Linux LWP ID's are process ID's. */ |
| 136 | tid = ptid_get_lwp (inferior_ptid); |
| 137 | if (tid == 0) |
| 138 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
| 139 | |
| 140 | errno = 0; |
| 141 | regcache_raw_collect (regcache, regno, &val); |
| 142 | ptrace (PTRACE_POKEUSER, tid, |
| 143 | i386_linux_gregset_reg_offset[regno], val); |
| 144 | if (errno != 0) |
| 145 | error (_("Couldn't write register %s (#%d): %s."), |
| 146 | gdbarch_register_name (get_regcache_arch (regcache), regno), |
| 147 | regno, safe_strerror (errno)); |
| 148 | } |
| 149 | \f |
| 150 | |
| 151 | /* Transfering the general-purpose registers between GDB, inferiors |
| 152 | and core files. */ |
| 153 | |
| 154 | /* Fill GDB's register array with the general-purpose register values |
| 155 | in *GREGSETP. */ |
| 156 | |
| 157 | void |
| 158 | supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp) |
| 159 | { |
| 160 | const gdb_byte *regp = (const gdb_byte *) gregsetp; |
| 161 | int i; |
| 162 | |
| 163 | for (i = 0; i < I386_NUM_GREGS; i++) |
| 164 | regcache_raw_supply (regcache, i, |
| 165 | regp + i386_linux_gregset_reg_offset[i]); |
| 166 | |
| 167 | if (I386_LINUX_ORIG_EAX_REGNUM |
| 168 | < gdbarch_num_regs (get_regcache_arch (regcache))) |
| 169 | regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp |
| 170 | + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]); |
| 171 | } |
| 172 | |
| 173 | /* Fill register REGNO (if it is a general-purpose register) in |
| 174 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, |
| 175 | do this for all registers. */ |
| 176 | |
| 177 | void |
| 178 | fill_gregset (const struct regcache *regcache, |
| 179 | elf_gregset_t *gregsetp, int regno) |
| 180 | { |
| 181 | gdb_byte *regp = (gdb_byte *) gregsetp; |
| 182 | int i; |
| 183 | |
| 184 | for (i = 0; i < I386_NUM_GREGS; i++) |
| 185 | if (regno == -1 || regno == i) |
| 186 | regcache_raw_collect (regcache, i, |
| 187 | regp + i386_linux_gregset_reg_offset[i]); |
| 188 | |
| 189 | if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM) |
| 190 | && I386_LINUX_ORIG_EAX_REGNUM |
| 191 | < gdbarch_num_regs (get_regcache_arch (regcache))) |
| 192 | regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp |
| 193 | + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]); |
| 194 | } |
| 195 | |
| 196 | #ifdef HAVE_PTRACE_GETREGS |
| 197 | |
| 198 | /* Fetch all general-purpose registers from process/thread TID and |
| 199 | store their values in GDB's register array. */ |
| 200 | |
| 201 | static void |
| 202 | fetch_regs (struct regcache *regcache, int tid) |
| 203 | { |
| 204 | elf_gregset_t regs; |
| 205 | elf_gregset_t *regs_p = ®s; |
| 206 | |
| 207 | if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) |
| 208 | { |
| 209 | if (errno == EIO) |
| 210 | { |
| 211 | /* The kernel we're running on doesn't support the GETREGS |
| 212 | request. Reset `have_ptrace_getregs'. */ |
| 213 | have_ptrace_getregs = 0; |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | perror_with_name (_("Couldn't get registers")); |
| 218 | } |
| 219 | |
| 220 | supply_gregset (regcache, (const elf_gregset_t *) regs_p); |
| 221 | } |
| 222 | |
| 223 | /* Store all valid general-purpose registers in GDB's register array |
| 224 | into the process/thread specified by TID. */ |
| 225 | |
| 226 | static void |
| 227 | store_regs (const struct regcache *regcache, int tid, int regno) |
| 228 | { |
| 229 | elf_gregset_t regs; |
| 230 | |
| 231 | if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) |
| 232 | perror_with_name (_("Couldn't get registers")); |
| 233 | |
| 234 | fill_gregset (regcache, ®s, regno); |
| 235 | |
| 236 | if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0) |
| 237 | perror_with_name (_("Couldn't write registers")); |
| 238 | } |
| 239 | |
| 240 | #else |
| 241 | |
| 242 | static void fetch_regs (struct regcache *regcache, int tid) {} |
| 243 | static void store_regs (const struct regcache *regcache, int tid, int regno) {} |
| 244 | |
| 245 | #endif |
| 246 | \f |
| 247 | |
| 248 | /* Transfering floating-point registers between GDB, inferiors and cores. */ |
| 249 | |
| 250 | /* Fill GDB's register array with the floating-point register values in |
| 251 | *FPREGSETP. */ |
| 252 | |
| 253 | void |
| 254 | supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp) |
| 255 | { |
| 256 | i387_supply_fsave (regcache, -1, fpregsetp); |
| 257 | } |
| 258 | |
| 259 | /* Fill register REGNO (if it is a floating-point register) in |
| 260 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, |
| 261 | do this for all registers. */ |
| 262 | |
| 263 | void |
| 264 | fill_fpregset (const struct regcache *regcache, |
| 265 | elf_fpregset_t *fpregsetp, int regno) |
| 266 | { |
| 267 | i387_collect_fsave (regcache, regno, fpregsetp); |
| 268 | } |
| 269 | |
| 270 | #ifdef HAVE_PTRACE_GETREGS |
| 271 | |
| 272 | /* Fetch all floating-point registers from process/thread TID and store |
| 273 | thier values in GDB's register array. */ |
| 274 | |
| 275 | static void |
| 276 | fetch_fpregs (struct regcache *regcache, int tid) |
| 277 | { |
| 278 | elf_fpregset_t fpregs; |
| 279 | |
| 280 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) |
| 281 | perror_with_name (_("Couldn't get floating point status")); |
| 282 | |
| 283 | supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs); |
| 284 | } |
| 285 | |
| 286 | /* Store all valid floating-point registers in GDB's register array |
| 287 | into the process/thread specified by TID. */ |
| 288 | |
| 289 | static void |
| 290 | store_fpregs (const struct regcache *regcache, int tid, int regno) |
| 291 | { |
| 292 | elf_fpregset_t fpregs; |
| 293 | |
| 294 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) |
| 295 | perror_with_name (_("Couldn't get floating point status")); |
| 296 | |
| 297 | fill_fpregset (regcache, &fpregs, regno); |
| 298 | |
| 299 | if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0) |
| 300 | perror_with_name (_("Couldn't write floating point status")); |
| 301 | } |
| 302 | |
| 303 | #else |
| 304 | |
| 305 | static void |
| 306 | fetch_fpregs (struct regcache *regcache, int tid) |
| 307 | { |
| 308 | } |
| 309 | |
| 310 | static void |
| 311 | store_fpregs (const struct regcache *regcache, int tid, int regno) |
| 312 | { |
| 313 | } |
| 314 | |
| 315 | #endif |
| 316 | \f |
| 317 | |
| 318 | /* Transfering floating-point and SSE registers to and from GDB. */ |
| 319 | |
| 320 | /* Fetch all registers covered by the PTRACE_GETREGSET request from |
| 321 | process/thread TID and store their values in GDB's register array. |
| 322 | Return non-zero if successful, zero otherwise. */ |
| 323 | |
| 324 | static int |
| 325 | fetch_xstateregs (struct regcache *regcache, int tid) |
| 326 | { |
| 327 | char xstateregs[X86_XSTATE_MAX_SIZE]; |
| 328 | struct iovec iov; |
| 329 | |
| 330 | if (have_ptrace_getregset != TRIBOOL_TRUE) |
| 331 | return 0; |
| 332 | |
| 333 | iov.iov_base = xstateregs; |
| 334 | iov.iov_len = sizeof(xstateregs); |
| 335 | if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, |
| 336 | &iov) < 0) |
| 337 | perror_with_name (_("Couldn't read extended state status")); |
| 338 | |
| 339 | i387_supply_xsave (regcache, -1, xstateregs); |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | /* Store all valid registers in GDB's register array covered by the |
| 344 | PTRACE_SETREGSET request into the process/thread specified by TID. |
| 345 | Return non-zero if successful, zero otherwise. */ |
| 346 | |
| 347 | static int |
| 348 | store_xstateregs (const struct regcache *regcache, int tid, int regno) |
| 349 | { |
| 350 | char xstateregs[X86_XSTATE_MAX_SIZE]; |
| 351 | struct iovec iov; |
| 352 | |
| 353 | if (have_ptrace_getregset != TRIBOOL_TRUE) |
| 354 | return 0; |
| 355 | |
| 356 | iov.iov_base = xstateregs; |
| 357 | iov.iov_len = sizeof(xstateregs); |
| 358 | if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, |
| 359 | &iov) < 0) |
| 360 | perror_with_name (_("Couldn't read extended state status")); |
| 361 | |
| 362 | i387_collect_xsave (regcache, regno, xstateregs, 0); |
| 363 | |
| 364 | if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE, |
| 365 | (int) &iov) < 0) |
| 366 | perror_with_name (_("Couldn't write extended state status")); |
| 367 | |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | #ifdef HAVE_PTRACE_GETFPXREGS |
| 372 | |
| 373 | /* Fetch all registers covered by the PTRACE_GETFPXREGS request from |
| 374 | process/thread TID and store their values in GDB's register array. |
| 375 | Return non-zero if successful, zero otherwise. */ |
| 376 | |
| 377 | static int |
| 378 | fetch_fpxregs (struct regcache *regcache, int tid) |
| 379 | { |
| 380 | elf_fpxregset_t fpxregs; |
| 381 | |
| 382 | if (! have_ptrace_getfpxregs) |
| 383 | return 0; |
| 384 | |
| 385 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) |
| 386 | { |
| 387 | if (errno == EIO) |
| 388 | { |
| 389 | have_ptrace_getfpxregs = 0; |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | perror_with_name (_("Couldn't read floating-point and SSE registers")); |
| 394 | } |
| 395 | |
| 396 | i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs); |
| 397 | return 1; |
| 398 | } |
| 399 | |
| 400 | /* Store all valid registers in GDB's register array covered by the |
| 401 | PTRACE_SETFPXREGS request into the process/thread specified by TID. |
| 402 | Return non-zero if successful, zero otherwise. */ |
| 403 | |
| 404 | static int |
| 405 | store_fpxregs (const struct regcache *regcache, int tid, int regno) |
| 406 | { |
| 407 | elf_fpxregset_t fpxregs; |
| 408 | |
| 409 | if (! have_ptrace_getfpxregs) |
| 410 | return 0; |
| 411 | |
| 412 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1) |
| 413 | { |
| 414 | if (errno == EIO) |
| 415 | { |
| 416 | have_ptrace_getfpxregs = 0; |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | perror_with_name (_("Couldn't read floating-point and SSE registers")); |
| 421 | } |
| 422 | |
| 423 | i387_collect_fxsave (regcache, regno, &fpxregs); |
| 424 | |
| 425 | if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1) |
| 426 | perror_with_name (_("Couldn't write floating-point and SSE registers")); |
| 427 | |
| 428 | return 1; |
| 429 | } |
| 430 | |
| 431 | #else |
| 432 | |
| 433 | static int |
| 434 | fetch_fpxregs (struct regcache *regcache, int tid) |
| 435 | { |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static int |
| 440 | store_fpxregs (const struct regcache *regcache, int tid, int regno) |
| 441 | { |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | #endif /* HAVE_PTRACE_GETFPXREGS */ |
| 446 | \f |
| 447 | |
| 448 | /* Transferring arbitrary registers between GDB and inferior. */ |
| 449 | |
| 450 | /* Fetch register REGNO from the child process. If REGNO is -1, do |
| 451 | this for all registers (including the floating point and SSE |
| 452 | registers). */ |
| 453 | |
| 454 | static void |
| 455 | i386_linux_fetch_inferior_registers (struct target_ops *ops, |
| 456 | struct regcache *regcache, int regno) |
| 457 | { |
| 458 | int tid; |
| 459 | |
| 460 | /* Use the old method of peeking around in `struct user' if the |
| 461 | GETREGS request isn't available. */ |
| 462 | if (!have_ptrace_getregs) |
| 463 | { |
| 464 | int i; |
| 465 | |
| 466 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
| 467 | if (regno == -1 || regno == i) |
| 468 | fetch_register (regcache, i); |
| 469 | |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | /* GNU/Linux LWP ID's are process ID's. */ |
| 474 | tid = ptid_get_lwp (inferior_ptid); |
| 475 | if (tid == 0) |
| 476 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
| 477 | |
| 478 | /* Use the PTRACE_GETFPXREGS request whenever possible, since it |
| 479 | transfers more registers in one system call, and we'll cache the |
| 480 | results. But remember that fetch_fpxregs can fail, and return |
| 481 | zero. */ |
| 482 | if (regno == -1) |
| 483 | { |
| 484 | fetch_regs (regcache, tid); |
| 485 | |
| 486 | /* The call above might reset `have_ptrace_getregs'. */ |
| 487 | if (!have_ptrace_getregs) |
| 488 | { |
| 489 | i386_linux_fetch_inferior_registers (ops, regcache, regno); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | if (fetch_xstateregs (regcache, tid)) |
| 494 | return; |
| 495 | if (fetch_fpxregs (regcache, tid)) |
| 496 | return; |
| 497 | fetch_fpregs (regcache, tid); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | if (GETREGS_SUPPLIES (regno)) |
| 502 | { |
| 503 | fetch_regs (regcache, tid); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | if (GETXSTATEREGS_SUPPLIES (regno)) |
| 508 | { |
| 509 | if (fetch_xstateregs (regcache, tid)) |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | if (GETFPXREGS_SUPPLIES (regno)) |
| 514 | { |
| 515 | if (fetch_fpxregs (regcache, tid)) |
| 516 | return; |
| 517 | |
| 518 | /* Either our processor or our kernel doesn't support the SSE |
| 519 | registers, so read the FP registers in the traditional way, |
| 520 | and fill the SSE registers with dummy values. It would be |
| 521 | more graceful to handle differences in the register set using |
| 522 | gdbarch. Until then, this will at least make things work |
| 523 | plausibly. */ |
| 524 | fetch_fpregs (regcache, tid); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | internal_error (__FILE__, __LINE__, |
| 529 | _("Got request for bad register number %d."), regno); |
| 530 | } |
| 531 | |
| 532 | /* Store register REGNO back into the child process. If REGNO is -1, |
| 533 | do this for all registers (including the floating point and SSE |
| 534 | registers). */ |
| 535 | static void |
| 536 | i386_linux_store_inferior_registers (struct target_ops *ops, |
| 537 | struct regcache *regcache, int regno) |
| 538 | { |
| 539 | int tid; |
| 540 | |
| 541 | /* Use the old method of poking around in `struct user' if the |
| 542 | SETREGS request isn't available. */ |
| 543 | if (!have_ptrace_getregs) |
| 544 | { |
| 545 | int i; |
| 546 | |
| 547 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
| 548 | if (regno == -1 || regno == i) |
| 549 | store_register (regcache, i); |
| 550 | |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | /* GNU/Linux LWP ID's are process ID's. */ |
| 555 | tid = ptid_get_lwp (inferior_ptid); |
| 556 | if (tid == 0) |
| 557 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
| 558 | |
| 559 | /* Use the PTRACE_SETFPXREGS requests whenever possible, since it |
| 560 | transfers more registers in one system call. But remember that |
| 561 | store_fpxregs can fail, and return zero. */ |
| 562 | if (regno == -1) |
| 563 | { |
| 564 | store_regs (regcache, tid, regno); |
| 565 | if (store_xstateregs (regcache, tid, regno)) |
| 566 | return; |
| 567 | if (store_fpxregs (regcache, tid, regno)) |
| 568 | return; |
| 569 | store_fpregs (regcache, tid, regno); |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | if (GETREGS_SUPPLIES (regno)) |
| 574 | { |
| 575 | store_regs (regcache, tid, regno); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | if (GETXSTATEREGS_SUPPLIES (regno)) |
| 580 | { |
| 581 | if (store_xstateregs (regcache, tid, regno)) |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | if (GETFPXREGS_SUPPLIES (regno)) |
| 586 | { |
| 587 | if (store_fpxregs (regcache, tid, regno)) |
| 588 | return; |
| 589 | |
| 590 | /* Either our processor or our kernel doesn't support the SSE |
| 591 | registers, so just write the FP registers in the traditional |
| 592 | way. */ |
| 593 | store_fpregs (regcache, tid, regno); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | internal_error (__FILE__, __LINE__, |
| 598 | _("Got request to store bad register number %d."), regno); |
| 599 | } |
| 600 | \f |
| 601 | |
| 602 | /* Called by libthread_db. Returns a pointer to the thread local |
| 603 | storage (or its descriptor). */ |
| 604 | |
| 605 | ps_err_e |
| 606 | ps_get_thread_area (const struct ps_prochandle *ph, |
| 607 | lwpid_t lwpid, int idx, void **base) |
| 608 | { |
| 609 | unsigned int base_addr; |
| 610 | ps_err_e result; |
| 611 | |
| 612 | result = x86_linux_get_thread_area (lwpid, (void *) idx, &base_addr); |
| 613 | |
| 614 | if (result == PS_OK) |
| 615 | *(int *) base = base_addr; |
| 616 | |
| 617 | return result; |
| 618 | } |
| 619 | \f |
| 620 | |
| 621 | /* The instruction for a GNU/Linux system call is: |
| 622 | int $0x80 |
| 623 | or 0xcd 0x80. */ |
| 624 | |
| 625 | static const unsigned char linux_syscall[] = { 0xcd, 0x80 }; |
| 626 | |
| 627 | #define LINUX_SYSCALL_LEN (sizeof linux_syscall) |
| 628 | |
| 629 | /* The system call number is stored in the %eax register. */ |
| 630 | #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM |
| 631 | |
| 632 | /* We are specifically interested in the sigreturn and rt_sigreturn |
| 633 | system calls. */ |
| 634 | |
| 635 | #ifndef SYS_sigreturn |
| 636 | #define SYS_sigreturn 0x77 |
| 637 | #endif |
| 638 | #ifndef SYS_rt_sigreturn |
| 639 | #define SYS_rt_sigreturn 0xad |
| 640 | #endif |
| 641 | |
| 642 | /* Offset to saved processor flags, from <asm/sigcontext.h>. */ |
| 643 | #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64) |
| 644 | |
| 645 | /* Resume execution of the inferior process. |
| 646 | If STEP is nonzero, single-step it. |
| 647 | If SIGNAL is nonzero, give it that signal. */ |
| 648 | |
| 649 | static void |
| 650 | i386_linux_resume (struct target_ops *ops, |
| 651 | ptid_t ptid, int step, enum gdb_signal signal) |
| 652 | { |
| 653 | int pid = ptid_get_lwp (ptid); |
| 654 | int request; |
| 655 | |
| 656 | if (catch_syscall_enabled () > 0) |
| 657 | request = PTRACE_SYSCALL; |
| 658 | else |
| 659 | request = PTRACE_CONT; |
| 660 | |
| 661 | if (step) |
| 662 | { |
| 663 | struct regcache *regcache = get_thread_regcache (ptid); |
| 664 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
| 665 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
| 666 | ULONGEST pc; |
| 667 | gdb_byte buf[LINUX_SYSCALL_LEN]; |
| 668 | |
| 669 | request = PTRACE_SINGLESTEP; |
| 670 | |
| 671 | regcache_cooked_read_unsigned (regcache, |
| 672 | gdbarch_pc_regnum (gdbarch), &pc); |
| 673 | |
| 674 | /* Returning from a signal trampoline is done by calling a |
| 675 | special system call (sigreturn or rt_sigreturn, see |
| 676 | i386-linux-tdep.c for more information). This system call |
| 677 | restores the registers that were saved when the signal was |
| 678 | raised, including %eflags. That means that single-stepping |
| 679 | won't work. Instead, we'll have to modify the signal context |
| 680 | that's about to be restored, and set the trace flag there. */ |
| 681 | |
| 682 | /* First check if PC is at a system call. */ |
| 683 | if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0 |
| 684 | && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0) |
| 685 | { |
| 686 | ULONGEST syscall; |
| 687 | regcache_cooked_read_unsigned (regcache, |
| 688 | LINUX_SYSCALL_REGNUM, &syscall); |
| 689 | |
| 690 | /* Then check the system call number. */ |
| 691 | if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn) |
| 692 | { |
| 693 | ULONGEST sp, addr; |
| 694 | unsigned long int eflags; |
| 695 | |
| 696 | regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp); |
| 697 | if (syscall == SYS_rt_sigreturn) |
| 698 | addr = read_memory_unsigned_integer (sp + 8, 4, byte_order) |
| 699 | + 20; |
| 700 | else |
| 701 | addr = sp; |
| 702 | |
| 703 | /* Set the trace flag in the context that's about to be |
| 704 | restored. */ |
| 705 | addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET; |
| 706 | read_memory (addr, (gdb_byte *) &eflags, 4); |
| 707 | eflags |= 0x0100; |
| 708 | write_memory (addr, (gdb_byte *) &eflags, 4); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1) |
| 714 | perror_with_name (("ptrace")); |
| 715 | } |
| 716 | \f |
| 717 | |
| 718 | /* -Wmissing-prototypes */ |
| 719 | extern initialize_file_ftype _initialize_i386_linux_nat; |
| 720 | |
| 721 | void |
| 722 | _initialize_i386_linux_nat (void) |
| 723 | { |
| 724 | /* Create a generic x86 GNU/Linux target. */ |
| 725 | struct target_ops *t = x86_linux_create_target (); |
| 726 | |
| 727 | /* Override the default ptrace resume method. */ |
| 728 | t->to_resume = i386_linux_resume; |
| 729 | |
| 730 | /* Add our register access methods. */ |
| 731 | t->to_fetch_registers = i386_linux_fetch_inferior_registers; |
| 732 | t->to_store_registers = i386_linux_store_inferior_registers; |
| 733 | |
| 734 | /* Add the target. */ |
| 735 | x86_linux_add_target (t); |
| 736 | } |