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)",
176 WEXITSTATUS (status
));
181 /* Initialize the list of LWPs. Note that this module, contrary to
182 what GDB's generic threads layer does for its thread list,
183 re-initializes the LWP lists whenever we mourn or detach (which
184 doesn't involve mourning) the inferior. */
189 struct lwp_info
*lp
, *lpnext
;
191 for (lp
= lwp_list
; lp
; lp
= lpnext
)
202 /* Add the LWP specified by PID to the list. If this causes the
203 number of LWPs to become larger than one, go into "threaded" mode.
204 Return a pointer to the structure describing the new LWP. */
206 static struct lwp_info
*
207 add_lwp (ptid_t ptid
)
211 gdb_assert (is_lwp (ptid
));
213 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
215 memset (lp
, 0, sizeof (struct lwp_info
));
227 /* Remove the LWP specified by PID from the list. */
230 delete_lwp (ptid_t ptid
)
232 struct lwp_info
*lp
, *lpprev
;
236 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
237 if (ptid_equal (lp
->ptid
, ptid
))
243 /* We don't go back to "non-threaded" mode if the number of threads
244 becomes less than two. */
248 lpprev
->next
= lp
->next
;
255 /* Return a pointer to the structure describing the LWP corresponding
256 to PID. If no corresponding LWP could be found, return NULL. */
258 static struct lwp_info
*
259 find_lwp_pid (ptid_t ptid
)
265 lwp
= GET_LWP (ptid
);
267 lwp
= GET_PID (ptid
);
269 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
270 if (lwp
== GET_LWP (lp
->ptid
))
276 /* Call CALLBACK with its second argument set to DATA for every LWP in
277 the list. If CALLBACK returns 1 for a particular LWP, return a
278 pointer to the structure describing that LWP immediately.
279 Otherwise return NULL. */
282 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
284 struct lwp_info
*lp
, *lpnext
;
286 for (lp
= lwp_list
; lp
; lp
= lpnext
)
289 if ((*callback
) (lp
, data
))
297 /* Implementation of the PREPARE_TO_PROCEED hook for the GNU/Linux LWP
300 Note that this implementation is potentially redundant now that
301 default_prepare_to_proceed() has been added.
303 FIXME This may not support switching threads after Ctrl-C
304 correctly. The default implementation does support this. */
307 lin_lwp_prepare_to_proceed (void)
309 if (! ptid_equal (trap_ptid
, null_ptid
)
310 && ! ptid_equal (inferior_ptid
, trap_ptid
))
312 /* Switched over from TRAP_PID. */
313 CORE_ADDR stop_pc
= read_pc ();
316 /* Avoid switching where it wouldn't do any good, i.e. if both
317 threads are at the same breakpoint. */
318 trap_pc
= read_pc_pid (trap_ptid
);
319 if (trap_pc
!= stop_pc
&& breakpoint_here_p (trap_pc
))
321 /* User hasn't deleted the breakpoint. Return non-zero, and
322 switch back to TRAP_PID. */
323 inferior_ptid
= trap_ptid
;
325 /* FIXME: Is this stuff really necessary? */
326 flush_cached_frames ();
327 registers_changed ();
339 lin_lwp_open (char *args
, int from_tty
)
341 push_target (&lin_lwp_ops
);
345 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
346 a message telling the user that a new LWP has been added to the
350 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
354 gdb_assert (is_lwp (ptid
));
356 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
357 to interrupt either the ptrace() or waitpid() calls below. */
358 if (! sigismember (&blocked_mask
, SIGCHLD
))
360 sigaddset (&blocked_mask
, SIGCHLD
);
361 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
365 printf_filtered ("[New %s]\n", target_pid_to_str (ptid
));
367 lp
= find_lwp_pid (ptid
);
371 /* We assume that we're already attached to any LWP that has an
372 id equal to the overall process id. */
373 if (GET_LWP (ptid
) != GET_PID (ptid
))
378 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
379 error ("Can't attach %s: %s", target_pid_to_str (ptid
),
380 safe_strerror (errno
));
383 fprintf_unfiltered (gdb_stdlog
,
384 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
385 target_pid_to_str (ptid
));
387 pid
= waitpid (GET_LWP (ptid
), &status
, 0);
388 if (pid
== -1 && errno
== ECHILD
)
390 /* Try again with __WCLONE to check cloned processes. */
391 pid
= waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
395 gdb_assert (pid
== GET_LWP (ptid
)
396 && WIFSTOPPED (status
) && WSTOPSIG (status
));
402 fprintf_unfiltered (gdb_stdlog
,
403 "LLAL: waitpid %s received %s\n",
404 target_pid_to_str (ptid
),
405 status_to_str (status
));
410 /* We assume that the LWP representing the original process
411 is already stopped. Mark it as stopped in the data structure
412 that the lin-lwp layer uses to keep track of threads. Note
413 that this won't have already been done since the main thread
414 will have, we assume, been stopped by an attach from a
421 lin_lwp_attach (char *args
, int from_tty
)
427 /* FIXME: We should probably accept a list of process id's, and
428 attach all of them. */
429 child_ops
.to_attach (args
, from_tty
);
431 /* Add the initial process as the first LWP to the list. */
432 lp
= add_lwp (BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
)));
434 /* Make sure the initial process is stopped. The user-level threads
435 layer might want to poke around in the inferior, and that won't
436 work if things haven't stabilized yet. */
437 pid
= waitpid (GET_PID (inferior_ptid
), &status
, 0);
438 if (pid
== -1 && errno
== ECHILD
)
440 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid
));
442 /* Try again with __WCLONE to check cloned processes. */
443 pid
= waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
447 gdb_assert (pid
== GET_PID (inferior_ptid
)
448 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
452 /* Fake the SIGSTOP that core GDB expects. */
453 lp
->status
= W_STOPCODE (SIGSTOP
);
457 fprintf_unfiltered (gdb_stdlog
,
458 "LLA: waitpid %ld, faking SIGSTOP\n",
464 detach_callback (struct lwp_info
*lp
, void *data
)
466 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
468 if (debug_lin_lwp
&& lp
->status
)
469 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
470 strsignal (WSTOPSIG (lp
->status
)),
471 target_pid_to_str (lp
->ptid
));
473 while (lp
->signalled
&& lp
->stopped
)
476 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
477 WSTOPSIG (lp
->status
)) < 0)
478 error ("Can't continue %s: %s", target_pid_to_str (lp
->ptid
),
479 safe_strerror (errno
));
482 fprintf_unfiltered (gdb_stdlog
,
483 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
484 target_pid_to_str (lp
->ptid
),
485 status_to_str (lp
->status
));
490 stop_wait_callback (lp
, NULL
);
492 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
495 /* We don't actually detach from the LWP that has an id equal to the
496 overall process id just yet. */
497 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
500 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
501 WSTOPSIG (lp
->status
)) < 0)
502 error ("Can't detach %s: %s", target_pid_to_str (lp
->ptid
),
503 safe_strerror (errno
));
506 fprintf_unfiltered (gdb_stdlog
,
507 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
508 target_pid_to_str (lp
->ptid
),
509 strsignal (WSTOPSIG (lp
->status
)));
511 delete_lwp (lp
->ptid
);
518 lin_lwp_detach (char *args
, int from_tty
)
520 iterate_over_lwps (detach_callback
, NULL
);
522 /* Only the initial process should be left right now. */
523 gdb_assert (num_lwps
== 1);
525 trap_ptid
= null_ptid
;
527 /* Destroy LWP info; it's no longer valid. */
530 /* Restore the original signal mask. */
531 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
532 sigemptyset (&blocked_mask
);
534 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
535 child_ops
.to_detach (args
, from_tty
);
542 resume_callback (struct lwp_info
*lp
, void *data
)
544 if (lp
->stopped
&& lp
->status
== 0)
546 struct thread_info
*tp
;
548 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), 0, TARGET_SIGNAL_0
);
550 fprintf_unfiltered (gdb_stdlog
,
551 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
552 target_pid_to_str (lp
->ptid
));
561 resume_clear_callback (struct lwp_info
*lp
, void *data
)
568 resume_set_callback (struct lwp_info
*lp
, void *data
)
575 lin_lwp_resume (ptid_t ptid
, int step
, enum target_signal signo
)
580 /* A specific PTID means `step only this process id'. */
581 resume_all
= (PIDGET (ptid
) == -1);
584 iterate_over_lwps (resume_set_callback
, NULL
);
586 iterate_over_lwps (resume_clear_callback
, NULL
);
588 /* If PID is -1, it's the current inferior that should be
589 handled specially. */
590 if (PIDGET (ptid
) == -1)
591 ptid
= inferior_ptid
;
593 lp
= find_lwp_pid (ptid
);
596 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
598 /* Remember if we're stepping. */
601 /* Mark this LWP as resumed. */
604 /* If we have a pending wait status for this thread, there is no
605 point in resuming the process. */
608 /* FIXME: What should we do if we are supposed to continue
609 this thread with a signal? */
610 gdb_assert (signo
== TARGET_SIGNAL_0
);
614 /* Mark LWP as not stopped to prevent it from being continued by
620 iterate_over_lwps (resume_callback
, NULL
);
622 child_resume (ptid
, step
, signo
);
624 fprintf_unfiltered (gdb_stdlog
,
625 "LLR: %s %s, %s (resume event thread)\n",
626 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
627 target_pid_to_str (ptid
),
628 signo
? strsignal (signo
) : "0");
632 /* Send a SIGSTOP to LP. */
635 stop_callback (struct lwp_info
*lp
, void *data
)
637 if (! lp
->stopped
&& ! lp
->signalled
)
643 fprintf_unfiltered (gdb_stdlog
,
644 "SC: kill %s **<SIGSTOP>**\n",
645 target_pid_to_str (lp
->ptid
));
647 ret
= kill (GET_LWP (lp
->ptid
), SIGSTOP
);
648 gdb_assert (ret
== 0);
651 gdb_assert (lp
->status
== 0);
657 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
658 a pointer to a set of signals to be flushed immediately. */
661 stop_wait_callback (struct lwp_info
*lp
, void *data
)
663 sigset_t
*flush_mask
= data
;
665 if (! lp
->stopped
&& lp
->signalled
)
670 gdb_assert (lp
->status
== 0);
672 pid
= waitpid (GET_LWP (lp
->ptid
), &status
, lp
->cloned
? __WCLONE
: 0);
673 if (pid
== -1 && errno
== ECHILD
)
674 /* OK, the proccess has disappeared. We'll catch the actual
675 exit event in lin_lwp_wait. */
678 gdb_assert (pid
== GET_LWP (lp
->ptid
));
682 fprintf_unfiltered (gdb_stdlog
,
683 "SWC: waitpid %s received %s\n",
684 target_pid_to_str (lp
->ptid
),
685 status_to_str (status
));
688 if (WIFEXITED (status
) || WIFSIGNALED (status
))
690 gdb_assert (num_lwps
> 1);
692 if (in_thread_list (lp
->ptid
))
694 /* Core GDB cannot deal with us deleting the current
696 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
697 delete_thread (lp
->ptid
);
698 printf_unfiltered ("[%s exited]\n",
699 target_pid_to_str (lp
->ptid
));
702 fprintf_unfiltered (gdb_stdlog
, "SWC: %s exited.\n",
703 target_pid_to_str (lp
->ptid
));
705 delete_lwp (lp
->ptid
);
709 gdb_assert (WIFSTOPPED (status
));
711 /* Ignore any signals in FLUSH_MASK. */
712 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
715 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
717 fprintf_unfiltered (gdb_stdlog
,
718 "PTRACE_CONT %s, 0, 0 (%s)\n",
719 target_pid_to_str (lp
->ptid
),
720 errno
? safe_strerror (errno
) : "OK");
722 return stop_wait_callback (lp
, flush_mask
);
725 if (WSTOPSIG (status
) != SIGSTOP
)
727 if (WSTOPSIG (status
) == SIGTRAP
)
729 /* If a LWP other than the LWP that we're reporting an
730 event for has hit a GDB breakpoint (as opposed to
731 some random trap signal), then just arrange for it to
732 hit it again later. We don't keep the SIGTRAP status
733 and don't forward the SIGTRAP signal to the LWP. We
734 will handle the current event, eventually we will
735 resume all LWPs, and this one will get its breakpoint
738 If we do not do this, then we run the risk that the
739 user will delete or disable the breakpoint, but the
740 thread will have already tripped on it. */
742 /* Now resume this LWP and get the SIGSTOP event. */
744 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
747 fprintf_unfiltered (gdb_stdlog
,
748 "PTRACE_CONT %s, 0, 0 (%s)\n",
749 target_pid_to_str (lp
->ptid
),
750 errno
? safe_strerror (errno
) : "OK");
752 fprintf_unfiltered (gdb_stdlog
,
753 "SWC: Candidate SIGTRAP event in %s\n",
754 target_pid_to_str (lp
->ptid
));
756 /* Hold the SIGTRAP for handling by lin_lwp_wait. */
757 stop_wait_callback (lp
, data
);
758 /* If there's another event, throw it back into the queue. */
761 kill (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
763 /* Save the sigtrap event. */
769 /* The thread was stopped with a signal other than
770 SIGSTOP, and didn't accidentally trip a breakpoint. */
774 fprintf_unfiltered (gdb_stdlog
,
775 "SWC: Pending event %s in %s\n",
776 status_to_str ((int) status
),
777 target_pid_to_str (lp
->ptid
));
779 /* Now resume this LWP and get the SIGSTOP event. */
781 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
783 fprintf_unfiltered (gdb_stdlog
,
784 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
785 target_pid_to_str (lp
->ptid
),
786 errno
? safe_strerror (errno
) : "OK");
788 /* Hold this event/waitstatus while we check to see if
789 there are any more (we still want to get that SIGSTOP). */
790 stop_wait_callback (lp
, data
);
791 /* If the lp->status field is still empty, use it to hold
792 this event. If not, then this event must be returned
793 to the event queue of the LWP. */
800 fprintf_unfiltered (gdb_stdlog
,
801 "SWC: kill %s, %s\n",
802 target_pid_to_str (lp
->ptid
),
803 status_to_str ((int) status
));
805 kill (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
812 /* We caught the SIGSTOP that we intended to catch, so
813 there's no SIGSTOP pending. */
822 /* Return non-zero if LP has a wait status pending. */
825 status_callback (struct lwp_info
*lp
, void *data
)
827 /* Only report a pending wait status if we pretend that this has
828 indeed been resumed. */
829 return (lp
->status
!= 0 && lp
->resumed
);
832 /* Return non-zero if LP isn't stopped. */
835 running_callback (struct lwp_info
*lp
, void *data
)
837 return (lp
->stopped
== 0);
840 /* Count the LWP's that have had events. */
843 count_events_callback (struct lwp_info
*lp
, void *data
)
847 gdb_assert (count
!= NULL
);
849 /* Count only LWPs that have a SIGTRAP event pending. */
851 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
857 /* Select the LWP (if any) that is currently being single-stepped. */
860 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
862 if (lp
->step
&& lp
->status
!= 0)
868 /* Select the Nth LWP that has had a SIGTRAP event. */
871 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
873 int *selector
= data
;
875 gdb_assert (selector
!= NULL
);
877 /* Select only LWPs that have a SIGTRAP event pending. */
879 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
880 if ((*selector
)-- == 0)
887 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
889 struct lwp_info
*event_lp
= data
;
891 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
895 /* If a LWP other than the LWP that we're reporting an event for has
896 hit a GDB breakpoint (as opposed to some random trap signal),
897 then just arrange for it to hit it again later. We don't keep
898 the SIGTRAP status and don't forward the SIGTRAP signal to the
899 LWP. We will handle the current event, eventually we will resume
900 all LWPs, and this one will get its breakpoint trap again.
902 If we do not do this, then we run the risk that the user will
903 delete or disable the breakpoint, but the LWP will have already
907 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
908 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
909 DECR_PC_AFTER_BREAK
))
912 fprintf_unfiltered (gdb_stdlog
,
913 "CBC: Push back breakpoint for %s\n",
914 target_pid_to_str (lp
->ptid
));
916 /* Back up the PC if necessary. */
917 if (DECR_PC_AFTER_BREAK
)
918 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
920 /* Throw away the SIGTRAP. */
927 /* Select one LWP out of those that have events pending. */
930 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
934 struct lwp_info
*event_lp
;
936 /* Record the wait status for the origional LWP. */
937 (*orig_lp
)->status
= *status
;
939 /* Give preference to any LWP that is being single-stepped. */
940 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
941 if (event_lp
!= NULL
)
944 fprintf_unfiltered (gdb_stdlog
,
945 "SEL: Select single-step %s\n",
946 target_pid_to_str (event_lp
->ptid
));
950 /* No single-stepping LWP. Select one at random, out of those
951 which have had SIGTRAP events. */
953 /* First see how many SIGTRAP events we have. */
954 iterate_over_lwps (count_events_callback
, &num_events
);
956 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
957 random_selector
= (int)
958 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
960 if (debug_lin_lwp
&& num_events
> 1)
961 fprintf_unfiltered (gdb_stdlog
,
962 "SEL: Found %d SIGTRAP events, selecting #%d\n",
963 num_events
, random_selector
);
965 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
969 if (event_lp
!= NULL
)
971 /* Switch the event LWP. */
973 *status
= event_lp
->status
;
976 /* Flush the wait status for the event LWP. */
977 (*orig_lp
)->status
= 0;
980 /* Return non-zero if LP has been resumed. */
983 resumed_callback (struct lwp_info
*lp
, void *data
)
990 /* We need to override child_wait to support attaching to cloned
991 processes, since a normal wait (as done by the default version)
992 ignores those processes. */
994 /* Wait for child PTID to do something. Return id of the child,
995 minus_one_ptid in case of error; store status into *OURSTATUS. */
998 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1006 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1007 attached process. */
1010 pid
= waitpid (GET_PID (ptid
), &status
, 0);
1011 if (pid
== -1 && errno
== ECHILD
)
1012 /* Try again with __WCLONE to check cloned processes. */
1013 pid
= waitpid (GET_PID (ptid
), &status
, __WCLONE
);
1017 fprintf_unfiltered (gdb_stdlog
,
1018 "CW: waitpid %ld received %s\n",
1020 status_to_str (status
));
1025 /* Make sure we don't report an event for the exit of the
1026 original program, if we've detached from it. */
1027 if (pid
!= -1 && ! WIFSTOPPED (status
) && pid
!= GET_PID (inferior_ptid
))
1033 clear_sigio_trap ();
1034 clear_sigint_trap ();
1036 while (pid
== -1 && save_errno
== EINTR
);
1040 warning ("Child process unexpectedly missing: %s",
1041 safe_strerror (errno
));
1043 /* Claim it exited with unknown signal. */
1044 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1045 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1046 return minus_one_ptid
;
1049 store_waitstatus (ourstatus
, status
);
1050 return pid_to_ptid (pid
);
1056 lin_lwp_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1058 struct lwp_info
*lp
= NULL
;
1061 pid_t pid
= PIDGET (ptid
);
1062 sigset_t flush_mask
;
1064 sigemptyset (&flush_mask
);
1066 /* Make sure SIGCHLD is blocked. */
1067 if (! sigismember (&blocked_mask
, SIGCHLD
))
1069 sigaddset (&blocked_mask
, SIGCHLD
);
1070 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1075 /* Make sure there is at least one LWP that has been resumed, at
1076 least if there are any LWPs at all. */
1077 gdb_assert (num_lwps
== 0 || iterate_over_lwps (resumed_callback
, NULL
));
1079 /* First check if there is a LWP with a wait status pending. */
1082 /* Any LWP that's been resumed will do. */
1083 lp
= iterate_over_lwps (status_callback
, NULL
);
1086 status
= lp
->status
;
1089 if (debug_lin_lwp
&& status
)
1090 fprintf_unfiltered (gdb_stdlog
,
1091 "LLW: Using pending wait status %s for %s.\n",
1092 status_to_str (status
),
1093 target_pid_to_str (lp
->ptid
));
1096 /* But if we don't fine one, we'll have to wait, and check both
1097 cloned and uncloned processes. We start with the cloned
1099 options
= __WCLONE
| WNOHANG
;
1101 else if (is_lwp (ptid
))
1104 fprintf_unfiltered (gdb_stdlog
,
1105 "LLW: Waiting for specific LWP %s.\n",
1106 target_pid_to_str (ptid
));
1108 /* We have a specific LWP to check. */
1109 lp
= find_lwp_pid (ptid
);
1111 status
= lp
->status
;
1114 if (debug_lin_lwp
&& status
)
1115 fprintf_unfiltered (gdb_stdlog
,
1116 "LLW: Using pending wait status %s for %s.\n",
1117 status_to_str (status
),
1118 target_pid_to_str (lp
->ptid
));
1120 /* If we have to wait, take into account whether PID is a cloned
1121 process or not. And we have to convert it to something that
1122 the layer beneath us can understand. */
1123 options
= lp
->cloned
? __WCLONE
: 0;
1124 pid
= GET_LWP (ptid
);
1127 if (status
&& lp
->signalled
)
1129 /* A pending SIGSTOP may interfere with the normal stream of
1130 events. In a typical case where interference is a problem,
1131 we have a SIGSTOP signal pending for LWP A while
1132 single-stepping it, encounter an event in LWP B, and take the
1133 pending SIGSTOP while trying to stop LWP A. After processing
1134 the event in LWP B, LWP A is continued, and we'll never see
1135 the SIGTRAP associated with the last time we were
1136 single-stepping LWP A. */
1138 /* Resume the thread. It should halt immediately returning the
1140 registers_changed ();
1141 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
1144 fprintf_unfiltered (gdb_stdlog
,
1145 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1146 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1147 target_pid_to_str (lp
->ptid
));
1149 gdb_assert (lp
->resumed
);
1151 /* This should catch the pending SIGSTOP. */
1152 stop_wait_callback (lp
, NULL
);
1155 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1156 attached process. */
1163 lwpid
= waitpid (pid
, &status
, options
);
1166 gdb_assert (pid
== -1 || lwpid
== pid
);
1170 fprintf_unfiltered (gdb_stdlog
,
1171 "LLW: waitpid %ld received %s\n",
1173 status_to_str (status
));
1176 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
1178 /* Make sure we don't report an event for the exit of an LWP not in
1179 our list, i.e. not part of the current process. This can happen
1180 if we detach from a program we original forked and then it
1182 if (! WIFSTOPPED (status
) && ! lp
)
1190 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
1191 if (options
& __WCLONE
)
1196 gdb_assert (WIFSTOPPED (status
)
1197 && WSTOPSIG (status
) == SIGSTOP
);
1200 if (! in_thread_list (inferior_ptid
))
1202 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
1203 GET_PID (inferior_ptid
));
1204 add_thread (inferior_ptid
);
1207 add_thread (lp
->ptid
);
1208 printf_unfiltered ("[New %s]\n",
1209 target_pid_to_str (lp
->ptid
));
1213 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
1214 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
1215 left in the process. */
1216 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
1218 if (in_thread_list (lp
->ptid
))
1220 /* Core GDB cannot deal with us deleting the current
1222 if (! ptid_equal (lp
->ptid
, inferior_ptid
))
1223 delete_thread (lp
->ptid
);
1224 printf_unfiltered ("[%s exited]\n",
1225 target_pid_to_str (lp
->ptid
));
1228 fprintf_unfiltered (gdb_stdlog
,
1229 "LLW: %s exited.\n",
1230 target_pid_to_str (lp
->ptid
));
1232 delete_lwp (lp
->ptid
);
1234 /* Make sure there is at least one thread running. */
1235 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
1237 /* Discard the event. */
1242 /* Make sure we don't report a SIGSTOP that we sent
1243 ourselves in an attempt to stop an LWP. */
1245 && WIFSTOPPED (status
)
1246 && WSTOPSIG (status
) == SIGSTOP
)
1249 fprintf_unfiltered (gdb_stdlog
,
1250 "LLW: Delayed SIGSTOP caught for %s.\n",
1251 target_pid_to_str (lp
->ptid
));
1253 /* This is a delayed SIGSTOP. */
1256 registers_changed ();
1257 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
1260 fprintf_unfiltered (gdb_stdlog
,
1261 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
1263 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1264 target_pid_to_str (lp
->ptid
));
1267 gdb_assert (lp
->resumed
);
1269 /* Discard the event. */
1279 /* Alternate between checking cloned and uncloned processes. */
1280 options
^= __WCLONE
;
1282 /* And suspend every time we have checked both. */
1283 if (options
& __WCLONE
)
1284 sigsuspend (&suspend_mask
);
1287 /* We shouldn't end up here unless we want to try again. */
1288 gdb_assert (status
== 0);
1291 clear_sigio_trap ();
1292 clear_sigint_trap ();
1296 /* Don't report signals that GDB isn't interested in, such as
1297 signals that are neither printed nor stopped upon. Stopping all
1298 threads can be a bit time-consuming so if we want decent
1299 performance with heavily multi-threaded programs, especially when
1300 they're using a high frequency timer, we'd better avoid it if we
1303 if (WIFSTOPPED (status
))
1305 int signo
= target_signal_from_host (WSTOPSIG (status
));
1307 if (signal_stop_state (signo
) == 0
1308 && signal_print_state (signo
) == 0
1309 && signal_pass_state (signo
) == 1)
1311 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
1312 here? It is not clear we should. GDB may not expect
1313 other threads to run. On the other hand, not resuming
1314 newly attached threads may cause an unwanted delay in
1315 getting them running. */
1316 registers_changed ();
1317 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
, signo
);
1319 fprintf_unfiltered (gdb_stdlog
,
1320 "LLW: %s %s, %s (preempt 'handle')\n",
1321 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1322 target_pid_to_str (lp
->ptid
),
1323 signo
? strsignal (signo
) : "0");
1329 if (signo
== TARGET_SIGNAL_INT
1330 && signal_pass_state (signo
) == 0)
1332 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
1333 forwarded to the entire process group, that is, all LWP's
1334 will receive it. Since we only want to report it once,
1335 we try to flush it from all LWPs except this one. */
1336 sigaddset (&flush_mask
, SIGINT
);
1340 /* This LWP is stopped now. */
1344 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
1345 status_to_str (status
),
1346 target_pid_to_str (lp
->ptid
));
1348 /* Now stop all other LWP's ... */
1349 iterate_over_lwps (stop_callback
, NULL
);
1351 /* ... and wait until all of them have reported back that they're no
1353 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
1355 /* If we're not waiting for a specific LWP, choose an event LWP from
1356 among those that have had events. Giving equal priority to all
1357 LWPs that have had events helps prevent starvation. */
1359 select_event_lwp (&lp
, &status
);
1361 /* Now that we've selected our final event LWP, cancel any
1362 breakpoints in other LWPs that have hit a GDB breakpoint. See
1363 the comment in cancel_breakpoints_callback to find out why. */
1364 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
1366 /* If we're not running in "threaded" mode, we'll report the bare
1369 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
1371 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
1373 fprintf_unfiltered (gdb_stdlog
,
1374 "LLW: trap_ptid is %s.\n",
1375 target_pid_to_str (trap_ptid
));
1378 trap_ptid
= null_ptid
;
1380 store_waitstatus (ourstatus
, status
);
1381 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
1385 kill_callback (struct lwp_info
*lp
, void *data
)
1388 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
1390 fprintf_unfiltered (gdb_stdlog
,
1391 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
1392 target_pid_to_str (lp
->ptid
),
1393 errno
? safe_strerror (errno
) : "OK");
1399 kill_wait_callback (struct lwp_info
*lp
, void *data
)
1403 /* We must make sure that there are no pending events (delayed
1404 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
1405 program doesn't interfere with any following debugging session. */
1407 /* For cloned processes we must check both with __WCLONE and
1408 without, since the exit status of a cloned process isn't reported
1414 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
1415 if (pid
!= (pid_t
) -1 && debug_lin_lwp
)
1417 fprintf_unfiltered (gdb_stdlog
,
1418 "KWC: wait %s received unknown.\n",
1419 target_pid_to_str (lp
->ptid
));
1422 while (pid
== GET_LWP (lp
->ptid
));
1424 gdb_assert (pid
== -1 && errno
== ECHILD
);
1429 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
1430 if (pid
!= (pid_t
) -1 && debug_lin_lwp
)
1432 fprintf_unfiltered (gdb_stdlog
,
1433 "KWC: wait %s received unk.\n",
1434 target_pid_to_str (lp
->ptid
));
1437 while (pid
== GET_LWP (lp
->ptid
));
1439 gdb_assert (pid
== -1 && errno
== ECHILD
);
1446 /* Kill all LWP's ... */
1447 iterate_over_lwps (kill_callback
, NULL
);
1449 /* ... and wait until we've flushed all events. */
1450 iterate_over_lwps (kill_wait_callback
, NULL
);
1452 target_mourn_inferior ();
1456 lin_lwp_create_inferior (char *exec_file
, char *allargs
, char **env
)
1458 child_ops
.to_create_inferior (exec_file
, allargs
, env
);
1462 lin_lwp_mourn_inferior (void)
1464 trap_ptid
= null_ptid
;
1466 /* Destroy LWP info; it's no longer valid. */
1469 /* Restore the original signal mask. */
1470 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1471 sigemptyset (&blocked_mask
);
1473 child_ops
.to_mourn_inferior ();
1477 lin_lwp_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
1478 struct mem_attrib
*attrib
,
1479 struct target_ops
*target
)
1481 struct cleanup
*old_chain
= save_inferior_ptid ();
1484 if (is_lwp (inferior_ptid
))
1485 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
1487 xfer
= linux_proc_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
1489 xfer
= child_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
1491 do_cleanups (old_chain
);
1496 lin_lwp_thread_alive (ptid_t ptid
)
1498 gdb_assert (is_lwp (ptid
));
1501 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
1503 fprintf_unfiltered (gdb_stdlog
,
1504 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
1505 target_pid_to_str (ptid
),
1506 errno
? safe_strerror (errno
) : "OK");
1514 lin_lwp_pid_to_str (ptid_t ptid
)
1516 static char buf
[64];
1520 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
1524 return normal_pid_to_str (ptid
);
1528 init_lin_lwp_ops (void)
1531 lin_lwp_ops
.to_open
= lin_lwp_open
;
1533 lin_lwp_ops
.to_shortname
= "lwp-layer";
1534 lin_lwp_ops
.to_longname
= "lwp-layer";
1535 lin_lwp_ops
.to_doc
= "Low level threads support (LWP layer)";
1536 lin_lwp_ops
.to_attach
= lin_lwp_attach
;
1537 lin_lwp_ops
.to_detach
= lin_lwp_detach
;
1538 lin_lwp_ops
.to_resume
= lin_lwp_resume
;
1539 lin_lwp_ops
.to_wait
= lin_lwp_wait
;
1540 /* fetch_inferior_registers and store_inferior_registers will
1541 honor the LWP id, so we can use them directly. */
1542 lin_lwp_ops
.to_fetch_registers
= fetch_inferior_registers
;
1543 lin_lwp_ops
.to_store_registers
= store_inferior_registers
;
1544 lin_lwp_ops
.to_xfer_memory
= lin_lwp_xfer_memory
;
1545 lin_lwp_ops
.to_kill
= lin_lwp_kill
;
1546 lin_lwp_ops
.to_create_inferior
= lin_lwp_create_inferior
;
1547 lin_lwp_ops
.to_mourn_inferior
= lin_lwp_mourn_inferior
;
1548 lin_lwp_ops
.to_thread_alive
= lin_lwp_thread_alive
;
1549 lin_lwp_ops
.to_pid_to_str
= lin_lwp_pid_to_str
;
1550 lin_lwp_ops
.to_stratum
= thread_stratum
;
1551 lin_lwp_ops
.to_has_thread_control
= tc_schedlock
;
1552 lin_lwp_ops
.to_magic
= OPS_MAGIC
;
1556 sigchld_handler (int signo
)
1558 /* Do nothing. The only reason for this handler is that it allows
1559 us to use sigsuspend in lin_lwp_wait above to wait for the
1560 arrival of a SIGCHLD. */
1564 _initialize_lin_lwp (void)
1566 struct sigaction action
;
1568 extern void thread_db_init (struct target_ops
*);
1570 init_lin_lwp_ops ();
1571 add_target (&lin_lwp_ops
);
1572 thread_db_init (&lin_lwp_ops
);
1574 /* Save the original signal mask. */
1575 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
1577 action
.sa_handler
= sigchld_handler
;
1578 sigemptyset (&action
.sa_mask
);
1579 action
.sa_flags
= 0;
1580 sigaction (SIGCHLD
, &action
, NULL
);
1582 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1583 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
1584 sigdelset (&suspend_mask
, SIGCHLD
);
1586 sigemptyset (&blocked_mask
);
1588 add_show_from_set (add_set_cmd ("lin-lwp", no_class
, var_zinteger
,
1589 (char *) &debug_lin_lwp
,
1590 "Set debugging of GNU/Linux lwp module.\n\
1591 Enables printf debugging output.\n",
1597 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
1598 the GNU/Linux Threads library and therefore doesn't really belong
1601 /* Read variable NAME in the target and return its value if found.
1602 Otherwise return zero. It is assumed that the type of the variable
1606 get_signo (const char *name
)
1608 struct minimal_symbol
*ms
;
1611 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
1615 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (char *) &signo
,
1616 sizeof (signo
)) != 0)
1622 /* Return the set of signals used by the threads library in *SET. */
1625 lin_thread_get_thread_signals (sigset_t
*set
)
1627 struct sigaction action
;
1628 int restart
, cancel
;
1632 restart
= get_signo ("__pthread_sig_restart");
1636 cancel
= get_signo ("__pthread_sig_cancel");
1640 sigaddset (set
, restart
);
1641 sigaddset (set
, cancel
);
1643 /* The GNU/Linux Threads library makes terminating threads send a
1644 special "cancel" signal instead of SIGCHLD. Make sure we catch
1645 those (to prevent them from terminating GDB itself, which is
1646 likely to be their default action) and treat them the same way as
1649 action
.sa_handler
= sigchld_handler
;
1650 sigemptyset (&action
.sa_mask
);
1651 action
.sa_flags
= 0;
1652 sigaction (cancel
, &action
, NULL
);
1654 /* We block the "cancel" signal throughout this code ... */
1655 sigaddset (&blocked_mask
, cancel
);
1656 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1658 /* ... except during a sigsuspend. */
1659 sigdelset (&suspend_mask
, cancel
);