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