1 /* Multi-threaded debugging support for GNU/Linux (LWP layer).
2 Copyright 2000, 2001, 2002, 2003 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"
24 #include "gdb_string.h"
27 #include <sys/ptrace.h>
30 #include "gdbthread.h"
36 static int debug_lin_lwp;
37 extern char *strsignal (int sig);
39 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
40 are processes sharing the same VM space. A multi-threaded process
41 is basically a group of such processes. However, such a grouping
42 is almost entirely a user-space issue; the kernel doesn't enforce
43 such a grouping at all (this might change in the future). In
44 general, we'll rely on the threads library (i.e. the GNU/Linux
45 Threads library) to provide such a grouping.
47 It is perfectly well possible to write a multi-threaded application
48 without the assistance of a threads library, by using the clone
49 system call directly. This module should be able to give some
50 rudimentary support for debugging such applications if developers
51 specify the CLONE_PTRACE flag in the clone system call, and are
52 using the Linux kernel 2.4 or above.
54 Note that there are some peculiarities in GNU/Linux that affect
57 - In general one should specify the __WCLONE flag to waitpid in
58 order to make it report events for any of the cloned processes
59 (and leave it out for the initial process). However, if a cloned
60 process has exited the exit status is only reported if the
61 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
62 we cannot use it since GDB must work on older systems too.
64 - When a traced, cloned process exits and is waited for by the
65 debugger, the kernel reassigns it to the original parent and
66 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
67 library doesn't notice this, which leads to the "zombie problem":
68 When debugged a multi-threaded process that spawns a lot of
69 threads will run out of processes, even if the threads exit,
70 because the "zombies" stay around. */
72 /* Structure describing a LWP. */
75 /* The process id of the LWP. This is a combination of the LWP id
76 and overall process id. */
79 /* Non-zero if this LWP is cloned. In this context "cloned" means
80 that the LWP is reporting to its parent using a signal other than
84 /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report
88 /* Non-zero if this LWP is stopped. */
91 /* Non-zero if this LWP will be/has been resumed. Note that an LWP
92 can be marked both as stopped and resumed at the same time. This
93 happens if we try to resume an LWP that has a wait status
94 pending. We shouldn't let the LWP run until that wait status has
95 been processed, but we should not report that wait status if GDB
96 didn't try to let the LWP run. */
99 /* If non-zero, a pending wait status. */
102 /* Non-zero if we were stepping this LWP. */
105 /* Next LWP in list. */
106 struct lwp_info *next;
109 /* List of known LWPs. */
110 static struct lwp_info *lwp_list;
112 /* Number of LWPs in the list. */
115 /* Non-zero if we're running in "threaded" mode. */
119 #define GET_LWP(ptid) ptid_get_lwp (ptid)
120 #define GET_PID(ptid) ptid_get_pid (ptid)
121 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
122 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
124 /* If the last reported event was a SIGTRAP, this variable is set to
125 the process id of the LWP/thread that got it. */
129 /* This module's target-specific operations. */
130 static struct target_ops lin_lwp_ops;
132 /* The standard child operations. */
133 extern struct target_ops child_ops;
135 /* Since we cannot wait (in lin_lwp_wait) for the initial process and
136 any cloned processes with a single call to waitpid, we have to use
137 the WNOHANG flag and call waitpid in a loop. To optimize
138 things a bit we use `sigsuspend' to wake us up when a process has
139 something to report (it will send us a SIGCHLD if it has). To make
140 this work we have to juggle with the signal mask. We save the
141 original signal mask such that we can restore it before creating a
142 new process in order to avoid blocking certain signals in the
143 inferior. We then block SIGCHLD during the waitpid/sigsuspend
146 /* Original signal mask. */
147 static sigset_t normal_mask;
149 /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
150 _initialize_lin_lwp. */
151 static sigset_t suspend_mask;
153 /* Signals to block to make that sigsuspend work. */
154 static sigset_t blocked_mask;
157 /* Prototypes for local functions. */
158 static int stop_wait_callback (struct lwp_info *lp, void *data);
160 /* Convert wait status STATUS to a string. Used for printing debug
164 status_to_str (int status)
168 if (WIFSTOPPED (status))
169 snprintf (buf, sizeof (buf), "%s (stopped)",
170 strsignal (WSTOPSIG (status)));
171 else if (WIFSIGNALED (status))
172 snprintf (buf, sizeof (buf), "%s (terminated)",
173 strsignal (WSTOPSIG (status)));
175 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
180 /* Initialize the list of LWPs. Note that this module, contrary to
181 what GDB's generic threads layer does for its thread list,
182 re-initializes the LWP lists whenever we mourn or detach (which
183 doesn't involve mourning) the inferior. */
188 struct lwp_info *lp, *lpnext;
190 for (lp = lwp_list; lp; lp = lpnext)
201 /* Add the LWP specified by PID to the list. If this causes the
202 number of LWPs to become larger than one, go into "threaded" mode.
203 Return a pointer to the structure describing the new LWP. */
205 static struct lwp_info *
206 add_lwp (ptid_t ptid)
210 gdb_assert (is_lwp (ptid));
212 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
214 memset (lp, 0, sizeof (struct lwp_info));
226 /* Remove the LWP specified by PID from the list. */
229 delete_lwp (ptid_t ptid)
231 struct lwp_info *lp, *lpprev;
235 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
236 if (ptid_equal (lp->ptid, ptid))
242 /* We don't go back to "non-threaded" mode if the number of threads
243 becomes less than two. */
247 lpprev->next = lp->next;
254 /* Return a pointer to the structure describing the LWP corresponding
255 to PID. If no corresponding LWP could be found, return NULL. */
257 static struct lwp_info *
258 find_lwp_pid (ptid_t ptid)
264 lwp = GET_LWP (ptid);
266 lwp = GET_PID (ptid);
268 for (lp = lwp_list; lp; lp = lp->next)
269 if (lwp == GET_LWP (lp->ptid))
275 /* Call CALLBACK with its second argument set to DATA for every LWP in
276 the list. If CALLBACK returns 1 for a particular LWP, return a
277 pointer to the structure describing that LWP immediately.
278 Otherwise return NULL. */
281 iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
283 struct lwp_info *lp, *lpnext;
285 for (lp = lwp_list; lp; lp = lpnext)
288 if ((*callback) (lp, data))
296 /* Implementation of the PREPARE_TO_PROCEED hook for the GNU/Linux LWP
299 Note that this implementation is potentially redundant now that
300 default_prepare_to_proceed() has been added.
302 FIXME This may not support switching threads after Ctrl-C
303 correctly. The default implementation does support this. */
306 lin_lwp_prepare_to_proceed (void)
308 if (!ptid_equal (trap_ptid, null_ptid)
309 && !ptid_equal (inferior_ptid, trap_ptid))
311 /* Switched over from TRAP_PID. */
312 CORE_ADDR stop_pc = read_pc ();
315 /* Avoid switching where it wouldn't do any good, i.e. if both
316 threads are at the same breakpoint. */
317 trap_pc = read_pc_pid (trap_ptid);
318 if (trap_pc != stop_pc && breakpoint_here_p (trap_pc))
320 /* User hasn't deleted the breakpoint. Return non-zero, and
321 switch back to TRAP_PID. */
322 inferior_ptid = trap_ptid;
324 /* FIXME: Is this stuff really necessary? */
325 flush_cached_frames ();
326 registers_changed ();
338 lin_lwp_open (char *args, int from_tty)
340 push_target (&lin_lwp_ops);
344 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
345 a message telling the user that a new LWP has been added to the
349 lin_lwp_attach_lwp (ptid_t ptid, int verbose)
353 gdb_assert (is_lwp (ptid));
355 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
356 to interrupt either the ptrace() or waitpid() calls below. */
357 if (!sigismember (&blocked_mask, SIGCHLD))
359 sigaddset (&blocked_mask, SIGCHLD);
360 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
364 printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
366 lp = find_lwp_pid (ptid);
370 /* We assume that we're already attached to any LWP that has an
371 id equal to the overall process id. */
372 if (GET_LWP (ptid) != GET_PID (ptid))
377 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
378 error ("Can't attach %s: %s", target_pid_to_str (ptid),
379 safe_strerror (errno));
382 fprintf_unfiltered (gdb_stdlog,
383 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
384 target_pid_to_str (ptid));
386 pid = waitpid (GET_LWP (ptid), &status, 0);
387 if (pid == -1 && errno == ECHILD)
389 /* Try again with __WCLONE to check cloned processes. */
390 pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
394 gdb_assert (pid == GET_LWP (ptid)
395 && WIFSTOPPED (status) && WSTOPSIG (status));
401 fprintf_unfiltered (gdb_stdlog,
402 "LLAL: waitpid %s received %s\n",
403 target_pid_to_str (ptid),
404 status_to_str (status));
409 /* We assume that the LWP representing the original process
410 is already stopped. Mark it as stopped in the data structure
411 that the lin-lwp layer uses to keep track of threads. Note
412 that this won't have already been done since the main thread
413 will have, we assume, been stopped by an attach from a
420 lin_lwp_attach (char *args, int from_tty)
426 /* FIXME: We should probably accept a list of process id's, and
427 attach all of them. */
428 child_ops.to_attach (args, from_tty);
430 /* Add the initial process as the first LWP to the list. */
431 lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
433 /* Make sure the initial process is stopped. The user-level threads
434 layer might want to poke around in the inferior, and that won't
435 work if things haven't stabilized yet. */
436 pid = waitpid (GET_PID (inferior_ptid), &status, 0);
437 if (pid == -1 && errno == ECHILD)
439 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
441 /* Try again with __WCLONE to check cloned processes. */
442 pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
446 gdb_assert (pid == GET_PID (inferior_ptid)
447 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
451 /* Fake the SIGSTOP that core GDB expects. */
452 lp->status = W_STOPCODE (SIGSTOP);
456 fprintf_unfiltered (gdb_stdlog,
457 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid);
462 detach_callback (struct lwp_info *lp, void *data)
464 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
466 if (debug_lin_lwp && lp->status)
467 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
468 strsignal (WSTOPSIG (lp->status)),
469 target_pid_to_str (lp->ptid));
471 while (lp->signalled && lp->stopped)
474 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
475 WSTOPSIG (lp->status)) < 0)
476 error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
477 safe_strerror (errno));
480 fprintf_unfiltered (gdb_stdlog,
481 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
482 target_pid_to_str (lp->ptid),
483 status_to_str (lp->status));
488 stop_wait_callback (lp, NULL);
490 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
493 /* We don't actually detach from the LWP that has an id equal to the
494 overall process id just yet. */
495 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
498 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
499 WSTOPSIG (lp->status)) < 0)
500 error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
501 safe_strerror (errno));
504 fprintf_unfiltered (gdb_stdlog,
505 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
506 target_pid_to_str (lp->ptid),
507 strsignal (WSTOPSIG (lp->status)));
509 delete_lwp (lp->ptid);
516 lin_lwp_detach (char *args, int from_tty)
518 iterate_over_lwps (detach_callback, NULL);
520 /* Only the initial process should be left right now. */
521 gdb_assert (num_lwps == 1);
523 trap_ptid = null_ptid;
525 /* Destroy LWP info; it's no longer valid. */
528 /* Restore the original signal mask. */
529 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
530 sigemptyset (&blocked_mask);
532 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
533 child_ops.to_detach (args, from_tty);
540 resume_callback (struct lwp_info *lp, void *data)
542 if (lp->stopped && lp->status == 0)
544 struct thread_info *tp;
546 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
548 fprintf_unfiltered (gdb_stdlog,
549 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
550 target_pid_to_str (lp->ptid));
559 resume_clear_callback (struct lwp_info *lp, void *data)
566 resume_set_callback (struct lwp_info *lp, void *data)
573 lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
578 /* A specific PTID means `step only this process id'. */
579 resume_all = (PIDGET (ptid) == -1);
582 iterate_over_lwps (resume_set_callback, NULL);
584 iterate_over_lwps (resume_clear_callback, NULL);
586 /* If PID is -1, it's the current inferior that should be
587 handled specially. */
588 if (PIDGET (ptid) == -1)
589 ptid = inferior_ptid;
591 lp = find_lwp_pid (ptid);
594 ptid = pid_to_ptid (GET_LWP (lp->ptid));
596 /* Remember if we're stepping. */
599 /* Mark this LWP as resumed. */
602 /* If we have a pending wait status for this thread, there is no
603 point in resuming the process. */
606 /* FIXME: What should we do if we are supposed to continue
607 this thread with a signal? */
608 gdb_assert (signo == TARGET_SIGNAL_0);
612 /* Mark LWP as not stopped to prevent it from being continued by
618 iterate_over_lwps (resume_callback, NULL);
620 child_resume (ptid, step, signo);
622 fprintf_unfiltered (gdb_stdlog,
623 "LLR: %s %s, %s (resume event thread)\n",
624 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
625 target_pid_to_str (ptid),
626 signo ? strsignal (signo) : "0");
630 /* Send a SIGSTOP to LP. */
633 stop_callback (struct lwp_info *lp, void *data)
635 if (!lp->stopped && !lp->signalled)
641 fprintf_unfiltered (gdb_stdlog,
642 "SC: kill %s **<SIGSTOP>**\n",
643 target_pid_to_str (lp->ptid));
645 ret = kill (GET_LWP (lp->ptid), SIGSTOP);
646 gdb_assert (ret == 0);
649 gdb_assert (lp->status == 0);
655 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
656 a pointer to a set of signals to be flushed immediately. */
659 stop_wait_callback (struct lwp_info *lp, void *data)
661 sigset_t *flush_mask = data;
663 if (!lp->stopped && lp->signalled)
668 gdb_assert (lp->status == 0);
670 pid = waitpid (GET_LWP (lp->ptid), &status, lp->cloned ? __WCLONE : 0);
671 if (pid == -1 && errno == ECHILD)
672 /* OK, the proccess has disappeared. We'll catch the actual
673 exit event in lin_lwp_wait. */
676 gdb_assert (pid == GET_LWP (lp->ptid));
680 fprintf_unfiltered (gdb_stdlog,
681 "SWC: waitpid %s received %s\n",
682 target_pid_to_str (lp->ptid),
683 status_to_str (status));
686 if (WIFEXITED (status) || WIFSIGNALED (status))
688 gdb_assert (num_lwps > 1);
690 if (in_thread_list (lp->ptid))
692 /* Core GDB cannot deal with us deleting the current
694 if (!ptid_equal (lp->ptid, inferior_ptid))
695 delete_thread (lp->ptid);
696 printf_unfiltered ("[%s exited]\n",
697 target_pid_to_str (lp->ptid));
700 fprintf_unfiltered (gdb_stdlog, "SWC: %s exited.\n",
701 target_pid_to_str (lp->ptid));
703 delete_lwp (lp->ptid);
707 gdb_assert (WIFSTOPPED (status));
709 /* Ignore any signals in FLUSH_MASK. */
710 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
713 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
715 fprintf_unfiltered (gdb_stdlog,
716 "PTRACE_CONT %s, 0, 0 (%s)\n",
717 target_pid_to_str (lp->ptid),
718 errno ? safe_strerror (errno) : "OK");
720 return stop_wait_callback (lp, flush_mask);
723 if (WSTOPSIG (status) != SIGSTOP)
725 if (WSTOPSIG (status) == SIGTRAP)
727 /* If a LWP other than the LWP that we're reporting an
728 event for has hit a GDB breakpoint (as opposed to
729 some random trap signal), then just arrange for it to
730 hit it again later. We don't keep the SIGTRAP status
731 and don't forward the SIGTRAP signal to the LWP. We
732 will handle the current event, eventually we will
733 resume all LWPs, and this one will get its breakpoint
736 If we do not do this, then we run the risk that the
737 user will delete or disable the breakpoint, but the
738 thread will have already tripped on it. */
740 /* Now resume this LWP and get the SIGSTOP event. */
742 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
745 fprintf_unfiltered (gdb_stdlog,
746 "PTRACE_CONT %s, 0, 0 (%s)\n",
747 target_pid_to_str (lp->ptid),
748 errno ? safe_strerror (errno) : "OK");
750 fprintf_unfiltered (gdb_stdlog,
751 "SWC: Candidate SIGTRAP event in %s\n",
752 target_pid_to_str (lp->ptid));
754 /* Hold the SIGTRAP for handling by lin_lwp_wait. */
755 stop_wait_callback (lp, data);
756 /* If there's another event, throw it back into the queue. */
759 kill (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
761 /* Save the sigtrap event. */
767 /* The thread was stopped with a signal other than
768 SIGSTOP, and didn't accidentally trip a breakpoint. */
772 fprintf_unfiltered (gdb_stdlog,
773 "SWC: Pending event %s in %s\n",
774 status_to_str ((int) status),
775 target_pid_to_str (lp->ptid));
777 /* Now resume this LWP and get the SIGSTOP event. */
779 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
781 fprintf_unfiltered (gdb_stdlog,
782 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
783 target_pid_to_str (lp->ptid),
784 errno ? safe_strerror (errno) : "OK");
786 /* Hold this event/waitstatus while we check to see if
787 there are any more (we still want to get that SIGSTOP). */
788 stop_wait_callback (lp, data);
789 /* If the lp->status field is still empty, use it to hold
790 this event. If not, then this event must be returned
791 to the event queue of the LWP. */
798 fprintf_unfiltered (gdb_stdlog,
799 "SWC: kill %s, %s\n",
800 target_pid_to_str (lp->ptid),
801 status_to_str ((int) status));
803 kill (GET_LWP (lp->ptid), WSTOPSIG (status));
810 /* We caught the SIGSTOP that we intended to catch, so
811 there's no SIGSTOP pending. */
820 /* Return non-zero if LP has a wait status pending. */
823 status_callback (struct lwp_info *lp, void *data)
825 /* Only report a pending wait status if we pretend that this has
826 indeed been resumed. */
827 return (lp->status != 0 && lp->resumed);
830 /* Return non-zero if LP isn't stopped. */
833 running_callback (struct lwp_info *lp, void *data)
835 return (lp->stopped == 0);
838 /* Count the LWP's that have had events. */
841 count_events_callback (struct lwp_info *lp, void *data)
845 gdb_assert (count != NULL);
847 /* Count only LWPs that have a SIGTRAP event pending. */
849 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
855 /* Select the LWP (if any) that is currently being single-stepped. */
858 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
860 if (lp->step && lp->status != 0)
866 /* Select the Nth LWP that has had a SIGTRAP event. */
869 select_event_lwp_callback (struct lwp_info *lp, void *data)
871 int *selector = data;
873 gdb_assert (selector != NULL);
875 /* Select only LWPs that have a SIGTRAP event pending. */
877 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
878 if ((*selector)-- == 0)
885 cancel_breakpoints_callback (struct lwp_info *lp, void *data)
887 struct lwp_info *event_lp = data;
889 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
893 /* If a LWP other than the LWP that we're reporting an event for has
894 hit a GDB breakpoint (as opposed to some random trap signal),
895 then just arrange for it to hit it again later. We don't keep
896 the SIGTRAP status and don't forward the SIGTRAP signal to the
897 LWP. We will handle the current event, eventually we will resume
898 all LWPs, and this one will get its breakpoint trap again.
900 If we do not do this, then we run the risk that the user will
901 delete or disable the breakpoint, but the LWP will have already
905 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
906 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
907 DECR_PC_AFTER_BREAK))
910 fprintf_unfiltered (gdb_stdlog,
911 "CBC: Push back breakpoint for %s\n",
912 target_pid_to_str (lp->ptid));
914 /* Back up the PC if necessary. */
915 if (DECR_PC_AFTER_BREAK)
916 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
918 /* Throw away the SIGTRAP. */
925 /* Select one LWP out of those that have events pending. */
928 select_event_lwp (struct lwp_info **orig_lp, int *status)
932 struct lwp_info *event_lp;
934 /* Record the wait status for the origional LWP. */
935 (*orig_lp)->status = *status;
937 /* Give preference to any LWP that is being single-stepped. */
938 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
939 if (event_lp != NULL)
942 fprintf_unfiltered (gdb_stdlog,
943 "SEL: Select single-step %s\n",
944 target_pid_to_str (event_lp->ptid));
948 /* No single-stepping LWP. Select one at random, out of those
949 which have had SIGTRAP events. */
951 /* First see how many SIGTRAP events we have. */
952 iterate_over_lwps (count_events_callback, &num_events);
954 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
955 random_selector = (int)
956 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
958 if (debug_lin_lwp && num_events > 1)
959 fprintf_unfiltered (gdb_stdlog,
960 "SEL: Found %d SIGTRAP events, selecting #%d\n",
961 num_events, random_selector);
963 event_lp = iterate_over_lwps (select_event_lwp_callback,
967 if (event_lp != NULL)
969 /* Switch the event LWP. */
971 *status = event_lp->status;
974 /* Flush the wait status for the event LWP. */
975 (*orig_lp)->status = 0;
978 /* Return non-zero if LP has been resumed. */
981 resumed_callback (struct lwp_info *lp, void *data)
988 /* We need to override child_wait to support attaching to cloned
989 processes, since a normal wait (as done by the default version)
990 ignores those processes. */
992 /* Wait for child PTID to do something. Return id of the child,
993 minus_one_ptid in case of error; store status into *OURSTATUS. */
996 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1004 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1005 attached process. */
1008 pid = waitpid (GET_PID (ptid), &status, 0);
1009 if (pid == -1 && errno == ECHILD)
1010 /* Try again with __WCLONE to check cloned processes. */
1011 pid = waitpid (GET_PID (ptid), &status, __WCLONE);
1015 fprintf_unfiltered (gdb_stdlog,
1016 "CW: waitpid %ld received %s\n",
1017 (long) pid, status_to_str (status));
1022 /* Make sure we don't report an event for the exit of the
1023 original program, if we've detached from it. */
1024 if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid))
1030 clear_sigio_trap ();
1031 clear_sigint_trap ();
1033 while (pid == -1 && save_errno == EINTR);
1037 warning ("Child process unexpectedly missing: %s",
1038 safe_strerror (errno));
1040 /* Claim it exited with unknown signal. */
1041 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1042 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1043 return minus_one_ptid;
1046 store_waitstatus (ourstatus, status);
1047 return pid_to_ptid (pid);
1053 lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1055 struct lwp_info *lp = NULL;
1058 pid_t pid = PIDGET (ptid);
1059 sigset_t flush_mask;
1061 sigemptyset (&flush_mask);
1063 /* Make sure SIGCHLD is blocked. */
1064 if (!sigismember (&blocked_mask, SIGCHLD))
1066 sigaddset (&blocked_mask, SIGCHLD);
1067 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1072 /* Make sure there is at least one LWP that has been resumed, at
1073 least if there are any LWPs at all. */
1074 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
1076 /* First check if there is a LWP with a wait status pending. */
1079 /* Any LWP that's been resumed will do. */
1080 lp = iterate_over_lwps (status_callback, NULL);
1083 status = lp->status;
1086 if (debug_lin_lwp && status)
1087 fprintf_unfiltered (gdb_stdlog,
1088 "LLW: Using pending wait status %s for %s.\n",
1089 status_to_str (status),
1090 target_pid_to_str (lp->ptid));
1093 /* But if we don't fine one, we'll have to wait, and check both
1094 cloned and uncloned processes. We start with the cloned
1096 options = __WCLONE | WNOHANG;
1098 else if (is_lwp (ptid))
1101 fprintf_unfiltered (gdb_stdlog,
1102 "LLW: Waiting for specific LWP %s.\n",
1103 target_pid_to_str (ptid));
1105 /* We have a specific LWP to check. */
1106 lp = find_lwp_pid (ptid);
1108 status = lp->status;
1111 if (debug_lin_lwp && status)
1112 fprintf_unfiltered (gdb_stdlog,
1113 "LLW: Using pending wait status %s for %s.\n",
1114 status_to_str (status),
1115 target_pid_to_str (lp->ptid));
1117 /* If we have to wait, take into account whether PID is a cloned
1118 process or not. And we have to convert it to something that
1119 the layer beneath us can understand. */
1120 options = lp->cloned ? __WCLONE : 0;
1121 pid = GET_LWP (ptid);
1124 if (status && lp->signalled)
1126 /* A pending SIGSTOP may interfere with the normal stream of
1127 events. In a typical case where interference is a problem,
1128 we have a SIGSTOP signal pending for LWP A while
1129 single-stepping it, encounter an event in LWP B, and take the
1130 pending SIGSTOP while trying to stop LWP A. After processing
1131 the event in LWP B, LWP A is continued, and we'll never see
1132 the SIGTRAP associated with the last time we were
1133 single-stepping LWP A. */
1135 /* Resume the thread. It should halt immediately returning the
1137 registers_changed ();
1138 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1141 fprintf_unfiltered (gdb_stdlog,
1142 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1143 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1144 target_pid_to_str (lp->ptid));
1146 gdb_assert (lp->resumed);
1148 /* This should catch the pending SIGSTOP. */
1149 stop_wait_callback (lp, NULL);
1152 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1153 attached process. */
1160 lwpid = waitpid (pid, &status, options);
1163 gdb_assert (pid == -1 || lwpid == pid);
1167 fprintf_unfiltered (gdb_stdlog,
1168 "LLW: waitpid %ld received %s\n",
1169 (long) lwpid, status_to_str (status));
1172 lp = find_lwp_pid (pid_to_ptid (lwpid));
1174 /* Make sure we don't report an event for the exit of an LWP not in
1175 our list, i.e. not part of the current process. This can happen
1176 if we detach from a program we original forked and then it
1178 if (!WIFSTOPPED (status) && !lp)
1186 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
1187 if (options & __WCLONE)
1192 gdb_assert (WIFSTOPPED (status)
1193 && WSTOPSIG (status) == SIGSTOP);
1196 if (!in_thread_list (inferior_ptid))
1198 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1199 GET_PID (inferior_ptid));
1200 add_thread (inferior_ptid);
1203 add_thread (lp->ptid);
1204 printf_unfiltered ("[New %s]\n",
1205 target_pid_to_str (lp->ptid));
1209 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
1210 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
1211 left in the process. */
1212 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
1214 if (in_thread_list (lp->ptid))
1216 /* Core GDB cannot deal with us deleting the current
1218 if (!ptid_equal (lp->ptid, inferior_ptid))
1219 delete_thread (lp->ptid);
1220 printf_unfiltered ("[%s exited]\n",
1221 target_pid_to_str (lp->ptid));
1224 fprintf_unfiltered (gdb_stdlog,
1225 "LLW: %s exited.\n",
1226 target_pid_to_str (lp->ptid));
1228 delete_lwp (lp->ptid);
1230 /* Make sure there is at least one thread running. */
1231 gdb_assert (iterate_over_lwps (running_callback, NULL));
1233 /* Discard the event. */
1238 /* Make sure we don't report a SIGSTOP that we sent
1239 ourselves in an attempt to stop an LWP. */
1241 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
1244 fprintf_unfiltered (gdb_stdlog,
1245 "LLW: Delayed SIGSTOP caught for %s.\n",
1246 target_pid_to_str (lp->ptid));
1248 /* This is a delayed SIGSTOP. */
1251 registers_changed ();
1252 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1255 fprintf_unfiltered (gdb_stdlog,
1256 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
1258 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1259 target_pid_to_str (lp->ptid));
1262 gdb_assert (lp->resumed);
1264 /* Discard the event. */
1274 /* Alternate between checking cloned and uncloned processes. */
1275 options ^= __WCLONE;
1277 /* And suspend every time we have checked both. */
1278 if (options & __WCLONE)
1279 sigsuspend (&suspend_mask);
1282 /* We shouldn't end up here unless we want to try again. */
1283 gdb_assert (status == 0);
1286 clear_sigio_trap ();
1287 clear_sigint_trap ();
1291 /* Don't report signals that GDB isn't interested in, such as
1292 signals that are neither printed nor stopped upon. Stopping all
1293 threads can be a bit time-consuming so if we want decent
1294 performance with heavily multi-threaded programs, especially when
1295 they're using a high frequency timer, we'd better avoid it if we
1298 if (WIFSTOPPED (status))
1300 int signo = target_signal_from_host (WSTOPSIG (status));
1302 if (signal_stop_state (signo) == 0
1303 && signal_print_state (signo) == 0
1304 && signal_pass_state (signo) == 1)
1306 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
1307 here? It is not clear we should. GDB may not expect
1308 other threads to run. On the other hand, not resuming
1309 newly attached threads may cause an unwanted delay in
1310 getting them running. */
1311 registers_changed ();
1312 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
1314 fprintf_unfiltered (gdb_stdlog,
1315 "LLW: %s %s, %s (preempt 'handle')\n",
1317 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1318 target_pid_to_str (lp->ptid),
1319 signo ? strsignal (signo) : "0");
1325 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
1327 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
1328 forwarded to the entire process group, that is, all LWP's
1329 will receive it. Since we only want to report it once,
1330 we try to flush it from all LWPs except this one. */
1331 sigaddset (&flush_mask, SIGINT);
1335 /* This LWP is stopped now. */
1339 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
1340 status_to_str (status), target_pid_to_str (lp->ptid));
1342 /* Now stop all other LWP's ... */
1343 iterate_over_lwps (stop_callback, NULL);
1345 /* ... and wait until all of them have reported back that they're no
1347 iterate_over_lwps (stop_wait_callback, &flush_mask);
1349 /* If we're not waiting for a specific LWP, choose an event LWP from
1350 among those that have had events. Giving equal priority to all
1351 LWPs that have had events helps prevent starvation. */
1353 select_event_lwp (&lp, &status);
1355 /* Now that we've selected our final event LWP, cancel any
1356 breakpoints in other LWPs that have hit a GDB breakpoint. See
1357 the comment in cancel_breakpoints_callback to find out why. */
1358 iterate_over_lwps (cancel_breakpoints_callback, lp);
1360 /* If we're not running in "threaded" mode, we'll report the bare
1363 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
1365 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1367 fprintf_unfiltered (gdb_stdlog,
1368 "LLW: trap_ptid is %s.\n",
1369 target_pid_to_str (trap_ptid));
1372 trap_ptid = null_ptid;
1374 store_waitstatus (ourstatus, status);
1375 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1379 kill_callback (struct lwp_info *lp, void *data)
1382 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
1384 fprintf_unfiltered (gdb_stdlog,
1385 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
1386 target_pid_to_str (lp->ptid),
1387 errno ? safe_strerror (errno) : "OK");
1393 kill_wait_callback (struct lwp_info *lp, void *data)
1397 /* We must make sure that there are no pending events (delayed
1398 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
1399 program doesn't interfere with any following debugging session. */
1401 /* For cloned processes we must check both with __WCLONE and
1402 without, since the exit status of a cloned process isn't reported
1408 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
1409 if (pid != (pid_t) -1 && debug_lin_lwp)
1411 fprintf_unfiltered (gdb_stdlog,
1412 "KWC: wait %s received unknown.\n",
1413 target_pid_to_str (lp->ptid));
1416 while (pid == GET_LWP (lp->ptid));
1418 gdb_assert (pid == -1 && errno == ECHILD);
1423 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
1424 if (pid != (pid_t) -1 && debug_lin_lwp)
1426 fprintf_unfiltered (gdb_stdlog,
1427 "KWC: wait %s received unk.\n",
1428 target_pid_to_str (lp->ptid));
1431 while (pid == GET_LWP (lp->ptid));
1433 gdb_assert (pid == -1 && errno == ECHILD);
1440 /* Kill all LWP's ... */
1441 iterate_over_lwps (kill_callback, NULL);
1443 /* ... and wait until we've flushed all events. */
1444 iterate_over_lwps (kill_wait_callback, NULL);
1446 target_mourn_inferior ();
1450 lin_lwp_create_inferior (char *exec_file, char *allargs, char **env)
1452 child_ops.to_create_inferior (exec_file, allargs, env);
1456 lin_lwp_mourn_inferior (void)
1458 trap_ptid = null_ptid;
1460 /* Destroy LWP info; it's no longer valid. */
1463 /* Restore the original signal mask. */
1464 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1465 sigemptyset (&blocked_mask);
1467 child_ops.to_mourn_inferior ();
1471 lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
1472 struct mem_attrib *attrib, struct target_ops *target)
1474 struct cleanup *old_chain = save_inferior_ptid ();
1477 if (is_lwp (inferior_ptid))
1478 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
1480 xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1482 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1484 do_cleanups (old_chain);
1489 lin_lwp_thread_alive (ptid_t ptid)
1491 gdb_assert (is_lwp (ptid));
1494 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
1496 fprintf_unfiltered (gdb_stdlog,
1497 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
1498 target_pid_to_str (ptid),
1499 errno ? safe_strerror (errno) : "OK");
1507 lin_lwp_pid_to_str (ptid_t ptid)
1509 static char buf[64];
1513 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
1517 return normal_pid_to_str (ptid);
1521 init_lin_lwp_ops (void)
1524 lin_lwp_ops.to_open = lin_lwp_open;
1526 lin_lwp_ops.to_shortname = "lwp-layer";
1527 lin_lwp_ops.to_longname = "lwp-layer";
1528 lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
1529 lin_lwp_ops.to_attach = lin_lwp_attach;
1530 lin_lwp_ops.to_detach = lin_lwp_detach;
1531 lin_lwp_ops.to_resume = lin_lwp_resume;
1532 lin_lwp_ops.to_wait = lin_lwp_wait;
1533 /* fetch_inferior_registers and store_inferior_registers will
1534 honor the LWP id, so we can use them directly. */
1535 lin_lwp_ops.to_fetch_registers = fetch_inferior_registers;
1536 lin_lwp_ops.to_store_registers = store_inferior_registers;
1537 lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
1538 lin_lwp_ops.to_kill = lin_lwp_kill;
1539 lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
1540 lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
1541 lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
1542 lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
1543 lin_lwp_ops.to_stratum = thread_stratum;
1544 lin_lwp_ops.to_has_thread_control = tc_schedlock;
1545 lin_lwp_ops.to_magic = OPS_MAGIC;
1549 sigchld_handler (int signo)
1551 /* Do nothing. The only reason for this handler is that it allows
1552 us to use sigsuspend in lin_lwp_wait above to wait for the
1553 arrival of a SIGCHLD. */
1557 _initialize_lin_lwp (void)
1559 struct sigaction action;
1561 extern void thread_db_init (struct target_ops *);
1563 init_lin_lwp_ops ();
1564 add_target (&lin_lwp_ops);
1565 thread_db_init (&lin_lwp_ops);
1567 /* Save the original signal mask. */
1568 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
1570 action.sa_handler = sigchld_handler;
1571 sigemptyset (&action.sa_mask);
1572 action.sa_flags = 0;
1573 sigaction (SIGCHLD, &action, NULL);
1575 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1576 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
1577 sigdelset (&suspend_mask, SIGCHLD);
1579 sigemptyset (&blocked_mask);
1581 add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
1582 (char *) &debug_lin_lwp,
1583 "Set debugging of GNU/Linux lwp module.\n\
1584 Enables printf debugging output.\n", &setdebuglist), &showdebuglist);
1588 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
1589 the GNU/Linux Threads library and therefore doesn't really belong
1592 /* Read variable NAME in the target and return its value if found.
1593 Otherwise return zero. It is assumed that the type of the variable
1597 get_signo (const char *name)
1599 struct minimal_symbol *ms;
1602 ms = lookup_minimal_symbol (name, NULL, NULL);
1606 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
1607 sizeof (signo)) != 0)
1613 /* Return the set of signals used by the threads library in *SET. */
1616 lin_thread_get_thread_signals (sigset_t *set)
1618 struct sigaction action;
1619 int restart, cancel;
1623 restart = get_signo ("__pthread_sig_restart");
1627 cancel = get_signo ("__pthread_sig_cancel");
1631 sigaddset (set, restart);
1632 sigaddset (set, cancel);
1634 /* The GNU/Linux Threads library makes terminating threads send a
1635 special "cancel" signal instead of SIGCHLD. Make sure we catch
1636 those (to prevent them from terminating GDB itself, which is
1637 likely to be their default action) and treat them the same way as
1640 action.sa_handler = sigchld_handler;
1641 sigemptyset (&action.sa_mask);
1642 action.sa_flags = 0;
1643 sigaction (cancel, &action, NULL);
1645 /* We block the "cancel" signal throughout this code ... */
1646 sigaddset (&blocked_mask, cancel);
1647 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1649 /* ... except during a sigsuspend. */
1650 sigdelset (&suspend_mask, cancel);