1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
24 #include "nat/linux-nat.h"
25 #include "nat/linux-waitpid.h"
27 #ifdef HAVE_TKILL_SYSCALL
29 #include <sys/syscall.h>
31 #include <sys/ptrace.h>
32 #include "linux-nat.h"
33 #include "nat/linux-ptrace.h"
34 #include "nat/linux-procfs.h"
35 #include "nat/linux-personality.h"
36 #include "linux-fork.h"
37 #include "gdbthread.h"
41 #include "inf-child.h"
42 #include "inf-ptrace.h"
44 #include <sys/procfs.h> /* for elf_gregset etc. */
45 #include "elf-bfd.h" /* for elfcore_write_* */
46 #include "gregset.h" /* for gregset */
47 #include "gdbcore.h" /* for get_exec_file */
48 #include <ctype.h> /* for isdigit */
49 #include <sys/stat.h> /* for struct stat */
50 #include <fcntl.h> /* for O_RDONLY */
52 #include "event-loop.h"
53 #include "event-top.h"
55 #include <sys/types.h>
57 #include "xml-support.h"
60 #include "nat/linux-osdata.h"
61 #include "linux-tdep.h"
64 #include "tracepoint.h"
66 #include "target-descriptions.h"
67 #include "filestuff.h"
71 #define SPUFS_MAGIC 0x23c9b64e
74 /* This comment documents high-level logic of this file.
76 Waiting for events in sync mode
77 ===============================
79 When waiting for an event in a specific thread, we just use waitpid, passing
80 the specific pid, and not passing WNOHANG.
82 When waiting for an event in all threads, waitpid is not quite good. Prior to
83 version 2.4, Linux can either wait for event in main thread, or in secondary
84 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
85 miss an event. The solution is to use non-blocking waitpid, together with
86 sigsuspend. First, we use non-blocking waitpid to get an event in the main
87 process, if any. Second, we use non-blocking waitpid with the __WCLONED
88 flag to check for events in cloned processes. If nothing is found, we use
89 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
90 happened to a child process -- and SIGCHLD will be delivered both for events
91 in main debugged process and in cloned processes. As soon as we know there's
92 an event, we get back to calling nonblocking waitpid with and without
95 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
96 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
97 blocked, the signal becomes pending and sigsuspend immediately
98 notices it and returns.
100 Waiting for events in async mode
101 ================================
103 In async mode, GDB should always be ready to handle both user input
104 and target events, so neither blocking waitpid nor sigsuspend are
105 viable options. Instead, we should asynchronously notify the GDB main
106 event loop whenever there's an unprocessed event from the target. We
107 detect asynchronous target events by handling SIGCHLD signals. To
108 notify the event loop about target events, the self-pipe trick is used
109 --- a pipe is registered as waitable event source in the event loop,
110 the event loop select/poll's on the read end of this pipe (as well on
111 other event sources, e.g., stdin), and the SIGCHLD handler writes a
112 byte to this pipe. This is more portable than relying on
113 pselect/ppoll, since on kernels that lack those syscalls, libc
114 emulates them with select/poll+sigprocmask, and that is racy
115 (a.k.a. plain broken).
117 Obviously, if we fail to notify the event loop if there's a target
118 event, it's bad. OTOH, if we notify the event loop when there's no
119 event from the target, linux_nat_wait will detect that there's no real
120 event to report, and return event of type TARGET_WAITKIND_IGNORE.
121 This is mostly harmless, but it will waste time and is better avoided.
123 The main design point is that every time GDB is outside linux-nat.c,
124 we have a SIGCHLD handler installed that is called when something
125 happens to the target and notifies the GDB event loop. Whenever GDB
126 core decides to handle the event, and calls into linux-nat.c, we
127 process things as in sync mode, except that the we never block in
130 While processing an event, we may end up momentarily blocked in
131 waitpid calls. Those waitpid calls, while blocking, are guarantied to
132 return quickly. E.g., in all-stop mode, before reporting to the core
133 that an LWP hit a breakpoint, all LWPs are stopped by sending them
134 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
135 Note that this is different from blocking indefinitely waiting for the
136 next event --- here, we're already handling an event.
141 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
142 signal is not entirely significant; we just need for a signal to be delivered,
143 so that we can intercept it. SIGSTOP's advantage is that it can not be
144 blocked. A disadvantage is that it is not a real-time signal, so it can only
145 be queued once; we do not keep track of other sources of SIGSTOP.
147 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
148 use them, because they have special behavior when the signal is generated -
149 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
150 kills the entire thread group.
152 A delivered SIGSTOP would stop the entire thread group, not just the thread we
153 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
154 cancel it (by PTRACE_CONT without passing SIGSTOP).
156 We could use a real-time signal instead. This would solve those problems; we
157 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
158 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
159 generates it, and there are races with trying to find a signal that is not
163 #define O_LARGEFILE 0
166 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
167 the use of the multi-threaded target. */
168 static struct target_ops *linux_ops;
169 static struct target_ops linux_ops_saved;
171 /* The method to call, if any, when a new thread is attached. */
172 static void (*linux_nat_new_thread) (struct lwp_info *);
174 /* The method to call, if any, when a new fork is attached. */
175 static linux_nat_new_fork_ftype *linux_nat_new_fork;
177 /* The method to call, if any, when a process is no longer
179 static linux_nat_forget_process_ftype *linux_nat_forget_process_hook;
181 /* Hook to call prior to resuming a thread. */
182 static void (*linux_nat_prepare_to_resume) (struct lwp_info *);
184 /* The method to call, if any, when the siginfo object needs to be
185 converted between the layout returned by ptrace, and the layout in
186 the architecture of the inferior. */
187 static int (*linux_nat_siginfo_fixup) (siginfo_t *,
191 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
192 Called by our to_xfer_partial. */
193 static target_xfer_partial_ftype *super_xfer_partial;
195 /* The saved to_close method, inherited from inf-ptrace.c.
196 Called by our to_close. */
197 static void (*super_close) (struct target_ops *);
199 static unsigned int debug_linux_nat;
201 show_debug_linux_nat (struct ui_file *file, int from_tty,
202 struct cmd_list_element *c, const char *value)
204 fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
208 struct simple_pid_list
212 struct simple_pid_list *next;
214 struct simple_pid_list *stopped_pids;
216 /* Async mode support. */
218 /* The read/write ends of the pipe registered as waitable file in the
220 static int linux_nat_event_pipe[2] = { -1, -1 };
222 /* True if we're currently in async mode. */
223 #define linux_is_async_p() (linux_nat_event_pipe[0] != -1)
225 /* Flush the event pipe. */
228 async_file_flush (void)
235 ret = read (linux_nat_event_pipe[0], &buf, 1);
237 while (ret >= 0 || (ret == -1 && errno == EINTR));
240 /* Put something (anything, doesn't matter what, or how much) in event
241 pipe, so that the select/poll in the event-loop realizes we have
242 something to process. */
245 async_file_mark (void)
249 /* It doesn't really matter what the pipe contains, as long we end
250 up with something in it. Might as well flush the previous
256 ret = write (linux_nat_event_pipe[1], "+", 1);
258 while (ret == -1 && errno == EINTR);
260 /* Ignore EAGAIN. If the pipe is full, the event loop will already
261 be awakened anyway. */
264 static int kill_lwp (int lwpid, int signo);
266 static int stop_callback (struct lwp_info *lp, void *data);
267 static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
269 static void block_child_signals (sigset_t *prev_mask);
270 static void restore_child_signals_mask (sigset_t *prev_mask);
273 static struct lwp_info *add_lwp (ptid_t ptid);
274 static void purge_lwp_list (int pid);
275 static void delete_lwp (ptid_t ptid);
276 static struct lwp_info *find_lwp_pid (ptid_t ptid);
278 static int lwp_status_pending_p (struct lwp_info *lp);
280 static int check_stopped_by_breakpoint (struct lwp_info *lp);
281 static int sigtrap_is_event (int status);
282 static int (*linux_nat_status_is_event) (int status) = sigtrap_is_event;
287 /* See nat/linux-nat.h. */
290 ptid_of_lwp (struct lwp_info *lwp)
295 /* See nat/linux-nat.h. */
298 lwp_set_arch_private_info (struct lwp_info *lwp,
299 struct arch_lwp_info *info)
301 lwp->arch_private = info;
304 /* See nat/linux-nat.h. */
306 struct arch_lwp_info *
307 lwp_arch_private_info (struct lwp_info *lwp)
309 return lwp->arch_private;
312 /* See nat/linux-nat.h. */
315 lwp_is_stopped (struct lwp_info *lwp)
320 /* See nat/linux-nat.h. */
322 enum target_stop_reason
323 lwp_stop_reason (struct lwp_info *lwp)
325 return lwp->stop_reason;
329 /* Trivial list manipulation functions to keep track of a list of
330 new stopped processes. */
332 add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
334 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
337 new_pid->status = status;
338 new_pid->next = *listp;
343 in_pid_list_p (struct simple_pid_list *list, int pid)
345 struct simple_pid_list *p;
347 for (p = list; p != NULL; p = p->next)
354 pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
356 struct simple_pid_list **p;
358 for (p = listp; *p != NULL; p = &(*p)->next)
359 if ((*p)->pid == pid)
361 struct simple_pid_list *next = (*p)->next;
363 *statusp = (*p)->status;
371 /* Return the ptrace options that we want to try to enable. */
374 linux_nat_ptrace_options (int attached)
379 options |= PTRACE_O_EXITKILL;
381 options |= (PTRACE_O_TRACESYSGOOD
382 | PTRACE_O_TRACEVFORKDONE
383 | PTRACE_O_TRACEVFORK
385 | PTRACE_O_TRACEEXEC);
390 /* Initialize ptrace warnings and check for supported ptrace
393 ATTACHED should be nonzero iff we attached to the inferior. */
396 linux_init_ptrace (pid_t pid, int attached)
398 int options = linux_nat_ptrace_options (attached);
400 linux_enable_event_reporting (pid, options);
401 linux_ptrace_init_warnings ();
405 linux_child_post_attach (struct target_ops *self, int pid)
407 linux_init_ptrace (pid, 1);
411 linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
413 linux_init_ptrace (ptid_get_pid (ptid), 0);
416 /* Return the number of known LWPs in the tgid given by PID. */
424 for (lp = lwp_list; lp; lp = lp->next)
425 if (ptid_get_pid (lp->ptid) == pid)
431 /* Call delete_lwp with prototype compatible for make_cleanup. */
434 delete_lwp_cleanup (void *lp_voidp)
436 struct lwp_info *lp = lp_voidp;
438 delete_lwp (lp->ptid);
441 /* Target hook for follow_fork. On entry inferior_ptid must be the
442 ptid of the followed inferior. At return, inferior_ptid will be
446 linux_child_follow_fork (struct target_ops *ops, int follow_child,
451 struct lwp_info *child_lp = NULL;
452 int status = W_STOPCODE (0);
453 struct cleanup *old_chain;
455 ptid_t parent_ptid, child_ptid;
456 int parent_pid, child_pid;
458 has_vforked = (inferior_thread ()->pending_follow.kind
459 == TARGET_WAITKIND_VFORKED);
460 parent_ptid = inferior_ptid;
461 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
462 parent_pid = ptid_get_lwp (parent_ptid);
463 child_pid = ptid_get_lwp (child_ptid);
465 /* We're already attached to the parent, by default. */
466 old_chain = save_inferior_ptid ();
467 inferior_ptid = child_ptid;
468 child_lp = add_lwp (inferior_ptid);
469 child_lp->stopped = 1;
470 child_lp->last_resume_kind = resume_stop;
472 /* Detach new forked process? */
475 make_cleanup (delete_lwp_cleanup, child_lp);
477 if (linux_nat_prepare_to_resume != NULL)
478 linux_nat_prepare_to_resume (child_lp);
480 /* When debugging an inferior in an architecture that supports
481 hardware single stepping on a kernel without commit
482 6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child
483 process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits
484 set if the parent process had them set.
485 To work around this, single step the child process
486 once before detaching to clear the flags. */
488 if (!gdbarch_software_single_step_p (target_thread_architecture
491 linux_disable_event_reporting (child_pid);
492 if (ptrace (PTRACE_SINGLESTEP, child_pid, 0, 0) < 0)
493 perror_with_name (_("Couldn't do single step"));
494 if (my_waitpid (child_pid, &status, 0) < 0)
495 perror_with_name (_("Couldn't wait vfork process"));
498 if (WIFSTOPPED (status))
502 signo = WSTOPSIG (status);
504 && !signal_pass_state (gdb_signal_from_host (signo)))
506 ptrace (PTRACE_DETACH, child_pid, 0, signo);
509 /* Resets value of inferior_ptid to parent ptid. */
510 do_cleanups (old_chain);
514 /* Let the thread_db layer learn about this new process. */
515 check_for_thread_db ();
518 do_cleanups (old_chain);
522 struct lwp_info *parent_lp;
524 parent_lp = find_lwp_pid (parent_ptid);
525 gdb_assert (linux_supports_tracefork () >= 0);
527 if (linux_supports_tracevforkdone ())
530 fprintf_unfiltered (gdb_stdlog,
531 "LCFF: waiting for VFORK_DONE on %d\n",
533 parent_lp->stopped = 1;
535 /* We'll handle the VFORK_DONE event like any other
536 event, in target_wait. */
540 /* We can't insert breakpoints until the child has
541 finished with the shared memory region. We need to
542 wait until that happens. Ideal would be to just
544 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
545 - waitpid (parent_pid, &status, __WALL);
546 However, most architectures can't handle a syscall
547 being traced on the way out if it wasn't traced on
550 We might also think to loop, continuing the child
551 until it exits or gets a SIGTRAP. One problem is
552 that the child might call ptrace with PTRACE_TRACEME.
554 There's no simple and reliable way to figure out when
555 the vforked child will be done with its copy of the
556 shared memory. We could step it out of the syscall,
557 two instructions, let it go, and then single-step the
558 parent once. When we have hardware single-step, this
559 would work; with software single-step it could still
560 be made to work but we'd have to be able to insert
561 single-step breakpoints in the child, and we'd have
562 to insert -just- the single-step breakpoint in the
563 parent. Very awkward.
565 In the end, the best we can do is to make sure it
566 runs for a little while. Hopefully it will be out of
567 range of any breakpoints we reinsert. Usually this
568 is only the single-step breakpoint at vfork's return
572 fprintf_unfiltered (gdb_stdlog,
573 "LCFF: no VFORK_DONE "
574 "support, sleeping a bit\n");
578 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
579 and leave it pending. The next linux_nat_resume call
580 will notice a pending event, and bypasses actually
581 resuming the inferior. */
582 parent_lp->status = 0;
583 parent_lp->waitstatus.kind = TARGET_WAITKIND_VFORK_DONE;
584 parent_lp->stopped = 1;
586 /* If we're in async mode, need to tell the event loop
587 there's something here to process. */
588 if (target_is_async_p ())
595 struct lwp_info *child_lp;
597 child_lp = add_lwp (inferior_ptid);
598 child_lp->stopped = 1;
599 child_lp->last_resume_kind = resume_stop;
601 /* Let the thread_db layer learn about this new process. */
602 check_for_thread_db ();
610 linux_child_insert_fork_catchpoint (struct target_ops *self, int pid)
612 return !linux_supports_tracefork ();
616 linux_child_remove_fork_catchpoint (struct target_ops *self, int pid)
622 linux_child_insert_vfork_catchpoint (struct target_ops *self, int pid)
624 return !linux_supports_tracefork ();
628 linux_child_remove_vfork_catchpoint (struct target_ops *self, int pid)
634 linux_child_insert_exec_catchpoint (struct target_ops *self, int pid)
636 return !linux_supports_tracefork ();
640 linux_child_remove_exec_catchpoint (struct target_ops *self, int pid)
646 linux_child_set_syscall_catchpoint (struct target_ops *self,
647 int pid, int needed, int any_count,
648 int table_size, int *table)
650 if (!linux_supports_tracesysgood ())
653 /* On GNU/Linux, we ignore the arguments. It means that we only
654 enable the syscall catchpoints, but do not disable them.
656 Also, we do not use the `table' information because we do not
657 filter system calls here. We let GDB do the logic for us. */
661 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
662 are processes sharing the same VM space. A multi-threaded process
663 is basically a group of such processes. However, such a grouping
664 is almost entirely a user-space issue; the kernel doesn't enforce
665 such a grouping at all (this might change in the future). In
666 general, we'll rely on the threads library (i.e. the GNU/Linux
667 Threads library) to provide such a grouping.
669 It is perfectly well possible to write a multi-threaded application
670 without the assistance of a threads library, by using the clone
671 system call directly. This module should be able to give some
672 rudimentary support for debugging such applications if developers
673 specify the CLONE_PTRACE flag in the clone system call, and are
674 using the Linux kernel 2.4 or above.
676 Note that there are some peculiarities in GNU/Linux that affect
679 - In general one should specify the __WCLONE flag to waitpid in
680 order to make it report events for any of the cloned processes
681 (and leave it out for the initial process). However, if a cloned
682 process has exited the exit status is only reported if the
683 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
684 we cannot use it since GDB must work on older systems too.
686 - When a traced, cloned process exits and is waited for by the
687 debugger, the kernel reassigns it to the original parent and
688 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
689 library doesn't notice this, which leads to the "zombie problem":
690 When debugged a multi-threaded process that spawns a lot of
691 threads will run out of processes, even if the threads exit,
692 because the "zombies" stay around. */
694 /* List of known LWPs. */
695 struct lwp_info *lwp_list;
698 /* Original signal mask. */
699 static sigset_t normal_mask;
701 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
702 _initialize_linux_nat. */
703 static sigset_t suspend_mask;
705 /* Signals to block to make that sigsuspend work. */
706 static sigset_t blocked_mask;
708 /* SIGCHLD action. */
709 struct sigaction sigchld_action;
711 /* Block child signals (SIGCHLD and linux threads signals), and store
712 the previous mask in PREV_MASK. */
715 block_child_signals (sigset_t *prev_mask)
717 /* Make sure SIGCHLD is blocked. */
718 if (!sigismember (&blocked_mask, SIGCHLD))
719 sigaddset (&blocked_mask, SIGCHLD);
721 sigprocmask (SIG_BLOCK, &blocked_mask, prev_mask);
724 /* Restore child signals mask, previously returned by
725 block_child_signals. */
728 restore_child_signals_mask (sigset_t *prev_mask)
730 sigprocmask (SIG_SETMASK, prev_mask, NULL);
733 /* Mask of signals to pass directly to the inferior. */
734 static sigset_t pass_mask;
736 /* Update signals to pass to the inferior. */
738 linux_nat_pass_signals (struct target_ops *self,
739 int numsigs, unsigned char *pass_signals)
743 sigemptyset (&pass_mask);
745 for (signo = 1; signo < NSIG; signo++)
747 int target_signo = gdb_signal_from_host (signo);
748 if (target_signo < numsigs && pass_signals[target_signo])
749 sigaddset (&pass_mask, signo);
755 /* Prototypes for local functions. */
756 static int stop_wait_callback (struct lwp_info *lp, void *data);
757 static int linux_thread_alive (ptid_t ptid);
758 static char *linux_child_pid_to_exec_file (struct target_ops *self, int pid);
759 static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
763 /* Destroy and free LP. */
766 lwp_free (struct lwp_info *lp)
768 xfree (lp->arch_private);
772 /* Remove all LWPs belong to PID from the lwp list. */
775 purge_lwp_list (int pid)
777 struct lwp_info *lp, *lpprev, *lpnext;
781 for (lp = lwp_list; lp; lp = lpnext)
785 if (ptid_get_pid (lp->ptid) == pid)
790 lpprev->next = lp->next;
799 /* Add the LWP specified by PTID to the list. PTID is the first LWP
800 in the process. Return a pointer to the structure describing the
803 This differs from add_lwp in that we don't let the arch specific
804 bits know about this new thread. Current clients of this callback
805 take the opportunity to install watchpoints in the new thread, and
806 we shouldn't do that for the first thread. If we're spawning a
807 child ("run"), the thread executes the shell wrapper first, and we
808 shouldn't touch it until it execs the program we want to debug.
809 For "attach", it'd be okay to call the callback, but it's not
810 necessary, because watchpoints can't yet have been inserted into
813 static struct lwp_info *
814 add_initial_lwp (ptid_t ptid)
818 gdb_assert (ptid_lwp_p (ptid));
820 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
822 memset (lp, 0, sizeof (struct lwp_info));
824 lp->last_resume_kind = resume_continue;
825 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
836 /* Add the LWP specified by PID to the list. Return a pointer to the
837 structure describing the new LWP. The LWP should already be
840 static struct lwp_info *
841 add_lwp (ptid_t ptid)
845 lp = add_initial_lwp (ptid);
847 /* Let the arch specific bits know about this new thread. Current
848 clients of this callback take the opportunity to install
849 watchpoints in the new thread. We don't do this for the first
850 thread though. See add_initial_lwp. */
851 if (linux_nat_new_thread != NULL)
852 linux_nat_new_thread (lp);
857 /* Remove the LWP specified by PID from the list. */
860 delete_lwp (ptid_t ptid)
862 struct lwp_info *lp, *lpprev;
866 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
867 if (ptid_equal (lp->ptid, ptid))
874 lpprev->next = lp->next;
881 /* Return a pointer to the structure describing the LWP corresponding
882 to PID. If no corresponding LWP could be found, return NULL. */
884 static struct lwp_info *
885 find_lwp_pid (ptid_t ptid)
890 if (ptid_lwp_p (ptid))
891 lwp = ptid_get_lwp (ptid);
893 lwp = ptid_get_pid (ptid);
895 for (lp = lwp_list; lp; lp = lp->next)
896 if (lwp == ptid_get_lwp (lp->ptid))
902 /* See nat/linux-nat.h. */
905 iterate_over_lwps (ptid_t filter,
906 iterate_over_lwps_ftype callback,
909 struct lwp_info *lp, *lpnext;
911 for (lp = lwp_list; lp; lp = lpnext)
915 if (ptid_match (lp->ptid, filter))
917 if ((*callback) (lp, data) != 0)
925 /* Update our internal state when changing from one checkpoint to
926 another indicated by NEW_PTID. We can only switch single-threaded
927 applications, so we only create one new LWP, and the previous list
931 linux_nat_switch_fork (ptid_t new_ptid)
935 purge_lwp_list (ptid_get_pid (inferior_ptid));
937 lp = add_lwp (new_ptid);
940 /* This changes the thread's ptid while preserving the gdb thread
941 num. Also changes the inferior pid, while preserving the
943 thread_change_ptid (inferior_ptid, new_ptid);
945 /* We've just told GDB core that the thread changed target id, but,
946 in fact, it really is a different thread, with different register
948 registers_changed ();
951 /* Handle the exit of a single thread LP. */
954 exit_lwp (struct lwp_info *lp)
956 struct thread_info *th = find_thread_ptid (lp->ptid);
960 if (print_thread_events)
961 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid));
963 delete_thread (lp->ptid);
966 delete_lwp (lp->ptid);
969 /* Wait for the LWP specified by LP, which we have just attached to.
970 Returns a wait status for that LWP, to cache. */
973 linux_nat_post_attach_wait (ptid_t ptid, int first, int *cloned,
976 pid_t new_pid, pid = ptid_get_lwp (ptid);
979 if (linux_proc_pid_is_stopped (pid))
982 fprintf_unfiltered (gdb_stdlog,
983 "LNPAW: Attaching to a stopped process\n");
985 /* The process is definitely stopped. It is in a job control
986 stop, unless the kernel predates the TASK_STOPPED /
987 TASK_TRACED distinction, in which case it might be in a
988 ptrace stop. Make sure it is in a ptrace stop; from there we
989 can kill it, signal it, et cetera.
991 First make sure there is a pending SIGSTOP. Since we are
992 already attached, the process can not transition from stopped
993 to running without a PTRACE_CONT; so we know this signal will
994 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
995 probably already in the queue (unless this kernel is old
996 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
997 is not an RT signal, it can only be queued once. */
998 kill_lwp (pid, SIGSTOP);
1000 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1001 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1002 ptrace (PTRACE_CONT, pid, 0, 0);
1005 /* Make sure the initial process is stopped. The user-level threads
1006 layer might want to poke around in the inferior, and that won't
1007 work if things haven't stabilized yet. */
1008 new_pid = my_waitpid (pid, &status, 0);
1009 if (new_pid == -1 && errno == ECHILD)
1012 warning (_("%s is a cloned process"), target_pid_to_str (ptid));
1014 /* Try again with __WCLONE to check cloned processes. */
1015 new_pid = my_waitpid (pid, &status, __WCLONE);
1019 gdb_assert (pid == new_pid);
1021 if (!WIFSTOPPED (status))
1023 /* The pid we tried to attach has apparently just exited. */
1024 if (debug_linux_nat)
1025 fprintf_unfiltered (gdb_stdlog, "LNPAW: Failed to stop %d: %s",
1026 pid, status_to_str (status));
1030 if (WSTOPSIG (status) != SIGSTOP)
1033 if (debug_linux_nat)
1034 fprintf_unfiltered (gdb_stdlog,
1035 "LNPAW: Received %s after attaching\n",
1036 status_to_str (status));
1042 /* Attach to the LWP specified by PID. Return 0 if successful, -1 if
1043 the new LWP could not be attached, or 1 if we're already auto
1044 attached to this thread, but haven't processed the
1045 PTRACE_EVENT_CLONE event of its parent thread, so we just ignore
1046 its existance, without considering it an error. */
1049 lin_lwp_attach_lwp (ptid_t ptid)
1051 struct lwp_info *lp;
1054 gdb_assert (ptid_lwp_p (ptid));
1056 lp = find_lwp_pid (ptid);
1057 lwpid = ptid_get_lwp (ptid);
1059 /* We assume that we're already attached to any LWP that is already
1060 in our list of LWPs. If we're not seeing exit events from threads
1061 and we've had PID wraparound since we last tried to stop all threads,
1062 this assumption might be wrong; fortunately, this is very unlikely
1066 int status, cloned = 0, signalled = 0;
1068 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0)
1070 if (linux_supports_tracefork ())
1072 /* If we haven't stopped all threads when we get here,
1073 we may have seen a thread listed in thread_db's list,
1074 but not processed the PTRACE_EVENT_CLONE yet. If
1075 that's the case, ignore this new thread, and let
1076 normal event handling discover it later. */
1077 if (in_pid_list_p (stopped_pids, lwpid))
1079 /* We've already seen this thread stop, but we
1080 haven't seen the PTRACE_EVENT_CLONE extended
1082 if (debug_linux_nat)
1083 fprintf_unfiltered (gdb_stdlog,
1084 "LLAL: attach failed, but already seen "
1085 "this thread %s stop\n",
1086 target_pid_to_str (ptid));
1094 if (debug_linux_nat)
1095 fprintf_unfiltered (gdb_stdlog,
1096 "LLAL: attach failed, and haven't seen "
1097 "this thread %s stop yet\n",
1098 target_pid_to_str (ptid));
1100 /* We may or may not be attached to the LWP already.
1101 Try waitpid on it. If that errors, we're not
1102 attached to the LWP yet. Otherwise, we're
1103 already attached. */
1104 gdb_assert (lwpid > 0);
1105 new_pid = my_waitpid (lwpid, &status, WNOHANG);
1106 if (new_pid == -1 && errno == ECHILD)
1107 new_pid = my_waitpid (lwpid, &status, __WCLONE | WNOHANG);
1112 /* The child hasn't stopped for its initial
1113 SIGSTOP stop yet. */
1114 if (debug_linux_nat)
1115 fprintf_unfiltered (gdb_stdlog,
1116 "LLAL: child hasn't "
1119 else if (WIFSTOPPED (status))
1121 if (debug_linux_nat)
1122 fprintf_unfiltered (gdb_stdlog,
1123 "LLAL: adding to stopped_pids\n");
1124 add_to_pid_list (&stopped_pids, lwpid, status);
1131 /* If we fail to attach to the thread, issue a warning,
1132 but continue. One way this can happen is if thread
1133 creation is interrupted; as of Linux kernel 2.6.19, a
1134 bug may place threads in the thread list and then fail
1136 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid),
1137 safe_strerror (errno));
1141 if (debug_linux_nat)
1142 fprintf_unfiltered (gdb_stdlog,
1143 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1144 target_pid_to_str (ptid));
1146 status = linux_nat_post_attach_wait (ptid, 0, &cloned, &signalled);
1147 if (!WIFSTOPPED (status))
1150 lp = add_lwp (ptid);
1152 lp->last_resume_kind = resume_stop;
1153 lp->cloned = cloned;
1154 lp->signalled = signalled;
1155 if (WSTOPSIG (status) != SIGSTOP)
1158 lp->status = status;
1161 target_post_attach (ptid_get_lwp (lp->ptid));
1163 if (debug_linux_nat)
1165 fprintf_unfiltered (gdb_stdlog,
1166 "LLAL: waitpid %s received %s\n",
1167 target_pid_to_str (ptid),
1168 status_to_str (status));
1176 linux_nat_create_inferior (struct target_ops *ops,
1177 char *exec_file, char *allargs, char **env,
1180 struct cleanup *restore_personality
1181 = maybe_disable_address_space_randomization (disable_randomization);
1183 /* The fork_child mechanism is synchronous and calls target_wait, so
1184 we have to mask the async mode. */
1186 /* Make sure we report all signals during startup. */
1187 linux_nat_pass_signals (ops, 0, NULL);
1189 linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
1191 do_cleanups (restore_personality);
1194 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1195 already attached. Returns true if a new LWP is found, false
1199 attach_proc_task_lwp_callback (ptid_t ptid)
1201 struct lwp_info *lp;
1203 /* Ignore LWPs we're already attached to. */
1204 lp = find_lwp_pid (ptid);
1207 int lwpid = ptid_get_lwp (ptid);
1209 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0)
1213 /* Be quiet if we simply raced with the thread exiting.
1214 EPERM is returned if the thread's task still exists, and
1215 is marked as exited or zombie, as well as other
1216 conditions, so in that case, confirm the status in
1217 /proc/PID/status. */
1219 || (err == EPERM && linux_proc_pid_is_gone (lwpid)))
1221 if (debug_linux_nat)
1223 fprintf_unfiltered (gdb_stdlog,
1224 "Cannot attach to lwp %d: "
1225 "thread is gone (%d: %s)\n",
1226 lwpid, err, safe_strerror (err));
1231 warning (_("Cannot attach to lwp %d: %s"),
1233 linux_ptrace_attach_fail_reason_string (ptid,
1239 if (debug_linux_nat)
1240 fprintf_unfiltered (gdb_stdlog,
1241 "PTRACE_ATTACH %s, 0, 0 (OK)\n",
1242 target_pid_to_str (ptid));
1244 lp = add_lwp (ptid);
1247 /* The next time we wait for this LWP we'll see a SIGSTOP as
1248 PTRACE_ATTACH brings it to a halt. */
1251 /* We need to wait for a stop before being able to make the
1252 next ptrace call on this LWP. */
1253 lp->must_set_ptrace_flags = 1;
1262 linux_nat_attach (struct target_ops *ops, const char *args, int from_tty)
1264 struct lwp_info *lp;
1268 /* Make sure we report all signals during attach. */
1269 linux_nat_pass_signals (ops, 0, NULL);
1273 linux_ops->to_attach (ops, args, from_tty);
1275 CATCH (ex, RETURN_MASK_ERROR)
1277 pid_t pid = parse_pid_to_attach (args);
1278 struct buffer buffer;
1279 char *message, *buffer_s;
1281 message = xstrdup (ex.message);
1282 make_cleanup (xfree, message);
1284 buffer_init (&buffer);
1285 linux_ptrace_attach_fail_reason (pid, &buffer);
1287 buffer_grow_str0 (&buffer, "");
1288 buffer_s = buffer_finish (&buffer);
1289 make_cleanup (xfree, buffer_s);
1291 if (*buffer_s != '\0')
1292 throw_error (ex.error, "warning: %s\n%s", buffer_s, message);
1294 throw_error (ex.error, "%s", message);
1298 /* The ptrace base target adds the main thread with (pid,0,0)
1299 format. Decorate it with lwp info. */
1300 ptid = ptid_build (ptid_get_pid (inferior_ptid),
1301 ptid_get_pid (inferior_ptid),
1303 thread_change_ptid (inferior_ptid, ptid);
1305 /* Add the initial process as the first LWP to the list. */
1306 lp = add_initial_lwp (ptid);
1308 status = linux_nat_post_attach_wait (lp->ptid, 1, &lp->cloned,
1310 if (!WIFSTOPPED (status))
1312 if (WIFEXITED (status))
1314 int exit_code = WEXITSTATUS (status);
1316 target_terminal_ours ();
1317 target_mourn_inferior ();
1319 error (_("Unable to attach: program exited normally."));
1321 error (_("Unable to attach: program exited with code %d."),
1324 else if (WIFSIGNALED (status))
1326 enum gdb_signal signo;
1328 target_terminal_ours ();
1329 target_mourn_inferior ();
1331 signo = gdb_signal_from_host (WTERMSIG (status));
1332 error (_("Unable to attach: program terminated with signal "
1334 gdb_signal_to_name (signo),
1335 gdb_signal_to_string (signo));
1338 internal_error (__FILE__, __LINE__,
1339 _("unexpected status %d for PID %ld"),
1340 status, (long) ptid_get_lwp (ptid));
1345 /* Save the wait status to report later. */
1347 if (debug_linux_nat)
1348 fprintf_unfiltered (gdb_stdlog,
1349 "LNA: waitpid %ld, saving status %s\n",
1350 (long) ptid_get_pid (lp->ptid), status_to_str (status));
1352 lp->status = status;
1354 /* We must attach to every LWP. If /proc is mounted, use that to
1355 find them now. The inferior may be using raw clone instead of
1356 using pthreads. But even if it is using pthreads, thread_db
1357 walks structures in the inferior's address space to find the list
1358 of threads/LWPs, and those structures may well be corrupted.
1359 Note that once thread_db is loaded, we'll still use it to list
1360 threads and associate pthread info with each LWP. */
1361 linux_proc_attach_tgid_threads (ptid_get_pid (lp->ptid),
1362 attach_proc_task_lwp_callback);
1364 if (target_can_async_p ())
1368 /* Get pending status of LP. */
1370 get_pending_status (struct lwp_info *lp, int *status)
1372 enum gdb_signal signo = GDB_SIGNAL_0;
1374 /* If we paused threads momentarily, we may have stored pending
1375 events in lp->status or lp->waitstatus (see stop_wait_callback),
1376 and GDB core hasn't seen any signal for those threads.
1377 Otherwise, the last signal reported to the core is found in the
1378 thread object's stop_signal.
1380 There's a corner case that isn't handled here at present. Only
1381 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1382 stop_signal make sense as a real signal to pass to the inferior.
1383 Some catchpoint related events, like
1384 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1385 to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1386 those traps are debug API (ptrace in our case) related and
1387 induced; the inferior wouldn't see them if it wasn't being
1388 traced. Hence, we should never pass them to the inferior, even
1389 when set to pass state. Since this corner case isn't handled by
1390 infrun.c when proceeding with a signal, for consistency, neither
1391 do we handle it here (or elsewhere in the file we check for
1392 signal pass state). Normally SIGTRAP isn't set to pass state, so
1393 this is really a corner case. */
1395 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
1396 signo = GDB_SIGNAL_0; /* a pending ptrace event, not a real signal. */
1397 else if (lp->status)
1398 signo = gdb_signal_from_host (WSTOPSIG (lp->status));
1399 else if (non_stop && !is_executing (lp->ptid))
1401 struct thread_info *tp = find_thread_ptid (lp->ptid);
1403 signo = tp->suspend.stop_signal;
1407 struct target_waitstatus last;
1410 get_last_target_status (&last_ptid, &last);
1412 if (ptid_get_lwp (lp->ptid) == ptid_get_lwp (last_ptid))
1414 struct thread_info *tp = find_thread_ptid (lp->ptid);
1416 signo = tp->suspend.stop_signal;
1422 if (signo == GDB_SIGNAL_0)
1424 if (debug_linux_nat)
1425 fprintf_unfiltered (gdb_stdlog,
1426 "GPT: lwp %s has no pending signal\n",
1427 target_pid_to_str (lp->ptid));
1429 else if (!signal_pass_state (signo))
1431 if (debug_linux_nat)
1432 fprintf_unfiltered (gdb_stdlog,
1433 "GPT: lwp %s had signal %s, "
1434 "but it is in no pass state\n",
1435 target_pid_to_str (lp->ptid),
1436 gdb_signal_to_string (signo));
1440 *status = W_STOPCODE (gdb_signal_to_host (signo));
1442 if (debug_linux_nat)
1443 fprintf_unfiltered (gdb_stdlog,
1444 "GPT: lwp %s has pending signal %s\n",
1445 target_pid_to_str (lp->ptid),
1446 gdb_signal_to_string (signo));
1453 detach_callback (struct lwp_info *lp, void *data)
1455 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1457 if (debug_linux_nat && lp->status)
1458 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
1459 strsignal (WSTOPSIG (lp->status)),
1460 target_pid_to_str (lp->ptid));
1462 /* If there is a pending SIGSTOP, get rid of it. */
1465 if (debug_linux_nat)
1466 fprintf_unfiltered (gdb_stdlog,
1467 "DC: Sending SIGCONT to %s\n",
1468 target_pid_to_str (lp->ptid));
1470 kill_lwp (ptid_get_lwp (lp->ptid), SIGCONT);
1474 /* We don't actually detach from the LWP that has an id equal to the
1475 overall process id just yet. */
1476 if (ptid_get_lwp (lp->ptid) != ptid_get_pid (lp->ptid))
1480 /* Pass on any pending signal for this LWP. */
1481 get_pending_status (lp, &status);
1483 if (linux_nat_prepare_to_resume != NULL)
1484 linux_nat_prepare_to_resume (lp);
1486 if (ptrace (PTRACE_DETACH, ptid_get_lwp (lp->ptid), 0,
1487 WSTOPSIG (status)) < 0)
1488 error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
1489 safe_strerror (errno));
1491 if (debug_linux_nat)
1492 fprintf_unfiltered (gdb_stdlog,
1493 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1494 target_pid_to_str (lp->ptid),
1495 strsignal (WSTOPSIG (status)));
1497 delete_lwp (lp->ptid);
1504 linux_nat_detach (struct target_ops *ops, const char *args, int from_tty)
1508 struct lwp_info *main_lwp;
1510 pid = ptid_get_pid (inferior_ptid);
1512 /* Don't unregister from the event loop, as there may be other
1513 inferiors running. */
1515 /* Stop all threads before detaching. ptrace requires that the
1516 thread is stopped to sucessfully detach. */
1517 iterate_over_lwps (pid_to_ptid (pid), stop_callback, NULL);
1518 /* ... and wait until all of them have reported back that
1519 they're no longer running. */
1520 iterate_over_lwps (pid_to_ptid (pid), stop_wait_callback, NULL);
1522 iterate_over_lwps (pid_to_ptid (pid), detach_callback, NULL);
1524 /* Only the initial process should be left right now. */
1525 gdb_assert (num_lwps (ptid_get_pid (inferior_ptid)) == 1);
1527 main_lwp = find_lwp_pid (pid_to_ptid (pid));
1529 /* Pass on any pending signal for the last LWP. */
1530 if ((args == NULL || *args == '\0')
1531 && get_pending_status (main_lwp, &status) != -1
1532 && WIFSTOPPED (status))
1536 /* Put the signal number in ARGS so that inf_ptrace_detach will
1537 pass it along with PTRACE_DETACH. */
1539 xsnprintf (tem, 8, "%d", (int) WSTOPSIG (status));
1541 if (debug_linux_nat)
1542 fprintf_unfiltered (gdb_stdlog,
1543 "LND: Sending signal %s to %s\n",
1545 target_pid_to_str (main_lwp->ptid));
1548 if (linux_nat_prepare_to_resume != NULL)
1549 linux_nat_prepare_to_resume (main_lwp);
1550 delete_lwp (main_lwp->ptid);
1552 if (forks_exist_p ())
1554 /* Multi-fork case. The current inferior_ptid is being detached
1555 from, but there are other viable forks to debug. Detach from
1556 the current fork, and context-switch to the first
1558 linux_fork_detach (args, from_tty);
1561 linux_ops->to_detach (ops, args, from_tty);
1564 /* Resume execution of the inferior process. If STEP is nonzero,
1565 single-step it. If SIGNAL is nonzero, give it that signal. */
1568 linux_resume_one_lwp_throw (struct lwp_info *lp, int step,
1569 enum gdb_signal signo)
1573 /* stop_pc doubles as the PC the LWP had when it was last resumed.
1574 We only presently need that if the LWP is stepped though (to
1575 handle the case of stepping a breakpoint instruction). */
1578 struct regcache *regcache = get_thread_regcache (lp->ptid);
1580 lp->stop_pc = regcache_read_pc (regcache);
1585 if (linux_nat_prepare_to_resume != NULL)
1586 linux_nat_prepare_to_resume (lp);
1587 linux_ops->to_resume (linux_ops, lp->ptid, step, signo);
1589 /* Successfully resumed. Clear state that no longer makes sense,
1590 and mark the LWP as running. Must not do this before resuming
1591 otherwise if that fails other code will be confused. E.g., we'd
1592 later try to stop the LWP and hang forever waiting for a stop
1593 status. Note that we must not throw after this is cleared,
1594 otherwise handle_zombie_lwp_error would get confused. */
1596 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
1597 registers_changed_ptid (lp->ptid);
1600 /* Called when we try to resume a stopped LWP and that errors out. If
1601 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
1602 or about to become), discard the error, clear any pending status
1603 the LWP may have, and return true (we'll collect the exit status
1604 soon enough). Otherwise, return false. */
1607 check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
1609 /* If we get an error after resuming the LWP successfully, we'd
1610 confuse !T state for the LWP being gone. */
1611 gdb_assert (lp->stopped);
1613 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
1614 because even if ptrace failed with ESRCH, the tracee may be "not
1615 yet fully dead", but already refusing ptrace requests. In that
1616 case the tracee has 'R (Running)' state for a little bit
1617 (observed in Linux 3.18). See also the note on ESRCH in the
1618 ptrace(2) man page. Instead, check whether the LWP has any state
1619 other than ptrace-stopped. */
1621 /* Don't assume anything if /proc/PID/status can't be read. */
1622 if (linux_proc_pid_is_trace_stopped_nowarn (ptid_get_lwp (lp->ptid)) == 0)
1624 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
1626 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1632 /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
1633 disappears while we try to resume it. */
1636 linux_resume_one_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
1640 linux_resume_one_lwp_throw (lp, step, signo);
1642 CATCH (ex, RETURN_MASK_ERROR)
1644 if (!check_ptrace_stopped_lwp_gone (lp))
1645 throw_exception (ex);
1653 resume_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
1657 struct inferior *inf = find_inferior_ptid (lp->ptid);
1659 if (inf->vfork_child != NULL)
1661 if (debug_linux_nat)
1662 fprintf_unfiltered (gdb_stdlog,
1663 "RC: Not resuming %s (vfork parent)\n",
1664 target_pid_to_str (lp->ptid));
1666 else if (!lwp_status_pending_p (lp))
1668 if (debug_linux_nat)
1669 fprintf_unfiltered (gdb_stdlog,
1670 "RC: Resuming sibling %s, %s, %s\n",
1671 target_pid_to_str (lp->ptid),
1672 (signo != GDB_SIGNAL_0
1673 ? strsignal (gdb_signal_to_host (signo))
1675 step ? "step" : "resume");
1677 linux_resume_one_lwp (lp, step, signo);
1681 if (debug_linux_nat)
1682 fprintf_unfiltered (gdb_stdlog,
1683 "RC: Not resuming sibling %s (has pending)\n",
1684 target_pid_to_str (lp->ptid));
1689 if (debug_linux_nat)
1690 fprintf_unfiltered (gdb_stdlog,
1691 "RC: Not resuming sibling %s (not stopped)\n",
1692 target_pid_to_str (lp->ptid));
1696 /* Callback for iterate_over_lwps. If LWP is EXCEPT, do nothing.
1697 Resume LWP with the last stop signal, if it is in pass state. */
1700 linux_nat_resume_callback (struct lwp_info *lp, void *except)
1702 enum gdb_signal signo = GDB_SIGNAL_0;
1709 struct thread_info *thread;
1711 thread = find_thread_ptid (lp->ptid);
1714 signo = thread->suspend.stop_signal;
1715 thread->suspend.stop_signal = GDB_SIGNAL_0;
1719 resume_lwp (lp, 0, signo);
1724 resume_clear_callback (struct lwp_info *lp, void *data)
1727 lp->last_resume_kind = resume_stop;
1732 resume_set_callback (struct lwp_info *lp, void *data)
1735 lp->last_resume_kind = resume_continue;
1740 linux_nat_resume (struct target_ops *ops,
1741 ptid_t ptid, int step, enum gdb_signal signo)
1743 struct lwp_info *lp;
1746 if (debug_linux_nat)
1747 fprintf_unfiltered (gdb_stdlog,
1748 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1749 step ? "step" : "resume",
1750 target_pid_to_str (ptid),
1751 (signo != GDB_SIGNAL_0
1752 ? strsignal (gdb_signal_to_host (signo)) : "0"),
1753 target_pid_to_str (inferior_ptid));
1755 /* A specific PTID means `step only this process id'. */
1756 resume_many = (ptid_equal (minus_one_ptid, ptid)
1757 || ptid_is_pid (ptid));
1759 /* Mark the lwps we're resuming as resumed. */
1760 iterate_over_lwps (ptid, resume_set_callback, NULL);
1762 /* See if it's the current inferior that should be handled
1765 lp = find_lwp_pid (inferior_ptid);
1767 lp = find_lwp_pid (ptid);
1768 gdb_assert (lp != NULL);
1770 /* Remember if we're stepping. */
1771 lp->last_resume_kind = step ? resume_step : resume_continue;
1773 /* If we have a pending wait status for this thread, there is no
1774 point in resuming the process. But first make sure that
1775 linux_nat_wait won't preemptively handle the event - we
1776 should never take this short-circuit if we are going to
1777 leave LP running, since we have skipped resuming all the
1778 other threads. This bit of code needs to be synchronized
1779 with linux_nat_wait. */
1781 if (lp->status && WIFSTOPPED (lp->status))
1784 && WSTOPSIG (lp->status)
1785 && sigismember (&pass_mask, WSTOPSIG (lp->status)))
1787 if (debug_linux_nat)
1788 fprintf_unfiltered (gdb_stdlog,
1789 "LLR: Not short circuiting for ignored "
1790 "status 0x%x\n", lp->status);
1792 /* FIXME: What should we do if we are supposed to continue
1793 this thread with a signal? */
1794 gdb_assert (signo == GDB_SIGNAL_0);
1795 signo = gdb_signal_from_host (WSTOPSIG (lp->status));
1800 if (lwp_status_pending_p (lp))
1802 /* FIXME: What should we do if we are supposed to continue
1803 this thread with a signal? */
1804 gdb_assert (signo == GDB_SIGNAL_0);
1806 if (debug_linux_nat)
1807 fprintf_unfiltered (gdb_stdlog,
1808 "LLR: Short circuiting for status 0x%x\n",
1811 if (target_can_async_p ())
1814 /* Tell the event loop we have something to process. */
1821 iterate_over_lwps (ptid, linux_nat_resume_callback, lp);
1823 if (debug_linux_nat)
1824 fprintf_unfiltered (gdb_stdlog,
1825 "LLR: %s %s, %s (resume event thread)\n",
1826 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1827 target_pid_to_str (lp->ptid),
1828 (signo != GDB_SIGNAL_0
1829 ? strsignal (gdb_signal_to_host (signo)) : "0"));
1831 linux_resume_one_lwp (lp, step, signo);
1833 if (target_can_async_p ())
1837 /* Send a signal to an LWP. */
1840 kill_lwp (int lwpid, int signo)
1842 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1843 fails, then we are not using nptl threads and we should be using kill. */
1845 #ifdef HAVE_TKILL_SYSCALL
1847 static int tkill_failed;
1854 ret = syscall (__NR_tkill, lwpid, signo);
1855 if (errno != ENOSYS)
1862 return kill (lwpid, signo);
1865 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
1866 event, check if the core is interested in it: if not, ignore the
1867 event, and keep waiting; otherwise, we need to toggle the LWP's
1868 syscall entry/exit status, since the ptrace event itself doesn't
1869 indicate it, and report the trap to higher layers. */
1872 linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
1874 struct target_waitstatus *ourstatus = &lp->waitstatus;
1875 struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
1876 int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, lp->ptid);
1880 /* If we're stopping threads, there's a SIGSTOP pending, which
1881 makes it so that the LWP reports an immediate syscall return,
1882 followed by the SIGSTOP. Skip seeing that "return" using
1883 PTRACE_CONT directly, and let stop_wait_callback collect the
1884 SIGSTOP. Later when the thread is resumed, a new syscall
1885 entry event. If we didn't do this (and returned 0), we'd
1886 leave a syscall entry pending, and our caller, by using
1887 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
1888 itself. Later, when the user re-resumes this LWP, we'd see
1889 another syscall entry event and we'd mistake it for a return.
1891 If stop_wait_callback didn't force the SIGSTOP out of the LWP
1892 (leaving immediately with LWP->signalled set, without issuing
1893 a PTRACE_CONT), it would still be problematic to leave this
1894 syscall enter pending, as later when the thread is resumed,
1895 it would then see the same syscall exit mentioned above,
1896 followed by the delayed SIGSTOP, while the syscall didn't
1897 actually get to execute. It seems it would be even more
1898 confusing to the user. */
1900 if (debug_linux_nat)
1901 fprintf_unfiltered (gdb_stdlog,
1902 "LHST: ignoring syscall %d "
1903 "for LWP %ld (stopping threads), "
1904 "resuming with PTRACE_CONT for SIGSTOP\n",
1906 ptid_get_lwp (lp->ptid));
1908 lp->syscall_state = TARGET_WAITKIND_IGNORE;
1909 ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
1914 if (catch_syscall_enabled ())
1916 /* Always update the entry/return state, even if this particular
1917 syscall isn't interesting to the core now. In async mode,
1918 the user could install a new catchpoint for this syscall
1919 between syscall enter/return, and we'll need to know to
1920 report a syscall return if that happens. */
1921 lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
1922 ? TARGET_WAITKIND_SYSCALL_RETURN
1923 : TARGET_WAITKIND_SYSCALL_ENTRY);
1925 if (catching_syscall_number (syscall_number))
1927 /* Alright, an event to report. */
1928 ourstatus->kind = lp->syscall_state;
1929 ourstatus->value.syscall_number = syscall_number;
1931 if (debug_linux_nat)
1932 fprintf_unfiltered (gdb_stdlog,
1933 "LHST: stopping for %s of syscall %d"
1936 == TARGET_WAITKIND_SYSCALL_ENTRY
1937 ? "entry" : "return",
1939 ptid_get_lwp (lp->ptid));
1943 if (debug_linux_nat)
1944 fprintf_unfiltered (gdb_stdlog,
1945 "LHST: ignoring %s of syscall %d "
1947 lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
1948 ? "entry" : "return",
1950 ptid_get_lwp (lp->ptid));
1954 /* If we had been syscall tracing, and hence used PT_SYSCALL
1955 before on this LWP, it could happen that the user removes all
1956 syscall catchpoints before we get to process this event.
1957 There are two noteworthy issues here:
1959 - When stopped at a syscall entry event, resuming with
1960 PT_STEP still resumes executing the syscall and reports a
1963 - Only PT_SYSCALL catches syscall enters. If we last
1964 single-stepped this thread, then this event can't be a
1965 syscall enter. If we last single-stepped this thread, this
1966 has to be a syscall exit.
1968 The points above mean that the next resume, be it PT_STEP or
1969 PT_CONTINUE, can not trigger a syscall trace event. */
1970 if (debug_linux_nat)
1971 fprintf_unfiltered (gdb_stdlog,
1972 "LHST: caught syscall event "
1973 "with no syscall catchpoints."
1974 " %d for LWP %ld, ignoring\n",
1976 ptid_get_lwp (lp->ptid));
1977 lp->syscall_state = TARGET_WAITKIND_IGNORE;
1980 /* The core isn't interested in this event. For efficiency, avoid
1981 stopping all threads only to have the core resume them all again.
1982 Since we're not stopping threads, if we're still syscall tracing
1983 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
1984 subsequent syscall. Simply resume using the inf-ptrace layer,
1985 which knows when to use PT_SYSCALL or PT_CONTINUE. */
1987 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
1991 /* Handle a GNU/Linux extended wait response. If we see a clone
1992 event, we need to add the new LWP to our list (and not report the
1993 trap to higher layers). This function returns non-zero if the
1994 event should be ignored and we should wait again. If STOPPING is
1995 true, the new LWP remains stopped, otherwise it is continued. */
1998 linux_handle_extended_wait (struct lwp_info *lp, int status,
2001 int pid = ptid_get_lwp (lp->ptid);
2002 struct target_waitstatus *ourstatus = &lp->waitstatus;
2003 int event = linux_ptrace_get_extended_event (status);
2005 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
2006 || event == PTRACE_EVENT_CLONE)
2008 unsigned long new_pid;
2011 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
2013 /* If we haven't already seen the new PID stop, wait for it now. */
2014 if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
2016 /* The new child has a pending SIGSTOP. We can't affect it until it
2017 hits the SIGSTOP, but we're already attached. */
2018 ret = my_waitpid (new_pid, &status,
2019 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
2021 perror_with_name (_("waiting for new child"));
2022 else if (ret != new_pid)
2023 internal_error (__FILE__, __LINE__,
2024 _("wait returned unexpected PID %d"), ret);
2025 else if (!WIFSTOPPED (status))
2026 internal_error (__FILE__, __LINE__,
2027 _("wait returned unexpected status 0x%x"), status);
2030 ourstatus->value.related_pid = ptid_build (new_pid, new_pid, 0);
2032 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
2034 /* The arch-specific native code may need to know about new
2035 forks even if those end up never mapped to an
2037 if (linux_nat_new_fork != NULL)
2038 linux_nat_new_fork (lp, new_pid);
2041 if (event == PTRACE_EVENT_FORK
2042 && linux_fork_checkpointing_p (ptid_get_pid (lp->ptid)))
2044 /* Handle checkpointing by linux-fork.c here as a special
2045 case. We don't want the follow-fork-mode or 'catch fork'
2046 to interfere with this. */
2048 /* This won't actually modify the breakpoint list, but will
2049 physically remove the breakpoints from the child. */
2050 detach_breakpoints (ptid_build (new_pid, new_pid, 0));
2052 /* Retain child fork in ptrace (stopped) state. */
2053 if (!find_fork_pid (new_pid))
2056 /* Report as spurious, so that infrun doesn't want to follow
2057 this fork. We're actually doing an infcall in
2059 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
2061 /* Report the stop to the core. */
2065 if (event == PTRACE_EVENT_FORK)
2066 ourstatus->kind = TARGET_WAITKIND_FORKED;
2067 else if (event == PTRACE_EVENT_VFORK)
2068 ourstatus->kind = TARGET_WAITKIND_VFORKED;
2071 struct lwp_info *new_lp;
2073 ourstatus->kind = TARGET_WAITKIND_IGNORE;
2075 if (debug_linux_nat)
2076 fprintf_unfiltered (gdb_stdlog,
2077 "LHEW: Got clone event "
2078 "from LWP %d, new child is LWP %ld\n",
2081 new_lp = add_lwp (ptid_build (ptid_get_pid (lp->ptid), new_pid, 0));
2083 new_lp->stopped = 1;
2085 if (WSTOPSIG (status) != SIGSTOP)
2087 /* This can happen if someone starts sending signals to
2088 the new thread before it gets a chance to run, which
2089 have a lower number than SIGSTOP (e.g. SIGUSR1).
2090 This is an unlikely case, and harder to handle for
2091 fork / vfork than for clone, so we do not try - but
2092 we handle it for clone events here. We'll send
2093 the other signal on to the thread below. */
2095 new_lp->signalled = 1;
2099 struct thread_info *tp;
2101 /* When we stop for an event in some other thread, and
2102 pull the thread list just as this thread has cloned,
2103 we'll have seen the new thread in the thread_db list
2104 before handling the CLONE event (glibc's
2105 pthread_create adds the new thread to the thread list
2106 before clone'ing, and has the kernel fill in the
2107 thread's tid on the clone call with
2108 CLONE_PARENT_SETTID). If that happened, and the core
2109 had requested the new thread to stop, we'll have
2110 killed it with SIGSTOP. But since SIGSTOP is not an
2111 RT signal, it can only be queued once. We need to be
2112 careful to not resume the LWP if we wanted it to
2113 stop. In that case, we'll leave the SIGSTOP pending.
2114 It will later be reported as GDB_SIGNAL_0. */
2115 tp = find_thread_ptid (new_lp->ptid);
2116 if (tp != NULL && tp->stop_requested)
2117 new_lp->last_resume_kind = resume_stop;
2122 /* If the thread_db layer is active, let it record the user
2123 level thread id and status, and add the thread to GDB's
2125 if (!thread_db_notice_clone (lp->ptid, new_lp->ptid))
2127 /* The process is not using thread_db. Add the LWP to
2129 target_post_attach (ptid_get_lwp (new_lp->ptid));
2130 add_thread (new_lp->ptid);
2133 /* Even if we're stopping the thread for some reason
2134 internal to this module, from the user/frontend's
2135 perspective, this new thread is running. */
2136 set_running (new_lp->ptid, 1);
2139 set_executing (new_lp->ptid, 1);
2140 /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
2142 new_lp->last_resume_kind = resume_continue;
2147 /* We created NEW_LP so it cannot yet contain STATUS. */
2148 gdb_assert (new_lp->status == 0);
2150 /* Save the wait status to report later. */
2151 if (debug_linux_nat)
2152 fprintf_unfiltered (gdb_stdlog,
2153 "LHEW: waitpid of new LWP %ld, "
2154 "saving status %s\n",
2155 (long) ptid_get_lwp (new_lp->ptid),
2156 status_to_str (status));
2157 new_lp->status = status;
2160 new_lp->resumed = !stopping;
2167 if (event == PTRACE_EVENT_EXEC)
2169 if (debug_linux_nat)
2170 fprintf_unfiltered (gdb_stdlog,
2171 "LHEW: Got exec event from LWP %ld\n",
2172 ptid_get_lwp (lp->ptid));
2174 ourstatus->kind = TARGET_WAITKIND_EXECD;
2175 ourstatus->value.execd_pathname
2176 = xstrdup (linux_child_pid_to_exec_file (NULL, pid));
2178 /* The thread that execed must have been resumed, but, when a
2179 thread execs, it changes its tid to the tgid, and the old
2180 tgid thread might have not been resumed. */
2185 if (event == PTRACE_EVENT_VFORK_DONE)
2187 if (current_inferior ()->waiting_for_vfork_done)
2189 if (debug_linux_nat)
2190 fprintf_unfiltered (gdb_stdlog,
2191 "LHEW: Got expected PTRACE_EVENT_"
2192 "VFORK_DONE from LWP %ld: stopping\n",
2193 ptid_get_lwp (lp->ptid));
2195 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
2199 if (debug_linux_nat)
2200 fprintf_unfiltered (gdb_stdlog,
2201 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2202 "from LWP %ld: ignoring\n",
2203 ptid_get_lwp (lp->ptid));
2207 internal_error (__FILE__, __LINE__,
2208 _("unknown ptrace event %d"), event);
2211 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2215 wait_lwp (struct lwp_info *lp)
2219 int thread_dead = 0;
2222 gdb_assert (!lp->stopped);
2223 gdb_assert (lp->status == 0);
2225 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2226 block_child_signals (&prev_mask);
2230 /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2231 was right and we should just call sigsuspend. */
2233 pid = my_waitpid (ptid_get_lwp (lp->ptid), &status, WNOHANG);
2234 if (pid == -1 && errno == ECHILD)
2235 pid = my_waitpid (ptid_get_lwp (lp->ptid), &status, __WCLONE | WNOHANG);
2236 if (pid == -1 && errno == ECHILD)
2238 /* The thread has previously exited. We need to delete it
2239 now because, for some vendor 2.4 kernels with NPTL
2240 support backported, there won't be an exit event unless
2241 it is the main thread. 2.6 kernels will report an exit
2242 event for each thread that exits, as expected. */
2244 if (debug_linux_nat)
2245 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
2246 target_pid_to_str (lp->ptid));
2251 /* Bugs 10970, 12702.
2252 Thread group leader may have exited in which case we'll lock up in
2253 waitpid if there are other threads, even if they are all zombies too.
2254 Basically, we're not supposed to use waitpid this way.
2255 __WCLONE is not applicable for the leader so we can't use that.
2256 LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2257 process; it gets ESRCH both for the zombie and for running processes.
2259 As a workaround, check if we're waiting for the thread group leader and
2260 if it's a zombie, and avoid calling waitpid if it is.
2262 This is racy, what if the tgl becomes a zombie right after we check?
2263 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2264 waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
2266 if (ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid)
2267 && linux_proc_pid_is_zombie (ptid_get_lwp (lp->ptid)))
2270 if (debug_linux_nat)
2271 fprintf_unfiltered (gdb_stdlog,
2272 "WL: Thread group leader %s vanished.\n",
2273 target_pid_to_str (lp->ptid));
2277 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2278 get invoked despite our caller had them intentionally blocked by
2279 block_child_signals. This is sensitive only to the loop of
2280 linux_nat_wait_1 and there if we get called my_waitpid gets called
2281 again before it gets to sigsuspend so we can safely let the handlers
2282 get executed here. */
2284 if (debug_linux_nat)
2285 fprintf_unfiltered (gdb_stdlog, "WL: about to sigsuspend\n");
2286 sigsuspend (&suspend_mask);
2289 restore_child_signals_mask (&prev_mask);
2293 gdb_assert (pid == ptid_get_lwp (lp->ptid));
2295 if (debug_linux_nat)
2297 fprintf_unfiltered (gdb_stdlog,
2298 "WL: waitpid %s received %s\n",
2299 target_pid_to_str (lp->ptid),
2300 status_to_str (status));
2303 /* Check if the thread has exited. */
2304 if (WIFEXITED (status) || WIFSIGNALED (status))
2307 if (debug_linux_nat)
2308 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
2309 target_pid_to_str (lp->ptid));
2319 gdb_assert (WIFSTOPPED (status));
2322 if (lp->must_set_ptrace_flags)
2324 struct inferior *inf = find_inferior_pid (ptid_get_pid (lp->ptid));
2325 int options = linux_nat_ptrace_options (inf->attach_flag);
2327 linux_enable_event_reporting (ptid_get_lwp (lp->ptid), options);
2328 lp->must_set_ptrace_flags = 0;
2331 /* Handle GNU/Linux's syscall SIGTRAPs. */
2332 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
2334 /* No longer need the sysgood bit. The ptrace event ends up
2335 recorded in lp->waitstatus if we care for it. We can carry
2336 on handling the event like a regular SIGTRAP from here
2338 status = W_STOPCODE (SIGTRAP);
2339 if (linux_handle_syscall_trap (lp, 1))
2340 return wait_lwp (lp);
2343 /* Handle GNU/Linux's extended waitstatus for trace events. */
2344 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
2345 && linux_is_extended_waitstatus (status))
2347 if (debug_linux_nat)
2348 fprintf_unfiltered (gdb_stdlog,
2349 "WL: Handling extended status 0x%06x\n",
2351 linux_handle_extended_wait (lp, status, 1);
2358 /* Send a SIGSTOP to LP. */
2361 stop_callback (struct lwp_info *lp, void *data)
2363 if (!lp->stopped && !lp->signalled)
2367 if (debug_linux_nat)
2369 fprintf_unfiltered (gdb_stdlog,
2370 "SC: kill %s **<SIGSTOP>**\n",
2371 target_pid_to_str (lp->ptid));
2374 ret = kill_lwp (ptid_get_lwp (lp->ptid), SIGSTOP);
2375 if (debug_linux_nat)
2377 fprintf_unfiltered (gdb_stdlog,
2378 "SC: lwp kill %d %s\n",
2380 errno ? safe_strerror (errno) : "ERRNO-OK");
2384 gdb_assert (lp->status == 0);
2390 /* Request a stop on LWP. */
2393 linux_stop_lwp (struct lwp_info *lwp)
2395 stop_callback (lwp, NULL);
2398 /* See linux-nat.h */
2401 linux_stop_and_wait_all_lwps (void)
2403 /* Stop all LWP's ... */
2404 iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
2406 /* ... and wait until all of them have reported back that
2407 they're no longer running. */
2408 iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
2411 /* See linux-nat.h */
2414 linux_unstop_all_lwps (void)
2416 iterate_over_lwps (minus_one_ptid,
2417 resume_stopped_resumed_lwps, &minus_one_ptid);
2420 /* Return non-zero if LWP PID has a pending SIGINT. */
2423 linux_nat_has_pending_sigint (int pid)
2425 sigset_t pending, blocked, ignored;
2427 linux_proc_pending_signals (pid, &pending, &blocked, &ignored);
2429 if (sigismember (&pending, SIGINT)
2430 && !sigismember (&ignored, SIGINT))
2436 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2439 set_ignore_sigint (struct lwp_info *lp, void *data)
2441 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2442 flag to consume the next one. */
2443 if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status)
2444 && WSTOPSIG (lp->status) == SIGINT)
2447 lp->ignore_sigint = 1;
2452 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2453 This function is called after we know the LWP has stopped; if the LWP
2454 stopped before the expected SIGINT was delivered, then it will never have
2455 arrived. Also, if the signal was delivered to a shared queue and consumed
2456 by a different thread, it will never be delivered to this LWP. */
2459 maybe_clear_ignore_sigint (struct lwp_info *lp)
2461 if (!lp->ignore_sigint)
2464 if (!linux_nat_has_pending_sigint (ptid_get_lwp (lp->ptid)))
2466 if (debug_linux_nat)
2467 fprintf_unfiltered (gdb_stdlog,
2468 "MCIS: Clearing bogus flag for %s\n",
2469 target_pid_to_str (lp->ptid));
2470 lp->ignore_sigint = 0;
2474 /* Fetch the possible triggered data watchpoint info and store it in
2477 On some archs, like x86, that use debug registers to set
2478 watchpoints, it's possible that the way to know which watched
2479 address trapped, is to check the register that is used to select
2480 which address to watch. Problem is, between setting the watchpoint
2481 and reading back which data address trapped, the user may change
2482 the set of watchpoints, and, as a consequence, GDB changes the
2483 debug registers in the inferior. To avoid reading back a stale
2484 stopped-data-address when that happens, we cache in LP the fact
2485 that a watchpoint trapped, and the corresponding data address, as
2486 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2487 registers meanwhile, we have the cached data we can rely on. */
2490 check_stopped_by_watchpoint (struct lwp_info *lp)
2492 struct cleanup *old_chain;
2494 if (linux_ops->to_stopped_by_watchpoint == NULL)
2497 old_chain = save_inferior_ptid ();
2498 inferior_ptid = lp->ptid;
2500 if (linux_ops->to_stopped_by_watchpoint (linux_ops))
2502 lp->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
2504 if (linux_ops->to_stopped_data_address != NULL)
2505 lp->stopped_data_address_p =
2506 linux_ops->to_stopped_data_address (¤t_target,
2507 &lp->stopped_data_address);
2509 lp->stopped_data_address_p = 0;
2512 do_cleanups (old_chain);
2514 return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
2517 /* Called when the LWP stopped for a trap that could be explained by a
2518 watchpoint or a breakpoint. */
2521 save_sigtrap (struct lwp_info *lp)
2523 gdb_assert (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON);
2524 gdb_assert (lp->status != 0);
2526 /* Check first if this was a SW/HW breakpoint before checking
2527 watchpoints, because at least s390 can't tell the data address of
2528 hardware watchpoint hits, and the kernel returns
2529 stopped-by-watchpoint as long as there's a watchpoint set. */
2530 if (linux_nat_status_is_event (lp->status))
2531 check_stopped_by_breakpoint (lp);
2533 /* Note that TRAP_HWBKPT can indicate either a hardware breakpoint
2534 or hardware watchpoint. Check which is which if we got
2535 TARGET_STOPPED_BY_HW_BREAKPOINT. */
2536 if (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON
2537 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
2538 check_stopped_by_watchpoint (lp);
2541 /* Returns true if the LWP had stopped for a watchpoint. */
2544 linux_nat_stopped_by_watchpoint (struct target_ops *ops)
2546 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2548 gdb_assert (lp != NULL);
2550 return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
2554 linux_nat_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
2556 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2558 gdb_assert (lp != NULL);
2560 *addr_p = lp->stopped_data_address;
2562 return lp->stopped_data_address_p;
2565 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2568 sigtrap_is_event (int status)
2570 return WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP;
2573 /* Set alternative SIGTRAP-like events recognizer. If
2574 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2578 linux_nat_set_status_is_event (struct target_ops *t,
2579 int (*status_is_event) (int status))
2581 linux_nat_status_is_event = status_is_event;
2584 /* Wait until LP is stopped. */
2587 stop_wait_callback (struct lwp_info *lp, void *data)
2589 struct inferior *inf = find_inferior_ptid (lp->ptid);
2591 /* If this is a vfork parent, bail out, it is not going to report
2592 any SIGSTOP until the vfork is done with. */
2593 if (inf->vfork_child != NULL)
2600 status = wait_lwp (lp);
2604 if (lp->ignore_sigint && WIFSTOPPED (status)
2605 && WSTOPSIG (status) == SIGINT)
2607 lp->ignore_sigint = 0;
2610 ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
2612 if (debug_linux_nat)
2613 fprintf_unfiltered (gdb_stdlog,
2614 "PTRACE_CONT %s, 0, 0 (%s) "
2615 "(discarding SIGINT)\n",
2616 target_pid_to_str (lp->ptid),
2617 errno ? safe_strerror (errno) : "OK");
2619 return stop_wait_callback (lp, NULL);
2622 maybe_clear_ignore_sigint (lp);
2624 if (WSTOPSIG (status) != SIGSTOP)
2626 /* The thread was stopped with a signal other than SIGSTOP. */
2628 if (debug_linux_nat)
2629 fprintf_unfiltered (gdb_stdlog,
2630 "SWC: Pending event %s in %s\n",
2631 status_to_str ((int) status),
2632 target_pid_to_str (lp->ptid));
2634 /* Save the sigtrap event. */
2635 lp->status = status;
2636 gdb_assert (lp->signalled);
2641 /* We caught the SIGSTOP that we intended to catch, so
2642 there's no SIGSTOP pending. */
2644 if (debug_linux_nat)
2645 fprintf_unfiltered (gdb_stdlog,
2646 "SWC: Expected SIGSTOP caught for %s.\n",
2647 target_pid_to_str (lp->ptid));
2649 /* Reset SIGNALLED only after the stop_wait_callback call
2650 above as it does gdb_assert on SIGNALLED. */
2658 /* Return non-zero if LP has a wait status pending. Discard the
2659 pending event and resume the LWP if the event that originally
2660 caused the stop became uninteresting. */
2663 status_callback (struct lwp_info *lp, void *data)
2665 /* Only report a pending wait status if we pretend that this has
2666 indeed been resumed. */
2670 if (!lwp_status_pending_p (lp))
2673 if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
2674 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
2676 struct regcache *regcache = get_thread_regcache (lp->ptid);
2677 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2681 pc = regcache_read_pc (regcache);
2683 if (pc != lp->stop_pc)
2685 if (debug_linux_nat)
2686 fprintf_unfiltered (gdb_stdlog,
2687 "SC: PC of %s changed. was=%s, now=%s\n",
2688 target_pid_to_str (lp->ptid),
2689 paddress (target_gdbarch (), lp->stop_pc),
2690 paddress (target_gdbarch (), pc));
2694 #if !USE_SIGTRAP_SIGINFO
2695 else if (!breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
2697 if (debug_linux_nat)
2698 fprintf_unfiltered (gdb_stdlog,
2699 "SC: previous breakpoint of %s, at %s gone\n",
2700 target_pid_to_str (lp->ptid),
2701 paddress (target_gdbarch (), lp->stop_pc));
2709 if (debug_linux_nat)
2710 fprintf_unfiltered (gdb_stdlog,
2711 "SC: pending event of %s cancelled.\n",
2712 target_pid_to_str (lp->ptid));
2715 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
2723 /* Return non-zero if LP isn't stopped. */
2726 running_callback (struct lwp_info *lp, void *data)
2728 return (!lp->stopped
2729 || (lwp_status_pending_p (lp) && lp->resumed));
2732 /* Count the LWP's that have had events. */
2735 count_events_callback (struct lwp_info *lp, void *data)
2739 gdb_assert (count != NULL);
2741 /* Select only resumed LWPs that have an event pending. */
2742 if (lp->resumed && lwp_status_pending_p (lp))
2748 /* Select the LWP (if any) that is currently being single-stepped. */
2751 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
2753 if (lp->last_resume_kind == resume_step
2760 /* Returns true if LP has a status pending. */
2763 lwp_status_pending_p (struct lwp_info *lp)
2765 /* We check for lp->waitstatus in addition to lp->status, because we
2766 can have pending process exits recorded in lp->status and
2767 W_EXITCODE(0,0) happens to be 0. */
2768 return lp->status != 0 || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE;
2771 /* Select the Nth LWP that has had an event. */
2774 select_event_lwp_callback (struct lwp_info *lp, void *data)
2776 int *selector = data;
2778 gdb_assert (selector != NULL);
2780 /* Select only resumed LWPs that have an event pending. */
2781 if (lp->resumed && lwp_status_pending_p (lp))
2782 if ((*selector)-- == 0)
2788 /* Called when the LWP got a signal/trap that could be explained by a
2789 software or hardware breakpoint. */
2792 check_stopped_by_breakpoint (struct lwp_info *lp)
2794 /* Arrange for a breakpoint to be hit again later. We don't keep
2795 the SIGTRAP status and don't forward the SIGTRAP signal to the
2796 LWP. We will handle the current event, eventually we will resume
2797 this LWP, and this breakpoint will trap again.
2799 If we do not do this, then we run the risk that the user will
2800 delete or disable the breakpoint, but the LWP will have already
2803 struct regcache *regcache = get_thread_regcache (lp->ptid);
2804 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2807 #if USE_SIGTRAP_SIGINFO
2811 pc = regcache_read_pc (regcache);
2812 sw_bp_pc = pc - gdbarch_decr_pc_after_break (gdbarch);
2814 #if USE_SIGTRAP_SIGINFO
2815 if (linux_nat_get_siginfo (lp->ptid, &siginfo))
2817 if (siginfo.si_signo == SIGTRAP)
2819 if (siginfo.si_code == GDB_ARCH_TRAP_BRKPT)
2821 if (debug_linux_nat)
2822 fprintf_unfiltered (gdb_stdlog,
2823 "CSBB: %s stopped by software "
2825 target_pid_to_str (lp->ptid));
2827 /* Back up the PC if necessary. */
2829 regcache_write_pc (regcache, sw_bp_pc);
2831 lp->stop_pc = sw_bp_pc;
2832 lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
2835 else if (siginfo.si_code == TRAP_HWBKPT)
2837 if (debug_linux_nat)
2838 fprintf_unfiltered (gdb_stdlog,
2839 "CSBB: %s stopped by hardware "
2840 "breakpoint/watchpoint\n",
2841 target_pid_to_str (lp->ptid));
2844 lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
2847 else if (siginfo.si_code == TRAP_TRACE)
2849 if (debug_linux_nat)
2850 fprintf_unfiltered (gdb_stdlog,
2851 "CSBB: %s stopped by trace\n",
2852 target_pid_to_str (lp->ptid));
2857 if ((!lp->step || lp->stop_pc == sw_bp_pc)
2858 && software_breakpoint_inserted_here_p (get_regcache_aspace (regcache),
2861 /* The LWP was either continued, or stepped a software
2862 breakpoint instruction. */
2863 if (debug_linux_nat)
2864 fprintf_unfiltered (gdb_stdlog,
2865 "CSBB: %s stopped by software breakpoint\n",
2866 target_pid_to_str (lp->ptid));
2868 /* Back up the PC if necessary. */
2870 regcache_write_pc (regcache, sw_bp_pc);
2872 lp->stop_pc = sw_bp_pc;
2873 lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
2877 if (hardware_breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
2879 if (debug_linux_nat)
2880 fprintf_unfiltered (gdb_stdlog,
2881 "CSBB: stopped by hardware breakpoint %s\n",
2882 target_pid_to_str (lp->ptid));
2885 lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
2894 /* Returns true if the LWP had stopped for a software breakpoint. */
2897 linux_nat_stopped_by_sw_breakpoint (struct target_ops *ops)
2899 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2901 gdb_assert (lp != NULL);
2903 return lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
2906 /* Implement the supports_stopped_by_sw_breakpoint method. */
2909 linux_nat_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
2911 return USE_SIGTRAP_SIGINFO;
2914 /* Returns true if the LWP had stopped for a hardware
2915 breakpoint/watchpoint. */
2918 linux_nat_stopped_by_hw_breakpoint (struct target_ops *ops)
2920 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2922 gdb_assert (lp != NULL);
2924 return lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
2927 /* Implement the supports_stopped_by_hw_breakpoint method. */
2930 linux_nat_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
2932 return USE_SIGTRAP_SIGINFO;
2935 /* Select one LWP out of those that have events pending. */
2938 select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status)
2941 int random_selector;
2942 struct lwp_info *event_lp = NULL;
2944 /* Record the wait status for the original LWP. */
2945 (*orig_lp)->status = *status;
2947 /* In all-stop, give preference to the LWP that is being
2948 single-stepped. There will be at most one, and it will be the
2949 LWP that the core is most interested in. If we didn't do this,
2950 then we'd have to handle pending step SIGTRAPs somehow in case
2951 the core later continues the previously-stepped thread, as
2952 otherwise we'd report the pending SIGTRAP then, and the core, not
2953 having stepped the thread, wouldn't understand what the trap was
2954 for, and therefore would report it to the user as a random
2958 event_lp = iterate_over_lwps (filter,
2959 select_singlestep_lwp_callback, NULL);
2960 if (event_lp != NULL)
2962 if (debug_linux_nat)
2963 fprintf_unfiltered (gdb_stdlog,
2964 "SEL: Select single-step %s\n",
2965 target_pid_to_str (event_lp->ptid));
2969 if (event_lp == NULL)
2971 /* Pick one at random, out of those which have had events. */
2973 /* First see how many events we have. */
2974 iterate_over_lwps (filter, count_events_callback, &num_events);
2975 gdb_assert (num_events > 0);
2977 /* Now randomly pick a LWP out of those that have had
2979 random_selector = (int)
2980 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
2982 if (debug_linux_nat && num_events > 1)
2983 fprintf_unfiltered (gdb_stdlog,
2984 "SEL: Found %d events, selecting #%d\n",
2985 num_events, random_selector);
2987 event_lp = iterate_over_lwps (filter,
2988 select_event_lwp_callback,
2992 if (event_lp != NULL)
2994 /* Switch the event LWP. */
2995 *orig_lp = event_lp;
2996 *status = event_lp->status;
2999 /* Flush the wait status for the event LWP. */
3000 (*orig_lp)->status = 0;
3003 /* Return non-zero if LP has been resumed. */
3006 resumed_callback (struct lwp_info *lp, void *data)
3011 /* Stop an active thread, verify it still exists, then resume it. If
3012 the thread ends up with a pending status, then it is not resumed,
3013 and *DATA (really a pointer to int), is set. */
3016 stop_and_resume_callback (struct lwp_info *lp, void *data)
3020 ptid_t ptid = lp->ptid;
3022 stop_callback (lp, NULL);
3023 stop_wait_callback (lp, NULL);
3025 /* Resume if the lwp still exists, and the core wanted it
3027 lp = find_lwp_pid (ptid);
3030 if (lp->last_resume_kind == resume_stop
3031 && !lwp_status_pending_p (lp))
3033 /* The core wanted the LWP to stop. Even if it stopped
3034 cleanly (with SIGSTOP), leave the event pending. */
3035 if (debug_linux_nat)
3036 fprintf_unfiltered (gdb_stdlog,
3037 "SARC: core wanted LWP %ld stopped "
3038 "(leaving SIGSTOP pending)\n",
3039 ptid_get_lwp (lp->ptid));
3040 lp->status = W_STOPCODE (SIGSTOP);
3043 if (!lwp_status_pending_p (lp))
3045 if (debug_linux_nat)
3046 fprintf_unfiltered (gdb_stdlog,
3047 "SARC: re-resuming LWP %ld\n",
3048 ptid_get_lwp (lp->ptid));
3049 resume_lwp (lp, lp->step, GDB_SIGNAL_0);
3053 if (debug_linux_nat)
3054 fprintf_unfiltered (gdb_stdlog,
3055 "SARC: not re-resuming LWP %ld "
3057 ptid_get_lwp (lp->ptid));
3064 /* Check if we should go on and pass this event to common code.
3065 Return the affected lwp if we are, or NULL otherwise. */
3067 static struct lwp_info *
3068 linux_nat_filter_event (int lwpid, int status)
3070 struct lwp_info *lp;
3071 int event = linux_ptrace_get_extended_event (status);
3073 lp = find_lwp_pid (pid_to_ptid (lwpid));
3075 /* Check for stop events reported by a process we didn't already
3076 know about - anything not already in our LWP list.
3078 If we're expecting to receive stopped processes after
3079 fork, vfork, and clone events, then we'll just add the
3080 new one to our list and go back to waiting for the event
3081 to be reported - the stopped process might be returned
3082 from waitpid before or after the event is.
3084 But note the case of a non-leader thread exec'ing after the
3085 leader having exited, and gone from our lists. The non-leader
3086 thread changes its tid to the tgid. */
3088 if (WIFSTOPPED (status) && lp == NULL
3089 && (WSTOPSIG (status) == SIGTRAP && event == PTRACE_EVENT_EXEC))
3091 /* A multi-thread exec after we had seen the leader exiting. */
3092 if (debug_linux_nat)
3093 fprintf_unfiltered (gdb_stdlog,
3094 "LLW: Re-adding thread group leader LWP %d.\n",
3097 lp = add_lwp (ptid_build (lwpid, lwpid, 0));
3100 add_thread (lp->ptid);
3103 if (WIFSTOPPED (status) && !lp)
3105 if (debug_linux_nat)
3106 fprintf_unfiltered (gdb_stdlog,
3107 "LHEW: saving LWP %ld status %s in stopped_pids list\n",
3108 (long) lwpid, status_to_str (status));
3109 add_to_pid_list (&stopped_pids, lwpid, status);
3113 /* Make sure we don't report an event for the exit of an LWP not in
3114 our list, i.e. not part of the current process. This can happen
3115 if we detach from a program we originally forked and then it
3117 if (!WIFSTOPPED (status) && !lp)
3120 /* This LWP is stopped now. (And if dead, this prevents it from
3121 ever being continued.) */
3124 if (WIFSTOPPED (status) && lp->must_set_ptrace_flags)
3126 struct inferior *inf = find_inferior_pid (ptid_get_pid (lp->ptid));
3127 int options = linux_nat_ptrace_options (inf->attach_flag);
3129 linux_enable_event_reporting (ptid_get_lwp (lp->ptid), options);
3130 lp->must_set_ptrace_flags = 0;
3133 /* Handle GNU/Linux's syscall SIGTRAPs. */
3134 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
3136 /* No longer need the sysgood bit. The ptrace event ends up
3137 recorded in lp->waitstatus if we care for it. We can carry
3138 on handling the event like a regular SIGTRAP from here
3140 status = W_STOPCODE (SIGTRAP);
3141 if (linux_handle_syscall_trap (lp, 0))
3145 /* Handle GNU/Linux's extended waitstatus for trace events. */
3146 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
3147 && linux_is_extended_waitstatus (status))
3149 if (debug_linux_nat)
3150 fprintf_unfiltered (gdb_stdlog,
3151 "LLW: Handling extended status 0x%06x\n",
3153 if (linux_handle_extended_wait (lp, status, 0))
3157 /* Check if the thread has exited. */
3158 if (WIFEXITED (status) || WIFSIGNALED (status))
3160 if (num_lwps (ptid_get_pid (lp->ptid)) > 1)
3162 /* If this is the main thread, we must stop all threads and
3163 verify if they are still alive. This is because in the
3164 nptl thread model on Linux 2.4, there is no signal issued
3165 for exiting LWPs other than the main thread. We only get
3166 the main thread exit signal once all child threads have
3167 already exited. If we stop all the threads and use the
3168 stop_wait_callback to check if they have exited we can
3169 determine whether this signal should be ignored or
3170 whether it means the end of the debugged application,
3171 regardless of which threading model is being used. */
3172 if (ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid))
3174 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp->ptid)),
3175 stop_and_resume_callback, NULL);
3178 if (debug_linux_nat)
3179 fprintf_unfiltered (gdb_stdlog,
3180 "LLW: %s exited.\n",
3181 target_pid_to_str (lp->ptid));
3183 if (num_lwps (ptid_get_pid (lp->ptid)) > 1)
3185 /* If there is at least one more LWP, then the exit signal
3186 was not the end of the debugged application and should be
3193 gdb_assert (lp->resumed);
3195 if (debug_linux_nat)
3196 fprintf_unfiltered (gdb_stdlog,
3197 "Process %ld exited\n",
3198 ptid_get_lwp (lp->ptid));
3200 /* This was the last lwp in the process. Since events are
3201 serialized to GDB core, we may not be able report this one
3202 right now, but GDB core and the other target layers will want
3203 to be notified about the exit code/signal, leave the status
3204 pending for the next time we're able to report it. */
3206 /* Dead LWP's aren't expected to reported a pending sigstop. */
3209 /* Store the pending event in the waitstatus, because
3210 W_EXITCODE(0,0) == 0. */
3211 store_waitstatus (&lp->waitstatus, status);
3215 /* Check if the current LWP has previously exited. In the nptl
3216 thread model, LWPs other than the main thread do not issue
3217 signals when they exit so we must check whenever the thread has
3218 stopped. A similar check is made in stop_wait_callback(). */
3219 if (num_lwps (ptid_get_pid (lp->ptid)) > 1 && !linux_thread_alive (lp->ptid))
3221 ptid_t ptid = pid_to_ptid (ptid_get_pid (lp->ptid));
3223 if (debug_linux_nat)
3224 fprintf_unfiltered (gdb_stdlog,
3225 "LLW: %s exited.\n",
3226 target_pid_to_str (lp->ptid));
3230 /* Make sure there is at least one thread running. */
3231 gdb_assert (iterate_over_lwps (ptid, running_callback, NULL));
3233 /* Discard the event. */
3237 /* Make sure we don't report a SIGSTOP that we sent ourselves in
3238 an attempt to stop an LWP. */
3240 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
3244 if (lp->last_resume_kind == resume_stop)
3246 if (debug_linux_nat)
3247 fprintf_unfiltered (gdb_stdlog,
3248 "LLW: resume_stop SIGSTOP caught for %s.\n",
3249 target_pid_to_str (lp->ptid));
3253 /* This is a delayed SIGSTOP. Filter out the event. */
3255 if (debug_linux_nat)
3256 fprintf_unfiltered (gdb_stdlog,
3257 "LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
3259 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3260 target_pid_to_str (lp->ptid));
3262 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
3263 gdb_assert (lp->resumed);
3268 /* Make sure we don't report a SIGINT that we have already displayed
3269 for another thread. */
3270 if (lp->ignore_sigint
3271 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT)
3273 if (debug_linux_nat)
3274 fprintf_unfiltered (gdb_stdlog,
3275 "LLW: Delayed SIGINT caught for %s.\n",
3276 target_pid_to_str (lp->ptid));
3278 /* This is a delayed SIGINT. */
3279 lp->ignore_sigint = 0;
3281 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
3282 if (debug_linux_nat)
3283 fprintf_unfiltered (gdb_stdlog,
3284 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
3286 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3287 target_pid_to_str (lp->ptid));
3288 gdb_assert (lp->resumed);
3290 /* Discard the event. */
3294 /* Don't report signals that GDB isn't interested in, such as
3295 signals that are neither printed nor stopped upon. Stopping all
3296 threads can be a bit time-consuming so if we want decent
3297 performance with heavily multi-threaded programs, especially when
3298 they're using a high frequency timer, we'd better avoid it if we
3300 if (WIFSTOPPED (status))
3302 enum gdb_signal signo = gdb_signal_from_host (WSTOPSIG (status));
3306 /* Only do the below in all-stop, as we currently use SIGSTOP
3307 to implement target_stop (see linux_nat_stop) in
3309 if (signo == GDB_SIGNAL_INT && signal_pass_state (signo) == 0)
3311 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3312 forwarded to the entire process group, that is, all LWPs
3313 will receive it - unless they're using CLONE_THREAD to
3314 share signals. Since we only want to report it once, we
3315 mark it as ignored for all LWPs except this one. */
3316 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp->ptid)),
3317 set_ignore_sigint, NULL);
3318 lp->ignore_sigint = 0;
3321 maybe_clear_ignore_sigint (lp);
3324 /* When using hardware single-step, we need to report every signal.
3325 Otherwise, signals in pass_mask may be short-circuited
3326 except signals that might be caused by a breakpoint. */
3328 && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status))
3329 && !linux_wstatus_maybe_breakpoint (status))
3331 linux_resume_one_lwp (lp, lp->step, signo);
3332 if (debug_linux_nat)
3333 fprintf_unfiltered (gdb_stdlog,
3334 "LLW: %s %s, %s (preempt 'handle')\n",
3336 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3337 target_pid_to_str (lp->ptid),
3338 (signo != GDB_SIGNAL_0
3339 ? strsignal (gdb_signal_to_host (signo))
3345 /* An interesting event. */
3347 lp->status = status;
3352 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3353 their exits until all other threads in the group have exited. */
3356 check_zombie_leaders (void)
3358 struct inferior *inf;
3362 struct lwp_info *leader_lp;
3367 leader_lp = find_lwp_pid (pid_to_ptid (inf->pid));
3368 if (leader_lp != NULL
3369 /* Check if there are other threads in the group, as we may
3370 have raced with the inferior simply exiting. */
3371 && num_lwps (inf->pid) > 1
3372 && linux_proc_pid_is_zombie (inf->pid))
3374 if (debug_linux_nat)
3375 fprintf_unfiltered (gdb_stdlog,
3376 "CZL: Thread group leader %d zombie "
3377 "(it exited, or another thread execd).\n",
3380 /* A leader zombie can mean one of two things:
3382 - It exited, and there's an exit status pending
3383 available, or only the leader exited (not the whole
3384 program). In the latter case, we can't waitpid the
3385 leader's exit status until all other threads are gone.
3387 - There are 3 or more threads in the group, and a thread
3388 other than the leader exec'd. On an exec, the Linux
3389 kernel destroys all other threads (except the execing
3390 one) in the thread group, and resets the execing thread's
3391 tid to the tgid. No exit notification is sent for the
3392 execing thread -- from the ptracer's perspective, it
3393 appears as though the execing thread just vanishes.
3394 Until we reap all other threads except the leader and the
3395 execing thread, the leader will be zombie, and the
3396 execing thread will be in `D (disc sleep)'. As soon as
3397 all other threads are reaped, the execing thread changes
3398 it's tid to the tgid, and the previous (zombie) leader
3399 vanishes, giving place to the "new" leader. We could try
3400 distinguishing the exit and exec cases, by waiting once
3401 more, and seeing if something comes out, but it doesn't
3402 sound useful. The previous leader _does_ go away, and
3403 we'll re-add the new one once we see the exec event
3404 (which is just the same as what would happen if the
3405 previous leader did exit voluntarily before some other
3408 if (debug_linux_nat)
3409 fprintf_unfiltered (gdb_stdlog,
3410 "CZL: Thread group leader %d vanished.\n",
3412 exit_lwp (leader_lp);
3418 linux_nat_wait_1 (struct target_ops *ops,
3419 ptid_t ptid, struct target_waitstatus *ourstatus,
3423 enum resume_kind last_resume_kind;
3424 struct lwp_info *lp;
3427 if (debug_linux_nat)
3428 fprintf_unfiltered (gdb_stdlog, "LLW: enter\n");
3430 /* The first time we get here after starting a new inferior, we may
3431 not have added it to the LWP list yet - this is the earliest
3432 moment at which we know its PID. */
3433 if (ptid_is_pid (inferior_ptid))
3435 /* Upgrade the main thread's ptid. */
3436 thread_change_ptid (inferior_ptid,
3437 ptid_build (ptid_get_pid (inferior_ptid),
3438 ptid_get_pid (inferior_ptid), 0));
3440 lp = add_initial_lwp (inferior_ptid);
3444 /* Make sure SIGCHLD is blocked until the sigsuspend below. */
3445 block_child_signals (&prev_mask);
3447 /* First check if there is a LWP with a wait status pending. */
3448 lp = iterate_over_lwps (ptid, status_callback, NULL);
3451 if (debug_linux_nat)
3452 fprintf_unfiltered (gdb_stdlog,
3453 "LLW: Using pending wait status %s for %s.\n",
3454 status_to_str (lp->status),
3455 target_pid_to_str (lp->ptid));
3458 if (!target_is_async_p ())
3460 /* Causes SIGINT to be passed on to the attached process. */
3464 /* But if we don't find a pending event, we'll have to wait. Always
3465 pull all events out of the kernel. We'll randomly select an
3466 event LWP out of all that have events, to prevent starvation. */
3472 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3475 - If the thread group leader exits while other threads in the
3476 thread group still exist, waitpid(TGID, ...) hangs. That
3477 waitpid won't return an exit status until the other threads
3478 in the group are reapped.
3480 - When a non-leader thread execs, that thread just vanishes
3481 without reporting an exit (so we'd hang if we waited for it
3482 explicitly in that case). The exec event is reported to
3486 lwpid = my_waitpid (-1, &status, __WCLONE | WNOHANG);
3487 if (lwpid == 0 || (lwpid == -1 && errno == ECHILD))
3488 lwpid = my_waitpid (-1, &status, WNOHANG);
3490 if (debug_linux_nat)
3491 fprintf_unfiltered (gdb_stdlog,
3492 "LNW: waitpid(-1, ...) returned %d, %s\n",
3493 lwpid, errno ? safe_strerror (errno) : "ERRNO-OK");
3497 if (debug_linux_nat)
3499 fprintf_unfiltered (gdb_stdlog,
3500 "LLW: waitpid %ld received %s\n",
3501 (long) lwpid, status_to_str (status));
3504 linux_nat_filter_event (lwpid, status);
3505 /* Retry until nothing comes out of waitpid. A single
3506 SIGCHLD can indicate more than one child stopped. */
3510 /* Now that we've pulled all events out of the kernel, resume
3511 LWPs that don't have an interesting event to report. */
3512 iterate_over_lwps (minus_one_ptid,
3513 resume_stopped_resumed_lwps, &minus_one_ptid);
3515 /* ... and find an LWP with a status to report to the core, if
3517 lp = iterate_over_lwps (ptid, status_callback, NULL);
3521 /* Check for zombie thread group leaders. Those can't be reaped
3522 until all other threads in the thread group are. */
3523 check_zombie_leaders ();
3525 /* If there are no resumed children left, bail. We'd be stuck
3526 forever in the sigsuspend call below otherwise. */
3527 if (iterate_over_lwps (ptid, resumed_callback, NULL) == NULL)
3529 if (debug_linux_nat)
3530 fprintf_unfiltered (gdb_stdlog, "LLW: exit (no resumed LWP)\n");
3532 ourstatus->kind = TARGET_WAITKIND_NO_RESUMED;
3534 if (!target_is_async_p ())
3535 clear_sigint_trap ();
3537 restore_child_signals_mask (&prev_mask);
3538 return minus_one_ptid;
3541 /* No interesting event to report to the core. */
3543 if (target_options & TARGET_WNOHANG)
3545 if (debug_linux_nat)
3546 fprintf_unfiltered (gdb_stdlog, "LLW: exit (ignore)\n");
3548 ourstatus->kind = TARGET_WAITKIND_IGNORE;
3549 restore_child_signals_mask (&prev_mask);
3550 return minus_one_ptid;
3553 /* We shouldn't end up here unless we want to try again. */
3554 gdb_assert (lp == NULL);
3556 /* Block until we get an event reported with SIGCHLD. */
3557 if (debug_linux_nat)
3558 fprintf_unfiltered (gdb_stdlog, "LNW: about to sigsuspend\n");
3559 sigsuspend (&suspend_mask);
3562 if (!target_is_async_p ())
3563 clear_sigint_trap ();
3567 status = lp->status;
3572 /* Now stop all other LWP's ... */
3573 iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
3575 /* ... and wait until all of them have reported back that
3576 they're no longer running. */
3577 iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
3580 /* If we're not waiting for a specific LWP, choose an event LWP from
3581 among those that have had events. Giving equal priority to all
3582 LWPs that have had events helps prevent starvation. */
3583 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
3584 select_event_lwp (ptid, &lp, &status);
3586 gdb_assert (lp != NULL);
3588 /* Now that we've selected our final event LWP, un-adjust its PC if
3589 it was a software breakpoint, and we can't reliably support the
3590 "stopped by software breakpoint" stop reason. */
3591 if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
3592 && !USE_SIGTRAP_SIGINFO)
3594 struct regcache *regcache = get_thread_regcache (lp->ptid);
3595 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3596 int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
3602 pc = regcache_read_pc (regcache);
3603 regcache_write_pc (regcache, pc + decr_pc);
3607 /* We'll need this to determine whether to report a SIGSTOP as
3608 GDB_SIGNAL_0. Need to take a copy because resume_clear_callback
3610 last_resume_kind = lp->last_resume_kind;
3614 /* In all-stop, from the core's perspective, all LWPs are now
3615 stopped until a new resume action is sent over. */
3616 iterate_over_lwps (minus_one_ptid, resume_clear_callback, NULL);
3620 resume_clear_callback (lp, NULL);
3623 if (linux_nat_status_is_event (status))
3625 if (debug_linux_nat)
3626 fprintf_unfiltered (gdb_stdlog,
3627 "LLW: trap ptid is %s.\n",
3628 target_pid_to_str (lp->ptid));
3631 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3633 *ourstatus = lp->waitstatus;
3634 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
3637 store_waitstatus (ourstatus, status);
3639 if (debug_linux_nat)
3640 fprintf_unfiltered (gdb_stdlog, "LLW: exit\n");
3642 restore_child_signals_mask (&prev_mask);
3644 if (last_resume_kind == resume_stop
3645 && ourstatus->kind == TARGET_WAITKIND_STOPPED
3646 && WSTOPSIG (status) == SIGSTOP)
3648 /* A thread that has been requested to stop by GDB with
3649 target_stop, and it stopped cleanly, so report as SIG0. The
3650 use of SIGSTOP is an implementation detail. */
3651 ourstatus->value.sig = GDB_SIGNAL_0;
3654 if (ourstatus->kind == TARGET_WAITKIND_EXITED
3655 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
3658 lp->core = linux_common_core_of_thread (lp->ptid);
3663 /* Resume LWPs that are currently stopped without any pending status
3664 to report, but are resumed from the core's perspective. */
3667 resume_stopped_resumed_lwps (struct lwp_info *lp, void *data)
3669 ptid_t *wait_ptid_p = data;
3673 && !lwp_status_pending_p (lp))
3675 struct regcache *regcache = get_thread_regcache (lp->ptid);
3676 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3680 CORE_ADDR pc = regcache_read_pc (regcache);
3681 int leave_stopped = 0;
3683 /* Don't bother if there's a breakpoint at PC that we'd hit
3684 immediately, and we're not waiting for this LWP. */
3685 if (!ptid_match (lp->ptid, *wait_ptid_p))
3687 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
3693 if (debug_linux_nat)
3694 fprintf_unfiltered (gdb_stdlog,
3695 "RSRL: resuming stopped-resumed LWP %s at "
3697 target_pid_to_str (lp->ptid),
3698 paddress (gdbarch, pc),
3701 linux_resume_one_lwp_throw (lp, lp->step, GDB_SIGNAL_0);
3704 CATCH (ex, RETURN_MASK_ERROR)
3706 if (!check_ptrace_stopped_lwp_gone (lp))
3707 throw_exception (ex);
3716 linux_nat_wait (struct target_ops *ops,
3717 ptid_t ptid, struct target_waitstatus *ourstatus,
3722 if (debug_linux_nat)
3724 char *options_string;
3726 options_string = target_options_to_string (target_options);
3727 fprintf_unfiltered (gdb_stdlog,
3728 "linux_nat_wait: [%s], [%s]\n",
3729 target_pid_to_str (ptid),
3731 xfree (options_string);
3734 /* Flush the async file first. */
3735 if (target_is_async_p ())
3736 async_file_flush ();
3738 /* Resume LWPs that are currently stopped without any pending status
3739 to report, but are resumed from the core's perspective. LWPs get
3740 in this state if we find them stopping at a time we're not
3741 interested in reporting the event (target_wait on a
3742 specific_process, for example, see linux_nat_wait_1), and
3743 meanwhile the event became uninteresting. Don't bother resuming
3744 LWPs we're not going to wait for if they'd stop immediately. */
3746 iterate_over_lwps (minus_one_ptid, resume_stopped_resumed_lwps, &ptid);
3748 event_ptid = linux_nat_wait_1 (ops, ptid, ourstatus, target_options);
3750 /* If we requested any event, and something came out, assume there
3751 may be more. If we requested a specific lwp or process, also
3752 assume there may be more. */
3753 if (target_is_async_p ()
3754 && ((ourstatus->kind != TARGET_WAITKIND_IGNORE
3755 && ourstatus->kind != TARGET_WAITKIND_NO_RESUMED)
3756 || !ptid_equal (ptid, minus_one_ptid)))
3763 kill_callback (struct lwp_info *lp, void *data)
3765 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
3768 kill_lwp (ptid_get_lwp (lp->ptid), SIGKILL);
3769 if (debug_linux_nat)
3771 int save_errno = errno;
3773 fprintf_unfiltered (gdb_stdlog,
3774 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
3775 target_pid_to_str (lp->ptid),
3776 save_errno ? safe_strerror (save_errno) : "OK");
3779 /* Some kernels ignore even SIGKILL for processes under ptrace. */
3782 ptrace (PTRACE_KILL, ptid_get_lwp (lp->ptid), 0, 0);
3783 if (debug_linux_nat)
3785 int save_errno = errno;
3787 fprintf_unfiltered (gdb_stdlog,
3788 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3789 target_pid_to_str (lp->ptid),
3790 save_errno ? safe_strerror (save_errno) : "OK");
3797 kill_wait_callback (struct lwp_info *lp, void *data)
3801 /* We must make sure that there are no pending events (delayed
3802 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3803 program doesn't interfere with any following debugging session. */
3805 /* For cloned processes we must check both with __WCLONE and
3806 without, since the exit status of a cloned process isn't reported
3812 pid = my_waitpid (ptid_get_lwp (lp->ptid), NULL, __WCLONE);
3813 if (pid != (pid_t) -1)
3815 if (debug_linux_nat)
3816 fprintf_unfiltered (gdb_stdlog,
3817 "KWC: wait %s received unknown.\n",
3818 target_pid_to_str (lp->ptid));
3819 /* The Linux kernel sometimes fails to kill a thread
3820 completely after PTRACE_KILL; that goes from the stop
3821 point in do_fork out to the one in
3822 get_signal_to_deliever and waits again. So kill it
3824 kill_callback (lp, NULL);
3827 while (pid == ptid_get_lwp (lp->ptid));
3829 gdb_assert (pid == -1 && errno == ECHILD);
3834 pid = my_waitpid (ptid_get_lwp (lp->ptid), NULL, 0);
3835 if (pid != (pid_t) -1)
3837 if (debug_linux_nat)
3838 fprintf_unfiltered (gdb_stdlog,
3839 "KWC: wait %s received unk.\n",
3840 target_pid_to_str (lp->ptid));
3841 /* See the call to kill_callback above. */
3842 kill_callback (lp, NULL);
3845 while (pid == ptid_get_lwp (lp->ptid));
3847 gdb_assert (pid == -1 && errno == ECHILD);
3852 linux_nat_kill (struct target_ops *ops)
3854 struct target_waitstatus last;
3858 /* If we're stopped while forking and we haven't followed yet,
3859 kill the other task. We need to do this first because the
3860 parent will be sleeping if this is a vfork. */
3862 get_last_target_status (&last_ptid, &last);
3864 if (last.kind == TARGET_WAITKIND_FORKED
3865 || last.kind == TARGET_WAITKIND_VFORKED)
3867 ptrace (PT_KILL, ptid_get_pid (last.value.related_pid), 0, 0);
3870 /* Let the arch-specific native code know this process is
3872 linux_nat_forget_process (ptid_get_pid (last.value.related_pid));
3875 if (forks_exist_p ())
3876 linux_fork_killall ();
3879 ptid_t ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
3881 /* Stop all threads before killing them, since ptrace requires
3882 that the thread is stopped to sucessfully PTRACE_KILL. */
3883 iterate_over_lwps (ptid, stop_callback, NULL);
3884 /* ... and wait until all of them have reported back that
3885 they're no longer running. */
3886 iterate_over_lwps (ptid, stop_wait_callback, NULL);
3888 /* Kill all LWP's ... */
3889 iterate_over_lwps (ptid, kill_callback, NULL);
3891 /* ... and wait until we've flushed all events. */
3892 iterate_over_lwps (ptid, kill_wait_callback, NULL);
3895 target_mourn_inferior ();
3899 linux_nat_mourn_inferior (struct target_ops *ops)
3901 int pid = ptid_get_pid (inferior_ptid);
3903 purge_lwp_list (pid);
3905 if (! forks_exist_p ())
3906 /* Normal case, no other forks available. */
3907 linux_ops->to_mourn_inferior (ops);
3909 /* Multi-fork case. The current inferior_ptid has exited, but
3910 there are other viable forks to debug. Delete the exiting
3911 one and context-switch to the first available. */
3912 linux_fork_mourn_inferior ();
3914 /* Let the arch-specific native code know this process is gone. */
3915 linux_nat_forget_process (pid);
3918 /* Convert a native/host siginfo object, into/from the siginfo in the
3919 layout of the inferiors' architecture. */
3922 siginfo_fixup (siginfo_t *siginfo, gdb_byte *inf_siginfo, int direction)
3926 if (linux_nat_siginfo_fixup != NULL)
3927 done = linux_nat_siginfo_fixup (siginfo, inf_siginfo, direction);
3929 /* If there was no callback, or the callback didn't do anything,
3930 then just do a straight memcpy. */
3934 memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
3936 memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
3940 static enum target_xfer_status
3941 linux_xfer_siginfo (struct target_ops *ops, enum target_object object,
3942 const char *annex, gdb_byte *readbuf,
3943 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
3944 ULONGEST *xfered_len)
3948 gdb_byte inf_siginfo[sizeof (siginfo_t)];
3950 gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO);
3951 gdb_assert (readbuf || writebuf);
3953 pid = ptid_get_lwp (inferior_ptid);
3955 pid = ptid_get_pid (inferior_ptid);
3957 if (offset > sizeof (siginfo))
3958 return TARGET_XFER_E_IO;
3961 ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
3963 return TARGET_XFER_E_IO;
3965 /* When GDB is built as a 64-bit application, ptrace writes into
3966 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3967 inferior with a 64-bit GDB should look the same as debugging it
3968 with a 32-bit GDB, we need to convert it. GDB core always sees
3969 the converted layout, so any read/write will have to be done
3971 siginfo_fixup (&siginfo, inf_siginfo, 0);
3973 if (offset + len > sizeof (siginfo))
3974 len = sizeof (siginfo) - offset;
3976 if (readbuf != NULL)
3977 memcpy (readbuf, inf_siginfo + offset, len);
3980 memcpy (inf_siginfo + offset, writebuf, len);
3982 /* Convert back to ptrace layout before flushing it out. */
3983 siginfo_fixup (&siginfo, inf_siginfo, 1);
3986 ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
3988 return TARGET_XFER_E_IO;
3992 return TARGET_XFER_OK;
3995 static enum target_xfer_status
3996 linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
3997 const char *annex, gdb_byte *readbuf,
3998 const gdb_byte *writebuf,
3999 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
4001 struct cleanup *old_chain;
4002 enum target_xfer_status xfer;
4004 if (object == TARGET_OBJECT_SIGNAL_INFO)
4005 return linux_xfer_siginfo (ops, object, annex, readbuf, writebuf,
4006 offset, len, xfered_len);
4008 /* The target is connected but no live inferior is selected. Pass
4009 this request down to a lower stratum (e.g., the executable
4011 if (object == TARGET_OBJECT_MEMORY && ptid_equal (inferior_ptid, null_ptid))
4012 return TARGET_XFER_EOF;
4014 old_chain = save_inferior_ptid ();
4016 if (ptid_lwp_p (inferior_ptid))
4017 inferior_ptid = pid_to_ptid (ptid_get_lwp (inferior_ptid));
4019 xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
4020 offset, len, xfered_len);
4022 do_cleanups (old_chain);
4027 linux_thread_alive (ptid_t ptid)
4031 gdb_assert (ptid_lwp_p (ptid));
4033 /* Send signal 0 instead of anything ptrace, because ptracing a
4034 running thread errors out claiming that the thread doesn't
4036 err = kill_lwp (ptid_get_lwp (ptid), 0);
4038 if (debug_linux_nat)
4039 fprintf_unfiltered (gdb_stdlog,
4040 "LLTA: KILL(SIG0) %s (%s)\n",
4041 target_pid_to_str (ptid),
4042 err ? safe_strerror (tmp_errno) : "OK");
4051 linux_nat_thread_alive (struct target_ops *ops, ptid_t ptid)
4053 return linux_thread_alive (ptid);
4056 /* Implement the to_update_thread_list target method for this
4060 linux_nat_update_thread_list (struct target_ops *ops)
4062 if (linux_supports_traceclone ())
4064 /* With support for clone events, we add/delete threads from the
4065 list as clone/exit events are processed, so just try deleting
4066 exited threads still in the thread list. */
4067 delete_exited_threads ();
4074 linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
4076 static char buf[64];
4078 if (ptid_lwp_p (ptid)
4079 && (ptid_get_pid (ptid) != ptid_get_lwp (ptid)
4080 || num_lwps (ptid_get_pid (ptid)) > 1))
4082 snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
4086 return normal_pid_to_str (ptid);
4090 linux_nat_thread_name (struct target_ops *self, struct thread_info *thr)
4092 int pid = ptid_get_pid (thr->ptid);
4093 long lwp = ptid_get_lwp (thr->ptid);
4094 #define FORMAT "/proc/%d/task/%ld/comm"
4095 char buf[sizeof (FORMAT) + 30];
4097 char *result = NULL;
4099 snprintf (buf, sizeof (buf), FORMAT, pid, lwp);
4100 comm_file = gdb_fopen_cloexec (buf, "r");
4103 /* Not exported by the kernel, so we define it here. */
4105 static char line[COMM_LEN + 1];
4107 if (fgets (line, sizeof (line), comm_file))
4109 char *nl = strchr (line, '\n');
4126 /* Accepts an integer PID; Returns a string representing a file that
4127 can be opened to get the symbols for the child process. */
4130 linux_child_pid_to_exec_file (struct target_ops *self, int pid)
4132 return linux_proc_pid_to_exec_file (pid);
4135 /* Implement the to_xfer_partial interface for memory reads using the /proc
4136 filesystem. Because we can use a single read() call for /proc, this
4137 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4138 but it doesn't support writes. */
4140 static enum target_xfer_status
4141 linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
4142 const char *annex, gdb_byte *readbuf,
4143 const gdb_byte *writebuf,
4144 ULONGEST offset, LONGEST len, ULONGEST *xfered_len)
4150 if (object != TARGET_OBJECT_MEMORY || !readbuf)
4153 /* Don't bother for one word. */
4154 if (len < 3 * sizeof (long))
4155 return TARGET_XFER_EOF;
4157 /* We could keep this file open and cache it - possibly one per
4158 thread. That requires some juggling, but is even faster. */
4159 xsnprintf (filename, sizeof filename, "/proc/%d/mem",
4160 ptid_get_pid (inferior_ptid));
4161 fd = gdb_open_cloexec (filename, O_RDONLY | O_LARGEFILE, 0);
4163 return TARGET_XFER_EOF;
4165 /* If pread64 is available, use it. It's faster if the kernel
4166 supports it (only one syscall), and it's 64-bit safe even on
4167 32-bit platforms (for instance, SPARC debugging a SPARC64
4170 if (pread64 (fd, readbuf, len, offset) != len)
4172 if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
4181 return TARGET_XFER_EOF;
4185 return TARGET_XFER_OK;
4190 /* Enumerate spufs IDs for process PID. */
4192 spu_enumerate_spu_ids (int pid, gdb_byte *buf, ULONGEST offset, ULONGEST len)
4194 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
4196 LONGEST written = 0;
4199 struct dirent *entry;
4201 xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
4202 dir = opendir (path);
4207 while ((entry = readdir (dir)) != NULL)
4213 fd = atoi (entry->d_name);
4217 xsnprintf (path, sizeof path, "/proc/%d/fd/%d", pid, fd);
4218 if (stat (path, &st) != 0)
4220 if (!S_ISDIR (st.st_mode))
4223 if (statfs (path, &stfs) != 0)
4225 if (stfs.f_type != SPUFS_MAGIC)
4228 if (pos >= offset && pos + 4 <= offset + len)
4230 store_unsigned_integer (buf + pos - offset, 4, byte_order, fd);
4240 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4241 object type, using the /proc file system. */
4243 static enum target_xfer_status
4244 linux_proc_xfer_spu (struct target_ops *ops, enum target_object object,
4245 const char *annex, gdb_byte *readbuf,
4246 const gdb_byte *writebuf,
4247 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
4252 int pid = ptid_get_pid (inferior_ptid);
4257 return TARGET_XFER_E_IO;
4260 LONGEST l = spu_enumerate_spu_ids (pid, readbuf, offset, len);
4263 return TARGET_XFER_E_IO;
4265 return TARGET_XFER_EOF;
4268 *xfered_len = (ULONGEST) l;
4269 return TARGET_XFER_OK;
4274 xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
4275 fd = gdb_open_cloexec (buf, writebuf? O_WRONLY : O_RDONLY, 0);
4277 return TARGET_XFER_E_IO;
4280 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
4283 return TARGET_XFER_EOF;
4287 ret = write (fd, writebuf, (size_t) len);
4289 ret = read (fd, readbuf, (size_t) len);
4294 return TARGET_XFER_E_IO;
4296 return TARGET_XFER_EOF;
4299 *xfered_len = (ULONGEST) ret;
4300 return TARGET_XFER_OK;
4305 /* Parse LINE as a signal set and add its set bits to SIGS. */
4308 add_line_to_sigset (const char *line, sigset_t *sigs)
4310 int len = strlen (line) - 1;
4314 if (line[len] != '\n')
4315 error (_("Could not parse signal set: %s"), line);
4323 if (*p >= '0' && *p <= '9')
4325 else if (*p >= 'a' && *p <= 'f')
4326 digit = *p - 'a' + 10;
4328 error (_("Could not parse signal set: %s"), line);
4333 sigaddset (sigs, signum + 1);
4335 sigaddset (sigs, signum + 2);
4337 sigaddset (sigs, signum + 3);
4339 sigaddset (sigs, signum + 4);
4345 /* Find process PID's pending signals from /proc/pid/status and set
4349 linux_proc_pending_signals (int pid, sigset_t *pending,
4350 sigset_t *blocked, sigset_t *ignored)
4353 char buffer[PATH_MAX], fname[PATH_MAX];
4354 struct cleanup *cleanup;
4356 sigemptyset (pending);
4357 sigemptyset (blocked);
4358 sigemptyset (ignored);
4359 xsnprintf (fname, sizeof fname, "/proc/%d/status", pid);
4360 procfile = gdb_fopen_cloexec (fname, "r");
4361 if (procfile == NULL)
4362 error (_("Could not open %s"), fname);
4363 cleanup = make_cleanup_fclose (procfile);
4365 while (fgets (buffer, PATH_MAX, procfile) != NULL)
4367 /* Normal queued signals are on the SigPnd line in the status
4368 file. However, 2.6 kernels also have a "shared" pending
4369 queue for delivering signals to a thread group, so check for
4372 Unfortunately some Red Hat kernels include the shared pending
4373 queue but not the ShdPnd status field. */
4375 if (startswith (buffer, "SigPnd:\t"))
4376 add_line_to_sigset (buffer + 8, pending);
4377 else if (startswith (buffer, "ShdPnd:\t"))
4378 add_line_to_sigset (buffer + 8, pending);
4379 else if (startswith (buffer, "SigBlk:\t"))
4380 add_line_to_sigset (buffer + 8, blocked);
4381 else if (startswith (buffer, "SigIgn:\t"))
4382 add_line_to_sigset (buffer + 8, ignored);
4385 do_cleanups (cleanup);
4388 static enum target_xfer_status
4389 linux_nat_xfer_osdata (struct target_ops *ops, enum target_object object,
4390 const char *annex, gdb_byte *readbuf,
4391 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
4392 ULONGEST *xfered_len)
4394 gdb_assert (object == TARGET_OBJECT_OSDATA);
4396 *xfered_len = linux_common_xfer_osdata (annex, readbuf, offset, len);
4397 if (*xfered_len == 0)
4398 return TARGET_XFER_EOF;
4400 return TARGET_XFER_OK;
4403 static enum target_xfer_status
4404 linux_xfer_partial (struct target_ops *ops, enum target_object object,
4405 const char *annex, gdb_byte *readbuf,
4406 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
4407 ULONGEST *xfered_len)
4409 enum target_xfer_status xfer;
4411 if (object == TARGET_OBJECT_AUXV)
4412 return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
4413 offset, len, xfered_len);
4415 if (object == TARGET_OBJECT_OSDATA)
4416 return linux_nat_xfer_osdata (ops, object, annex, readbuf, writebuf,
4417 offset, len, xfered_len);
4419 if (object == TARGET_OBJECT_SPU)
4420 return linux_proc_xfer_spu (ops, object, annex, readbuf, writebuf,
4421 offset, len, xfered_len);
4423 /* GDB calculates all the addresses in possibly larget width of the address.
4424 Address width needs to be masked before its final use - either by
4425 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4427 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4429 if (object == TARGET_OBJECT_MEMORY)
4431 int addr_bit = gdbarch_addr_bit (target_gdbarch ());
4433 if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
4434 offset &= ((ULONGEST) 1 << addr_bit) - 1;
4437 xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
4438 offset, len, xfered_len);
4439 if (xfer != TARGET_XFER_EOF)
4442 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
4443 offset, len, xfered_len);
4447 cleanup_target_stop (void *arg)
4449 ptid_t *ptid = (ptid_t *) arg;
4451 gdb_assert (arg != NULL);
4454 target_resume (*ptid, 0, GDB_SIGNAL_0);
4457 static VEC(static_tracepoint_marker_p) *
4458 linux_child_static_tracepoint_markers_by_strid (struct target_ops *self,
4461 char s[IPA_CMD_BUF_SIZE];
4462 struct cleanup *old_chain;
4463 int pid = ptid_get_pid (inferior_ptid);
4464 VEC(static_tracepoint_marker_p) *markers = NULL;
4465 struct static_tracepoint_marker *marker = NULL;
4467 ptid_t ptid = ptid_build (pid, 0, 0);
4472 memcpy (s, "qTfSTM", sizeof ("qTfSTM"));
4473 s[sizeof ("qTfSTM")] = 0;
4475 agent_run_command (pid, s, strlen (s) + 1);
4477 old_chain = make_cleanup (free_current_marker, &marker);
4478 make_cleanup (cleanup_target_stop, &ptid);
4483 marker = XCNEW (struct static_tracepoint_marker);
4487 parse_static_tracepoint_marker_definition (p, &p, marker);
4489 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
4491 VEC_safe_push (static_tracepoint_marker_p,
4497 release_static_tracepoint_marker (marker);
4498 memset (marker, 0, sizeof (*marker));
4501 while (*p++ == ','); /* comma-separated list */
4503 memcpy (s, "qTsSTM", sizeof ("qTsSTM"));
4504 s[sizeof ("qTsSTM")] = 0;
4505 agent_run_command (pid, s, strlen (s) + 1);
4509 do_cleanups (old_chain);
4514 /* Create a prototype generic GNU/Linux target. The client can override
4515 it with local methods. */
4518 linux_target_install_ops (struct target_ops *t)
4520 t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
4521 t->to_remove_fork_catchpoint = linux_child_remove_fork_catchpoint;
4522 t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
4523 t->to_remove_vfork_catchpoint = linux_child_remove_vfork_catchpoint;
4524 t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
4525 t->to_remove_exec_catchpoint = linux_child_remove_exec_catchpoint;
4526 t->to_set_syscall_catchpoint = linux_child_set_syscall_catchpoint;
4527 t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
4528 t->to_post_startup_inferior = linux_child_post_startup_inferior;
4529 t->to_post_attach = linux_child_post_attach;
4530 t->to_follow_fork = linux_child_follow_fork;
4532 super_xfer_partial = t->to_xfer_partial;
4533 t->to_xfer_partial = linux_xfer_partial;
4535 t->to_static_tracepoint_markers_by_strid
4536 = linux_child_static_tracepoint_markers_by_strid;
4542 struct target_ops *t;
4544 t = inf_ptrace_target ();
4545 linux_target_install_ops (t);
4551 linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
4553 struct target_ops *t;
4555 t = inf_ptrace_trad_target (register_u_offset);
4556 linux_target_install_ops (t);
4561 /* target_is_async_p implementation. */
4564 linux_nat_is_async_p (struct target_ops *ops)
4566 return linux_is_async_p ();
4569 /* target_can_async_p implementation. */
4572 linux_nat_can_async_p (struct target_ops *ops)
4574 /* NOTE: palves 2008-03-21: We're only async when the user requests
4575 it explicitly with the "set target-async" command.
4576 Someday, linux will always be async. */
4577 return target_async_permitted;
4581 linux_nat_supports_non_stop (struct target_ops *self)
4586 /* True if we want to support multi-process. To be removed when GDB
4587 supports multi-exec. */
4589 int linux_multi_process = 1;
4592 linux_nat_supports_multi_process (struct target_ops *self)
4594 return linux_multi_process;
4598 linux_nat_supports_disable_randomization (struct target_ops *self)
4600 #ifdef HAVE_PERSONALITY
4607 static int async_terminal_is_ours = 1;
4609 /* target_terminal_inferior implementation.
4611 This is a wrapper around child_terminal_inferior to add async support. */
4614 linux_nat_terminal_inferior (struct target_ops *self)
4616 /* Like target_terminal_inferior, use target_can_async_p, not
4617 target_is_async_p, since at this point the target is not async
4618 yet. If it can async, then we know it will become async prior to
4620 if (!target_can_async_p ())
4622 /* Async mode is disabled. */
4623 child_terminal_inferior (self);
4627 child_terminal_inferior (self);
4629 /* Calls to target_terminal_*() are meant to be idempotent. */
4630 if (!async_terminal_is_ours)
4633 delete_file_handler (input_fd);
4634 async_terminal_is_ours = 0;
4638 /* target_terminal_ours implementation.
4640 This is a wrapper around child_terminal_ours to add async support (and
4641 implement the target_terminal_ours vs target_terminal_ours_for_output
4642 distinction). child_terminal_ours is currently no different than
4643 child_terminal_ours_for_output.
4644 We leave target_terminal_ours_for_output alone, leaving it to
4645 child_terminal_ours_for_output. */
4648 linux_nat_terminal_ours (struct target_ops *self)
4650 /* GDB should never give the terminal to the inferior if the
4651 inferior is running in the background (run&, continue&, etc.),
4652 but claiming it sure should. */
4653 child_terminal_ours (self);
4655 if (async_terminal_is_ours)
4658 clear_sigint_trap ();
4659 add_file_handler (input_fd, stdin_event_handler, 0);
4660 async_terminal_is_ours = 1;
4663 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4664 so we notice when any child changes state, and notify the
4665 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4666 above to wait for the arrival of a SIGCHLD. */
4669 sigchld_handler (int signo)
4671 int old_errno = errno;
4673 if (debug_linux_nat)
4674 ui_file_write_async_safe (gdb_stdlog,
4675 "sigchld\n", sizeof ("sigchld\n") - 1);
4677 if (signo == SIGCHLD
4678 && linux_nat_event_pipe[0] != -1)
4679 async_file_mark (); /* Let the event loop know that there are
4680 events to handle. */
4685 /* Callback registered with the target events file descriptor. */
4688 handle_target_event (int error, gdb_client_data client_data)
4690 inferior_event_handler (INF_REG_EVENT, NULL);
4693 /* Create/destroy the target events pipe. Returns previous state. */
4696 linux_async_pipe (int enable)
4698 int previous = linux_is_async_p ();
4700 if (previous != enable)
4704 /* Block child signals while we create/destroy the pipe, as
4705 their handler writes to it. */
4706 block_child_signals (&prev_mask);
4710 if (gdb_pipe_cloexec (linux_nat_event_pipe) == -1)
4711 internal_error (__FILE__, __LINE__,
4712 "creating event pipe failed.");
4714 fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
4715 fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
4719 close (linux_nat_event_pipe[0]);
4720 close (linux_nat_event_pipe[1]);
4721 linux_nat_event_pipe[0] = -1;
4722 linux_nat_event_pipe[1] = -1;
4725 restore_child_signals_mask (&prev_mask);
4731 /* target_async implementation. */
4734 linux_nat_async (struct target_ops *ops, int enable)
4738 if (!linux_async_pipe (1))
4740 add_file_handler (linux_nat_event_pipe[0],
4741 handle_target_event, NULL);
4742 /* There may be pending events to handle. Tell the event loop
4749 delete_file_handler (linux_nat_event_pipe[0]);
4750 linux_async_pipe (0);
4755 /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
4759 linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
4763 if (debug_linux_nat)
4764 fprintf_unfiltered (gdb_stdlog,
4765 "LNSL: running -> suspending %s\n",
4766 target_pid_to_str (lwp->ptid));
4769 if (lwp->last_resume_kind == resume_stop)
4771 if (debug_linux_nat)
4772 fprintf_unfiltered (gdb_stdlog,
4773 "linux-nat: already stopping LWP %ld at "
4775 ptid_get_lwp (lwp->ptid));
4779 stop_callback (lwp, NULL);
4780 lwp->last_resume_kind = resume_stop;
4784 /* Already known to be stopped; do nothing. */
4786 if (debug_linux_nat)
4788 if (find_thread_ptid (lwp->ptid)->stop_requested)
4789 fprintf_unfiltered (gdb_stdlog,
4790 "LNSL: already stopped/stop_requested %s\n",
4791 target_pid_to_str (lwp->ptid));
4793 fprintf_unfiltered (gdb_stdlog,
4794 "LNSL: already stopped/no "
4795 "stop_requested yet %s\n",
4796 target_pid_to_str (lwp->ptid));
4803 linux_nat_stop (struct target_ops *self, ptid_t ptid)
4806 iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
4808 linux_ops->to_stop (linux_ops, ptid);
4812 linux_nat_close (struct target_ops *self)
4814 /* Unregister from the event loop. */
4815 if (linux_nat_is_async_p (self))
4816 linux_nat_async (self, 0);
4818 if (linux_ops->to_close)
4819 linux_ops->to_close (linux_ops);
4824 /* When requests are passed down from the linux-nat layer to the
4825 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
4826 used. The address space pointer is stored in the inferior object,
4827 but the common code that is passed such ptid can't tell whether
4828 lwpid is a "main" process id or not (it assumes so). We reverse
4829 look up the "main" process id from the lwp here. */
4831 static struct address_space *
4832 linux_nat_thread_address_space (struct target_ops *t, ptid_t ptid)
4834 struct lwp_info *lwp;
4835 struct inferior *inf;
4838 if (ptid_get_lwp (ptid) == 0)
4840 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
4842 lwp = find_lwp_pid (ptid);
4843 pid = ptid_get_pid (lwp->ptid);
4847 /* A (pid,lwpid,0) ptid. */
4848 pid = ptid_get_pid (ptid);
4851 inf = find_inferior_pid (pid);
4852 gdb_assert (inf != NULL);
4856 /* Return the cached value of the processor core for thread PTID. */
4859 linux_nat_core_of_thread (struct target_ops *ops, ptid_t ptid)
4861 struct lwp_info *info = find_lwp_pid (ptid);
4869 linux_nat_add_target (struct target_ops *t)
4871 /* Save the provided single-threaded target. We save this in a separate
4872 variable because another target we've inherited from (e.g. inf-ptrace)
4873 may have saved a pointer to T; we want to use it for the final
4874 process stratum target. */
4875 linux_ops_saved = *t;
4876 linux_ops = &linux_ops_saved;
4878 /* Override some methods for multithreading. */
4879 t->to_create_inferior = linux_nat_create_inferior;
4880 t->to_attach = linux_nat_attach;
4881 t->to_detach = linux_nat_detach;
4882 t->to_resume = linux_nat_resume;
4883 t->to_wait = linux_nat_wait;
4884 t->to_pass_signals = linux_nat_pass_signals;
4885 t->to_xfer_partial = linux_nat_xfer_partial;
4886 t->to_kill = linux_nat_kill;
4887 t->to_mourn_inferior = linux_nat_mourn_inferior;
4888 t->to_thread_alive = linux_nat_thread_alive;
4889 t->to_update_thread_list = linux_nat_update_thread_list;
4890 t->to_pid_to_str = linux_nat_pid_to_str;
4891 t->to_thread_name = linux_nat_thread_name;
4892 t->to_has_thread_control = tc_schedlock;
4893 t->to_thread_address_space = linux_nat_thread_address_space;
4894 t->to_stopped_by_watchpoint = linux_nat_stopped_by_watchpoint;
4895 t->to_stopped_data_address = linux_nat_stopped_data_address;
4896 t->to_stopped_by_sw_breakpoint = linux_nat_stopped_by_sw_breakpoint;
4897 t->to_supports_stopped_by_sw_breakpoint = linux_nat_supports_stopped_by_sw_breakpoint;
4898 t->to_stopped_by_hw_breakpoint = linux_nat_stopped_by_hw_breakpoint;
4899 t->to_supports_stopped_by_hw_breakpoint = linux_nat_supports_stopped_by_hw_breakpoint;
4901 t->to_can_async_p = linux_nat_can_async_p;
4902 t->to_is_async_p = linux_nat_is_async_p;
4903 t->to_supports_non_stop = linux_nat_supports_non_stop;
4904 t->to_async = linux_nat_async;
4905 t->to_terminal_inferior = linux_nat_terminal_inferior;
4906 t->to_terminal_ours = linux_nat_terminal_ours;
4908 super_close = t->to_close;
4909 t->to_close = linux_nat_close;
4911 /* Methods for non-stop support. */
4912 t->to_stop = linux_nat_stop;
4914 t->to_supports_multi_process = linux_nat_supports_multi_process;
4916 t->to_supports_disable_randomization
4917 = linux_nat_supports_disable_randomization;
4919 t->to_core_of_thread = linux_nat_core_of_thread;
4921 /* We don't change the stratum; this target will sit at
4922 process_stratum and thread_db will set at thread_stratum. This
4923 is a little strange, since this is a multi-threaded-capable
4924 target, but we want to be on the stack below thread_db, and we
4925 also want to be used for single-threaded processes. */
4930 /* Register a method to call whenever a new thread is attached. */
4932 linux_nat_set_new_thread (struct target_ops *t,
4933 void (*new_thread) (struct lwp_info *))
4935 /* Save the pointer. We only support a single registered instance
4936 of the GNU/Linux native target, so we do not need to map this to
4938 linux_nat_new_thread = new_thread;
4941 /* See declaration in linux-nat.h. */
4944 linux_nat_set_new_fork (struct target_ops *t,
4945 linux_nat_new_fork_ftype *new_fork)
4947 /* Save the pointer. */
4948 linux_nat_new_fork = new_fork;
4951 /* See declaration in linux-nat.h. */
4954 linux_nat_set_forget_process (struct target_ops *t,
4955 linux_nat_forget_process_ftype *fn)
4957 /* Save the pointer. */
4958 linux_nat_forget_process_hook = fn;
4961 /* See declaration in linux-nat.h. */
4964 linux_nat_forget_process (pid_t pid)
4966 if (linux_nat_forget_process_hook != NULL)
4967 linux_nat_forget_process_hook (pid);
4970 /* Register a method that converts a siginfo object between the layout
4971 that ptrace returns, and the layout in the architecture of the
4974 linux_nat_set_siginfo_fixup (struct target_ops *t,
4975 int (*siginfo_fixup) (siginfo_t *,
4979 /* Save the pointer. */
4980 linux_nat_siginfo_fixup = siginfo_fixup;
4983 /* Register a method to call prior to resuming a thread. */
4986 linux_nat_set_prepare_to_resume (struct target_ops *t,
4987 void (*prepare_to_resume) (struct lwp_info *))
4989 /* Save the pointer. */
4990 linux_nat_prepare_to_resume = prepare_to_resume;
4993 /* See linux-nat.h. */
4996 linux_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo)
5000 pid = ptid_get_lwp (ptid);
5002 pid = ptid_get_pid (ptid);
5005 ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, siginfo);
5008 memset (siginfo, 0, sizeof (*siginfo));
5014 /* See nat/linux-nat.h. */
5017 current_lwp_ptid (void)
5019 gdb_assert (ptid_lwp_p (inferior_ptid));
5020 return inferior_ptid;
5023 /* Provide a prototype to silence -Wmissing-prototypes. */
5024 extern initialize_file_ftype _initialize_linux_nat;
5027 _initialize_linux_nat (void)
5029 add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance,
5030 &debug_linux_nat, _("\
5031 Set debugging of GNU/Linux lwp module."), _("\
5032 Show debugging of GNU/Linux lwp module."), _("\
5033 Enables printf debugging output."),
5035 show_debug_linux_nat,
5036 &setdebuglist, &showdebuglist);
5038 /* Save this mask as the default. */
5039 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
5041 /* Install a SIGCHLD handler. */
5042 sigchld_action.sa_handler = sigchld_handler;
5043 sigemptyset (&sigchld_action.sa_mask);
5044 sigchld_action.sa_flags = SA_RESTART;
5046 /* Make it the default. */
5047 sigaction (SIGCHLD, &sigchld_action, NULL);
5049 /* Make sure we don't block SIGCHLD during a sigsuspend. */
5050 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
5051 sigdelset (&suspend_mask, SIGCHLD);
5053 sigemptyset (&blocked_mask);
5057 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
5058 the GNU/Linux Threads library and therefore doesn't really belong
5061 /* Read variable NAME in the target and return its value if found.
5062 Otherwise return zero. It is assumed that the type of the variable
5066 get_signo (const char *name)
5068 struct bound_minimal_symbol ms;
5071 ms = lookup_minimal_symbol (name, NULL, NULL);
5072 if (ms.minsym == NULL)
5075 if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms), (gdb_byte *) &signo,
5076 sizeof (signo)) != 0)
5082 /* Return the set of signals used by the threads library in *SET. */
5085 lin_thread_get_thread_signals (sigset_t *set)
5087 struct sigaction action;
5088 int restart, cancel;
5090 sigemptyset (&blocked_mask);
5093 restart = get_signo ("__pthread_sig_restart");
5094 cancel = get_signo ("__pthread_sig_cancel");
5096 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5097 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5098 not provide any way for the debugger to query the signal numbers -
5099 fortunately they don't change! */
5102 restart = __SIGRTMIN;
5105 cancel = __SIGRTMIN + 1;
5107 sigaddset (set, restart);
5108 sigaddset (set, cancel);
5110 /* The GNU/Linux Threads library makes terminating threads send a
5111 special "cancel" signal instead of SIGCHLD. Make sure we catch
5112 those (to prevent them from terminating GDB itself, which is
5113 likely to be their default action) and treat them the same way as
5116 action.sa_handler = sigchld_handler;
5117 sigemptyset (&action.sa_mask);
5118 action.sa_flags = SA_RESTART;
5119 sigaction (cancel, &action, NULL);
5121 /* We block the "cancel" signal throughout this code ... */
5122 sigaddset (&blocked_mask, cancel);
5123 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
5125 /* ... except during a sigsuspend. */
5126 sigdelset (&suspend_mask, cancel);