| 1 | /* Native-dependent code for PA-RISC HP-UX. |
| 2 | |
| 3 | Copyright (C) 2004, 2005, 2007 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., 51 Franklin Street, Fifth Floor, |
| 20 | Boston, MA 02110-1301, USA. */ |
| 21 | |
| 22 | #include "defs.h" |
| 23 | #include "inferior.h" |
| 24 | #include "regcache.h" |
| 25 | #include "target.h" |
| 26 | |
| 27 | #include "gdb_assert.h" |
| 28 | #include <sys/ptrace.h> |
| 29 | #include <machine/save_state.h> |
| 30 | |
| 31 | #ifdef HAVE_TTRACE |
| 32 | #include <sys/ttrace.h> |
| 33 | #endif |
| 34 | |
| 35 | #include "hppa-tdep.h" |
| 36 | #include "inf-ptrace.h" |
| 37 | #include "inf-ttrace.h" |
| 38 | |
| 39 | /* Non-zero if we should pretend not to be a runnable target. */ |
| 40 | int child_suppress_run = 0; |
| 41 | |
| 42 | /* Return the offset of register REGNUM within `struct save_state'. |
| 43 | The offset returns depends on the flags in the "flags" register and |
| 44 | the register size (32-bit or 64-bit). These are taken from |
| 45 | REGCACHE. */ |
| 46 | |
| 47 | LONGEST |
| 48 | hppa_hpux_save_state_offset (struct regcache *regcache, int regnum) |
| 49 | { |
| 50 | LONGEST offset; |
| 51 | |
| 52 | if (regnum == HPPA_FLAGS_REGNUM) |
| 53 | return ssoff (ss_flags); |
| 54 | |
| 55 | if (HPPA_R0_REGNUM < regnum && regnum < HPPA_FP0_REGNUM) |
| 56 | { |
| 57 | struct gdbarch *arch = get_regcache_arch (regcache); |
| 58 | size_t size = register_size (arch, HPPA_R1_REGNUM); |
| 59 | ULONGEST flags; |
| 60 | |
| 61 | gdb_assert (size == 4 || size == 8); |
| 62 | |
| 63 | regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags); |
| 64 | if (flags & SS_WIDEREGS) |
| 65 | offset = ssoff (ss_wide) + (8 - size) + (regnum - HPPA_R0_REGNUM) * 8; |
| 66 | else |
| 67 | offset = ssoff (ss_narrow) + (regnum - HPPA_R1_REGNUM) * 4; |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | struct gdbarch *arch = get_regcache_arch (regcache); |
| 72 | size_t size = register_size (arch, HPPA_FP0_REGNUM); |
| 73 | |
| 74 | gdb_assert (size == 4 || size == 8); |
| 75 | gdb_assert (regnum >= HPPA_FP0_REGNUM); |
| 76 | offset = ssoff(ss_fpblock) + (regnum - HPPA_FP0_REGNUM) * size; |
| 77 | } |
| 78 | |
| 79 | gdb_assert (offset < sizeof (save_state_t)); |
| 80 | return offset; |
| 81 | } |
| 82 | |
| 83 | /* Just in case a future version of PA-RISC HP-UX won't have ptrace(2) |
| 84 | at all. */ |
| 85 | #ifndef PTRACE_TYPE_RET |
| 86 | #define PTRACE_TYPE_RET void |
| 87 | #endif |
| 88 | |
| 89 | static void |
| 90 | hppa_hpux_fetch_register (struct regcache *regcache, int regnum) |
| 91 | { |
| 92 | CORE_ADDR addr; |
| 93 | size_t size; |
| 94 | PTRACE_TYPE_RET *buf; |
| 95 | pid_t pid; |
| 96 | int i; |
| 97 | |
| 98 | pid = ptid_get_pid (inferior_ptid); |
| 99 | |
| 100 | /* This isn't really an address, but ptrace thinks of it as one. */ |
| 101 | addr = hppa_hpux_save_state_offset (regcache, regnum); |
| 102 | size = register_size (current_gdbarch, regnum); |
| 103 | |
| 104 | gdb_assert (size == 4 || size == 8); |
| 105 | buf = alloca (size); |
| 106 | |
| 107 | #ifdef HAVE_TTRACE |
| 108 | { |
| 109 | lwpid_t lwp = ptid_get_lwp (inferior_ptid); |
| 110 | |
| 111 | if (ttrace (TT_LWP_RUREGS, pid, lwp, addr, size, (uintptr_t)buf) == -1) |
| 112 | error (_("Couldn't read register %s (#%d): %s"), |
| 113 | gdbarch_register_name (current_gdbarch, regnum), |
| 114 | regnum, safe_strerror (errno)); |
| 115 | } |
| 116 | #else |
| 117 | { |
| 118 | int i; |
| 119 | |
| 120 | /* Read the register contents from the inferior a chuck at the time. */ |
| 121 | for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++) |
| 122 | { |
| 123 | errno = 0; |
| 124 | buf[i] = ptrace (PT_RUREGS, pid, (PTRACE_TYPE_ARG3) addr, 0, 0); |
| 125 | if (errno != 0) |
| 126 | error (_("Couldn't read register %s (#%d): %s"), |
| 127 | gdbarch_register_name (current_gdbarch, regnum), |
| 128 | regnum, safe_strerror (errno)); |
| 129 | |
| 130 | addr += sizeof (PTRACE_TYPE_RET); |
| 131 | } |
| 132 | } |
| 133 | #endif |
| 134 | |
| 135 | /* Take care with the "flags" register. It's stored as an `int' in |
| 136 | `struct save_state', even for 64-bit code. */ |
| 137 | if (regnum == HPPA_FLAGS_REGNUM && size == 8) |
| 138 | { |
| 139 | ULONGEST flags = extract_unsigned_integer ((gdb_byte *)buf, 4); |
| 140 | store_unsigned_integer ((gdb_byte *)buf, 8, flags); |
| 141 | } |
| 142 | |
| 143 | regcache_raw_supply (regcache, regnum, buf); |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | hppa_hpux_fetch_inferior_registers (struct regcache *regcache, int regnum) |
| 148 | { |
| 149 | if (regnum == -1) |
| 150 | for (regnum = 0; regnum < gdbarch_num_regs (current_gdbarch); regnum++) |
| 151 | hppa_hpux_fetch_register (regcache, regnum); |
| 152 | else |
| 153 | hppa_hpux_fetch_register (regcache, regnum); |
| 154 | } |
| 155 | |
| 156 | /* Store register REGNUM into the inferior. */ |
| 157 | |
| 158 | static void |
| 159 | hppa_hpux_store_register (struct regcache *regcache, int regnum) |
| 160 | { |
| 161 | CORE_ADDR addr; |
| 162 | size_t size; |
| 163 | PTRACE_TYPE_RET *buf; |
| 164 | pid_t pid; |
| 165 | |
| 166 | pid = ptid_get_pid (inferior_ptid); |
| 167 | |
| 168 | /* This isn't really an address, but ptrace thinks of it as one. */ |
| 169 | addr = hppa_hpux_save_state_offset (regcache, regnum); |
| 170 | size = register_size (current_gdbarch, regnum); |
| 171 | |
| 172 | gdb_assert (size == 4 || size == 8); |
| 173 | buf = alloca (size); |
| 174 | |
| 175 | regcache_raw_collect (regcache, regnum, buf); |
| 176 | |
| 177 | /* Take care with the "flags" register. It's stored as an `int' in |
| 178 | `struct save_state', even for 64-bit code. */ |
| 179 | if (regnum == HPPA_FLAGS_REGNUM && size == 8) |
| 180 | { |
| 181 | ULONGEST flags = extract_unsigned_integer ((gdb_byte *)buf, 8); |
| 182 | store_unsigned_integer ((gdb_byte *)buf, 4, flags); |
| 183 | size = 4; |
| 184 | } |
| 185 | |
| 186 | #ifdef HAVE_TTRACE |
| 187 | { |
| 188 | lwpid_t lwp = ptid_get_lwp (inferior_ptid); |
| 189 | |
| 190 | if (ttrace (TT_LWP_WUREGS, pid, lwp, addr, size, (uintptr_t)buf) == -1) |
| 191 | error (_("Couldn't write register %s (#%d): %s"), |
| 192 | gdbarch_register_name (current_gdbarch, regnum), |
| 193 | regnum, safe_strerror (errno)); |
| 194 | } |
| 195 | #else |
| 196 | { |
| 197 | int i; |
| 198 | |
| 199 | /* Write the register contents into the inferior a chunk at the time. */ |
| 200 | for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++) |
| 201 | { |
| 202 | errno = 0; |
| 203 | ptrace (PT_WUREGS, pid, (PTRACE_TYPE_ARG3) addr, buf[i], 0); |
| 204 | if (errno != 0) |
| 205 | error (_("Couldn't write register %s (#%d): %s"), |
| 206 | gdbarch_register_name (current_gdbarch, regnum), |
| 207 | regnum, safe_strerror (errno)); |
| 208 | |
| 209 | addr += sizeof (PTRACE_TYPE_RET); |
| 210 | } |
| 211 | } |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do |
| 216 | this for all registers (including the floating point registers). */ |
| 217 | |
| 218 | static void |
| 219 | hppa_hpux_store_inferior_registers (struct regcache *regcache, int regnum) |
| 220 | { |
| 221 | if (regnum == -1) |
| 222 | for (regnum = 0; regnum < gdbarch_num_regs (current_gdbarch); regnum++) |
| 223 | hppa_hpux_store_register (regcache, regnum); |
| 224 | else |
| 225 | hppa_hpux_store_register (regcache, regnum); |
| 226 | } |
| 227 | |
| 228 | static int |
| 229 | hppa_hpux_child_can_run (void) |
| 230 | { |
| 231 | /* This variable is controlled by modules that layer their own |
| 232 | process structure atop that provided here. The code in |
| 233 | hpux-thread.c does this to support the HP-UX user-mode DCE |
| 234 | threads. */ |
| 235 | return !child_suppress_run; |
| 236 | } |
| 237 | \f |
| 238 | |
| 239 | /* Prevent warning from -Wmissing-prototypes. */ |
| 240 | void _initialize_hppa_hpux_nat (void); |
| 241 | |
| 242 | void |
| 243 | _initialize_hppa_hpux_nat (void) |
| 244 | { |
| 245 | struct target_ops *t; |
| 246 | |
| 247 | #ifdef HAVE_TTRACE |
| 248 | t = inf_ttrace_target (); |
| 249 | #else |
| 250 | t = inf_ptrace_target (); |
| 251 | #endif |
| 252 | |
| 253 | t->to_fetch_registers = hppa_hpux_fetch_inferior_registers; |
| 254 | t->to_store_registers = hppa_hpux_store_inferior_registers; |
| 255 | t->to_can_run = hppa_hpux_child_can_run; |
| 256 | |
| 257 | add_target (t); |
| 258 | } |