| 1 | /* Machine-dependent hooks for the unix child process stratum. This |
| 2 | code is for the HP PA-RISC cpu. |
| 3 | |
| 4 | Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc. |
| 5 | |
| 6 | Contributed by the Center for Software Science at the |
| 7 | University of Utah (pa-gdb-bugs@cs.utah.edu). |
| 8 | |
| 9 | This file is part of GDB. |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation; either version 2 of the License, or |
| 14 | (at your option) any later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 24 | |
| 25 | #include "defs.h" |
| 26 | #include "inferior.h" |
| 27 | #include "target.h" |
| 28 | #include <sys/ptrace.h> |
| 29 | |
| 30 | #ifndef PT_ATTACH |
| 31 | #define PT_ATTACH PTRACE_ATTACH |
| 32 | #endif |
| 33 | |
| 34 | #ifndef PT_DETACH |
| 35 | #define PT_DETACH PTRACE_DETACH |
| 36 | #endif |
| 37 | |
| 38 | /* This function simply calls ptrace with the given arguments. |
| 39 | It exists so that all calls to ptrace are isolated in this |
| 40 | machine-dependent file. */ |
| 41 | |
| 42 | int |
| 43 | call_ptrace (request, pid, addr, data) |
| 44 | int request, pid; |
| 45 | PTRACE_ARG3_TYPE addr; |
| 46 | int data; |
| 47 | { |
| 48 | return ptrace (request, pid, addr, data, 0); |
| 49 | } |
| 50 | |
| 51 | /* Use an extra level of indirection for ptrace calls. |
| 52 | This lets us breakpoint usefully on call_ptrace. It also |
| 53 | allows us to pass an extra argument to ptrace without |
| 54 | using an ANSI-C specific macro. */ |
| 55 | |
| 56 | #define ptrace call_ptrace |
| 57 | |
| 58 | void |
| 59 | kill_inferior () |
| 60 | { |
| 61 | if (inferior_pid == 0) |
| 62 | return; |
| 63 | ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0); |
| 64 | wait ((int *)0); |
| 65 | target_mourn_inferior (); |
| 66 | } |
| 67 | |
| 68 | #ifdef ATTACH_DETACH |
| 69 | |
| 70 | /* Start debugging the process whose number is PID. */ |
| 71 | int |
| 72 | attach (pid) |
| 73 | int pid; |
| 74 | { |
| 75 | errno = 0; |
| 76 | ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0); |
| 77 | if (errno) |
| 78 | perror_with_name ("ptrace"); |
| 79 | attach_flag = 1; |
| 80 | return pid; |
| 81 | } |
| 82 | |
| 83 | /* Stop debugging the process whose number is PID |
| 84 | and continue it with signal number SIGNAL. |
| 85 | SIGNAL = 0 means just continue it. */ |
| 86 | |
| 87 | void |
| 88 | detach (signal) |
| 89 | int signal; |
| 90 | { |
| 91 | errno = 0; |
| 92 | ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal); |
| 93 | if (errno) |
| 94 | perror_with_name ("ptrace"); |
| 95 | attach_flag = 0; |
| 96 | } |
| 97 | #endif /* ATTACH_DETACH */ |
| 98 | \f |
| 99 | |
| 100 | |
| 101 | /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0 |
| 102 | to get the offset in the core file of the register values. */ |
| 103 | #if defined (KERNEL_U_ADDR_BSD) |
| 104 | /* Get kernel_u_addr using BSD-style nlist(). */ |
| 105 | CORE_ADDR kernel_u_addr; |
| 106 | |
| 107 | #include <a.out.gnu.h> /* For struct nlist */ |
| 108 | |
| 109 | void |
| 110 | _initialize_kernel_u_addr () |
| 111 | { |
| 112 | struct nlist names[2]; |
| 113 | |
| 114 | names[0].n_un.n_name = "_u"; |
| 115 | names[1].n_un.n_name = NULL; |
| 116 | if (nlist ("/vmunix", names) == 0) |
| 117 | kernel_u_addr = names[0].n_value; |
| 118 | else |
| 119 | fatal ("Unable to get kernel u area address."); |
| 120 | } |
| 121 | #endif /* KERNEL_U_ADDR_BSD. */ |
| 122 | |
| 123 | #if defined (KERNEL_U_ADDR_HPUX) |
| 124 | /* Get kernel_u_addr using HPUX-style nlist(). */ |
| 125 | CORE_ADDR kernel_u_addr; |
| 126 | |
| 127 | struct hpnlist { |
| 128 | char * n_name; |
| 129 | long n_value; |
| 130 | unsigned char n_type; |
| 131 | unsigned char n_length; |
| 132 | short n_almod; |
| 133 | short n_unused; |
| 134 | }; |
| 135 | static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }}; |
| 136 | |
| 137 | /* read the value of the u area from the hp-ux kernel */ |
| 138 | void _initialize_kernel_u_addr () |
| 139 | { |
| 140 | struct user u; |
| 141 | nlist ("/hp-ux", &nl); |
| 142 | kernel_u_addr = nl[0].n_value; |
| 143 | } |
| 144 | #endif /* KERNEL_U_ADDR_HPUX. */ |
| 145 | |
| 146 | #if !defined (offsetof) |
| 147 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) |
| 148 | #endif |
| 149 | |
| 150 | /* U_REGS_OFFSET is the offset of the registers within the u area. */ |
| 151 | #if !defined (U_REGS_OFFSET) |
| 152 | #define U_REGS_OFFSET \ |
| 153 | ptrace (PT_READ_U, inferior_pid, \ |
| 154 | (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \ |
| 155 | - KERNEL_U_ADDR |
| 156 | #endif |
| 157 | |
| 158 | /* Fetch one register. */ |
| 159 | |
| 160 | static void |
| 161 | fetch_register (regno) |
| 162 | int regno; |
| 163 | { |
| 164 | register unsigned int regaddr; |
| 165 | char buf[MAX_REGISTER_RAW_SIZE]; |
| 166 | register int i; |
| 167 | |
| 168 | /* Offset of registers within the u area. */ |
| 169 | unsigned int offset; |
| 170 | |
| 171 | offset = U_REGS_OFFSET; |
| 172 | |
| 173 | regaddr = register_addr (regno, offset); |
| 174 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) |
| 175 | { |
| 176 | errno = 0; |
| 177 | *(int *) &buf[i] = ptrace (PT_RUREGS, inferior_pid, |
| 178 | (PTRACE_ARG3_TYPE) regaddr, 0); |
| 179 | regaddr += sizeof (int); |
| 180 | if (errno != 0) |
| 181 | { |
| 182 | /* Warning, not error, in case we are attached; sometimes the |
| 183 | kernel doesn't let us at the registers. */ |
| 184 | char *err = safe_strerror (errno); |
| 185 | char *msg = alloca (strlen (err) + 128); |
| 186 | sprintf (msg, "reading register %s: %s", reg_names[regno], err); |
| 187 | warning (msg); |
| 188 | goto error_exit; |
| 189 | } |
| 190 | } |
| 191 | supply_register (regno, buf); |
| 192 | error_exit:; |
| 193 | } |
| 194 | |
| 195 | /* Fetch all registers, or just one, from the child process. */ |
| 196 | |
| 197 | void |
| 198 | fetch_inferior_registers (regno) |
| 199 | int regno; |
| 200 | { |
| 201 | if (regno == -1) |
| 202 | for (regno = 0; regno < NUM_REGS; regno++) |
| 203 | fetch_register (regno); |
| 204 | else |
| 205 | fetch_register (regno); |
| 206 | } |
| 207 | |
| 208 | /* Store our register values back into the inferior. |
| 209 | If REGNO is -1, do this for all registers. |
| 210 | Otherwise, REGNO specifies which register (so we can save time). */ |
| 211 | |
| 212 | void |
| 213 | store_inferior_registers (regno) |
| 214 | int regno; |
| 215 | { |
| 216 | register unsigned int regaddr; |
| 217 | extern char registers[]; |
| 218 | register int i; |
| 219 | |
| 220 | unsigned int offset = U_REGS_OFFSET; |
| 221 | |
| 222 | if (regno >= 0) |
| 223 | { |
| 224 | regaddr = register_addr (regno, offset); |
| 225 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) |
| 226 | { |
| 227 | errno = 0; |
| 228 | ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, |
| 229 | *(int *) ®isters[REGISTER_BYTE (regno) + i]); |
| 230 | if (errno != 0) |
| 231 | { |
| 232 | char *err = safe_strerror (errno); |
| 233 | char *msg = alloca (strlen (err) + 128); |
| 234 | sprintf (msg, "writing register %s: %s", reg_names[regno], err); |
| 235 | warning (msg); |
| 236 | } |
| 237 | regaddr += sizeof(int); |
| 238 | } |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | for (regno = 0; regno < NUM_REGS; regno++) |
| 243 | { |
| 244 | if (CANNOT_STORE_REGISTER (regno)) |
| 245 | continue; |
| 246 | store_inferior_registers (regno); |
| 247 | } |
| 248 | } |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | /* Resume execution of process PID. |
| 253 | If STEP is nonzero, single-step it. |
| 254 | If SIGNAL is nonzero, give it that signal. */ |
| 255 | |
| 256 | void |
| 257 | child_resume (pid, step, signal) |
| 258 | int pid; |
| 259 | int step; |
| 260 | int signal; |
| 261 | { |
| 262 | errno = 0; |
| 263 | |
| 264 | /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where |
| 265 | it was. (If GDB wanted it to start some other way, we have already |
| 266 | written a new PC value to the child.) */ |
| 267 | |
| 268 | if (step) |
| 269 | ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1, signal); |
| 270 | else |
| 271 | ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1, signal); |
| 272 | |
| 273 | if (errno) |
| 274 | perror_with_name ("ptrace"); |
| 275 | } |
| 276 | |
| 277 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory |
| 278 | in the NEW_SUN_PTRACE case. |
| 279 | It ought to be straightforward. But it appears that writing did |
| 280 | not write the data that I specified. I cannot understand where |
| 281 | it got the data that it actually did write. */ |
| 282 | |
| 283 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR |
| 284 | to debugger memory starting at MYADDR. Copy to inferior if |
| 285 | WRITE is nonzero. |
| 286 | |
| 287 | Returns the length copied, which is either the LEN argument or zero. |
| 288 | This xfer function does not do partial moves, since child_ops |
| 289 | doesn't allow memory operations to cross below us in the target stack |
| 290 | anyway. */ |
| 291 | |
| 292 | int |
| 293 | child_xfer_memory (memaddr, myaddr, len, write, target) |
| 294 | CORE_ADDR memaddr; |
| 295 | char *myaddr; |
| 296 | int len; |
| 297 | int write; |
| 298 | struct target_ops *target; /* ignored */ |
| 299 | { |
| 300 | register int i; |
| 301 | /* Round starting address down to longword boundary. */ |
| 302 | register CORE_ADDR addr = memaddr & - sizeof (int); |
| 303 | /* Round ending address up; get number of longwords that makes. */ |
| 304 | register int count |
| 305 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); |
| 306 | /* Allocate buffer of that many longwords. */ |
| 307 | register int *buffer = (int *) alloca (count * sizeof (int)); |
| 308 | |
| 309 | if (write) |
| 310 | { |
| 311 | /* Fill start and end extra bytes of buffer with existing memory data. */ |
| 312 | |
| 313 | if (addr != memaddr || len < (int)sizeof (int)) { |
| 314 | /* Need part of initial word -- fetch it. */ |
| 315 | buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr, |
| 316 | 0); |
| 317 | } |
| 318 | |
| 319 | if (count > 1) /* FIXME, avoid if even boundary */ |
| 320 | { |
| 321 | buffer[count - 1] |
| 322 | = ptrace (PT_READ_I, inferior_pid, |
| 323 | (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)), |
| 324 | 0); |
| 325 | } |
| 326 | |
| 327 | /* Copy data to be written over corresponding part of buffer */ |
| 328 | |
| 329 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); |
| 330 | |
| 331 | /* Write the entire buffer. */ |
| 332 | |
| 333 | for (i = 0; i < count; i++, addr += sizeof (int)) |
| 334 | { |
| 335 | errno = 0; |
| 336 | ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, |
| 337 | buffer[i]); |
| 338 | if (errno) |
| 339 | { |
| 340 | /* Using the appropriate one (I or D) is necessary for |
| 341 | Gould NP1, at least. */ |
| 342 | errno = 0; |
| 343 | ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr, |
| 344 | buffer[i]); |
| 345 | } |
| 346 | if (errno) |
| 347 | return 0; |
| 348 | } |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | /* Read all the longwords */ |
| 353 | for (i = 0; i < count; i++, addr += sizeof (int)) |
| 354 | { |
| 355 | errno = 0; |
| 356 | buffer[i] = ptrace (PT_READ_I, inferior_pid, |
| 357 | (PTRACE_ARG3_TYPE) addr, 0); |
| 358 | if (errno) |
| 359 | return 0; |
| 360 | QUIT; |
| 361 | } |
| 362 | |
| 363 | /* Copy appropriate bytes out of the buffer. */ |
| 364 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); |
| 365 | } |
| 366 | return len; |
| 367 | } |
| 368 | |