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