| 1 | /* Generic GNU/Linux target using traditional ptrace register access. |
| 2 | |
| 3 | Copyright (C) 1988-2019 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 "linux-nat-trad.h" |
| 22 | |
| 23 | #include "nat/gdb_ptrace.h" |
| 24 | #include "inf-ptrace.h" |
| 25 | |
| 26 | /* Fetch register REGNUM from the inferior. */ |
| 27 | |
| 28 | void |
| 29 | linux_nat_trad_target::fetch_register (struct regcache *regcache, int regnum) |
| 30 | { |
| 31 | struct gdbarch *gdbarch = regcache->arch (); |
| 32 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
| 33 | CORE_ADDR addr; |
| 34 | gdb_byte *buf; |
| 35 | size_t size; |
| 36 | pid_t pid; |
| 37 | int i; |
| 38 | |
| 39 | /* This isn't really an address, but ptrace thinks of it as one. */ |
| 40 | addr = register_u_offset (gdbarch, regnum, 0); |
| 41 | if (addr == (CORE_ADDR)-1 |
| 42 | || gdbarch_cannot_fetch_register (gdbarch, regnum)) |
| 43 | { |
| 44 | regcache->raw_supply (regnum, NULL); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | pid = get_ptrace_pid (regcache->ptid ()); |
| 49 | |
| 50 | size = register_size (gdbarch, regnum); |
| 51 | buf = (gdb_byte *) alloca (size); |
| 52 | |
| 53 | /* Read the register contents from the inferior a chunk at a time. */ |
| 54 | for (i = 0; i < size; i += sizeof (PTRACE_TYPE_RET)) |
| 55 | { |
| 56 | size_t chunk = std::min (sizeof (PTRACE_TYPE_RET), size - i); |
| 57 | PTRACE_TYPE_RET val; |
| 58 | |
| 59 | errno = 0; |
| 60 | val = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3) (uintptr_t) addr, 0); |
| 61 | if (errno != 0) |
| 62 | error (_("Couldn't read register %s (#%d): %s."), |
| 63 | gdbarch_register_name (gdbarch, regnum), |
| 64 | regnum, safe_strerror (errno)); |
| 65 | store_unsigned_integer (buf + i, chunk, byte_order, val); |
| 66 | |
| 67 | addr += sizeof (PTRACE_TYPE_RET); |
| 68 | } |
| 69 | regcache->raw_supply (regnum, buf); |
| 70 | } |
| 71 | |
| 72 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this |
| 73 | for all registers. */ |
| 74 | |
| 75 | void |
| 76 | linux_nat_trad_target::fetch_registers (struct regcache *regcache, int regnum) |
| 77 | { |
| 78 | if (regnum == -1) |
| 79 | for (regnum = 0; |
| 80 | regnum < gdbarch_num_regs (regcache->arch ()); |
| 81 | regnum++) |
| 82 | fetch_register (regcache, regnum); |
| 83 | else |
| 84 | fetch_register (regcache, regnum); |
| 85 | } |
| 86 | |
| 87 | /* Store register REGNUM into the inferior. */ |
| 88 | |
| 89 | void |
| 90 | linux_nat_trad_target::store_register (const struct regcache *regcache, |
| 91 | int regnum) |
| 92 | { |
| 93 | struct gdbarch *gdbarch = regcache->arch (); |
| 94 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
| 95 | CORE_ADDR addr; |
| 96 | size_t size; |
| 97 | gdb_byte *buf; |
| 98 | pid_t pid; |
| 99 | int i; |
| 100 | |
| 101 | /* This isn't really an address, but ptrace thinks of it as one. */ |
| 102 | addr = register_u_offset (gdbarch, regnum, 1); |
| 103 | if (addr == (CORE_ADDR)-1 |
| 104 | || gdbarch_cannot_store_register (gdbarch, regnum)) |
| 105 | return; |
| 106 | |
| 107 | pid = get_ptrace_pid (regcache->ptid ()); |
| 108 | |
| 109 | size = register_size (gdbarch, regnum); |
| 110 | buf = (gdb_byte *) alloca (size); |
| 111 | |
| 112 | /* Write the register contents into the inferior a chunk at a time. */ |
| 113 | regcache->raw_collect (regnum, buf); |
| 114 | for (i = 0; i < size; i += sizeof (PTRACE_TYPE_RET)) |
| 115 | { |
| 116 | size_t chunk = std::min (sizeof (PTRACE_TYPE_RET), size - i); |
| 117 | PTRACE_TYPE_RET val; |
| 118 | |
| 119 | val = extract_unsigned_integer (buf + i, chunk, byte_order); |
| 120 | errno = 0; |
| 121 | ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3) (uintptr_t) addr, val); |
| 122 | if (errno != 0) |
| 123 | error (_("Couldn't write register %s (#%d): %s."), |
| 124 | gdbarch_register_name (gdbarch, regnum), |
| 125 | regnum, safe_strerror (errno)); |
| 126 | |
| 127 | addr += sizeof (PTRACE_TYPE_RET); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do |
| 132 | this for all registers. */ |
| 133 | |
| 134 | void |
| 135 | linux_nat_trad_target::store_registers (struct regcache *regcache, int regnum) |
| 136 | { |
| 137 | if (regnum == -1) |
| 138 | for (regnum = 0; |
| 139 | regnum < gdbarch_num_regs (regcache->arch ()); |
| 140 | regnum++) |
| 141 | store_register (regcache, regnum); |
| 142 | else |
| 143 | store_register (regcache, regnum); |
| 144 | } |