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