| 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 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 | |
| 26 | #include "defs.h" |
| 27 | #include "inferior.h" |
| 28 | |
| 29 | #ifndef PT_ATTACH |
| 30 | #define PT_ATTACH PTRACE_ATTACH |
| 31 | #endif |
| 32 | #ifndef PT_DETACH |
| 33 | #define PT_DETACH PTRACE_DETACH |
| 34 | #endif |
| 35 | |
| 36 | /* This function simply calls ptrace with the given arguments. |
| 37 | It exists so that all calls to ptrace are isolated in this |
| 38 | machine-dependent file. */ |
| 39 | #ifdef WANT_NATIVE_TARGET |
| 40 | int |
| 41 | call_ptrace (request, pid, addr, data) |
| 42 | int request, pid; |
| 43 | PTRACE_ARG3_TYPE addr; |
| 44 | int data; |
| 45 | { |
| 46 | return ptrace (request, pid, addr, data, 0); |
| 47 | } |
| 48 | #endif /* WANT_NATIVE_TARGET */ |
| 49 | |
| 50 | #ifdef DEBUG_PTRACE |
| 51 | /* For the rest of the file, use an extra level of indirection */ |
| 52 | /* This lets us breakpoint usefully on call_ptrace. */ |
| 53 | #define ptrace call_ptrace |
| 54 | #endif |
| 55 | |
| 56 | void |
| 57 | kill_inferior () |
| 58 | { |
| 59 | if (inferior_pid == 0) |
| 60 | return; |
| 61 | ptrace (PT_EXIT, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0, 0); /* PT_EXIT = PT_KILL ? */ |
| 62 | wait ((int *)0); |
| 63 | target_mourn_inferior (); |
| 64 | } |
| 65 | |
| 66 | #ifdef ATTACH_DETACH |
| 67 | /* Nonzero if we are debugging an attached process rather than |
| 68 | an inferior. */ |
| 69 | extern int attach_flag; |
| 70 | |
| 71 | /* Start debugging the process whose number is PID. */ |
| 72 | int |
| 73 | attach (pid) |
| 74 | int pid; |
| 75 | { |
| 76 | errno = 0; |
| 77 | ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0, 0); |
| 78 | if (errno) |
| 79 | perror_with_name ("ptrace"); |
| 80 | attach_flag = 1; |
| 81 | return pid; |
| 82 | } |
| 83 | |
| 84 | /* Stop debugging the process whose number is PID |
| 85 | and continue it with signal number SIGNAL. |
| 86 | SIGNAL = 0 means just continue it. */ |
| 87 | |
| 88 | void |
| 89 | detach (signal) |
| 90 | int signal; |
| 91 | { |
| 92 | errno = 0; |
| 93 | ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0); |
| 94 | if (errno) |
| 95 | perror_with_name ("ptrace"); |
| 96 | attach_flag = 0; |
| 97 | } |
| 98 | #endif /* ATTACH_DETACH */ |
| 99 | |
| 100 | /* Fetch all registers, or just one, from the child process. */ |
| 101 | |
| 102 | void |
| 103 | fetch_inferior_registers (regno) |
| 104 | int regno; |
| 105 | { |
| 106 | if (regno == -1) |
| 107 | for (regno = 0; regno < NUM_REGS; regno++) |
| 108 | fetch_register (regno); |
| 109 | else |
| 110 | fetch_register (regno); |
| 111 | } |
| 112 | |
| 113 | /* Registers we shouldn't try to store. */ |
| 114 | #if !defined (CANNOT_STORE_REGISTER) |
| 115 | #define CANNOT_STORE_REGISTER(regno) 0 |
| 116 | #endif |
| 117 | |
| 118 | /* Store our register values back into the inferior. |
| 119 | If REGNO is -1, do this for all registers. |
| 120 | Otherwise, REGNO specifies which register (so we can save time). */ |
| 121 | |
| 122 | void |
| 123 | store_inferior_registers (regno) |
| 124 | int regno; |
| 125 | { |
| 126 | register unsigned int regaddr; |
| 127 | char buf[80]; |
| 128 | extern char registers[]; |
| 129 | register int i; |
| 130 | |
| 131 | unsigned int offset = U_REGS_OFFSET; |
| 132 | |
| 133 | if (regno >= 0) |
| 134 | { |
| 135 | regaddr = register_addr (regno, offset); |
| 136 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) |
| 137 | { |
| 138 | errno = 0; |
| 139 | ptrace (PT_WUAREA, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, |
| 140 | *(int *) ®isters[REGISTER_BYTE (regno) + i], 0); |
| 141 | if (errno != 0) |
| 142 | { |
| 143 | sprintf (buf, "writing register number %d(%d)", regno, i); |
| 144 | perror_with_name (buf); |
| 145 | } |
| 146 | regaddr += sizeof(int); |
| 147 | } |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | for (regno = 0; regno < NUM_REGS; regno++) |
| 152 | { |
| 153 | if (CANNOT_STORE_REGISTER (regno)) |
| 154 | continue; |
| 155 | regaddr = register_addr (regno, offset); |
| 156 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) |
| 157 | { |
| 158 | errno = 0; |
| 159 | ptrace (PT_WUAREA, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, |
| 160 | *(int *) ®isters[REGISTER_BYTE (regno) + i], 0); |
| 161 | if (errno != 0) |
| 162 | { |
| 163 | sprintf (buf, "writing register number %d(%d)", regno, i); |
| 164 | perror_with_name (buf); |
| 165 | } |
| 166 | regaddr += sizeof(int); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0 |
| 174 | to get the offset in the core file of the register values. */ |
| 175 | #if defined (KERNEL_U_ADDR_BSD) |
| 176 | /* Get kernel_u_addr using BSD-style nlist(). */ |
| 177 | CORE_ADDR kernel_u_addr; |
| 178 | |
| 179 | #include <a.out.gnu.h> /* For struct nlist */ |
| 180 | |
| 181 | void |
| 182 | _initialize_kernel_u_addr () |
| 183 | { |
| 184 | struct nlist names[2]; |
| 185 | |
| 186 | names[0].n_un.n_name = "_u"; |
| 187 | names[1].n_un.n_name = NULL; |
| 188 | if (nlist ("/vmunix", names) == 0) |
| 189 | kernel_u_addr = names[0].n_value; |
| 190 | else |
| 191 | fatal ("Unable to get kernel u area address."); |
| 192 | } |
| 193 | #endif /* KERNEL_U_ADDR_BSD. */ |
| 194 | |
| 195 | #if defined (KERNEL_U_ADDR_HPUX) |
| 196 | /* Get kernel_u_addr using HPUX-style nlist(). */ |
| 197 | CORE_ADDR kernel_u_addr; |
| 198 | |
| 199 | struct hpnlist { |
| 200 | char * n_name; |
| 201 | long n_value; |
| 202 | unsigned char n_type; |
| 203 | unsigned char n_length; |
| 204 | short n_almod; |
| 205 | short n_unused; |
| 206 | }; |
| 207 | static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }}; |
| 208 | |
| 209 | /* read the value of the u area from the hp-ux kernel */ |
| 210 | void _initialize_kernel_u_addr () |
| 211 | { |
| 212 | struct user u; |
| 213 | nlist ("/hp-ux", &nl); |
| 214 | kernel_u_addr = nl[0].n_value; |
| 215 | } |
| 216 | #endif /* KERNEL_U_ADDR_HPUX. */ |
| 217 | |
| 218 | #if !defined (offsetof) |
| 219 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) |
| 220 | #endif |
| 221 | |
| 222 | /* U_REGS_OFFSET is the offset of the registers within the u area. */ |
| 223 | #if !defined (U_REGS_OFFSET) |
| 224 | #define U_REGS_OFFSET \ |
| 225 | ptrace (PT_READ_U, inferior_pid, \ |
| 226 | (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0, 0) \ |
| 227 | - KERNEL_U_ADDR |
| 228 | #endif |
| 229 | |
| 230 | /* Registers we shouldn't try to fetch. */ |
| 231 | #if !defined (CANNOT_FETCH_REGISTER) |
| 232 | #define CANNOT_FETCH_REGISTER(regno) 0 |
| 233 | #endif |
| 234 | |
| 235 | /* Fetch one register. */ |
| 236 | |
| 237 | static void |
| 238 | fetch_register (regno) |
| 239 | int regno; |
| 240 | { |
| 241 | register unsigned int regaddr; |
| 242 | char buf[MAX_REGISTER_RAW_SIZE]; |
| 243 | char mess[128]; /* For messages */ |
| 244 | register int i; |
| 245 | |
| 246 | /* Offset of registers within the u area. */ |
| 247 | unsigned int offset; |
| 248 | |
| 249 | if (CANNOT_FETCH_REGISTER (regno)) |
| 250 | { |
| 251 | bzero (buf, REGISTER_RAW_SIZE (regno)); /* Supply zeroes */ |
| 252 | supply_register (regno, buf); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | offset = U_REGS_OFFSET; |
| 257 | |
| 258 | regaddr = register_addr (regno, offset); |
| 259 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) |
| 260 | { |
| 261 | errno = 0; |
| 262 | *(int *) &buf[i] = ptrace (PT_RUREGS, inferior_pid, |
| 263 | (PTRACE_ARG3_TYPE) regaddr, 0, 0); |
| 264 | regaddr += sizeof (int); |
| 265 | if (errno != 0) |
| 266 | { |
| 267 | sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno); |
| 268 | perror_with_name (mess); |
| 269 | } |
| 270 | } |
| 271 | supply_register (regno, buf); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | |
| 276 | /* Resume execution of the inferior process. |
| 277 | If STEP is nonzero, single-step it. |
| 278 | If SIGNAL is nonzero, give it that signal. */ |
| 279 | |
| 280 | void |
| 281 | child_resume (step, signal) |
| 282 | int step; |
| 283 | int signal; |
| 284 | { |
| 285 | errno = 0; |
| 286 | |
| 287 | /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where |
| 288 | it was. (If GDB wanted it to start some other way, we have already |
| 289 | written a new PC value to the child.) */ |
| 290 | |
| 291 | if (step) |
| 292 | ptrace (PT_SINGLE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0); |
| 293 | else |
| 294 | ptrace (PT_CONTIN, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0); |
| 295 | |
| 296 | if (errno) |
| 297 | perror_with_name ("ptrace"); |
| 298 | } |
| 299 | |
| 300 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory |
| 301 | in the NEW_SUN_PTRACE case. |
| 302 | It ought to be straightforward. But it appears that writing did |
| 303 | not write the data that I specified. I cannot understand where |
| 304 | it got the data that it actually did write. */ |
| 305 | |
| 306 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR |
| 307 | to debugger memory starting at MYADDR. Copy to inferior if |
| 308 | WRITE is nonzero. |
| 309 | |
| 310 | Returns the length copied, which is either the LEN argument or zero. |
| 311 | This xfer function does not do partial moves, since child_ops |
| 312 | doesn't allow memory operations to cross below us in the target stack |
| 313 | anyway. */ |
| 314 | |
| 315 | int |
| 316 | child_xfer_memory (memaddr, myaddr, len, write, target) |
| 317 | CORE_ADDR memaddr; |
| 318 | char *myaddr; |
| 319 | int len; |
| 320 | int write; |
| 321 | struct target_ops *target; /* ignored */ |
| 322 | { |
| 323 | register int i; |
| 324 | /* Round starting address down to longword boundary. */ |
| 325 | register CORE_ADDR addr = memaddr & - sizeof (int); |
| 326 | /* Round ending address up; get number of longwords that makes. */ |
| 327 | register int count |
| 328 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); |
| 329 | /* Allocate buffer of that many longwords. */ |
| 330 | register int *buffer = (int *) alloca (count * sizeof (int)); |
| 331 | |
| 332 | if (write) |
| 333 | { |
| 334 | /* Fill start and end extra bytes of buffer with existing memory data. */ |
| 335 | |
| 336 | if (addr != memaddr || len < (int)sizeof (int)) { |
| 337 | /* Need part of initial word -- fetch it. */ |
| 338 | buffer[0] = ptrace (PT_RIUSER, inferior_pid, |
| 339 | (PTRACE_ARG3_TYPE) addr, 0, 0); |
| 340 | } |
| 341 | |
| 342 | if (count > 1) /* FIXME, avoid if even boundary */ |
| 343 | { |
| 344 | buffer[count - 1] |
| 345 | = ptrace (PT_RIUSER, inferior_pid, |
| 346 | (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)), |
| 347 | 0, 0); |
| 348 | } |
| 349 | |
| 350 | /* Copy data to be written over corresponding part of buffer */ |
| 351 | |
| 352 | bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); |
| 353 | |
| 354 | /* Write the entire buffer. */ |
| 355 | |
| 356 | for (i = 0; i < count; i++, addr += sizeof (int)) |
| 357 | { |
| 358 | #if 0 |
| 359 | /* The HP-UX kernel crashes if you use PT_WDUSER to write into the text |
| 360 | segment. FIXME -- does it work to write into the data segment using |
| 361 | WIUSER, or do these idiots really expect us to figure out which segment |
| 362 | the address is in, so we can use a separate system call for it??! */ |
| 363 | errno = 0; |
| 364 | ptrace (PT_WDUSER, inferior_pid, (PTRACE_ARG3_TYPE) addr, |
| 365 | buffer[i], 0); |
| 366 | if (errno) |
| 367 | #endif |
| 368 | { |
| 369 | /* Using the appropriate one (I or D) is necessary for |
| 370 | Gould NP1, at least. */ |
| 371 | errno = 0; |
| 372 | ptrace (PT_WIUSER, inferior_pid, (PTRACE_ARG3_TYPE) addr, |
| 373 | buffer[i], 0); |
| 374 | } |
| 375 | if (errno) |
| 376 | return 0; |
| 377 | } |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | /* Read all the longwords */ |
| 382 | for (i = 0; i < count; i++, addr += sizeof (int)) |
| 383 | { |
| 384 | errno = 0; |
| 385 | buffer[i] = ptrace (PT_RIUSER, inferior_pid, |
| 386 | (PTRACE_ARG3_TYPE) addr, 0, 0); |
| 387 | if (errno) |
| 388 | return 0; |
| 389 | QUIT; |
| 390 | } |
| 391 | |
| 392 | /* Copy appropriate bytes out of the buffer. */ |
| 393 | bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); |
| 394 | } |
| 395 | return len; |
| 396 | } |
| 397 | |