1 /* Low level Unix child interface to ptrace, for GDB when running under Unix. 2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc. 3 4 This file is part of GDB. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 19 20 #include "defs.h" 21 #include "frame.h" 22 #include "inferior.h" 23 #include "target.h" 24 #include "gdb_string.h" 25 #include "wait.h" 26 #include "command.h" 27 28 #ifdef USG 29 #include <sys/types.h> 30 #endif 31 32 #include <sys/param.h> 33 #include <sys/dir.h> 34 #include <signal.h> 35 #include <sys/ioctl.h> 36 37 #ifndef NO_PTRACE_H 38 #ifdef PTRACE_IN_WRONG_PLACE 39 #include <ptrace.h> 40 #else 41 #include <sys/ptrace.h> 42 #endif 43 #endif /* NO_PTRACE_H */ 44 45 #if !defined (PT_READ_I) 46 #define PT_READ_I 1 /* Read word from text space */ 47 #endif 48 #if !defined (PT_READ_D) 49 #define PT_READ_D 2 /* Read word from data space */ 50 #endif 51 #if !defined (PT_READ_U) 52 #define PT_READ_U 3 /* Read word from kernel user struct */ 53 #endif 54 #if !defined (PT_WRITE_I) 55 #define PT_WRITE_I 4 /* Write word to text space */ 56 #endif 57 #if !defined (PT_WRITE_D) 58 #define PT_WRITE_D 5 /* Write word to data space */ 59 #endif 60 #if !defined (PT_WRITE_U) 61 #define PT_WRITE_U 6 /* Write word to kernel user struct */ 62 #endif 63 #if !defined (PT_CONTINUE) 64 #define PT_CONTINUE 7 /* Continue after signal */ 65 #endif 66 #if !defined (PT_STEP) 67 #define PT_STEP 9 /* Set flag for single stepping */ 68 #endif 69 #if !defined (PT_KILL) 70 #define PT_KILL 8 /* Send child a SIGKILL signal */ 71 #endif 72 73 #ifndef PT_ATTACH 74 #define PT_ATTACH PTRACE_ATTACH 75 #endif 76 #ifndef PT_DETACH 77 #define PT_DETACH PTRACE_DETACH 78 #endif 79 80 #include "gdbcore.h" 81 #ifndef NO_SYS_FILE 82 #include <sys/file.h> 83 #endif 84 #if 0 85 /* Don't think this is used anymore. On the sequent (not sure whether it's 86 dynix or ptx or both), it is included unconditionally by sys/user.h and 87 not protected against multiple inclusion. */ 88 #include "gdb_stat.h" 89 #endif 90 91 #if !defined (FETCH_INFERIOR_REGISTERS) 92 #include <sys/user.h> /* Probably need to poke the user structure */ 93 #if defined (KERNEL_U_ADDR_BSD) 94 #include <a.out.h> /* For struct nlist */ 95 #endif /* KERNEL_U_ADDR_BSD. */ 96 #endif /* !FETCH_INFERIOR_REGISTERS */ 97 98 #if !defined (CHILD_XFER_MEMORY) 99 static void udot_info PARAMS ((char *, int)); 100 #endif 101 102 #if !defined (FETCH_INFERIOR_REGISTERS) 103 static void fetch_register PARAMS ((int)); 104 #endif 105 106 107 /* This function simply calls ptrace with the given arguments. 108 It exists so that all calls to ptrace are isolated in this 109 machine-dependent file. */ 110 int 111 call_ptrace (request, pid, addr, data) 112 int request, pid; 113 PTRACE_ARG3_TYPE addr; 114 int data; 115 { 116 return ptrace (request, pid, addr, data 117 #if defined (FIVE_ARG_PTRACE) 118 /* Deal with HPUX 8.0 braindamage. We never use the 119 calls which require the fifth argument. */ 120 , 0 121 #endif 122 ); 123 } 124 125 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE) 126 /* For the rest of the file, use an extra level of indirection */ 127 /* This lets us breakpoint usefully on call_ptrace. */ 128 #define ptrace call_ptrace 129 #endif 130 131 void 132 kill_inferior () 133 { 134 if (inferior_pid == 0) 135 return; 136 137 /* This once used to call "kill" to kill the inferior just in case 138 the inferior was still running. As others have noted in the past 139 (kingdon) there shouldn't be any way to get here if the inferior 140 is still running -- else there's a major problem elsewere in gdb 141 and it needs to be fixed. 142 143 The kill call causes problems under hpux10, so it's been removed; 144 if this causes problems we'll deal with them as they arise. */ 145 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0); 146 wait ((int *)0); 147 target_mourn_inferior (); 148 } 149 150 #ifndef CHILD_RESUME 151 152 /* Resume execution of the inferior process. 153 If STEP is nonzero, single-step it. 154 If SIGNAL is nonzero, give it that signal. */ 155 156 void 157 child_resume (pid, step, signal) 158 int pid; 159 int step; 160 enum target_signal signal; 161 { 162 errno = 0; 163 164 if (pid == -1) 165 /* Resume all threads. */ 166 /* I think this only gets used in the non-threaded case, where "resume 167 all threads" and "resume inferior_pid" are the same. */ 168 pid = inferior_pid; 169 170 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where 171 it was. (If GDB wanted it to start some other way, we have already 172 written a new PC value to the child.) 173 174 If this system does not support PT_STEP, a higher level function will 175 have called single_step() to transmute the step request into a 176 continue request (by setting breakpoints on all possible successor 177 instructions), so we don't have to worry about that here. */ 178 179 if (step) 180 ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1, 181 target_signal_to_host (signal)); 182 else 183 ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1, 184 target_signal_to_host (signal)); 185 186 if (errno) 187 perror_with_name ("ptrace"); 188 } 189 #endif /* CHILD_RESUME */ 190 191 192 #ifdef ATTACH_DETACH 193 /* Start debugging the process whose number is PID. */ 194 int 195 attach (pid) 196 int pid; 197 { 198 errno = 0; 199 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0); 200 if (errno) 201 perror_with_name ("ptrace"); 202 attach_flag = 1; 203 return pid; 204 } 205 206 /* Stop debugging the process whose number is PID 207 and continue it with signal number SIGNAL. 208 SIGNAL = 0 means just continue it. */ 209 210 void 211 detach (signal) 212 int signal; 213 { 214 errno = 0; 215 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal); 216 if (errno) 217 perror_with_name ("ptrace"); 218 attach_flag = 0; 219 } 220 #endif /* ATTACH_DETACH */ 221 222 /* Default the type of the ptrace transfer to int. */ 223 #ifndef PTRACE_XFER_TYPE 224 #define PTRACE_XFER_TYPE int 225 #endif 226 227 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0 228 to get the offset in the core file of the register values. */ 229 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS) 230 /* Get kernel_u_addr using BSD-style nlist(). */ 231 CORE_ADDR kernel_u_addr; 232 #endif /* KERNEL_U_ADDR_BSD. */ 233 234 void 235 _initialize_kernel_u_addr () 236 { 237 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS) 238 struct nlist names[2]; 239 240 names[0].n_un.n_name = "_u"; 241 names[1].n_un.n_name = NULL; 242 if (nlist ("/vmunix", names) == 0) 243 kernel_u_addr = names[0].n_value; 244 else 245 fatal ("Unable to get kernel u area address."); 246 #endif /* KERNEL_U_ADDR_BSD. */ 247 } 248 249 #if !defined (FETCH_INFERIOR_REGISTERS) 250 251 #if !defined (offsetof) 252 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) 253 #endif 254 255 /* U_REGS_OFFSET is the offset of the registers within the u area. */ 256 #if !defined (U_REGS_OFFSET) 257 #define U_REGS_OFFSET \ 258 ptrace (PT_READ_U, inferior_pid, \ 259 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \ 260 - KERNEL_U_ADDR 261 #endif 262 263 /* Registers we shouldn't try to fetch. */ 264 #if !defined (CANNOT_FETCH_REGISTER) 265 #define CANNOT_FETCH_REGISTER(regno) 0 266 #endif 267 268 /* Fetch one register. */ 269 270 static void 271 fetch_register (regno) 272 int regno; 273 { 274 /* This isn't really an address. But ptrace thinks of it as one. */ 275 CORE_ADDR regaddr; 276 char buf[MAX_REGISTER_RAW_SIZE]; 277 char mess[128]; /* For messages */ 278 register int i; 279 280 /* Offset of registers within the u area. */ 281 unsigned int offset; 282 283 if (CANNOT_FETCH_REGISTER (regno)) 284 { 285 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */ 286 supply_register (regno, buf); 287 return; 288 } 289 290 offset = U_REGS_OFFSET; 291 292 regaddr = register_addr (regno, offset); 293 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) 294 { 295 errno = 0; 296 *(PTRACE_XFER_TYPE *) &buf[i] = ptrace (PT_READ_U, inferior_pid, 297 (PTRACE_ARG3_TYPE) regaddr, 0); 298 regaddr += sizeof (PTRACE_XFER_TYPE); 299 if (errno != 0) 300 { 301 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno); 302 perror_with_name (mess); 303 } 304 } 305 supply_register (regno, buf); 306 } 307 308 309 /* Fetch all registers, or just one, from the child process. */ 310 311 void 312 fetch_inferior_registers (regno) 313 int regno; 314 { 315 int numregs; 316 317 if (regno == -1) 318 { 319 numregs = ARCH_NUM_REGS; 320 for (regno = 0; regno < numregs; regno++) 321 fetch_register (regno); 322 } 323 else 324 fetch_register (regno); 325 } 326 327 /* Registers we shouldn't try to store. */ 328 #if !defined (CANNOT_STORE_REGISTER) 329 #define CANNOT_STORE_REGISTER(regno) 0 330 #endif 331 332 /* Store our register values back into the inferior. 333 If REGNO is -1, do this for all registers. 334 Otherwise, REGNO specifies which register (so we can save time). */ 335 336 void 337 store_inferior_registers (regno) 338 int regno; 339 { 340 /* This isn't really an address. But ptrace thinks of it as one. */ 341 CORE_ADDR regaddr; 342 char buf[80]; 343 register int i, numregs; 344 345 unsigned int offset = U_REGS_OFFSET; 346 347 if (regno >= 0) 348 { 349 regaddr = register_addr (regno, offset); 350 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE)) 351 { 352 errno = 0; 353 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 354 *(PTRACE_XFER_TYPE *) ®isters[REGISTER_BYTE (regno) + i]); 355 if (errno != 0) 356 { 357 sprintf (buf, "writing register number %d(%d)", regno, i); 358 perror_with_name (buf); 359 } 360 regaddr += sizeof(PTRACE_XFER_TYPE); 361 } 362 } 363 else 364 { 365 numregs = ARCH_NUM_REGS; 366 for (regno = 0; regno < numregs; regno++) 367 { 368 if (CANNOT_STORE_REGISTER (regno)) 369 continue; 370 regaddr = register_addr (regno, offset); 371 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE)) 372 { 373 errno = 0; 374 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 375 *(PTRACE_XFER_TYPE *) ®isters[REGISTER_BYTE (regno) + i]); 376 if (errno != 0) 377 { 378 sprintf (buf, "writing register number %d(%d)", regno, i); 379 perror_with_name (buf); 380 } 381 regaddr += sizeof(PTRACE_XFER_TYPE); 382 } 383 } 384 } 385 } 386 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */ 387 388 389 #if !defined (CHILD_XFER_MEMORY) 390 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory 391 in the NEW_SUN_PTRACE case. 392 It ought to be straightforward. But it appears that writing did 393 not write the data that I specified. I cannot understand where 394 it got the data that it actually did write. */ 395 396 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR 397 to debugger memory starting at MYADDR. Copy to inferior if 398 WRITE is nonzero. 399 400 Returns the length copied, which is either the LEN argument or zero. 401 This xfer function does not do partial moves, since child_ops 402 doesn't allow memory operations to cross below us in the target stack 403 anyway. */ 404 405 int 406 child_xfer_memory (memaddr, myaddr, len, write, target) 407 CORE_ADDR memaddr; 408 char *myaddr; 409 int len; 410 int write; 411 struct target_ops *target; /* ignored */ 412 { 413 register int i; 414 /* Round starting address down to longword boundary. */ 415 register CORE_ADDR addr = memaddr & - sizeof (PTRACE_XFER_TYPE); 416 /* Round ending address up; get number of longwords that makes. */ 417 register int count 418 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) 419 / sizeof (PTRACE_XFER_TYPE); 420 /* Allocate buffer of that many longwords. */ 421 register PTRACE_XFER_TYPE *buffer 422 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); 423 424 if (write) 425 { 426 /* Fill start and end extra bytes of buffer with existing memory data. */ 427 428 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) { 429 /* Need part of initial word -- fetch it. */ 430 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr, 431 0); 432 } 433 434 if (count > 1) /* FIXME, avoid if even boundary */ 435 { 436 buffer[count - 1] 437 = ptrace (PT_READ_I, inferior_pid, 438 ((PTRACE_ARG3_TYPE) 439 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))), 440 0); 441 } 442 443 /* Copy data to be written over corresponding part of buffer */ 444 445 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), 446 myaddr, 447 len); 448 449 /* Write the entire buffer. */ 450 451 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) 452 { 453 errno = 0; 454 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 455 buffer[i]); 456 if (errno) 457 { 458 /* Using the appropriate one (I or D) is necessary for 459 Gould NP1, at least. */ 460 errno = 0; 461 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr, 462 buffer[i]); 463 } 464 if (errno) 465 return 0; 466 } 467 } 468 else 469 { 470 /* Read all the longwords */ 471 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) 472 { 473 errno = 0; 474 buffer[i] = ptrace (PT_READ_I, inferior_pid, 475 (PTRACE_ARG3_TYPE) addr, 0); 476 if (errno) 477 return 0; 478 QUIT; 479 } 480 481 /* Copy appropriate bytes out of the buffer. */ 482 memcpy (myaddr, 483 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), 484 len); 485 } 486 return len; 487 } 488 489 490 static void 491 udot_info (dummy1, dummy2) 492 char *dummy1; 493 int dummy2; 494 { 495 #if defined (KERNEL_U_SIZE) 496 int udot_off; /* Offset into user struct */ 497 int udot_val; /* Value from user struct at udot_off */ 498 char mess[128]; /* For messages */ 499 #endif 500 501 if (!target_has_execution) 502 { 503 error ("The program is not being run."); 504 } 505 506 #if !defined (KERNEL_U_SIZE) 507 508 /* Adding support for this command is easy. Typically you just add a 509 routine, called "kernel_u_size" that returns the size of the user 510 struct, to the appropriate *-nat.c file and then add to the native 511 config file "#define KERNEL_U_SIZE kernel_u_size()" */ 512 error ("Don't know how large ``struct user'' is in this version of gdb."); 513 514 #else 515 516 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val)) 517 { 518 if ((udot_off % 24) == 0) 519 { 520 if (udot_off > 0) 521 { 522 printf_filtered ("\n"); 523 } 524 printf_filtered ("%04x:", udot_off); 525 } 526 udot_val = ptrace (PT_READ_U, inferior_pid, (PTRACE_ARG3_TYPE) udot_off, 0); 527 if (errno != 0) 528 { 529 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off); 530 perror_with_name (mess); 531 } 532 /* Avoid using nonportable (?) "*" in print specs */ 533 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val); 534 } 535 printf_filtered ("\n"); 536 537 #endif 538 } 539 #endif /* !defined (CHILD_XFER_MEMORY). */ 540 541 542 void 543 _initialize_infptrace () 544 { 545 #if !defined (CHILD_XFER_MEMORY) 546 add_info ("udot", udot_info, 547 "Print contents of kernel ``struct user'' for current child."); 548 #endif 549 } 550