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