1 /* Low-level child interface to ptrace. 2 3 Copyright (C) 1988-2015 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 "command.h" 22 #include "inferior.h" 23 #include "inflow.h" 24 #include "terminal.h" 25 #include "gdbcore.h" 26 #include "regcache.h" 27 #include "gdb_ptrace.h" 28 #include "gdb_wait.h" 29 #include <signal.h> 30 31 #include "inf-ptrace.h" 32 #include "inf-child.h" 33 #include "gdbthread.h" 34 35 36 37 #ifdef PT_GET_PROCESS_STATE 38 39 /* Target hook for follow_fork. On entry and at return inferior_ptid is 40 the ptid of the followed inferior. */ 41 42 static int 43 inf_ptrace_follow_fork (struct target_ops *ops, int follow_child, 44 int detach_fork) 45 { 46 if (!follow_child) 47 { 48 struct thread_info *tp = inferior_thread (); 49 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid); 50 51 /* Breakpoints have already been detached from the child by 52 infrun.c. */ 53 54 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1) 55 perror_with_name (("ptrace")); 56 } 57 58 return 0; 59 } 60 61 static int 62 inf_ptrace_insert_fork_catchpoint (struct target_ops *self, int pid) 63 { 64 return 0; 65 } 66 67 static int 68 inf_ptrace_remove_fork_catchpoint (struct target_ops *self, int pid) 69 { 70 return 0; 71 } 72 73 #endif /* PT_GET_PROCESS_STATE */ 74 75 76 /* Prepare to be traced. */ 77 78 static void 79 inf_ptrace_me (void) 80 { 81 /* "Trace me, Dr. Memory!" */ 82 ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0); 83 } 84 85 /* Start a new inferior Unix child process. EXEC_FILE is the file to 86 run, ALLARGS is a string containing the arguments to the program. 87 ENV is the environment vector to pass. If FROM_TTY is non-zero, be 88 chatty about it. */ 89 90 static void 91 inf_ptrace_create_inferior (struct target_ops *ops, 92 char *exec_file, char *allargs, char **env, 93 int from_tty) 94 { 95 int pid; 96 97 /* Do not change either targets above or the same target if already present. 98 The reason is the target stack is shared across multiple inferiors. */ 99 int ops_already_pushed = target_is_pushed (ops); 100 struct cleanup *back_to = make_cleanup (null_cleanup, NULL); 101 102 if (! ops_already_pushed) 103 { 104 /* Clear possible core file with its process_stratum. */ 105 push_target (ops); 106 make_cleanup_unpush_target (ops); 107 } 108 109 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL, 110 NULL, NULL, NULL); 111 112 discard_cleanups (back_to); 113 114 startup_inferior (START_INFERIOR_TRAPS_EXPECTED); 115 116 /* On some targets, there must be some explicit actions taken after 117 the inferior has been started up. */ 118 target_post_startup_inferior (pid_to_ptid (pid)); 119 } 120 121 #ifdef PT_GET_PROCESS_STATE 122 123 static void 124 inf_ptrace_post_startup_inferior (struct target_ops *self, ptid_t pid) 125 { 126 ptrace_event_t pe; 127 128 /* Set the initial event mask. */ 129 memset (&pe, 0, sizeof pe); 130 pe.pe_set_event |= PTRACE_FORK; 131 if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid), 132 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) 133 perror_with_name (("ptrace")); 134 } 135 136 #endif 137 138 /* Clean up a rotting corpse of an inferior after it died. */ 139 140 static void 141 inf_ptrace_mourn_inferior (struct target_ops *ops) 142 { 143 int status; 144 145 /* Wait just one more time to collect the inferior's exit status. 146 Do not check whether this succeeds though, since we may be 147 dealing with a process that we attached to. Such a process will 148 only report its exit status to its original parent. */ 149 waitpid (ptid_get_pid (inferior_ptid), &status, 0); 150 151 inf_child_mourn_inferior (ops); 152 } 153 154 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero, 155 be chatty about it. */ 156 157 static void 158 inf_ptrace_attach (struct target_ops *ops, const char *args, int from_tty) 159 { 160 char *exec_file; 161 pid_t pid; 162 struct inferior *inf; 163 164 /* Do not change either targets above or the same target if already present. 165 The reason is the target stack is shared across multiple inferiors. */ 166 int ops_already_pushed = target_is_pushed (ops); 167 struct cleanup *back_to = make_cleanup (null_cleanup, NULL); 168 169 pid = parse_pid_to_attach (args); 170 171 if (pid == getpid ()) /* Trying to masturbate? */ 172 error (_("I refuse to debug myself!")); 173 174 if (! ops_already_pushed) 175 { 176 /* target_pid_to_str already uses the target. Also clear possible core 177 file with its process_stratum. */ 178 push_target (ops); 179 make_cleanup_unpush_target (ops); 180 } 181 182 if (from_tty) 183 { 184 exec_file = get_exec_file (0); 185 186 if (exec_file) 187 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file, 188 target_pid_to_str (pid_to_ptid (pid))); 189 else 190 printf_unfiltered (_("Attaching to %s\n"), 191 target_pid_to_str (pid_to_ptid (pid))); 192 193 gdb_flush (gdb_stdout); 194 } 195 196 #ifdef PT_ATTACH 197 errno = 0; 198 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0); 199 if (errno != 0) 200 perror_with_name (("ptrace")); 201 #else 202 error (_("This system does not support attaching to a process")); 203 #endif 204 205 inf = current_inferior (); 206 inferior_appeared (inf, pid); 207 inf->attach_flag = 1; 208 inferior_ptid = pid_to_ptid (pid); 209 210 /* Always add a main thread. If some target extends the ptrace 211 target, it should decorate the ptid later with more info. */ 212 add_thread_silent (inferior_ptid); 213 214 discard_cleanups (back_to); 215 } 216 217 #ifdef PT_GET_PROCESS_STATE 218 219 static void 220 inf_ptrace_post_attach (struct target_ops *self, int pid) 221 { 222 ptrace_event_t pe; 223 224 /* Set the initial event mask. */ 225 memset (&pe, 0, sizeof pe); 226 pe.pe_set_event |= PTRACE_FORK; 227 if (ptrace (PT_SET_EVENT_MASK, pid, 228 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) 229 perror_with_name (("ptrace")); 230 } 231 232 #endif 233 234 /* Detach from the inferior, optionally passing it the signal 235 specified by ARGS. If FROM_TTY is non-zero, be chatty about it. */ 236 237 static void 238 inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty) 239 { 240 pid_t pid = ptid_get_pid (inferior_ptid); 241 int sig = 0; 242 243 if (from_tty) 244 { 245 char *exec_file = get_exec_file (0); 246 if (exec_file == 0) 247 exec_file = ""; 248 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file, 249 target_pid_to_str (pid_to_ptid (pid))); 250 gdb_flush (gdb_stdout); 251 } 252 if (args) 253 sig = atoi (args); 254 255 #ifdef PT_DETACH 256 /* We'd better not have left any breakpoints in the program or it'll 257 die when it hits one. Also note that this may only work if we 258 previously attached to the inferior. It *might* work if we 259 started the process ourselves. */ 260 errno = 0; 261 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig); 262 if (errno != 0) 263 perror_with_name (("ptrace")); 264 #else 265 error (_("This system does not support detaching from a process")); 266 #endif 267 268 inferior_ptid = null_ptid; 269 detach_inferior (pid); 270 271 inf_child_maybe_unpush_target (ops); 272 } 273 274 /* Kill the inferior. */ 275 276 static void 277 inf_ptrace_kill (struct target_ops *ops) 278 { 279 pid_t pid = ptid_get_pid (inferior_ptid); 280 int status; 281 282 if (pid == 0) 283 return; 284 285 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0); 286 waitpid (pid, &status, 0); 287 288 target_mourn_inferior (); 289 } 290 291 /* Stop the inferior. */ 292 293 static void 294 inf_ptrace_stop (struct target_ops *self, ptid_t ptid) 295 { 296 /* Send a SIGINT to the process group. This acts just like the user 297 typed a ^C on the controlling terminal. Note that using a 298 negative process number in kill() is a System V-ism. The proper 299 BSD interface is killpg(). However, all modern BSDs support the 300 System V interface too. */ 301 kill (-inferior_process_group (), SIGINT); 302 } 303 304 /* Return which PID to pass to ptrace in order to observe/control the 305 tracee identified by PTID. */ 306 307 static pid_t 308 get_ptrace_pid (ptid_t ptid) 309 { 310 pid_t pid; 311 312 #ifndef __NetBSD__ 313 /* If we have an LWPID to work with, use it. Otherwise, we're 314 dealing with a non-threaded program/target. */ 315 pid = ptid_get_lwp (ptid); 316 if (pid == 0) 317 #endif 318 pid = ptid_get_pid (ptid); 319 return pid; 320 } 321 322 /* Resume execution of thread PTID, or all threads if PTID is -1. If 323 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it 324 that signal. */ 325 326 static void 327 inf_ptrace_resume (struct target_ops *ops, 328 ptid_t ptid, int step, enum gdb_signal signal) 329 { 330 pid_t pid; 331 int request, sig; 332 333 if (ptid_equal (minus_one_ptid, ptid)) 334 /* Resume all threads. Traditionally ptrace() only supports 335 single-threaded processes, so simply resume the inferior. */ 336 pid = ptid_get_pid (inferior_ptid); 337 else 338 pid = get_ptrace_pid (ptid); 339 340 if (catch_syscall_enabled () > 0) 341 request = PT_SYSCALL; 342 else 343 request = PT_CONTINUE; 344 345 if (step) 346 { 347 /* If this system does not support PT_STEP, a higher level 348 function will have called single_step() to transmute the step 349 request into a continue request (by setting breakpoints on 350 all possible successor instructions), so we don't have to 351 worry about that here. */ 352 request = PT_STEP; 353 #ifdef __NetBSD__ 354 /* 355 * On NetBSD the data field of PT_STEP contains the thread 356 * to be stepped; all other threads are continued if this value is > 0 357 */ 358 sig = ptid_get_lwp(ptid); 359 #else 360 sig = 0; 361 #endif 362 } else 363 sig = gdb_signal_to_host (signal); 364 365 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from 366 where it was. If GDB wanted it to start some other way, we have 367 already written a new program counter value to the child. */ 368 errno = 0; 369 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, sig); 370 if (errno != 0) 371 perror_with_name (("ptrace")); 372 } 373 374 /* Wait for the child specified by PTID to do something. Return the 375 process ID of the child, or MINUS_ONE_PTID in case of error; store 376 the status in *OURSTATUS. */ 377 378 static ptid_t 379 inf_ptrace_wait (struct target_ops *ops, 380 ptid_t ptid, struct target_waitstatus *ourstatus, int options) 381 { 382 pid_t pid; 383 int status, save_errno; 384 385 do 386 { 387 set_sigint_trap (); 388 389 do 390 { 391 pid = waitpid (ptid_get_pid (ptid), &status, 0); 392 save_errno = errno; 393 } 394 while (pid == -1 && errno == EINTR); 395 396 clear_sigint_trap (); 397 398 if (pid == -1) 399 { 400 fprintf_unfiltered (gdb_stderr, 401 _("Child process unexpectedly missing: %s.\n"), 402 safe_strerror (save_errno)); 403 404 /* Claim it exited with unknown signal. */ 405 ourstatus->kind = TARGET_WAITKIND_SIGNALLED; 406 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN; 407 return inferior_ptid; 408 } 409 410 /* Ignore terminated detached child processes. */ 411 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid)) 412 pid = -1; 413 } 414 while (pid == -1); 415 416 #ifdef PT_GET_PROCESS_STATE 417 if (WIFSTOPPED (status)) 418 { 419 ptrace_state_t pe; 420 pid_t fpid; 421 422 if (ptrace (PT_GET_PROCESS_STATE, pid, 423 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) 424 perror_with_name (("ptrace")); 425 426 switch (pe.pe_report_event) 427 { 428 case PTRACE_FORK: 429 ourstatus->kind = TARGET_WAITKIND_FORKED; 430 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid); 431 432 /* Make sure the other end of the fork is stopped too. */ 433 fpid = waitpid (pe.pe_other_pid, &status, 0); 434 if (fpid == -1) 435 perror_with_name (("waitpid")); 436 437 if (ptrace (PT_GET_PROCESS_STATE, fpid, 438 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) 439 perror_with_name (("ptrace")); 440 441 gdb_assert (pe.pe_report_event == PTRACE_FORK); 442 gdb_assert (pe.pe_other_pid == pid); 443 if (fpid == ptid_get_pid (inferior_ptid)) 444 { 445 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid); 446 return pid_to_ptid (fpid); 447 } 448 449 return pid_to_ptid (pid); 450 } 451 } 452 #endif 453 454 store_waitstatus (ourstatus, status); 455 return pid_to_ptid (pid); 456 } 457 458 /* Implement the to_xfer_partial target_ops method. */ 459 460 static enum target_xfer_status 461 inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object, 462 const char *annex, gdb_byte *readbuf, 463 const gdb_byte *writebuf, 464 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) 465 { 466 pid_t pid = ptid_get_pid (inferior_ptid); 467 468 switch (object) 469 { 470 case TARGET_OBJECT_MEMORY: 471 #ifdef PT_IO 472 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO 473 request that promises to be much more efficient in reading 474 and writing data in the traced process's address space. */ 475 { 476 struct ptrace_io_desc piod; 477 478 /* NOTE: We assume that there are no distinct address spaces 479 for instruction and data. However, on OpenBSD 3.9 and 480 later, PIOD_WRITE_D doesn't allow changing memory that's 481 mapped read-only. Since most code segments will be 482 read-only, using PIOD_WRITE_D will prevent us from 483 inserting breakpoints, so we use PIOD_WRITE_I instead. */ 484 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D; 485 piod.piod_addr = writebuf ? (void *) writebuf : readbuf; 486 piod.piod_offs = (void *) (long) offset; 487 piod.piod_len = len; 488 489 errno = 0; 490 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0) 491 { 492 /* Return the actual number of bytes read or written. */ 493 *xfered_len = piod.piod_len; 494 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; 495 } 496 /* If the PT_IO request is somehow not supported, fallback on 497 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero 498 to indicate failure. */ 499 if (errno != EINVAL) 500 return TARGET_XFER_EOF; 501 } 502 #endif 503 { 504 union 505 { 506 PTRACE_TYPE_RET word; 507 gdb_byte byte[sizeof (PTRACE_TYPE_RET)]; 508 } buffer; 509 ULONGEST rounded_offset; 510 ULONGEST partial_len; 511 512 /* Round the start offset down to the next long word 513 boundary. */ 514 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET); 515 516 /* Since ptrace will transfer a single word starting at that 517 rounded_offset the partial_len needs to be adjusted down to 518 that (remember this function only does a single transfer). 519 Should the required length be even less, adjust it down 520 again. */ 521 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset; 522 if (partial_len > len) 523 partial_len = len; 524 525 if (writebuf) 526 { 527 /* If OFFSET:PARTIAL_LEN is smaller than 528 ROUNDED_OFFSET:WORDSIZE then a read/modify write will 529 be needed. Read in the entire word. */ 530 if (rounded_offset < offset 531 || (offset + partial_len 532 < rounded_offset + sizeof (PTRACE_TYPE_RET))) 533 /* Need part of initial word -- fetch it. */ 534 buffer.word = ptrace (PT_READ_I, pid, 535 (PTRACE_TYPE_ARG3)(uintptr_t) 536 rounded_offset, 0); 537 538 /* Copy data to be written over corresponding part of 539 buffer. */ 540 memcpy (buffer.byte + (offset - rounded_offset), 541 writebuf, partial_len); 542 543 errno = 0; 544 ptrace (PT_WRITE_D, pid, 545 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, 546 buffer.word); 547 if (errno) 548 { 549 /* Using the appropriate one (I or D) is necessary for 550 Gould NP1, at least. */ 551 errno = 0; 552 ptrace (PT_WRITE_I, pid, 553 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, 554 buffer.word); 555 if (errno) 556 return TARGET_XFER_EOF; 557 } 558 } 559 560 if (readbuf) 561 { 562 errno = 0; 563 buffer.word = ptrace (PT_READ_I, pid, 564 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, 565 0); 566 if (errno) 567 return TARGET_XFER_EOF; 568 /* Copy appropriate bytes out of the buffer. */ 569 memcpy (readbuf, buffer.byte + (offset - rounded_offset), 570 partial_len); 571 } 572 573 *xfered_len = partial_len; 574 return TARGET_XFER_OK; 575 } 576 577 case TARGET_OBJECT_UNWIND_TABLE: 578 return TARGET_XFER_E_IO; 579 580 case TARGET_OBJECT_AUXV: 581 #if defined (PT_IO) && defined (PIOD_READ_AUXV) 582 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO 583 request that allows us to read the auxilliary vector. Other 584 BSD's may follow if they feel the need to support PIE. */ 585 { 586 struct ptrace_io_desc piod; 587 588 if (writebuf) 589 return TARGET_XFER_E_IO; 590 piod.piod_op = PIOD_READ_AUXV; 591 piod.piod_addr = readbuf; 592 piod.piod_offs = (void *) (long) offset; 593 piod.piod_len = len; 594 595 errno = 0; 596 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0) 597 { 598 /* Return the actual number of bytes read or written. */ 599 *xfered_len = piod.piod_len; 600 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; 601 } 602 } 603 #endif 604 return TARGET_XFER_E_IO; 605 606 case TARGET_OBJECT_WCOOKIE: 607 return TARGET_XFER_E_IO; 608 609 default: 610 return TARGET_XFER_E_IO; 611 } 612 } 613 614 /* Return non-zero if the thread specified by PTID is alive. */ 615 616 static int 617 inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid) 618 { 619 /* ??? Is kill the right way to do this? */ 620 return (kill (ptid_get_pid (ptid), 0) != -1); 621 } 622 623 /* Print status information about what we're accessing. */ 624 625 static void 626 inf_ptrace_files_info (struct target_ops *ignore) 627 { 628 struct inferior *inf = current_inferior (); 629 630 printf_filtered (_("\tUsing the running image of %s %s.\n"), 631 inf->attach_flag ? "attached" : "child", 632 target_pid_to_str (inferior_ptid)); 633 } 634 635 static char * 636 inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid) 637 { 638 return normal_pid_to_str (ptid); 639 } 640 641 #if defined (PT_IO) && defined (PIOD_READ_AUXV) 642 643 /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR. 644 Return 0 if *READPTR is already at the end of the buffer. 645 Return -1 if there is insufficient buffer for a whole entry. 646 Return 1 if an entry was read into *TYPEP and *VALP. */ 647 648 static int 649 inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr, 650 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp) 651 { 652 struct type *int_type = builtin_type (target_gdbarch ())->builtin_int; 653 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr; 654 const int sizeof_auxv_type = TYPE_LENGTH (int_type); 655 const int sizeof_auxv_val = TYPE_LENGTH (ptr_type); 656 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); 657 gdb_byte *ptr = *readptr; 658 659 if (endptr == ptr) 660 return 0; 661 662 if (endptr - ptr < 2 * sizeof_auxv_val) 663 return -1; 664 665 *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order); 666 ptr += sizeof_auxv_val; /* Alignment. */ 667 *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order); 668 ptr += sizeof_auxv_val; 669 670 *readptr = ptr; 671 return 1; 672 } 673 674 #endif 675 676 /* Create a prototype ptrace target. The client can override it with 677 local methods. */ 678 679 struct target_ops * 680 inf_ptrace_target (void) 681 { 682 struct target_ops *t = inf_child_target (); 683 684 t->to_attach = inf_ptrace_attach; 685 t->to_detach = inf_ptrace_detach; 686 t->to_resume = inf_ptrace_resume; 687 t->to_wait = inf_ptrace_wait; 688 t->to_files_info = inf_ptrace_files_info; 689 t->to_kill = inf_ptrace_kill; 690 t->to_create_inferior = inf_ptrace_create_inferior; 691 #ifdef PT_GET_PROCESS_STATE 692 t->to_follow_fork = inf_ptrace_follow_fork; 693 t->to_insert_fork_catchpoint = inf_ptrace_insert_fork_catchpoint; 694 t->to_remove_fork_catchpoint = inf_ptrace_remove_fork_catchpoint; 695 t->to_post_startup_inferior = inf_ptrace_post_startup_inferior; 696 t->to_post_attach = inf_ptrace_post_attach; 697 #endif 698 t->to_mourn_inferior = inf_ptrace_mourn_inferior; 699 t->to_thread_alive = inf_ptrace_thread_alive; 700 t->to_pid_to_str = inf_ptrace_pid_to_str; 701 t->to_stop = inf_ptrace_stop; 702 t->to_xfer_partial = inf_ptrace_xfer_partial; 703 #if defined (PT_IO) && defined (PIOD_READ_AUXV) 704 t->to_auxv_parse = inf_ptrace_auxv_parse; 705 #endif 706 707 return t; 708 } 709 710 711 /* Pointer to a function that returns the offset within the user area 712 where a particular register is stored. */ 713 static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int); 714 715 /* Fetch register REGNUM from the inferior. */ 716 717 static void 718 inf_ptrace_fetch_register (struct regcache *regcache, int regnum) 719 { 720 struct gdbarch *gdbarch = get_regcache_arch (regcache); 721 CORE_ADDR addr; 722 size_t size; 723 PTRACE_TYPE_RET *buf; 724 int pid, i; 725 726 /* This isn't really an address, but ptrace thinks of it as one. */ 727 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0); 728 if (addr == (CORE_ADDR)-1 729 || gdbarch_cannot_fetch_register (gdbarch, regnum)) 730 { 731 regcache_raw_supply (regcache, regnum, NULL); 732 return; 733 } 734 735 /* Cater for systems like GNU/Linux, that implement threads as 736 separate processes. */ 737 pid = ptid_get_lwp (inferior_ptid); 738 if (pid == 0) 739 pid = ptid_get_pid (inferior_ptid); 740 741 size = register_size (gdbarch, regnum); 742 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0); 743 buf = alloca (size); 744 745 /* Read the register contents from the inferior a chunk at a time. */ 746 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++) 747 { 748 errno = 0; 749 buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0); 750 if (errno != 0) 751 error (_("Couldn't read register %s (#%d): %s."), 752 gdbarch_register_name (gdbarch, regnum), 753 regnum, safe_strerror (errno)); 754 755 addr += sizeof (PTRACE_TYPE_RET); 756 } 757 regcache_raw_supply (regcache, regnum, buf); 758 } 759 760 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this 761 for all registers. */ 762 763 static void 764 inf_ptrace_fetch_registers (struct target_ops *ops, 765 struct regcache *regcache, int regnum) 766 { 767 if (regnum == -1) 768 for (regnum = 0; 769 regnum < gdbarch_num_regs (get_regcache_arch (regcache)); 770 regnum++) 771 inf_ptrace_fetch_register (regcache, regnum); 772 else 773 inf_ptrace_fetch_register (regcache, regnum); 774 } 775 776 /* Store register REGNUM into the inferior. */ 777 778 static void 779 inf_ptrace_store_register (const struct regcache *regcache, int regnum) 780 { 781 struct gdbarch *gdbarch = get_regcache_arch (regcache); 782 CORE_ADDR addr; 783 size_t size; 784 PTRACE_TYPE_RET *buf; 785 int pid, i; 786 787 /* This isn't really an address, but ptrace thinks of it as one. */ 788 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1); 789 if (addr == (CORE_ADDR)-1 790 || gdbarch_cannot_store_register (gdbarch, regnum)) 791 return; 792 793 /* Cater for systems like GNU/Linux, that implement threads as 794 separate processes. */ 795 pid = ptid_get_lwp (inferior_ptid); 796 if (pid == 0) 797 pid = ptid_get_pid (inferior_ptid); 798 799 size = register_size (gdbarch, regnum); 800 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0); 801 buf = alloca (size); 802 803 /* Write the register contents into the inferior a chunk at a time. */ 804 regcache_raw_collect (regcache, regnum, buf); 805 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++) 806 { 807 errno = 0; 808 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]); 809 if (errno != 0) 810 error (_("Couldn't write register %s (#%d): %s."), 811 gdbarch_register_name (gdbarch, regnum), 812 regnum, safe_strerror (errno)); 813 814 addr += sizeof (PTRACE_TYPE_RET); 815 } 816 } 817 818 /* Store register REGNUM back into the inferior. If REGNUM is -1, do 819 this for all registers. */ 820 821 static void 822 inf_ptrace_store_registers (struct target_ops *ops, 823 struct regcache *regcache, int regnum) 824 { 825 if (regnum == -1) 826 for (regnum = 0; 827 regnum < gdbarch_num_regs (get_regcache_arch (regcache)); 828 regnum++) 829 inf_ptrace_store_register (regcache, regnum); 830 else 831 inf_ptrace_store_register (regcache, regnum); 832 } 833 834 /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be 835 a function returning the offset within the user area where a 836 particular register is stored. */ 837 838 struct target_ops * 839 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset) 840 (struct gdbarch *, int, int)) 841 { 842 struct target_ops *t = inf_ptrace_target(); 843 844 gdb_assert (register_u_offset); 845 inf_ptrace_register_u_offset = register_u_offset; 846 t->to_fetch_registers = inf_ptrace_fetch_registers; 847 t->to_store_registers = inf_ptrace_store_registers; 848 849 return t; 850 } 851