dma: beautify queue listing output
[dragonfly.git] / contrib / gdb-6.2.1 / gdb / lin-lwp.c
blob4d04d62167e7661dc9db192383a2f554145563ef
1 /* Multi-threaded debugging support for GNU/Linux (LWP layer).
2 Copyright 2000, 2001, 2002, 2003, 2004 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. */
21 #include "defs.h"
23 #include "gdb_assert.h"
24 #include "gdb_string.h"
25 #include <errno.h>
26 #include <signal.h>
27 #ifdef HAVE_TKILL_SYSCALL
28 #include <unistd.h>
29 #include <sys/syscall.h>
30 #endif
31 #include <sys/ptrace.h>
32 #include "gdb_wait.h"
34 #include "gdbthread.h"
35 #include "inferior.h"
36 #include "target.h"
37 #include "regcache.h"
38 #include "gdbcmd.h"
40 static int debug_lin_lwp;
41 extern char *strsignal (int sig);
43 #include "linux-nat.h"
45 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
46 are processes sharing the same VM space. A multi-threaded process
47 is basically a group of such processes. However, such a grouping
48 is almost entirely a user-space issue; the kernel doesn't enforce
49 such a grouping at all (this might change in the future). In
50 general, we'll rely on the threads library (i.e. the GNU/Linux
51 Threads library) to provide such a grouping.
53 It is perfectly well possible to write a multi-threaded application
54 without the assistance of a threads library, by using the clone
55 system call directly. This module should be able to give some
56 rudimentary support for debugging such applications if developers
57 specify the CLONE_PTRACE flag in the clone system call, and are
58 using the Linux kernel 2.4 or above.
60 Note that there are some peculiarities in GNU/Linux that affect
61 this code:
63 - In general one should specify the __WCLONE flag to waitpid in
64 order to make it report events for any of the cloned processes
65 (and leave it out for the initial process). However, if a cloned
66 process has exited the exit status is only reported if the
67 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
68 we cannot use it since GDB must work on older systems too.
70 - When a traced, cloned process exits and is waited for by the
71 debugger, the kernel reassigns it to the original parent and
72 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
73 library doesn't notice this, which leads to the "zombie problem":
74 When debugged a multi-threaded process that spawns a lot of
75 threads will run out of processes, even if the threads exit,
76 because the "zombies" stay around. */
78 /* List of known LWPs. */
79 static struct lwp_info *lwp_list;
81 /* Number of LWPs in the list. */
82 static int num_lwps;
84 /* Non-zero if we're running in "threaded" mode. */
85 static int threaded;
88 #define GET_LWP(ptid) ptid_get_lwp (ptid)
89 #define GET_PID(ptid) ptid_get_pid (ptid)
90 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
91 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
93 /* If the last reported event was a SIGTRAP, this variable is set to
94 the process id of the LWP/thread that got it. */
95 ptid_t trap_ptid;
98 /* This module's target-specific operations. */
99 static struct target_ops lin_lwp_ops;
101 /* The standard child operations. */
102 extern struct target_ops child_ops;
104 /* Since we cannot wait (in lin_lwp_wait) for the initial process and
105 any cloned processes with a single call to waitpid, we have to use
106 the WNOHANG flag and call waitpid in a loop. To optimize
107 things a bit we use `sigsuspend' to wake us up when a process has
108 something to report (it will send us a SIGCHLD if it has). To make
109 this work we have to juggle with the signal mask. We save the
110 original signal mask such that we can restore it before creating a
111 new process in order to avoid blocking certain signals in the
112 inferior. We then block SIGCHLD during the waitpid/sigsuspend
113 loop. */
115 /* Original signal mask. */
116 static sigset_t normal_mask;
118 /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
119 _initialize_lin_lwp. */
120 static sigset_t suspend_mask;
122 /* Signals to block to make that sigsuspend work. */
123 static sigset_t blocked_mask;
126 /* Prototypes for local functions. */
127 static int stop_wait_callback (struct lwp_info *lp, void *data);
128 static int lin_lwp_thread_alive (ptid_t ptid);
130 /* Convert wait status STATUS to a string. Used for printing debug
131 messages only. */
133 static char *
134 status_to_str (int status)
136 static char buf[64];
138 if (WIFSTOPPED (status))
139 snprintf (buf, sizeof (buf), "%s (stopped)",
140 strsignal (WSTOPSIG (status)));
141 else if (WIFSIGNALED (status))
142 snprintf (buf, sizeof (buf), "%s (terminated)",
143 strsignal (WSTOPSIG (status)));
144 else
145 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
147 return buf;
150 /* Initialize the list of LWPs. Note that this module, contrary to
151 what GDB's generic threads layer does for its thread list,
152 re-initializes the LWP lists whenever we mourn or detach (which
153 doesn't involve mourning) the inferior. */
155 static void
156 init_lwp_list (void)
158 struct lwp_info *lp, *lpnext;
160 for (lp = lwp_list; lp; lp = lpnext)
162 lpnext = lp->next;
163 xfree (lp);
166 lwp_list = NULL;
167 num_lwps = 0;
168 threaded = 0;
171 /* Add the LWP specified by PID to the list. If this causes the
172 number of LWPs to become larger than one, go into "threaded" mode.
173 Return a pointer to the structure describing the new LWP. */
175 static struct lwp_info *
176 add_lwp (ptid_t ptid)
178 struct lwp_info *lp;
180 gdb_assert (is_lwp (ptid));
182 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
184 memset (lp, 0, sizeof (struct lwp_info));
186 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
188 lp->ptid = ptid;
190 lp->next = lwp_list;
191 lwp_list = lp;
192 if (++num_lwps > 1)
193 threaded = 1;
195 return lp;
198 /* Remove the LWP specified by PID from the list. */
200 static void
201 delete_lwp (ptid_t ptid)
203 struct lwp_info *lp, *lpprev;
205 lpprev = NULL;
207 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
208 if (ptid_equal (lp->ptid, ptid))
209 break;
211 if (!lp)
212 return;
214 /* We don't go back to "non-threaded" mode if the number of threads
215 becomes less than two. */
216 num_lwps--;
218 if (lpprev)
219 lpprev->next = lp->next;
220 else
221 lwp_list = lp->next;
223 xfree (lp);
226 /* Return a pointer to the structure describing the LWP corresponding
227 to PID. If no corresponding LWP could be found, return NULL. */
229 static struct lwp_info *
230 find_lwp_pid (ptid_t ptid)
232 struct lwp_info *lp;
233 int lwp;
235 if (is_lwp (ptid))
236 lwp = GET_LWP (ptid);
237 else
238 lwp = GET_PID (ptid);
240 for (lp = lwp_list; lp; lp = lp->next)
241 if (lwp == GET_LWP (lp->ptid))
242 return lp;
244 return NULL;
247 /* Call CALLBACK with its second argument set to DATA for every LWP in
248 the list. If CALLBACK returns 1 for a particular LWP, return a
249 pointer to the structure describing that LWP immediately.
250 Otherwise return NULL. */
252 struct lwp_info *
253 iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
255 struct lwp_info *lp, *lpnext;
257 for (lp = lwp_list; lp; lp = lpnext)
259 lpnext = lp->next;
260 if ((*callback) (lp, data))
261 return lp;
264 return NULL;
268 #if 0
269 static void
270 lin_lwp_open (char *args, int from_tty)
272 push_target (&lin_lwp_ops);
274 #endif
276 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
277 a message telling the user that a new LWP has been added to the
278 process. */
280 void
281 lin_lwp_attach_lwp (ptid_t ptid, int verbose)
283 struct lwp_info *lp, *found_lp;
285 gdb_assert (is_lwp (ptid));
287 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
288 to interrupt either the ptrace() or waitpid() calls below. */
289 if (!sigismember (&blocked_mask, SIGCHLD))
291 sigaddset (&blocked_mask, SIGCHLD);
292 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
295 if (verbose)
296 printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
298 found_lp = lp = find_lwp_pid (ptid);
299 if (lp == NULL)
300 lp = add_lwp (ptid);
302 /* We assume that we're already attached to any LWP that has an id
303 equal to the overall process id, and to any LWP that is already
304 in our list of LWPs. If we're not seeing exit events from threads
305 and we've had PID wraparound since we last tried to stop all threads,
306 this assumption might be wrong; fortunately, this is very unlikely
307 to happen. */
308 if (GET_LWP (ptid) != GET_PID (ptid) && found_lp == NULL)
310 pid_t pid;
311 int status;
313 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
314 error ("Can't attach %s: %s", target_pid_to_str (ptid),
315 safe_strerror (errno));
317 if (debug_lin_lwp)
318 fprintf_unfiltered (gdb_stdlog,
319 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
320 target_pid_to_str (ptid));
322 pid = waitpid (GET_LWP (ptid), &status, 0);
323 if (pid == -1 && errno == ECHILD)
325 /* Try again with __WCLONE to check cloned processes. */
326 pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
327 lp->cloned = 1;
330 gdb_assert (pid == GET_LWP (ptid)
331 && WIFSTOPPED (status) && WSTOPSIG (status));
333 child_post_attach (pid);
335 lp->stopped = 1;
337 if (debug_lin_lwp)
339 fprintf_unfiltered (gdb_stdlog,
340 "LLAL: waitpid %s received %s\n",
341 target_pid_to_str (ptid),
342 status_to_str (status));
345 else
347 /* We assume that the LWP representing the original process
348 is already stopped. Mark it as stopped in the data structure
349 that the lin-lwp layer uses to keep track of threads. Note
350 that this won't have already been done since the main thread
351 will have, we assume, been stopped by an attach from a
352 different layer. */
353 lp->stopped = 1;
357 static void
358 lin_lwp_attach (char *args, int from_tty)
360 struct lwp_info *lp;
361 pid_t pid;
362 int status;
364 /* FIXME: We should probably accept a list of process id's, and
365 attach all of them. */
366 child_ops.to_attach (args, from_tty);
368 /* Add the initial process as the first LWP to the list. */
369 lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
371 /* Make sure the initial process is stopped. The user-level threads
372 layer might want to poke around in the inferior, and that won't
373 work if things haven't stabilized yet. */
374 pid = waitpid (GET_PID (inferior_ptid), &status, 0);
375 if (pid == -1 && errno == ECHILD)
377 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
379 /* Try again with __WCLONE to check cloned processes. */
380 pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
381 lp->cloned = 1;
384 gdb_assert (pid == GET_PID (inferior_ptid)
385 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
387 lp->stopped = 1;
389 /* Fake the SIGSTOP that core GDB expects. */
390 lp->status = W_STOPCODE (SIGSTOP);
391 lp->resumed = 1;
392 if (debug_lin_lwp)
394 fprintf_unfiltered (gdb_stdlog,
395 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid);
399 static int
400 detach_callback (struct lwp_info *lp, void *data)
402 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
404 if (debug_lin_lwp && lp->status)
405 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
406 strsignal (WSTOPSIG (lp->status)),
407 target_pid_to_str (lp->ptid));
409 while (lp->signalled && lp->stopped)
411 errno = 0;
412 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
413 WSTOPSIG (lp->status)) < 0)
414 error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
415 safe_strerror (errno));
417 if (debug_lin_lwp)
418 fprintf_unfiltered (gdb_stdlog,
419 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
420 target_pid_to_str (lp->ptid),
421 status_to_str (lp->status));
423 lp->stopped = 0;
424 lp->signalled = 0;
425 lp->status = 0;
426 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
427 here. But since lp->signalled was cleared above,
428 stop_wait_callback didn't do anything; the process was left
429 running. Shouldn't we be waiting for it to stop?
430 I've removed the call, since stop_wait_callback now does do
431 something when called with lp->signalled == 0. */
433 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
436 /* We don't actually detach from the LWP that has an id equal to the
437 overall process id just yet. */
438 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
440 errno = 0;
441 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
442 WSTOPSIG (lp->status)) < 0)
443 error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
444 safe_strerror (errno));
446 if (debug_lin_lwp)
447 fprintf_unfiltered (gdb_stdlog,
448 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
449 target_pid_to_str (lp->ptid),
450 strsignal (WSTOPSIG (lp->status)));
452 delete_lwp (lp->ptid);
455 return 0;
458 static void
459 lin_lwp_detach (char *args, int from_tty)
461 iterate_over_lwps (detach_callback, NULL);
463 /* Only the initial process should be left right now. */
464 gdb_assert (num_lwps == 1);
466 trap_ptid = null_ptid;
468 /* Destroy LWP info; it's no longer valid. */
469 init_lwp_list ();
471 /* Restore the original signal mask. */
472 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
473 sigemptyset (&blocked_mask);
475 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
476 child_ops.to_detach (args, from_tty);
480 /* Resume LP. */
482 static int
483 resume_callback (struct lwp_info *lp, void *data)
485 if (lp->stopped && lp->status == 0)
487 struct thread_info *tp;
489 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
490 if (debug_lin_lwp)
491 fprintf_unfiltered (gdb_stdlog,
492 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
493 target_pid_to_str (lp->ptid));
494 lp->stopped = 0;
495 lp->step = 0;
498 return 0;
501 static int
502 resume_clear_callback (struct lwp_info *lp, void *data)
504 lp->resumed = 0;
505 return 0;
508 static int
509 resume_set_callback (struct lwp_info *lp, void *data)
511 lp->resumed = 1;
512 return 0;
515 static void
516 lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
518 struct lwp_info *lp;
519 int resume_all;
521 /* A specific PTID means `step only this process id'. */
522 resume_all = (PIDGET (ptid) == -1);
524 if (resume_all)
525 iterate_over_lwps (resume_set_callback, NULL);
526 else
527 iterate_over_lwps (resume_clear_callback, NULL);
529 /* If PID is -1, it's the current inferior that should be
530 handled specially. */
531 if (PIDGET (ptid) == -1)
532 ptid = inferior_ptid;
534 lp = find_lwp_pid (ptid);
535 if (lp)
537 ptid = pid_to_ptid (GET_LWP (lp->ptid));
539 /* Remember if we're stepping. */
540 lp->step = step;
542 /* Mark this LWP as resumed. */
543 lp->resumed = 1;
545 /* If we have a pending wait status for this thread, there is no
546 point in resuming the process. */
547 if (lp->status)
549 /* FIXME: What should we do if we are supposed to continue
550 this thread with a signal? */
551 gdb_assert (signo == TARGET_SIGNAL_0);
552 return;
555 /* Mark LWP as not stopped to prevent it from being continued by
556 resume_callback. */
557 lp->stopped = 0;
560 if (resume_all)
561 iterate_over_lwps (resume_callback, NULL);
563 child_resume (ptid, step, signo);
564 if (debug_lin_lwp)
565 fprintf_unfiltered (gdb_stdlog,
566 "LLR: %s %s, %s (resume event thread)\n",
567 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
568 target_pid_to_str (ptid),
569 signo ? strsignal (signo) : "0");
573 /* Issue kill to specified lwp. */
575 static int tkill_failed;
577 static int
578 kill_lwp (int lwpid, int signo)
580 errno = 0;
582 /* Use tkill, if possible, in case we are using nptl threads. If tkill
583 fails, then we are not using nptl threads and we should be using kill. */
585 #ifdef HAVE_TKILL_SYSCALL
586 if (!tkill_failed)
588 int ret = syscall (__NR_tkill, lwpid, signo);
589 if (errno != ENOSYS)
590 return ret;
591 errno = 0;
592 tkill_failed = 1;
594 #endif
596 return kill (lwpid, signo);
599 /* Handle a GNU/Linux extended wait response. Most of the work we
600 just pass off to linux_handle_extended_wait, but if it reports a
601 clone event we need to add the new LWP to our list (and not report
602 the trap to higher layers). This function returns non-zero if
603 the event should be ignored and we should wait again. */
605 static int
606 lin_lwp_handle_extended (struct lwp_info *lp, int status)
608 linux_handle_extended_wait (GET_LWP (lp->ptid), status,
609 &lp->waitstatus);
611 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
612 if (lp->waitstatus.kind == TARGET_WAITKIND_SPURIOUS)
614 struct lwp_info *new_lp;
615 new_lp = add_lwp (BUILD_LWP (lp->waitstatus.value.related_pid,
616 GET_PID (inferior_ptid)));
617 new_lp->cloned = 1;
618 new_lp->stopped = 1;
620 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
622 if (debug_lin_lwp)
623 fprintf_unfiltered (gdb_stdlog,
624 "LLHE: Got clone event from LWP %ld, resuming\n",
625 GET_LWP (lp->ptid));
626 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
628 return 1;
631 return 0;
634 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
635 exited. */
637 static int
638 wait_lwp (struct lwp_info *lp)
640 pid_t pid;
641 int status;
642 int thread_dead = 0;
644 gdb_assert (!lp->stopped);
645 gdb_assert (lp->status == 0);
647 pid = waitpid (GET_LWP (lp->ptid), &status, 0);
648 if (pid == -1 && errno == ECHILD)
650 pid = waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
651 if (pid == -1 && errno == ECHILD)
653 /* The thread has previously exited. We need to delete it
654 now because, for some vendor 2.4 kernels with NPTL
655 support backported, there won't be an exit event unless
656 it is the main thread. 2.6 kernels will report an exit
657 event for each thread that exits, as expected. */
658 thread_dead = 1;
659 if (debug_lin_lwp)
660 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
661 target_pid_to_str (lp->ptid));
665 if (!thread_dead)
667 gdb_assert (pid == GET_LWP (lp->ptid));
669 if (debug_lin_lwp)
671 fprintf_unfiltered (gdb_stdlog,
672 "WL: waitpid %s received %s\n",
673 target_pid_to_str (lp->ptid),
674 status_to_str (status));
678 /* Check if the thread has exited. */
679 if (WIFEXITED (status) || WIFSIGNALED (status))
681 thread_dead = 1;
682 if (debug_lin_lwp)
683 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
684 target_pid_to_str (lp->ptid));
687 if (thread_dead)
689 if (in_thread_list (lp->ptid))
691 /* Core GDB cannot deal with us deleting the current thread. */
692 if (!ptid_equal (lp->ptid, inferior_ptid))
693 delete_thread (lp->ptid);
694 printf_unfiltered ("[%s exited]\n",
695 target_pid_to_str (lp->ptid));
698 delete_lwp (lp->ptid);
699 return 0;
702 gdb_assert (WIFSTOPPED (status));
704 /* Handle GNU/Linux's extended waitstatus for trace events. */
705 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
707 if (debug_lin_lwp)
708 fprintf_unfiltered (gdb_stdlog,
709 "WL: Handling extended status 0x%06x\n",
710 status);
711 if (lin_lwp_handle_extended (lp, status))
712 return wait_lwp (lp);
715 return status;
718 /* Send a SIGSTOP to LP. */
720 static int
721 stop_callback (struct lwp_info *lp, void *data)
723 if (!lp->stopped && !lp->signalled)
725 int ret;
727 if (debug_lin_lwp)
729 fprintf_unfiltered (gdb_stdlog,
730 "SC: kill %s **<SIGSTOP>**\n",
731 target_pid_to_str (lp->ptid));
733 errno = 0;
734 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
735 if (debug_lin_lwp)
737 fprintf_unfiltered (gdb_stdlog,
738 "SC: lwp kill %d %s\n",
739 ret,
740 errno ? safe_strerror (errno) : "ERRNO-OK");
743 lp->signalled = 1;
744 gdb_assert (lp->status == 0);
747 return 0;
750 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
751 a pointer to a set of signals to be flushed immediately. */
753 static int
754 stop_wait_callback (struct lwp_info *lp, void *data)
756 sigset_t *flush_mask = data;
758 if (!lp->stopped)
760 int status;
762 status = wait_lwp (lp);
763 if (status == 0)
764 return 0;
766 /* Ignore any signals in FLUSH_MASK. */
767 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
769 if (!lp->signalled)
771 lp->stopped = 1;
772 return 0;
775 errno = 0;
776 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
777 if (debug_lin_lwp)
778 fprintf_unfiltered (gdb_stdlog,
779 "PTRACE_CONT %s, 0, 0 (%s)\n",
780 target_pid_to_str (lp->ptid),
781 errno ? safe_strerror (errno) : "OK");
783 return stop_wait_callback (lp, flush_mask);
786 if (WSTOPSIG (status) != SIGSTOP)
788 if (WSTOPSIG (status) == SIGTRAP)
790 /* If a LWP other than the LWP that we're reporting an
791 event for has hit a GDB breakpoint (as opposed to
792 some random trap signal), then just arrange for it to
793 hit it again later. We don't keep the SIGTRAP status
794 and don't forward the SIGTRAP signal to the LWP. We
795 will handle the current event, eventually we will
796 resume all LWPs, and this one will get its breakpoint
797 trap again.
799 If we do not do this, then we run the risk that the
800 user will delete or disable the breakpoint, but the
801 thread will have already tripped on it. */
803 /* Now resume this LWP and get the SIGSTOP event. */
804 errno = 0;
805 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
806 if (debug_lin_lwp)
808 fprintf_unfiltered (gdb_stdlog,
809 "PTRACE_CONT %s, 0, 0 (%s)\n",
810 target_pid_to_str (lp->ptid),
811 errno ? safe_strerror (errno) : "OK");
813 fprintf_unfiltered (gdb_stdlog,
814 "SWC: Candidate SIGTRAP event in %s\n",
815 target_pid_to_str (lp->ptid));
817 /* Hold the SIGTRAP for handling by lin_lwp_wait. */
818 stop_wait_callback (lp, data);
819 /* If there's another event, throw it back into the queue. */
820 if (lp->status)
822 if (debug_lin_lwp)
824 fprintf_unfiltered (gdb_stdlog,
825 "SWC: kill %s, %s\n",
826 target_pid_to_str (lp->ptid),
827 status_to_str ((int) status));
829 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
831 /* Save the sigtrap event. */
832 lp->status = status;
833 return 0;
835 else
837 /* The thread was stopped with a signal other than
838 SIGSTOP, and didn't accidentally trip a breakpoint. */
840 if (debug_lin_lwp)
842 fprintf_unfiltered (gdb_stdlog,
843 "SWC: Pending event %s in %s\n",
844 status_to_str ((int) status),
845 target_pid_to_str (lp->ptid));
847 /* Now resume this LWP and get the SIGSTOP event. */
848 errno = 0;
849 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
850 if (debug_lin_lwp)
851 fprintf_unfiltered (gdb_stdlog,
852 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
853 target_pid_to_str (lp->ptid),
854 errno ? safe_strerror (errno) : "OK");
856 /* Hold this event/waitstatus while we check to see if
857 there are any more (we still want to get that SIGSTOP). */
858 stop_wait_callback (lp, data);
859 /* If the lp->status field is still empty, use it to hold
860 this event. If not, then this event must be returned
861 to the event queue of the LWP. */
862 if (lp->status == 0)
863 lp->status = status;
864 else
866 if (debug_lin_lwp)
868 fprintf_unfiltered (gdb_stdlog,
869 "SWC: kill %s, %s\n",
870 target_pid_to_str (lp->ptid),
871 status_to_str ((int) status));
873 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
875 return 0;
878 else
880 /* We caught the SIGSTOP that we intended to catch, so
881 there's no SIGSTOP pending. */
882 lp->stopped = 1;
883 lp->signalled = 0;
887 return 0;
890 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
891 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
893 static int
894 lin_lwp_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask)
896 sigset_t blocked, ignored;
897 int i;
899 linux_proc_pending_signals (pid, pending, &blocked, &ignored);
901 if (!flush_mask)
902 return 0;
904 for (i = 1; i < NSIG; i++)
905 if (sigismember (pending, i))
906 if (!sigismember (flush_mask, i)
907 || sigismember (&blocked, i)
908 || sigismember (&ignored, i))
909 sigdelset (pending, i);
911 if (sigisemptyset (pending))
912 return 0;
914 return 1;
917 /* DATA is interpreted as a mask of signals to flush. If LP has
918 signals pending, and they are all in the flush mask, then arrange
919 to flush them. LP should be stopped, as should all other threads
920 it might share a signal queue with. */
922 static int
923 flush_callback (struct lwp_info *lp, void *data)
925 sigset_t *flush_mask = data;
926 sigset_t pending, intersection, blocked, ignored;
927 int pid, status;
929 /* Normally, when an LWP exits, it is removed from the LWP list. The
930 last LWP isn't removed till later, however. So if there is only
931 one LWP on the list, make sure it's alive. */
932 if (lwp_list == lp && lp->next == NULL)
933 if (!lin_lwp_thread_alive (lp->ptid))
934 return 0;
936 /* Just because the LWP is stopped doesn't mean that new signals
937 can't arrive from outside, so this function must be careful of
938 race conditions. However, because all threads are stopped, we
939 can assume that the pending mask will not shrink unless we resume
940 the LWP, and that it will then get another signal. We can't
941 control which one, however. */
943 if (lp->status)
945 if (debug_lin_lwp)
946 printf_unfiltered ("FC: LP has pending status %06x\n", lp->status);
947 if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status)))
948 lp->status = 0;
951 while (lin_lwp_has_pending (GET_LWP (lp->ptid), &pending, flush_mask))
953 int ret;
955 errno = 0;
956 ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
957 if (debug_lin_lwp)
958 fprintf_unfiltered (gdb_stderr,
959 "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno);
961 lp->stopped = 0;
962 stop_wait_callback (lp, flush_mask);
963 if (debug_lin_lwp)
964 fprintf_unfiltered (gdb_stderr,
965 "FC: Wait finished; saved status is %d\n",
966 lp->status);
969 return 0;
972 /* Return non-zero if LP has a wait status pending. */
974 static int
975 status_callback (struct lwp_info *lp, void *data)
977 /* Only report a pending wait status if we pretend that this has
978 indeed been resumed. */
979 return (lp->status != 0 && lp->resumed);
982 /* Return non-zero if LP isn't stopped. */
984 static int
985 running_callback (struct lwp_info *lp, void *data)
987 return (lp->stopped == 0 || (lp->status != 0 && lp->resumed));
990 /* Count the LWP's that have had events. */
992 static int
993 count_events_callback (struct lwp_info *lp, void *data)
995 int *count = data;
997 gdb_assert (count != NULL);
999 /* Count only LWPs that have a SIGTRAP event pending. */
1000 if (lp->status != 0
1001 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1002 (*count)++;
1004 return 0;
1007 /* Select the LWP (if any) that is currently being single-stepped. */
1009 static int
1010 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
1012 if (lp->step && lp->status != 0)
1013 return 1;
1014 else
1015 return 0;
1018 /* Select the Nth LWP that has had a SIGTRAP event. */
1020 static int
1021 select_event_lwp_callback (struct lwp_info *lp, void *data)
1023 int *selector = data;
1025 gdb_assert (selector != NULL);
1027 /* Select only LWPs that have a SIGTRAP event pending. */
1028 if (lp->status != 0
1029 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1030 if ((*selector)-- == 0)
1031 return 1;
1033 return 0;
1036 static int
1037 cancel_breakpoints_callback (struct lwp_info *lp, void *data)
1039 struct lwp_info *event_lp = data;
1041 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1042 if (lp == event_lp)
1043 return 0;
1045 /* If a LWP other than the LWP that we're reporting an event for has
1046 hit a GDB breakpoint (as opposed to some random trap signal),
1047 then just arrange for it to hit it again later. We don't keep
1048 the SIGTRAP status and don't forward the SIGTRAP signal to the
1049 LWP. We will handle the current event, eventually we will resume
1050 all LWPs, and this one will get its breakpoint trap again.
1052 If we do not do this, then we run the risk that the user will
1053 delete or disable the breakpoint, but the LWP will have already
1054 tripped on it. */
1056 if (lp->status != 0
1057 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
1058 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
1059 DECR_PC_AFTER_BREAK))
1061 if (debug_lin_lwp)
1062 fprintf_unfiltered (gdb_stdlog,
1063 "CBC: Push back breakpoint for %s\n",
1064 target_pid_to_str (lp->ptid));
1066 /* Back up the PC if necessary. */
1067 if (DECR_PC_AFTER_BREAK)
1068 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
1070 /* Throw away the SIGTRAP. */
1071 lp->status = 0;
1074 return 0;
1077 /* Select one LWP out of those that have events pending. */
1079 static void
1080 select_event_lwp (struct lwp_info **orig_lp, int *status)
1082 int num_events = 0;
1083 int random_selector;
1084 struct lwp_info *event_lp;
1086 /* Record the wait status for the origional LWP. */
1087 (*orig_lp)->status = *status;
1089 /* Give preference to any LWP that is being single-stepped. */
1090 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
1091 if (event_lp != NULL)
1093 if (debug_lin_lwp)
1094 fprintf_unfiltered (gdb_stdlog,
1095 "SEL: Select single-step %s\n",
1096 target_pid_to_str (event_lp->ptid));
1098 else
1100 /* No single-stepping LWP. Select one at random, out of those
1101 which have had SIGTRAP events. */
1103 /* First see how many SIGTRAP events we have. */
1104 iterate_over_lwps (count_events_callback, &num_events);
1106 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1107 random_selector = (int)
1108 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
1110 if (debug_lin_lwp && num_events > 1)
1111 fprintf_unfiltered (gdb_stdlog,
1112 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1113 num_events, random_selector);
1115 event_lp = iterate_over_lwps (select_event_lwp_callback,
1116 &random_selector);
1119 if (event_lp != NULL)
1121 /* Switch the event LWP. */
1122 *orig_lp = event_lp;
1123 *status = event_lp->status;
1126 /* Flush the wait status for the event LWP. */
1127 (*orig_lp)->status = 0;
1130 /* Return non-zero if LP has been resumed. */
1132 static int
1133 resumed_callback (struct lwp_info *lp, void *data)
1135 return lp->resumed;
1138 #ifdef CHILD_WAIT
1140 /* We need to override child_wait to support attaching to cloned
1141 processes, since a normal wait (as done by the default version)
1142 ignores those processes. */
1144 /* Wait for child PTID to do something. Return id of the child,
1145 minus_one_ptid in case of error; store status into *OURSTATUS. */
1147 ptid_t
1148 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1150 int save_errno;
1151 int status;
1152 pid_t pid;
1154 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1158 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1159 attached process. */
1160 set_sigio_trap ();
1162 pid = waitpid (GET_PID (ptid), &status, 0);
1163 if (pid == -1 && errno == ECHILD)
1164 /* Try again with __WCLONE to check cloned processes. */
1165 pid = waitpid (GET_PID (ptid), &status, __WCLONE);
1167 if (debug_lin_lwp)
1169 fprintf_unfiltered (gdb_stdlog,
1170 "CW: waitpid %ld received %s\n",
1171 (long) pid, status_to_str (status));
1174 save_errno = errno;
1176 /* Make sure we don't report an event for the exit of the
1177 original program, if we've detached from it. */
1178 if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid))
1180 pid = -1;
1181 save_errno = EINTR;
1184 /* Check for stop events reported by a process we didn't already
1185 know about - in this case, anything other than inferior_ptid.
1187 If we're expecting to receive stopped processes after fork,
1188 vfork, and clone events, then we'll just add the new one to
1189 our list and go back to waiting for the event to be reported
1190 - the stopped process might be returned from waitpid before
1191 or after the event is. If we want to handle debugging of
1192 CLONE_PTRACE processes we need to do more here, i.e. switch
1193 to multi-threaded mode. */
1194 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP
1195 && pid != GET_PID (inferior_ptid))
1197 linux_record_stopped_pid (pid);
1198 pid = -1;
1199 save_errno = EINTR;
1202 /* Handle GNU/Linux's extended waitstatus for trace events. */
1203 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
1204 && status >> 16 != 0)
1206 linux_handle_extended_wait (pid, status, ourstatus);
1208 /* If we see a clone event, detach the child, and don't
1209 report the event. It would be nice to offer some way to
1210 switch into a non-thread-db based threaded mode at this
1211 point. */
1212 if (ourstatus->kind == TARGET_WAITKIND_SPURIOUS)
1214 ptrace (PTRACE_DETACH, ourstatus->value.related_pid, 0, 0);
1215 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1216 pid = -1;
1217 save_errno = EINTR;
1221 clear_sigio_trap ();
1222 clear_sigint_trap ();
1224 while (pid == -1 && save_errno == EINTR);
1226 if (pid == -1)
1228 warning ("Child process unexpectedly missing: %s",
1229 safe_strerror (errno));
1231 /* Claim it exited with unknown signal. */
1232 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1233 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1234 return minus_one_ptid;
1237 if (ourstatus->kind == TARGET_WAITKIND_IGNORE)
1238 store_waitstatus (ourstatus, status);
1240 return pid_to_ptid (pid);
1243 #endif
1245 /* Stop an active thread, verify it still exists, then resume it. */
1247 static int
1248 stop_and_resume_callback (struct lwp_info *lp, void *data)
1250 struct lwp_info *ptr;
1252 if (!lp->stopped && !lp->signalled)
1254 stop_callback (lp, NULL);
1255 stop_wait_callback (lp, NULL);
1256 /* Resume if the lwp still exists. */
1257 for (ptr = lwp_list; ptr; ptr = ptr->next)
1258 if (lp == ptr)
1260 resume_callback (lp, NULL);
1261 resume_set_callback (lp, NULL);
1264 return 0;
1267 static ptid_t
1268 lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1270 struct lwp_info *lp = NULL;
1271 int options = 0;
1272 int status = 0;
1273 pid_t pid = PIDGET (ptid);
1274 sigset_t flush_mask;
1276 sigemptyset (&flush_mask);
1278 /* Make sure SIGCHLD is blocked. */
1279 if (!sigismember (&blocked_mask, SIGCHLD))
1281 sigaddset (&blocked_mask, SIGCHLD);
1282 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1285 retry:
1287 /* Make sure there is at least one LWP that has been resumed, at
1288 least if there are any LWPs at all. */
1289 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
1291 /* First check if there is a LWP with a wait status pending. */
1292 if (pid == -1)
1294 /* Any LWP that's been resumed will do. */
1295 lp = iterate_over_lwps (status_callback, NULL);
1296 if (lp)
1298 status = lp->status;
1299 lp->status = 0;
1301 if (debug_lin_lwp && status)
1302 fprintf_unfiltered (gdb_stdlog,
1303 "LLW: Using pending wait status %s for %s.\n",
1304 status_to_str (status),
1305 target_pid_to_str (lp->ptid));
1308 /* But if we don't fine one, we'll have to wait, and check both
1309 cloned and uncloned processes. We start with the cloned
1310 processes. */
1311 options = __WCLONE | WNOHANG;
1313 else if (is_lwp (ptid))
1315 if (debug_lin_lwp)
1316 fprintf_unfiltered (gdb_stdlog,
1317 "LLW: Waiting for specific LWP %s.\n",
1318 target_pid_to_str (ptid));
1320 /* We have a specific LWP to check. */
1321 lp = find_lwp_pid (ptid);
1322 gdb_assert (lp);
1323 status = lp->status;
1324 lp->status = 0;
1326 if (debug_lin_lwp && status)
1327 fprintf_unfiltered (gdb_stdlog,
1328 "LLW: Using pending wait status %s for %s.\n",
1329 status_to_str (status),
1330 target_pid_to_str (lp->ptid));
1332 /* If we have to wait, take into account whether PID is a cloned
1333 process or not. And we have to convert it to something that
1334 the layer beneath us can understand. */
1335 options = lp->cloned ? __WCLONE : 0;
1336 pid = GET_LWP (ptid);
1339 if (status && lp->signalled)
1341 /* A pending SIGSTOP may interfere with the normal stream of
1342 events. In a typical case where interference is a problem,
1343 we have a SIGSTOP signal pending for LWP A while
1344 single-stepping it, encounter an event in LWP B, and take the
1345 pending SIGSTOP while trying to stop LWP A. After processing
1346 the event in LWP B, LWP A is continued, and we'll never see
1347 the SIGTRAP associated with the last time we were
1348 single-stepping LWP A. */
1350 /* Resume the thread. It should halt immediately returning the
1351 pending SIGSTOP. */
1352 registers_changed ();
1353 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1354 TARGET_SIGNAL_0);
1355 if (debug_lin_lwp)
1356 fprintf_unfiltered (gdb_stdlog,
1357 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1358 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1359 target_pid_to_str (lp->ptid));
1360 lp->stopped = 0;
1361 gdb_assert (lp->resumed);
1363 /* This should catch the pending SIGSTOP. */
1364 stop_wait_callback (lp, NULL);
1367 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1368 attached process. */
1369 set_sigio_trap ();
1371 while (status == 0)
1373 pid_t lwpid;
1375 lwpid = waitpid (pid, &status, options);
1376 if (lwpid > 0)
1378 gdb_assert (pid == -1 || lwpid == pid);
1380 if (debug_lin_lwp)
1382 fprintf_unfiltered (gdb_stdlog,
1383 "LLW: waitpid %ld received %s\n",
1384 (long) lwpid, status_to_str (status));
1387 lp = find_lwp_pid (pid_to_ptid (lwpid));
1389 /* Check for stop events reported by a process we didn't
1390 already know about - anything not already in our LWP
1391 list.
1393 If we're expecting to receive stopped processes after
1394 fork, vfork, and clone events, then we'll just add the
1395 new one to our list and go back to waiting for the event
1396 to be reported - the stopped process might be returned
1397 from waitpid before or after the event is. */
1398 if (WIFSTOPPED (status) && !lp)
1400 linux_record_stopped_pid (lwpid);
1401 status = 0;
1402 continue;
1405 /* Make sure we don't report an event for the exit of an LWP not in
1406 our list, i.e. not part of the current process. This can happen
1407 if we detach from a program we original forked and then it
1408 exits. */
1409 if (!WIFSTOPPED (status) && !lp)
1411 status = 0;
1412 continue;
1415 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1416 CLONE_PTRACE processes which do not use the thread library -
1417 otherwise we wouldn't find the new LWP this way. That doesn't
1418 currently work, and the following code is currently unreachable
1419 due to the two blocks above. If it's fixed some day, this code
1420 should be broken out into a function so that we can also pick up
1421 LWPs from the new interface. */
1422 if (!lp)
1424 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
1425 if (options & __WCLONE)
1426 lp->cloned = 1;
1428 if (threaded)
1430 gdb_assert (WIFSTOPPED (status)
1431 && WSTOPSIG (status) == SIGSTOP);
1432 lp->signalled = 1;
1434 if (!in_thread_list (inferior_ptid))
1436 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1437 GET_PID (inferior_ptid));
1438 add_thread (inferior_ptid);
1441 add_thread (lp->ptid);
1442 printf_unfiltered ("[New %s]\n",
1443 target_pid_to_str (lp->ptid));
1447 /* Handle GNU/Linux's extended waitstatus for trace events. */
1448 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1450 if (debug_lin_lwp)
1451 fprintf_unfiltered (gdb_stdlog,
1452 "LLW: Handling extended status 0x%06x\n",
1453 status);
1454 if (lin_lwp_handle_extended (lp, status))
1456 status = 0;
1457 continue;
1461 /* Check if the thread has exited. */
1462 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
1464 if (in_thread_list (lp->ptid))
1466 /* Core GDB cannot deal with us deleting the current
1467 thread. */
1468 if (!ptid_equal (lp->ptid, inferior_ptid))
1469 delete_thread (lp->ptid);
1470 printf_unfiltered ("[%s exited]\n",
1471 target_pid_to_str (lp->ptid));
1474 /* If this is the main thread, we must stop all threads and
1475 verify if they are still alive. This is because in the nptl
1476 thread model, there is no signal issued for exiting LWPs
1477 other than the main thread. We only get the main thread
1478 exit signal once all child threads have already exited.
1479 If we stop all the threads and use the stop_wait_callback
1480 to check if they have exited we can determine whether this
1481 signal should be ignored or whether it means the end of the
1482 debugged application, regardless of which threading model
1483 is being used. */
1484 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
1486 lp->stopped = 1;
1487 iterate_over_lwps (stop_and_resume_callback, NULL);
1490 if (debug_lin_lwp)
1491 fprintf_unfiltered (gdb_stdlog,
1492 "LLW: %s exited.\n",
1493 target_pid_to_str (lp->ptid));
1495 delete_lwp (lp->ptid);
1497 /* If there is at least one more LWP, then the exit signal
1498 was not the end of the debugged application and should be
1499 ignored. */
1500 if (num_lwps > 0)
1502 /* Make sure there is at least one thread running. */
1503 gdb_assert (iterate_over_lwps (running_callback, NULL));
1505 /* Discard the event. */
1506 status = 0;
1507 continue;
1511 /* Check if the current LWP has previously exited. In the nptl
1512 thread model, LWPs other than the main thread do not issue
1513 signals when they exit so we must check whenever the thread
1514 has stopped. A similar check is made in stop_wait_callback(). */
1515 if (num_lwps > 1 && !lin_lwp_thread_alive (lp->ptid))
1517 if (in_thread_list (lp->ptid))
1519 /* Core GDB cannot deal with us deleting the current
1520 thread. */
1521 if (!ptid_equal (lp->ptid, inferior_ptid))
1522 delete_thread (lp->ptid);
1523 printf_unfiltered ("[%s exited]\n",
1524 target_pid_to_str (lp->ptid));
1526 if (debug_lin_lwp)
1527 fprintf_unfiltered (gdb_stdlog,
1528 "LLW: %s exited.\n",
1529 target_pid_to_str (lp->ptid));
1531 delete_lwp (lp->ptid);
1533 /* Make sure there is at least one thread running. */
1534 gdb_assert (iterate_over_lwps (running_callback, NULL));
1536 /* Discard the event. */
1537 status = 0;
1538 continue;
1541 /* Make sure we don't report a SIGSTOP that we sent
1542 ourselves in an attempt to stop an LWP. */
1543 if (lp->signalled
1544 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
1546 if (debug_lin_lwp)
1547 fprintf_unfiltered (gdb_stdlog,
1548 "LLW: Delayed SIGSTOP caught for %s.\n",
1549 target_pid_to_str (lp->ptid));
1551 /* This is a delayed SIGSTOP. */
1552 lp->signalled = 0;
1554 registers_changed ();
1555 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1556 TARGET_SIGNAL_0);
1557 if (debug_lin_lwp)
1558 fprintf_unfiltered (gdb_stdlog,
1559 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
1560 lp->step ?
1561 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1562 target_pid_to_str (lp->ptid));
1564 lp->stopped = 0;
1565 gdb_assert (lp->resumed);
1567 /* Discard the event. */
1568 status = 0;
1569 continue;
1572 break;
1575 if (pid == -1)
1577 /* Alternate between checking cloned and uncloned processes. */
1578 options ^= __WCLONE;
1580 /* And suspend every time we have checked both. */
1581 if (options & __WCLONE)
1582 sigsuspend (&suspend_mask);
1585 /* We shouldn't end up here unless we want to try again. */
1586 gdb_assert (status == 0);
1589 clear_sigio_trap ();
1590 clear_sigint_trap ();
1592 gdb_assert (lp);
1594 /* Don't report signals that GDB isn't interested in, such as
1595 signals that are neither printed nor stopped upon. Stopping all
1596 threads can be a bit time-consuming so if we want decent
1597 performance with heavily multi-threaded programs, especially when
1598 they're using a high frequency timer, we'd better avoid it if we
1599 can. */
1601 if (WIFSTOPPED (status))
1603 int signo = target_signal_from_host (WSTOPSIG (status));
1605 if (signal_stop_state (signo) == 0
1606 && signal_print_state (signo) == 0
1607 && signal_pass_state (signo) == 1)
1609 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
1610 here? It is not clear we should. GDB may not expect
1611 other threads to run. On the other hand, not resuming
1612 newly attached threads may cause an unwanted delay in
1613 getting them running. */
1614 registers_changed ();
1615 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
1616 if (debug_lin_lwp)
1617 fprintf_unfiltered (gdb_stdlog,
1618 "LLW: %s %s, %s (preempt 'handle')\n",
1619 lp->step ?
1620 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1621 target_pid_to_str (lp->ptid),
1622 signo ? strsignal (signo) : "0");
1623 lp->stopped = 0;
1624 status = 0;
1625 goto retry;
1628 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
1630 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
1631 forwarded to the entire process group, that is, all LWP's
1632 will receive it. Since we only want to report it once,
1633 we try to flush it from all LWPs except this one. */
1634 sigaddset (&flush_mask, SIGINT);
1638 /* This LWP is stopped now. */
1639 lp->stopped = 1;
1641 if (debug_lin_lwp)
1642 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
1643 status_to_str (status), target_pid_to_str (lp->ptid));
1645 /* Now stop all other LWP's ... */
1646 iterate_over_lwps (stop_callback, NULL);
1648 /* ... and wait until all of them have reported back that they're no
1649 longer running. */
1650 iterate_over_lwps (stop_wait_callback, &flush_mask);
1651 iterate_over_lwps (flush_callback, &flush_mask);
1653 /* If we're not waiting for a specific LWP, choose an event LWP from
1654 among those that have had events. Giving equal priority to all
1655 LWPs that have had events helps prevent starvation. */
1656 if (pid == -1)
1657 select_event_lwp (&lp, &status);
1659 /* Now that we've selected our final event LWP, cancel any
1660 breakpoints in other LWPs that have hit a GDB breakpoint. See
1661 the comment in cancel_breakpoints_callback to find out why. */
1662 iterate_over_lwps (cancel_breakpoints_callback, lp);
1664 /* If we're not running in "threaded" mode, we'll report the bare
1665 process id. */
1667 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
1669 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1670 if (debug_lin_lwp)
1671 fprintf_unfiltered (gdb_stdlog,
1672 "LLW: trap_ptid is %s.\n",
1673 target_pid_to_str (trap_ptid));
1675 else
1676 trap_ptid = null_ptid;
1678 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
1680 *ourstatus = lp->waitstatus;
1681 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1683 else
1684 store_waitstatus (ourstatus, status);
1686 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1689 static int
1690 kill_callback (struct lwp_info *lp, void *data)
1692 errno = 0;
1693 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
1694 if (debug_lin_lwp)
1695 fprintf_unfiltered (gdb_stdlog,
1696 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
1697 target_pid_to_str (lp->ptid),
1698 errno ? safe_strerror (errno) : "OK");
1700 return 0;
1703 static int
1704 kill_wait_callback (struct lwp_info *lp, void *data)
1706 pid_t pid;
1708 /* We must make sure that there are no pending events (delayed
1709 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
1710 program doesn't interfere with any following debugging session. */
1712 /* For cloned processes we must check both with __WCLONE and
1713 without, since the exit status of a cloned process isn't reported
1714 with __WCLONE. */
1715 if (lp->cloned)
1719 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
1720 if (pid != (pid_t) -1 && debug_lin_lwp)
1722 fprintf_unfiltered (gdb_stdlog,
1723 "KWC: wait %s received unknown.\n",
1724 target_pid_to_str (lp->ptid));
1727 while (pid == GET_LWP (lp->ptid));
1729 gdb_assert (pid == -1 && errno == ECHILD);
1734 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
1735 if (pid != (pid_t) -1 && debug_lin_lwp)
1737 fprintf_unfiltered (gdb_stdlog,
1738 "KWC: wait %s received unk.\n",
1739 target_pid_to_str (lp->ptid));
1742 while (pid == GET_LWP (lp->ptid));
1744 gdb_assert (pid == -1 && errno == ECHILD);
1745 return 0;
1748 static void
1749 lin_lwp_kill (void)
1751 /* Kill all LWP's ... */
1752 iterate_over_lwps (kill_callback, NULL);
1754 /* ... and wait until we've flushed all events. */
1755 iterate_over_lwps (kill_wait_callback, NULL);
1757 target_mourn_inferior ();
1760 static void
1761 lin_lwp_create_inferior (char *exec_file, char *allargs, char **env,
1762 int from_tty)
1764 child_ops.to_create_inferior (exec_file, allargs, env, from_tty);
1767 static void
1768 lin_lwp_mourn_inferior (void)
1770 trap_ptid = null_ptid;
1772 /* Destroy LWP info; it's no longer valid. */
1773 init_lwp_list ();
1775 /* Restore the original signal mask. */
1776 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1777 sigemptyset (&blocked_mask);
1779 child_ops.to_mourn_inferior ();
1782 static int
1783 lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
1784 struct mem_attrib *attrib, struct target_ops *target)
1786 struct cleanup *old_chain = save_inferior_ptid ();
1787 int xfer;
1789 if (is_lwp (inferior_ptid))
1790 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
1792 xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1793 if (xfer == 0)
1794 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1796 do_cleanups (old_chain);
1797 return xfer;
1800 static int
1801 lin_lwp_thread_alive (ptid_t ptid)
1803 gdb_assert (is_lwp (ptid));
1805 errno = 0;
1806 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
1807 if (debug_lin_lwp)
1808 fprintf_unfiltered (gdb_stdlog,
1809 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
1810 target_pid_to_str (ptid),
1811 errno ? safe_strerror (errno) : "OK");
1812 if (errno)
1813 return 0;
1815 return 1;
1818 static char *
1819 lin_lwp_pid_to_str (ptid_t ptid)
1821 static char buf[64];
1823 if (is_lwp (ptid))
1825 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
1826 return buf;
1829 return normal_pid_to_str (ptid);
1832 static void
1833 init_lin_lwp_ops (void)
1835 #if 0
1836 lin_lwp_ops.to_open = lin_lwp_open;
1837 #endif
1838 lin_lwp_ops.to_shortname = "lwp-layer";
1839 lin_lwp_ops.to_longname = "lwp-layer";
1840 lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
1841 lin_lwp_ops.to_attach = lin_lwp_attach;
1842 lin_lwp_ops.to_detach = lin_lwp_detach;
1843 lin_lwp_ops.to_resume = lin_lwp_resume;
1844 lin_lwp_ops.to_wait = lin_lwp_wait;
1845 /* fetch_inferior_registers and store_inferior_registers will
1846 honor the LWP id, so we can use them directly. */
1847 lin_lwp_ops.to_fetch_registers = fetch_inferior_registers;
1848 lin_lwp_ops.to_store_registers = store_inferior_registers;
1849 lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
1850 lin_lwp_ops.to_kill = lin_lwp_kill;
1851 lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
1852 lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
1853 lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
1854 lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
1855 lin_lwp_ops.to_post_startup_inferior = child_post_startup_inferior;
1856 lin_lwp_ops.to_post_attach = child_post_attach;
1857 lin_lwp_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
1858 lin_lwp_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
1859 lin_lwp_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
1861 lin_lwp_ops.to_stratum = thread_stratum;
1862 lin_lwp_ops.to_has_thread_control = tc_schedlock;
1863 lin_lwp_ops.to_magic = OPS_MAGIC;
1866 static void
1867 sigchld_handler (int signo)
1869 /* Do nothing. The only reason for this handler is that it allows
1870 us to use sigsuspend in lin_lwp_wait above to wait for the
1871 arrival of a SIGCHLD. */
1874 void
1875 _initialize_lin_lwp (void)
1877 struct sigaction action;
1879 extern void thread_db_init (struct target_ops *);
1881 init_lin_lwp_ops ();
1882 add_target (&lin_lwp_ops);
1883 thread_db_init (&lin_lwp_ops);
1885 /* Save the original signal mask. */
1886 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
1888 action.sa_handler = sigchld_handler;
1889 sigemptyset (&action.sa_mask);
1890 action.sa_flags = 0;
1891 sigaction (SIGCHLD, &action, NULL);
1893 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1894 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
1895 sigdelset (&suspend_mask, SIGCHLD);
1897 sigemptyset (&blocked_mask);
1899 add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
1900 (char *) &debug_lin_lwp,
1901 "Set debugging of GNU/Linux lwp module.\n\
1902 Enables printf debugging output.\n", &setdebuglist), &showdebuglist);
1906 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
1907 the GNU/Linux Threads library and therefore doesn't really belong
1908 here. */
1910 /* Read variable NAME in the target and return its value if found.
1911 Otherwise return zero. It is assumed that the type of the variable
1912 is `int'. */
1914 static int
1915 get_signo (const char *name)
1917 struct minimal_symbol *ms;
1918 int signo;
1920 ms = lookup_minimal_symbol (name, NULL, NULL);
1921 if (ms == NULL)
1922 return 0;
1924 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
1925 sizeof (signo)) != 0)
1926 return 0;
1928 return signo;
1931 /* Return the set of signals used by the threads library in *SET. */
1933 void
1934 lin_thread_get_thread_signals (sigset_t *set)
1936 struct sigaction action;
1937 int restart, cancel;
1939 sigemptyset (set);
1941 restart = get_signo ("__pthread_sig_restart");
1942 if (restart == 0)
1943 return;
1945 cancel = get_signo ("__pthread_sig_cancel");
1946 if (cancel == 0)
1947 return;
1949 sigaddset (set, restart);
1950 sigaddset (set, cancel);
1952 /* The GNU/Linux Threads library makes terminating threads send a
1953 special "cancel" signal instead of SIGCHLD. Make sure we catch
1954 those (to prevent them from terminating GDB itself, which is
1955 likely to be their default action) and treat them the same way as
1956 SIGCHLD. */
1958 action.sa_handler = sigchld_handler;
1959 sigemptyset (&action.sa_mask);
1960 action.sa_flags = 0;
1961 sigaction (cancel, &action, NULL);
1963 /* We block the "cancel" signal throughout this code ... */
1964 sigaddset (&blocked_mask, cancel);
1965 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1967 /* ... except during a sigsuspend. */
1968 sigdelset (&suspend_mask, cancel);