1 /* Multi-threaded debugging support for Linux (LWP layer).
2 Copyright 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "gdb_assert.h"
26 #include <sys/ptrace.h>
29 #include "gdbthread.h"
35 static int debug_lin_lwp;
36 extern const char *strsignal (int sig);
38 /* On Linux there are no real LWP's. The closest thing to LWP's are
39 processes sharing the same VM space. A multi-threaded process is
40 basically a group of such processes. However, such a grouping is
41 almost entirely a user-space issue; the kernel doesn't enforce such
42 a grouping at all (this might change in the future). In general,
43 we'll rely on the threads library (i.e. the LinuxThreads library)
44 to provide such a grouping.
46 It is perfectly well possible to write a multi-threaded application
47 without the assistance of a threads library, by using the clone
48 system call directly. This module should be able to give some
49 rudimentary support for debugging such applications if developers
50 specify the CLONE_PTRACE flag in the clone system call, and are
51 using Linux 2.4 or above.
53 Note that there are some peculiarities in Linux that affect this
56 - In general one should specify the __WCLONE flag to waitpid in
57 order to make it report events for any of the cloned processes
58 (and leave it out for the initial process). However, if a cloned
59 process has exited the exit status is only reported if the
60 __WCLONE flag is absent. Linux 2.4 has a __WALL flag, but we
61 cannot use it since GDB must work on older systems too.
63 - When a traced, cloned process exits and is waited for by the
64 debugger, the kernel reassigns it to the original parent and
65 keeps it around as a "zombie". Somehow, the LinuxThreads library
66 doesn't notice this, which leads to the "zombie problem": When
67 debugged a multi-threaded process that spawns a lot of threads
68 will run out of processes, even if the threads exit, because the
69 "zombies" stay around. */
71 /* Structure describing a LWP. */
74 /* The process id of the LWP. This is a combination of the LWP id
75 and overall process id. */
78 /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report
82 /* Non-zero if this LWP is stopped. */
85 /* Non-zero if this LWP will be/has been resumed. Note that an LWP
86 can be marked both as stopped and resumed at the same time. This
87 happens if we try to resume an LWP that has a wait status
88 pending. We shouldn't let the LWP run until that wait status has
89 been processed, but we should not report that wait status if GDB
90 didn't try to let the LWP run. */
93 /* If non-zero, a pending wait status. */
96 /* Non-zero if we were stepping this LWP. */
99 /* Next LWP in list. */
100 struct lwp_info *next;
103 /* List of known LWPs. */
104 static struct lwp_info *lwp_list;
106 /* Number of LWPs in the list. */
109 /* Non-zero if we're running in "threaded" mode. */
113 #define GET_LWP(ptid) ptid_get_lwp (ptid)
114 #define GET_PID(ptid) ptid_get_pid (ptid)
115 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
116 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
118 #define is_cloned(pid) (GET_LWP (pid) != GET_PID (pid))
120 /* If the last reported event was a SIGTRAP, this variable is set to
121 the process id of the LWP/thread that got it. */
125 /* This module's target-specific operations. */
126 static struct target_ops lin_lwp_ops;
128 /* The standard child operations. */
129 extern struct target_ops child_ops;
131 /* Since we cannot wait (in lin_lwp_wait) for the initial process and
132 any cloned processes with a single call to waitpid, we have to use
133 the WNOHANG flag and call waitpid in a loop. To optimize
134 things a bit we use `sigsuspend' to wake us up when a process has
135 something to report (it will send us a SIGCHLD if it has). To make
136 this work we have to juggle with the signal mask. We save the
137 original signal mask such that we can restore it before creating a
138 new process in order to avoid blocking certain signals in the
139 inferior. We then block SIGCHLD during the waitpid/sigsuspend
142 /* Original signal mask. */
143 static sigset_t normal_mask;
145 /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
146 _initialize_lin_lwp. */
147 static sigset_t suspend_mask;
149 /* Signals to block to make that sigsuspend work. */
150 static sigset_t blocked_mask;
153 /* Prototypes for local functions. */
154 static int stop_wait_callback (struct lwp_info *lp, void *data);
156 /* Convert wait status STATUS to a string. Used for printing debug
160 status_to_str (int status)
164 if (WIFSTOPPED (status))
165 snprintf (buf, sizeof (buf), "%s (stopped)",
166 strsignal (WSTOPSIG (status)));
167 else if (WIFSIGNALED (status))
168 snprintf (buf, sizeof (buf), "%s (terminated)",
169 strsignal (WSTOPSIG (status)));
171 snprintf (buf, sizeof (buf), "%d (exited)",
172 WEXITSTATUS (status));
177 /* Initialize the list of LWPs. Note that this module, contrary to
178 what GDB's generic threads layer does for its thread list,
179 re-initializes the LWP lists whenever we mourn or detach (which
180 doesn't involve mourning) the inferior. */
185 struct lwp_info *lp, *lpnext;
187 for (lp = lwp_list; lp; lp = lpnext)
198 /* Add the LWP specified by PID to the list. If this causes the
199 number of LWPs to become larger than one, go into "threaded" mode.
200 Return a pointer to the structure describing the new LWP. */
202 static struct lwp_info *
203 add_lwp (ptid_t ptid)
207 gdb_assert (is_lwp (ptid));
209 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
211 memset (lp, 0, sizeof (struct lwp_info));
223 /* Remove the LWP specified by PID from the list. */
226 delete_lwp (ptid_t ptid)
228 struct lwp_info *lp, *lpprev;
232 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
233 if (ptid_equal (lp->ptid, ptid))
239 /* We don't go back to "non-threaded" mode if the number of threads
240 becomes less than two. */
244 lpprev->next = lp->next;
251 /* Return a pointer to the structure describing the LWP corresponding
252 to PID. If no corresponding LWP could be found, return NULL. */
254 static struct lwp_info *
255 find_lwp_pid (ptid_t ptid)
261 lwp = GET_LWP (ptid);
263 lwp = GET_PID (ptid);
265 for (lp = lwp_list; lp; lp = lp->next)
266 if (lwp == GET_LWP (lp->ptid))
272 /* Call CALLBACK with its second argument set to DATA for every LWP in
273 the list. If CALLBACK returns 1 for a particular LWP, return a
274 pointer to the structure describing that LWP immediately.
275 Otherwise return NULL. */
278 iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
280 struct lwp_info *lp, *lpnext;
282 for (lp = lwp_list; lp; lp = lpnext)
285 if ((*callback) (lp, data))
293 /* Implementation of the PREPARE_TO_PROCEED hook for the Linux LWP
296 Note that this implementation is potentially redundant now that
297 default_prepare_to_proceed() has been added.
299 FIXME This may not support switching threads after Ctrl-C
300 correctly. The default implementation does support this. */
303 lin_lwp_prepare_to_proceed (void)
305 if (! ptid_equal (trap_ptid, null_ptid)
306 && ! ptid_equal (inferior_ptid, trap_ptid))
308 /* Switched over from TRAP_PID. */
309 CORE_ADDR stop_pc = read_pc ();
312 /* Avoid switching where it wouldn't do any good, i.e. if both
313 threads are at the same breakpoint. */
314 trap_pc = read_pc_pid (trap_ptid);
315 if (trap_pc != stop_pc && breakpoint_here_p (trap_pc))
317 /* User hasn't deleted the breakpoint. Return non-zero, and
318 switch back to TRAP_PID. */
319 inferior_ptid = trap_ptid;
321 /* FIXME: Is this stuff really necessary? */
322 flush_cached_frames ();
323 registers_changed ();
335 lin_lwp_open (char *args, int from_tty)
337 push_target (&lin_lwp_ops);
341 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
342 a message telling the user that a new LWP has been added to the
346 lin_lwp_attach_lwp (ptid_t ptid, int verbose)
350 gdb_assert (is_lwp (ptid));
353 printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
355 /* We assume that we're already tracing the initial process. */
356 if (is_cloned (ptid) && ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
357 error ("Can't attach %s: %s", target_pid_to_str (ptid), strerror (errno));
359 lp = find_lwp_pid (ptid);
363 if (is_cloned (ptid))
366 stop_wait_callback (lp, NULL);
371 lin_lwp_attach (char *args, int from_tty)
375 /* FIXME: We should probably accept a list of process id's, and
376 attach all of them. */
377 child_ops.to_attach (args, from_tty);
379 /* Add the initial process as the first LWP to the list. */
380 lp = add_lwp (BUILD_LWP (PIDGET (inferior_ptid), PIDGET (inferior_ptid)));
382 /* Make sure the initial process is stopped. The user-level threads
383 layer might want to poke around in the inferior, and that won't
384 work if things haven't stabilized yet. */
386 stop_wait_callback (lp, NULL);
387 gdb_assert (lp->status == 0);
389 /* Fake the SIGSTOP that core GDB expects. */
390 lp->status = W_STOPCODE (SIGSTOP);
395 detach_callback (struct lwp_info *lp, void *data)
397 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
399 if (debug_lin_lwp && lp->status)
400 fprintf_unfiltered (gdb_stdlog, "Pending %s for LWP %ld on detach.\n",
401 strsignal (WSTOPSIG (lp->status)), GET_LWP (lp->ptid));
403 while (lp->signalled && lp->stopped)
405 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
406 WSTOPSIG (lp->status)) < 0)
407 error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
413 stop_wait_callback (lp, NULL);
415 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
418 if (is_cloned (lp->ptid))
420 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
421 WSTOPSIG (lp->status)) < 0)
422 error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
425 delete_lwp (lp->ptid);
432 lin_lwp_detach (char *args, int from_tty)
434 iterate_over_lwps (detach_callback, NULL);
436 /* Only the initial (uncloned) process should be left right now. */
437 gdb_assert (num_lwps == 1);
439 trap_ptid = null_ptid;
441 /* Destroy LWP info; it's no longer valid. */
444 /* Restore the original signal mask. */
445 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
446 sigemptyset (&blocked_mask);
448 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
449 child_ops.to_detach (args, from_tty);
453 struct private_thread_info
458 /* Return non-zero if TP corresponds to the LWP specified by DATA
459 (which is assumed to be a pointer to a `struct lwp_info'. */
462 find_lwp_callback (struct thread_info *tp, void *data)
464 struct lwp_info *lp = data;
466 if (tp->private->lwpid == GET_LWP (lp->ptid))
475 resume_callback (struct lwp_info *lp, void *data)
477 if (lp->stopped && lp->status == 0)
479 struct thread_info *tp;
482 /* FIXME: kettenis/2000-08-26: This should really be handled
483 properly by core GDB. */
485 tp = find_thread_pid (lp->ptid);
487 tp = iterate_over_threads (find_lwp_callback, lp);
490 /* If we were previously stepping the thread, and now continue
491 the thread we must invalidate the stepping range. However,
492 if there is a step_resume breakpoint for this thread, we must
493 preserve the stepping range to make it possible to continue
494 stepping once we hit it. */
495 if (tp->step_range_end && tp->step_resume_breakpoint == NULL)
497 gdb_assert (lp->step);
498 tp->step_range_start = tp->step_range_end = 0;
502 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
511 resume_clear_callback (struct lwp_info *lp, void *data)
518 resume_set_callback (struct lwp_info *lp, void *data)
525 lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
530 /* Apparently the interpretation of PID is dependent on STEP: If
531 STEP is non-zero, a specific PID means `step only this process
532 id'. But if STEP is zero, then PID means `continue *all*
533 processes, but give the signal only to this one'. */
534 resume_all = (PIDGET (ptid) == -1) || !step;
537 iterate_over_lwps (resume_set_callback, NULL);
539 iterate_over_lwps (resume_clear_callback, NULL);
541 /* If PID is -1, it's the current inferior that should be
542 handled specially. */
543 if (PIDGET (ptid) == -1)
544 ptid = inferior_ptid;
546 lp = find_lwp_pid (ptid);
549 ptid = pid_to_ptid (GET_LWP (lp->ptid));
551 /* Remember if we're stepping. */
554 /* Mark this LWP as resumed. */
557 /* If we have a pending wait status for this thread, there is no
558 point in resuming the process. */
561 /* FIXME: What should we do if we are supposed to continue
562 this thread with a signal? */
563 gdb_assert (signo == TARGET_SIGNAL_0);
567 /* Mark LWP as not stopped to prevent it from being continued by
573 iterate_over_lwps (resume_callback, NULL);
575 child_resume (ptid, step, signo);
579 /* Send a SIGSTOP to LP. */
582 stop_callback (struct lwp_info *lp, void *data)
584 if (! lp->stopped && ! lp->signalled)
588 ret = kill (GET_LWP (lp->ptid), SIGSTOP);
589 gdb_assert (ret == 0);
592 gdb_assert (lp->status == 0);
598 /* Wait until LP is stopped. */
601 stop_wait_callback (struct lwp_info *lp, void *data)
603 if (! lp->stopped && lp->signalled)
608 gdb_assert (lp->status == 0);
610 pid = waitpid (GET_LWP (lp->ptid), &status,
611 is_cloned (lp->ptid) ? __WCLONE : 0);
612 if (pid == -1 && errno == ECHILD)
613 /* OK, the proccess has disappeared. We'll catch the actual
614 exit event in lin_lwp_wait. */
617 gdb_assert (pid == GET_LWP (lp->ptid));
619 if (WIFEXITED (status) || WIFSIGNALED (status))
621 gdb_assert (num_lwps > 1);
623 if (in_thread_list (lp->ptid))
625 /* Core GDB cannot deal with us deleting the current
627 if (!ptid_equal (lp->ptid, inferior_ptid))
628 delete_thread (lp->ptid);
629 printf_unfiltered ("[%s exited]\n",
630 target_pid_to_str (lp->ptid));
633 fprintf_unfiltered (gdb_stdlog,
634 "%s exited.\n", target_pid_to_str (lp->ptid));
636 delete_lwp (lp->ptid);
640 gdb_assert (WIFSTOPPED (status));
642 if (WSTOPSIG (status) != SIGSTOP)
644 if (WSTOPSIG (status) == SIGTRAP)
646 /* If a LWP other than the LWP that we're reporting an
647 event for has hit a GDB breakpoint (as opposed to
648 some random trap signal), then just arrange for it to
649 hit it again later. We don't keep the SIGTRAP status
650 and don't forward the SIGTRAP signal to the LWP. We
651 will handle the current event, eventually we will
652 resume all LWPs, and this one will get its breakpoint
655 If we do not do this, then we run the risk that the
656 user will delete or disable the breakpoint, but the
657 thread will have already tripped on it. */
659 /* Now resume this LWP and get the SIGSTOP event. */
660 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
663 fprintf_unfiltered (gdb_stderr,
664 "SWC: Candidate SIGTRAP event in %ld\n",
667 /* Hold the SIGTRAP for handling by lin_lwp_wait. */
668 stop_wait_callback (lp, data);
669 /* If there's another event, throw it back into the queue. */
671 kill (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
672 /* Save the sigtrap event. */
676 else if (WSTOPSIG (status) == SIGINT &&
677 signal_pass_state (SIGINT) == 0)
679 /* Since SIGINT gets forwarded to the entire process group
680 (in the case where ^C/BREAK is typed at the tty/console),
681 just ignore all SIGINT events from all lwp's except for
682 the one that was caught by lin_lwp_wait. */
684 /* Now resume this LWP and get the SIGSTOP event. */
685 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
686 return stop_wait_callback (lp, data);
690 /* The thread was stopped with a signal other than
691 SIGSTOP, and didn't accidentally trip a breakpoint. */
695 fprintf_unfiltered (gdb_stderr,
696 "SWC: Pending event %d in %ld\n",
697 WSTOPSIG (status), GET_LWP (lp->ptid));
699 /* Now resume this LWP and get the SIGSTOP event. */
700 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
702 /* Hold this event/waitstatus while we check to see if
703 there are any more (we still want to get that SIGSTOP). */
704 stop_wait_callback (lp, data);
705 /* If the lp->status field is still empty, use it to hold
706 this event. If not, then this event must be returned
707 to the event queue of the LWP. */
711 kill (GET_LWP (lp->ptid), WSTOPSIG (status));
717 /* We caught the SIGSTOP that we intended to catch, so
718 there's no SIGSTOP pending. */
727 /* Return non-zero if LP has a wait status pending. */
730 status_callback (struct lwp_info *lp, void *data)
732 /* Only report a pending wait status if we pretend that this has
733 indeed been resumed. */
734 return (lp->status != 0 && lp->resumed);
737 /* Return non-zero if LP isn't stopped. */
740 running_callback (struct lwp_info *lp, void *data)
742 return (lp->stopped == 0);
745 /* Count the LWP's that have had events. */
748 count_events_callback (struct lwp_info *lp, void *data)
752 gdb_assert (count != NULL);
754 /* Count only LWPs that have a SIGTRAP event pending. */
756 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
762 /* Select the LWP (if any) that is currently being single-stepped. */
765 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
767 if (lp->step && lp->status != 0)
773 /* Select the Nth LWP that has had a SIGTRAP event. */
776 select_event_lwp_callback (struct lwp_info *lp, void *data)
778 int *selector = data;
780 gdb_assert (selector != NULL);
782 /* Select only LWPs that have a SIGTRAP event pending. */
784 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
785 if ((*selector)-- == 0)
792 cancel_breakpoints_callback (struct lwp_info *lp, void *data)
794 struct lwp_info *event_lp = data;
796 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
800 /* If a LWP other than the LWP that we're reporting an event for has
801 hit a GDB breakpoint (as opposed to some random trap signal),
802 then just arrange for it to hit it again later. We don't keep
803 the SIGTRAP status and don't forward the SIGTRAP signal to the
804 LWP. We will handle the current event, eventually we will resume
805 all LWPs, and this one will get its breakpoint trap again.
807 If we do not do this, then we run the risk that the user will
808 delete or disable the breakpoint, but the LWP will have already
812 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
813 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
814 DECR_PC_AFTER_BREAK))
817 fprintf_unfiltered (gdb_stdlog,
818 "Push back breakpoint for LWP %ld\n",
821 /* Back up the PC if necessary. */
822 if (DECR_PC_AFTER_BREAK)
823 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
825 /* Throw away the SIGTRAP. */
832 /* Select one LWP out of those that have events pending. */
835 select_event_lwp (struct lwp_info **orig_lp, int *status)
839 struct lwp_info *event_lp;
841 /* Record the wait status for the origional LWP. */
842 (*orig_lp)->status = *status;
844 /* Give preference to any LWP that is being single-stepped. */
845 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
846 if (event_lp != NULL)
849 fprintf_unfiltered (gdb_stdlog,
850 "Select single-step LWP %ld\n",
851 GET_LWP (event_lp->ptid));
855 /* No single-stepping LWP. Select one at random, out of those
856 which have had SIGTRAP events. */
858 /* First see how many SIGTRAP events we have. */
859 iterate_over_lwps (count_events_callback, &num_events);
861 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
862 random_selector = (int)
863 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
865 if (debug_lin_lwp && num_events > 1)
866 fprintf_unfiltered (gdb_stdlog,
867 "Found %d SIGTRAP events, selecting #%d\n",
868 num_events, random_selector);
870 event_lp = iterate_over_lwps (select_event_lwp_callback,
874 if (event_lp != NULL)
876 /* Switch the event LWP. */
878 *status = event_lp->status;
881 /* Flush the wait status for the event LWP. */
882 (*orig_lp)->status = 0;
885 /* Return non-zero if LP has been resumed. */
888 resumed_callback (struct lwp_info *lp, void *data)
894 lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
896 struct lwp_info *lp = NULL;
899 pid_t pid = PIDGET (ptid);
901 /* Make sure SIGCHLD is blocked. */
902 if (! sigismember (&blocked_mask, SIGCHLD))
904 sigaddset (&blocked_mask, SIGCHLD);
905 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
910 /* Make sure there is at least one thread that has been resumed. */
911 gdb_assert (iterate_over_lwps (resumed_callback, NULL));
913 /* First check if there is a LWP with a wait status pending. */
916 /* Any LWP that's been resumed will do. */
917 lp = iterate_over_lwps (status_callback, NULL);
923 if (debug_lin_lwp && status)
924 fprintf_unfiltered (gdb_stdlog,
925 "Using pending wait status %s for LWP %ld.\n",
926 status_to_str (status), GET_LWP (lp->ptid));
929 /* But if we don't fine one, we'll have to wait, and check both
930 cloned and uncloned processes. We start with the cloned
932 options = __WCLONE | WNOHANG;
934 else if (is_lwp (ptid))
937 fprintf_unfiltered (gdb_stdlog,
938 "Waiting for specific LWP %ld.\n",
941 /* We have a specific LWP to check. */
942 lp = find_lwp_pid (ptid);
947 if (debug_lin_lwp && status)
948 fprintf_unfiltered (gdb_stdlog,
949 "Using pending wait status %s for LWP %ld.\n",
950 status_to_str (status), GET_LWP (lp->ptid));
952 /* If we have to wait, take into account whether PID is a cloned
953 process or not. And we have to convert it to something that
954 the layer beneath us can understand. */
955 options = is_cloned (lp->ptid) ? __WCLONE : 0;
956 pid = GET_LWP (ptid);
959 if (status && lp->signalled)
961 /* A pending SIGSTOP may interfere with the normal stream of
962 events. In a typical case where interference is a problem,
963 we have a SIGSTOP signal pending for LWP A while
964 single-stepping it, encounter an event in LWP B, and take the
965 pending SIGSTOP while trying to stop LWP A. After processing
966 the event in LWP B, LWP A is continued, and we'll never see
967 the SIGTRAP associated with the last time we were
968 single-stepping LWP A. */
970 /* Resume the thread. It should halt immediately returning the
972 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
975 gdb_assert (lp->resumed);
977 /* This should catch the pending SIGSTOP. */
978 stop_wait_callback (lp, NULL);
981 set_sigint_trap (); /* Causes SIGINT to be passed on to the
989 lwpid = waitpid (pid, &status, options);
992 gdb_assert (pid == -1 || lwpid == pid);
994 lp = find_lwp_pid (pid_to_ptid (lwpid));
997 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
1000 gdb_assert (WIFSTOPPED (status)
1001 && WSTOPSIG (status) == SIGSTOP);
1004 if (! in_thread_list (inferior_ptid))
1006 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1007 GET_PID (inferior_ptid));
1008 add_thread (inferior_ptid);
1011 add_thread (lp->ptid);
1012 printf_unfiltered ("[New %s]\n",
1013 target_pid_to_str (lp->ptid));
1017 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
1018 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
1019 left in the process. */
1020 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
1022 if (in_thread_list (lp->ptid))
1024 /* Core GDB cannot deal with us deleting the current
1026 if (! ptid_equal (lp->ptid, inferior_ptid))
1027 delete_thread (lp->ptid);
1028 printf_unfiltered ("[%s exited]\n",
1029 target_pid_to_str (lp->ptid));
1032 fprintf_unfiltered (gdb_stdlog,
1034 target_pid_to_str (lp->ptid));
1036 delete_lwp (lp->ptid);
1038 /* Make sure there is at least one thread running. */
1039 gdb_assert (iterate_over_lwps (running_callback, NULL));
1041 /* Discard the event. */
1046 /* Make sure we don't report a SIGSTOP that we sent
1047 ourselves in an attempt to stop an LWP. */
1048 if (lp->signalled && WIFSTOPPED (status)
1049 && WSTOPSIG (status) == SIGSTOP)
1052 fprintf_unfiltered (gdb_stdlog,
1053 "Delayed SIGSTOP caught for %s.\n",
1054 target_pid_to_str (lp->ptid));
1056 /* This is a delayed SIGSTOP. */
1059 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1062 gdb_assert (lp->resumed);
1064 /* Discard the event. */
1074 /* Alternate between checking cloned and uncloned processes. */
1075 options ^= __WCLONE;
1077 /* And suspend every time we have checked both. */
1078 if (options & __WCLONE)
1079 sigsuspend (&suspend_mask);
1082 /* We shouldn't end up here unless we want to try again. */
1083 gdb_assert (status == 0);
1086 clear_sigio_trap ();
1087 clear_sigint_trap ();
1091 /* Don't report signals that GDB isn't interested in, such as
1092 signals that are neither printed nor stopped upon. Stopping all
1093 threads can be a bit time-consuming so if we want decent
1094 performance with heavily multi-threaded programs, especially when
1095 they're using a high frequency timer, we'd better avoid it if we
1098 if (WIFSTOPPED (status))
1100 int signo = target_signal_from_host (WSTOPSIG (status));
1102 if (signal_stop_state (signo) == 0
1103 && signal_print_state (signo) == 0
1104 && signal_pass_state (signo) == 1)
1106 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
1107 here? It is not clear we should. GDB may not expect
1108 other threads to run. On the other hand, not resuming
1109 newly attached threads may cause an unwanted delay in
1110 getting them running. */
1111 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
1118 /* This LWP is stopped now. */
1122 fprintf_unfiltered (gdb_stdlog, "Candidate event %s in LWP %ld.\n",
1123 status_to_str (status), GET_LWP (lp->ptid));
1125 /* Now stop all other LWP's ... */
1126 iterate_over_lwps (stop_callback, NULL);
1128 /* ... and wait until all of them have reported back that they're no
1130 iterate_over_lwps (stop_wait_callback, NULL);
1132 /* If we're not waiting for a specific LWP, choose an event LWP from
1133 among those that have had events. Giving equal priority to all
1134 LWPs that have had events helps prevent starvation. */
1136 select_event_lwp (&lp, &status);
1138 /* Now that we've selected our final event LWP, cancel any
1139 breakpoints in other LWPs that have hit a GDB breakpoint. See
1140 the comment in cancel_breakpoints_callback to find out why. */
1141 iterate_over_lwps (cancel_breakpoints_callback, lp);
1143 /* If we're not running in "threaded" mode, we'll report the bare
1146 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
1148 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1150 fprintf_unfiltered (gdb_stdlog,
1151 "LLW: trap_ptid is %ld\n",
1152 GET_LWP (trap_ptid));
1155 trap_ptid = null_ptid;
1157 store_waitstatus (ourstatus, status);
1158 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1162 kill_callback (struct lwp_info *lp, void *data)
1164 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
1169 kill_wait_callback (struct lwp_info *lp, void *data)
1173 /* We must make sure that there are no pending events (delayed
1174 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
1175 program doesn't interfere with any following debugging session. */
1177 /* For cloned processes we must check both with __WCLONE and
1178 without, since the exit status of a cloned process isn't reported
1180 if (is_cloned (lp->ptid))
1184 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
1186 while (pid == GET_LWP (lp->ptid));
1188 gdb_assert (pid == -1 && errno == ECHILD);
1193 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
1195 while (pid == GET_LWP (lp->ptid));
1197 gdb_assert (pid == -1 && errno == ECHILD);
1204 /* Kill all LWP's ... */
1205 iterate_over_lwps (kill_callback, NULL);
1207 /* ... and wait until we've flushed all events. */
1208 iterate_over_lwps (kill_wait_callback, NULL);
1210 target_mourn_inferior ();
1214 lin_lwp_create_inferior (char *exec_file, char *allargs, char **env)
1216 child_ops.to_create_inferior (exec_file, allargs, env);
1220 lin_lwp_mourn_inferior (void)
1222 trap_ptid = null_ptid;
1224 /* Destroy LWP info; it's no longer valid. */
1227 /* Restore the original signal mask. */
1228 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1229 sigemptyset (&blocked_mask);
1231 child_ops.to_mourn_inferior ();
1235 lin_lwp_fetch_registers (int regno)
1237 struct cleanup *old_chain = save_inferior_ptid ();
1239 if (is_lwp (inferior_ptid))
1240 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
1242 fetch_inferior_registers (regno);
1244 do_cleanups (old_chain);
1248 lin_lwp_store_registers (int regno)
1250 struct cleanup *old_chain = save_inferior_ptid ();
1252 if (is_lwp (inferior_ptid))
1253 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
1255 store_inferior_registers (regno);
1257 do_cleanups (old_chain);
1261 lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
1262 struct mem_attrib *attrib,
1263 struct target_ops *target)
1265 struct cleanup *old_chain = save_inferior_ptid ();
1268 if (is_lwp (inferior_ptid))
1269 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
1271 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1273 do_cleanups (old_chain);
1278 lin_lwp_thread_alive (ptid_t ptid)
1280 gdb_assert (is_lwp (ptid));
1283 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
1291 lin_lwp_pid_to_str (ptid_t ptid)
1293 static char buf[64];
1297 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
1301 return normal_pid_to_str (ptid);
1305 init_lin_lwp_ops (void)
1308 lin_lwp_ops.to_open = lin_lwp_open;
1310 lin_lwp_ops.to_shortname = "lwp-layer";
1311 lin_lwp_ops.to_longname = "lwp-layer";
1312 lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
1313 lin_lwp_ops.to_attach = lin_lwp_attach;
1314 lin_lwp_ops.to_detach = lin_lwp_detach;
1315 lin_lwp_ops.to_resume = lin_lwp_resume;
1316 lin_lwp_ops.to_wait = lin_lwp_wait;
1317 lin_lwp_ops.to_fetch_registers = lin_lwp_fetch_registers;
1318 lin_lwp_ops.to_store_registers = lin_lwp_store_registers;
1319 lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
1320 lin_lwp_ops.to_kill = lin_lwp_kill;
1321 lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
1322 lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
1323 lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
1324 lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
1325 lin_lwp_ops.to_stratum = thread_stratum;
1326 lin_lwp_ops.to_has_thread_control = tc_schedlock;
1327 lin_lwp_ops.to_magic = OPS_MAGIC;
1331 sigchld_handler (int signo)
1333 /* Do nothing. The only reason for this handler is that it allows
1334 us to use sigsuspend in lin_lwp_wait above to wait for the
1335 arrival of a SIGCHLD. */
1339 _initialize_lin_lwp (void)
1341 struct sigaction action;
1343 extern void thread_db_init (struct target_ops *);
1345 init_lin_lwp_ops ();
1346 add_target (&lin_lwp_ops);
1347 thread_db_init (&lin_lwp_ops);
1349 /* Save the original signal mask. */
1350 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
1352 action.sa_handler = sigchld_handler;
1353 sigemptyset (&action.sa_mask);
1354 action.sa_flags = 0;
1355 sigaction (SIGCHLD, &action, NULL);
1357 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1358 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
1359 sigdelset (&suspend_mask, SIGCHLD);
1361 sigemptyset (&blocked_mask);
1363 add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
1364 (char *) &debug_lin_lwp,
1365 "Set debugging of linux lwp module.\n\
1366 Enables printf debugging output.\n",
1372 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
1373 the LinuxThreads library and therefore doesn't really belong here. */
1375 /* Read variable NAME in the target and return its value if found.
1376 Otherwise return zero. It is assumed that the type of the variable
1380 get_signo (const char *name)
1382 struct minimal_symbol *ms;
1385 ms = lookup_minimal_symbol (name, NULL, NULL);
1389 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
1390 sizeof (signo)) != 0)
1396 /* Return the set of signals used by the threads library in *SET. */
1399 lin_thread_get_thread_signals (sigset_t *set)
1401 struct sigaction action;
1402 int restart, cancel;
1406 restart = get_signo ("__pthread_sig_restart");
1410 cancel = get_signo ("__pthread_sig_cancel");
1414 sigaddset (set, restart);
1415 sigaddset (set, cancel);
1417 /* The LinuxThreads library makes terminating threads send a special
1418 "cancel" signal instead of SIGCHLD. Make sure we catch those (to
1419 prevent them from terminating GDB itself, which is likely to be
1420 their default action) and treat them the same way as SIGCHLD. */
1422 action.sa_handler = sigchld_handler;
1423 sigemptyset (&action.sa_mask);
1424 action.sa_flags = 0;
1425 sigaction (cancel, &action, NULL);
1427 /* We block the "cancel" signal throughout this code ... */
1428 sigaddset (&blocked_mask, cancel);
1429 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1431 /* ... except during a sigsuspend. */
1432 sigdelset (&suspend_mask, cancel);