2003-09-11 David Carlton <carlton@kealia.com>
[deliverable/binutils-gdb.git] / gdb / lin-lwp.c
CommitLineData
8605d56e 1/* Multi-threaded debugging support for GNU/Linux (LWP layer).
b3ba1b44 2 Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
fb0e1ba7
MK
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21#include "defs.h"
22
23#include "gdb_assert.h"
309367d4 24#include "gdb_string.h"
fb0e1ba7
MK
25#include <errno.h>
26#include <signal.h>
b757528f
JJ
27#ifdef HAVE_TKILL_SYSCALL
28#include <unistd.h>
29#include <sys/syscall.h>
30#endif
fb0e1ba7
MK
31#include <sys/ptrace.h>
32#include "gdb_wait.h"
33
34#include "gdbthread.h"
35#include "inferior.h"
36#include "target.h"
4e052eda 37#include "regcache.h"
7ca673cd 38#include "gdbcmd.h"
fb0e1ba7 39
7ca673cd 40static int debug_lin_lwp;
1b84163e 41extern char *strsignal (int sig);
fb0e1ba7 42
0274a8ce
MS
43#include "linux-nat.h"
44
8605d56e
AC
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.
fb0e1ba7
MK
52
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
8605d56e 58 using the Linux kernel 2.4 or above.
fb0e1ba7 59
8605d56e
AC
60 Note that there are some peculiarities in GNU/Linux that affect
61 this code:
fb0e1ba7
MK
62
63 - In general one should specify the __WCLONE flag to waitpid in
3f07c44b
MK
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
8605d56e
AC
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.
fb0e1ba7
MK
69
70 - When a traced, cloned process exits and is waited for by the
4c8de859 71 debugger, the kernel reassigns it to the original parent and
8605d56e
AC
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. */
fb0e1ba7 77
fb0e1ba7
MK
78/* List of known LWPs. */
79static struct lwp_info *lwp_list;
80
81/* Number of LWPs in the list. */
82static int num_lwps;
83
84/* Non-zero if we're running in "threaded" mode. */
85static int threaded;
86\f
87
ca6724c1
KB
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)
fb0e1ba7 92
fb0e1ba7
MK
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. */
39f77062 95ptid_t trap_ptid;
fb0e1ba7
MK
96\f
97
98/* This module's target-specific operations. */
99static struct target_ops lin_lwp_ops;
100
101/* The standard child operations. */
102extern struct target_ops child_ops;
103
3f07c44b
MK
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
4c8de859 106 the WNOHANG flag and call waitpid in a loop. To optimize
3f07c44b
MK
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
4c8de859 110 original signal mask such that we can restore it before creating a
3f07c44b
MK
111 new process in order to avoid blocking certain signals in the
112 inferior. We then block SIGCHLD during the waitpid/sigsuspend
113 loop. */
114
4c8de859 115/* Original signal mask. */
3f07c44b
MK
116static sigset_t normal_mask;
117
fb0e1ba7
MK
118/* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
119 _initialize_lin_lwp. */
120static sigset_t suspend_mask;
3f07c44b
MK
121
122/* Signals to block to make that sigsuspend work. */
123static sigset_t blocked_mask;
fb0e1ba7
MK
124\f
125
126/* Prototypes for local functions. */
c194fbe1 127static int stop_wait_callback (struct lwp_info *lp, void *data);
b757528f 128static int lin_lwp_thread_alive (ptid_t ptid);
fb0e1ba7 129\f
58eeadba
MK
130/* Convert wait status STATUS to a string. Used for printing debug
131 messages only. */
fb0e1ba7 132
58eeadba
MK
133static char *
134status_to_str (int status)
135{
136 static char buf[64];
137
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
6949171e 145 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
58eeadba
MK
146
147 return buf;
148}
149\f
c194fbe1
MK
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. */
fb0e1ba7
MK
154
155static void
156init_lwp_list (void)
157{
158 struct lwp_info *lp, *lpnext;
159
160 for (lp = lwp_list; lp; lp = lpnext)
161 {
162 lpnext = lp->next;
b8c9b27d 163 xfree (lp);
fb0e1ba7
MK
164 }
165
166 lwp_list = NULL;
167 num_lwps = 0;
168 threaded = 0;
169}
170
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. */
174
175static struct lwp_info *
39f77062 176add_lwp (ptid_t ptid)
fb0e1ba7
MK
177{
178 struct lwp_info *lp;
179
39f77062 180 gdb_assert (is_lwp (ptid));
fb0e1ba7
MK
181
182 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
183
184 memset (lp, 0, sizeof (struct lwp_info));
185
39f77062 186 lp->ptid = ptid;
fb0e1ba7
MK
187
188 lp->next = lwp_list;
189 lwp_list = lp;
190 if (++num_lwps > 1)
191 threaded = 1;
192
193 return lp;
194}
195
196/* Remove the LWP specified by PID from the list. */
197
198static void
39f77062 199delete_lwp (ptid_t ptid)
fb0e1ba7
MK
200{
201 struct lwp_info *lp, *lpprev;
202
203 lpprev = NULL;
204
205 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
39f77062 206 if (ptid_equal (lp->ptid, ptid))
fb0e1ba7
MK
207 break;
208
209 if (!lp)
210 return;
211
212 /* We don't go back to "non-threaded" mode if the number of threads
213 becomes less than two. */
214 num_lwps--;
215
216 if (lpprev)
217 lpprev->next = lp->next;
218 else
219 lwp_list = lp->next;
220
b8c9b27d 221 xfree (lp);
fb0e1ba7
MK
222}
223
224/* Return a pointer to the structure describing the LWP corresponding
225 to PID. If no corresponding LWP could be found, return NULL. */
226
227static struct lwp_info *
39f77062 228find_lwp_pid (ptid_t ptid)
fb0e1ba7
MK
229{
230 struct lwp_info *lp;
39f77062 231 int lwp;
fb0e1ba7 232
39f77062
KB
233 if (is_lwp (ptid))
234 lwp = GET_LWP (ptid);
235 else
236 lwp = GET_PID (ptid);
fb0e1ba7
MK
237
238 for (lp = lwp_list; lp; lp = lp->next)
39f77062 239 if (lwp == GET_LWP (lp->ptid))
fb0e1ba7
MK
240 return lp;
241
242 return NULL;
243}
244
245/* Call CALLBACK with its second argument set to DATA for every LWP in
246 the list. If CALLBACK returns 1 for a particular LWP, return a
247 pointer to the structure describing that LWP immediately.
248 Otherwise return NULL. */
249
250struct lwp_info *
251iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
252{
fce0e6e1 253 struct lwp_info *lp, *lpnext;
fb0e1ba7 254
fce0e6e1
MK
255 for (lp = lwp_list; lp; lp = lpnext)
256 {
257 lpnext = lp->next;
258 if ((*callback) (lp, data))
259 return lp;
260 }
fb0e1ba7
MK
261
262 return NULL;
263}
264\f
265
fb0e1ba7
MK
266#if 0
267static void
268lin_lwp_open (char *args, int from_tty)
269{
270 push_target (&lin_lwp_ops);
271}
272#endif
273
274/* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
275 a message telling the user that a new LWP has been added to the
276 process. */
277
278void
39f77062 279lin_lwp_attach_lwp (ptid_t ptid, int verbose)
fb0e1ba7
MK
280{
281 struct lwp_info *lp;
282
39f77062 283 gdb_assert (is_lwp (ptid));
fb0e1ba7 284
da9c7185
KB
285 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
286 to interrupt either the ptrace() or waitpid() calls below. */
6949171e 287 if (!sigismember (&blocked_mask, SIGCHLD))
da9c7185
KB
288 {
289 sigaddset (&blocked_mask, SIGCHLD);
290 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
291 }
292
fb0e1ba7 293 if (verbose)
39f77062 294 printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
fb0e1ba7 295
c194fbe1
MK
296 lp = find_lwp_pid (ptid);
297 if (lp == NULL)
298 lp = add_lwp (ptid);
299
cacab7c4
MK
300 /* We assume that we're already attached to any LWP that has an
301 id equal to the overall process id. */
302 if (GET_LWP (ptid) != GET_PID (ptid))
c4365b19 303 {
cacab7c4
MK
304 pid_t pid;
305 int status;
306
307 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
308 error ("Can't attach %s: %s", target_pid_to_str (ptid),
dc672865 309 safe_strerror (errno));
cacab7c4 310
9fe7d6bf 311 if (debug_lin_lwp)
6949171e
JJ
312 fprintf_unfiltered (gdb_stdlog,
313 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
9fe7d6bf
MS
314 target_pid_to_str (ptid));
315
cacab7c4
MK
316 pid = waitpid (GET_LWP (ptid), &status, 0);
317 if (pid == -1 && errno == ECHILD)
318 {
319 /* Try again with __WCLONE to check cloned processes. */
320 pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
321 lp->cloned = 1;
322 }
323
324 gdb_assert (pid == GET_LWP (ptid)
325 && WIFSTOPPED (status) && WSTOPSIG (status));
326
4de4c07c
DJ
327 child_post_attach (pid);
328
cacab7c4 329 lp->stopped = 1;
9fe7d6bf
MS
330
331 if (debug_lin_lwp)
332 {
333 fprintf_unfiltered (gdb_stdlog,
334 "LLAL: waitpid %s received %s\n",
6949171e 335 target_pid_to_str (ptid),
9fe7d6bf
MS
336 status_to_str (status));
337 }
c4365b19 338 }
da9c7185
KB
339 else
340 {
341 /* We assume that the LWP representing the original process
6949171e
JJ
342 is already stopped. Mark it as stopped in the data structure
343 that the lin-lwp layer uses to keep track of threads. Note
344 that this won't have already been done since the main thread
345 will have, we assume, been stopped by an attach from a
346 different layer. */
da9c7185
KB
347 lp->stopped = 1;
348 }
fb0e1ba7
MK
349}
350
351static void
352lin_lwp_attach (char *args, int from_tty)
353{
c194fbe1 354 struct lwp_info *lp;
cacab7c4
MK
355 pid_t pid;
356 int status;
c194fbe1 357
fb0e1ba7
MK
358 /* FIXME: We should probably accept a list of process id's, and
359 attach all of them. */
c194fbe1
MK
360 child_ops.to_attach (args, from_tty);
361
362 /* Add the initial process as the first LWP to the list. */
cacab7c4 363 lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
c194fbe1
MK
364
365 /* Make sure the initial process is stopped. The user-level threads
366 layer might want to poke around in the inferior, and that won't
367 work if things haven't stabilized yet. */
cacab7c4
MK
368 pid = waitpid (GET_PID (inferior_ptid), &status, 0);
369 if (pid == -1 && errno == ECHILD)
370 {
371 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
372
373 /* Try again with __WCLONE to check cloned processes. */
374 pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
375 lp->cloned = 1;
376 }
377
378 gdb_assert (pid == GET_PID (inferior_ptid)
379 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
380
381 lp->stopped = 1;
c194fbe1
MK
382
383 /* Fake the SIGSTOP that core GDB expects. */
384 lp->status = W_STOPCODE (SIGSTOP);
fce0e6e1 385 lp->resumed = 1;
9fe7d6bf
MS
386 if (debug_lin_lwp)
387 {
388 fprintf_unfiltered (gdb_stdlog,
6949171e 389 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid);
9fe7d6bf 390 }
c194fbe1
MK
391}
392
393static int
394detach_callback (struct lwp_info *lp, void *data)
395{
396 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
397
398 if (debug_lin_lwp && lp->status)
9fe7d6bf 399 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
6949171e 400 strsignal (WSTOPSIG (lp->status)),
9fe7d6bf 401 target_pid_to_str (lp->ptid));
c194fbe1
MK
402
403 while (lp->signalled && lp->stopped)
404 {
9fe7d6bf 405 errno = 0;
c194fbe1
MK
406 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
407 WSTOPSIG (lp->status)) < 0)
408 error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
dc672865 409 safe_strerror (errno));
c194fbe1 410
9fe7d6bf 411 if (debug_lin_lwp)
6949171e 412 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf
MS
413 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
414 target_pid_to_str (lp->ptid),
6949171e 415 status_to_str (lp->status));
9fe7d6bf 416
c194fbe1 417 lp->stopped = 0;
c4365b19 418 lp->signalled = 0;
c194fbe1 419 lp->status = 0;
bfb39158
DJ
420 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
421 here. But since lp->signalled was cleared above,
422 stop_wait_callback didn't do anything; the process was left
423 running. Shouldn't we be waiting for it to stop?
424 I've removed the call, since stop_wait_callback now does do
425 something when called with lp->signalled == 0. */
c194fbe1
MK
426
427 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
428 }
429
cacab7c4
MK
430 /* We don't actually detach from the LWP that has an id equal to the
431 overall process id just yet. */
432 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
c194fbe1 433 {
9fe7d6bf 434 errno = 0;
c194fbe1
MK
435 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
436 WSTOPSIG (lp->status)) < 0)
437 error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
dc672865 438 safe_strerror (errno));
c194fbe1 439
9fe7d6bf
MS
440 if (debug_lin_lwp)
441 fprintf_unfiltered (gdb_stdlog,
442 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
6949171e 443 target_pid_to_str (lp->ptid),
9fe7d6bf
MS
444 strsignal (WSTOPSIG (lp->status)));
445
c194fbe1
MK
446 delete_lwp (lp->ptid);
447 }
448
449 return 0;
fb0e1ba7
MK
450}
451
452static void
453lin_lwp_detach (char *args, int from_tty)
454{
c194fbe1
MK
455 iterate_over_lwps (detach_callback, NULL);
456
cacab7c4 457 /* Only the initial process should be left right now. */
c194fbe1
MK
458 gdb_assert (num_lwps == 1);
459
460 trap_ptid = null_ptid;
461
462 /* Destroy LWP info; it's no longer valid. */
463 init_lwp_list ();
464
465 /* Restore the original signal mask. */
466 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
467 sigemptyset (&blocked_mask);
468
01263b57 469 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
c194fbe1 470 child_ops.to_detach (args, from_tty);
fb0e1ba7
MK
471}
472\f
473
fb0e1ba7
MK
474/* Resume LP. */
475
476static int
477resume_callback (struct lwp_info *lp, void *data)
478{
479 if (lp->stopped && lp->status == 0)
480 {
481 struct thread_info *tp;
482
39f77062 483 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
9fe7d6bf 484 if (debug_lin_lwp)
6949171e 485 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf
MS
486 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
487 target_pid_to_str (lp->ptid));
fb0e1ba7
MK
488 lp->stopped = 0;
489 lp->step = 0;
490 }
491
492 return 0;
493}
494
fce0e6e1
MK
495static int
496resume_clear_callback (struct lwp_info *lp, void *data)
497{
498 lp->resumed = 0;
499 return 0;
500}
501
502static int
503resume_set_callback (struct lwp_info *lp, void *data)
504{
505 lp->resumed = 1;
506 return 0;
507}
508
fb0e1ba7 509static void
39f77062 510lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
fb0e1ba7
MK
511{
512 struct lwp_info *lp;
513 int resume_all;
514
2a4b7c45
DJ
515 /* A specific PTID means `step only this process id'. */
516 resume_all = (PIDGET (ptid) == -1);
fb0e1ba7 517
fce0e6e1
MK
518 if (resume_all)
519 iterate_over_lwps (resume_set_callback, NULL);
520 else
521 iterate_over_lwps (resume_clear_callback, NULL);
522
fb0e1ba7 523 /* If PID is -1, it's the current inferior that should be
c4365b19 524 handled specially. */
39f77062
KB
525 if (PIDGET (ptid) == -1)
526 ptid = inferior_ptid;
fb0e1ba7 527
39f77062 528 lp = find_lwp_pid (ptid);
fb0e1ba7
MK
529 if (lp)
530 {
39f77062 531 ptid = pid_to_ptid (GET_LWP (lp->ptid));
fb0e1ba7 532
fb0e1ba7
MK
533 /* Remember if we're stepping. */
534 lp->step = step;
535
fce0e6e1
MK
536 /* Mark this LWP as resumed. */
537 lp->resumed = 1;
538
fb0e1ba7
MK
539 /* If we have a pending wait status for this thread, there is no
540 point in resuming the process. */
541 if (lp->status)
542 {
543 /* FIXME: What should we do if we are supposed to continue
6949171e 544 this thread with a signal? */
fb0e1ba7
MK
545 gdb_assert (signo == TARGET_SIGNAL_0);
546 return;
547 }
40564aca
MK
548
549 /* Mark LWP as not stopped to prevent it from being continued by
6949171e 550 resume_callback. */
40564aca 551 lp->stopped = 0;
fb0e1ba7
MK
552 }
553
554 if (resume_all)
555 iterate_over_lwps (resume_callback, NULL);
556
39f77062 557 child_resume (ptid, step, signo);
9fe7d6bf
MS
558 if (debug_lin_lwp)
559 fprintf_unfiltered (gdb_stdlog,
560 "LLR: %s %s, %s (resume event thread)\n",
561 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
562 target_pid_to_str (ptid),
563 signo ? strsignal (signo) : "0");
fb0e1ba7
MK
564}
565\f
566
b757528f
JJ
567/* Issue kill to specified lwp. */
568
569static int tkill_failed;
570
571static int
572kill_lwp (int lwpid, int signo)
573{
574 errno = 0;
575
576/* Use tkill, if possible, in case we are using nptl threads. If tkill
577 fails, then we are not using nptl threads and we should be using kill. */
578
579#ifdef HAVE_TKILL_SYSCALL
580 if (!tkill_failed)
581 {
582 int ret = syscall (__NR_tkill, lwpid, signo);
583 if (errno != ENOSYS)
584 return ret;
585 errno = 0;
586 tkill_failed = 1;
587 }
588#endif
589
590 return kill (lwpid, signo);
591}
592
c64bd0ce
DJ
593/* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
594 exited. */
595
596static int
597wait_lwp (struct lwp_info *lp)
598{
599 pid_t pid;
600 int status;
601 int thread_dead = 0;
602
603 gdb_assert (!lp->stopped);
604 gdb_assert (lp->status == 0);
605
606 pid = waitpid (GET_LWP (lp->ptid), &status, 0);
607 if (pid == -1 && errno == ECHILD)
608 {
609 pid = waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
610 if (pid == -1 && errno == ECHILD)
611 {
612 /* The thread has previously exited. We need to delete it now
613 because in the case of NPTL threads, there won't be an
614 exit event unless it is the main thread. */
615 thread_dead = 1;
616 if (debug_lin_lwp)
617 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
618 target_pid_to_str (lp->ptid));
619 }
620 }
621
622 if (!thread_dead)
623 {
624 gdb_assert (pid == GET_LWP (lp->ptid));
625
626 if (debug_lin_lwp)
627 {
628 fprintf_unfiltered (gdb_stdlog,
629 "WL: waitpid %s received %s\n",
630 target_pid_to_str (lp->ptid),
631 status_to_str (status));
632 }
633 }
634
635 /* Check if the thread has exited. */
636 if (WIFEXITED (status) || WIFSIGNALED (status))
637 {
638 thread_dead = 1;
639 if (debug_lin_lwp)
640 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
641 target_pid_to_str (lp->ptid));
642 }
643
644 if (thread_dead)
645 {
646 if (in_thread_list (lp->ptid))
647 {
648 /* Core GDB cannot deal with us deleting the current thread. */
649 if (!ptid_equal (lp->ptid, inferior_ptid))
650 delete_thread (lp->ptid);
651 printf_unfiltered ("[%s exited]\n",
652 target_pid_to_str (lp->ptid));
653 }
654
655 delete_lwp (lp->ptid);
656 return 0;
657 }
658
659 gdb_assert (WIFSTOPPED (status));
660
661 return status;
662}
663
fb0e1ba7
MK
664/* Send a SIGSTOP to LP. */
665
666static int
667stop_callback (struct lwp_info *lp, void *data)
668{
6949171e 669 if (!lp->stopped && !lp->signalled)
fb0e1ba7
MK
670 {
671 int ret;
672
9fe7d6bf
MS
673 if (debug_lin_lwp)
674 {
675 fprintf_unfiltered (gdb_stdlog,
676 "SC: kill %s **<SIGSTOP>**\n",
677 target_pid_to_str (lp->ptid));
678 }
b757528f
JJ
679 errno = 0;
680 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
681 if (debug_lin_lwp)
682 {
683 fprintf_unfiltered (gdb_stdlog,
684 "SC: lwp kill %d %s\n",
685 ret,
686 errno ? safe_strerror (errno) : "ERRNO-OK");
687 }
fb0e1ba7
MK
688
689 lp->signalled = 1;
690 gdb_assert (lp->status == 0);
691 }
692
693 return 0;
694}
695
de4ca854
MK
696/* Wait until LP is stopped. If DATA is non-null it is interpreted as
697 a pointer to a set of signals to be flushed immediately. */
fb0e1ba7
MK
698
699static int
700stop_wait_callback (struct lwp_info *lp, void *data)
701{
de4ca854
MK
702 sigset_t *flush_mask = data;
703
bfb39158 704 if (!lp->stopped)
fb0e1ba7 705 {
fb0e1ba7
MK
706 int status;
707
c64bd0ce
DJ
708 status = wait_lwp (lp);
709 if (status == 0)
710 return 0;
fb0e1ba7 711
de4ca854
MK
712 /* Ignore any signals in FLUSH_MASK. */
713 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
714 {
bfb39158
DJ
715 if (!lp->signalled)
716 {
717 lp->stopped = 1;
718 return 0;
719 }
720
9fe7d6bf 721 errno = 0;
de4ca854 722 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
9fe7d6bf
MS
723 if (debug_lin_lwp)
724 fprintf_unfiltered (gdb_stdlog,
725 "PTRACE_CONT %s, 0, 0 (%s)\n",
726 target_pid_to_str (lp->ptid),
727 errno ? safe_strerror (errno) : "OK");
728
de4ca854
MK
729 return stop_wait_callback (lp, flush_mask);
730 }
731
fb0e1ba7
MK
732 if (WSTOPSIG (status) != SIGSTOP)
733 {
b1aeb4c5 734 if (WSTOPSIG (status) == SIGTRAP)
fb0e1ba7
MK
735 {
736 /* If a LWP other than the LWP that we're reporting an
6949171e
JJ
737 event for has hit a GDB breakpoint (as opposed to
738 some random trap signal), then just arrange for it to
739 hit it again later. We don't keep the SIGTRAP status
740 and don't forward the SIGTRAP signal to the LWP. We
741 will handle the current event, eventually we will
742 resume all LWPs, and this one will get its breakpoint
743 trap again.
744
745 If we do not do this, then we run the risk that the
746 user will delete or disable the breakpoint, but the
747 thread will have already tripped on it. */
7ca673cd 748
c4365b19 749 /* Now resume this LWP and get the SIGSTOP event. */
9fe7d6bf 750 errno = 0;
c4365b19 751 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
b1aeb4c5
MS
752 if (debug_lin_lwp)
753 {
6949171e 754 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf
MS
755 "PTRACE_CONT %s, 0, 0 (%s)\n",
756 target_pid_to_str (lp->ptid),
757 errno ? safe_strerror (errno) : "OK");
758
6949171e 759 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf
MS
760 "SWC: Candidate SIGTRAP event in %s\n",
761 target_pid_to_str (lp->ptid));
b1aeb4c5
MS
762 }
763 /* Hold the SIGTRAP for handling by lin_lwp_wait. */
764 stop_wait_callback (lp, data);
765 /* If there's another event, throw it back into the queue. */
766 if (lp->status)
9fe7d6bf 767 {
b757528f
JJ
768 if (debug_lin_lwp)
769 {
770 fprintf_unfiltered (gdb_stdlog,
771 "SWC: kill %s, %s\n",
772 target_pid_to_str (lp->ptid),
773 status_to_str ((int) status));
774 }
775 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
9fe7d6bf 776 }
b1aeb4c5
MS
777 /* Save the sigtrap event. */
778 lp->status = status;
779 return 0;
fb0e1ba7
MK
780 }
781 else
782 {
b1aeb4c5 783 /* The thread was stopped with a signal other than
6949171e 784 SIGSTOP, and didn't accidentally trip a breakpoint. */
b1aeb4c5 785
7ca673cd 786 if (debug_lin_lwp)
b1aeb4c5 787 {
6949171e 788 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf 789 "SWC: Pending event %s in %s\n",
6949171e 790 status_to_str ((int) status),
9fe7d6bf 791 target_pid_to_str (lp->ptid));
b1aeb4c5
MS
792 }
793 /* Now resume this LWP and get the SIGSTOP event. */
9fe7d6bf 794 errno = 0;
b1aeb4c5 795 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
9fe7d6bf 796 if (debug_lin_lwp)
6949171e 797 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf
MS
798 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
799 target_pid_to_str (lp->ptid),
800 errno ? safe_strerror (errno) : "OK");
7ca673cd 801
b1aeb4c5 802 /* Hold this event/waitstatus while we check to see if
6949171e 803 there are any more (we still want to get that SIGSTOP). */
b1aeb4c5
MS
804 stop_wait_callback (lp, data);
805 /* If the lp->status field is still empty, use it to hold
6949171e
JJ
806 this event. If not, then this event must be returned
807 to the event queue of the LWP. */
b1aeb4c5
MS
808 if (lp->status == 0)
809 lp->status = status;
810 else
9fe7d6bf
MS
811 {
812 if (debug_lin_lwp)
813 {
6949171e 814 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf 815 "SWC: kill %s, %s\n",
6949171e 816 target_pid_to_str (lp->ptid),
9fe7d6bf
MS
817 status_to_str ((int) status));
818 }
b757528f 819 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
9fe7d6bf 820 }
b1aeb4c5 821 return 0;
fb0e1ba7
MK
822 }
823 }
824 else
825 {
826 /* We caught the SIGSTOP that we intended to catch, so
6949171e 827 there's no SIGSTOP pending. */
b1aeb4c5 828 lp->stopped = 1;
fb0e1ba7
MK
829 lp->signalled = 0;
830 }
831 }
832
833 return 0;
834}
835
bfb39158
DJ
836/* Check whether PID has any pending signals in FLUSH_MASK. If so set
837 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
838
839static int
840lin_lwp_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask)
841{
842 sigset_t blocked, ignored;
843 int i;
844
845 linux_proc_pending_signals (pid, pending, &blocked, &ignored);
846
847 if (!flush_mask)
848 return 0;
849
850 for (i = 1; i < NSIG; i++)
851 if (sigismember (pending, i))
852 if (!sigismember (flush_mask, i)
853 || sigismember (&blocked, i)
854 || sigismember (&ignored, i))
855 sigdelset (pending, i);
856
857 if (sigisemptyset (pending))
858 return 0;
859
860 return 1;
861}
862
863/* DATA is interpreted as a mask of signals to flush. If LP has
864 signals pending, and they are all in the flush mask, then arrange
865 to flush them. LP should be stopped, as should all other threads
866 it might share a signal queue with. */
867
868static int
869flush_callback (struct lwp_info *lp, void *data)
870{
871 sigset_t *flush_mask = data;
872 sigset_t pending, intersection, blocked, ignored;
873 int pid, status;
874
875 /* Normally, when an LWP exits, it is removed from the LWP list. The
876 last LWP isn't removed till later, however. So if there is only
877 one LWP on the list, make sure it's alive. */
878 if (lwp_list == lp && lp->next == NULL)
879 if (!lin_lwp_thread_alive (lp->ptid))
880 return 0;
881
882 /* Just because the LWP is stopped doesn't mean that new signals
883 can't arrive from outside, so this function must be careful of
884 race conditions. However, because all threads are stopped, we
885 can assume that the pending mask will not shrink unless we resume
886 the LWP, and that it will then get another signal. We can't
887 control which one, however. */
888
889 if (lp->status)
890 {
891 if (debug_lin_lwp)
892 printf_unfiltered ("FC: LP has pending status %06x\n", lp->status);
893 if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status)))
894 lp->status = 0;
895 }
896
897 while (lin_lwp_has_pending (GET_LWP (lp->ptid), &pending, flush_mask))
898 {
899 int ret;
900
901 errno = 0;
902 ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
903 if (debug_lin_lwp)
904 fprintf_unfiltered (gdb_stderr,
905 "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno);
906
907 lp->stopped = 0;
908 stop_wait_callback (lp, flush_mask);
909 if (debug_lin_lwp)
910 fprintf_unfiltered (gdb_stderr,
911 "FC: Wait finished; saved status is %d\n",
912 lp->status);
913 }
914
915 return 0;
916}
917
fb0e1ba7
MK
918/* Return non-zero if LP has a wait status pending. */
919
920static int
921status_callback (struct lwp_info *lp, void *data)
922{
fce0e6e1
MK
923 /* Only report a pending wait status if we pretend that this has
924 indeed been resumed. */
925 return (lp->status != 0 && lp->resumed);
fb0e1ba7
MK
926}
927
928/* Return non-zero if LP isn't stopped. */
929
930static int
931running_callback (struct lwp_info *lp, void *data)
932{
933 return (lp->stopped == 0);
934}
935
00d4fce6 936/* Count the LWP's that have had events. */
b1aeb4c5
MS
937
938static int
939count_events_callback (struct lwp_info *lp, void *data)
940{
941 int *count = data;
942
00d4fce6
MK
943 gdb_assert (count != NULL);
944
945 /* Count only LWPs that have a SIGTRAP event pending. */
946 if (lp->status != 0
947 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
b1aeb4c5
MS
948 (*count)++;
949
950 return 0;
951}
952
00d4fce6 953/* Select the LWP (if any) that is currently being single-stepped. */
b1aeb4c5
MS
954
955static int
956select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
957{
958 if (lp->step && lp->status != 0)
959 return 1;
960 else
961 return 0;
962}
963
00d4fce6 964/* Select the Nth LWP that has had a SIGTRAP event. */
b1aeb4c5
MS
965
966static int
967select_event_lwp_callback (struct lwp_info *lp, void *data)
968{
969 int *selector = data;
970
00d4fce6
MK
971 gdb_assert (selector != NULL);
972
973 /* Select only LWPs that have a SIGTRAP event pending. */
974 if (lp->status != 0
975 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
b1aeb4c5
MS
976 if ((*selector)-- == 0)
977 return 1;
978
979 return 0;
980}
981
982static int
983cancel_breakpoints_callback (struct lwp_info *lp, void *data)
984{
985 struct lwp_info *event_lp = data;
986
00d4fce6
MK
987 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
988 if (lp == event_lp)
989 return 0;
990
991 /* If a LWP other than the LWP that we're reporting an event for has
992 hit a GDB breakpoint (as opposed to some random trap signal),
993 then just arrange for it to hit it again later. We don't keep
994 the SIGTRAP status and don't forward the SIGTRAP signal to the
995 LWP. We will handle the current event, eventually we will resume
996 all LWPs, and this one will get its breakpoint trap again.
997
998 If we do not do this, then we run the risk that the user will
999 delete or disable the breakpoint, but the LWP will have already
1000 tripped on it. */
1001
1002 if (lp->status != 0
6949171e
JJ
1003 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
1004 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
00d4fce6 1005 DECR_PC_AFTER_BREAK))
b1aeb4c5
MS
1006 {
1007 if (debug_lin_lwp)
00d4fce6 1008 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf
MS
1009 "CBC: Push back breakpoint for %s\n",
1010 target_pid_to_str (lp->ptid));
00d4fce6
MK
1011
1012 /* Back up the PC if necessary. */
b1aeb4c5 1013 if (DECR_PC_AFTER_BREAK)
00d4fce6
MK
1014 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
1015
1016 /* Throw away the SIGTRAP. */
b1aeb4c5
MS
1017 lp->status = 0;
1018 }
00d4fce6 1019
b1aeb4c5
MS
1020 return 0;
1021}
1022
00d4fce6 1023/* Select one LWP out of those that have events pending. */
b1aeb4c5
MS
1024
1025static void
1026select_event_lwp (struct lwp_info **orig_lp, int *status)
1027{
1028 int num_events = 0;
1029 int random_selector;
1030 struct lwp_info *event_lp;
1031
00d4fce6
MK
1032 /* Record the wait status for the origional LWP. */
1033 (*orig_lp)->status = *status;
1034
1035 /* Give preference to any LWP that is being single-stepped. */
b1aeb4c5
MS
1036 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
1037 if (event_lp != NULL)
1038 {
1039 if (debug_lin_lwp)
00d4fce6 1040 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf
MS
1041 "SEL: Select single-step %s\n",
1042 target_pid_to_str (event_lp->ptid));
b1aeb4c5
MS
1043 }
1044 else
1045 {
1046 /* No single-stepping LWP. Select one at random, out of those
6949171e 1047 which have had SIGTRAP events. */
b1aeb4c5 1048
00d4fce6
MK
1049 /* First see how many SIGTRAP events we have. */
1050 iterate_over_lwps (count_events_callback, &num_events);
b1aeb4c5 1051
00d4fce6
MK
1052 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1053 random_selector = (int)
b1aeb4c5
MS
1054 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
1055
00d4fce6 1056 if (debug_lin_lwp && num_events > 1)
6949171e
JJ
1057 fprintf_unfiltered (gdb_stdlog,
1058 "SEL: Found %d SIGTRAP events, selecting #%d\n",
00d4fce6 1059 num_events, random_selector);
b1aeb4c5 1060
00d4fce6
MK
1061 event_lp = iterate_over_lwps (select_event_lwp_callback,
1062 &random_selector);
b1aeb4c5
MS
1063 }
1064
1065 if (event_lp != NULL)
1066 {
00d4fce6 1067 /* Switch the event LWP. */
b1aeb4c5 1068 *orig_lp = event_lp;
6949171e 1069 *status = event_lp->status;
b1aeb4c5 1070 }
00d4fce6
MK
1071
1072 /* Flush the wait status for the event LWP. */
1073 (*orig_lp)->status = 0;
b1aeb4c5
MS
1074}
1075
fce0e6e1
MK
1076/* Return non-zero if LP has been resumed. */
1077
1078static int
1079resumed_callback (struct lwp_info *lp, void *data)
1080{
1081 return lp->resumed;
1082}
1083
cacab7c4
MK
1084#ifdef CHILD_WAIT
1085
1086/* We need to override child_wait to support attaching to cloned
1087 processes, since a normal wait (as done by the default version)
1088 ignores those processes. */
1089
1090/* Wait for child PTID to do something. Return id of the child,
1091 minus_one_ptid in case of error; store status into *OURSTATUS. */
1092
1093ptid_t
1094child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1095{
1096 int save_errno;
1097 int status;
1098 pid_t pid;
1099
1100 do
1101 {
1102 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1103 attached process. */
1104 set_sigio_trap ();
1105
1106 pid = waitpid (GET_PID (ptid), &status, 0);
1107 if (pid == -1 && errno == ECHILD)
1108 /* Try again with __WCLONE to check cloned processes. */
1109 pid = waitpid (GET_PID (ptid), &status, __WCLONE);
9fe7d6bf
MS
1110
1111 if (debug_lin_lwp)
1112 {
6949171e 1113 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf 1114 "CW: waitpid %ld received %s\n",
6949171e 1115 (long) pid, status_to_str (status));
9fe7d6bf
MS
1116 }
1117
cacab7c4
MK
1118 save_errno = errno;
1119
b3ba1b44 1120 /* Make sure we don't report an event for the exit of the
6949171e
JJ
1121 original program, if we've detached from it. */
1122 if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid))
b3ba1b44
DJ
1123 {
1124 pid = -1;
1125 save_errno = EINTR;
1126 }
1127
ae087d01
DJ
1128 /* Check for stop events reported by a process we didn't already
1129 know about - in this case, anything other than inferior_ptid.
1130
1131 If we're expecting to receive stopped processes after fork,
1132 vfork, and clone events, then we'll just add the new one to
1133 our list and go back to waiting for the event to be reported
1134 - the stopped process might be returned from waitpid before
1135 or after the event is. If we want to handle debugging of
1136 CLONE_PTRACE processes we need to do more here, i.e. switch
1137 to multi-threaded mode. */
1138 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP
1139 && pid != GET_PID (inferior_ptid))
1140 {
c538c11c 1141 linux_record_stopped_pid (pid);
ae087d01
DJ
1142 pid = -1;
1143 save_errno = EINTR;
1144 }
1145
cacab7c4
MK
1146 clear_sigio_trap ();
1147 clear_sigint_trap ();
1148 }
2d1bfe2e 1149 while (pid == -1 && save_errno == EINTR);
cacab7c4
MK
1150
1151 if (pid == -1)
1152 {
6949171e 1153 warning ("Child process unexpectedly missing: %s",
9fe7d6bf 1154 safe_strerror (errno));
cacab7c4
MK
1155
1156 /* Claim it exited with unknown signal. */
1157 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1158 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1159 return minus_one_ptid;
1160 }
1161
4de4c07c
DJ
1162 /* Handle GNU/Linux's extended waitstatus for trace events. */
1163 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1164 return linux_handle_extended_wait (pid, status, ourstatus);
1165
cacab7c4
MK
1166 store_waitstatus (ourstatus, status);
1167 return pid_to_ptid (pid);
1168}
1169
1170#endif
1171
b757528f
JJ
1172/* Stop an active thread, verify it still exists, then resume it. */
1173
1174static int
1175stop_and_resume_callback (struct lwp_info *lp, void *data)
1176{
1177 struct lwp_info *ptr;
1178
1179 if (!lp->stopped && !lp->signalled)
1180 {
1181 stop_callback (lp, NULL);
1182 stop_wait_callback (lp, NULL);
1183 /* Resume if the lwp still exists. */
1184 for (ptr = lwp_list; ptr; ptr = ptr->next)
1185 if (lp == ptr)
1186 resume_callback (lp, NULL);
1187 }
1188 return 0;
1189}
1190
39f77062
KB
1191static ptid_t
1192lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
fb0e1ba7
MK
1193{
1194 struct lwp_info *lp = NULL;
1195 int options = 0;
1196 int status = 0;
39f77062 1197 pid_t pid = PIDGET (ptid);
de4ca854
MK
1198 sigset_t flush_mask;
1199
1200 sigemptyset (&flush_mask);
fb0e1ba7 1201
3f07c44b 1202 /* Make sure SIGCHLD is blocked. */
6949171e 1203 if (!sigismember (&blocked_mask, SIGCHLD))
3f07c44b
MK
1204 {
1205 sigaddset (&blocked_mask, SIGCHLD);
1206 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1207 }
1208
6949171e 1209retry:
fb0e1ba7 1210
9a973a8f
MK
1211 /* Make sure there is at least one LWP that has been resumed, at
1212 least if there are any LWPs at all. */
1213 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
fce0e6e1 1214
fb0e1ba7
MK
1215 /* First check if there is a LWP with a wait status pending. */
1216 if (pid == -1)
1217 {
b1aeb4c5 1218 /* Any LWP that's been resumed will do. */
fb0e1ba7
MK
1219 lp = iterate_over_lwps (status_callback, NULL);
1220 if (lp)
1221 {
fb0e1ba7
MK
1222 status = lp->status;
1223 lp->status = 0;
b1aeb4c5
MS
1224
1225 if (debug_lin_lwp && status)
58eeadba 1226 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf 1227 "LLW: Using pending wait status %s for %s.\n",
6949171e 1228 status_to_str (status),
9fe7d6bf 1229 target_pid_to_str (lp->ptid));
fb0e1ba7
MK
1230 }
1231
1232 /* But if we don't fine one, we'll have to wait, and check both
1233 cloned and uncloned processes. We start with the cloned
1234 processes. */
1235 options = __WCLONE | WNOHANG;
1236 }
39f77062 1237 else if (is_lwp (ptid))
fb0e1ba7 1238 {
7ca673cd 1239 if (debug_lin_lwp)
6949171e 1240 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf
MS
1241 "LLW: Waiting for specific LWP %s.\n",
1242 target_pid_to_str (ptid));
7ca673cd 1243
fb0e1ba7 1244 /* We have a specific LWP to check. */
39f77062 1245 lp = find_lwp_pid (ptid);
fb0e1ba7
MK
1246 gdb_assert (lp);
1247 status = lp->status;
1248 lp->status = 0;
7ca673cd 1249
b1aeb4c5 1250 if (debug_lin_lwp && status)
58eeadba 1251 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf 1252 "LLW: Using pending wait status %s for %s.\n",
6949171e 1253 status_to_str (status),
9fe7d6bf 1254 target_pid_to_str (lp->ptid));
fb0e1ba7
MK
1255
1256 /* If we have to wait, take into account whether PID is a cloned
1257 process or not. And we have to convert it to something that
1258 the layer beneath us can understand. */
cacab7c4 1259 options = lp->cloned ? __WCLONE : 0;
39f77062 1260 pid = GET_LWP (ptid);
fb0e1ba7
MK
1261 }
1262
1263 if (status && lp->signalled)
1264 {
1265 /* A pending SIGSTOP may interfere with the normal stream of
6949171e
JJ
1266 events. In a typical case where interference is a problem,
1267 we have a SIGSTOP signal pending for LWP A while
1268 single-stepping it, encounter an event in LWP B, and take the
1269 pending SIGSTOP while trying to stop LWP A. After processing
1270 the event in LWP B, LWP A is continued, and we'll never see
1271 the SIGTRAP associated with the last time we were
1272 single-stepping LWP A. */
fb0e1ba7
MK
1273
1274 /* Resume the thread. It should halt immediately returning the
6949171e 1275 pending SIGSTOP. */
9fe7d6bf 1276 registers_changed ();
39f77062 1277 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
6949171e 1278 TARGET_SIGNAL_0);
9fe7d6bf
MS
1279 if (debug_lin_lwp)
1280 fprintf_unfiltered (gdb_stdlog,
1281 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1282 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1283 target_pid_to_str (lp->ptid));
fb0e1ba7 1284 lp->stopped = 0;
fce0e6e1 1285 gdb_assert (lp->resumed);
fb0e1ba7
MK
1286
1287 /* This should catch the pending SIGSTOP. */
1288 stop_wait_callback (lp, NULL);
1289 }
1290
6949171e
JJ
1291 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1292 attached process. */
fb0e1ba7
MK
1293 set_sigio_trap ();
1294
1295 while (status == 0)
1296 {
1297 pid_t lwpid;
1298
1299 lwpid = waitpid (pid, &status, options);
1300 if (lwpid > 0)
1301 {
1302 gdb_assert (pid == -1 || lwpid == pid);
1303
9fe7d6bf
MS
1304 if (debug_lin_lwp)
1305 {
1306 fprintf_unfiltered (gdb_stdlog,
1307 "LLW: waitpid %ld received %s\n",
6949171e 1308 (long) lwpid, status_to_str (status));
9fe7d6bf
MS
1309 }
1310
39f77062 1311 lp = find_lwp_pid (pid_to_ptid (lwpid));
b3ba1b44 1312
ae087d01
DJ
1313 /* Check for stop events reported by a process we didn't
1314 already know about - anything not already in our LWP
1315 list.
1316
1317 If we're expecting to receive stopped processes after
1318 fork, vfork, and clone events, then we'll just add the
1319 new one to our list and go back to waiting for the event
1320 to be reported - the stopped process might be returned
1321 from waitpid before or after the event is. */
1322 if (WIFSTOPPED (status) && !lp)
1323 {
1324 linux_record_stopped_pid (lwpid);
1325 status = 0;
1326 continue;
1327 }
1328
b3ba1b44
DJ
1329 /* Make sure we don't report an event for the exit of an LWP not in
1330 our list, i.e. not part of the current process. This can happen
1331 if we detach from a program we original forked and then it
1332 exits. */
6949171e 1333 if (!WIFSTOPPED (status) && !lp)
b3ba1b44
DJ
1334 {
1335 status = 0;
1336 continue;
1337 }
1338
ae087d01
DJ
1339 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1340 CLONE_PTRACE processes which do not use the thread library -
1341 otherwise we wouldn't find the new LWP this way. That doesn't
1342 currently work, and the following code is currently unreachable
1343 due to the two blocks above. If it's fixed some day, this code
1344 should be broken out into a function so that we can also pick up
1345 LWPs from the new interface. */
6949171e 1346 if (!lp)
fb0e1ba7 1347 {
39f77062 1348 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
cacab7c4
MK
1349 if (options & __WCLONE)
1350 lp->cloned = 1;
1351
fb0e1ba7
MK
1352 if (threaded)
1353 {
3f07c44b
MK
1354 gdb_assert (WIFSTOPPED (status)
1355 && WSTOPSIG (status) == SIGSTOP);
fb0e1ba7
MK
1356 lp->signalled = 1;
1357
6949171e 1358 if (!in_thread_list (inferior_ptid))
fb0e1ba7 1359 {
39f77062 1360 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
6949171e 1361 GET_PID (inferior_ptid));
39f77062 1362 add_thread (inferior_ptid);
fb0e1ba7
MK
1363 }
1364
39f77062 1365 add_thread (lp->ptid);
fb0e1ba7 1366 printf_unfiltered ("[New %s]\n",
39f77062 1367 target_pid_to_str (lp->ptid));
fb0e1ba7
MK
1368 }
1369 }
1370
b757528f 1371 /* Check if the thread has exited. */
fb0e1ba7 1372 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
b757528f
JJ
1373 {
1374 if (in_thread_list (lp->ptid))
1375 {
1376 /* Core GDB cannot deal with us deleting the current
1377 thread. */
1378 if (!ptid_equal (lp->ptid, inferior_ptid))
1379 delete_thread (lp->ptid);
1380 printf_unfiltered ("[%s exited]\n",
1381 target_pid_to_str (lp->ptid));
1382 }
1383
1384 /* If this is the main thread, we must stop all threads and
1385 verify if they are still alive. This is because in the nptl
1386 thread model, there is no signal issued for exiting LWPs
1387 other than the main thread. We only get the main thread
1388 exit signal once all child threads have already exited.
1389 If we stop all the threads and use the stop_wait_callback
1390 to check if they have exited we can determine whether this
1391 signal should be ignored or whether it means the end of the
1392 debugged application, regardless of which threading model
1393 is being used. */
1394 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
1395 {
1396 lp->stopped = 1;
1397 iterate_over_lwps (stop_and_resume_callback, NULL);
1398 }
1399
1400 if (debug_lin_lwp)
1401 fprintf_unfiltered (gdb_stdlog,
1402 "LLW: %s exited.\n",
1403 target_pid_to_str (lp->ptid));
1404
1405 delete_lwp (lp->ptid);
1406
1407 /* If there is at least one more LWP, then the exit signal
1408 was not the end of the debugged application and should be
1409 ignored. */
1410 if (num_lwps > 0)
1411 {
1412 /* Make sure there is at least one thread running. */
1413 gdb_assert (iterate_over_lwps (running_callback, NULL));
1414
1415 /* Discard the event. */
1416 status = 0;
1417 continue;
1418 }
1419 }
1420
1421 /* Check if the current LWP has previously exited. In the nptl
1422 thread model, LWPs other than the main thread do not issue
1423 signals when they exit so we must check whenever the thread
1424 has stopped. A similar check is made in stop_wait_callback(). */
1425 if (num_lwps > 1 && !lin_lwp_thread_alive (lp->ptid))
fb0e1ba7 1426 {
39f77062 1427 if (in_thread_list (lp->ptid))
fb0e1ba7 1428 {
e6328671 1429 /* Core GDB cannot deal with us deleting the current
6949171e
JJ
1430 thread. */
1431 if (!ptid_equal (lp->ptid, inferior_ptid))
39f77062 1432 delete_thread (lp->ptid);
fb0e1ba7 1433 printf_unfiltered ("[%s exited]\n",
39f77062 1434 target_pid_to_str (lp->ptid));
fb0e1ba7 1435 }
7ca673cd 1436 if (debug_lin_lwp)
6949171e
JJ
1437 fprintf_unfiltered (gdb_stdlog,
1438 "LLW: %s exited.\n",
39f77062 1439 target_pid_to_str (lp->ptid));
7ca673cd 1440
39f77062 1441 delete_lwp (lp->ptid);
fb0e1ba7
MK
1442
1443 /* Make sure there is at least one thread running. */
1444 gdb_assert (iterate_over_lwps (running_callback, NULL));
1445
1446 /* Discard the event. */
1447 status = 0;
1448 continue;
1449 }
1450
1451 /* Make sure we don't report a SIGSTOP that we sent
6949171e 1452 ourselves in an attempt to stop an LWP. */
9fe7d6bf 1453 if (lp->signalled
6949171e 1454 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
fb0e1ba7 1455 {
7ca673cd 1456 if (debug_lin_lwp)
6949171e 1457 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf 1458 "LLW: Delayed SIGSTOP caught for %s.\n",
39f77062 1459 target_pid_to_str (lp->ptid));
7ca673cd 1460
fb0e1ba7
MK
1461 /* This is a delayed SIGSTOP. */
1462 lp->signalled = 0;
1463
9fe7d6bf 1464 registers_changed ();
39f77062 1465 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
6949171e 1466 TARGET_SIGNAL_0);
9fe7d6bf
MS
1467 if (debug_lin_lwp)
1468 fprintf_unfiltered (gdb_stdlog,
1469 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
6949171e 1470 lp->step ?
9fe7d6bf
MS
1471 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1472 target_pid_to_str (lp->ptid));
1473
fb0e1ba7 1474 lp->stopped = 0;
fce0e6e1 1475 gdb_assert (lp->resumed);
fb0e1ba7
MK
1476
1477 /* Discard the event. */
1478 status = 0;
1479 continue;
1480 }
1481
1482 break;
1483 }
1484
1485 if (pid == -1)
1486 {
1487 /* Alternate between checking cloned and uncloned processes. */
1488 options ^= __WCLONE;
1489
1490 /* And suspend every time we have checked both. */
1491 if (options & __WCLONE)
1492 sigsuspend (&suspend_mask);
1493 }
1494
1495 /* We shouldn't end up here unless we want to try again. */
1496 gdb_assert (status == 0);
1497 }
1498
1499 clear_sigio_trap ();
1500 clear_sigint_trap ();
1501
1502 gdb_assert (lp);
1503
1504 /* Don't report signals that GDB isn't interested in, such as
1505 signals that are neither printed nor stopped upon. Stopping all
1506 threads can be a bit time-consuming so if we want decent
1507 performance with heavily multi-threaded programs, especially when
1508 they're using a high frequency timer, we'd better avoid it if we
1509 can. */
1510
1511 if (WIFSTOPPED (status))
1512 {
1513 int signo = target_signal_from_host (WSTOPSIG (status));
1514
1515 if (signal_stop_state (signo) == 0
1516 && signal_print_state (signo) == 0
1517 && signal_pass_state (signo) == 1)
1518 {
fce0e6e1 1519 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
6949171e
JJ
1520 here? It is not clear we should. GDB may not expect
1521 other threads to run. On the other hand, not resuming
1522 newly attached threads may cause an unwanted delay in
1523 getting them running. */
9fe7d6bf 1524 registers_changed ();
c4365b19 1525 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
9fe7d6bf
MS
1526 if (debug_lin_lwp)
1527 fprintf_unfiltered (gdb_stdlog,
1528 "LLW: %s %s, %s (preempt 'handle')\n",
6949171e
JJ
1529 lp->step ?
1530 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
9fe7d6bf
MS
1531 target_pid_to_str (lp->ptid),
1532 signo ? strsignal (signo) : "0");
fce0e6e1 1533 lp->stopped = 0;
fb0e1ba7
MK
1534 status = 0;
1535 goto retry;
1536 }
de4ca854 1537
6949171e 1538 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
de4ca854
MK
1539 {
1540 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
6949171e
JJ
1541 forwarded to the entire process group, that is, all LWP's
1542 will receive it. Since we only want to report it once,
1543 we try to flush it from all LWPs except this one. */
de4ca854
MK
1544 sigaddset (&flush_mask, SIGINT);
1545 }
fb0e1ba7
MK
1546 }
1547
1548 /* This LWP is stopped now. */
1549 lp->stopped = 1;
1550
b1aeb4c5 1551 if (debug_lin_lwp)
9fe7d6bf 1552 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
6949171e 1553 status_to_str (status), target_pid_to_str (lp->ptid));
b1aeb4c5 1554
fb0e1ba7
MK
1555 /* Now stop all other LWP's ... */
1556 iterate_over_lwps (stop_callback, NULL);
1557
1558 /* ... and wait until all of them have reported back that they're no
1559 longer running. */
de4ca854 1560 iterate_over_lwps (stop_wait_callback, &flush_mask);
bfb39158 1561 iterate_over_lwps (flush_callback, &flush_mask);
fb0e1ba7 1562
00d4fce6
MK
1563 /* If we're not waiting for a specific LWP, choose an event LWP from
1564 among those that have had events. Giving equal priority to all
1565 LWPs that have had events helps prevent starvation. */
1566 if (pid == -1)
1567 select_event_lwp (&lp, &status);
b1aeb4c5 1568
00d4fce6
MK
1569 /* Now that we've selected our final event LWP, cancel any
1570 breakpoints in other LWPs that have hit a GDB breakpoint. See
1571 the comment in cancel_breakpoints_callback to find out why. */
1572 iterate_over_lwps (cancel_breakpoints_callback, lp);
b1aeb4c5 1573
fb0e1ba7
MK
1574 /* If we're not running in "threaded" mode, we'll report the bare
1575 process id. */
1576
1577 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
b1aeb4c5
MS
1578 {
1579 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1580 if (debug_lin_lwp)
6949171e 1581 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf
MS
1582 "LLW: trap_ptid is %s.\n",
1583 target_pid_to_str (trap_ptid));
b1aeb4c5 1584 }
fb0e1ba7 1585 else
39f77062 1586 trap_ptid = null_ptid;
fb0e1ba7 1587
4de4c07c
DJ
1588 /* Handle GNU/Linux's extended waitstatus for trace events. */
1589 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1590 {
1591 linux_handle_extended_wait (ptid_get_pid (trap_ptid),
1592 status, ourstatus);
1593 return trap_ptid;
1594 }
1595
fb0e1ba7 1596 store_waitstatus (ourstatus, status);
39f77062 1597 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
fb0e1ba7
MK
1598}
1599
1600static int
1601kill_callback (struct lwp_info *lp, void *data)
1602{
9fe7d6bf 1603 errno = 0;
39f77062 1604 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
9fe7d6bf 1605 if (debug_lin_lwp)
6949171e 1606 fprintf_unfiltered (gdb_stdlog,
9fe7d6bf
MS
1607 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
1608 target_pid_to_str (lp->ptid),
1609 errno ? safe_strerror (errno) : "OK");
1610
fb0e1ba7
MK
1611 return 0;
1612}
1613
1614static int
1615kill_wait_callback (struct lwp_info *lp, void *data)
1616{
1617 pid_t pid;
1618
1619 /* We must make sure that there are no pending events (delayed
1620 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
1621 program doesn't interfere with any following debugging session. */
1622
1623 /* For cloned processes we must check both with __WCLONE and
1624 without, since the exit status of a cloned process isn't reported
1625 with __WCLONE. */
cacab7c4 1626 if (lp->cloned)
fb0e1ba7
MK
1627 {
1628 do
1629 {
39f77062 1630 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
9fe7d6bf
MS
1631 if (pid != (pid_t) -1 && debug_lin_lwp)
1632 {
1633 fprintf_unfiltered (gdb_stdlog,
1634 "KWC: wait %s received unknown.\n",
1635 target_pid_to_str (lp->ptid));
1636 }
fb0e1ba7 1637 }
39f77062 1638 while (pid == GET_LWP (lp->ptid));
fb0e1ba7
MK
1639
1640 gdb_assert (pid == -1 && errno == ECHILD);
1641 }
1642
1643 do
1644 {
39f77062 1645 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
9fe7d6bf
MS
1646 if (pid != (pid_t) -1 && debug_lin_lwp)
1647 {
1648 fprintf_unfiltered (gdb_stdlog,
6949171e 1649 "KWC: wait %s received unk.\n",
9fe7d6bf
MS
1650 target_pid_to_str (lp->ptid));
1651 }
fb0e1ba7 1652 }
39f77062 1653 while (pid == GET_LWP (lp->ptid));
fb0e1ba7
MK
1654
1655 gdb_assert (pid == -1 && errno == ECHILD);
1656 return 0;
1657}
1658
1659static void
1660lin_lwp_kill (void)
1661{
1662 /* Kill all LWP's ... */
1663 iterate_over_lwps (kill_callback, NULL);
1664
1665 /* ... and wait until we've flushed all events. */
1666 iterate_over_lwps (kill_wait_callback, NULL);
1667
1668 target_mourn_inferior ();
1669}
1670
1671static void
1672lin_lwp_create_inferior (char *exec_file, char *allargs, char **env)
1673{
c194fbe1 1674 child_ops.to_create_inferior (exec_file, allargs, env);
fb0e1ba7
MK
1675}
1676
6949171e 1677static void
fb0e1ba7
MK
1678lin_lwp_mourn_inferior (void)
1679{
c194fbe1 1680 trap_ptid = null_ptid;
fb0e1ba7 1681
c194fbe1 1682 /* Destroy LWP info; it's no longer valid. */
fb0e1ba7
MK
1683 init_lwp_list ();
1684
4c8de859 1685 /* Restore the original signal mask. */
3f07c44b
MK
1686 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1687 sigemptyset (&blocked_mask);
1688
c194fbe1 1689 child_ops.to_mourn_inferior ();
fb0e1ba7
MK
1690}
1691
fb0e1ba7
MK
1692static int
1693lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
6949171e 1694 struct mem_attrib *attrib, struct target_ops *target)
fb0e1ba7 1695{
39f77062 1696 struct cleanup *old_chain = save_inferior_ptid ();
fb0e1ba7
MK
1697 int xfer;
1698
39f77062
KB
1699 if (is_lwp (inferior_ptid))
1700 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
fb0e1ba7 1701
eb784848
DJ
1702 xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1703 if (xfer == 0)
1704 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
fb0e1ba7
MK
1705
1706 do_cleanups (old_chain);
1707 return xfer;
1708}
1709
1710static int
39f77062 1711lin_lwp_thread_alive (ptid_t ptid)
fb0e1ba7 1712{
39f77062 1713 gdb_assert (is_lwp (ptid));
fb0e1ba7
MK
1714
1715 errno = 0;
39f77062 1716 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
9fe7d6bf
MS
1717 if (debug_lin_lwp)
1718 fprintf_unfiltered (gdb_stdlog,
1719 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
6949171e 1720 target_pid_to_str (ptid),
9fe7d6bf 1721 errno ? safe_strerror (errno) : "OK");
fb0e1ba7
MK
1722 if (errno)
1723 return 0;
1724
1725 return 1;
1726}
1727
1728static char *
39f77062 1729lin_lwp_pid_to_str (ptid_t ptid)
fb0e1ba7
MK
1730{
1731 static char buf[64];
1732
39f77062 1733 if (is_lwp (ptid))
fb0e1ba7 1734 {
b08cfdb6 1735 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
fb0e1ba7
MK
1736 return buf;
1737 }
1738
39f77062 1739 return normal_pid_to_str (ptid);
fb0e1ba7
MK
1740}
1741
1742static void
1743init_lin_lwp_ops (void)
1744{
1745#if 0
1746 lin_lwp_ops.to_open = lin_lwp_open;
1747#endif
1748 lin_lwp_ops.to_shortname = "lwp-layer";
1749 lin_lwp_ops.to_longname = "lwp-layer";
1750 lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
1751 lin_lwp_ops.to_attach = lin_lwp_attach;
1752 lin_lwp_ops.to_detach = lin_lwp_detach;
1753 lin_lwp_ops.to_resume = lin_lwp_resume;
1754 lin_lwp_ops.to_wait = lin_lwp_wait;
02ae7771
AC
1755 /* fetch_inferior_registers and store_inferior_registers will
1756 honor the LWP id, so we can use them directly. */
1757 lin_lwp_ops.to_fetch_registers = fetch_inferior_registers;
1758 lin_lwp_ops.to_store_registers = store_inferior_registers;
fb0e1ba7
MK
1759 lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
1760 lin_lwp_ops.to_kill = lin_lwp_kill;
1761 lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
1762 lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
1763 lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
1764 lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
4de4c07c
DJ
1765 lin_lwp_ops.to_post_startup_inferior = child_post_startup_inferior;
1766 lin_lwp_ops.to_post_attach = child_post_attach;
1767 lin_lwp_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
1768 lin_lwp_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
1769 lin_lwp_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
1770
fb0e1ba7
MK
1771 lin_lwp_ops.to_stratum = thread_stratum;
1772 lin_lwp_ops.to_has_thread_control = tc_schedlock;
1773 lin_lwp_ops.to_magic = OPS_MAGIC;
1774}
1775
1776static void
1777sigchld_handler (int signo)
1778{
1779 /* Do nothing. The only reason for this handler is that it allows
1780 us to use sigsuspend in lin_lwp_wait above to wait for the
1781 arrival of a SIGCHLD. */
1782}
1783
1784void
1785_initialize_lin_lwp (void)
1786{
1787 struct sigaction action;
fb0e1ba7
MK
1788
1789 extern void thread_db_init (struct target_ops *);
1790
1791 init_lin_lwp_ops ();
1792 add_target (&lin_lwp_ops);
1793 thread_db_init (&lin_lwp_ops);
1794
4c8de859 1795 /* Save the original signal mask. */
3f07c44b
MK
1796 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
1797
fb0e1ba7
MK
1798 action.sa_handler = sigchld_handler;
1799 sigemptyset (&action.sa_mask);
1800 action.sa_flags = 0;
1801 sigaction (SIGCHLD, &action, NULL);
1802
3f07c44b
MK
1803 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1804 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
fb0e1ba7 1805 sigdelset (&suspend_mask, SIGCHLD);
3f07c44b
MK
1806
1807 sigemptyset (&blocked_mask);
7ca673cd
MS
1808
1809 add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
6949171e 1810 (char *) &debug_lin_lwp,
8605d56e 1811 "Set debugging of GNU/Linux lwp module.\n\
6949171e 1812Enables printf debugging output.\n", &setdebuglist), &showdebuglist);
fb0e1ba7
MK
1813}
1814\f
1815
1816/* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
8605d56e
AC
1817 the GNU/Linux Threads library and therefore doesn't really belong
1818 here. */
fb0e1ba7
MK
1819
1820/* Read variable NAME in the target and return its value if found.
1821 Otherwise return zero. It is assumed that the type of the variable
1822 is `int'. */
1823
1824static int
1825get_signo (const char *name)
1826{
1827 struct minimal_symbol *ms;
1828 int signo;
1829
1830 ms = lookup_minimal_symbol (name, NULL, NULL);
1831 if (ms == NULL)
1832 return 0;
1833
1834 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
1835 sizeof (signo)) != 0)
1836 return 0;
1837
1838 return signo;
1839}
1840
1841/* Return the set of signals used by the threads library in *SET. */
1842
1843void
1844lin_thread_get_thread_signals (sigset_t *set)
1845{
3f07c44b
MK
1846 struct sigaction action;
1847 int restart, cancel;
fb0e1ba7
MK
1848
1849 sigemptyset (set);
1850
1851 restart = get_signo ("__pthread_sig_restart");
1852 if (restart == 0)
1853 return;
1854
1855 cancel = get_signo ("__pthread_sig_cancel");
1856 if (cancel == 0)
1857 return;
1858
1859 sigaddset (set, restart);
1860 sigaddset (set, cancel);
3f07c44b 1861
8605d56e
AC
1862 /* The GNU/Linux Threads library makes terminating threads send a
1863 special "cancel" signal instead of SIGCHLD. Make sure we catch
1864 those (to prevent them from terminating GDB itself, which is
1865 likely to be their default action) and treat them the same way as
1866 SIGCHLD. */
3f07c44b
MK
1867
1868 action.sa_handler = sigchld_handler;
1869 sigemptyset (&action.sa_mask);
1870 action.sa_flags = 0;
1871 sigaction (cancel, &action, NULL);
1872
1873 /* We block the "cancel" signal throughout this code ... */
1874 sigaddset (&blocked_mask, cancel);
1875 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1876
1877 /* ... except during a sigsuspend. */
1878 sigdelset (&suspend_mask, cancel);
fb0e1ba7 1879}
This page took 0.354368 seconds and 4 git commands to generate.