1 /* GNU/Linux native-dependent code common to multiple platforms. 2 3 Copyright (C) 2001-2024 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 "inferior.h" 21 #include "infrun.h" 22 #include "target.h" 23 #include "nat/linux-nat.h" 24 #include "nat/linux-waitpid.h" 25 #include "gdbsupport/gdb_wait.h" 26 #include <unistd.h> 27 #include <sys/syscall.h> 28 #include "nat/gdb_ptrace.h" 29 #include "linux-nat.h" 30 #include "nat/linux-ptrace.h" 31 #include "nat/linux-procfs.h" 32 #include "nat/linux-personality.h" 33 #include "linux-fork.h" 34 #include "gdbthread.h" 35 #include "cli/cli-cmds.h" 36 #include "regcache.h" 37 #include "regset.h" 38 #include "inf-child.h" 39 #include "inf-ptrace.h" 40 #include "auxv.h" 41 #include <sys/procfs.h> 42 #include "elf-bfd.h" 43 #include "gregset.h" 44 #include "gdbcore.h" 45 #include <ctype.h> 46 #include <sys/stat.h> 47 #include <fcntl.h> 48 #include "inf-loop.h" 49 #include "gdbsupport/event-loop.h" 50 #include "event-top.h" 51 #include <pwd.h> 52 #include <sys/types.h> 53 #include <dirent.h> 54 #include "xml-support.h" 55 #include <sys/vfs.h> 56 #include "solib.h" 57 #include "nat/linux-osdata.h" 58 #include "linux-tdep.h" 59 #include "symfile.h" 60 #include "gdbsupport/agent.h" 61 #include "tracepoint.h" 62 #include "target-descriptions.h" 63 #include "gdbsupport/filestuff.h" 64 #include "objfiles.h" 65 #include "nat/linux-namespaces.h" 66 #include "gdbsupport/block-signals.h" 67 #include "gdbsupport/fileio.h" 68 #include "gdbsupport/scope-exit.h" 69 #include "gdbsupport/gdb-sigmask.h" 70 #include "gdbsupport/common-debug.h" 71 #include <unordered_map> 72 73 /* This comment documents high-level logic of this file. 74 75 Waiting for events in sync mode 76 =============================== 77 78 When waiting for an event in a specific thread, we just use waitpid, 79 passing the specific pid, and not passing WNOHANG. 80 81 When waiting for an event in all threads, waitpid is not quite good: 82 83 - If the thread group leader exits while other threads in the thread 84 group still exist, waitpid(TGID, ...) hangs. That waitpid won't 85 return an exit status until the other threads in the group are 86 reaped. 87 88 - When a non-leader thread execs, that thread just vanishes without 89 reporting an exit (so we'd hang if we waited for it explicitly in 90 that case). The exec event is instead reported to the TGID pid. 91 92 The solution is to always use -1 and WNOHANG, together with 93 sigsuspend. 94 95 First, we use non-blocking waitpid to check for events. If nothing is 96 found, we use sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, 97 it means something happened to a child process. As soon as we know 98 there's an event, we get back to calling nonblocking waitpid. 99 100 Note that SIGCHLD should be blocked between waitpid and sigsuspend 101 calls, so that we don't miss a signal. If SIGCHLD arrives in between, 102 when it's blocked, the signal becomes pending and sigsuspend 103 immediately notices it and returns. 104 105 Waiting for events in async mode (TARGET_WNOHANG) 106 ================================================= 107 108 In async mode, GDB should always be ready to handle both user input 109 and target events, so neither blocking waitpid nor sigsuspend are 110 viable options. Instead, we should asynchronously notify the GDB main 111 event loop whenever there's an unprocessed event from the target. We 112 detect asynchronous target events by handling SIGCHLD signals. To 113 notify the event loop about target events, an event pipe is used 114 --- the pipe is registered as waitable event source in the event loop, 115 the event loop select/poll's on the read end of this pipe (as well on 116 other event sources, e.g., stdin), and the SIGCHLD handler marks the 117 event pipe to raise an event. This is more portable than relying on 118 pselect/ppoll, since on kernels that lack those syscalls, libc 119 emulates them with select/poll+sigprocmask, and that is racy 120 (a.k.a. plain broken). 121 122 Obviously, if we fail to notify the event loop if there's a target 123 event, it's bad. OTOH, if we notify the event loop when there's no 124 event from the target, linux_nat_wait will detect that there's no real 125 event to report, and return event of type TARGET_WAITKIND_IGNORE. 126 This is mostly harmless, but it will waste time and is better avoided. 127 128 The main design point is that every time GDB is outside linux-nat.c, 129 we have a SIGCHLD handler installed that is called when something 130 happens to the target and notifies the GDB event loop. Whenever GDB 131 core decides to handle the event, and calls into linux-nat.c, we 132 process things as in sync mode, except that the we never block in 133 sigsuspend. 134 135 While processing an event, we may end up momentarily blocked in 136 waitpid calls. Those waitpid calls, while blocking, are guarantied to 137 return quickly. E.g., in all-stop mode, before reporting to the core 138 that an LWP hit a breakpoint, all LWPs are stopped by sending them 139 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported. 140 Note that this is different from blocking indefinitely waiting for the 141 next event --- here, we're already handling an event. 142 143 Use of signals 144 ============== 145 146 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another 147 signal is not entirely significant; we just need for a signal to be delivered, 148 so that we can intercept it. SIGSTOP's advantage is that it can not be 149 blocked. A disadvantage is that it is not a real-time signal, so it can only 150 be queued once; we do not keep track of other sources of SIGSTOP. 151 152 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't 153 use them, because they have special behavior when the signal is generated - 154 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL 155 kills the entire thread group. 156 157 A delivered SIGSTOP would stop the entire thread group, not just the thread we 158 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and 159 cancel it (by PTRACE_CONT without passing SIGSTOP). 160 161 We could use a real-time signal instead. This would solve those problems; we 162 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB. 163 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH 164 generates it, and there are races with trying to find a signal that is not 165 blocked. 166 167 Exec events 168 =========== 169 170 The case of a thread group (process) with 3 or more threads, and a 171 thread other than the leader execs is worth detailing: 172 173 On an exec, the Linux kernel destroys all threads except the execing 174 one in the thread group, and resets the execing thread's tid to the 175 tgid. No exit notification is sent for the execing thread -- from the 176 ptracer's perspective, it appears as though the execing thread just 177 vanishes. Until we reap all other threads except the leader and the 178 execing thread, the leader will be zombie, and the execing thread will 179 be in `D (disc sleep)' state. As soon as all other threads are 180 reaped, the execing thread changes its tid to the tgid, and the 181 previous (zombie) leader vanishes, giving place to the "new" 182 leader. 183 184 Accessing inferior memory 185 ========================= 186 187 To access inferior memory, we strongly prefer /proc/PID/mem. We 188 fallback to ptrace if and only if /proc/PID/mem is not writable, as a 189 concession for obsolescent kernels (such as found in RHEL6). For 190 modern kernels, the fallback shouldn't trigger. GDBserver does not 191 have the ptrace fallback already, and at some point, we'll consider 192 removing it from native GDB too. 193 194 /proc/PID/mem has a few advantages over alternatives like 195 PTRACE_PEEKTEXT/PTRACE_POKETEXT or process_vm_readv/process_vm_writev: 196 197 - Because we can use a single read/write call, /proc/PID/mem can be 198 much more efficient than banging away at 199 PTRACE_PEEKTEXT/PTRACE_POKETEXT, one word at a time. 200 201 - /proc/PID/mem allows writing to read-only pages, which we need to 202 e.g., plant breakpoint instructions. process_vm_writev does not 203 allow this. 204 205 - /proc/PID/mem allows memory access even if all threads are running. 206 OTOH, PTRACE_PEEKTEXT/PTRACE_POKETEXT require passing down the tid 207 of a stopped task. This lets us e.g., install breakpoints while the 208 inferior is running, clear a displaced stepping scratch pad when the 209 thread that was displaced stepping exits, print inferior globals, 210 etc., all without having to worry about temporarily pausing some 211 thread. 212 213 - /proc/PID/mem does not suffer from a race that could cause us to 214 access memory of the wrong address space when the inferior execs. 215 216 process_vm_readv/process_vm_writev have this problem. 217 218 E.g., say GDB decides to write to memory just while the inferior 219 execs. In this scenario, GDB could write memory to the post-exec 220 address space thinking it was writing to the pre-exec address space, 221 with high probability of corrupting the inferior. Or if GDB decides 222 instead to read memory just while the inferior execs, it could read 223 bogus contents out of the wrong address space. 224 225 ptrace used to have this problem too, but no longer has since Linux 226 commit dbb5afad100a ("ptrace: make ptrace() fail if the tracee 227 changed its pid unexpectedly"), in Linux 5.13. (And if ptrace were 228 ever changed to allow access memory via zombie or running threads, 229 it would better not forget to consider this scenario.) 230 231 We avoid this race with /proc/PID/mem, by opening the file as soon 232 as we start debugging the inferior, when it is known the inferior is 233 stopped, and holding on to the open file descriptor, to be used 234 whenever we need to access inferior memory. If the inferior execs 235 or exits, reading/writing from/to the file returns 0 (EOF), 236 indicating the address space is gone, and so we return 237 TARGET_XFER_EOF to the core. We close the old file and open a new 238 one when we finally see the PTRACE_EVENT_EXEC event. */ 239 240 #ifndef O_LARGEFILE 241 #define O_LARGEFILE 0 242 #endif 243 244 struct linux_nat_target *linux_target; 245 246 /* See nat/linux-nat.h. */ 247 enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN; 248 249 /* When true, print debug messages relating to the linux native target. */ 250 251 static bool debug_linux_nat; 252 253 /* Implement 'show debug linux-nat'. */ 254 255 static void 256 show_debug_linux_nat (struct ui_file *file, int from_tty, 257 struct cmd_list_element *c, const char *value) 258 { 259 gdb_printf (file, _("Debugging of GNU/Linux native targets is %s.\n"), 260 value); 261 } 262 263 /* Print a linux-nat debug statement. */ 264 265 #define linux_nat_debug_printf(fmt, ...) \ 266 debug_prefixed_printf_cond (debug_linux_nat, "linux-nat", fmt, ##__VA_ARGS__) 267 268 /* Print "linux-nat" enter/exit debug statements. */ 269 270 #define LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT \ 271 scoped_debug_enter_exit (debug_linux_nat, "linux-nat") 272 273 struct simple_pid_list 274 { 275 int pid; 276 int status; 277 struct simple_pid_list *next; 278 }; 279 static struct simple_pid_list *stopped_pids; 280 281 /* Whether target_thread_events is in effect. */ 282 static bool report_thread_events; 283 284 static int kill_lwp (int lwpid, int signo); 285 286 static int stop_callback (struct lwp_info *lp); 287 288 static void block_child_signals (sigset_t *prev_mask); 289 static void restore_child_signals_mask (sigset_t *prev_mask); 290 291 struct lwp_info; 292 static struct lwp_info *add_lwp (ptid_t ptid); 293 static void purge_lwp_list (int pid); 294 static void delete_lwp (ptid_t ptid); 295 static struct lwp_info *find_lwp_pid (ptid_t ptid); 296 297 static int lwp_status_pending_p (struct lwp_info *lp); 298 299 static bool is_lwp_marked_dead (lwp_info *lp); 300 301 static void save_stop_reason (struct lwp_info *lp); 302 303 static bool proc_mem_file_is_writable (); 304 static void close_proc_mem_file (pid_t pid); 305 static void open_proc_mem_file (ptid_t ptid); 306 307 /* Return TRUE if LWP is the leader thread of the process. */ 308 309 static bool 310 is_leader (lwp_info *lp) 311 { 312 return lp->ptid.pid () == lp->ptid.lwp (); 313 } 314 315 /* Convert an LWP's pending status to a std::string. */ 316 317 static std::string 318 pending_status_str (lwp_info *lp) 319 { 320 gdb_assert (lwp_status_pending_p (lp)); 321 322 if (lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE) 323 return lp->waitstatus.to_string (); 324 else 325 return status_to_str (lp->status); 326 } 327 328 /* Return true if we should report exit events for LP. */ 329 330 static bool 331 report_exit_events_for (lwp_info *lp) 332 { 333 thread_info *thr = linux_target->find_thread (lp->ptid); 334 gdb_assert (thr != nullptr); 335 336 return (report_thread_events 337 || (thr->thread_options () & GDB_THREAD_OPTION_EXIT) != 0); 338 } 339 340 341 /* LWP accessors. */ 343 344 /* See nat/linux-nat.h. */ 345 346 ptid_t 347 ptid_of_lwp (struct lwp_info *lwp) 348 { 349 return lwp->ptid; 350 } 351 352 /* See nat/linux-nat.h. */ 353 354 void 355 lwp_set_arch_private_info (struct lwp_info *lwp, 356 struct arch_lwp_info *info) 357 { 358 lwp->arch_private = info; 359 } 360 361 /* See nat/linux-nat.h. */ 362 363 struct arch_lwp_info * 364 lwp_arch_private_info (struct lwp_info *lwp) 365 { 366 return lwp->arch_private; 367 } 368 369 /* See nat/linux-nat.h. */ 370 371 int 372 lwp_is_stopped (struct lwp_info *lwp) 373 { 374 return lwp->stopped; 375 } 376 377 /* See nat/linux-nat.h. */ 378 379 enum target_stop_reason 380 lwp_stop_reason (struct lwp_info *lwp) 381 { 382 return lwp->stop_reason; 383 } 384 385 /* See nat/linux-nat.h. */ 386 387 int 388 lwp_is_stepping (struct lwp_info *lwp) 389 { 390 return lwp->step; 391 } 392 393 394 /* Trivial list manipulation functions to keep track of a list of 396 new stopped processes. */ 397 static void 398 add_to_pid_list (struct simple_pid_list **listp, int pid, int status) 399 { 400 struct simple_pid_list *new_pid = XNEW (struct simple_pid_list); 401 402 new_pid->pid = pid; 403 new_pid->status = status; 404 new_pid->next = *listp; 405 *listp = new_pid; 406 } 407 408 static int 409 pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp) 410 { 411 struct simple_pid_list **p; 412 413 for (p = listp; *p != NULL; p = &(*p)->next) 414 if ((*p)->pid == pid) 415 { 416 struct simple_pid_list *next = (*p)->next; 417 418 *statusp = (*p)->status; 419 xfree (*p); 420 *p = next; 421 return 1; 422 } 423 return 0; 424 } 425 426 /* Return the ptrace options that we want to try to enable. */ 427 428 static int 429 linux_nat_ptrace_options (int attached) 430 { 431 int options = 0; 432 433 if (!attached) 434 options |= PTRACE_O_EXITKILL; 435 436 options |= (PTRACE_O_TRACESYSGOOD 437 | PTRACE_O_TRACEVFORKDONE 438 | PTRACE_O_TRACEVFORK 439 | PTRACE_O_TRACEFORK 440 | PTRACE_O_TRACEEXEC); 441 442 return options; 443 } 444 445 /* Initialize ptrace and procfs warnings and check for supported 446 ptrace features given PID. 447 448 ATTACHED should be nonzero iff we attached to the inferior. */ 449 450 static void 451 linux_init_ptrace_procfs (pid_t pid, int attached) 452 { 453 int options = linux_nat_ptrace_options (attached); 454 455 linux_enable_event_reporting (pid, options); 456 linux_ptrace_init_warnings (); 457 linux_proc_init_warnings (); 458 proc_mem_file_is_writable (); 459 460 /* Let the arch-specific native code do any needed initialization. 461 Some architectures need to call ptrace to check for hardware 462 watchpoints support, etc. Call it now, when we know the tracee 463 is ptrace-stopped. */ 464 linux_target->low_init_process (pid); 465 } 466 467 linux_nat_target::~linux_nat_target () 468 {} 469 470 void 471 linux_nat_target::post_attach (int pid) 472 { 473 linux_init_ptrace_procfs (pid, 1); 474 } 475 476 /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */ 477 478 void 479 linux_nat_target::post_startup_inferior (ptid_t ptid) 480 { 481 linux_init_ptrace_procfs (ptid.pid (), 0); 482 } 483 484 /* Return the number of known LWPs in the tgid given by PID. */ 485 486 static int 487 num_lwps (int pid) 488 { 489 int count = 0; 490 491 for (const lwp_info *lp ATTRIBUTE_UNUSED : all_lwps ()) 492 if (lp->ptid.pid () == pid) 493 count++; 494 495 return count; 496 } 497 498 /* Deleter for lwp_info unique_ptr specialisation. */ 499 500 struct lwp_deleter 501 { 502 void operator() (struct lwp_info *lwp) const 503 { 504 delete_lwp (lwp->ptid); 505 } 506 }; 507 508 /* A unique_ptr specialisation for lwp_info. */ 509 510 typedef std::unique_ptr<struct lwp_info, lwp_deleter> lwp_info_up; 511 512 /* Target hook for follow_fork. */ 513 514 void 515 linux_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid, 516 target_waitkind fork_kind, bool follow_child, 517 bool detach_fork) 518 { 519 inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind, 520 follow_child, detach_fork); 521 522 if (!follow_child) 523 { 524 bool has_vforked = fork_kind == TARGET_WAITKIND_VFORKED; 525 ptid_t parent_ptid = inferior_ptid; 526 int parent_pid = parent_ptid.lwp (); 527 int child_pid = child_ptid.lwp (); 528 529 /* We're already attached to the parent, by default. */ 530 lwp_info *child_lp = add_lwp (child_ptid); 531 child_lp->stopped = 1; 532 child_lp->last_resume_kind = resume_stop; 533 534 /* Detach new forked process? */ 535 if (detach_fork) 536 { 537 int child_stop_signal = 0; 538 bool detach_child = true; 539 540 /* Move CHILD_LP into a unique_ptr and clear the source pointer 541 to prevent us doing anything stupid with it. */ 542 lwp_info_up child_lp_ptr (child_lp); 543 child_lp = nullptr; 544 545 linux_target->low_prepare_to_resume (child_lp_ptr.get ()); 546 547 /* When debugging an inferior in an architecture that supports 548 hardware single stepping on a kernel without commit 549 6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child 550 process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits 551 set if the parent process had them set. 552 To work around this, single step the child process 553 once before detaching to clear the flags. */ 554 555 /* Note that we consult the parent's architecture instead of 556 the child's because there's no inferior for the child at 557 this point. */ 558 if (!gdbarch_software_single_step_p (target_thread_architecture 559 (parent_ptid))) 560 { 561 int status; 562 563 linux_disable_event_reporting (child_pid); 564 if (ptrace (PTRACE_SINGLESTEP, child_pid, 0, 0) < 0) 565 perror_with_name (_("Couldn't do single step")); 566 if (my_waitpid (child_pid, &status, 0) < 0) 567 perror_with_name (_("Couldn't wait vfork process")); 568 else 569 { 570 detach_child = WIFSTOPPED (status); 571 child_stop_signal = WSTOPSIG (status); 572 } 573 } 574 575 if (detach_child) 576 { 577 int signo = child_stop_signal; 578 579 if (signo != 0 580 && !signal_pass_state (gdb_signal_from_host (signo))) 581 signo = 0; 582 ptrace (PTRACE_DETACH, child_pid, 0, signo); 583 584 close_proc_mem_file (child_pid); 585 } 586 } 587 588 if (has_vforked) 589 { 590 lwp_info *parent_lp = find_lwp_pid (parent_ptid); 591 linux_nat_debug_printf ("waiting for VFORK_DONE on %d", parent_pid); 592 parent_lp->stopped = 1; 593 594 /* We'll handle the VFORK_DONE event like any other 595 event, in target_wait. */ 596 } 597 } 598 else 599 { 600 struct lwp_info *child_lp; 601 602 child_lp = add_lwp (child_ptid); 603 child_lp->stopped = 1; 604 child_lp->last_resume_kind = resume_stop; 605 } 606 } 607 608 609 int 611 linux_nat_target::insert_fork_catchpoint (int pid) 612 { 613 return 0; 614 } 615 616 int 617 linux_nat_target::remove_fork_catchpoint (int pid) 618 { 619 return 0; 620 } 621 622 int 623 linux_nat_target::insert_vfork_catchpoint (int pid) 624 { 625 return 0; 626 } 627 628 int 629 linux_nat_target::remove_vfork_catchpoint (int pid) 630 { 631 return 0; 632 } 633 634 int 635 linux_nat_target::insert_exec_catchpoint (int pid) 636 { 637 return 0; 638 } 639 640 int 641 linux_nat_target::remove_exec_catchpoint (int pid) 642 { 643 return 0; 644 } 645 646 int 647 linux_nat_target::set_syscall_catchpoint (int pid, bool needed, int any_count, 648 gdb::array_view<const int> syscall_counts) 649 { 650 /* On GNU/Linux, we ignore the arguments. It means that we only 651 enable the syscall catchpoints, but do not disable them. 652 653 Also, we do not use the `syscall_counts' information because we do not 654 filter system calls here. We let GDB do the logic for us. */ 655 return 0; 656 } 657 658 /* List of known LWPs, keyed by LWP PID. This speeds up the common 659 case of mapping a PID returned from the kernel to our corresponding 660 lwp_info data structure. */ 661 static htab_t lwp_lwpid_htab; 662 663 /* Calculate a hash from a lwp_info's LWP PID. */ 664 665 static hashval_t 666 lwp_info_hash (const void *ap) 667 { 668 const struct lwp_info *lp = (struct lwp_info *) ap; 669 pid_t pid = lp->ptid.lwp (); 670 671 return iterative_hash_object (pid, 0); 672 } 673 674 /* Equality function for the lwp_info hash table. Compares the LWP's 675 PID. */ 676 677 static int 678 lwp_lwpid_htab_eq (const void *a, const void *b) 679 { 680 const struct lwp_info *entry = (const struct lwp_info *) a; 681 const struct lwp_info *element = (const struct lwp_info *) b; 682 683 return entry->ptid.lwp () == element->ptid.lwp (); 684 } 685 686 /* Create the lwp_lwpid_htab hash table. */ 687 688 static void 689 lwp_lwpid_htab_create (void) 690 { 691 lwp_lwpid_htab = htab_create (100, lwp_info_hash, lwp_lwpid_htab_eq, NULL); 692 } 693 694 /* Add LP to the hash table. */ 695 696 static void 697 lwp_lwpid_htab_add_lwp (struct lwp_info *lp) 698 { 699 void **slot; 700 701 slot = htab_find_slot (lwp_lwpid_htab, lp, INSERT); 702 gdb_assert (slot != NULL && *slot == NULL); 703 *slot = lp; 704 } 705 706 /* Head of doubly-linked list of known LWPs. Sorted by reverse 707 creation order. This order is assumed in some cases. E.g., 708 reaping status after killing alls lwps of a process: the leader LWP 709 must be reaped last. */ 710 711 static intrusive_list<lwp_info> lwp_list; 712 713 /* See linux-nat.h. */ 714 715 lwp_info_range 716 all_lwps () 717 { 718 return lwp_info_range (lwp_list.begin ()); 719 } 720 721 /* See linux-nat.h. */ 722 723 lwp_info_safe_range 724 all_lwps_safe () 725 { 726 return lwp_info_safe_range (lwp_list.begin ()); 727 } 728 729 /* Add LP to sorted-by-reverse-creation-order doubly-linked list. */ 730 731 static void 732 lwp_list_add (struct lwp_info *lp) 733 { 734 lwp_list.push_front (*lp); 735 } 736 737 /* Remove LP from sorted-by-reverse-creation-order doubly-linked 738 list. */ 739 740 static void 741 lwp_list_remove (struct lwp_info *lp) 742 { 743 /* Remove from sorted-by-creation-order list. */ 744 lwp_list.erase (lwp_list.iterator_to (*lp)); 745 } 746 747 748 750 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in 751 _initialize_linux_nat. */ 752 static sigset_t suspend_mask; 753 754 /* Signals to block to make that sigsuspend work. */ 755 static sigset_t blocked_mask; 756 757 /* SIGCHLD action. */ 758 static struct sigaction sigchld_action; 759 760 /* Block child signals (SIGCHLD and linux threads signals), and store 761 the previous mask in PREV_MASK. */ 762 763 static void 764 block_child_signals (sigset_t *prev_mask) 765 { 766 /* Make sure SIGCHLD is blocked. */ 767 if (!sigismember (&blocked_mask, SIGCHLD)) 768 sigaddset (&blocked_mask, SIGCHLD); 769 770 gdb_sigmask (SIG_BLOCK, &blocked_mask, prev_mask); 771 } 772 773 /* Restore child signals mask, previously returned by 774 block_child_signals. */ 775 776 static void 777 restore_child_signals_mask (sigset_t *prev_mask) 778 { 779 gdb_sigmask (SIG_SETMASK, prev_mask, NULL); 780 } 781 782 /* Mask of signals to pass directly to the inferior. */ 783 static sigset_t pass_mask; 784 785 /* Update signals to pass to the inferior. */ 786 void 787 linux_nat_target::pass_signals 788 (gdb::array_view<const unsigned char> pass_signals) 789 { 790 int signo; 791 792 sigemptyset (&pass_mask); 793 794 for (signo = 1; signo < NSIG; signo++) 795 { 796 int target_signo = gdb_signal_from_host (signo); 797 if (target_signo < pass_signals.size () && pass_signals[target_signo]) 798 sigaddset (&pass_mask, signo); 799 } 800 } 801 802 803 805 /* Prototypes for local functions. */ 806 static int stop_wait_callback (struct lwp_info *lp); 807 static int resume_stopped_resumed_lwps (struct lwp_info *lp, const ptid_t wait_ptid); 808 static int check_ptrace_stopped_lwp_gone (struct lwp_info *lp); 809 810 811 813 /* Destroy and free LP. */ 814 815 lwp_info::~lwp_info () 816 { 817 /* Let the arch specific bits release arch_lwp_info. */ 818 linux_target->low_delete_thread (this->arch_private); 819 } 820 821 /* Traversal function for purge_lwp_list. */ 822 823 static int 824 lwp_lwpid_htab_remove_pid (void **slot, void *info) 825 { 826 struct lwp_info *lp = (struct lwp_info *) *slot; 827 int pid = *(int *) info; 828 829 if (lp->ptid.pid () == pid) 830 { 831 htab_clear_slot (lwp_lwpid_htab, slot); 832 lwp_list_remove (lp); 833 delete lp; 834 } 835 836 return 1; 837 } 838 839 /* Remove all LWPs belong to PID from the lwp list. */ 840 841 static void 842 purge_lwp_list (int pid) 843 { 844 htab_traverse_noresize (lwp_lwpid_htab, lwp_lwpid_htab_remove_pid, &pid); 845 } 846 847 /* Add the LWP specified by PTID to the list. PTID is the first LWP 848 in the process. Return a pointer to the structure describing the 849 new LWP. 850 851 This differs from add_lwp in that we don't let the arch specific 852 bits know about this new thread. Current clients of this callback 853 take the opportunity to install watchpoints in the new thread, and 854 we shouldn't do that for the first thread. If we're spawning a 855 child ("run"), the thread executes the shell wrapper first, and we 856 shouldn't touch it until it execs the program we want to debug. 857 For "attach", it'd be okay to call the callback, but it's not 858 necessary, because watchpoints can't yet have been inserted into 859 the inferior. */ 860 861 static struct lwp_info * 862 add_initial_lwp (ptid_t ptid) 863 { 864 gdb_assert (ptid.lwp_p ()); 865 866 lwp_info *lp = new lwp_info (ptid); 867 868 869 /* Add to sorted-by-reverse-creation-order list. */ 870 lwp_list_add (lp); 871 872 /* Add to keyed-by-pid htab. */ 873 lwp_lwpid_htab_add_lwp (lp); 874 875 return lp; 876 } 877 878 /* Add the LWP specified by PID to the list. Return a pointer to the 879 structure describing the new LWP. The LWP should already be 880 stopped. */ 881 882 static struct lwp_info * 883 add_lwp (ptid_t ptid) 884 { 885 struct lwp_info *lp; 886 887 lp = add_initial_lwp (ptid); 888 889 /* Let the arch specific bits know about this new thread. Current 890 clients of this callback take the opportunity to install 891 watchpoints in the new thread. We don't do this for the first 892 thread though. See add_initial_lwp. */ 893 linux_target->low_new_thread (lp); 894 895 return lp; 896 } 897 898 /* Remove the LWP specified by PID from the list. */ 899 900 static void 901 delete_lwp (ptid_t ptid) 902 { 903 lwp_info dummy (ptid); 904 905 void **slot = htab_find_slot (lwp_lwpid_htab, &dummy, NO_INSERT); 906 if (slot == NULL) 907 return; 908 909 lwp_info *lp = *(struct lwp_info **) slot; 910 gdb_assert (lp != NULL); 911 912 htab_clear_slot (lwp_lwpid_htab, slot); 913 914 /* Remove from sorted-by-creation-order list. */ 915 lwp_list_remove (lp); 916 917 /* Release. */ 918 delete lp; 919 } 920 921 /* Return a pointer to the structure describing the LWP corresponding 922 to PID. If no corresponding LWP could be found, return NULL. */ 923 924 static struct lwp_info * 925 find_lwp_pid (ptid_t ptid) 926 { 927 int lwp; 928 929 if (ptid.lwp_p ()) 930 lwp = ptid.lwp (); 931 else 932 lwp = ptid.pid (); 933 934 lwp_info dummy (ptid_t (0, lwp)); 935 return (struct lwp_info *) htab_find (lwp_lwpid_htab, &dummy); 936 } 937 938 /* See nat/linux-nat.h. */ 939 940 struct lwp_info * 941 iterate_over_lwps (ptid_t filter, 942 gdb::function_view<iterate_over_lwps_ftype> callback) 943 { 944 for (lwp_info *lp : all_lwps_safe ()) 945 { 946 if (lp->ptid.matches (filter)) 947 { 948 if (callback (lp) != 0) 949 return lp; 950 } 951 } 952 953 return NULL; 954 } 955 956 /* Update our internal state when changing from one checkpoint to 957 another indicated by NEW_PTID. We can only switch single-threaded 958 applications, so we only create one new LWP, and the previous list 959 is discarded. */ 960 961 void 962 linux_nat_switch_fork (ptid_t new_ptid) 963 { 964 struct lwp_info *lp; 965 966 purge_lwp_list (inferior_ptid.pid ()); 967 968 lp = add_lwp (new_ptid); 969 lp->stopped = 1; 970 971 /* This changes the thread's ptid while preserving the gdb thread 972 num. Also changes the inferior pid, while preserving the 973 inferior num. */ 974 thread_change_ptid (linux_target, inferior_ptid, new_ptid); 975 976 /* We've just told GDB core that the thread changed target id, but, 977 in fact, it really is a different thread, with different register 978 contents. */ 979 registers_changed (); 980 } 981 982 /* Handle the exit of a single thread LP. If DEL_THREAD is true, 983 delete the thread_info associated to LP, if it exists. */ 984 985 static void 986 exit_lwp (struct lwp_info *lp, bool del_thread = true) 987 { 988 struct thread_info *th = linux_target->find_thread (lp->ptid); 989 990 if (th != nullptr && del_thread) 991 delete_thread (th); 992 993 delete_lwp (lp->ptid); 994 } 995 996 /* Wait for the LWP specified by LP, which we have just attached to. 997 Returns a wait status for that LWP, to cache. */ 998 999 static int 1000 linux_nat_post_attach_wait (ptid_t ptid, int *signalled) 1001 { 1002 pid_t new_pid, pid = ptid.lwp (); 1003 int status; 1004 1005 if (linux_proc_pid_is_stopped (pid)) 1006 { 1007 linux_nat_debug_printf ("Attaching to a stopped process"); 1008 1009 /* The process is definitely stopped. It is in a job control 1010 stop, unless the kernel predates the TASK_STOPPED / 1011 TASK_TRACED distinction, in which case it might be in a 1012 ptrace stop. Make sure it is in a ptrace stop; from there we 1013 can kill it, signal it, et cetera. 1014 1015 First make sure there is a pending SIGSTOP. Since we are 1016 already attached, the process can not transition from stopped 1017 to running without a PTRACE_CONT; so we know this signal will 1018 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is 1019 probably already in the queue (unless this kernel is old 1020 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP 1021 is not an RT signal, it can only be queued once. */ 1022 kill_lwp (pid, SIGSTOP); 1023 1024 /* Finally, resume the stopped process. This will deliver the SIGSTOP 1025 (or a higher priority signal, just like normal PTRACE_ATTACH). */ 1026 ptrace (PTRACE_CONT, pid, 0, 0); 1027 } 1028 1029 /* Make sure the initial process is stopped. The user-level threads 1030 layer might want to poke around in the inferior, and that won't 1031 work if things haven't stabilized yet. */ 1032 new_pid = my_waitpid (pid, &status, __WALL); 1033 gdb_assert (pid == new_pid); 1034 1035 if (!WIFSTOPPED (status)) 1036 { 1037 /* The pid we tried to attach has apparently just exited. */ 1038 linux_nat_debug_printf ("Failed to stop %d: %s", pid, 1039 status_to_str (status).c_str ()); 1040 return status; 1041 } 1042 1043 if (WSTOPSIG (status) != SIGSTOP) 1044 { 1045 *signalled = 1; 1046 linux_nat_debug_printf ("Received %s after attaching", 1047 status_to_str (status).c_str ()); 1048 } 1049 1050 return status; 1051 } 1052 1053 void 1054 linux_nat_target::create_inferior (const char *exec_file, 1055 const std::string &allargs, 1056 char **env, int from_tty) 1057 { 1058 maybe_disable_address_space_randomization restore_personality 1059 (disable_randomization); 1060 1061 /* The fork_child mechanism is synchronous and calls target_wait, so 1062 we have to mask the async mode. */ 1063 1064 /* Make sure we report all signals during startup. */ 1065 pass_signals ({}); 1066 1067 inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty); 1068 1069 open_proc_mem_file (inferior_ptid); 1070 } 1071 1072 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not 1073 already attached. Returns true if a new LWP is found, false 1074 otherwise. */ 1075 1076 static int 1077 attach_proc_task_lwp_callback (ptid_t ptid) 1078 { 1079 struct lwp_info *lp; 1080 1081 /* Ignore LWPs we're already attached to. */ 1082 lp = find_lwp_pid (ptid); 1083 if (lp == NULL) 1084 { 1085 int lwpid = ptid.lwp (); 1086 1087 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0) 1088 { 1089 int err = errno; 1090 1091 /* Be quiet if we simply raced with the thread exiting. 1092 EPERM is returned if the thread's task still exists, and 1093 is marked as exited or zombie, as well as other 1094 conditions, so in that case, confirm the status in 1095 /proc/PID/status. */ 1096 if (err == ESRCH 1097 || (err == EPERM && linux_proc_pid_is_gone (lwpid))) 1098 { 1099 linux_nat_debug_printf 1100 ("Cannot attach to lwp %d: thread is gone (%d: %s)", 1101 lwpid, err, safe_strerror (err)); 1102 1103 } 1104 else 1105 { 1106 std::string reason 1107 = linux_ptrace_attach_fail_reason_string (ptid, err); 1108 1109 error (_("Cannot attach to lwp %d: %s"), 1110 lwpid, reason.c_str ()); 1111 } 1112 } 1113 else 1114 { 1115 linux_nat_debug_printf ("PTRACE_ATTACH %s, 0, 0 (OK)", 1116 ptid.to_string ().c_str ()); 1117 1118 lp = add_lwp (ptid); 1119 1120 /* The next time we wait for this LWP we'll see a SIGSTOP as 1121 PTRACE_ATTACH brings it to a halt. */ 1122 lp->signalled = 1; 1123 1124 /* We need to wait for a stop before being able to make the 1125 next ptrace call on this LWP. */ 1126 lp->must_set_ptrace_flags = 1; 1127 1128 /* So that wait collects the SIGSTOP. */ 1129 lp->resumed = 1; 1130 } 1131 1132 return 1; 1133 } 1134 return 0; 1135 } 1136 1137 void 1138 linux_nat_target::attach (const char *args, int from_tty) 1139 { 1140 struct lwp_info *lp; 1141 int status; 1142 ptid_t ptid; 1143 1144 /* Make sure we report all signals during attach. */ 1145 pass_signals ({}); 1146 1147 try 1148 { 1149 inf_ptrace_target::attach (args, from_tty); 1150 } 1151 catch (const gdb_exception_error &ex) 1152 { 1153 pid_t pid = parse_pid_to_attach (args); 1154 std::string reason = linux_ptrace_attach_fail_reason (pid); 1155 1156 if (!reason.empty ()) 1157 throw_error (ex.error, "warning: %s\n%s", reason.c_str (), 1158 ex.what ()); 1159 else 1160 throw_error (ex.error, "%s", ex.what ()); 1161 } 1162 1163 /* The ptrace base target adds the main thread with (pid,0,0) 1164 format. Decorate it with lwp info. */ 1165 ptid = ptid_t (inferior_ptid.pid (), 1166 inferior_ptid.pid ()); 1167 thread_change_ptid (linux_target, inferior_ptid, ptid); 1168 1169 /* Add the initial process as the first LWP to the list. */ 1170 lp = add_initial_lwp (ptid); 1171 1172 status = linux_nat_post_attach_wait (lp->ptid, &lp->signalled); 1173 if (!WIFSTOPPED (status)) 1174 { 1175 if (WIFEXITED (status)) 1176 { 1177 int exit_code = WEXITSTATUS (status); 1178 1179 target_terminal::ours (); 1180 target_mourn_inferior (inferior_ptid); 1181 if (exit_code == 0) 1182 error (_("Unable to attach: program exited normally.")); 1183 else 1184 error (_("Unable to attach: program exited with code %d."), 1185 exit_code); 1186 } 1187 else if (WIFSIGNALED (status)) 1188 { 1189 enum gdb_signal signo; 1190 1191 target_terminal::ours (); 1192 target_mourn_inferior (inferior_ptid); 1193 1194 signo = gdb_signal_from_host (WTERMSIG (status)); 1195 error (_("Unable to attach: program terminated with signal " 1196 "%s, %s."), 1197 gdb_signal_to_name (signo), 1198 gdb_signal_to_string (signo)); 1199 } 1200 1201 internal_error (_("unexpected status %d for PID %ld"), 1202 status, (long) ptid.lwp ()); 1203 } 1204 1205 lp->stopped = 1; 1206 1207 open_proc_mem_file (lp->ptid); 1208 1209 /* Save the wait status to report later. */ 1210 lp->resumed = 1; 1211 linux_nat_debug_printf ("waitpid %ld, saving status %s", 1212 (long) lp->ptid.pid (), 1213 status_to_str (status).c_str ()); 1214 1215 lp->status = status; 1216 1217 /* We must attach to every LWP. If /proc is mounted, use that to 1218 find them now. The inferior may be using raw clone instead of 1219 using pthreads. But even if it is using pthreads, thread_db 1220 walks structures in the inferior's address space to find the list 1221 of threads/LWPs, and those structures may well be corrupted. 1222 Note that once thread_db is loaded, we'll still use it to list 1223 threads and associate pthread info with each LWP. */ 1224 try 1225 { 1226 linux_proc_attach_tgid_threads (lp->ptid.pid (), 1227 attach_proc_task_lwp_callback); 1228 } 1229 catch (const gdb_exception_error &) 1230 { 1231 /* Failed to attach to some LWP. Detach any we've already 1232 attached to. */ 1233 iterate_over_lwps (ptid_t (ptid.pid ()), 1234 [] (struct lwp_info *lwp) -> int 1235 { 1236 /* Ignore errors when detaching. */ 1237 ptrace (PTRACE_DETACH, lwp->ptid.lwp (), 0, 0); 1238 delete_lwp (lwp->ptid); 1239 return 0; 1240 }); 1241 1242 target_terminal::ours (); 1243 target_mourn_inferior (inferior_ptid); 1244 1245 throw; 1246 } 1247 1248 /* Add all the LWPs to gdb's thread list. */ 1249 iterate_over_lwps (ptid_t (ptid.pid ()), 1250 [] (struct lwp_info *lwp) -> int 1251 { 1252 if (lwp->ptid.pid () != lwp->ptid.lwp ()) 1253 { 1254 add_thread (linux_target, lwp->ptid); 1255 set_running (linux_target, lwp->ptid, true); 1256 set_executing (linux_target, lwp->ptid, true); 1257 } 1258 return 0; 1259 }); 1260 } 1261 1262 /* Ptrace-detach the thread with pid PID. */ 1263 1264 static void 1265 detach_one_pid (int pid, int signo) 1266 { 1267 if (ptrace (PTRACE_DETACH, pid, 0, signo) < 0) 1268 { 1269 int save_errno = errno; 1270 1271 /* We know the thread exists, so ESRCH must mean the lwp is 1272 zombie. This can happen if one of the already-detached 1273 threads exits the whole thread group. In that case we're 1274 still attached, and must reap the lwp. */ 1275 if (save_errno == ESRCH) 1276 { 1277 int ret, status; 1278 1279 ret = my_waitpid (pid, &status, __WALL); 1280 if (ret == -1) 1281 { 1282 warning (_("Couldn't reap LWP %d while detaching: %s"), 1283 pid, safe_strerror (errno)); 1284 } 1285 else if (!WIFEXITED (status) && !WIFSIGNALED (status)) 1286 { 1287 warning (_("Reaping LWP %d while detaching " 1288 "returned unexpected status 0x%x"), 1289 pid, status); 1290 } 1291 } 1292 else 1293 error (_("Can't detach %d: %s"), 1294 pid, safe_strerror (save_errno)); 1295 } 1296 else 1297 linux_nat_debug_printf ("PTRACE_DETACH (%d, %s, 0) (OK)", 1298 pid, strsignal (signo)); 1299 } 1300 1301 /* Get pending signal of THREAD as a host signal number, for detaching 1302 purposes. This is the signal the thread last stopped for, which we 1303 need to deliver to the thread when detaching, otherwise, it'd be 1304 suppressed/lost. */ 1305 1306 static int 1307 get_detach_signal (struct lwp_info *lp) 1308 { 1309 enum gdb_signal signo = GDB_SIGNAL_0; 1310 1311 /* If we paused threads momentarily, we may have stored pending 1312 events in lp->status or lp->waitstatus (see stop_wait_callback), 1313 and GDB core hasn't seen any signal for those threads. 1314 Otherwise, the last signal reported to the core is found in the 1315 thread object's stop_signal. 1316 1317 There's a corner case that isn't handled here at present. Only 1318 if the thread stopped with a TARGET_WAITKIND_STOPPED does 1319 stop_signal make sense as a real signal to pass to the inferior. 1320 Some catchpoint related events, like 1321 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set 1322 to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But, 1323 those traps are debug API (ptrace in our case) related and 1324 induced; the inferior wouldn't see them if it wasn't being 1325 traced. Hence, we should never pass them to the inferior, even 1326 when set to pass state. Since this corner case isn't handled by 1327 infrun.c when proceeding with a signal, for consistency, neither 1328 do we handle it here (or elsewhere in the file we check for 1329 signal pass state). Normally SIGTRAP isn't set to pass state, so 1330 this is really a corner case. */ 1331 1332 if (lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE) 1333 signo = GDB_SIGNAL_0; /* a pending ptrace event, not a real signal. */ 1334 else if (lp->status) 1335 signo = gdb_signal_from_host (WSTOPSIG (lp->status)); 1336 else 1337 { 1338 thread_info *tp = linux_target->find_thread (lp->ptid); 1339 1340 if (target_is_non_stop_p () && !tp->executing ()) 1341 { 1342 if (tp->has_pending_waitstatus ()) 1343 { 1344 /* If the thread has a pending event, and it was stopped with a 1345 signal, use that signal to resume it. If it has a pending 1346 event of another kind, it was not stopped with a signal, so 1347 resume it without a signal. */ 1348 if (tp->pending_waitstatus ().kind () == TARGET_WAITKIND_STOPPED) 1349 signo = tp->pending_waitstatus ().sig (); 1350 else 1351 signo = GDB_SIGNAL_0; 1352 } 1353 else 1354 signo = tp->stop_signal (); 1355 } 1356 else if (!target_is_non_stop_p ()) 1357 { 1358 ptid_t last_ptid; 1359 process_stratum_target *last_target; 1360 1361 get_last_target_status (&last_target, &last_ptid, nullptr); 1362 1363 if (last_target == linux_target 1364 && lp->ptid.lwp () == last_ptid.lwp ()) 1365 signo = tp->stop_signal (); 1366 } 1367 } 1368 1369 if (signo == GDB_SIGNAL_0) 1370 { 1371 linux_nat_debug_printf ("lwp %s has no pending signal", 1372 lp->ptid.to_string ().c_str ()); 1373 } 1374 else if (!signal_pass_state (signo)) 1375 { 1376 linux_nat_debug_printf 1377 ("lwp %s had signal %s but it is in no pass state", 1378 lp->ptid.to_string ().c_str (), gdb_signal_to_string (signo)); 1379 } 1380 else 1381 { 1382 linux_nat_debug_printf ("lwp %s has pending signal %s", 1383 lp->ptid.to_string ().c_str (), 1384 gdb_signal_to_string (signo)); 1385 1386 return gdb_signal_to_host (signo); 1387 } 1388 1389 return 0; 1390 } 1391 1392 /* If LP has a pending fork/vfork/clone status, return it. */ 1393 1394 static std::optional<target_waitstatus> 1395 get_pending_child_status (lwp_info *lp) 1396 { 1397 LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 1398 1399 linux_nat_debug_printf ("lwp %s (stopped = %d)", 1400 lp->ptid.to_string ().c_str (), lp->stopped); 1401 1402 /* Check in lwp_info::status. */ 1403 if (WIFSTOPPED (lp->status) && linux_is_extended_waitstatus (lp->status)) 1404 { 1405 int event = linux_ptrace_get_extended_event (lp->status); 1406 1407 if (event == PTRACE_EVENT_FORK 1408 || event == PTRACE_EVENT_VFORK 1409 || event == PTRACE_EVENT_CLONE) 1410 { 1411 unsigned long child_pid; 1412 int ret = ptrace (PTRACE_GETEVENTMSG, lp->ptid.lwp (), 0, &child_pid); 1413 if (ret == 0) 1414 { 1415 target_waitstatus ws; 1416 1417 if (event == PTRACE_EVENT_FORK) 1418 ws.set_forked (ptid_t (child_pid, child_pid)); 1419 else if (event == PTRACE_EVENT_VFORK) 1420 ws.set_vforked (ptid_t (child_pid, child_pid)); 1421 else if (event == PTRACE_EVENT_CLONE) 1422 ws.set_thread_cloned (ptid_t (lp->ptid.pid (), child_pid)); 1423 else 1424 gdb_assert_not_reached ("unhandled"); 1425 1426 return ws; 1427 } 1428 else 1429 { 1430 perror_warning_with_name (_("Failed to retrieve event msg")); 1431 return {}; 1432 } 1433 } 1434 } 1435 1436 /* Check in lwp_info::waitstatus. */ 1437 if (is_new_child_status (lp->waitstatus.kind ())) 1438 return lp->waitstatus; 1439 1440 thread_info *tp = linux_target->find_thread (lp->ptid); 1441 1442 /* Check in thread_info::pending_waitstatus. */ 1443 if (tp->has_pending_waitstatus () 1444 && is_new_child_status (tp->pending_waitstatus ().kind ())) 1445 return tp->pending_waitstatus (); 1446 1447 /* Check in thread_info::pending_follow. */ 1448 if (is_new_child_status (tp->pending_follow.kind ())) 1449 return tp->pending_follow; 1450 1451 return {}; 1452 } 1453 1454 /* Detach from LP. If SIGNO_P is non-NULL, then it points to the 1455 signal number that should be passed to the LWP when detaching. 1456 Otherwise pass any pending signal the LWP may have, if any. */ 1457 1458 static void 1459 detach_one_lwp (struct lwp_info *lp, int *signo_p) 1460 { 1461 int lwpid = lp->ptid.lwp (); 1462 int signo; 1463 1464 /* If the lwp/thread we are about to detach has a pending fork/clone 1465 event, there is a process/thread GDB is attached to that the core 1466 of GDB doesn't know about. Detach from it. */ 1467 1468 std::optional<target_waitstatus> ws = get_pending_child_status (lp); 1469 if (ws.has_value ()) 1470 detach_one_pid (ws->child_ptid ().lwp (), 0); 1471 1472 /* If there is a pending SIGSTOP, get rid of it. */ 1473 if (lp->signalled) 1474 { 1475 linux_nat_debug_printf ("Sending SIGCONT to %s", 1476 lp->ptid.to_string ().c_str ()); 1477 1478 kill_lwp (lwpid, SIGCONT); 1479 lp->signalled = 0; 1480 } 1481 1482 /* If the lwp has exited or was terminated due to a signal, there's 1483 nothing left to do. */ 1484 if (is_lwp_marked_dead (lp)) 1485 { 1486 linux_nat_debug_printf 1487 ("Can't detach %s - it has exited or was terminated: %s.", 1488 lp->ptid.to_string ().c_str (), 1489 lp->waitstatus.to_string ().c_str ()); 1490 delete_lwp (lp->ptid); 1491 return; 1492 } 1493 1494 if (signo_p == NULL) 1495 { 1496 /* Pass on any pending signal for this LWP. */ 1497 signo = get_detach_signal (lp); 1498 } 1499 else 1500 signo = *signo_p; 1501 1502 linux_nat_debug_printf ("preparing to resume lwp %s (stopped = %d)", 1503 lp->ptid.to_string ().c_str (), 1504 lp->stopped); 1505 1506 /* Preparing to resume may try to write registers, and fail if the 1507 lwp is zombie. If that happens, ignore the error. We'll handle 1508 it below, when detach fails with ESRCH. */ 1509 try 1510 { 1511 linux_target->low_prepare_to_resume (lp); 1512 } 1513 catch (const gdb_exception_error &ex) 1514 { 1515 if (!check_ptrace_stopped_lwp_gone (lp)) 1516 throw; 1517 } 1518 1519 detach_one_pid (lwpid, signo); 1520 1521 delete_lwp (lp->ptid); 1522 } 1523 1524 static int 1525 detach_callback (struct lwp_info *lp) 1526 { 1527 /* We don't actually detach from the thread group leader just yet. 1528 If the thread group exits, we must reap the zombie clone lwps 1529 before we're able to reap the leader. */ 1530 if (lp->ptid.lwp () != lp->ptid.pid ()) 1531 detach_one_lwp (lp, NULL); 1532 return 0; 1533 } 1534 1535 void 1536 linux_nat_target::detach (inferior *inf, int from_tty) 1537 { 1538 LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 1539 1540 struct lwp_info *main_lwp; 1541 int pid = inf->pid; 1542 1543 /* Don't unregister from the event loop, as there may be other 1544 inferiors running. */ 1545 1546 /* Stop all threads before detaching. ptrace requires that the 1547 thread is stopped to successfully detach. */ 1548 iterate_over_lwps (ptid_t (pid), stop_callback); 1549 /* ... and wait until all of them have reported back that 1550 they're no longer running. */ 1551 iterate_over_lwps (ptid_t (pid), stop_wait_callback); 1552 1553 /* We can now safely remove breakpoints. We don't this in earlier 1554 in common code because this target doesn't currently support 1555 writing memory while the inferior is running. */ 1556 remove_breakpoints_inf (current_inferior ()); 1557 1558 iterate_over_lwps (ptid_t (pid), detach_callback); 1559 1560 /* We have detached from everything except the main thread now, so 1561 should only have one thread left. However, in non-stop mode the 1562 main thread might have exited, in which case we'll have no threads 1563 left. */ 1564 gdb_assert (num_lwps (pid) == 1 1565 || (target_is_non_stop_p () && num_lwps (pid) == 0)); 1566 1567 if (pid == inferior_ptid.pid () && forks_exist_p ()) 1568 { 1569 /* Multi-fork case. The current inferior_ptid is being detached 1570 from, but there are other viable forks to debug. Detach from 1571 the current fork, and context-switch to the first 1572 available. */ 1573 linux_fork_detach (from_tty, find_lwp_pid (ptid_t (pid))); 1574 } 1575 else 1576 { 1577 target_announce_detach (from_tty); 1578 1579 /* In non-stop mode it is possible that the main thread has exited, 1580 in which case we don't try to detach. */ 1581 main_lwp = find_lwp_pid (ptid_t (pid)); 1582 if (main_lwp != nullptr) 1583 { 1584 /* Pass on any pending signal for the last LWP. */ 1585 int signo = get_detach_signal (main_lwp); 1586 1587 detach_one_lwp (main_lwp, &signo); 1588 } 1589 else 1590 gdb_assert (target_is_non_stop_p ()); 1591 1592 detach_success (inf); 1593 } 1594 1595 close_proc_mem_file (pid); 1596 } 1597 1598 /* Resume execution of the inferior process. If STEP is nonzero, 1599 single-step it. If SIGNAL is nonzero, give it that signal. */ 1600 1601 static void 1602 linux_resume_one_lwp_throw (struct lwp_info *lp, int step, 1603 enum gdb_signal signo) 1604 { 1605 lp->step = step; 1606 1607 /* stop_pc doubles as the PC the LWP had when it was last resumed. 1608 We only presently need that if the LWP is stepped though (to 1609 handle the case of stepping a breakpoint instruction). */ 1610 if (step) 1611 { 1612 struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid); 1613 1614 lp->stop_pc = regcache_read_pc (regcache); 1615 } 1616 else 1617 lp->stop_pc = 0; 1618 1619 linux_target->low_prepare_to_resume (lp); 1620 linux_target->low_resume (lp->ptid, step, signo); 1621 1622 /* Successfully resumed. Clear state that no longer makes sense, 1623 and mark the LWP as running. Must not do this before resuming 1624 otherwise if that fails other code will be confused. E.g., we'd 1625 later try to stop the LWP and hang forever waiting for a stop 1626 status. Note that we must not throw after this is cleared, 1627 otherwise handle_zombie_lwp_error would get confused. */ 1628 lp->stopped = 0; 1629 lp->core = -1; 1630 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON; 1631 registers_changed_ptid (linux_target, lp->ptid); 1632 } 1633 1634 /* Called when we try to resume a stopped LWP and that errors out. If 1635 the LWP is no longer in ptrace-stopped state (meaning it's zombie, 1636 or about to become), discard the error, clear any pending status 1637 the LWP may have, and return true (we'll collect the exit status 1638 soon enough). Otherwise, return false. */ 1639 1640 static int 1641 check_ptrace_stopped_lwp_gone (struct lwp_info *lp) 1642 { 1643 /* If we get an error after resuming the LWP successfully, we'd 1644 confuse !T state for the LWP being gone. */ 1645 gdb_assert (lp->stopped); 1646 1647 /* We can't just check whether the LWP is in 'Z (Zombie)' state, 1648 because even if ptrace failed with ESRCH, the tracee may be "not 1649 yet fully dead", but already refusing ptrace requests. In that 1650 case the tracee has 'R (Running)' state for a little bit 1651 (observed in Linux 3.18). See also the note on ESRCH in the 1652 ptrace(2) man page. Instead, check whether the LWP has any state 1653 other than ptrace-stopped. */ 1654 1655 /* Don't assume anything if /proc/PID/status can't be read. */ 1656 if (linux_proc_pid_is_trace_stopped_nowarn (lp->ptid.lwp ()) == 0) 1657 { 1658 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON; 1659 lp->status = 0; 1660 lp->waitstatus.set_ignore (); 1661 return 1; 1662 } 1663 return 0; 1664 } 1665 1666 /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP 1667 disappears while we try to resume it. */ 1668 1669 static void 1670 linux_resume_one_lwp (struct lwp_info *lp, int step, enum gdb_signal signo) 1671 { 1672 try 1673 { 1674 linux_resume_one_lwp_throw (lp, step, signo); 1675 } 1676 catch (const gdb_exception_error &ex) 1677 { 1678 if (!check_ptrace_stopped_lwp_gone (lp)) 1679 throw; 1680 } 1681 } 1682 1683 /* Resume LP. */ 1684 1685 static void 1686 resume_lwp (struct lwp_info *lp, int step, enum gdb_signal signo) 1687 { 1688 if (lp->stopped) 1689 { 1690 struct inferior *inf = find_inferior_ptid (linux_target, lp->ptid); 1691 1692 if (inf->vfork_child != NULL) 1693 { 1694 linux_nat_debug_printf ("Not resuming sibling %s (vfork parent)", 1695 lp->ptid.to_string ().c_str ()); 1696 } 1697 else if (!lwp_status_pending_p (lp)) 1698 { 1699 linux_nat_debug_printf ("Resuming sibling %s, %s, %s", 1700 lp->ptid.to_string ().c_str (), 1701 (signo != GDB_SIGNAL_0 1702 ? strsignal (gdb_signal_to_host (signo)) 1703 : "0"), 1704 step ? "step" : "resume"); 1705 1706 linux_resume_one_lwp (lp, step, signo); 1707 } 1708 else 1709 { 1710 linux_nat_debug_printf ("Not resuming sibling %s (has pending)", 1711 lp->ptid.to_string ().c_str ()); 1712 } 1713 } 1714 else 1715 linux_nat_debug_printf ("Not resuming sibling %s (not stopped)", 1716 lp->ptid.to_string ().c_str ()); 1717 } 1718 1719 /* Callback for iterate_over_lwps. If LWP is EXCEPT, do nothing. 1720 Resume LWP with the last stop signal, if it is in pass state. */ 1721 1722 static int 1723 linux_nat_resume_callback (struct lwp_info *lp, struct lwp_info *except) 1724 { 1725 enum gdb_signal signo = GDB_SIGNAL_0; 1726 1727 if (lp == except) 1728 return 0; 1729 1730 if (lp->stopped) 1731 { 1732 struct thread_info *thread; 1733 1734 thread = linux_target->find_thread (lp->ptid); 1735 if (thread != NULL) 1736 { 1737 signo = thread->stop_signal (); 1738 thread->set_stop_signal (GDB_SIGNAL_0); 1739 } 1740 } 1741 1742 resume_lwp (lp, 0, signo); 1743 return 0; 1744 } 1745 1746 static int 1747 resume_clear_callback (struct lwp_info *lp) 1748 { 1749 lp->resumed = 0; 1750 lp->last_resume_kind = resume_stop; 1751 return 0; 1752 } 1753 1754 static int 1755 resume_set_callback (struct lwp_info *lp) 1756 { 1757 lp->resumed = 1; 1758 lp->last_resume_kind = resume_continue; 1759 return 0; 1760 } 1761 1762 void 1763 linux_nat_target::resume (ptid_t scope_ptid, int step, enum gdb_signal signo) 1764 { 1765 struct lwp_info *lp; 1766 1767 linux_nat_debug_printf ("Preparing to %s %s, %s, inferior_ptid %s", 1768 step ? "step" : "resume", 1769 scope_ptid.to_string ().c_str (), 1770 (signo != GDB_SIGNAL_0 1771 ? strsignal (gdb_signal_to_host (signo)) : "0"), 1772 inferior_ptid.to_string ().c_str ()); 1773 1774 /* Mark the lwps we're resuming as resumed and update their 1775 last_resume_kind to resume_continue. */ 1776 iterate_over_lwps (scope_ptid, resume_set_callback); 1777 1778 lp = find_lwp_pid (inferior_ptid); 1779 gdb_assert (lp != NULL); 1780 1781 /* Remember if we're stepping. */ 1782 lp->last_resume_kind = step ? resume_step : resume_continue; 1783 1784 /* If we have a pending wait status for this thread, there is no 1785 point in resuming the process. But first make sure that 1786 linux_nat_wait won't preemptively handle the event - we 1787 should never take this short-circuit if we are going to 1788 leave LP running, since we have skipped resuming all the 1789 other threads. This bit of code needs to be synchronized 1790 with linux_nat_wait. */ 1791 1792 if (lp->status && WIFSTOPPED (lp->status)) 1793 { 1794 if (!lp->step 1795 && WSTOPSIG (lp->status) 1796 && sigismember (&pass_mask, WSTOPSIG (lp->status))) 1797 { 1798 linux_nat_debug_printf 1799 ("Not short circuiting for ignored status 0x%x", lp->status); 1800 1801 /* FIXME: What should we do if we are supposed to continue 1802 this thread with a signal? */ 1803 gdb_assert (signo == GDB_SIGNAL_0); 1804 signo = gdb_signal_from_host (WSTOPSIG (lp->status)); 1805 lp->status = 0; 1806 } 1807 } 1808 1809 if (lwp_status_pending_p (lp)) 1810 { 1811 /* FIXME: What should we do if we are supposed to continue 1812 this thread with a signal? */ 1813 gdb_assert (signo == GDB_SIGNAL_0); 1814 1815 linux_nat_debug_printf ("Short circuiting for status %s", 1816 pending_status_str (lp).c_str ()); 1817 1818 if (target_can_async_p ()) 1819 { 1820 target_async (true); 1821 /* Tell the event loop we have something to process. */ 1822 async_file_mark (); 1823 } 1824 return; 1825 } 1826 1827 /* No use iterating unless we're resuming other threads. */ 1828 if (scope_ptid != lp->ptid) 1829 iterate_over_lwps (scope_ptid, [=] (struct lwp_info *info) 1830 { 1831 return linux_nat_resume_callback (info, lp); 1832 }); 1833 1834 linux_nat_debug_printf ("%s %s, %s (resume event thread)", 1835 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", 1836 lp->ptid.to_string ().c_str (), 1837 (signo != GDB_SIGNAL_0 1838 ? strsignal (gdb_signal_to_host (signo)) : "0")); 1839 1840 linux_resume_one_lwp (lp, step, signo); 1841 } 1842 1843 /* Send a signal to an LWP. */ 1844 1845 static int 1846 kill_lwp (int lwpid, int signo) 1847 { 1848 int ret; 1849 1850 errno = 0; 1851 ret = syscall (__NR_tkill, lwpid, signo); 1852 if (errno == ENOSYS) 1853 { 1854 /* If tkill fails, then we are not using nptl threads, a 1855 configuration we no longer support. */ 1856 perror_with_name (("tkill")); 1857 } 1858 return ret; 1859 } 1860 1861 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall 1862 event, check if the core is interested in it: if not, ignore the 1863 event, and keep waiting; otherwise, we need to toggle the LWP's 1864 syscall entry/exit status, since the ptrace event itself doesn't 1865 indicate it, and report the trap to higher layers. */ 1866 1867 static int 1868 linux_handle_syscall_trap (struct lwp_info *lp, int stopping) 1869 { 1870 struct target_waitstatus *ourstatus = &lp->waitstatus; 1871 struct gdbarch *gdbarch = target_thread_architecture (lp->ptid); 1872 thread_info *thread = linux_target->find_thread (lp->ptid); 1873 int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, thread); 1874 1875 if (stopping) 1876 { 1877 /* If we're stopping threads, there's a SIGSTOP pending, which 1878 makes it so that the LWP reports an immediate syscall return, 1879 followed by the SIGSTOP. Skip seeing that "return" using 1880 PTRACE_CONT directly, and let stop_wait_callback collect the 1881 SIGSTOP. Later when the thread is resumed, a new syscall 1882 entry event. If we didn't do this (and returned 0), we'd 1883 leave a syscall entry pending, and our caller, by using 1884 PTRACE_CONT to collect the SIGSTOP, skips the syscall return 1885 itself. Later, when the user re-resumes this LWP, we'd see 1886 another syscall entry event and we'd mistake it for a return. 1887 1888 If stop_wait_callback didn't force the SIGSTOP out of the LWP 1889 (leaving immediately with LWP->signalled set, without issuing 1890 a PTRACE_CONT), it would still be problematic to leave this 1891 syscall enter pending, as later when the thread is resumed, 1892 it would then see the same syscall exit mentioned above, 1893 followed by the delayed SIGSTOP, while the syscall didn't 1894 actually get to execute. It seems it would be even more 1895 confusing to the user. */ 1896 1897 linux_nat_debug_printf 1898 ("ignoring syscall %d for LWP %ld (stopping threads), resuming with " 1899 "PTRACE_CONT for SIGSTOP", syscall_number, lp->ptid.lwp ()); 1900 1901 lp->syscall_state = TARGET_WAITKIND_IGNORE; 1902 ptrace (PTRACE_CONT, lp->ptid.lwp (), 0, 0); 1903 lp->stopped = 0; 1904 return 1; 1905 } 1906 1907 /* Always update the entry/return state, even if this particular 1908 syscall isn't interesting to the core now. In async mode, 1909 the user could install a new catchpoint for this syscall 1910 between syscall enter/return, and we'll need to know to 1911 report a syscall return if that happens. */ 1912 lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY 1913 ? TARGET_WAITKIND_SYSCALL_RETURN 1914 : TARGET_WAITKIND_SYSCALL_ENTRY); 1915 1916 if (catch_syscall_enabled ()) 1917 { 1918 if (catching_syscall_number (syscall_number)) 1919 { 1920 /* Alright, an event to report. */ 1921 if (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY) 1922 ourstatus->set_syscall_entry (syscall_number); 1923 else if (lp->syscall_state == TARGET_WAITKIND_SYSCALL_RETURN) 1924 ourstatus->set_syscall_return (syscall_number); 1925 else 1926 gdb_assert_not_reached ("unexpected syscall state"); 1927 1928 linux_nat_debug_printf 1929 ("stopping for %s of syscall %d for LWP %ld", 1930 (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY 1931 ? "entry" : "return"), syscall_number, lp->ptid.lwp ()); 1932 1933 return 0; 1934 } 1935 1936 linux_nat_debug_printf 1937 ("ignoring %s of syscall %d for LWP %ld", 1938 (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY 1939 ? "entry" : "return"), syscall_number, lp->ptid.lwp ()); 1940 } 1941 else 1942 { 1943 /* If we had been syscall tracing, and hence used PT_SYSCALL 1944 before on this LWP, it could happen that the user removes all 1945 syscall catchpoints before we get to process this event. 1946 There are two noteworthy issues here: 1947 1948 - When stopped at a syscall entry event, resuming with 1949 PT_STEP still resumes executing the syscall and reports a 1950 syscall return. 1951 1952 - Only PT_SYSCALL catches syscall enters. If we last 1953 single-stepped this thread, then this event can't be a 1954 syscall enter. If we last single-stepped this thread, this 1955 has to be a syscall exit. 1956 1957 The points above mean that the next resume, be it PT_STEP or 1958 PT_CONTINUE, can not trigger a syscall trace event. */ 1959 linux_nat_debug_printf 1960 ("caught syscall event with no syscall catchpoints. %d for LWP %ld, " 1961 "ignoring", syscall_number, lp->ptid.lwp ()); 1962 lp->syscall_state = TARGET_WAITKIND_IGNORE; 1963 } 1964 1965 /* The core isn't interested in this event. For efficiency, avoid 1966 stopping all threads only to have the core resume them all again. 1967 Since we're not stopping threads, if we're still syscall tracing 1968 and not stepping, we can't use PTRACE_CONT here, as we'd miss any 1969 subsequent syscall. Simply resume using the inf-ptrace layer, 1970 which knows when to use PT_SYSCALL or PT_CONTINUE. */ 1971 1972 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0); 1973 return 1; 1974 } 1975 1976 /* See target.h. */ 1977 1978 void 1979 linux_nat_target::follow_clone (ptid_t child_ptid) 1980 { 1981 lwp_info *new_lp = add_lwp (child_ptid); 1982 new_lp->stopped = 1; 1983 1984 /* If the thread_db layer is active, let it record the user 1985 level thread id and status, and add the thread to GDB's 1986 list. */ 1987 if (!thread_db_notice_clone (inferior_ptid, new_lp->ptid)) 1988 { 1989 /* The process is not using thread_db. Add the LWP to 1990 GDB's list. */ 1991 add_thread (linux_target, new_lp->ptid); 1992 } 1993 1994 /* We just created NEW_LP so it cannot yet contain STATUS. */ 1995 gdb_assert (new_lp->status == 0); 1996 1997 if (!pull_pid_from_list (&stopped_pids, child_ptid.lwp (), &new_lp->status)) 1998 internal_error (_("no saved status for clone lwp")); 1999 2000 if (WSTOPSIG (new_lp->status) != SIGSTOP) 2001 { 2002 /* This can happen if someone starts sending signals to 2003 the new thread before it gets a chance to run, which 2004 have a lower number than SIGSTOP (e.g. SIGUSR1). 2005 This is an unlikely case, and harder to handle for 2006 fork / vfork than for clone, so we do not try - but 2007 we handle it for clone events here. */ 2008 2009 new_lp->signalled = 1; 2010 2011 /* Save the wait status to report later. */ 2012 linux_nat_debug_printf 2013 ("waitpid of new LWP %ld, saving status %s", 2014 (long) new_lp->ptid.lwp (), status_to_str (new_lp->status).c_str ()); 2015 } 2016 else 2017 { 2018 new_lp->status = 0; 2019 2020 if (report_thread_events) 2021 new_lp->waitstatus.set_thread_created (); 2022 } 2023 } 2024 2025 /* Handle a GNU/Linux extended wait response. If we see a clone 2026 event, we need to add the new LWP to our list (and not report the 2027 trap to higher layers). This function returns non-zero if the 2028 event should be ignored and we should wait again. If STOPPING is 2029 true, the new LWP remains stopped, otherwise it is continued. */ 2030 2031 static int 2032 linux_handle_extended_wait (struct lwp_info *lp, int status) 2033 { 2034 int pid = lp->ptid.lwp (); 2035 struct target_waitstatus *ourstatus = &lp->waitstatus; 2036 int event = linux_ptrace_get_extended_event (status); 2037 2038 /* All extended events we currently use are mid-syscall. Only 2039 PTRACE_EVENT_STOP is delivered more like a signal-stop, but 2040 you have to be using PTRACE_SEIZE to get that. */ 2041 lp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY; 2042 2043 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK 2044 || event == PTRACE_EVENT_CLONE) 2045 { 2046 unsigned long new_pid; 2047 int ret; 2048 2049 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid); 2050 2051 /* If we haven't already seen the new PID stop, wait for it now. */ 2052 if (! pull_pid_from_list (&stopped_pids, new_pid, &status)) 2053 { 2054 /* The new child has a pending SIGSTOP. We can't affect it until it 2055 hits the SIGSTOP, but we're already attached. */ 2056 ret = my_waitpid (new_pid, &status, __WALL); 2057 if (ret == -1) 2058 perror_with_name (_("waiting for new child")); 2059 else if (ret != new_pid) 2060 internal_error (_("wait returned unexpected PID %d"), ret); 2061 else if (!WIFSTOPPED (status)) 2062 internal_error (_("wait returned unexpected status 0x%x"), status); 2063 } 2064 2065 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK) 2066 { 2067 open_proc_mem_file (ptid_t (new_pid, new_pid)); 2068 2069 /* The arch-specific native code may need to know about new 2070 forks even if those end up never mapped to an 2071 inferior. */ 2072 linux_target->low_new_fork (lp, new_pid); 2073 } 2074 else if (event == PTRACE_EVENT_CLONE) 2075 { 2076 linux_target->low_new_clone (lp, new_pid); 2077 } 2078 2079 if (event == PTRACE_EVENT_FORK 2080 && linux_fork_checkpointing_p (lp->ptid.pid ())) 2081 { 2082 /* Handle checkpointing by linux-fork.c here as a special 2083 case. We don't want the follow-fork-mode or 'catch fork' 2084 to interfere with this. */ 2085 2086 /* This won't actually modify the breakpoint list, but will 2087 physically remove the breakpoints from the child. */ 2088 detach_breakpoints (ptid_t (new_pid, new_pid)); 2089 2090 /* Retain child fork in ptrace (stopped) state. */ 2091 if (!find_fork_pid (new_pid)) 2092 add_fork (new_pid); 2093 2094 /* Report as spurious, so that infrun doesn't want to follow 2095 this fork. We're actually doing an infcall in 2096 linux-fork.c. */ 2097 ourstatus->set_spurious (); 2098 2099 /* Report the stop to the core. */ 2100 return 0; 2101 } 2102 2103 if (event == PTRACE_EVENT_FORK) 2104 ourstatus->set_forked (ptid_t (new_pid, new_pid)); 2105 else if (event == PTRACE_EVENT_VFORK) 2106 ourstatus->set_vforked (ptid_t (new_pid, new_pid)); 2107 else if (event == PTRACE_EVENT_CLONE) 2108 { 2109 linux_nat_debug_printf 2110 ("Got clone event from LWP %d, new child is LWP %ld", pid, new_pid); 2111 2112 /* Save the status again, we'll use it in follow_clone. */ 2113 add_to_pid_list (&stopped_pids, new_pid, status); 2114 2115 ourstatus->set_thread_cloned (ptid_t (lp->ptid.pid (), new_pid)); 2116 } 2117 2118 return 0; 2119 } 2120 2121 if (event == PTRACE_EVENT_EXEC) 2122 { 2123 linux_nat_debug_printf ("Got exec event from LWP %ld", lp->ptid.lwp ()); 2124 2125 /* Close the previous /proc/PID/mem file for this inferior, 2126 which was using the address space which is now gone. 2127 Reading/writing from this file would return 0/EOF. */ 2128 close_proc_mem_file (lp->ptid.pid ()); 2129 2130 /* Open a new file for the new address space. */ 2131 open_proc_mem_file (lp->ptid); 2132 2133 ourstatus->set_execd 2134 (make_unique_xstrdup (linux_proc_pid_to_exec_file (pid))); 2135 2136 /* The thread that execed must have been resumed, but, when a 2137 thread execs, it changes its tid to the tgid, and the old 2138 tgid thread might have not been resumed. */ 2139 lp->resumed = 1; 2140 2141 /* All other LWPs are gone now. We'll have received a thread 2142 exit notification for all threads other the execing one. 2143 That one, if it wasn't the leader, just silently changes its 2144 tid to the tgid, and the previous leader vanishes. Since 2145 Linux 3.0, the former thread ID can be retrieved with 2146 PTRACE_GETEVENTMSG, but since we support older kernels, don't 2147 bother with it, and just walk the LWP list. Even with 2148 PTRACE_GETEVENTMSG, we'd still need to lookup the 2149 corresponding LWP object, and it would be an extra ptrace 2150 syscall, so this way may even be more efficient. */ 2151 for (lwp_info *other_lp : all_lwps_safe ()) 2152 if (other_lp != lp && other_lp->ptid.pid () == lp->ptid.pid ()) 2153 exit_lwp (other_lp); 2154 2155 return 0; 2156 } 2157 2158 if (event == PTRACE_EVENT_VFORK_DONE) 2159 { 2160 linux_nat_debug_printf 2161 ("Got PTRACE_EVENT_VFORK_DONE from LWP %ld", 2162 lp->ptid.lwp ()); 2163 ourstatus->set_vfork_done (); 2164 return 0; 2165 } 2166 2167 internal_error (_("unknown ptrace event %d"), event); 2168 } 2169 2170 /* Suspend waiting for a signal. We're mostly interested in 2171 SIGCHLD/SIGINT. */ 2172 2173 static void 2174 wait_for_signal () 2175 { 2176 linux_nat_debug_printf ("about to sigsuspend"); 2177 sigsuspend (&suspend_mask); 2178 2179 /* If the quit flag is set, it means that the user pressed Ctrl-C 2180 and we're debugging a process that is running on a separate 2181 terminal, so we must forward the Ctrl-C to the inferior. (If the 2182 inferior is sharing GDB's terminal, then the Ctrl-C reaches the 2183 inferior directly.) We must do this here because functions that 2184 need to block waiting for a signal loop forever until there's an 2185 event to report before returning back to the event loop. */ 2186 if (!target_terminal::is_ours ()) 2187 { 2188 if (check_quit_flag ()) 2189 target_pass_ctrlc (); 2190 } 2191 } 2192 2193 /* Mark LWP dead, with STATUS as exit status pending to report 2194 later. */ 2195 2196 static void 2197 mark_lwp_dead (lwp_info *lp, int status) 2198 { 2199 /* Store the exit status lp->waitstatus, because lp->status would be 2200 ambiguous (W_EXITCODE(0,0) == 0). */ 2201 lp->waitstatus = host_status_to_waitstatus (status); 2202 2203 /* If we're processing LP's status, there should be no other event 2204 already recorded as pending. */ 2205 gdb_assert (lp->status == 0); 2206 2207 /* Dead LWPs aren't expected to report a pending sigstop. */ 2208 lp->signalled = 0; 2209 2210 /* Prevent trying to stop it. */ 2211 lp->stopped = 1; 2212 } 2213 2214 /* Return true if LP is dead, with a pending exit/signalled event. */ 2215 2216 static bool 2217 is_lwp_marked_dead (lwp_info *lp) 2218 { 2219 switch (lp->waitstatus.kind ()) 2220 { 2221 case TARGET_WAITKIND_EXITED: 2222 case TARGET_WAITKIND_THREAD_EXITED: 2223 case TARGET_WAITKIND_SIGNALLED: 2224 return true; 2225 } 2226 return false; 2227 } 2228 2229 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has 2230 exited. */ 2231 2232 static int 2233 wait_lwp (struct lwp_info *lp) 2234 { 2235 pid_t pid; 2236 int status = 0; 2237 int thread_dead = 0; 2238 sigset_t prev_mask; 2239 2240 gdb_assert (!lp->stopped); 2241 gdb_assert (lp->status == 0); 2242 2243 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */ 2244 block_child_signals (&prev_mask); 2245 2246 for (;;) 2247 { 2248 pid = my_waitpid (lp->ptid.lwp (), &status, __WALL | WNOHANG); 2249 if (pid == -1 && errno == ECHILD) 2250 { 2251 /* The thread has previously exited. We need to delete it 2252 now because if this was a non-leader thread execing, we 2253 won't get an exit event. See comments on exec events at 2254 the top of the file. */ 2255 thread_dead = 1; 2256 linux_nat_debug_printf ("%s vanished.", 2257 lp->ptid.to_string ().c_str ()); 2258 } 2259 if (pid != 0) 2260 break; 2261 2262 /* Bugs 10970, 12702. 2263 Thread group leader may have exited in which case we'll lock up in 2264 waitpid if there are other threads, even if they are all zombies too. 2265 Basically, we're not supposed to use waitpid this way. 2266 tkill(pid,0) cannot be used here as it gets ESRCH for both 2267 for zombie and running processes. 2268 2269 As a workaround, check if we're waiting for the thread group leader and 2270 if it's a zombie, and avoid calling waitpid if it is. 2271 2272 This is racy, what if the tgl becomes a zombie right after we check? 2273 Therefore always use WNOHANG with sigsuspend - it is equivalent to 2274 waiting waitpid but linux_proc_pid_is_zombie is safe this way. */ 2275 2276 if (lp->ptid.pid () == lp->ptid.lwp () 2277 && linux_proc_pid_is_zombie (lp->ptid.lwp ())) 2278 { 2279 thread_dead = 1; 2280 linux_nat_debug_printf ("Thread group leader %s vanished.", 2281 lp->ptid.to_string ().c_str ()); 2282 break; 2283 } 2284 2285 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers 2286 get invoked despite our caller had them intentionally blocked by 2287 block_child_signals. This is sensitive only to the loop of 2288 linux_nat_wait_1 and there if we get called my_waitpid gets called 2289 again before it gets to sigsuspend so we can safely let the handlers 2290 get executed here. */ 2291 wait_for_signal (); 2292 } 2293 2294 restore_child_signals_mask (&prev_mask); 2295 2296 if (!thread_dead) 2297 { 2298 gdb_assert (pid == lp->ptid.lwp ()); 2299 2300 linux_nat_debug_printf ("waitpid %s received %s", 2301 lp->ptid.to_string ().c_str (), 2302 status_to_str (status).c_str ()); 2303 2304 /* Check if the thread has exited. */ 2305 if (WIFEXITED (status) || WIFSIGNALED (status)) 2306 { 2307 if (report_exit_events_for (lp) || is_leader (lp)) 2308 { 2309 linux_nat_debug_printf ("LWP %d exited.", lp->ptid.pid ()); 2310 2311 /* If this is the leader exiting, it means the whole 2312 process is gone. Store the status to report to the 2313 core. */ 2314 mark_lwp_dead (lp, status); 2315 return 0; 2316 } 2317 2318 thread_dead = 1; 2319 linux_nat_debug_printf ("%s exited.", 2320 lp->ptid.to_string ().c_str ()); 2321 } 2322 } 2323 2324 if (thread_dead) 2325 { 2326 exit_lwp (lp); 2327 return 0; 2328 } 2329 2330 gdb_assert (WIFSTOPPED (status)); 2331 lp->stopped = 1; 2332 2333 if (lp->must_set_ptrace_flags) 2334 { 2335 inferior *inf = find_inferior_pid (linux_target, lp->ptid.pid ()); 2336 int options = linux_nat_ptrace_options (inf->attach_flag); 2337 2338 linux_enable_event_reporting (lp->ptid.lwp (), options); 2339 lp->must_set_ptrace_flags = 0; 2340 } 2341 2342 /* Handle GNU/Linux's syscall SIGTRAPs. */ 2343 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP) 2344 { 2345 /* No longer need the sysgood bit. The ptrace event ends up 2346 recorded in lp->waitstatus if we care for it. We can carry 2347 on handling the event like a regular SIGTRAP from here 2348 on. */ 2349 status = W_STOPCODE (SIGTRAP); 2350 if (linux_handle_syscall_trap (lp, 1)) 2351 return wait_lwp (lp); 2352 } 2353 else 2354 { 2355 /* Almost all other ptrace-stops are known to be outside of system 2356 calls, with further exceptions in linux_handle_extended_wait. */ 2357 lp->syscall_state = TARGET_WAITKIND_IGNORE; 2358 } 2359 2360 /* Handle GNU/Linux's extended waitstatus for trace events. */ 2361 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP 2362 && linux_is_extended_waitstatus (status)) 2363 { 2364 linux_nat_debug_printf ("Handling extended status 0x%06x", status); 2365 linux_handle_extended_wait (lp, status); 2366 return 0; 2367 } 2368 2369 return status; 2370 } 2371 2372 /* Send a SIGSTOP to LP. */ 2373 2374 static int 2375 stop_callback (struct lwp_info *lp) 2376 { 2377 if (!lp->stopped && !lp->signalled) 2378 { 2379 int ret; 2380 2381 linux_nat_debug_printf ("kill %s **<SIGSTOP>**", 2382 lp->ptid.to_string ().c_str ()); 2383 2384 errno = 0; 2385 ret = kill_lwp (lp->ptid.lwp (), SIGSTOP); 2386 linux_nat_debug_printf ("lwp kill %d %s", ret, 2387 errno ? safe_strerror (errno) : "ERRNO-OK"); 2388 2389 lp->signalled = 1; 2390 gdb_assert (lp->status == 0); 2391 } 2392 2393 return 0; 2394 } 2395 2396 /* Request a stop on LWP. */ 2397 2398 void 2399 linux_stop_lwp (struct lwp_info *lwp) 2400 { 2401 stop_callback (lwp); 2402 } 2403 2404 /* See linux-nat.h */ 2405 2406 void 2407 linux_stop_and_wait_all_lwps (void) 2408 { 2409 /* Stop all LWP's ... */ 2410 iterate_over_lwps (minus_one_ptid, stop_callback); 2411 2412 /* ... and wait until all of them have reported back that 2413 they're no longer running. */ 2414 iterate_over_lwps (minus_one_ptid, stop_wait_callback); 2415 } 2416 2417 /* See linux-nat.h */ 2418 2419 void 2420 linux_unstop_all_lwps (void) 2421 { 2422 iterate_over_lwps (minus_one_ptid, 2423 [] (struct lwp_info *info) 2424 { 2425 return resume_stopped_resumed_lwps (info, minus_one_ptid); 2426 }); 2427 } 2428 2429 /* Return non-zero if LWP PID has a pending SIGINT. */ 2430 2431 static int 2432 linux_nat_has_pending_sigint (int pid) 2433 { 2434 sigset_t pending, blocked, ignored; 2435 2436 linux_proc_pending_signals (pid, &pending, &blocked, &ignored); 2437 2438 if (sigismember (&pending, SIGINT) 2439 && !sigismember (&ignored, SIGINT)) 2440 return 1; 2441 2442 return 0; 2443 } 2444 2445 /* Set a flag in LP indicating that we should ignore its next SIGINT. */ 2446 2447 static int 2448 set_ignore_sigint (struct lwp_info *lp) 2449 { 2450 /* If a thread has a pending SIGINT, consume it; otherwise, set a 2451 flag to consume the next one. */ 2452 if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status) 2453 && WSTOPSIG (lp->status) == SIGINT) 2454 lp->status = 0; 2455 else 2456 lp->ignore_sigint = 1; 2457 2458 return 0; 2459 } 2460 2461 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag. 2462 This function is called after we know the LWP has stopped; if the LWP 2463 stopped before the expected SIGINT was delivered, then it will never have 2464 arrived. Also, if the signal was delivered to a shared queue and consumed 2465 by a different thread, it will never be delivered to this LWP. */ 2466 2467 static void 2468 maybe_clear_ignore_sigint (struct lwp_info *lp) 2469 { 2470 if (!lp->ignore_sigint) 2471 return; 2472 2473 if (!linux_nat_has_pending_sigint (lp->ptid.lwp ())) 2474 { 2475 linux_nat_debug_printf ("Clearing bogus flag for %s", 2476 lp->ptid.to_string ().c_str ()); 2477 lp->ignore_sigint = 0; 2478 } 2479 } 2480 2481 /* Fetch the possible triggered data watchpoint info and store it in 2482 LP. 2483 2484 On some archs, like x86, that use debug registers to set 2485 watchpoints, it's possible that the way to know which watched 2486 address trapped, is to check the register that is used to select 2487 which address to watch. Problem is, between setting the watchpoint 2488 and reading back which data address trapped, the user may change 2489 the set of watchpoints, and, as a consequence, GDB changes the 2490 debug registers in the inferior. To avoid reading back a stale 2491 stopped-data-address when that happens, we cache in LP the fact 2492 that a watchpoint trapped, and the corresponding data address, as 2493 soon as we see LP stop with a SIGTRAP. If GDB changes the debug 2494 registers meanwhile, we have the cached data we can rely on. */ 2495 2496 static int 2497 check_stopped_by_watchpoint (struct lwp_info *lp) 2498 { 2499 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); 2500 inferior_ptid = lp->ptid; 2501 2502 if (linux_target->low_stopped_by_watchpoint ()) 2503 { 2504 lp->stop_reason = TARGET_STOPPED_BY_WATCHPOINT; 2505 lp->stopped_data_address_p 2506 = linux_target->low_stopped_data_address (&lp->stopped_data_address); 2507 } 2508 2509 return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT; 2510 } 2511 2512 /* Returns true if the LWP had stopped for a watchpoint. */ 2513 2514 bool 2515 linux_nat_target::stopped_by_watchpoint () 2516 { 2517 struct lwp_info *lp = find_lwp_pid (inferior_ptid); 2518 2519 gdb_assert (lp != NULL); 2520 2521 return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT; 2522 } 2523 2524 bool 2525 linux_nat_target::stopped_data_address (CORE_ADDR *addr_p) 2526 { 2527 struct lwp_info *lp = find_lwp_pid (inferior_ptid); 2528 2529 gdb_assert (lp != NULL); 2530 2531 *addr_p = lp->stopped_data_address; 2532 2533 return lp->stopped_data_address_p; 2534 } 2535 2536 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */ 2537 2538 bool 2539 linux_nat_target::low_status_is_event (int status) 2540 { 2541 return WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP; 2542 } 2543 2544 /* Wait until LP is stopped. */ 2545 2546 static int 2547 stop_wait_callback (struct lwp_info *lp) 2548 { 2549 inferior *inf = find_inferior_ptid (linux_target, lp->ptid); 2550 2551 /* If this is a vfork parent, bail out, it is not going to report 2552 any SIGSTOP until the vfork is done with. */ 2553 if (inf->vfork_child != NULL) 2554 return 0; 2555 2556 if (!lp->stopped) 2557 { 2558 int status; 2559 2560 status = wait_lwp (lp); 2561 if (status == 0) 2562 return 0; 2563 2564 if (lp->ignore_sigint && WIFSTOPPED (status) 2565 && WSTOPSIG (status) == SIGINT) 2566 { 2567 lp->ignore_sigint = 0; 2568 2569 errno = 0; 2570 ptrace (PTRACE_CONT, lp->ptid.lwp (), 0, 0); 2571 lp->stopped = 0; 2572 linux_nat_debug_printf 2573 ("PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)", 2574 lp->ptid.to_string ().c_str (), 2575 errno ? safe_strerror (errno) : "OK"); 2576 2577 return stop_wait_callback (lp); 2578 } 2579 2580 maybe_clear_ignore_sigint (lp); 2581 2582 if (WSTOPSIG (status) != SIGSTOP) 2583 { 2584 /* The thread was stopped with a signal other than SIGSTOP. */ 2585 2586 linux_nat_debug_printf ("Pending event %s in %s", 2587 status_to_str ((int) status).c_str (), 2588 lp->ptid.to_string ().c_str ()); 2589 2590 /* Save the sigtrap event. */ 2591 lp->status = status; 2592 gdb_assert (lp->signalled); 2593 save_stop_reason (lp); 2594 } 2595 else 2596 { 2597 /* We caught the SIGSTOP that we intended to catch. */ 2598 2599 linux_nat_debug_printf ("Expected SIGSTOP caught for %s.", 2600 lp->ptid.to_string ().c_str ()); 2601 2602 lp->signalled = 0; 2603 2604 /* If we are waiting for this stop so we can report the thread 2605 stopped then we need to record this status. Otherwise, we can 2606 now discard this stop event. */ 2607 if (lp->last_resume_kind == resume_stop) 2608 { 2609 lp->status = status; 2610 save_stop_reason (lp); 2611 } 2612 } 2613 } 2614 2615 return 0; 2616 } 2617 2618 /* Get the inferior associated to LWP. Must be called with an LWP that has 2619 an associated inferior. Always return non-nullptr. */ 2620 2621 static inferior * 2622 lwp_inferior (const lwp_info *lwp) 2623 { 2624 inferior *inf = find_inferior_ptid (linux_target, lwp->ptid); 2625 gdb_assert (inf != nullptr); 2626 return inf; 2627 } 2628 2629 /* Return non-zero if LP has a wait status pending. Discard the 2630 pending event and resume the LWP if the event that originally 2631 caused the stop became uninteresting. */ 2632 2633 static int 2634 status_callback (struct lwp_info *lp) 2635 { 2636 /* Only report a pending wait status if we pretend that this has 2637 indeed been resumed. */ 2638 if (!lp->resumed) 2639 return 0; 2640 2641 if (!lwp_status_pending_p (lp)) 2642 return 0; 2643 2644 if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT 2645 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT) 2646 { 2647 struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid); 2648 CORE_ADDR pc; 2649 int discard = 0; 2650 2651 pc = regcache_read_pc (regcache); 2652 2653 if (pc != lp->stop_pc) 2654 { 2655 linux_nat_debug_printf ("PC of %s changed. was=%s, now=%s", 2656 lp->ptid.to_string ().c_str (), 2657 paddress (current_inferior ()->arch (), 2658 lp->stop_pc), 2659 paddress (current_inferior ()->arch (), pc)); 2660 discard = 1; 2661 } 2662 2663 if (discard) 2664 { 2665 linux_nat_debug_printf ("pending event of %s cancelled.", 2666 lp->ptid.to_string ().c_str ()); 2667 2668 lp->status = 0; 2669 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0); 2670 return 0; 2671 } 2672 } 2673 2674 return 1; 2675 } 2676 2677 /* Count the LWP's that have had events. */ 2678 2679 static int 2680 count_events_callback (struct lwp_info *lp, int *count) 2681 { 2682 gdb_assert (count != NULL); 2683 2684 /* Select only resumed LWPs that have an event pending. */ 2685 if (lp->resumed && lwp_status_pending_p (lp)) 2686 (*count)++; 2687 2688 return 0; 2689 } 2690 2691 /* Select the LWP (if any) that is currently being single-stepped. */ 2692 2693 static int 2694 select_singlestep_lwp_callback (struct lwp_info *lp) 2695 { 2696 if (lp->last_resume_kind == resume_step 2697 && lp->status != 0) 2698 return 1; 2699 else 2700 return 0; 2701 } 2702 2703 /* Returns true if LP has a status pending. */ 2704 2705 static int 2706 lwp_status_pending_p (struct lwp_info *lp) 2707 { 2708 /* We check for lp->waitstatus in addition to lp->status, because we 2709 can have pending process exits recorded in lp->status and 2710 W_EXITCODE(0,0) happens to be 0. */ 2711 return lp->status != 0 || lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE; 2712 } 2713 2714 /* Select the Nth LWP that has had an event. */ 2715 2716 static int 2717 select_event_lwp_callback (struct lwp_info *lp, int *selector) 2718 { 2719 gdb_assert (selector != NULL); 2720 2721 /* Select only resumed LWPs that have an event pending. */ 2722 if (lp->resumed && lwp_status_pending_p (lp)) 2723 if ((*selector)-- == 0) 2724 return 1; 2725 2726 return 0; 2727 } 2728 2729 /* Called when the LWP stopped for a signal/trap. If it stopped for a 2730 trap check what caused it (breakpoint, watchpoint, trace, etc.), 2731 and save the result in the LWP's stop_reason field. If it stopped 2732 for a breakpoint, decrement the PC if necessary on the lwp's 2733 architecture. */ 2734 2735 static void 2736 save_stop_reason (struct lwp_info *lp) 2737 { 2738 struct regcache *regcache; 2739 struct gdbarch *gdbarch; 2740 CORE_ADDR pc; 2741 CORE_ADDR sw_bp_pc; 2742 siginfo_t siginfo; 2743 2744 gdb_assert (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON); 2745 gdb_assert (lp->status != 0); 2746 2747 if (!linux_target->low_status_is_event (lp->status)) 2748 return; 2749 2750 inferior *inf = lwp_inferior (lp); 2751 if (inf->starting_up) 2752 return; 2753 2754 regcache = get_thread_regcache (linux_target, lp->ptid); 2755 gdbarch = regcache->arch (); 2756 2757 pc = regcache_read_pc (regcache); 2758 sw_bp_pc = pc - gdbarch_decr_pc_after_break (gdbarch); 2759 2760 if (linux_nat_get_siginfo (lp->ptid, &siginfo)) 2761 { 2762 if (siginfo.si_signo == SIGTRAP) 2763 { 2764 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code) 2765 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code)) 2766 { 2767 /* The si_code is ambiguous on this arch -- check debug 2768 registers. */ 2769 if (!check_stopped_by_watchpoint (lp)) 2770 lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT; 2771 } 2772 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code)) 2773 { 2774 /* If we determine the LWP stopped for a SW breakpoint, 2775 trust it. Particularly don't check watchpoint 2776 registers, because, at least on s390, we'd find 2777 stopped-by-watchpoint as long as there's a watchpoint 2778 set. */ 2779 lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT; 2780 } 2781 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code)) 2782 { 2783 /* This can indicate either a hardware breakpoint or 2784 hardware watchpoint. Check debug registers. */ 2785 if (!check_stopped_by_watchpoint (lp)) 2786 lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT; 2787 } 2788 else if (siginfo.si_code == TRAP_TRACE) 2789 { 2790 linux_nat_debug_printf ("%s stopped by trace", 2791 lp->ptid.to_string ().c_str ()); 2792 2793 /* We may have single stepped an instruction that 2794 triggered a watchpoint. In that case, on some 2795 architectures (such as x86), instead of TRAP_HWBKPT, 2796 si_code indicates TRAP_TRACE, and we need to check 2797 the debug registers separately. */ 2798 check_stopped_by_watchpoint (lp); 2799 } 2800 } 2801 } 2802 2803 if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT) 2804 { 2805 linux_nat_debug_printf ("%s stopped by software breakpoint", 2806 lp->ptid.to_string ().c_str ()); 2807 2808 /* Back up the PC if necessary. */ 2809 if (pc != sw_bp_pc) 2810 regcache_write_pc (regcache, sw_bp_pc); 2811 2812 /* Update this so we record the correct stop PC below. */ 2813 pc = sw_bp_pc; 2814 } 2815 else if (lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT) 2816 { 2817 linux_nat_debug_printf ("%s stopped by hardware breakpoint", 2818 lp->ptid.to_string ().c_str ()); 2819 } 2820 else if (lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT) 2821 { 2822 linux_nat_debug_printf ("%s stopped by hardware watchpoint", 2823 lp->ptid.to_string ().c_str ()); 2824 } 2825 2826 lp->stop_pc = pc; 2827 } 2828 2829 2830 /* Returns true if the LWP had stopped for a software breakpoint. */ 2831 2832 bool 2833 linux_nat_target::stopped_by_sw_breakpoint () 2834 { 2835 struct lwp_info *lp = find_lwp_pid (inferior_ptid); 2836 2837 gdb_assert (lp != NULL); 2838 2839 return lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT; 2840 } 2841 2842 /* Implement the supports_stopped_by_sw_breakpoint method. */ 2843 2844 bool 2845 linux_nat_target::supports_stopped_by_sw_breakpoint () 2846 { 2847 return true; 2848 } 2849 2850 /* Returns true if the LWP had stopped for a hardware 2851 breakpoint/watchpoint. */ 2852 2853 bool 2854 linux_nat_target::stopped_by_hw_breakpoint () 2855 { 2856 struct lwp_info *lp = find_lwp_pid (inferior_ptid); 2857 2858 gdb_assert (lp != NULL); 2859 2860 return lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT; 2861 } 2862 2863 /* Implement the supports_stopped_by_hw_breakpoint method. */ 2864 2865 bool 2866 linux_nat_target::supports_stopped_by_hw_breakpoint () 2867 { 2868 return true; 2869 } 2870 2871 /* Select one LWP out of those that have events pending. */ 2872 2873 static void 2874 select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status) 2875 { 2876 int num_events = 0; 2877 int random_selector; 2878 struct lwp_info *event_lp = NULL; 2879 2880 /* Record the wait status for the original LWP. */ 2881 (*orig_lp)->status = *status; 2882 2883 /* In all-stop, give preference to the LWP that is being 2884 single-stepped. There will be at most one, and it will be the 2885 LWP that the core is most interested in. If we didn't do this, 2886 then we'd have to handle pending step SIGTRAPs somehow in case 2887 the core later continues the previously-stepped thread, as 2888 otherwise we'd report the pending SIGTRAP then, and the core, not 2889 having stepped the thread, wouldn't understand what the trap was 2890 for, and therefore would report it to the user as a random 2891 signal. */ 2892 if (!target_is_non_stop_p ()) 2893 { 2894 event_lp = iterate_over_lwps (filter, select_singlestep_lwp_callback); 2895 if (event_lp != NULL) 2896 { 2897 linux_nat_debug_printf ("Select single-step %s", 2898 event_lp->ptid.to_string ().c_str ()); 2899 } 2900 } 2901 2902 if (event_lp == NULL) 2903 { 2904 /* Pick one at random, out of those which have had events. */ 2905 2906 /* First see how many events we have. */ 2907 iterate_over_lwps (filter, 2908 [&] (struct lwp_info *info) 2909 { 2910 return count_events_callback (info, &num_events); 2911 }); 2912 gdb_assert (num_events > 0); 2913 2914 /* Now randomly pick a LWP out of those that have had 2915 events. */ 2916 random_selector = (int) 2917 ((num_events * (double) rand ()) / (RAND_MAX + 1.0)); 2918 2919 if (num_events > 1) 2920 linux_nat_debug_printf ("Found %d events, selecting #%d", 2921 num_events, random_selector); 2922 2923 event_lp 2924 = (iterate_over_lwps 2925 (filter, 2926 [&] (struct lwp_info *info) 2927 { 2928 return select_event_lwp_callback (info, 2929 &random_selector); 2930 })); 2931 } 2932 2933 if (event_lp != NULL) 2934 { 2935 /* Switch the event LWP. */ 2936 *orig_lp = event_lp; 2937 *status = event_lp->status; 2938 } 2939 2940 /* Flush the wait status for the event LWP. */ 2941 (*orig_lp)->status = 0; 2942 } 2943 2944 /* Return non-zero if LP has been resumed. */ 2945 2946 static int 2947 resumed_callback (struct lwp_info *lp) 2948 { 2949 return lp->resumed; 2950 } 2951 2952 /* Check if we should go on and pass this event to common code. 2953 2954 If so, save the status to the lwp_info structure associated to LWPID. */ 2955 2956 static void 2957 linux_nat_filter_event (int lwpid, int status) 2958 { 2959 struct lwp_info *lp; 2960 int event = linux_ptrace_get_extended_event (status); 2961 2962 lp = find_lwp_pid (ptid_t (lwpid)); 2963 2964 /* Check for events reported by anything not in our LWP list. */ 2965 if (lp == nullptr) 2966 { 2967 if (WIFSTOPPED (status)) 2968 { 2969 if (WSTOPSIG (status) == SIGTRAP && event == PTRACE_EVENT_EXEC) 2970 { 2971 /* A non-leader thread exec'ed after we've seen the 2972 leader zombie, and removed it from our lists (in 2973 check_zombie_leaders). The non-leader thread changes 2974 its tid to the tgid. */ 2975 linux_nat_debug_printf 2976 ("Re-adding thread group leader LWP %d after exec.", 2977 lwpid); 2978 2979 lp = add_lwp (ptid_t (lwpid, lwpid)); 2980 lp->stopped = 1; 2981 lp->resumed = 1; 2982 add_thread (linux_target, lp->ptid); 2983 } 2984 else 2985 { 2986 /* A process we are controlling has forked and the new 2987 child's stop was reported to us by the kernel. Save 2988 its PID and go back to waiting for the fork event to 2989 be reported - the stopped process might be returned 2990 from waitpid before or after the fork event is. */ 2991 linux_nat_debug_printf 2992 ("Saving LWP %d status %s in stopped_pids list", 2993 lwpid, status_to_str (status).c_str ()); 2994 add_to_pid_list (&stopped_pids, lwpid, status); 2995 } 2996 } 2997 else 2998 { 2999 /* Don't report an event for the exit of an LWP not in our 3000 list, i.e. not part of any inferior we're debugging. 3001 This can happen if we detach from a program we originally 3002 forked and then it exits. However, note that we may have 3003 earlier deleted a leader of an inferior we're debugging, 3004 in check_zombie_leaders. Re-add it back here if so. */ 3005 for (inferior *inf : all_inferiors (linux_target)) 3006 { 3007 if (inf->pid == lwpid) 3008 { 3009 linux_nat_debug_printf 3010 ("Re-adding thread group leader LWP %d after exit.", 3011 lwpid); 3012 3013 lp = add_lwp (ptid_t (lwpid, lwpid)); 3014 lp->resumed = 1; 3015 add_thread (linux_target, lp->ptid); 3016 break; 3017 } 3018 } 3019 } 3020 3021 if (lp == nullptr) 3022 return; 3023 } 3024 3025 /* This LWP is stopped now. (And if dead, this prevents it from 3026 ever being continued.) */ 3027 lp->stopped = 1; 3028 3029 if (WIFSTOPPED (status) && lp->must_set_ptrace_flags) 3030 { 3031 inferior *inf = find_inferior_pid (linux_target, lp->ptid.pid ()); 3032 int options = linux_nat_ptrace_options (inf->attach_flag); 3033 3034 linux_enable_event_reporting (lp->ptid.lwp (), options); 3035 lp->must_set_ptrace_flags = 0; 3036 } 3037 3038 /* Handle GNU/Linux's syscall SIGTRAPs. */ 3039 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP) 3040 { 3041 /* No longer need the sysgood bit. The ptrace event ends up 3042 recorded in lp->waitstatus if we care for it. We can carry 3043 on handling the event like a regular SIGTRAP from here 3044 on. */ 3045 status = W_STOPCODE (SIGTRAP); 3046 if (linux_handle_syscall_trap (lp, 0)) 3047 return; 3048 } 3049 else 3050 { 3051 /* Almost all other ptrace-stops are known to be outside of system 3052 calls, with further exceptions in linux_handle_extended_wait. */ 3053 lp->syscall_state = TARGET_WAITKIND_IGNORE; 3054 } 3055 3056 /* Handle GNU/Linux's extended waitstatus for trace events. */ 3057 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP 3058 && linux_is_extended_waitstatus (status)) 3059 { 3060 linux_nat_debug_printf ("Handling extended status 0x%06x", status); 3061 3062 if (linux_handle_extended_wait (lp, status)) 3063 return; 3064 } 3065 3066 /* Check if the thread has exited. */ 3067 if (WIFEXITED (status) || WIFSIGNALED (status)) 3068 { 3069 if (!report_exit_events_for (lp) && !is_leader (lp)) 3070 { 3071 linux_nat_debug_printf ("%s exited.", 3072 lp->ptid.to_string ().c_str ()); 3073 3074 /* If this was not the leader exiting, then the exit signal 3075 was not the end of the debugged application and should be 3076 ignored. */ 3077 exit_lwp (lp); 3078 return; 3079 } 3080 3081 /* Note that even if the leader was ptrace-stopped, it can still 3082 exit, if e.g., some other thread brings down the whole 3083 process (calls `exit'). So don't assert that the lwp is 3084 resumed. */ 3085 linux_nat_debug_printf ("LWP %ld exited (resumed=%d)", 3086 lp->ptid.lwp (), lp->resumed); 3087 3088 mark_lwp_dead (lp, status); 3089 return; 3090 } 3091 3092 /* Make sure we don't report a SIGSTOP that we sent ourselves in 3093 an attempt to stop an LWP. */ 3094 if (lp->signalled 3095 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP) 3096 { 3097 lp->signalled = 0; 3098 3099 if (lp->last_resume_kind == resume_stop) 3100 { 3101 linux_nat_debug_printf ("resume_stop SIGSTOP caught for %s.", 3102 lp->ptid.to_string ().c_str ()); 3103 } 3104 else 3105 { 3106 /* This is a delayed SIGSTOP. Filter out the event. */ 3107 3108 linux_nat_debug_printf 3109 ("%s %s, 0, 0 (discard delayed SIGSTOP)", 3110 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", 3111 lp->ptid.to_string ().c_str ()); 3112 3113 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0); 3114 gdb_assert (lp->resumed); 3115 return; 3116 } 3117 } 3118 3119 /* Make sure we don't report a SIGINT that we have already displayed 3120 for another thread. */ 3121 if (lp->ignore_sigint 3122 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT) 3123 { 3124 linux_nat_debug_printf ("Delayed SIGINT caught for %s.", 3125 lp->ptid.to_string ().c_str ()); 3126 3127 /* This is a delayed SIGINT. */ 3128 lp->ignore_sigint = 0; 3129 3130 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0); 3131 linux_nat_debug_printf ("%s %s, 0, 0 (discard SIGINT)", 3132 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", 3133 lp->ptid.to_string ().c_str ()); 3134 gdb_assert (lp->resumed); 3135 3136 /* Discard the event. */ 3137 return; 3138 } 3139 3140 /* Don't report signals that GDB isn't interested in, such as 3141 signals that are neither printed nor stopped upon. Stopping all 3142 threads can be a bit time-consuming, so if we want decent 3143 performance with heavily multi-threaded programs, especially when 3144 they're using a high frequency timer, we'd better avoid it if we 3145 can. */ 3146 if (WIFSTOPPED (status)) 3147 { 3148 enum gdb_signal signo = gdb_signal_from_host (WSTOPSIG (status)); 3149 3150 if (!target_is_non_stop_p ()) 3151 { 3152 /* Only do the below in all-stop, as we currently use SIGSTOP 3153 to implement target_stop (see linux_nat_stop) in 3154 non-stop. */ 3155 if (signo == GDB_SIGNAL_INT && signal_pass_state (signo) == 0) 3156 { 3157 /* If ^C/BREAK is typed at the tty/console, SIGINT gets 3158 forwarded to the entire process group, that is, all LWPs 3159 will receive it - unless they're using CLONE_THREAD to 3160 share signals. Since we only want to report it once, we 3161 mark it as ignored for all LWPs except this one. */ 3162 iterate_over_lwps (ptid_t (lp->ptid.pid ()), set_ignore_sigint); 3163 lp->ignore_sigint = 0; 3164 } 3165 else 3166 maybe_clear_ignore_sigint (lp); 3167 } 3168 3169 /* When using hardware single-step, we need to report every signal. 3170 Otherwise, signals in pass_mask may be short-circuited 3171 except signals that might be caused by a breakpoint, or SIGSTOP 3172 if we sent the SIGSTOP and are waiting for it to arrive. */ 3173 if (!lp->step 3174 && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status)) 3175 && (WSTOPSIG (status) != SIGSTOP 3176 || !linux_target->find_thread (lp->ptid)->stop_requested) 3177 && !linux_wstatus_maybe_breakpoint (status)) 3178 { 3179 linux_resume_one_lwp (lp, lp->step, signo); 3180 linux_nat_debug_printf 3181 ("%s %s, %s (preempt 'handle')", 3182 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", 3183 lp->ptid.to_string ().c_str (), 3184 (signo != GDB_SIGNAL_0 3185 ? strsignal (gdb_signal_to_host (signo)) : "0")); 3186 return; 3187 } 3188 } 3189 3190 /* An interesting event. */ 3191 gdb_assert (lp); 3192 lp->status = status; 3193 save_stop_reason (lp); 3194 } 3195 3196 /* Detect zombie thread group leaders, and "exit" them. We can't reap 3197 their exits until all other threads in the group have exited. */ 3198 3199 static void 3200 check_zombie_leaders (void) 3201 { 3202 for (inferior *inf : all_inferiors ()) 3203 { 3204 struct lwp_info *leader_lp; 3205 3206 if (inf->pid == 0) 3207 continue; 3208 3209 leader_lp = find_lwp_pid (ptid_t (inf->pid)); 3210 if (leader_lp != NULL 3211 /* Check if there are other threads in the group, as we may 3212 have raced with the inferior simply exiting. Note this 3213 isn't a watertight check. If the inferior is 3214 multi-threaded and is exiting, it may be we see the 3215 leader as zombie before we reap all the non-leader 3216 threads. See comments below. */ 3217 && num_lwps (inf->pid) > 1 3218 && linux_proc_pid_is_zombie (inf->pid)) 3219 { 3220 /* A zombie leader in a multi-threaded program can mean one 3221 of three things: 3222 3223 #1 - Only the leader exited, not the whole program, e.g., 3224 with pthread_exit. Since we can't reap the leader's exit 3225 status until all other threads are gone and reaped too, 3226 we want to delete the zombie leader right away, as it 3227 can't be debugged, we can't read its registers, etc. 3228 This is the main reason we check for zombie leaders 3229 disappearing. 3230 3231 #2 - The whole thread-group/process exited (a group exit, 3232 via e.g. exit(3), and there is (or will be shortly) an 3233 exit reported for each thread in the process, and then 3234 finally an exit for the leader once the non-leaders are 3235 reaped. 3236 3237 #3 - There are 3 or more threads in the group, and a 3238 thread other than the leader exec'd. See comments on 3239 exec events at the top of the file. 3240 3241 Ideally we would never delete the leader for case #2. 3242 Instead, we want to collect the exit status of each 3243 non-leader thread, and then finally collect the exit 3244 status of the leader as normal and use its exit code as 3245 whole-process exit code. Unfortunately, there's no 3246 race-free way to distinguish cases #1 and #2. We can't 3247 assume the exit events for the non-leaders threads are 3248 already pending in the kernel, nor can we assume the 3249 non-leader threads are in zombie state already. Between 3250 the leader becoming zombie and the non-leaders exiting 3251 and becoming zombie themselves, there's a small time 3252 window, so such a check would be racy. Temporarily 3253 pausing all threads and checking to see if all threads 3254 exit or not before re-resuming them would work in the 3255 case that all threads are running right now, but it 3256 wouldn't work if some thread is currently already 3257 ptrace-stopped, e.g., due to scheduler-locking. 3258 3259 So what we do is we delete the leader anyhow, and then 3260 later on when we see its exit status, we re-add it back. 3261 We also make sure that we only report a whole-process 3262 exit when we see the leader exiting, as opposed to when 3263 the last LWP in the LWP list exits, which can be a 3264 non-leader if we deleted the leader here. */ 3265 linux_nat_debug_printf ("Thread group leader %d zombie " 3266 "(it exited, or another thread execd), " 3267 "deleting it.", 3268 inf->pid); 3269 exit_lwp (leader_lp); 3270 } 3271 } 3272 } 3273 3274 /* Convenience function that is called when we're about to return an 3275 event to the core. If the event is an exit or signalled event, 3276 then this decides whether to report it as process-wide event, as a 3277 thread exit event, or to suppress it. All other event kinds are 3278 passed through unmodified. */ 3279 3280 static ptid_t 3281 filter_exit_event (struct lwp_info *event_child, 3282 struct target_waitstatus *ourstatus) 3283 { 3284 ptid_t ptid = event_child->ptid; 3285 3286 /* Note we must filter TARGET_WAITKIND_SIGNALLED as well, otherwise 3287 if a non-leader thread exits with a signal, we'd report it to the 3288 core which would interpret it as the whole-process exiting. 3289 There is no TARGET_WAITKIND_THREAD_SIGNALLED event kind. */ 3290 if (ourstatus->kind () != TARGET_WAITKIND_EXITED 3291 && ourstatus->kind () != TARGET_WAITKIND_SIGNALLED) 3292 return ptid; 3293 3294 if (!is_leader (event_child)) 3295 { 3296 if (report_exit_events_for (event_child)) 3297 { 3298 ourstatus->set_thread_exited (0); 3299 /* Delete lwp, but not thread_info, infrun will need it to 3300 process the event. */ 3301 exit_lwp (event_child, false); 3302 } 3303 else 3304 { 3305 ourstatus->set_ignore (); 3306 exit_lwp (event_child); 3307 } 3308 } 3309 3310 return ptid; 3311 } 3312 3313 static ptid_t 3314 linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus, 3315 target_wait_flags target_options) 3316 { 3317 LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 3318 3319 sigset_t prev_mask; 3320 enum resume_kind last_resume_kind; 3321 struct lwp_info *lp; 3322 int status; 3323 3324 /* The first time we get here after starting a new inferior, we may 3325 not have added it to the LWP list yet - this is the earliest 3326 moment at which we know its PID. */ 3327 if (ptid.is_pid () && find_lwp_pid (ptid) == nullptr) 3328 { 3329 ptid_t lwp_ptid (ptid.pid (), ptid.pid ()); 3330 3331 /* Upgrade the main thread's ptid. */ 3332 thread_change_ptid (linux_target, ptid, lwp_ptid); 3333 lp = add_initial_lwp (lwp_ptid); 3334 lp->resumed = 1; 3335 } 3336 3337 /* Make sure SIGCHLD is blocked until the sigsuspend below. */ 3338 block_child_signals (&prev_mask); 3339 3340 /* First check if there is a LWP with a wait status pending. */ 3341 lp = iterate_over_lwps (ptid, status_callback); 3342 if (lp != NULL) 3343 { 3344 linux_nat_debug_printf ("Using pending wait status %s for %s.", 3345 pending_status_str (lp).c_str (), 3346 lp->ptid.to_string ().c_str ()); 3347 } 3348 3349 /* But if we don't find a pending event, we'll have to wait. Always 3350 pull all events out of the kernel. We'll randomly select an 3351 event LWP out of all that have events, to prevent starvation. */ 3352 3353 while (lp == NULL) 3354 { 3355 pid_t lwpid; 3356 3357 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace 3358 quirks: 3359 3360 - If the thread group leader exits while other threads in the 3361 thread group still exist, waitpid(TGID, ...) hangs. That 3362 waitpid won't return an exit status until the other threads 3363 in the group are reaped. 3364 3365 - When a non-leader thread execs, that thread just vanishes 3366 without reporting an exit (so we'd hang if we waited for it 3367 explicitly in that case). The exec event is reported to 3368 the TGID pid. */ 3369 3370 errno = 0; 3371 lwpid = my_waitpid (-1, &status, __WALL | WNOHANG); 3372 3373 linux_nat_debug_printf ("waitpid(-1, ...) returned %d, %s", 3374 lwpid, 3375 errno ? safe_strerror (errno) : "ERRNO-OK"); 3376 3377 if (lwpid > 0) 3378 { 3379 linux_nat_debug_printf ("waitpid %ld received %s", 3380 (long) lwpid, 3381 status_to_str (status).c_str ()); 3382 3383 linux_nat_filter_event (lwpid, status); 3384 /* Retry until nothing comes out of waitpid. A single 3385 SIGCHLD can indicate more than one child stopped. */ 3386 continue; 3387 } 3388 3389 /* Now that we've pulled all events out of the kernel, resume 3390 LWPs that don't have an interesting event to report. */ 3391 iterate_over_lwps (minus_one_ptid, 3392 [] (struct lwp_info *info) 3393 { 3394 return resume_stopped_resumed_lwps (info, minus_one_ptid); 3395 }); 3396 3397 /* ... and find an LWP with a status to report to the core, if 3398 any. */ 3399 lp = iterate_over_lwps (ptid, status_callback); 3400 if (lp != NULL) 3401 break; 3402 3403 /* Check for zombie thread group leaders. Those can't be reaped 3404 until all other threads in the thread group are. */ 3405 check_zombie_leaders (); 3406 3407 /* If there are no resumed children left, bail. We'd be stuck 3408 forever in the sigsuspend call below otherwise. */ 3409 if (iterate_over_lwps (ptid, resumed_callback) == NULL) 3410 { 3411 linux_nat_debug_printf ("exit (no resumed LWP)"); 3412 3413 ourstatus->set_no_resumed (); 3414 3415 restore_child_signals_mask (&prev_mask); 3416 return minus_one_ptid; 3417 } 3418 3419 /* No interesting event to report to the core. */ 3420 3421 if (target_options & TARGET_WNOHANG) 3422 { 3423 linux_nat_debug_printf ("no interesting events found"); 3424 3425 ourstatus->set_ignore (); 3426 restore_child_signals_mask (&prev_mask); 3427 return minus_one_ptid; 3428 } 3429 3430 /* We shouldn't end up here unless we want to try again. */ 3431 gdb_assert (lp == NULL); 3432 3433 /* Block until we get an event reported with SIGCHLD. */ 3434 wait_for_signal (); 3435 } 3436 3437 gdb_assert (lp); 3438 gdb_assert (lp->stopped); 3439 3440 status = lp->status; 3441 lp->status = 0; 3442 3443 if (!target_is_non_stop_p ()) 3444 { 3445 /* Now stop all other LWP's ... */ 3446 iterate_over_lwps (minus_one_ptid, stop_callback); 3447 3448 /* ... and wait until all of them have reported back that 3449 they're no longer running. */ 3450 iterate_over_lwps (minus_one_ptid, stop_wait_callback); 3451 } 3452 3453 /* If we're not waiting for a specific LWP, choose an event LWP from 3454 among those that have had events. Giving equal priority to all 3455 LWPs that have had events helps prevent starvation. */ 3456 if (ptid == minus_one_ptid || ptid.is_pid ()) 3457 select_event_lwp (ptid, &lp, &status); 3458 3459 gdb_assert (lp != NULL); 3460 3461 /* We'll need this to determine whether to report a SIGSTOP as 3462 GDB_SIGNAL_0. Need to take a copy because resume_clear_callback 3463 clears it. */ 3464 last_resume_kind = lp->last_resume_kind; 3465 3466 if (!target_is_non_stop_p ()) 3467 { 3468 /* In all-stop, from the core's perspective, all LWPs are now 3469 stopped until a new resume action is sent over. */ 3470 iterate_over_lwps (minus_one_ptid, resume_clear_callback); 3471 } 3472 else 3473 { 3474 resume_clear_callback (lp); 3475 } 3476 3477 if (linux_target->low_status_is_event (status)) 3478 { 3479 linux_nat_debug_printf ("trap ptid is %s.", 3480 lp->ptid.to_string ().c_str ()); 3481 } 3482 3483 if (lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE) 3484 { 3485 *ourstatus = lp->waitstatus; 3486 lp->waitstatus.set_ignore (); 3487 } 3488 else 3489 *ourstatus = host_status_to_waitstatus (status); 3490 3491 linux_nat_debug_printf ("event found"); 3492 3493 restore_child_signals_mask (&prev_mask); 3494 3495 if (last_resume_kind == resume_stop 3496 && ourstatus->kind () == TARGET_WAITKIND_STOPPED 3497 && WSTOPSIG (status) == SIGSTOP) 3498 { 3499 /* A thread that has been requested to stop by GDB with 3500 target_stop, and it stopped cleanly, so report as SIG0. The 3501 use of SIGSTOP is an implementation detail. */ 3502 ourstatus->set_stopped (GDB_SIGNAL_0); 3503 } 3504 3505 if (ourstatus->kind () == TARGET_WAITKIND_EXITED 3506 || ourstatus->kind () == TARGET_WAITKIND_SIGNALLED) 3507 lp->core = -1; 3508 else 3509 lp->core = linux_common_core_of_thread (lp->ptid); 3510 3511 return filter_exit_event (lp, ourstatus); 3512 } 3513 3514 /* Resume LWPs that are currently stopped without any pending status 3515 to report, but are resumed from the core's perspective. */ 3516 3517 static int 3518 resume_stopped_resumed_lwps (struct lwp_info *lp, const ptid_t wait_ptid) 3519 { 3520 inferior *inf = lwp_inferior (lp); 3521 3522 if (!lp->stopped) 3523 { 3524 linux_nat_debug_printf ("NOT resuming LWP %s, not stopped", 3525 lp->ptid.to_string ().c_str ()); 3526 } 3527 else if (!lp->resumed) 3528 { 3529 linux_nat_debug_printf ("NOT resuming LWP %s, not resumed", 3530 lp->ptid.to_string ().c_str ()); 3531 } 3532 else if (lwp_status_pending_p (lp)) 3533 { 3534 linux_nat_debug_printf ("NOT resuming LWP %s, has pending status", 3535 lp->ptid.to_string ().c_str ()); 3536 } 3537 else if (inf->vfork_child != nullptr) 3538 { 3539 linux_nat_debug_printf ("NOT resuming LWP %s (vfork parent)", 3540 lp->ptid.to_string ().c_str ()); 3541 } 3542 else 3543 { 3544 struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid); 3545 struct gdbarch *gdbarch = regcache->arch (); 3546 3547 try 3548 { 3549 CORE_ADDR pc = regcache_read_pc (regcache); 3550 int leave_stopped = 0; 3551 3552 /* Don't bother if there's a breakpoint at PC that we'd hit 3553 immediately, and we're not waiting for this LWP. */ 3554 if (!lp->ptid.matches (wait_ptid)) 3555 { 3556 if (breakpoint_inserted_here_p (inf->aspace.get (), pc)) 3557 leave_stopped = 1; 3558 } 3559 3560 if (!leave_stopped) 3561 { 3562 linux_nat_debug_printf 3563 ("resuming stopped-resumed LWP %s at %s: step=%d", 3564 lp->ptid.to_string ().c_str (), paddress (gdbarch, pc), 3565 lp->step); 3566 3567 linux_resume_one_lwp_throw (lp, lp->step, GDB_SIGNAL_0); 3568 } 3569 } 3570 catch (const gdb_exception_error &ex) 3571 { 3572 if (!check_ptrace_stopped_lwp_gone (lp)) 3573 throw; 3574 } 3575 } 3576 3577 return 0; 3578 } 3579 3580 ptid_t 3581 linux_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, 3582 target_wait_flags target_options) 3583 { 3584 LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 3585 3586 ptid_t event_ptid; 3587 3588 linux_nat_debug_printf ("[%s], [%s]", ptid.to_string ().c_str (), 3589 target_options_to_string (target_options).c_str ()); 3590 3591 /* Flush the async file first. */ 3592 if (target_is_async_p ()) 3593 async_file_flush (); 3594 3595 /* Resume LWPs that are currently stopped without any pending status 3596 to report, but are resumed from the core's perspective. LWPs get 3597 in this state if we find them stopping at a time we're not 3598 interested in reporting the event (target_wait on a 3599 specific_process, for example, see linux_nat_wait_1), and 3600 meanwhile the event became uninteresting. Don't bother resuming 3601 LWPs we're not going to wait for if they'd stop immediately. */ 3602 if (target_is_non_stop_p ()) 3603 iterate_over_lwps (minus_one_ptid, 3604 [=] (struct lwp_info *info) 3605 { 3606 return resume_stopped_resumed_lwps (info, ptid); 3607 }); 3608 3609 event_ptid = linux_nat_wait_1 (ptid, ourstatus, target_options); 3610 3611 /* If we requested any event, and something came out, assume there 3612 may be more. If we requested a specific lwp or process, also 3613 assume there may be more. */ 3614 if (target_is_async_p () 3615 && ((ourstatus->kind () != TARGET_WAITKIND_IGNORE 3616 && ourstatus->kind () != TARGET_WAITKIND_NO_RESUMED) 3617 || ptid != minus_one_ptid)) 3618 async_file_mark (); 3619 3620 return event_ptid; 3621 } 3622 3623 /* Kill one LWP. */ 3624 3625 static void 3626 kill_one_lwp (pid_t pid) 3627 { 3628 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */ 3629 3630 errno = 0; 3631 kill_lwp (pid, SIGKILL); 3632 3633 if (debug_linux_nat) 3634 { 3635 int save_errno = errno; 3636 3637 linux_nat_debug_printf 3638 ("kill (SIGKILL) %ld, 0, 0 (%s)", (long) pid, 3639 save_errno != 0 ? safe_strerror (save_errno) : "OK"); 3640 } 3641 3642 /* Some kernels ignore even SIGKILL for processes under ptrace. */ 3643 3644 errno = 0; 3645 ptrace (PTRACE_KILL, pid, 0, 0); 3646 if (debug_linux_nat) 3647 { 3648 int save_errno = errno; 3649 3650 linux_nat_debug_printf 3651 ("PTRACE_KILL %ld, 0, 0 (%s)", (long) pid, 3652 save_errno ? safe_strerror (save_errno) : "OK"); 3653 } 3654 } 3655 3656 /* Wait for an LWP to die. */ 3657 3658 static void 3659 kill_wait_one_lwp (pid_t pid) 3660 { 3661 pid_t res; 3662 3663 /* We must make sure that there are no pending events (delayed 3664 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current 3665 program doesn't interfere with any following debugging session. */ 3666 3667 do 3668 { 3669 res = my_waitpid (pid, NULL, __WALL); 3670 if (res != (pid_t) -1) 3671 { 3672 linux_nat_debug_printf ("wait %ld received unknown.", (long) pid); 3673 3674 /* The Linux kernel sometimes fails to kill a thread 3675 completely after PTRACE_KILL; that goes from the stop 3676 point in do_fork out to the one in get_signal_to_deliver 3677 and waits again. So kill it again. */ 3678 kill_one_lwp (pid); 3679 } 3680 } 3681 while (res == pid); 3682 3683 gdb_assert (res == -1 && errno == ECHILD); 3684 } 3685 3686 /* Callback for iterate_over_lwps. */ 3687 3688 static int 3689 kill_callback (struct lwp_info *lp) 3690 { 3691 kill_one_lwp (lp->ptid.lwp ()); 3692 return 0; 3693 } 3694 3695 /* Callback for iterate_over_lwps. */ 3696 3697 static int 3698 kill_wait_callback (struct lwp_info *lp) 3699 { 3700 kill_wait_one_lwp (lp->ptid.lwp ()); 3701 return 0; 3702 } 3703 3704 /* Kill the fork/clone child of LP if it has an unfollowed child. */ 3705 3706 static int 3707 kill_unfollowed_child_callback (lwp_info *lp) 3708 { 3709 std::optional<target_waitstatus> ws = get_pending_child_status (lp); 3710 if (ws.has_value ()) 3711 { 3712 ptid_t child_ptid = ws->child_ptid (); 3713 int child_pid = child_ptid.pid (); 3714 int child_lwp = child_ptid.lwp (); 3715 3716 kill_one_lwp (child_lwp); 3717 kill_wait_one_lwp (child_lwp); 3718 3719 /* Let the arch-specific native code know this process is 3720 gone. */ 3721 if (ws->kind () != TARGET_WAITKIND_THREAD_CLONED) 3722 linux_target->low_forget_process (child_pid); 3723 } 3724 3725 return 0; 3726 } 3727 3728 void 3729 linux_nat_target::kill () 3730 { 3731 ptid_t pid_ptid (inferior_ptid.pid ()); 3732 3733 /* If we're stopped while forking/cloning and we haven't followed 3734 yet, kill the child task. We need to do this first because the 3735 parent will be sleeping if this is a vfork. */ 3736 iterate_over_lwps (pid_ptid, kill_unfollowed_child_callback); 3737 3738 if (forks_exist_p ()) 3739 linux_fork_killall (); 3740 else 3741 { 3742 /* Stop all threads before killing them, since ptrace requires 3743 that the thread is stopped to successfully PTRACE_KILL. */ 3744 iterate_over_lwps (pid_ptid, stop_callback); 3745 /* ... and wait until all of them have reported back that 3746 they're no longer running. */ 3747 iterate_over_lwps (pid_ptid, stop_wait_callback); 3748 3749 /* Kill all LWP's ... */ 3750 iterate_over_lwps (pid_ptid, kill_callback); 3751 3752 /* ... and wait until we've flushed all events. */ 3753 iterate_over_lwps (pid_ptid, kill_wait_callback); 3754 } 3755 3756 target_mourn_inferior (inferior_ptid); 3757 } 3758 3759 void 3760 linux_nat_target::mourn_inferior () 3761 { 3762 LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 3763 3764 int pid = inferior_ptid.pid (); 3765 3766 purge_lwp_list (pid); 3767 3768 close_proc_mem_file (pid); 3769 3770 if (! forks_exist_p ()) 3771 /* Normal case, no other forks available. */ 3772 inf_ptrace_target::mourn_inferior (); 3773 else 3774 /* Multi-fork case. The current inferior_ptid has exited, but 3775 there are other viable forks to debug. Delete the exiting 3776 one and context-switch to the first available. */ 3777 linux_fork_mourn_inferior (); 3778 3779 /* Let the arch-specific native code know this process is gone. */ 3780 linux_target->low_forget_process (pid); 3781 } 3782 3783 /* Convert a native/host siginfo object, into/from the siginfo in the 3784 layout of the inferiors' architecture. */ 3785 3786 static void 3787 siginfo_fixup (siginfo_t *siginfo, gdb_byte *inf_siginfo, int direction) 3788 { 3789 /* If the low target didn't do anything, then just do a straight 3790 memcpy. */ 3791 if (!linux_target->low_siginfo_fixup (siginfo, inf_siginfo, direction)) 3792 { 3793 if (direction == 1) 3794 memcpy (siginfo, inf_siginfo, sizeof (siginfo_t)); 3795 else 3796 memcpy (inf_siginfo, siginfo, sizeof (siginfo_t)); 3797 } 3798 } 3799 3800 static enum target_xfer_status 3801 linux_xfer_siginfo (ptid_t ptid, enum target_object object, 3802 const char *annex, gdb_byte *readbuf, 3803 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, 3804 ULONGEST *xfered_len) 3805 { 3806 siginfo_t siginfo; 3807 gdb_byte inf_siginfo[sizeof (siginfo_t)]; 3808 3809 gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO); 3810 gdb_assert (readbuf || writebuf); 3811 3812 if (offset > sizeof (siginfo)) 3813 return TARGET_XFER_E_IO; 3814 3815 if (!linux_nat_get_siginfo (ptid, &siginfo)) 3816 return TARGET_XFER_E_IO; 3817 3818 /* When GDB is built as a 64-bit application, ptrace writes into 3819 SIGINFO an object with 64-bit layout. Since debugging a 32-bit 3820 inferior with a 64-bit GDB should look the same as debugging it 3821 with a 32-bit GDB, we need to convert it. GDB core always sees 3822 the converted layout, so any read/write will have to be done 3823 post-conversion. */ 3824 siginfo_fixup (&siginfo, inf_siginfo, 0); 3825 3826 if (offset + len > sizeof (siginfo)) 3827 len = sizeof (siginfo) - offset; 3828 3829 if (readbuf != NULL) 3830 memcpy (readbuf, inf_siginfo + offset, len); 3831 else 3832 { 3833 memcpy (inf_siginfo + offset, writebuf, len); 3834 3835 /* Convert back to ptrace layout before flushing it out. */ 3836 siginfo_fixup (&siginfo, inf_siginfo, 1); 3837 3838 int pid = get_ptrace_pid (ptid); 3839 errno = 0; 3840 ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo); 3841 if (errno != 0) 3842 return TARGET_XFER_E_IO; 3843 } 3844 3845 *xfered_len = len; 3846 return TARGET_XFER_OK; 3847 } 3848 3849 static enum target_xfer_status 3850 linux_nat_xfer_osdata (enum target_object object, 3851 const char *annex, gdb_byte *readbuf, 3852 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, 3853 ULONGEST *xfered_len); 3854 3855 static enum target_xfer_status 3856 linux_proc_xfer_memory_partial (int pid, gdb_byte *readbuf, 3857 const gdb_byte *writebuf, ULONGEST offset, 3858 LONGEST len, ULONGEST *xfered_len); 3859 3860 /* Look for an LWP of PID that we know is ptrace-stopped. Returns 3861 NULL if none is found. */ 3862 3863 static lwp_info * 3864 find_stopped_lwp (int pid) 3865 { 3866 for (lwp_info *lp : all_lwps ()) 3867 if (lp->ptid.pid () == pid 3868 && lp->stopped 3869 && !is_lwp_marked_dead (lp)) 3870 return lp; 3871 return nullptr; 3872 } 3873 3874 enum target_xfer_status 3875 linux_nat_target::xfer_partial (enum target_object object, 3876 const char *annex, gdb_byte *readbuf, 3877 const gdb_byte *writebuf, 3878 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) 3879 { 3880 if (object == TARGET_OBJECT_SIGNAL_INFO) 3881 return linux_xfer_siginfo (inferior_ptid, object, annex, readbuf, writebuf, 3882 offset, len, xfered_len); 3883 3884 /* The target is connected but no live inferior is selected. Pass 3885 this request down to a lower stratum (e.g., the executable 3886 file). */ 3887 if (object == TARGET_OBJECT_MEMORY && inferior_ptid == null_ptid) 3888 return TARGET_XFER_EOF; 3889 3890 if (object == TARGET_OBJECT_AUXV) 3891 return memory_xfer_auxv (this, object, annex, readbuf, writebuf, 3892 offset, len, xfered_len); 3893 3894 if (object == TARGET_OBJECT_OSDATA) 3895 return linux_nat_xfer_osdata (object, annex, readbuf, writebuf, 3896 offset, len, xfered_len); 3897 3898 if (object == TARGET_OBJECT_MEMORY) 3899 { 3900 /* GDB calculates all addresses in the largest possible address 3901 width. The address width must be masked before its final use 3902 by linux_proc_xfer_partial. 3903 3904 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */ 3905 int addr_bit = gdbarch_addr_bit (current_inferior ()->arch ()); 3906 3907 if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT)) 3908 offset &= ((ULONGEST) 1 << addr_bit) - 1; 3909 3910 /* If /proc/pid/mem is writable, don't fallback to ptrace. If 3911 the write via /proc/pid/mem fails because the inferior execed 3912 (and we haven't seen the exec event yet), a subsequent ptrace 3913 poke would incorrectly write memory to the post-exec address 3914 space, while the core was trying to write to the pre-exec 3915 address space. */ 3916 if (proc_mem_file_is_writable ()) 3917 return linux_proc_xfer_memory_partial (inferior_ptid.pid (), readbuf, 3918 writebuf, offset, len, 3919 xfered_len); 3920 3921 /* Fallback to ptrace. This should only really trigger on old 3922 systems. See "Accessing inferior memory" at the top. 3923 3924 The target_xfer interface for memory access uses 3925 inferior_ptid as sideband argument to indicate which process 3926 to access. Memory access is process-wide, it is not 3927 thread-specific, so inferior_ptid sometimes points at a 3928 process ptid_t. If we fallback to inf_ptrace_target with 3929 that inferior_ptid, then the ptrace code will do the ptrace 3930 call targeting inferior_ptid.pid(), the leader LWP. That 3931 may fail with ESRCH if the leader is currently running, or 3932 zombie. So if we get a pid-ptid, we try to find a stopped 3933 LWP to use with ptrace. 3934 3935 Note that inferior_ptid may not exist in the lwp / thread / 3936 inferior lists. This can happen when we're removing 3937 breakpoints from a fork child that we're not going to stay 3938 attached to. So if we don't find a stopped LWP, still do the 3939 ptrace call, targeting the inferior_ptid we had on entry. */ 3940 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); 3941 lwp_info *stopped = find_stopped_lwp (inferior_ptid.pid ()); 3942 if (stopped != nullptr) 3943 inferior_ptid = stopped->ptid; 3944 return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf, 3945 offset, len, xfered_len); 3946 } 3947 3948 return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf, 3949 offset, len, xfered_len); 3950 } 3951 3952 bool 3953 linux_nat_target::thread_alive (ptid_t ptid) 3954 { 3955 /* As long as a PTID is in lwp list, consider it alive. */ 3956 return find_lwp_pid (ptid) != NULL; 3957 } 3958 3959 /* Implement the to_update_thread_list target method for this 3960 target. */ 3961 3962 void 3963 linux_nat_target::update_thread_list () 3964 { 3965 /* We add/delete threads from the list as clone/exit events are 3966 processed, so just try deleting exited threads still in the 3967 thread list. */ 3968 delete_exited_threads (); 3969 3970 /* Update the processor core that each lwp/thread was last seen 3971 running on. */ 3972 for (lwp_info *lwp : all_lwps ()) 3973 { 3974 /* Avoid accessing /proc if the thread hasn't run since we last 3975 time we fetched the thread's core. Accessing /proc becomes 3976 noticeably expensive when we have thousands of LWPs. */ 3977 if (lwp->core == -1) 3978 lwp->core = linux_common_core_of_thread (lwp->ptid); 3979 } 3980 } 3981 3982 std::string 3983 linux_nat_target::pid_to_str (ptid_t ptid) 3984 { 3985 if (ptid.lwp_p () 3986 && (ptid.pid () != ptid.lwp () 3987 || num_lwps (ptid.pid ()) > 1)) 3988 return string_printf ("LWP %ld", ptid.lwp ()); 3989 3990 return normal_pid_to_str (ptid); 3991 } 3992 3993 const char * 3994 linux_nat_target::thread_name (struct thread_info *thr) 3995 { 3996 return linux_proc_tid_get_name (thr->ptid); 3997 } 3998 3999 /* Accepts an integer PID; Returns a string representing a file that 4000 can be opened to get the symbols for the child process. */ 4001 4002 const char * 4003 linux_nat_target::pid_to_exec_file (int pid) 4004 { 4005 return linux_proc_pid_to_exec_file (pid); 4006 } 4007 4008 /* Object representing an /proc/PID/mem open file. We keep one such 4009 file open per inferior. 4010 4011 It might be tempting to think about only ever opening one file at 4012 most for all inferiors, closing/reopening the file as we access 4013 memory of different inferiors, to minimize number of file 4014 descriptors open, which can otherwise run into resource limits. 4015 However, that does not work correctly -- if the inferior execs and 4016 we haven't processed the exec event yet, and, we opened a 4017 /proc/PID/mem file, we will get a mem file accessing the post-exec 4018 address space, thinking we're opening it for the pre-exec address 4019 space. That is dangerous as we can poke memory (e.g. clearing 4020 breakpoints) in the post-exec memory by mistake, corrupting the 4021 inferior. For that reason, we open the mem file as early as 4022 possible, right after spawning, forking or attaching to the 4023 inferior, when the inferior is stopped and thus before it has a 4024 chance of execing. 4025 4026 Note that after opening the file, even if the thread we opened it 4027 for subsequently exits, the open file is still usable for accessing 4028 memory. It's only when the whole process exits or execs that the 4029 file becomes invalid, at which point reads/writes return EOF. */ 4030 4031 class proc_mem_file 4032 { 4033 public: 4034 proc_mem_file (ptid_t ptid, int fd) 4035 : m_ptid (ptid), m_fd (fd) 4036 { 4037 gdb_assert (m_fd != -1); 4038 } 4039 4040 ~proc_mem_file () 4041 { 4042 linux_nat_debug_printf ("closing fd %d for /proc/%d/task/%ld/mem", 4043 m_fd, m_ptid.pid (), m_ptid.lwp ()); 4044 close (m_fd); 4045 } 4046 4047 DISABLE_COPY_AND_ASSIGN (proc_mem_file); 4048 4049 int fd () 4050 { 4051 return m_fd; 4052 } 4053 4054 private: 4055 /* The LWP this file was opened for. Just for debugging 4056 purposes. */ 4057 ptid_t m_ptid; 4058 4059 /* The file descriptor. */ 4060 int m_fd = -1; 4061 }; 4062 4063 /* The map between an inferior process id, and the open /proc/PID/mem 4064 file. This is stored in a map instead of in a per-inferior 4065 structure because we need to be able to access memory of processes 4066 which don't have a corresponding struct inferior object. E.g., 4067 with "detach-on-fork on" (the default), and "follow-fork parent" 4068 (also default), we don't create an inferior for the fork child, but 4069 we still need to remove breakpoints from the fork child's 4070 memory. */ 4071 static std::unordered_map<int, proc_mem_file> proc_mem_file_map; 4072 4073 /* Close the /proc/PID/mem file for PID. */ 4074 4075 static void 4076 close_proc_mem_file (pid_t pid) 4077 { 4078 proc_mem_file_map.erase (pid); 4079 } 4080 4081 /* Open the /proc/PID/mem file for the process (thread group) of PTID. 4082 We actually open /proc/PID/task/LWP/mem, as that's the LWP we know 4083 exists and is stopped right now. We prefer the 4084 /proc/PID/task/LWP/mem form over /proc/LWP/mem to avoid tid-reuse 4085 races, just in case this is ever called on an already-waited 4086 LWP. */ 4087 4088 static void 4089 open_proc_mem_file (ptid_t ptid) 4090 { 4091 auto iter = proc_mem_file_map.find (ptid.pid ()); 4092 gdb_assert (iter == proc_mem_file_map.end ()); 4093 4094 char filename[64]; 4095 xsnprintf (filename, sizeof filename, 4096 "/proc/%d/task/%ld/mem", ptid.pid (), ptid.lwp ()); 4097 4098 int fd = gdb_open_cloexec (filename, O_RDWR | O_LARGEFILE, 0).release (); 4099 4100 if (fd == -1) 4101 { 4102 warning (_("opening /proc/PID/mem file for lwp %d.%ld failed: %s (%d)"), 4103 ptid.pid (), ptid.lwp (), 4104 safe_strerror (errno), errno); 4105 return; 4106 } 4107 4108 proc_mem_file_map.emplace (std::piecewise_construct, 4109 std::forward_as_tuple (ptid.pid ()), 4110 std::forward_as_tuple (ptid, fd)); 4111 4112 linux_nat_debug_printf ("opened fd %d for lwp %d.%ld", 4113 fd, ptid.pid (), ptid.lwp ()); 4114 } 4115 4116 /* Helper for linux_proc_xfer_memory_partial and 4117 proc_mem_file_is_writable. FD is the already opened /proc/pid/mem 4118 file, and PID is the pid of the corresponding process. The rest of 4119 the arguments are like linux_proc_xfer_memory_partial's. */ 4120 4121 static enum target_xfer_status 4122 linux_proc_xfer_memory_partial_fd (int fd, int pid, 4123 gdb_byte *readbuf, const gdb_byte *writebuf, 4124 ULONGEST offset, LONGEST len, 4125 ULONGEST *xfered_len) 4126 { 4127 ssize_t ret; 4128 4129 gdb_assert (fd != -1); 4130 4131 /* Use pread64/pwrite64 if available, since they save a syscall and 4132 can handle 64-bit offsets even on 32-bit platforms (for instance, 4133 SPARC debugging a SPARC64 application). But only use them if the 4134 offset isn't so high that when cast to off_t it'd be negative, as 4135 seen on SPARC64. pread64/pwrite64 outright reject such offsets. 4136 lseek does not. */ 4137 #ifdef HAVE_PREAD64 4138 if ((off_t) offset >= 0) 4139 ret = (readbuf != nullptr 4140 ? pread64 (fd, readbuf, len, offset) 4141 : pwrite64 (fd, writebuf, len, offset)); 4142 else 4143 #endif 4144 { 4145 ret = lseek (fd, offset, SEEK_SET); 4146 if (ret != -1) 4147 ret = (readbuf != nullptr 4148 ? read (fd, readbuf, len) 4149 : write (fd, writebuf, len)); 4150 } 4151 4152 if (ret == -1) 4153 { 4154 linux_nat_debug_printf ("accessing fd %d for pid %d failed: %s (%d)", 4155 fd, pid, safe_strerror (errno), errno); 4156 return TARGET_XFER_E_IO; 4157 } 4158 else if (ret == 0) 4159 { 4160 /* EOF means the address space is gone, the whole process exited 4161 or execed. */ 4162 linux_nat_debug_printf ("accessing fd %d for pid %d got EOF", 4163 fd, pid); 4164 return TARGET_XFER_EOF; 4165 } 4166 else 4167 { 4168 *xfered_len = ret; 4169 return TARGET_XFER_OK; 4170 } 4171 } 4172 4173 /* Implement the to_xfer_partial target method using /proc/PID/mem. 4174 Because we can use a single read/write call, this can be much more 4175 efficient than banging away at PTRACE_PEEKTEXT. Also, unlike 4176 PTRACE_PEEKTEXT/PTRACE_POKETEXT, this works with running 4177 threads. */ 4178 4179 static enum target_xfer_status 4180 linux_proc_xfer_memory_partial (int pid, gdb_byte *readbuf, 4181 const gdb_byte *writebuf, ULONGEST offset, 4182 LONGEST len, ULONGEST *xfered_len) 4183 { 4184 auto iter = proc_mem_file_map.find (pid); 4185 if (iter == proc_mem_file_map.end ()) 4186 return TARGET_XFER_EOF; 4187 4188 int fd = iter->second.fd (); 4189 4190 return linux_proc_xfer_memory_partial_fd (fd, pid, readbuf, writebuf, offset, 4191 len, xfered_len); 4192 } 4193 4194 /* Check whether /proc/pid/mem is writable in the current kernel, and 4195 return true if so. It wasn't writable before Linux 2.6.39, but 4196 there's no way to know whether the feature was backported to older 4197 kernels. So we check to see if it works. The result is cached, 4198 and this is guaranteed to be called once early during inferior 4199 startup, so that any warning is printed out consistently between 4200 GDB invocations. Note we don't call it during GDB startup instead 4201 though, because then we might warn with e.g. just "gdb --version" 4202 on sandboxed systems. See PR gdb/29907. */ 4203 4204 static bool 4205 proc_mem_file_is_writable () 4206 { 4207 static std::optional<bool> writable; 4208 4209 if (writable.has_value ()) 4210 return *writable; 4211 4212 writable.emplace (false); 4213 4214 /* We check whether /proc/pid/mem is writable by trying to write to 4215 one of our variables via /proc/self/mem. */ 4216 4217 int fd = gdb_open_cloexec ("/proc/self/mem", O_RDWR | O_LARGEFILE, 0).release (); 4218 4219 if (fd == -1) 4220 { 4221 warning (_("opening /proc/self/mem file failed: %s (%d)"), 4222 safe_strerror (errno), errno); 4223 return *writable; 4224 } 4225 4226 SCOPE_EXIT { close (fd); }; 4227 4228 /* This is the variable we try to write to. Note OFFSET below. */ 4229 volatile gdb_byte test_var = 0; 4230 4231 gdb_byte writebuf[] = {0x55}; 4232 ULONGEST offset = (uintptr_t) &test_var; 4233 ULONGEST xfered_len; 4234 4235 enum target_xfer_status res 4236 = linux_proc_xfer_memory_partial_fd (fd, getpid (), nullptr, writebuf, 4237 offset, 1, &xfered_len); 4238 4239 if (res == TARGET_XFER_OK) 4240 { 4241 gdb_assert (xfered_len == 1); 4242 gdb_assert (test_var == 0x55); 4243 /* Success. */ 4244 *writable = true; 4245 } 4246 4247 return *writable; 4248 } 4249 4250 /* Parse LINE as a signal set and add its set bits to SIGS. */ 4251 4252 static void 4253 add_line_to_sigset (const char *line, sigset_t *sigs) 4254 { 4255 int len = strlen (line) - 1; 4256 const char *p; 4257 int signum; 4258 4259 if (line[len] != '\n') 4260 error (_("Could not parse signal set: %s"), line); 4261 4262 p = line; 4263 signum = len * 4; 4264 while (len-- > 0) 4265 { 4266 int digit; 4267 4268 if (*p >= '0' && *p <= '9') 4269 digit = *p - '0'; 4270 else if (*p >= 'a' && *p <= 'f') 4271 digit = *p - 'a' + 10; 4272 else 4273 error (_("Could not parse signal set: %s"), line); 4274 4275 signum -= 4; 4276 4277 if (digit & 1) 4278 sigaddset (sigs, signum + 1); 4279 if (digit & 2) 4280 sigaddset (sigs, signum + 2); 4281 if (digit & 4) 4282 sigaddset (sigs, signum + 3); 4283 if (digit & 8) 4284 sigaddset (sigs, signum + 4); 4285 4286 p++; 4287 } 4288 } 4289 4290 /* Find process PID's pending signals from /proc/pid/status and set 4291 SIGS to match. */ 4292 4293 void 4294 linux_proc_pending_signals (int pid, sigset_t *pending, 4295 sigset_t *blocked, sigset_t *ignored) 4296 { 4297 char buffer[PATH_MAX], fname[PATH_MAX]; 4298 4299 sigemptyset (pending); 4300 sigemptyset (blocked); 4301 sigemptyset (ignored); 4302 xsnprintf (fname, sizeof fname, "/proc/%d/status", pid); 4303 gdb_file_up procfile = gdb_fopen_cloexec (fname, "r"); 4304 if (procfile == NULL) 4305 error (_("Could not open %s"), fname); 4306 4307 while (fgets (buffer, PATH_MAX, procfile.get ()) != NULL) 4308 { 4309 /* Normal queued signals are on the SigPnd line in the status 4310 file. However, 2.6 kernels also have a "shared" pending 4311 queue for delivering signals to a thread group, so check for 4312 a ShdPnd line also. 4313 4314 Unfortunately some Red Hat kernels include the shared pending 4315 queue but not the ShdPnd status field. */ 4316 4317 if (startswith (buffer, "SigPnd:\t")) 4318 add_line_to_sigset (buffer + 8, pending); 4319 else if (startswith (buffer, "ShdPnd:\t")) 4320 add_line_to_sigset (buffer + 8, pending); 4321 else if (startswith (buffer, "SigBlk:\t")) 4322 add_line_to_sigset (buffer + 8, blocked); 4323 else if (startswith (buffer, "SigIgn:\t")) 4324 add_line_to_sigset (buffer + 8, ignored); 4325 } 4326 } 4327 4328 static enum target_xfer_status 4329 linux_nat_xfer_osdata (enum target_object object, 4330 const char *annex, gdb_byte *readbuf, 4331 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, 4332 ULONGEST *xfered_len) 4333 { 4334 gdb_assert (object == TARGET_OBJECT_OSDATA); 4335 4336 *xfered_len = linux_common_xfer_osdata (annex, readbuf, offset, len); 4337 if (*xfered_len == 0) 4338 return TARGET_XFER_EOF; 4339 else 4340 return TARGET_XFER_OK; 4341 } 4342 4343 std::vector<static_tracepoint_marker> 4344 linux_nat_target::static_tracepoint_markers_by_strid (const char *strid) 4345 { 4346 char s[IPA_CMD_BUF_SIZE]; 4347 int pid = inferior_ptid.pid (); 4348 std::vector<static_tracepoint_marker> markers; 4349 const char *p = s; 4350 ptid_t ptid = ptid_t (pid, 0); 4351 static_tracepoint_marker marker; 4352 4353 /* Pause all */ 4354 target_stop (ptid); 4355 4356 strcpy (s, "qTfSTM"); 4357 agent_run_command (pid, s, strlen (s) + 1); 4358 4359 /* Unpause all. */ 4360 SCOPE_EXIT { target_continue_no_signal (ptid); }; 4361 4362 while (*p++ == 'm') 4363 { 4364 do 4365 { 4366 parse_static_tracepoint_marker_definition (p, &p, &marker); 4367 4368 if (strid == NULL || marker.str_id == strid) 4369 markers.push_back (std::move (marker)); 4370 } 4371 while (*p++ == ','); /* comma-separated list */ 4372 4373 strcpy (s, "qTsSTM"); 4374 agent_run_command (pid, s, strlen (s) + 1); 4375 p = s; 4376 } 4377 4378 return markers; 4379 } 4380 4381 /* target_can_async_p implementation. */ 4382 4383 bool 4384 linux_nat_target::can_async_p () 4385 { 4386 /* This flag should be checked in the common target.c code. */ 4387 gdb_assert (target_async_permitted); 4388 4389 /* Otherwise, this targets is always able to support async mode. */ 4390 return true; 4391 } 4392 4393 bool 4394 linux_nat_target::supports_non_stop () 4395 { 4396 return true; 4397 } 4398 4399 /* to_always_non_stop_p implementation. */ 4400 4401 bool 4402 linux_nat_target::always_non_stop_p () 4403 { 4404 return true; 4405 } 4406 4407 bool 4408 linux_nat_target::supports_multi_process () 4409 { 4410 return true; 4411 } 4412 4413 bool 4414 linux_nat_target::supports_disable_randomization () 4415 { 4416 return true; 4417 } 4418 4419 /* SIGCHLD handler that serves two purposes: In non-stop/async mode, 4420 so we notice when any child changes state, and notify the 4421 event-loop; it allows us to use sigsuspend in linux_nat_wait_1 4422 above to wait for the arrival of a SIGCHLD. */ 4423 4424 static void 4425 sigchld_handler (int signo) 4426 { 4427 int old_errno = errno; 4428 4429 if (debug_linux_nat) 4430 gdb_stdlog->write_async_safe ("sigchld\n", sizeof ("sigchld\n") - 1); 4431 4432 if (signo == SIGCHLD) 4433 { 4434 /* Let the event loop know that there are events to handle. */ 4435 linux_nat_target::async_file_mark_if_open (); 4436 } 4437 4438 errno = old_errno; 4439 } 4440 4441 /* Callback registered with the target events file descriptor. */ 4442 4443 static void 4444 handle_target_event (int error, gdb_client_data client_data) 4445 { 4446 inferior_event_handler (INF_REG_EVENT); 4447 } 4448 4449 /* target_async implementation. */ 4450 4451 void 4452 linux_nat_target::async (bool enable) 4453 { 4454 if (enable == is_async_p ()) 4455 return; 4456 4457 /* Block child signals while we create/destroy the pipe, as their 4458 handler writes to it. */ 4459 gdb::block_signals blocker; 4460 4461 if (enable) 4462 { 4463 if (!async_file_open ()) 4464 internal_error ("creating event pipe failed."); 4465 4466 add_file_handler (async_wait_fd (), handle_target_event, NULL, 4467 "linux-nat"); 4468 4469 /* There may be pending events to handle. Tell the event loop 4470 to poll them. */ 4471 async_file_mark (); 4472 } 4473 else 4474 { 4475 delete_file_handler (async_wait_fd ()); 4476 async_file_close (); 4477 } 4478 } 4479 4480 /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other 4481 event came out. */ 4482 4483 static int 4484 linux_nat_stop_lwp (struct lwp_info *lwp) 4485 { 4486 if (!lwp->stopped) 4487 { 4488 linux_nat_debug_printf ("running -> suspending %s", 4489 lwp->ptid.to_string ().c_str ()); 4490 4491 4492 if (lwp->last_resume_kind == resume_stop) 4493 { 4494 linux_nat_debug_printf ("already stopping LWP %ld at GDB's request", 4495 lwp->ptid.lwp ()); 4496 return 0; 4497 } 4498 4499 stop_callback (lwp); 4500 lwp->last_resume_kind = resume_stop; 4501 } 4502 else 4503 { 4504 /* Already known to be stopped; do nothing. */ 4505 4506 if (debug_linux_nat) 4507 { 4508 if (linux_target->find_thread (lwp->ptid)->stop_requested) 4509 linux_nat_debug_printf ("already stopped/stop_requested %s", 4510 lwp->ptid.to_string ().c_str ()); 4511 else 4512 linux_nat_debug_printf ("already stopped/no stop_requested yet %s", 4513 lwp->ptid.to_string ().c_str ()); 4514 } 4515 } 4516 return 0; 4517 } 4518 4519 void 4520 linux_nat_target::stop (ptid_t ptid) 4521 { 4522 LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 4523 iterate_over_lwps (ptid, linux_nat_stop_lwp); 4524 } 4525 4526 /* Return the cached value of the processor core for thread PTID. */ 4527 4528 int 4529 linux_nat_target::core_of_thread (ptid_t ptid) 4530 { 4531 struct lwp_info *info = find_lwp_pid (ptid); 4532 4533 if (info) 4534 return info->core; 4535 return -1; 4536 } 4537 4538 /* Implementation of to_filesystem_is_local. */ 4539 4540 bool 4541 linux_nat_target::filesystem_is_local () 4542 { 4543 struct inferior *inf = current_inferior (); 4544 4545 if (inf->fake_pid_p || inf->pid == 0) 4546 return true; 4547 4548 return linux_ns_same (inf->pid, LINUX_NS_MNT); 4549 } 4550 4551 /* Convert the INF argument passed to a to_fileio_* method 4552 to a process ID suitable for passing to its corresponding 4553 linux_mntns_* function. If INF is non-NULL then the 4554 caller is requesting the filesystem seen by INF. If INF 4555 is NULL then the caller is requesting the filesystem seen 4556 by the GDB. We fall back to GDB's filesystem in the case 4557 that INF is non-NULL but its PID is unknown. */ 4558 4559 static pid_t 4560 linux_nat_fileio_pid_of (struct inferior *inf) 4561 { 4562 if (inf == NULL || inf->fake_pid_p || inf->pid == 0) 4563 return getpid (); 4564 else 4565 return inf->pid; 4566 } 4567 4568 /* Implementation of to_fileio_open. */ 4569 4570 int 4571 linux_nat_target::fileio_open (struct inferior *inf, const char *filename, 4572 int flags, int mode, int warn_if_slow, 4573 fileio_error *target_errno) 4574 { 4575 int nat_flags; 4576 mode_t nat_mode; 4577 int fd; 4578 4579 if (fileio_to_host_openflags (flags, &nat_flags) == -1 4580 || fileio_to_host_mode (mode, &nat_mode) == -1) 4581 { 4582 *target_errno = FILEIO_EINVAL; 4583 return -1; 4584 } 4585 4586 fd = linux_mntns_open_cloexec (linux_nat_fileio_pid_of (inf), 4587 filename, nat_flags, nat_mode); 4588 if (fd == -1) 4589 *target_errno = host_to_fileio_error (errno); 4590 4591 return fd; 4592 } 4593 4594 /* Implementation of to_fileio_readlink. */ 4595 4596 std::optional<std::string> 4597 linux_nat_target::fileio_readlink (struct inferior *inf, const char *filename, 4598 fileio_error *target_errno) 4599 { 4600 char buf[PATH_MAX]; 4601 int len; 4602 4603 len = linux_mntns_readlink (linux_nat_fileio_pid_of (inf), 4604 filename, buf, sizeof (buf)); 4605 if (len < 0) 4606 { 4607 *target_errno = host_to_fileio_error (errno); 4608 return {}; 4609 } 4610 4611 return std::string (buf, len); 4612 } 4613 4614 /* Implementation of to_fileio_unlink. */ 4615 4616 int 4617 linux_nat_target::fileio_unlink (struct inferior *inf, const char *filename, 4618 fileio_error *target_errno) 4619 { 4620 int ret; 4621 4622 ret = linux_mntns_unlink (linux_nat_fileio_pid_of (inf), 4623 filename); 4624 if (ret == -1) 4625 *target_errno = host_to_fileio_error (errno); 4626 4627 return ret; 4628 } 4629 4630 /* Implementation of the to_thread_events method. */ 4631 4632 void 4633 linux_nat_target::thread_events (bool enable) 4634 { 4635 report_thread_events = enable; 4636 } 4637 4638 bool 4639 linux_nat_target::supports_set_thread_options (gdb_thread_options options) 4640 { 4641 constexpr gdb_thread_options supported_options 4642 = GDB_THREAD_OPTION_CLONE | GDB_THREAD_OPTION_EXIT; 4643 return ((options & supported_options) == options); 4644 } 4645 4646 linux_nat_target::linux_nat_target () 4647 { 4648 /* We don't change the stratum; this target will sit at 4649 process_stratum and thread_db will set at thread_stratum. This 4650 is a little strange, since this is a multi-threaded-capable 4651 target, but we want to be on the stack below thread_db, and we 4652 also want to be used for single-threaded processes. */ 4653 } 4654 4655 /* See linux-nat.h. */ 4656 4657 bool 4658 linux_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo) 4659 { 4660 int pid = get_ptrace_pid (ptid); 4661 return ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, siginfo) == 0; 4662 } 4663 4664 /* See nat/linux-nat.h. */ 4665 4666 ptid_t 4667 current_lwp_ptid (void) 4668 { 4669 gdb_assert (inferior_ptid.lwp_p ()); 4670 return inferior_ptid; 4671 } 4672 4673 /* Implement 'maintenance info linux-lwps'. Displays some basic 4674 information about all the current lwp_info objects. */ 4675 4676 static void 4677 maintenance_info_lwps (const char *arg, int from_tty) 4678 { 4679 if (all_lwps ().size () == 0) 4680 { 4681 gdb_printf ("No Linux LWPs\n"); 4682 return; 4683 } 4684 4685 /* Start the width at 8 to match the column heading below, then 4686 figure out the widest ptid string. We'll use this to build our 4687 output table below. */ 4688 size_t ptid_width = 8; 4689 for (lwp_info *lp : all_lwps ()) 4690 ptid_width = std::max (ptid_width, lp->ptid.to_string ().size ()); 4691 4692 /* Setup the table headers. */ 4693 struct ui_out *uiout = current_uiout; 4694 ui_out_emit_table table_emitter (uiout, 2, -1, "linux-lwps"); 4695 uiout->table_header (ptid_width, ui_left, "lwp-ptid", _("LWP Ptid")); 4696 uiout->table_header (9, ui_left, "thread-info", _("Thread ID")); 4697 uiout->table_body (); 4698 4699 /* Display one table row for each lwp_info. */ 4700 for (lwp_info *lp : all_lwps ()) 4701 { 4702 ui_out_emit_tuple tuple_emitter (uiout, "lwp-entry"); 4703 4704 thread_info *th = linux_target->find_thread (lp->ptid); 4705 4706 uiout->field_string ("lwp-ptid", lp->ptid.to_string ().c_str ()); 4707 if (th == nullptr) 4708 uiout->field_string ("thread-info", "None"); 4709 else 4710 uiout->field_string ("thread-info", print_full_thread_id (th)); 4711 4712 uiout->message ("\n"); 4713 } 4714 } 4715 4716 void _initialize_linux_nat (); 4717 void 4718 _initialize_linux_nat () 4719 { 4720 add_setshow_boolean_cmd ("linux-nat", class_maintenance, 4721 &debug_linux_nat, _("\ 4722 Set debugging of GNU/Linux native target."), _("\ 4723 Show debugging of GNU/Linux native target."), _("\ 4724 When on, print debug messages relating to the GNU/Linux native target."), 4725 nullptr, 4726 show_debug_linux_nat, 4727 &setdebuglist, &showdebuglist); 4728 4729 add_setshow_boolean_cmd ("linux-namespaces", class_maintenance, 4730 &debug_linux_namespaces, _("\ 4731 Set debugging of GNU/Linux namespaces module."), _("\ 4732 Show debugging of GNU/Linux namespaces module."), _("\ 4733 Enables printf debugging output."), 4734 NULL, 4735 NULL, 4736 &setdebuglist, &showdebuglist); 4737 4738 /* Install a SIGCHLD handler. */ 4739 sigchld_action.sa_handler = sigchld_handler; 4740 sigemptyset (&sigchld_action.sa_mask); 4741 sigchld_action.sa_flags = SA_RESTART; 4742 4743 /* Make it the default. */ 4744 sigaction (SIGCHLD, &sigchld_action, NULL); 4745 4746 /* Make sure we don't block SIGCHLD during a sigsuspend. */ 4747 gdb_sigmask (SIG_SETMASK, NULL, &suspend_mask); 4748 sigdelset (&suspend_mask, SIGCHLD); 4749 4750 sigemptyset (&blocked_mask); 4751 4752 lwp_lwpid_htab_create (); 4753 4754 add_cmd ("linux-lwps", class_maintenance, maintenance_info_lwps, 4755 _("List the Linux LWPS."), &maintenanceinfolist); 4756 } 4757 4758 4760 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to 4761 the GNU/Linux Threads library and therefore doesn't really belong 4762 here. */ 4763 4764 /* NPTL reserves the first two RT signals, but does not provide any 4765 way for the debugger to query the signal numbers - fortunately 4766 they don't change. */ 4767 static int lin_thread_signals[] = { __SIGRTMIN, __SIGRTMIN + 1 }; 4768 4769 /* See linux-nat.h. */ 4770 4771 unsigned int 4772 lin_thread_get_thread_signal_num (void) 4773 { 4774 return sizeof (lin_thread_signals) / sizeof (lin_thread_signals[0]); 4775 } 4776 4777 /* See linux-nat.h. */ 4778 4779 int 4780 lin_thread_get_thread_signal (unsigned int i) 4781 { 4782 gdb_assert (i < lin_thread_get_thread_signal_num ()); 4783 return lin_thread_signals[i]; 4784 } 4785