* somsolib.c (dld_cache): Replace boolean by int for field is_valid.
[deliverable/binutils-gdb.git] / gdb / lin-lwp.c
CommitLineData
8605d56e 1/* Multi-threaded debugging support for GNU/Linux (LWP layer).
4e052eda 2 Copyright 2000, 2001 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>
27#include <sys/ptrace.h>
28#include "gdb_wait.h"
29
30#include "gdbthread.h"
31#include "inferior.h"
32#include "target.h"
4e052eda 33#include "regcache.h"
7ca673cd 34#include "gdbcmd.h"
fb0e1ba7 35
7ca673cd 36static int debug_lin_lwp;
fb0e1ba7 37extern const char *strsignal (int sig);
fb0e1ba7 38
8605d56e
AC
39/* On GNU/Linux there are no real LWP's. The closest thing to LWP's
40 are processes sharing the same VM space. A multi-threaded process
41 is basically a group of such processes. However, such a grouping
42 is almost entirely a user-space issue; the kernel doesn't enforce
43 such a grouping at all (this might change in the future). In
44 general, we'll rely on the threads library (i.e. the GNU/Linux
45 Threads library) to provide such a grouping.
fb0e1ba7
MK
46
47 It is perfectly well possible to write a multi-threaded application
48 without the assistance of a threads library, by using the clone
49 system call directly. This module should be able to give some
50 rudimentary support for debugging such applications if developers
51 specify the CLONE_PTRACE flag in the clone system call, and are
8605d56e 52 using the Linux kernel 2.4 or above.
fb0e1ba7 53
8605d56e
AC
54 Note that there are some peculiarities in GNU/Linux that affect
55 this code:
fb0e1ba7
MK
56
57 - In general one should specify the __WCLONE flag to waitpid in
3f07c44b
MK
58 order to make it report events for any of the cloned processes
59 (and leave it out for the initial process). However, if a cloned
60 process has exited the exit status is only reported if the
8605d56e
AC
61 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
62 we cannot use it since GDB must work on older systems too.
fb0e1ba7
MK
63
64 - When a traced, cloned process exits and is waited for by the
4c8de859 65 debugger, the kernel reassigns it to the original parent and
8605d56e
AC
66 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
67 library doesn't notice this, which leads to the "zombie problem":
68 When debugged a multi-threaded process that spawns a lot of
69 threads will run out of processes, even if the threads exit,
70 because the "zombies" stay around. */
fb0e1ba7
MK
71
72/* Structure describing a LWP. */
73struct lwp_info
74{
75 /* The process id of the LWP. This is a combination of the LWP id
76 and overall process id. */
39f77062 77 ptid_t ptid;
fb0e1ba7 78
cacab7c4
MK
79 /* Non-zero if this LWP is cloned. In this context "cloned" means
80 that the LWP is reporting to its parent using a signal other than
81 SIGCHLD. */
82 int cloned;
83
fb0e1ba7
MK
84 /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report
85 it back yet). */
86 int signalled;
87
88 /* Non-zero if this LWP is stopped. */
89 int stopped;
90
fce0e6e1
MK
91 /* Non-zero if this LWP will be/has been resumed. Note that an LWP
92 can be marked both as stopped and resumed at the same time. This
93 happens if we try to resume an LWP that has a wait status
94 pending. We shouldn't let the LWP run until that wait status has
95 been processed, but we should not report that wait status if GDB
96 didn't try to let the LWP run. */
97 int resumed;
98
fb0e1ba7
MK
99 /* If non-zero, a pending wait status. */
100 int status;
101
102 /* Non-zero if we were stepping this LWP. */
103 int step;
104
105 /* Next LWP in list. */
106 struct lwp_info *next;
107};
108
109/* List of known LWPs. */
110static struct lwp_info *lwp_list;
111
112/* Number of LWPs in the list. */
113static int num_lwps;
114
115/* Non-zero if we're running in "threaded" mode. */
116static int threaded;
117\f
118
ca6724c1
KB
119#define GET_LWP(ptid) ptid_get_lwp (ptid)
120#define GET_PID(ptid) ptid_get_pid (ptid)
121#define is_lwp(ptid) (GET_LWP (ptid) != 0)
122#define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
fb0e1ba7 123
fb0e1ba7
MK
124/* If the last reported event was a SIGTRAP, this variable is set to
125 the process id of the LWP/thread that got it. */
39f77062 126ptid_t trap_ptid;
fb0e1ba7
MK
127\f
128
129/* This module's target-specific operations. */
130static struct target_ops lin_lwp_ops;
131
132/* The standard child operations. */
133extern struct target_ops child_ops;
134
3f07c44b
MK
135/* Since we cannot wait (in lin_lwp_wait) for the initial process and
136 any cloned processes with a single call to waitpid, we have to use
4c8de859 137 the WNOHANG flag and call waitpid in a loop. To optimize
3f07c44b
MK
138 things a bit we use `sigsuspend' to wake us up when a process has
139 something to report (it will send us a SIGCHLD if it has). To make
140 this work we have to juggle with the signal mask. We save the
4c8de859 141 original signal mask such that we can restore it before creating a
3f07c44b
MK
142 new process in order to avoid blocking certain signals in the
143 inferior. We then block SIGCHLD during the waitpid/sigsuspend
144 loop. */
145
4c8de859 146/* Original signal mask. */
3f07c44b
MK
147static sigset_t normal_mask;
148
fb0e1ba7
MK
149/* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
150 _initialize_lin_lwp. */
151static sigset_t suspend_mask;
3f07c44b
MK
152
153/* Signals to block to make that sigsuspend work. */
154static sigset_t blocked_mask;
fb0e1ba7
MK
155\f
156
157/* Prototypes for local functions. */
c194fbe1 158static int stop_wait_callback (struct lwp_info *lp, void *data);
fb0e1ba7 159\f
58eeadba
MK
160/* Convert wait status STATUS to a string. Used for printing debug
161 messages only. */
fb0e1ba7 162
58eeadba
MK
163static char *
164status_to_str (int status)
165{
166 static char buf[64];
167
168 if (WIFSTOPPED (status))
169 snprintf (buf, sizeof (buf), "%s (stopped)",
170 strsignal (WSTOPSIG (status)));
171 else if (WIFSIGNALED (status))
172 snprintf (buf, sizeof (buf), "%s (terminated)",
173 strsignal (WSTOPSIG (status)));
174 else
175 snprintf (buf, sizeof (buf), "%d (exited)",
176 WEXITSTATUS (status));
177
178 return buf;
179}
180\f
c194fbe1
MK
181/* Initialize the list of LWPs. Note that this module, contrary to
182 what GDB's generic threads layer does for its thread list,
183 re-initializes the LWP lists whenever we mourn or detach (which
184 doesn't involve mourning) the inferior. */
fb0e1ba7
MK
185
186static void
187init_lwp_list (void)
188{
189 struct lwp_info *lp, *lpnext;
190
191 for (lp = lwp_list; lp; lp = lpnext)
192 {
193 lpnext = lp->next;
b8c9b27d 194 xfree (lp);
fb0e1ba7
MK
195 }
196
197 lwp_list = NULL;
198 num_lwps = 0;
199 threaded = 0;
200}
201
202/* Add the LWP specified by PID to the list. If this causes the
203 number of LWPs to become larger than one, go into "threaded" mode.
204 Return a pointer to the structure describing the new LWP. */
205
206static struct lwp_info *
39f77062 207add_lwp (ptid_t ptid)
fb0e1ba7
MK
208{
209 struct lwp_info *lp;
210
39f77062 211 gdb_assert (is_lwp (ptid));
fb0e1ba7
MK
212
213 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
214
215 memset (lp, 0, sizeof (struct lwp_info));
216
39f77062 217 lp->ptid = ptid;
fb0e1ba7
MK
218
219 lp->next = lwp_list;
220 lwp_list = lp;
221 if (++num_lwps > 1)
222 threaded = 1;
223
224 return lp;
225}
226
227/* Remove the LWP specified by PID from the list. */
228
229static void
39f77062 230delete_lwp (ptid_t ptid)
fb0e1ba7
MK
231{
232 struct lwp_info *lp, *lpprev;
233
234 lpprev = NULL;
235
236 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
39f77062 237 if (ptid_equal (lp->ptid, ptid))
fb0e1ba7
MK
238 break;
239
240 if (!lp)
241 return;
242
243 /* We don't go back to "non-threaded" mode if the number of threads
244 becomes less than two. */
245 num_lwps--;
246
247 if (lpprev)
248 lpprev->next = lp->next;
249 else
250 lwp_list = lp->next;
251
b8c9b27d 252 xfree (lp);
fb0e1ba7
MK
253}
254
255/* Return a pointer to the structure describing the LWP corresponding
256 to PID. If no corresponding LWP could be found, return NULL. */
257
258static struct lwp_info *
39f77062 259find_lwp_pid (ptid_t ptid)
fb0e1ba7
MK
260{
261 struct lwp_info *lp;
39f77062 262 int lwp;
fb0e1ba7 263
39f77062
KB
264 if (is_lwp (ptid))
265 lwp = GET_LWP (ptid);
266 else
267 lwp = GET_PID (ptid);
fb0e1ba7
MK
268
269 for (lp = lwp_list; lp; lp = lp->next)
39f77062 270 if (lwp == GET_LWP (lp->ptid))
fb0e1ba7
MK
271 return lp;
272
273 return NULL;
274}
275
276/* Call CALLBACK with its second argument set to DATA for every LWP in
277 the list. If CALLBACK returns 1 for a particular LWP, return a
278 pointer to the structure describing that LWP immediately.
279 Otherwise return NULL. */
280
281struct lwp_info *
282iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
283{
fce0e6e1 284 struct lwp_info *lp, *lpnext;
fb0e1ba7 285
fce0e6e1
MK
286 for (lp = lwp_list; lp; lp = lpnext)
287 {
288 lpnext = lp->next;
289 if ((*callback) (lp, data))
290 return lp;
291 }
fb0e1ba7
MK
292
293 return NULL;
294}
295\f
296
8605d56e 297/* Implementation of the PREPARE_TO_PROCEED hook for the GNU/Linux LWP
e02bc4cc
DS
298 layer.
299
300 Note that this implementation is potentially redundant now that
8849f47d
JL
301 default_prepare_to_proceed() has been added.
302
303 FIXME This may not support switching threads after Ctrl-C
304 correctly. The default implementation does support this. */
fb0e1ba7
MK
305
306int
307lin_lwp_prepare_to_proceed (void)
308{
39f77062
KB
309 if (! ptid_equal (trap_ptid, null_ptid)
310 && ! ptid_equal (inferior_ptid, trap_ptid))
fb0e1ba7
MK
311 {
312 /* Switched over from TRAP_PID. */
313 CORE_ADDR stop_pc = read_pc ();
314 CORE_ADDR trap_pc;
315
316 /* Avoid switching where it wouldn't do any good, i.e. if both
317 threads are at the same breakpoint. */
39f77062 318 trap_pc = read_pc_pid (trap_ptid);
fb0e1ba7
MK
319 if (trap_pc != stop_pc && breakpoint_here_p (trap_pc))
320 {
321 /* User hasn't deleted the breakpoint. Return non-zero, and
322 switch back to TRAP_PID. */
39f77062 323 inferior_ptid = trap_ptid;
fb0e1ba7
MK
324
325 /* FIXME: Is this stuff really necessary? */
326 flush_cached_frames ();
327 registers_changed ();
328
329 return 1;
330 }
331 }
332
333 return 0;
334}
335\f
336
337#if 0
338static void
339lin_lwp_open (char *args, int from_tty)
340{
341 push_target (&lin_lwp_ops);
342}
343#endif
344
345/* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
346 a message telling the user that a new LWP has been added to the
347 process. */
348
349void
39f77062 350lin_lwp_attach_lwp (ptid_t ptid, int verbose)
fb0e1ba7
MK
351{
352 struct lwp_info *lp;
353
39f77062 354 gdb_assert (is_lwp (ptid));
fb0e1ba7 355
da9c7185
KB
356 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
357 to interrupt either the ptrace() or waitpid() calls below. */
358 if (! sigismember (&blocked_mask, SIGCHLD))
359 {
360 sigaddset (&blocked_mask, SIGCHLD);
361 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
362 }
363
fb0e1ba7 364 if (verbose)
39f77062 365 printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
fb0e1ba7 366
c194fbe1
MK
367 lp = find_lwp_pid (ptid);
368 if (lp == NULL)
369 lp = add_lwp (ptid);
370
cacab7c4
MK
371 /* We assume that we're already attached to any LWP that has an
372 id equal to the overall process id. */
373 if (GET_LWP (ptid) != GET_PID (ptid))
c4365b19 374 {
cacab7c4
MK
375 pid_t pid;
376 int status;
377
378 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
379 error ("Can't attach %s: %s", target_pid_to_str (ptid),
dc672865 380 safe_strerror (errno));
cacab7c4
MK
381
382 pid = waitpid (GET_LWP (ptid), &status, 0);
383 if (pid == -1 && errno == ECHILD)
384 {
385 /* Try again with __WCLONE to check cloned processes. */
386 pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
387 lp->cloned = 1;
388 }
389
390 gdb_assert (pid == GET_LWP (ptid)
391 && WIFSTOPPED (status) && WSTOPSIG (status));
392
393 lp->stopped = 1;
c4365b19 394 }
da9c7185
KB
395 else
396 {
397 /* We assume that the LWP representing the original process
398 is already stopped. Mark it as stopped in the data structure
399 that the lin-lwp layer uses to keep track of threads. Note
400 that this won't have already been done since the main thread
401 will have, we assume, been stopped by an attach from a
402 different layer. */
403 lp->stopped = 1;
404 }
fb0e1ba7
MK
405}
406
407static void
408lin_lwp_attach (char *args, int from_tty)
409{
c194fbe1 410 struct lwp_info *lp;
cacab7c4
MK
411 pid_t pid;
412 int status;
c194fbe1 413
fb0e1ba7
MK
414 /* FIXME: We should probably accept a list of process id's, and
415 attach all of them. */
c194fbe1
MK
416 child_ops.to_attach (args, from_tty);
417
418 /* Add the initial process as the first LWP to the list. */
cacab7c4 419 lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
c194fbe1
MK
420
421 /* Make sure the initial process is stopped. The user-level threads
422 layer might want to poke around in the inferior, and that won't
423 work if things haven't stabilized yet. */
cacab7c4
MK
424 pid = waitpid (GET_PID (inferior_ptid), &status, 0);
425 if (pid == -1 && errno == ECHILD)
426 {
427 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
428
429 /* Try again with __WCLONE to check cloned processes. */
430 pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
431 lp->cloned = 1;
432 }
433
434 gdb_assert (pid == GET_PID (inferior_ptid)
435 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
436
437 lp->stopped = 1;
c194fbe1
MK
438
439 /* Fake the SIGSTOP that core GDB expects. */
440 lp->status = W_STOPCODE (SIGSTOP);
fce0e6e1 441 lp->resumed = 1;
c194fbe1
MK
442}
443
444static int
445detach_callback (struct lwp_info *lp, void *data)
446{
447 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
448
449 if (debug_lin_lwp && lp->status)
b08cfdb6 450 fprintf_unfiltered (gdb_stdlog, "Pending %s for LWP %ld on detach.\n",
c194fbe1
MK
451 strsignal (WSTOPSIG (lp->status)), GET_LWP (lp->ptid));
452
453 while (lp->signalled && lp->stopped)
454 {
455 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
456 WSTOPSIG (lp->status)) < 0)
457 error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
dc672865 458 safe_strerror (errno));
c194fbe1
MK
459
460 lp->stopped = 0;
c4365b19 461 lp->signalled = 0;
c194fbe1
MK
462 lp->status = 0;
463 stop_wait_callback (lp, NULL);
464
465 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
466 }
467
cacab7c4
MK
468 /* We don't actually detach from the LWP that has an id equal to the
469 overall process id just yet. */
470 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
c194fbe1
MK
471 {
472 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
473 WSTOPSIG (lp->status)) < 0)
474 error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
dc672865 475 safe_strerror (errno));
c194fbe1
MK
476
477 delete_lwp (lp->ptid);
478 }
479
480 return 0;
fb0e1ba7
MK
481}
482
483static void
484lin_lwp_detach (char *args, int from_tty)
485{
c194fbe1
MK
486 iterate_over_lwps (detach_callback, NULL);
487
cacab7c4 488 /* Only the initial process should be left right now. */
c194fbe1
MK
489 gdb_assert (num_lwps == 1);
490
491 trap_ptid = null_ptid;
492
493 /* Destroy LWP info; it's no longer valid. */
494 init_lwp_list ();
495
496 /* Restore the original signal mask. */
497 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
498 sigemptyset (&blocked_mask);
499
01263b57 500 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
c194fbe1 501 child_ops.to_detach (args, from_tty);
fb0e1ba7
MK
502}
503\f
504
505struct private_thread_info
506{
507 int lwpid;
508};
509
510/* Return non-zero if TP corresponds to the LWP specified by DATA
511 (which is assumed to be a pointer to a `struct lwp_info'. */
512
513static int
514find_lwp_callback (struct thread_info *tp, void *data)
515{
516 struct lwp_info *lp = data;
517
39f77062 518 if (tp->private->lwpid == GET_LWP (lp->ptid))
fb0e1ba7
MK
519 return 1;
520
521 return 0;
522}
523
524/* Resume LP. */
525
526static int
527resume_callback (struct lwp_info *lp, void *data)
528{
529 if (lp->stopped && lp->status == 0)
530 {
531 struct thread_info *tp;
532
b1aeb4c5 533#if 0
fb0e1ba7
MK
534 /* FIXME: kettenis/2000-08-26: This should really be handled
535 properly by core GDB. */
536
39f77062 537 tp = find_thread_pid (lp->ptid);
fb0e1ba7
MK
538 if (tp == NULL)
539 tp = iterate_over_threads (find_lwp_callback, lp);
540 gdb_assert (tp);
541
542 /* If we were previously stepping the thread, and now continue
543 the thread we must invalidate the stepping range. However,
544 if there is a step_resume breakpoint for this thread, we must
545 preserve the stepping range to make it possible to continue
546 stepping once we hit it. */
547 if (tp->step_range_end && tp->step_resume_breakpoint == NULL)
548 {
549 gdb_assert (lp->step);
550 tp->step_range_start = tp->step_range_end = 0;
551 }
552#endif
553
39f77062 554 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
fb0e1ba7
MK
555 lp->stopped = 0;
556 lp->step = 0;
557 }
558
559 return 0;
560}
561
fce0e6e1
MK
562static int
563resume_clear_callback (struct lwp_info *lp, void *data)
564{
565 lp->resumed = 0;
566 return 0;
567}
568
569static int
570resume_set_callback (struct lwp_info *lp, void *data)
571{
572 lp->resumed = 1;
573 return 0;
574}
575
fb0e1ba7 576static void
39f77062 577lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
fb0e1ba7
MK
578{
579 struct lwp_info *lp;
580 int resume_all;
581
2a4b7c45
DJ
582 /* A specific PTID means `step only this process id'. */
583 resume_all = (PIDGET (ptid) == -1);
fb0e1ba7 584
fce0e6e1
MK
585 if (resume_all)
586 iterate_over_lwps (resume_set_callback, NULL);
587 else
588 iterate_over_lwps (resume_clear_callback, NULL);
589
fb0e1ba7 590 /* If PID is -1, it's the current inferior that should be
c4365b19 591 handled specially. */
39f77062
KB
592 if (PIDGET (ptid) == -1)
593 ptid = inferior_ptid;
fb0e1ba7 594
39f77062 595 lp = find_lwp_pid (ptid);
fb0e1ba7
MK
596 if (lp)
597 {
39f77062 598 ptid = pid_to_ptid (GET_LWP (lp->ptid));
fb0e1ba7 599
fb0e1ba7
MK
600 /* Remember if we're stepping. */
601 lp->step = step;
602
fce0e6e1
MK
603 /* Mark this LWP as resumed. */
604 lp->resumed = 1;
605
fb0e1ba7
MK
606 /* If we have a pending wait status for this thread, there is no
607 point in resuming the process. */
608 if (lp->status)
609 {
610 /* FIXME: What should we do if we are supposed to continue
611 this thread with a signal? */
612 gdb_assert (signo == TARGET_SIGNAL_0);
613 return;
614 }
40564aca
MK
615
616 /* Mark LWP as not stopped to prevent it from being continued by
617 resume_callback. */
618 lp->stopped = 0;
fb0e1ba7
MK
619 }
620
621 if (resume_all)
622 iterate_over_lwps (resume_callback, NULL);
623
39f77062 624 child_resume (ptid, step, signo);
fb0e1ba7
MK
625}
626\f
627
628/* Send a SIGSTOP to LP. */
629
630static int
631stop_callback (struct lwp_info *lp, void *data)
632{
633 if (! lp->stopped && ! lp->signalled)
634 {
635 int ret;
636
39f77062 637 ret = kill (GET_LWP (lp->ptid), SIGSTOP);
fb0e1ba7
MK
638 gdb_assert (ret == 0);
639
640 lp->signalled = 1;
641 gdb_assert (lp->status == 0);
642 }
643
644 return 0;
645}
646
de4ca854
MK
647/* Wait until LP is stopped. If DATA is non-null it is interpreted as
648 a pointer to a set of signals to be flushed immediately. */
fb0e1ba7
MK
649
650static int
651stop_wait_callback (struct lwp_info *lp, void *data)
652{
de4ca854
MK
653 sigset_t *flush_mask = data;
654
fb0e1ba7
MK
655 if (! lp->stopped && lp->signalled)
656 {
657 pid_t pid;
658 int status;
659
660 gdb_assert (lp->status == 0);
661
cacab7c4 662 pid = waitpid (GET_LWP (lp->ptid), &status, lp->cloned ? __WCLONE : 0);
fb0e1ba7
MK
663 if (pid == -1 && errno == ECHILD)
664 /* OK, the proccess has disappeared. We'll catch the actual
3f07c44b 665 exit event in lin_lwp_wait. */
fb0e1ba7
MK
666 return 0;
667
39f77062 668 gdb_assert (pid == GET_LWP (lp->ptid));
fb0e1ba7
MK
669
670 if (WIFEXITED (status) || WIFSIGNALED (status))
671 {
672 gdb_assert (num_lwps > 1);
fb0e1ba7 673
39f77062 674 if (in_thread_list (lp->ptid))
e6328671
MK
675 {
676 /* Core GDB cannot deal with us deleting the current
677 thread. */
39f77062
KB
678 if (!ptid_equal (lp->ptid, inferior_ptid))
679 delete_thread (lp->ptid);
e6328671 680 printf_unfiltered ("[%s exited]\n",
39f77062 681 target_pid_to_str (lp->ptid));
e6328671 682 }
7ca673cd 683 if (debug_lin_lwp)
9085700c 684 fprintf_unfiltered (gdb_stdlog,
39f77062 685 "%s exited.\n", target_pid_to_str (lp->ptid));
7ca673cd 686
39f77062 687 delete_lwp (lp->ptid);
fb0e1ba7
MK
688 return 0;
689 }
690
691 gdb_assert (WIFSTOPPED (status));
fb0e1ba7 692
de4ca854
MK
693 /* Ignore any signals in FLUSH_MASK. */
694 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
695 {
696 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
697 return stop_wait_callback (lp, flush_mask);
698 }
699
fb0e1ba7
MK
700 if (WSTOPSIG (status) != SIGSTOP)
701 {
b1aeb4c5 702 if (WSTOPSIG (status) == SIGTRAP)
fb0e1ba7
MK
703 {
704 /* If a LWP other than the LWP that we're reporting an
705 event for has hit a GDB breakpoint (as opposed to
706 some random trap signal), then just arrange for it to
707 hit it again later. We don't keep the SIGTRAP status
708 and don't forward the SIGTRAP signal to the LWP. We
709 will handle the current event, eventually we will
710 resume all LWPs, and this one will get its breakpoint
711 trap again.
712
713 If we do not do this, then we run the risk that the
714 user will delete or disable the breakpoint, but the
715 thread will have already tripped on it. */
7ca673cd 716
c4365b19
MS
717 /* Now resume this LWP and get the SIGSTOP event. */
718 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
b1aeb4c5
MS
719 if (debug_lin_lwp)
720 {
721 fprintf_unfiltered (gdb_stderr,
722 "SWC: Candidate SIGTRAP event in %ld\n",
723 GET_LWP (lp->ptid));
724 }
725 /* Hold the SIGTRAP for handling by lin_lwp_wait. */
726 stop_wait_callback (lp, data);
727 /* If there's another event, throw it back into the queue. */
728 if (lp->status)
729 kill (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
730 /* Save the sigtrap event. */
731 lp->status = status;
732 return 0;
fb0e1ba7
MK
733 }
734 else
735 {
b1aeb4c5
MS
736 /* The thread was stopped with a signal other than
737 SIGSTOP, and didn't accidentally trip a breakpoint. */
738
7ca673cd 739 if (debug_lin_lwp)
b1aeb4c5
MS
740 {
741 fprintf_unfiltered (gdb_stderr,
742 "SWC: Pending event %d in %ld\n",
743 WSTOPSIG (status), GET_LWP (lp->ptid));
744 }
745 /* Now resume this LWP and get the SIGSTOP event. */
746 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
7ca673cd 747
b1aeb4c5
MS
748 /* Hold this event/waitstatus while we check to see if
749 there are any more (we still want to get that SIGSTOP). */
750 stop_wait_callback (lp, data);
751 /* If the lp->status field is still empty, use it to hold
752 this event. If not, then this event must be returned
753 to the event queue of the LWP. */
754 if (lp->status == 0)
755 lp->status = status;
756 else
757 kill (GET_LWP (lp->ptid), WSTOPSIG (status));
758 return 0;
fb0e1ba7
MK
759 }
760 }
761 else
762 {
763 /* We caught the SIGSTOP that we intended to catch, so
764 there's no SIGSTOP pending. */
b1aeb4c5 765 lp->stopped = 1;
fb0e1ba7
MK
766 lp->signalled = 0;
767 }
768 }
769
770 return 0;
771}
772
773/* Return non-zero if LP has a wait status pending. */
774
775static int
776status_callback (struct lwp_info *lp, void *data)
777{
fce0e6e1
MK
778 /* Only report a pending wait status if we pretend that this has
779 indeed been resumed. */
780 return (lp->status != 0 && lp->resumed);
fb0e1ba7
MK
781}
782
783/* Return non-zero if LP isn't stopped. */
784
785static int
786running_callback (struct lwp_info *lp, void *data)
787{
788 return (lp->stopped == 0);
789}
790
00d4fce6 791/* Count the LWP's that have had events. */
b1aeb4c5
MS
792
793static int
794count_events_callback (struct lwp_info *lp, void *data)
795{
796 int *count = data;
797
00d4fce6
MK
798 gdb_assert (count != NULL);
799
800 /* Count only LWPs that have a SIGTRAP event pending. */
801 if (lp->status != 0
802 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
b1aeb4c5
MS
803 (*count)++;
804
805 return 0;
806}
807
00d4fce6 808/* Select the LWP (if any) that is currently being single-stepped. */
b1aeb4c5
MS
809
810static int
811select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
812{
813 if (lp->step && lp->status != 0)
814 return 1;
815 else
816 return 0;
817}
818
00d4fce6 819/* Select the Nth LWP that has had a SIGTRAP event. */
b1aeb4c5
MS
820
821static int
822select_event_lwp_callback (struct lwp_info *lp, void *data)
823{
824 int *selector = data;
825
00d4fce6
MK
826 gdb_assert (selector != NULL);
827
828 /* Select only LWPs that have a SIGTRAP event pending. */
829 if (lp->status != 0
830 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
b1aeb4c5
MS
831 if ((*selector)-- == 0)
832 return 1;
833
834 return 0;
835}
836
837static int
838cancel_breakpoints_callback (struct lwp_info *lp, void *data)
839{
840 struct lwp_info *event_lp = data;
841
00d4fce6
MK
842 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
843 if (lp == event_lp)
844 return 0;
845
846 /* If a LWP other than the LWP that we're reporting an event for has
847 hit a GDB breakpoint (as opposed to some random trap signal),
848 then just arrange for it to hit it again later. We don't keep
849 the SIGTRAP status and don't forward the SIGTRAP signal to the
850 LWP. We will handle the current event, eventually we will resume
851 all LWPs, and this one will get its breakpoint trap again.
852
853 If we do not do this, then we run the risk that the user will
854 delete or disable the breakpoint, but the LWP will have already
855 tripped on it. */
856
857 if (lp->status != 0
858 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
859 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
860 DECR_PC_AFTER_BREAK))
b1aeb4c5
MS
861 {
862 if (debug_lin_lwp)
00d4fce6
MK
863 fprintf_unfiltered (gdb_stdlog,
864 "Push back breakpoint for LWP %ld\n",
865 GET_LWP (lp->ptid));
866
867 /* Back up the PC if necessary. */
b1aeb4c5 868 if (DECR_PC_AFTER_BREAK)
00d4fce6
MK
869 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
870
871 /* Throw away the SIGTRAP. */
b1aeb4c5
MS
872 lp->status = 0;
873 }
00d4fce6 874
b1aeb4c5
MS
875 return 0;
876}
877
00d4fce6 878/* Select one LWP out of those that have events pending. */
b1aeb4c5
MS
879
880static void
881select_event_lwp (struct lwp_info **orig_lp, int *status)
882{
883 int num_events = 0;
884 int random_selector;
885 struct lwp_info *event_lp;
886
00d4fce6
MK
887 /* Record the wait status for the origional LWP. */
888 (*orig_lp)->status = *status;
889
890 /* Give preference to any LWP that is being single-stepped. */
b1aeb4c5
MS
891 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
892 if (event_lp != NULL)
893 {
894 if (debug_lin_lwp)
00d4fce6
MK
895 fprintf_unfiltered (gdb_stdlog,
896 "Select single-step LWP %ld\n",
b1aeb4c5
MS
897 GET_LWP (event_lp->ptid));
898 }
899 else
900 {
901 /* No single-stepping LWP. Select one at random, out of those
00d4fce6 902 which have had SIGTRAP events. */
b1aeb4c5 903
00d4fce6
MK
904 /* First see how many SIGTRAP events we have. */
905 iterate_over_lwps (count_events_callback, &num_events);
b1aeb4c5 906
00d4fce6
MK
907 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
908 random_selector = (int)
b1aeb4c5
MS
909 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
910
00d4fce6
MK
911 if (debug_lin_lwp && num_events > 1)
912 fprintf_unfiltered (gdb_stdlog,
913 "Found %d SIGTRAP events, selecting #%d\n",
914 num_events, random_selector);
b1aeb4c5 915
00d4fce6
MK
916 event_lp = iterate_over_lwps (select_event_lwp_callback,
917 &random_selector);
b1aeb4c5
MS
918 }
919
920 if (event_lp != NULL)
921 {
00d4fce6 922 /* Switch the event LWP. */
b1aeb4c5
MS
923 *orig_lp = event_lp;
924 *status = event_lp->status;
b1aeb4c5 925 }
00d4fce6
MK
926
927 /* Flush the wait status for the event LWP. */
928 (*orig_lp)->status = 0;
b1aeb4c5
MS
929}
930
fce0e6e1
MK
931/* Return non-zero if LP has been resumed. */
932
933static int
934resumed_callback (struct lwp_info *lp, void *data)
935{
936 return lp->resumed;
937}
938
cacab7c4
MK
939#ifdef CHILD_WAIT
940
941/* We need to override child_wait to support attaching to cloned
942 processes, since a normal wait (as done by the default version)
943 ignores those processes. */
944
945/* Wait for child PTID to do something. Return id of the child,
946 minus_one_ptid in case of error; store status into *OURSTATUS. */
947
948ptid_t
949child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
950{
951 int save_errno;
952 int status;
953 pid_t pid;
954
955 do
956 {
957 set_sigint_trap (); /* Causes SIGINT to be passed on to the
958 attached process. */
959 set_sigio_trap ();
960
961 pid = waitpid (GET_PID (ptid), &status, 0);
962 if (pid == -1 && errno == ECHILD)
963 /* Try again with __WCLONE to check cloned processes. */
964 pid = waitpid (GET_PID (ptid), &status, __WCLONE);
965 save_errno = errno;
966
967 clear_sigio_trap ();
968 clear_sigint_trap ();
969 }
2d1bfe2e 970 while (pid == -1 && save_errno == EINTR);
cacab7c4
MK
971
972 if (pid == -1)
973 {
dc672865 974 warning ("Child process unexpectedly missing: %s", safe_strerror (errno));
cacab7c4
MK
975
976 /* Claim it exited with unknown signal. */
977 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
978 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
979 return minus_one_ptid;
980 }
981
982 store_waitstatus (ourstatus, status);
983 return pid_to_ptid (pid);
984}
985
986#endif
987
39f77062
KB
988static ptid_t
989lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
fb0e1ba7
MK
990{
991 struct lwp_info *lp = NULL;
992 int options = 0;
993 int status = 0;
39f77062 994 pid_t pid = PIDGET (ptid);
de4ca854
MK
995 sigset_t flush_mask;
996
997 sigemptyset (&flush_mask);
fb0e1ba7 998
3f07c44b
MK
999 /* Make sure SIGCHLD is blocked. */
1000 if (! sigismember (&blocked_mask, SIGCHLD))
1001 {
1002 sigaddset (&blocked_mask, SIGCHLD);
1003 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1004 }
1005
fb0e1ba7
MK
1006 retry:
1007
9a973a8f
MK
1008 /* Make sure there is at least one LWP that has been resumed, at
1009 least if there are any LWPs at all. */
1010 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
fce0e6e1 1011
fb0e1ba7
MK
1012 /* First check if there is a LWP with a wait status pending. */
1013 if (pid == -1)
1014 {
b1aeb4c5 1015 /* Any LWP that's been resumed will do. */
fb0e1ba7
MK
1016 lp = iterate_over_lwps (status_callback, NULL);
1017 if (lp)
1018 {
fb0e1ba7
MK
1019 status = lp->status;
1020 lp->status = 0;
b1aeb4c5
MS
1021
1022 if (debug_lin_lwp && status)
58eeadba
MK
1023 fprintf_unfiltered (gdb_stdlog,
1024 "Using pending wait status %s for LWP %ld.\n",
1025 status_to_str (status), GET_LWP (lp->ptid));
fb0e1ba7
MK
1026 }
1027
1028 /* But if we don't fine one, we'll have to wait, and check both
1029 cloned and uncloned processes. We start with the cloned
1030 processes. */
1031 options = __WCLONE | WNOHANG;
1032 }
39f77062 1033 else if (is_lwp (ptid))
fb0e1ba7 1034 {
7ca673cd 1035 if (debug_lin_lwp)
9085700c 1036 fprintf_unfiltered (gdb_stdlog,
b08cfdb6 1037 "Waiting for specific LWP %ld.\n",
ce696e05 1038 GET_LWP (ptid));
7ca673cd 1039
fb0e1ba7 1040 /* We have a specific LWP to check. */
39f77062 1041 lp = find_lwp_pid (ptid);
fb0e1ba7
MK
1042 gdb_assert (lp);
1043 status = lp->status;
1044 lp->status = 0;
7ca673cd 1045
b1aeb4c5 1046 if (debug_lin_lwp && status)
58eeadba
MK
1047 fprintf_unfiltered (gdb_stdlog,
1048 "Using pending wait status %s for LWP %ld.\n",
1049 status_to_str (status), GET_LWP (lp->ptid));
fb0e1ba7
MK
1050
1051 /* If we have to wait, take into account whether PID is a cloned
1052 process or not. And we have to convert it to something that
1053 the layer beneath us can understand. */
cacab7c4 1054 options = lp->cloned ? __WCLONE : 0;
39f77062 1055 pid = GET_LWP (ptid);
fb0e1ba7
MK
1056 }
1057
1058 if (status && lp->signalled)
1059 {
1060 /* A pending SIGSTOP may interfere with the normal stream of
1061 events. In a typical case where interference is a problem,
1062 we have a SIGSTOP signal pending for LWP A while
1063 single-stepping it, encounter an event in LWP B, and take the
1064 pending SIGSTOP while trying to stop LWP A. After processing
1065 the event in LWP B, LWP A is continued, and we'll never see
1066 the SIGTRAP associated with the last time we were
1067 single-stepping LWP A. */
1068
1069 /* Resume the thread. It should halt immediately returning the
1070 pending SIGSTOP. */
39f77062
KB
1071 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1072 TARGET_SIGNAL_0);
fb0e1ba7 1073 lp->stopped = 0;
fce0e6e1 1074 gdb_assert (lp->resumed);
fb0e1ba7
MK
1075
1076 /* This should catch the pending SIGSTOP. */
1077 stop_wait_callback (lp, NULL);
1078 }
1079
1080 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1081 attached process. */
1082 set_sigio_trap ();
1083
1084 while (status == 0)
1085 {
1086 pid_t lwpid;
1087
1088 lwpid = waitpid (pid, &status, options);
1089 if (lwpid > 0)
1090 {
1091 gdb_assert (pid == -1 || lwpid == pid);
1092
39f77062 1093 lp = find_lwp_pid (pid_to_ptid (lwpid));
fb0e1ba7
MK
1094 if (! lp)
1095 {
39f77062 1096 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
cacab7c4
MK
1097 if (options & __WCLONE)
1098 lp->cloned = 1;
1099
fb0e1ba7
MK
1100 if (threaded)
1101 {
3f07c44b
MK
1102 gdb_assert (WIFSTOPPED (status)
1103 && WSTOPSIG (status) == SIGSTOP);
fb0e1ba7
MK
1104 lp->signalled = 1;
1105
39f77062 1106 if (! in_thread_list (inferior_ptid))
fb0e1ba7 1107 {
39f77062
KB
1108 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1109 GET_PID (inferior_ptid));
1110 add_thread (inferior_ptid);
fb0e1ba7
MK
1111 }
1112
39f77062 1113 add_thread (lp->ptid);
fb0e1ba7 1114 printf_unfiltered ("[New %s]\n",
39f77062 1115 target_pid_to_str (lp->ptid));
fb0e1ba7
MK
1116 }
1117 }
1118
1119 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
1120 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
1121 left in the process. */
1122 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
1123 {
39f77062 1124 if (in_thread_list (lp->ptid))
fb0e1ba7 1125 {
e6328671 1126 /* Core GDB cannot deal with us deleting the current
fb0e1ba7 1127 thread. */
39f77062
KB
1128 if (! ptid_equal (lp->ptid, inferior_ptid))
1129 delete_thread (lp->ptid);
fb0e1ba7 1130 printf_unfiltered ("[%s exited]\n",
39f77062 1131 target_pid_to_str (lp->ptid));
fb0e1ba7 1132 }
7ca673cd 1133 if (debug_lin_lwp)
9085700c
MS
1134 fprintf_unfiltered (gdb_stdlog,
1135 "%s exited.\n",
39f77062 1136 target_pid_to_str (lp->ptid));
7ca673cd 1137
39f77062 1138 delete_lwp (lp->ptid);
fb0e1ba7
MK
1139
1140 /* Make sure there is at least one thread running. */
1141 gdb_assert (iterate_over_lwps (running_callback, NULL));
1142
1143 /* Discard the event. */
1144 status = 0;
1145 continue;
1146 }
1147
1148 /* Make sure we don't report a SIGSTOP that we sent
1149 ourselves in an attempt to stop an LWP. */
1150 if (lp->signalled && WIFSTOPPED (status)
1151 && WSTOPSIG (status) == SIGSTOP)
1152 {
7ca673cd 1153 if (debug_lin_lwp)
9085700c
MS
1154 fprintf_unfiltered (gdb_stdlog,
1155 "Delayed SIGSTOP caught for %s.\n",
39f77062 1156 target_pid_to_str (lp->ptid));
7ca673cd 1157
fb0e1ba7
MK
1158 /* This is a delayed SIGSTOP. */
1159 lp->signalled = 0;
1160
39f77062
KB
1161 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1162 TARGET_SIGNAL_0);
fb0e1ba7 1163 lp->stopped = 0;
fce0e6e1 1164 gdb_assert (lp->resumed);
fb0e1ba7
MK
1165
1166 /* Discard the event. */
1167 status = 0;
1168 continue;
1169 }
1170
1171 break;
1172 }
1173
1174 if (pid == -1)
1175 {
1176 /* Alternate between checking cloned and uncloned processes. */
1177 options ^= __WCLONE;
1178
1179 /* And suspend every time we have checked both. */
1180 if (options & __WCLONE)
1181 sigsuspend (&suspend_mask);
1182 }
1183
1184 /* We shouldn't end up here unless we want to try again. */
1185 gdb_assert (status == 0);
1186 }
1187
1188 clear_sigio_trap ();
1189 clear_sigint_trap ();
1190
1191 gdb_assert (lp);
1192
1193 /* Don't report signals that GDB isn't interested in, such as
1194 signals that are neither printed nor stopped upon. Stopping all
1195 threads can be a bit time-consuming so if we want decent
1196 performance with heavily multi-threaded programs, especially when
1197 they're using a high frequency timer, we'd better avoid it if we
1198 can. */
1199
1200 if (WIFSTOPPED (status))
1201 {
1202 int signo = target_signal_from_host (WSTOPSIG (status));
1203
1204 if (signal_stop_state (signo) == 0
1205 && signal_print_state (signo) == 0
1206 && signal_pass_state (signo) == 1)
1207 {
fce0e6e1
MK
1208 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
1209 here? It is not clear we should. GDB may not expect
1210 other threads to run. On the other hand, not resuming
1211 newly attached threads may cause an unwanted delay in
1212 getting them running. */
c4365b19 1213 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
fce0e6e1 1214 lp->stopped = 0;
fb0e1ba7
MK
1215 status = 0;
1216 goto retry;
1217 }
de4ca854
MK
1218
1219 if (signo == TARGET_SIGNAL_INT
1220 && signal_pass_state (signo) == 0)
1221 {
1222 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
1223 forwarded to the entire process group, that is, all LWP's
1224 will receive it. Since we only want to report it once,
1225 we try to flush it from all LWPs except this one. */
1226 sigaddset (&flush_mask, SIGINT);
1227 }
fb0e1ba7
MK
1228 }
1229
1230 /* This LWP is stopped now. */
1231 lp->stopped = 1;
1232
b1aeb4c5 1233 if (debug_lin_lwp)
58eeadba
MK
1234 fprintf_unfiltered (gdb_stdlog, "Candidate event %s in LWP %ld.\n",
1235 status_to_str (status), GET_LWP (lp->ptid));
b1aeb4c5 1236
fb0e1ba7
MK
1237 /* Now stop all other LWP's ... */
1238 iterate_over_lwps (stop_callback, NULL);
1239
1240 /* ... and wait until all of them have reported back that they're no
1241 longer running. */
de4ca854 1242 iterate_over_lwps (stop_wait_callback, &flush_mask);
fb0e1ba7 1243
00d4fce6
MK
1244 /* If we're not waiting for a specific LWP, choose an event LWP from
1245 among those that have had events. Giving equal priority to all
1246 LWPs that have had events helps prevent starvation. */
1247 if (pid == -1)
1248 select_event_lwp (&lp, &status);
b1aeb4c5 1249
00d4fce6
MK
1250 /* Now that we've selected our final event LWP, cancel any
1251 breakpoints in other LWPs that have hit a GDB breakpoint. See
1252 the comment in cancel_breakpoints_callback to find out why. */
1253 iterate_over_lwps (cancel_breakpoints_callback, lp);
b1aeb4c5 1254
fb0e1ba7
MK
1255 /* If we're not running in "threaded" mode, we'll report the bare
1256 process id. */
1257
1258 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
b1aeb4c5
MS
1259 {
1260 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1261 if (debug_lin_lwp)
1262 fprintf_unfiltered (gdb_stdlog,
1263 "LLW: trap_ptid is %ld\n",
1264 GET_LWP (trap_ptid));
1265 }
fb0e1ba7 1266 else
39f77062 1267 trap_ptid = null_ptid;
fb0e1ba7
MK
1268
1269 store_waitstatus (ourstatus, status);
39f77062 1270 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
fb0e1ba7
MK
1271}
1272
1273static int
1274kill_callback (struct lwp_info *lp, void *data)
1275{
39f77062 1276 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
fb0e1ba7
MK
1277 return 0;
1278}
1279
1280static int
1281kill_wait_callback (struct lwp_info *lp, void *data)
1282{
1283 pid_t pid;
1284
1285 /* We must make sure that there are no pending events (delayed
1286 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
1287 program doesn't interfere with any following debugging session. */
1288
1289 /* For cloned processes we must check both with __WCLONE and
1290 without, since the exit status of a cloned process isn't reported
1291 with __WCLONE. */
cacab7c4 1292 if (lp->cloned)
fb0e1ba7
MK
1293 {
1294 do
1295 {
39f77062 1296 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
fb0e1ba7 1297 }
39f77062 1298 while (pid == GET_LWP (lp->ptid));
fb0e1ba7
MK
1299
1300 gdb_assert (pid == -1 && errno == ECHILD);
1301 }
1302
1303 do
1304 {
39f77062 1305 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
fb0e1ba7 1306 }
39f77062 1307 while (pid == GET_LWP (lp->ptid));
fb0e1ba7
MK
1308
1309 gdb_assert (pid == -1 && errno == ECHILD);
1310 return 0;
1311}
1312
1313static void
1314lin_lwp_kill (void)
1315{
1316 /* Kill all LWP's ... */
1317 iterate_over_lwps (kill_callback, NULL);
1318
1319 /* ... and wait until we've flushed all events. */
1320 iterate_over_lwps (kill_wait_callback, NULL);
1321
1322 target_mourn_inferior ();
1323}
1324
1325static void
1326lin_lwp_create_inferior (char *exec_file, char *allargs, char **env)
1327{
c194fbe1 1328 child_ops.to_create_inferior (exec_file, allargs, env);
fb0e1ba7
MK
1329}
1330
1331static void
1332lin_lwp_mourn_inferior (void)
1333{
c194fbe1 1334 trap_ptid = null_ptid;
fb0e1ba7 1335
c194fbe1 1336 /* Destroy LWP info; it's no longer valid. */
fb0e1ba7
MK
1337 init_lwp_list ();
1338
4c8de859 1339 /* Restore the original signal mask. */
3f07c44b
MK
1340 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1341 sigemptyset (&blocked_mask);
1342
c194fbe1 1343 child_ops.to_mourn_inferior ();
fb0e1ba7
MK
1344}
1345
1346static void
1347lin_lwp_fetch_registers (int regno)
1348{
39f77062 1349 struct cleanup *old_chain = save_inferior_ptid ();
fb0e1ba7 1350
39f77062
KB
1351 if (is_lwp (inferior_ptid))
1352 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
fb0e1ba7
MK
1353
1354 fetch_inferior_registers (regno);
1355
1356 do_cleanups (old_chain);
1357}
1358
1359static void
1360lin_lwp_store_registers (int regno)
1361{
39f77062 1362 struct cleanup *old_chain = save_inferior_ptid ();
fb0e1ba7 1363
39f77062
KB
1364 if (is_lwp (inferior_ptid))
1365 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
fb0e1ba7
MK
1366
1367 store_inferior_registers (regno);
1368
1369 do_cleanups (old_chain);
1370}
1371
1372static int
1373lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
e5da8f38 1374 struct mem_attrib *attrib,
fb0e1ba7
MK
1375 struct target_ops *target)
1376{
39f77062 1377 struct cleanup *old_chain = save_inferior_ptid ();
fb0e1ba7
MK
1378 int xfer;
1379
39f77062
KB
1380 if (is_lwp (inferior_ptid))
1381 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
fb0e1ba7 1382
eb784848
DJ
1383 xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1384 if (xfer == 0)
1385 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
fb0e1ba7
MK
1386
1387 do_cleanups (old_chain);
1388 return xfer;
1389}
1390
1391static int
39f77062 1392lin_lwp_thread_alive (ptid_t ptid)
fb0e1ba7 1393{
39f77062 1394 gdb_assert (is_lwp (ptid));
fb0e1ba7
MK
1395
1396 errno = 0;
39f77062 1397 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
fb0e1ba7
MK
1398 if (errno)
1399 return 0;
1400
1401 return 1;
1402}
1403
1404static char *
39f77062 1405lin_lwp_pid_to_str (ptid_t ptid)
fb0e1ba7
MK
1406{
1407 static char buf[64];
1408
39f77062 1409 if (is_lwp (ptid))
fb0e1ba7 1410 {
b08cfdb6 1411 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
fb0e1ba7
MK
1412 return buf;
1413 }
1414
39f77062 1415 return normal_pid_to_str (ptid);
fb0e1ba7
MK
1416}
1417
1418static void
1419init_lin_lwp_ops (void)
1420{
1421#if 0
1422 lin_lwp_ops.to_open = lin_lwp_open;
1423#endif
1424 lin_lwp_ops.to_shortname = "lwp-layer";
1425 lin_lwp_ops.to_longname = "lwp-layer";
1426 lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
1427 lin_lwp_ops.to_attach = lin_lwp_attach;
1428 lin_lwp_ops.to_detach = lin_lwp_detach;
1429 lin_lwp_ops.to_resume = lin_lwp_resume;
1430 lin_lwp_ops.to_wait = lin_lwp_wait;
1431 lin_lwp_ops.to_fetch_registers = lin_lwp_fetch_registers;
1432 lin_lwp_ops.to_store_registers = lin_lwp_store_registers;
1433 lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
1434 lin_lwp_ops.to_kill = lin_lwp_kill;
1435 lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
1436 lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
1437 lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
1438 lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
1439 lin_lwp_ops.to_stratum = thread_stratum;
1440 lin_lwp_ops.to_has_thread_control = tc_schedlock;
1441 lin_lwp_ops.to_magic = OPS_MAGIC;
1442}
1443
1444static void
1445sigchld_handler (int signo)
1446{
1447 /* Do nothing. The only reason for this handler is that it allows
1448 us to use sigsuspend in lin_lwp_wait above to wait for the
1449 arrival of a SIGCHLD. */
1450}
1451
1452void
1453_initialize_lin_lwp (void)
1454{
1455 struct sigaction action;
fb0e1ba7
MK
1456
1457 extern void thread_db_init (struct target_ops *);
1458
1459 init_lin_lwp_ops ();
1460 add_target (&lin_lwp_ops);
1461 thread_db_init (&lin_lwp_ops);
1462
4c8de859 1463 /* Save the original signal mask. */
3f07c44b
MK
1464 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
1465
fb0e1ba7
MK
1466 action.sa_handler = sigchld_handler;
1467 sigemptyset (&action.sa_mask);
1468 action.sa_flags = 0;
1469 sigaction (SIGCHLD, &action, NULL);
1470
3f07c44b
MK
1471 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1472 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
fb0e1ba7 1473 sigdelset (&suspend_mask, SIGCHLD);
3f07c44b
MK
1474
1475 sigemptyset (&blocked_mask);
7ca673cd
MS
1476
1477 add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
1478 (char *) &debug_lin_lwp,
8605d56e 1479 "Set debugging of GNU/Linux lwp module.\n\
7ca673cd
MS
1480Enables printf debugging output.\n",
1481 &setdebuglist),
1482 &showdebuglist);
fb0e1ba7
MK
1483}
1484\f
1485
1486/* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
8605d56e
AC
1487 the GNU/Linux Threads library and therefore doesn't really belong
1488 here. */
fb0e1ba7
MK
1489
1490/* Read variable NAME in the target and return its value if found.
1491 Otherwise return zero. It is assumed that the type of the variable
1492 is `int'. */
1493
1494static int
1495get_signo (const char *name)
1496{
1497 struct minimal_symbol *ms;
1498 int signo;
1499
1500 ms = lookup_minimal_symbol (name, NULL, NULL);
1501 if (ms == NULL)
1502 return 0;
1503
1504 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
1505 sizeof (signo)) != 0)
1506 return 0;
1507
1508 return signo;
1509}
1510
1511/* Return the set of signals used by the threads library in *SET. */
1512
1513void
1514lin_thread_get_thread_signals (sigset_t *set)
1515{
3f07c44b
MK
1516 struct sigaction action;
1517 int restart, cancel;
fb0e1ba7
MK
1518
1519 sigemptyset (set);
1520
1521 restart = get_signo ("__pthread_sig_restart");
1522 if (restart == 0)
1523 return;
1524
1525 cancel = get_signo ("__pthread_sig_cancel");
1526 if (cancel == 0)
1527 return;
1528
1529 sigaddset (set, restart);
1530 sigaddset (set, cancel);
3f07c44b 1531
8605d56e
AC
1532 /* The GNU/Linux Threads library makes terminating threads send a
1533 special "cancel" signal instead of SIGCHLD. Make sure we catch
1534 those (to prevent them from terminating GDB itself, which is
1535 likely to be their default action) and treat them the same way as
1536 SIGCHLD. */
3f07c44b
MK
1537
1538 action.sa_handler = sigchld_handler;
1539 sigemptyset (&action.sa_mask);
1540 action.sa_flags = 0;
1541 sigaction (cancel, &action, NULL);
1542
1543 /* We block the "cancel" signal throughout this code ... */
1544 sigaddset (&blocked_mask, cancel);
1545 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1546
1547 /* ... except during a sigsuspend. */
1548 sigdelset (&suspend_mask, cancel);
fb0e1ba7 1549}
This page took 1.820378 seconds and 4 git commands to generate.