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
);
157 /* Initialize the list of LWPs. Note that this module, contrary to
158 what GDB's generic threads layer does for its thread list,
159 re-initializes the LWP lists whenever we mourn or detach (which
160 doesn't involve mourning) the inferior. */
165 struct lwp_info
*lp
, *lpnext
;
167 for (lp
= lwp_list
; lp
; lp
= lpnext
)
178 /* Add the LWP specified by PID to the list. If this causes the
179 number of LWPs to become larger than one, go into "threaded" mode.
180 Return a pointer to the structure describing the new LWP. */
182 static struct lwp_info
*
183 add_lwp (ptid_t ptid
)
187 gdb_assert (is_lwp (ptid
));
189 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
191 memset (lp
, 0, sizeof (struct lwp_info
));
203 /* Remove the LWP specified by PID from the list. */
206 delete_lwp (ptid_t ptid
)
208 struct lwp_info
*lp
, *lpprev
;
212 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
213 if (ptid_equal (lp
->ptid
, ptid
))
219 /* We don't go back to "non-threaded" mode if the number of threads
220 becomes less than two. */
224 lpprev
->next
= lp
->next
;
231 /* Return a pointer to the structure describing the LWP corresponding
232 to PID. If no corresponding LWP could be found, return NULL. */
234 static struct lwp_info
*
235 find_lwp_pid (ptid_t ptid
)
241 lwp
= GET_LWP (ptid
);
243 lwp
= GET_PID (ptid
);
245 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
246 if (lwp
== GET_LWP (lp
->ptid
))
252 /* Call CALLBACK with its second argument set to DATA for every LWP in
253 the list. If CALLBACK returns 1 for a particular LWP, return a
254 pointer to the structure describing that LWP immediately.
255 Otherwise return NULL. */
258 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
260 struct lwp_info
*lp
, *lpnext
;
262 for (lp
= lwp_list
; lp
; lp
= lpnext
)
265 if ((*callback
) (lp
, data
))
273 /* Implementation of the PREPARE_TO_PROCEED hook for the Linux LWP
276 Note that this implementation is potentially redundant now that
277 default_prepare_to_proceed() has been added.
279 FIXME This may not support switching threads after Ctrl-C
280 correctly. The default implementation does support this. */
283 lin_lwp_prepare_to_proceed (void)
285 if (! ptid_equal (trap_ptid
, null_ptid
)
286 && ! ptid_equal (inferior_ptid
, trap_ptid
))
288 /* Switched over from TRAP_PID. */
289 CORE_ADDR stop_pc
= read_pc ();
292 /* Avoid switching where it wouldn't do any good, i.e. if both
293 threads are at the same breakpoint. */
294 trap_pc
= read_pc_pid (trap_ptid
);
295 if (trap_pc
!= stop_pc
&& breakpoint_here_p (trap_pc
))
297 /* User hasn't deleted the breakpoint. Return non-zero, and
298 switch back to TRAP_PID. */
299 inferior_ptid
= trap_ptid
;
301 /* FIXME: Is this stuff really necessary? */
302 flush_cached_frames ();
303 registers_changed ();
315 lin_lwp_open (char *args
, int from_tty
)
317 push_target (&lin_lwp_ops
);
321 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
322 a message telling the user that a new LWP has been added to the
326 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
330 gdb_assert (is_lwp (ptid
));
333 printf_filtered ("[New %s]\n", target_pid_to_str (ptid
));
335 /* We assume that we're already tracing the initial process. */
336 if (is_cloned (ptid
) && ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
337 error ("Can't attach %s: %s", target_pid_to_str (ptid
), strerror (errno
));
339 lp
= find_lwp_pid (ptid
);
343 if (is_cloned (ptid
))
346 stop_wait_callback (lp
, NULL
);
351 lin_lwp_attach (char *args
, int from_tty
)
355 /* FIXME: We should probably accept a list of process id's, and
356 attach all of them. */
357 child_ops
.to_attach (args
, from_tty
);
359 /* Add the initial process as the first LWP to the list. */
360 lp
= add_lwp (BUILD_LWP (PIDGET (inferior_ptid
), PIDGET (inferior_ptid
)));
362 /* Make sure the initial process is stopped. The user-level threads
363 layer might want to poke around in the inferior, and that won't
364 work if things haven't stabilized yet. */
366 stop_wait_callback (lp
, NULL
);
367 gdb_assert (lp
->status
== 0);
369 /* Fake the SIGSTOP that core GDB expects. */
370 lp
->status
= W_STOPCODE (SIGSTOP
);
375 detach_callback (struct lwp_info
*lp
, void *data
)
377 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
379 if (debug_lin_lwp
&& lp
->status
)
380 fprintf_unfiltered (gdb_stdlog
, "Pending %s for LWP %ld on detach.\n",
381 strsignal (WSTOPSIG (lp
->status
)), GET_LWP (lp
->ptid
));
383 while (lp
->signalled
&& lp
->stopped
)
385 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
386 WSTOPSIG (lp
->status
)) < 0)
387 error ("Can't continue %s: %s", target_pid_to_str (lp
->ptid
),
393 stop_wait_callback (lp
, NULL
);
395 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
398 if (is_cloned (lp
->ptid
))
400 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
401 WSTOPSIG (lp
->status
)) < 0)
402 error ("Can't detach %s: %s", target_pid_to_str (lp
->ptid
),
405 delete_lwp (lp
->ptid
);
412 lin_lwp_detach (char *args
, int from_tty
)
414 iterate_over_lwps (detach_callback
, NULL
);
416 /* Only the initial (uncloned) process should be left right now. */
417 gdb_assert (num_lwps
== 1);
419 trap_ptid
= null_ptid
;
421 /* Destroy LWP info; it's no longer valid. */
424 /* Restore the original signal mask. */
425 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
426 sigemptyset (&blocked_mask
);
428 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
429 child_ops
.to_detach (args
, from_tty
);
433 struct private_thread_info
438 /* Return non-zero if TP corresponds to the LWP specified by DATA
439 (which is assumed to be a pointer to a `struct lwp_info'. */
442 find_lwp_callback (struct thread_info
*tp
, void *data
)
444 struct lwp_info
*lp
= data
;
446 if (tp
->private->lwpid
== GET_LWP (lp
->ptid
))
455 resume_callback (struct lwp_info
*lp
, void *data
)
457 if (lp
->stopped
&& lp
->status
== 0)
459 struct thread_info
*tp
;
462 /* FIXME: kettenis/2000-08-26: This should really be handled
463 properly by core GDB. */
465 tp
= find_thread_pid (lp
->ptid
);
467 tp
= iterate_over_threads (find_lwp_callback
, lp
);
470 /* If we were previously stepping the thread, and now continue
471 the thread we must invalidate the stepping range. However,
472 if there is a step_resume breakpoint for this thread, we must
473 preserve the stepping range to make it possible to continue
474 stepping once we hit it. */
475 if (tp
->step_range_end
&& tp
->step_resume_breakpoint
== NULL
)
477 gdb_assert (lp
->step
);
478 tp
->step_range_start
= tp
->step_range_end
= 0;
482 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), 0, TARGET_SIGNAL_0
);
491 resume_clear_callback (struct lwp_info
*lp
, void *data
)
498 resume_set_callback (struct lwp_info
*lp
, void *data
)
505 lin_lwp_resume (ptid_t ptid
, int step
, enum target_signal signo
)
510 /* Apparently the interpretation of PID is dependent on STEP: If
511 STEP is non-zero, a specific PID means `step only this process
512 id'. But if STEP is zero, then PID means `continue *all*
513 processes, but give the signal only to this one'. */
514 resume_all
= (PIDGET (ptid
) == -1) || !step
;
517 iterate_over_lwps (resume_set_callback
, NULL
);
519 iterate_over_lwps (resume_clear_callback
, NULL
);
521 /* If PID is -1, it's the current inferior that should be
522 handled specially. */
523 if (PIDGET (ptid
) == -1)
524 ptid
= inferior_ptid
;
526 lp
= find_lwp_pid (ptid
);
529 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
531 /* Remember if we're stepping. */
534 /* Mark this LWP as resumed. */
537 /* If we have a pending wait status for this thread, there is no
538 point in resuming the process. */
541 /* FIXME: What should we do if we are supposed to continue
542 this thread with a signal? */
543 gdb_assert (signo
== TARGET_SIGNAL_0
);
547 /* Mark LWP as not stopped to prevent it from being continued by
553 iterate_over_lwps (resume_callback
, NULL
);
555 child_resume (ptid
, step
, signo
);
559 /* Send a SIGSTOP to LP. */
562 stop_callback (struct lwp_info
*lp
, void *data
)
564 if (! lp
->stopped
&& ! lp
->signalled
)
568 ret
= kill (GET_LWP (lp
->ptid
), SIGSTOP
);
569 gdb_assert (ret
== 0);
572 gdb_assert (lp
->status
== 0);
578 /* Wait until LP is stopped. */
581 stop_wait_callback (struct lwp_info
*lp
, void *data
)
583 if (! lp
->stopped
&& lp
->signalled
)
588 gdb_assert (lp
->status
== 0);
590 pid
= waitpid (GET_LWP (lp
->ptid
), &status
,
591 is_cloned (lp
->ptid
) ? __WCLONE
: 0);
592 if (pid
== -1 && errno
== ECHILD
)
593 /* OK, the proccess has disappeared. We'll catch the actual
594 exit event in lin_lwp_wait. */
597 gdb_assert (pid
== GET_LWP (lp
->ptid
));
599 if (WIFEXITED (status
) || WIFSIGNALED (status
))
601 gdb_assert (num_lwps
> 1);
603 if (in_thread_list (lp
->ptid
))
605 /* Core GDB cannot deal with us deleting the current
607 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
608 delete_thread (lp
->ptid
);
609 printf_unfiltered ("[%s exited]\n",
610 target_pid_to_str (lp
->ptid
));
613 fprintf_unfiltered (gdb_stdlog
,
614 "%s exited.\n", target_pid_to_str (lp
->ptid
));
616 delete_lwp (lp
->ptid
);
620 gdb_assert (WIFSTOPPED (status
));
622 if (WSTOPSIG (status
) != SIGSTOP
)
624 if (WSTOPSIG (status
) == SIGTRAP
)
626 /* If a LWP other than the LWP that we're reporting an
627 event for has hit a GDB breakpoint (as opposed to
628 some random trap signal), then just arrange for it to
629 hit it again later. We don't keep the SIGTRAP status
630 and don't forward the SIGTRAP signal to the LWP. We
631 will handle the current event, eventually we will
632 resume all LWPs, and this one will get its breakpoint
635 If we do not do this, then we run the risk that the
636 user will delete or disable the breakpoint, but the
637 thread will have already tripped on it. */
639 /* Now resume this LWP and get the SIGSTOP event. */
640 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
643 fprintf_unfiltered (gdb_stderr
,
644 "SWC: Candidate SIGTRAP event in %ld\n",
647 /* Hold the SIGTRAP for handling by lin_lwp_wait. */
648 stop_wait_callback (lp
, data
);
649 /* If there's another event, throw it back into the queue. */
651 kill (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
652 /* Save the sigtrap event. */
656 else if (WSTOPSIG (status
) == SIGINT
&&
657 signal_pass_state (SIGINT
) == 0)
659 /* Since SIGINT gets forwarded to the entire process group
660 (in the case where ^C/BREAK is typed at the tty/console),
661 just ignore all SIGINT events from all lwp's except for
662 the one that was caught by lin_lwp_wait. */
664 /* Now resume this LWP and get the SIGSTOP event. */
665 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
666 return stop_wait_callback (lp
, data
);
670 /* The thread was stopped with a signal other than
671 SIGSTOP, and didn't accidentally trip a breakpoint. */
675 fprintf_unfiltered (gdb_stderr
,
676 "SWC: Pending event %d in %ld\n",
677 WSTOPSIG (status
), GET_LWP (lp
->ptid
));
679 /* Now resume this LWP and get the SIGSTOP event. */
680 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
682 /* Hold this event/waitstatus while we check to see if
683 there are any more (we still want to get that SIGSTOP). */
684 stop_wait_callback (lp
, data
);
685 /* If the lp->status field is still empty, use it to hold
686 this event. If not, then this event must be returned
687 to the event queue of the LWP. */
691 kill (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
697 /* We caught the SIGSTOP that we intended to catch, so
698 there's no SIGSTOP pending. */
707 /* Return non-zero if LP has a wait status pending. */
710 status_callback (struct lwp_info
*lp
, void *data
)
712 /* Only report a pending wait status if we pretend that this has
713 indeed been resumed. */
714 return (lp
->status
!= 0 && lp
->resumed
);
717 /* Return non-zero if LP isn't stopped. */
720 running_callback (struct lwp_info
*lp
, void *data
)
722 return (lp
->stopped
== 0);
725 /* Count the LWP's that have had events. */
728 count_events_callback (struct lwp_info
*lp
, void *data
)
732 gdb_assert (count
!= NULL
);
734 /* Count only LWPs that have a SIGTRAP event pending. */
736 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
742 /* Select the LWP (if any) that is currently being single-stepped. */
745 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
747 if (lp
->step
&& lp
->status
!= 0)
753 /* Select the Nth LWP that has had a SIGTRAP event. */
756 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
758 int *selector
= data
;
760 gdb_assert (selector
!= NULL
);
762 /* Select only LWPs that have a SIGTRAP event pending. */
764 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
765 if ((*selector
)-- == 0)
772 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
774 struct lwp_info
*event_lp
= data
;
776 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
780 /* If a LWP other than the LWP that we're reporting an event for has
781 hit a GDB breakpoint (as opposed to some random trap signal),
782 then just arrange for it to hit it again later. We don't keep
783 the SIGTRAP status and don't forward the SIGTRAP signal to the
784 LWP. We will handle the current event, eventually we will resume
785 all LWPs, and this one will get its breakpoint trap again.
787 If we do not do this, then we run the risk that the user will
788 delete or disable the breakpoint, but the LWP will have already
792 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
793 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
794 DECR_PC_AFTER_BREAK
))
797 fprintf_unfiltered (gdb_stdlog
,
798 "Push back breakpoint for LWP %ld\n",
801 /* Back up the PC if necessary. */
802 if (DECR_PC_AFTER_BREAK
)
803 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
805 /* Throw away the SIGTRAP. */
812 /* Select one LWP out of those that have events pending. */
815 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
819 struct lwp_info
*event_lp
;
821 /* Record the wait status for the origional LWP. */
822 (*orig_lp
)->status
= *status
;
824 /* Give preference to any LWP that is being single-stepped. */
825 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
826 if (event_lp
!= NULL
)
829 fprintf_unfiltered (gdb_stdlog
,
830 "Select single-step LWP %ld\n",
831 GET_LWP (event_lp
->ptid
));
835 /* No single-stepping LWP. Select one at random, out of those
836 which have had SIGTRAP events. */
838 /* First see how many SIGTRAP events we have. */
839 iterate_over_lwps (count_events_callback
, &num_events
);
841 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
842 random_selector
= (int)
843 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
845 if (debug_lin_lwp
&& num_events
> 1)
846 fprintf_unfiltered (gdb_stdlog
,
847 "Found %d SIGTRAP events, selecting #%d\n",
848 num_events
, random_selector
);
850 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
854 if (event_lp
!= NULL
)
856 /* Switch the event LWP. */
858 *status
= event_lp
->status
;
861 /* Flush the wait status for the event LWP. */
862 (*orig_lp
)->status
= 0;
865 /* Return non-zero if LP has been resumed. */
868 resumed_callback (struct lwp_info
*lp
, void *data
)
874 lin_lwp_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
876 struct lwp_info
*lp
= NULL
;
879 pid_t pid
= PIDGET (ptid
);
881 /* Make sure SIGCHLD is blocked. */
882 if (! sigismember (&blocked_mask
, SIGCHLD
))
884 sigaddset (&blocked_mask
, SIGCHLD
);
885 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
890 /* Make sure there is at least one thread that has been resumed. */
891 gdb_assert (iterate_over_lwps (resumed_callback
, NULL
));
893 /* First check if there is a LWP with a wait status pending. */
896 /* Any LWP that's been resumed will do. */
897 lp
= iterate_over_lwps (status_callback
, NULL
);
903 if (debug_lin_lwp
&& status
)
904 fprintf_unfiltered (gdb_stdlog
,
905 "Using pending wait status %d for LWP %ld.\n",
906 WIFSTOPPED (status
) ? WSTOPSIG (status
) :
907 WIFSIGNALED (status
) ? WTERMSIG (status
) :
908 WEXITSTATUS (status
), GET_LWP (lp
->ptid
));
911 /* But if we don't fine one, we'll have to wait, and check both
912 cloned and uncloned processes. We start with the cloned
914 options
= __WCLONE
| WNOHANG
;
916 else if (is_lwp (ptid
))
919 fprintf_unfiltered (gdb_stdlog
,
920 "Waiting for specific LWP %ld.\n",
923 /* We have a specific LWP to check. */
924 lp
= find_lwp_pid (ptid
);
929 if (debug_lin_lwp
&& status
)
930 fprintf_unfiltered (gdb_stdlog
,
931 "Using pending wait status %d for LWP %ld.\n",
932 WIFSTOPPED (status
) ? WSTOPSIG (status
) :
933 WIFSIGNALED (status
) ? WTERMSIG (status
) :
934 WEXITSTATUS (status
), GET_LWP (lp
->ptid
));
936 /* If we have to wait, take into account whether PID is a cloned
937 process or not. And we have to convert it to something that
938 the layer beneath us can understand. */
939 options
= is_cloned (lp
->ptid
) ? __WCLONE
: 0;
940 pid
= GET_LWP (ptid
);
943 if (status
&& lp
->signalled
)
945 /* A pending SIGSTOP may interfere with the normal stream of
946 events. In a typical case where interference is a problem,
947 we have a SIGSTOP signal pending for LWP A while
948 single-stepping it, encounter an event in LWP B, and take the
949 pending SIGSTOP while trying to stop LWP A. After processing
950 the event in LWP B, LWP A is continued, and we'll never see
951 the SIGTRAP associated with the last time we were
952 single-stepping LWP A. */
954 /* Resume the thread. It should halt immediately returning the
956 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
959 gdb_assert (lp
->resumed
);
961 /* This should catch the pending SIGSTOP. */
962 stop_wait_callback (lp
, NULL
);
965 set_sigint_trap (); /* Causes SIGINT to be passed on to the
973 lwpid
= waitpid (pid
, &status
, options
);
976 gdb_assert (pid
== -1 || lwpid
== pid
);
978 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
981 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
984 gdb_assert (WIFSTOPPED (status
)
985 && WSTOPSIG (status
) == SIGSTOP
);
988 if (! in_thread_list (inferior_ptid
))
990 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
991 GET_PID (inferior_ptid
));
992 add_thread (inferior_ptid
);
995 add_thread (lp
->ptid
);
996 printf_unfiltered ("[New %s]\n",
997 target_pid_to_str (lp
->ptid
));
1001 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
1002 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
1003 left in the process. */
1004 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
1006 if (in_thread_list (lp
->ptid
))
1008 /* Core GDB cannot deal with us deleting the current
1010 if (! ptid_equal (lp
->ptid
, inferior_ptid
))
1011 delete_thread (lp
->ptid
);
1012 printf_unfiltered ("[%s exited]\n",
1013 target_pid_to_str (lp
->ptid
));
1016 fprintf_unfiltered (gdb_stdlog
,
1018 target_pid_to_str (lp
->ptid
));
1020 delete_lwp (lp
->ptid
);
1022 /* Make sure there is at least one thread running. */
1023 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
1025 /* Discard the event. */
1030 /* Make sure we don't report a SIGSTOP that we sent
1031 ourselves in an attempt to stop an LWP. */
1032 if (lp
->signalled
&& WIFSTOPPED (status
)
1033 && WSTOPSIG (status
) == SIGSTOP
)
1036 fprintf_unfiltered (gdb_stdlog
,
1037 "Delayed SIGSTOP caught for %s.\n",
1038 target_pid_to_str (lp
->ptid
));
1040 /* This is a delayed SIGSTOP. */
1043 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
1046 gdb_assert (lp
->resumed
);
1048 /* Discard the event. */
1058 /* Alternate between checking cloned and uncloned processes. */
1059 options
^= __WCLONE
;
1061 /* And suspend every time we have checked both. */
1062 if (options
& __WCLONE
)
1063 sigsuspend (&suspend_mask
);
1066 /* We shouldn't end up here unless we want to try again. */
1067 gdb_assert (status
== 0);
1070 clear_sigio_trap ();
1071 clear_sigint_trap ();
1075 /* Don't report signals that GDB isn't interested in, such as
1076 signals that are neither printed nor stopped upon. Stopping all
1077 threads can be a bit time-consuming so if we want decent
1078 performance with heavily multi-threaded programs, especially when
1079 they're using a high frequency timer, we'd better avoid it if we
1082 if (WIFSTOPPED (status
))
1084 int signo
= target_signal_from_host (WSTOPSIG (status
));
1086 if (signal_stop_state (signo
) == 0
1087 && signal_print_state (signo
) == 0
1088 && signal_pass_state (signo
) == 1)
1090 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
1091 here? It is not clear we should. GDB may not expect
1092 other threads to run. On the other hand, not resuming
1093 newly attached threads may cause an unwanted delay in
1094 getting them running. */
1095 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
, signo
);
1102 /* This LWP is stopped now. */
1106 fprintf_unfiltered (gdb_stdlog
,
1107 "LLW: Candidate event %d in %ld\n",
1108 WSTOPSIG (status
), GET_LWP (lp
->ptid
));
1110 /* Now stop all other LWP's ... */
1111 iterate_over_lwps (stop_callback
, NULL
);
1113 /* ... and wait until all of them have reported back that they're no
1115 iterate_over_lwps (stop_wait_callback
, NULL
);
1117 /* If we're not waiting for a specific LWP, choose an event LWP from
1118 among those that have had events. Giving equal priority to all
1119 LWPs that have had events helps prevent starvation. */
1121 select_event_lwp (&lp
, &status
);
1123 /* Now that we've selected our final event LWP, cancel any
1124 breakpoints in other LWPs that have hit a GDB breakpoint. See
1125 the comment in cancel_breakpoints_callback to find out why. */
1126 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
1128 /* If we're not running in "threaded" mode, we'll report the bare
1131 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
1133 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
1135 fprintf_unfiltered (gdb_stdlog
,
1136 "LLW: trap_ptid is %ld\n",
1137 GET_LWP (trap_ptid
));
1140 trap_ptid
= null_ptid
;
1142 store_waitstatus (ourstatus
, status
);
1143 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
1147 kill_callback (struct lwp_info
*lp
, void *data
)
1149 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
1154 kill_wait_callback (struct lwp_info
*lp
, void *data
)
1158 /* We must make sure that there are no pending events (delayed
1159 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
1160 program doesn't interfere with any following debugging session. */
1162 /* For cloned processes we must check both with __WCLONE and
1163 without, since the exit status of a cloned process isn't reported
1165 if (is_cloned (lp
->ptid
))
1169 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
1171 while (pid
== GET_LWP (lp
->ptid
));
1173 gdb_assert (pid
== -1 && errno
== ECHILD
);
1178 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
1180 while (pid
== GET_LWP (lp
->ptid
));
1182 gdb_assert (pid
== -1 && errno
== ECHILD
);
1189 /* Kill all LWP's ... */
1190 iterate_over_lwps (kill_callback
, NULL
);
1192 /* ... and wait until we've flushed all events. */
1193 iterate_over_lwps (kill_wait_callback
, NULL
);
1195 target_mourn_inferior ();
1199 lin_lwp_create_inferior (char *exec_file
, char *allargs
, char **env
)
1201 child_ops
.to_create_inferior (exec_file
, allargs
, env
);
1205 lin_lwp_mourn_inferior (void)
1207 trap_ptid
= null_ptid
;
1209 /* Destroy LWP info; it's no longer valid. */
1212 /* Restore the original signal mask. */
1213 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1214 sigemptyset (&blocked_mask
);
1216 child_ops
.to_mourn_inferior ();
1220 lin_lwp_fetch_registers (int regno
)
1222 struct cleanup
*old_chain
= save_inferior_ptid ();
1224 if (is_lwp (inferior_ptid
))
1225 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
1227 fetch_inferior_registers (regno
);
1229 do_cleanups (old_chain
);
1233 lin_lwp_store_registers (int regno
)
1235 struct cleanup
*old_chain
= save_inferior_ptid ();
1237 if (is_lwp (inferior_ptid
))
1238 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
1240 store_inferior_registers (regno
);
1242 do_cleanups (old_chain
);
1246 lin_lwp_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
1247 struct mem_attrib
*attrib
,
1248 struct target_ops
*target
)
1250 struct cleanup
*old_chain
= save_inferior_ptid ();
1253 if (is_lwp (inferior_ptid
))
1254 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
1256 xfer
= child_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
1258 do_cleanups (old_chain
);
1263 lin_lwp_thread_alive (ptid_t ptid
)
1265 gdb_assert (is_lwp (ptid
));
1268 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
1276 lin_lwp_pid_to_str (ptid_t ptid
)
1278 static char buf
[64];
1282 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
1286 return normal_pid_to_str (ptid
);
1290 init_lin_lwp_ops (void)
1293 lin_lwp_ops
.to_open
= lin_lwp_open
;
1295 lin_lwp_ops
.to_shortname
= "lwp-layer";
1296 lin_lwp_ops
.to_longname
= "lwp-layer";
1297 lin_lwp_ops
.to_doc
= "Low level threads support (LWP layer)";
1298 lin_lwp_ops
.to_attach
= lin_lwp_attach
;
1299 lin_lwp_ops
.to_detach
= lin_lwp_detach
;
1300 lin_lwp_ops
.to_resume
= lin_lwp_resume
;
1301 lin_lwp_ops
.to_wait
= lin_lwp_wait
;
1302 lin_lwp_ops
.to_fetch_registers
= lin_lwp_fetch_registers
;
1303 lin_lwp_ops
.to_store_registers
= lin_lwp_store_registers
;
1304 lin_lwp_ops
.to_xfer_memory
= lin_lwp_xfer_memory
;
1305 lin_lwp_ops
.to_kill
= lin_lwp_kill
;
1306 lin_lwp_ops
.to_create_inferior
= lin_lwp_create_inferior
;
1307 lin_lwp_ops
.to_mourn_inferior
= lin_lwp_mourn_inferior
;
1308 lin_lwp_ops
.to_thread_alive
= lin_lwp_thread_alive
;
1309 lin_lwp_ops
.to_pid_to_str
= lin_lwp_pid_to_str
;
1310 lin_lwp_ops
.to_stratum
= thread_stratum
;
1311 lin_lwp_ops
.to_has_thread_control
= tc_schedlock
;
1312 lin_lwp_ops
.to_magic
= OPS_MAGIC
;
1316 sigchld_handler (int signo
)
1318 /* Do nothing. The only reason for this handler is that it allows
1319 us to use sigsuspend in lin_lwp_wait above to wait for the
1320 arrival of a SIGCHLD. */
1324 _initialize_lin_lwp (void)
1326 struct sigaction action
;
1328 extern void thread_db_init (struct target_ops
*);
1330 init_lin_lwp_ops ();
1331 add_target (&lin_lwp_ops
);
1332 thread_db_init (&lin_lwp_ops
);
1334 /* Save the original signal mask. */
1335 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
1337 action
.sa_handler
= sigchld_handler
;
1338 sigemptyset (&action
.sa_mask
);
1339 action
.sa_flags
= 0;
1340 sigaction (SIGCHLD
, &action
, NULL
);
1342 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1343 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
1344 sigdelset (&suspend_mask
, SIGCHLD
);
1346 sigemptyset (&blocked_mask
);
1348 add_show_from_set (add_set_cmd ("lin-lwp", no_class
, var_zinteger
,
1349 (char *) &debug_lin_lwp
,
1350 "Set debugging of linux lwp module.\n\
1351 Enables printf debugging output.\n",
1357 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
1358 the LinuxThreads library and therefore doesn't really belong here. */
1360 /* Read variable NAME in the target and return its value if found.
1361 Otherwise return zero. It is assumed that the type of the variable
1365 get_signo (const char *name
)
1367 struct minimal_symbol
*ms
;
1370 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
1374 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (char *) &signo
,
1375 sizeof (signo
)) != 0)
1381 /* Return the set of signals used by the threads library in *SET. */
1384 lin_thread_get_thread_signals (sigset_t
*set
)
1386 struct sigaction action
;
1387 int restart
, cancel
;
1391 restart
= get_signo ("__pthread_sig_restart");
1395 cancel
= get_signo ("__pthread_sig_cancel");
1399 sigaddset (set
, restart
);
1400 sigaddset (set
, cancel
);
1402 /* The LinuxThreads library makes terminating threads send a special
1403 "cancel" signal instead of SIGCHLD. Make sure we catch those (to
1404 prevent them from terminating GDB itself, which is likely to be
1405 their default action) and treat them the same way as SIGCHLD. */
1407 action
.sa_handler
= sigchld_handler
;
1408 sigemptyset (&action
.sa_mask
);
1409 action
.sa_flags
= 0;
1410 sigaction (cancel
, &action
, NULL
);
1412 /* We block the "cancel" signal throughout this code ... */
1413 sigaddset (&blocked_mask
, cancel
);
1414 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1416 /* ... except during a sigsuspend. */
1417 sigdelset (&suspend_mask
, cancel
);