2001-04-26 Michael Snyder <msnyder@redhat.com>
[binutils-gdb.git] / gdb / lin-lwp.c
1 /* Multi-threaded debugging support for 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 <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"
32 #include "regcache.h"
33
34 extern int debug_linux_threads;
35 extern const char *strsignal (int sig);
36
37 /* On Linux there are no real LWP's. The closest thing to LWP's are
38 processes sharing the same VM space. A multi-threaded process is
39 basically a group of such processes. However, such a grouping is
40 almost entirely a user-space issue; the kernel doesn't enforce such
41 a grouping at all (this might change in the future). In general,
42 we'll rely on the threads library (i.e. the LinuxThreads library)
43 to provide such a grouping.
44
45 It is perfectly well possible to write a multi-threaded application
46 without the assistance of a threads library, by using the clone
47 system call directly. This module should be able to give some
48 rudimentary support for debugging such applications if developers
49 specify the CLONE_PTRACE flag in the clone system call, and are
50 using Linux 2.4 or above.
51
52 Note that there are some peculiarities in Linux that affect this
53 code:
54
55 - In general one should specify the __WCLONE flag to waitpid in
56 order to make it report events for any of the cloned processes
57 (and leave it out for the initial process). However, if a cloned
58 process has exited the exit status is only reported if the
59 __WCLONE flag is absent. Linux 2.4 has a __WALL flag, but we
60 cannot use it since GDB must work on older systems too.
61
62 - When a traced, cloned process exits and is waited for by the
63 debugger, the kernel reassigns it to the original parent and
64 keeps it around as a "zombie". Somehow, the LinuxThreads library
65 doesn't notice this, which leads to the "zombie problem": When
66 debugged a multi-threaded process that spawns a lot of threads
67 will run out of processes, even if the threads exit, because the
68 "zombies" stay around. */
69
70 /* Structure describing a LWP. */
71 struct lwp_info
72 {
73 /* The process id of the LWP. This is a combination of the LWP id
74 and overall process id. */
75 int pid;
76
77 /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report
78 it back yet). */
79 int signalled;
80
81 /* Non-zero if this LWP is stopped. */
82 int stopped;
83
84 /* If non-zero, a pending wait status. */
85 int status;
86
87 /* Non-zero if we were stepping this LWP. */
88 int step;
89
90 /* Next LWP in list. */
91 struct lwp_info *next;
92 };
93
94 /* List of known LWPs. */
95 static struct lwp_info *lwp_list;
96
97 /* Number of LWPs in the list. */
98 static int num_lwps;
99
100 /* Non-zero if we're running in "threaded" mode. */
101 static int threaded;
102 \f
103
104 #ifndef TIDGET
105 #define TIDGET(PID) (((PID) & 0x7fffffff) >> 16)
106 #define PIDGET(PID) (((PID) & 0xffff))
107 #define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16))
108 #endif
109
110 #define THREAD_FLAG 0x80000000
111 #define is_lwp(pid) (((pid) & THREAD_FLAG) == 0 && TIDGET (pid))
112 #define GET_LWP(pid) TIDGET (pid)
113 #define GET_PID(pid) PIDGET (pid)
114 #define BUILD_LWP(tid, pid) MERGEPID (pid, tid)
115
116 #define is_cloned(pid) (GET_LWP (pid) != GET_PID (pid))
117
118 /* If the last reported event was a SIGTRAP, this variable is set to
119 the process id of the LWP/thread that got it. */
120 int trap_pid;
121 \f
122
123 /* This module's target-specific operations. */
124 static struct target_ops lin_lwp_ops;
125
126 /* The standard child operations. */
127 extern struct target_ops child_ops;
128
129 /* Since we cannot wait (in lin_lwp_wait) for the initial process and
130 any cloned processes with a single call to waitpid, we have to use
131 the WNOHANG flag and call waitpid in a loop. To optimize
132 things a bit we use `sigsuspend' to wake us up when a process has
133 something to report (it will send us a SIGCHLD if it has). To make
134 this work we have to juggle with the signal mask. We save the
135 original signal mask such that we can restore it before creating a
136 new process in order to avoid blocking certain signals in the
137 inferior. We then block SIGCHLD during the waitpid/sigsuspend
138 loop. */
139
140 /* Original signal mask. */
141 static sigset_t normal_mask;
142
143 /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
144 _initialize_lin_lwp. */
145 static sigset_t suspend_mask;
146
147 /* Signals to block to make that sigsuspend work. */
148 static sigset_t blocked_mask;
149 \f
150
151 /* Prototypes for local functions. */
152 static void lin_lwp_mourn_inferior (void);
153 \f
154
155 /* Initialize the list of LWPs. */
156
157 static void
158 init_lwp_list (void)
159 {
160 struct lwp_info *lp, *lpnext;
161
162 for (lp = lwp_list; lp; lp = lpnext)
163 {
164 lpnext = lp->next;
165 xfree (lp);
166 }
167
168 lwp_list = NULL;
169 num_lwps = 0;
170 threaded = 0;
171 }
172
173 /* Add the LWP specified by PID to the list. If this causes the
174 number of LWPs to become larger than one, go into "threaded" mode.
175 Return a pointer to the structure describing the new LWP. */
176
177 static struct lwp_info *
178 add_lwp (int pid)
179 {
180 struct lwp_info *lp;
181
182 gdb_assert (is_lwp (pid));
183
184 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
185
186 memset (lp, 0, sizeof (struct lwp_info));
187
188 lp->pid = pid;
189
190 lp->next = lwp_list;
191 lwp_list = lp;
192 if (++num_lwps > 1)
193 threaded = 1;
194
195 return lp;
196 }
197
198 /* Remove the LWP specified by PID from the list. */
199
200 static void
201 delete_lwp (int pid)
202 {
203 struct lwp_info *lp, *lpprev;
204
205 lpprev = NULL;
206
207 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
208 if (lp->pid == pid)
209 break;
210
211 if (!lp)
212 return;
213
214 /* We don't go back to "non-threaded" mode if the number of threads
215 becomes less than two. */
216 num_lwps--;
217
218 if (lpprev)
219 lpprev->next = lp->next;
220 else
221 lwp_list = lp->next;
222
223 xfree (lp);
224 }
225
226 /* Return a pointer to the structure describing the LWP corresponding
227 to PID. If no corresponding LWP could be found, return NULL. */
228
229 static struct lwp_info *
230 find_lwp_pid (int pid)
231 {
232 struct lwp_info *lp;
233
234 if (is_lwp (pid))
235 pid = GET_LWP (pid);
236
237 for (lp = lwp_list; lp; lp = lp->next)
238 if (pid == GET_LWP (lp->pid))
239 return lp;
240
241 return NULL;
242 }
243
244 /* Call CALLBACK with its second argument set to DATA for every LWP in
245 the list. If CALLBACK returns 1 for a particular LWP, return a
246 pointer to the structure describing that LWP immediately.
247 Otherwise return NULL. */
248
249 struct lwp_info *
250 iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
251 {
252 struct lwp_info *lp;
253
254 for (lp = lwp_list; lp; lp = lp->next)
255 if ((*callback) (lp, data))
256 return lp;
257
258 return NULL;
259 }
260 \f
261
262 /* Helper functions. */
263
264 static void
265 restore_inferior_pid (void *arg)
266 {
267 int *saved_pid_ptr = arg;
268 inferior_pid = *saved_pid_ptr;
269 xfree (arg);
270 }
271
272 static struct cleanup *
273 save_inferior_pid (void)
274 {
275 int *saved_pid_ptr;
276
277 saved_pid_ptr = xmalloc (sizeof (int));
278 *saved_pid_ptr = inferior_pid;
279 return make_cleanup (restore_inferior_pid, saved_pid_ptr);
280 }
281 \f
282
283 /* Implementation of the PREPARE_TO_PROCEED hook for the Linux LWP
284 layer.
285
286 Note that this implementation is potentially redundant now that
287 default_prepare_to_proceed() has been added. */
288
289 int
290 lin_lwp_prepare_to_proceed (void)
291 {
292 if (trap_pid && inferior_pid != trap_pid)
293 {
294 /* Switched over from TRAP_PID. */
295 CORE_ADDR stop_pc = read_pc ();
296 CORE_ADDR trap_pc;
297
298 /* Avoid switching where it wouldn't do any good, i.e. if both
299 threads are at the same breakpoint. */
300 trap_pc = read_pc_pid (trap_pid);
301 if (trap_pc != stop_pc && breakpoint_here_p (trap_pc))
302 {
303 /* User hasn't deleted the breakpoint. Return non-zero, and
304 switch back to TRAP_PID. */
305 inferior_pid = trap_pid;
306
307 /* FIXME: Is this stuff really necessary? */
308 flush_cached_frames ();
309 registers_changed ();
310
311 return 1;
312 }
313 }
314
315 return 0;
316 }
317 \f
318
319 #if 0
320 static void
321 lin_lwp_open (char *args, int from_tty)
322 {
323 push_target (&lin_lwp_ops);
324 }
325 #endif
326
327 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
328 a message telling the user that a new LWP has been added to the
329 process. */
330
331 void
332 lin_lwp_attach_lwp (int pid, int verbose)
333 {
334 struct lwp_info *lp;
335
336 gdb_assert (is_lwp (pid));
337
338 if (verbose)
339 printf_filtered ("[New %s]\n", target_pid_to_str (pid));
340
341 if (ptrace (PTRACE_ATTACH, GET_LWP (pid), 0, 0) < 0)
342 error ("Can't attach %s: %s", target_pid_to_str (pid), strerror (errno));
343
344 lp = add_lwp (pid);
345 lp->signalled = 1;
346 }
347
348 static void
349 lin_lwp_attach (char *args, int from_tty)
350 {
351 /* FIXME: We should probably accept a list of process id's, and
352 attach all of them. */
353 error("Not implemented yet");
354 }
355
356 static void
357 lin_lwp_detach (char *args, int from_tty)
358 {
359 /* FIXME: Provide implementation when we implement lin_lwp_attach. */
360 error ("Not implemented yet");
361 }
362 \f
363
364 struct private_thread_info
365 {
366 int lwpid;
367 };
368
369 /* Return non-zero if TP corresponds to the LWP specified by DATA
370 (which is assumed to be a pointer to a `struct lwp_info'. */
371
372 static int
373 find_lwp_callback (struct thread_info *tp, void *data)
374 {
375 struct lwp_info *lp = data;
376
377 if (tp->private->lwpid == GET_LWP (lp->pid))
378 return 1;
379
380 return 0;
381 }
382
383 /* Resume LP. */
384
385 static int
386 resume_callback (struct lwp_info *lp, void *data)
387 {
388 if (lp->stopped && lp->status == 0)
389 {
390 struct thread_info *tp;
391
392 #if 1
393 /* FIXME: kettenis/2000-08-26: This should really be handled
394 properly by core GDB. */
395
396 tp = find_thread_pid (lp->pid);
397 if (tp == NULL)
398 tp = iterate_over_threads (find_lwp_callback, lp);
399 gdb_assert (tp);
400
401 /* If we were previously stepping the thread, and now continue
402 the thread we must invalidate the stepping range. However,
403 if there is a step_resume breakpoint for this thread, we must
404 preserve the stepping range to make it possible to continue
405 stepping once we hit it. */
406 if (tp->step_range_end && tp->step_resume_breakpoint == NULL)
407 {
408 gdb_assert (lp->step);
409 tp->step_range_start = tp->step_range_end = 0;
410 }
411 #endif
412
413 child_resume (GET_LWP (lp->pid), 0, TARGET_SIGNAL_0);
414 lp->stopped = 0;
415 lp->step = 0;
416 }
417
418 return 0;
419 }
420
421 static void
422 lin_lwp_resume (int pid, int step, enum target_signal signo)
423 {
424 struct lwp_info *lp;
425 int resume_all;
426
427 /* Apparently the interpretation of PID is dependent on STEP: If
428 STEP is non-zero, a specific PID means `step only this process
429 id'. But if STEP is zero, then PID means `continue *all*
430 processes, but give the signal only to this one'. */
431 resume_all = (pid == -1) || !step;
432
433 /* If PID is -1, it's the current inferior that should be
434 handled special. */
435 if (pid == -1)
436 pid = inferior_pid;
437
438 lp = find_lwp_pid (pid);
439 if (lp)
440 {
441 pid = GET_LWP (lp->pid);
442
443 /* Remember if we're stepping. */
444 lp->step = step;
445
446 /* If we have a pending wait status for this thread, there is no
447 point in resuming the process. */
448 if (lp->status)
449 {
450 /* FIXME: What should we do if we are supposed to continue
451 this thread with a signal? */
452 gdb_assert (signo == TARGET_SIGNAL_0);
453 return;
454 }
455
456 /* Mark LWP as not stopped to prevent it from being continued by
457 resume_callback. */
458 lp->stopped = 0;
459 }
460
461 if (resume_all)
462 iterate_over_lwps (resume_callback, NULL);
463
464 child_resume (pid, step, signo);
465 }
466 \f
467
468 /* Send a SIGSTOP to LP. */
469
470 static int
471 stop_callback (struct lwp_info *lp, void *data)
472 {
473 if (! lp->stopped && ! lp->signalled)
474 {
475 int ret;
476
477 ret = kill (GET_LWP (lp->pid), SIGSTOP);
478 gdb_assert (ret == 0);
479
480 lp->signalled = 1;
481 gdb_assert (lp->status == 0);
482 }
483
484 return 0;
485 }
486
487 /* Wait until LP is stopped. */
488
489 static int
490 stop_wait_callback (struct lwp_info *lp, void *data)
491 {
492 if (! lp->stopped && lp->signalled)
493 {
494 pid_t pid;
495 int status;
496
497 gdb_assert (lp->status == 0);
498
499 pid = waitpid (GET_LWP (lp->pid), &status,
500 is_cloned (lp->pid) ? __WCLONE : 0);
501 if (pid == -1 && errno == ECHILD)
502 /* OK, the proccess has disappeared. We'll catch the actual
503 exit event in lin_lwp_wait. */
504 return 0;
505
506 gdb_assert (pid == GET_LWP (lp->pid));
507
508 if (WIFEXITED (status) || WIFSIGNALED (status))
509 {
510 gdb_assert (num_lwps > 1);
511
512 if (in_thread_list (lp->pid))
513 {
514 /* Core GDB cannot deal with us deleting the current
515 thread. */
516 if (lp->pid != inferior_pid)
517 delete_thread (lp->pid);
518 printf_unfiltered ("[%s exited]\n",
519 target_pid_to_str (lp->pid));
520 }
521 if (debug_linux_threads)
522 printf ("%s exited.\n", target_pid_to_str (lp->pid));
523
524 delete_lwp (lp->pid);
525 return 0;
526 }
527
528 gdb_assert (WIFSTOPPED (status));
529 lp->stopped = 1;
530
531 if (WSTOPSIG (status) != SIGSTOP)
532 {
533 if (WSTOPSIG (status) == SIGTRAP
534 && breakpoint_inserted_here_p (read_pc_pid (pid)
535 - DECR_PC_AFTER_BREAK))
536 {
537 /* If a LWP other than the LWP that we're reporting an
538 event for has hit a GDB breakpoint (as opposed to
539 some random trap signal), then just arrange for it to
540 hit it again later. We don't keep the SIGTRAP status
541 and don't forward the SIGTRAP signal to the LWP. We
542 will handle the current event, eventually we will
543 resume all LWPs, and this one will get its breakpoint
544 trap again.
545
546 If we do not do this, then we run the risk that the
547 user will delete or disable the breakpoint, but the
548 thread will have already tripped on it. */
549
550 if (debug_linux_threads)
551 printf ("Tripped breakpoint at %lx in LWP %d"
552 " while waiting for SIGSTOP.\n",
553 (long) read_pc_pid (lp->pid), pid);
554
555 /* Set the PC to before the trap. */
556 if (DECR_PC_AFTER_BREAK)
557 write_pc_pid (read_pc_pid (pid) - DECR_PC_AFTER_BREAK, pid);
558 }
559 else
560 {
561 if (debug_linux_threads)
562 printf ("Received %s in LWP %d while waiting for SIGSTOP.\n",
563 strsignal (WSTOPSIG (status)), pid);
564 /* The thread was stopped with a signal other than
565 SIGSTOP, and didn't accidentiliy trip a breakpoint.
566 Record the wait status. */
567 lp->status = status;
568 }
569 }
570 else
571 {
572 /* We caught the SIGSTOP that we intended to catch, so
573 there's no SIGSTOP pending. */
574 lp->signalled = 0;
575 }
576 }
577
578 return 0;
579 }
580
581 /* Return non-zero if LP has a wait status pending. */
582
583 static int
584 status_callback (struct lwp_info *lp, void *data)
585 {
586 return (lp->status != 0);
587 }
588
589 /* Return non-zero if LP isn't stopped. */
590
591 static int
592 running_callback (struct lwp_info *lp, void *data)
593 {
594 return (lp->stopped == 0);
595 }
596
597 static int
598 lin_lwp_wait (int pid, struct target_waitstatus *ourstatus)
599 {
600 struct lwp_info *lp = NULL;
601 int options = 0;
602 int status = 0;
603
604 /* Make sure SIGCHLD is blocked. */
605 if (! sigismember (&blocked_mask, SIGCHLD))
606 {
607 sigaddset (&blocked_mask, SIGCHLD);
608 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
609 }
610
611 retry:
612
613 /* First check if there is a LWP with a wait status pending. */
614 if (pid == -1)
615 {
616 /* Any LWP will do. */
617 lp = iterate_over_lwps (status_callback, NULL);
618 if (lp)
619 {
620 if (debug_linux_threads)
621 printf ("Using pending wait status for LWP %d.\n",
622 GET_LWP (lp->pid));
623 status = lp->status;
624 lp->status = 0;
625 }
626
627 /* But if we don't fine one, we'll have to wait, and check both
628 cloned and uncloned processes. We start with the cloned
629 processes. */
630 options = __WCLONE | WNOHANG;
631 }
632 else if (is_lwp (pid))
633 {
634 if (debug_linux_threads)
635 printf ("Waiting for specific LWP %d.\n", GET_LWP (pid));
636
637 /* We have a specific LWP to check. */
638 lp = find_lwp_pid (GET_LWP (pid));
639 gdb_assert (lp);
640 status = lp->status;
641 lp->status = 0;
642 if (debug_linux_threads)
643 if (status)
644 printf ("Using pending wait status for LWP %d.\n",
645 GET_LWP (lp->pid));
646
647 /* If we have to wait, take into account whether PID is a cloned
648 process or not. And we have to convert it to something that
649 the layer beneath us can understand. */
650 options = is_cloned (lp->pid) ? __WCLONE : 0;
651 pid = GET_LWP (pid);
652 }
653
654 if (status && lp->signalled)
655 {
656 /* A pending SIGSTOP may interfere with the normal stream of
657 events. In a typical case where interference is a problem,
658 we have a SIGSTOP signal pending for LWP A while
659 single-stepping it, encounter an event in LWP B, and take the
660 pending SIGSTOP while trying to stop LWP A. After processing
661 the event in LWP B, LWP A is continued, and we'll never see
662 the SIGTRAP associated with the last time we were
663 single-stepping LWP A. */
664
665 /* Resume the thread. It should halt immediately returning the
666 pending SIGSTOP. */
667 child_resume (GET_LWP (lp->pid), lp->step, TARGET_SIGNAL_0);
668 lp->stopped = 0;
669
670 /* This should catch the pending SIGSTOP. */
671 stop_wait_callback (lp, NULL);
672 }
673
674 set_sigint_trap (); /* Causes SIGINT to be passed on to the
675 attached process. */
676 set_sigio_trap ();
677
678 while (status == 0)
679 {
680 pid_t lwpid;
681
682 lwpid = waitpid (pid, &status, options);
683 if (lwpid > 0)
684 {
685 gdb_assert (pid == -1 || lwpid == pid);
686
687 lp = find_lwp_pid (lwpid);
688 if (! lp)
689 {
690 lp = add_lwp (BUILD_LWP (lwpid, inferior_pid));
691 if (threaded)
692 {
693 gdb_assert (WIFSTOPPED (status)
694 && WSTOPSIG (status) == SIGSTOP);
695 lp->signalled = 1;
696
697 if (! in_thread_list (inferior_pid))
698 {
699 inferior_pid = BUILD_LWP (inferior_pid, inferior_pid);
700 add_thread (inferior_pid);
701 }
702
703 add_thread (lp->pid);
704 printf_unfiltered ("[New %s]\n",
705 target_pid_to_str (lp->pid));
706 }
707 }
708
709 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
710 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
711 left in the process. */
712 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
713 {
714 if (in_thread_list (lp->pid))
715 {
716 /* Core GDB cannot deal with us deleting the current
717 thread. */
718 if (lp->pid != inferior_pid)
719 delete_thread (lp->pid);
720 printf_unfiltered ("[%s exited]\n",
721 target_pid_to_str (lp->pid));
722 }
723 if (debug_linux_threads)
724 printf ("%s exited.\n", target_pid_to_str (lp->pid));
725
726 delete_lwp (lp->pid);
727
728 /* Make sure there is at least one thread running. */
729 gdb_assert (iterate_over_lwps (running_callback, NULL));
730
731 /* Discard the event. */
732 status = 0;
733 continue;
734 }
735
736 /* Make sure we don't report a SIGSTOP that we sent
737 ourselves in an attempt to stop an LWP. */
738 if (lp->signalled && WIFSTOPPED (status)
739 && WSTOPSIG (status) == SIGSTOP)
740 {
741 if (debug_linux_threads)
742 printf ("Delayed SIGSTOP caught for %s.\n",
743 target_pid_to_str (lp->pid));
744
745 /* This is a delayed SIGSTOP. */
746 lp->signalled = 0;
747
748 child_resume (GET_LWP (lp->pid), lp->step, TARGET_SIGNAL_0);
749 lp->stopped = 0;
750
751 /* Discard the event. */
752 status = 0;
753 continue;
754 }
755
756 break;
757 }
758
759 if (pid == -1)
760 {
761 /* Alternate between checking cloned and uncloned processes. */
762 options ^= __WCLONE;
763
764 /* And suspend every time we have checked both. */
765 if (options & __WCLONE)
766 sigsuspend (&suspend_mask);
767 }
768
769 /* We shouldn't end up here unless we want to try again. */
770 gdb_assert (status == 0);
771 }
772
773 clear_sigio_trap ();
774 clear_sigint_trap ();
775
776 gdb_assert (lp);
777
778 /* Don't report signals that GDB isn't interested in, such as
779 signals that are neither printed nor stopped upon. Stopping all
780 threads can be a bit time-consuming so if we want decent
781 performance with heavily multi-threaded programs, especially when
782 they're using a high frequency timer, we'd better avoid it if we
783 can. */
784
785 if (WIFSTOPPED (status))
786 {
787 int signo = target_signal_from_host (WSTOPSIG (status));
788
789 if (signal_stop_state (signo) == 0
790 && signal_print_state (signo) == 0
791 && signal_pass_state (signo) == 1)
792 {
793 child_resume (GET_LWP (lp->pid), lp->step, signo);
794 lp->stopped = 0;
795 status = 0;
796 goto retry;
797 }
798 }
799
800 /* This LWP is stopped now. */
801 lp->stopped = 1;
802
803 /* Now stop all other LWP's ... */
804 iterate_over_lwps (stop_callback, NULL);
805
806 /* ... and wait until all of them have reported back that they're no
807 longer running. */
808 iterate_over_lwps (stop_wait_callback, NULL);
809
810 /* If we're not running in "threaded" mode, we'll report the bare
811 process id. */
812
813 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
814 trap_pid = (threaded ? lp->pid : GET_LWP (lp->pid));
815 else
816 trap_pid = 0;
817
818 store_waitstatus (ourstatus, status);
819 return (threaded ? lp->pid : GET_LWP (lp->pid));
820 }
821
822 static int
823 kill_callback (struct lwp_info *lp, void *data)
824 {
825 ptrace (PTRACE_KILL, GET_LWP (lp->pid), 0, 0);
826 return 0;
827 }
828
829 static int
830 kill_wait_callback (struct lwp_info *lp, void *data)
831 {
832 pid_t pid;
833
834 /* We must make sure that there are no pending events (delayed
835 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
836 program doesn't interfere with any following debugging session. */
837
838 /* For cloned processes we must check both with __WCLONE and
839 without, since the exit status of a cloned process isn't reported
840 with __WCLONE. */
841 if (is_cloned (lp->pid))
842 {
843 do
844 {
845 pid = waitpid (GET_LWP (lp->pid), NULL, __WCLONE);
846 }
847 while (pid == GET_LWP (lp->pid));
848
849 gdb_assert (pid == -1 && errno == ECHILD);
850 }
851
852 do
853 {
854 pid = waitpid (GET_LWP (lp->pid), NULL, 0);
855 }
856 while (pid == GET_LWP (lp->pid));
857
858 gdb_assert (pid == -1 && errno == ECHILD);
859 return 0;
860 }
861
862 static void
863 lin_lwp_kill (void)
864 {
865 /* Kill all LWP's ... */
866 iterate_over_lwps (kill_callback, NULL);
867
868 /* ... and wait until we've flushed all events. */
869 iterate_over_lwps (kill_wait_callback, NULL);
870
871 target_mourn_inferior ();
872 }
873
874 static void
875 lin_lwp_create_inferior (char *exec_file, char *allargs, char **env)
876 {
877 struct target_ops *target_beneath;
878
879 init_lwp_list ();
880
881 #if 0
882 target_beneath = find_target_beneath (&lin_lwp_ops);
883 #else
884 target_beneath = &child_ops;
885 #endif
886 target_beneath->to_create_inferior (exec_file, allargs, env);
887 }
888
889 static void
890 lin_lwp_mourn_inferior (void)
891 {
892 struct target_ops *target_beneath;
893
894 init_lwp_list ();
895
896 trap_pid = 0;
897
898 /* Restore the original signal mask. */
899 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
900 sigemptyset (&blocked_mask);
901
902 #if 0
903 target_beneath = find_target_beneath (&lin_lwp_ops);
904 #else
905 target_beneath = &child_ops;
906 #endif
907 target_beneath->to_mourn_inferior ();
908 }
909
910 static void
911 lin_lwp_fetch_registers (int regno)
912 {
913 struct cleanup *old_chain = save_inferior_pid ();
914
915 if (is_lwp (inferior_pid))
916 inferior_pid = GET_LWP (inferior_pid);
917
918 fetch_inferior_registers (regno);
919
920 do_cleanups (old_chain);
921 }
922
923 static void
924 lin_lwp_store_registers (int regno)
925 {
926 struct cleanup *old_chain = save_inferior_pid ();
927
928 if (is_lwp (inferior_pid))
929 inferior_pid = GET_LWP (inferior_pid);
930
931 store_inferior_registers (regno);
932
933 do_cleanups (old_chain);
934 }
935
936 static int
937 lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
938 struct mem_attrib *attrib,
939 struct target_ops *target)
940 {
941 struct cleanup *old_chain = save_inferior_pid ();
942 int xfer;
943
944 if (is_lwp (inferior_pid))
945 inferior_pid = GET_LWP (inferior_pid);
946
947 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
948
949 do_cleanups (old_chain);
950 return xfer;
951 }
952
953 static int
954 lin_lwp_thread_alive (int pid)
955 {
956 gdb_assert (is_lwp (pid));
957
958 errno = 0;
959 ptrace (PTRACE_PEEKUSER, GET_LWP (pid), 0, 0);
960 if (errno)
961 return 0;
962
963 return 1;
964 }
965
966 static char *
967 lin_lwp_pid_to_str (int pid)
968 {
969 static char buf[64];
970
971 if (is_lwp (pid))
972 {
973 snprintf (buf, sizeof (buf), "LWP %d", GET_LWP (pid));
974 return buf;
975 }
976
977 return normal_pid_to_str (pid);
978 }
979
980 static void
981 init_lin_lwp_ops (void)
982 {
983 #if 0
984 lin_lwp_ops.to_open = lin_lwp_open;
985 #endif
986 lin_lwp_ops.to_shortname = "lwp-layer";
987 lin_lwp_ops.to_longname = "lwp-layer";
988 lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
989 lin_lwp_ops.to_attach = lin_lwp_attach;
990 lin_lwp_ops.to_detach = lin_lwp_detach;
991 lin_lwp_ops.to_resume = lin_lwp_resume;
992 lin_lwp_ops.to_wait = lin_lwp_wait;
993 lin_lwp_ops.to_fetch_registers = lin_lwp_fetch_registers;
994 lin_lwp_ops.to_store_registers = lin_lwp_store_registers;
995 lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
996 lin_lwp_ops.to_kill = lin_lwp_kill;
997 lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
998 lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
999 lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
1000 lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
1001 lin_lwp_ops.to_stratum = thread_stratum;
1002 lin_lwp_ops.to_has_thread_control = tc_schedlock;
1003 lin_lwp_ops.to_magic = OPS_MAGIC;
1004 }
1005
1006 static void
1007 sigchld_handler (int signo)
1008 {
1009 /* Do nothing. The only reason for this handler is that it allows
1010 us to use sigsuspend in lin_lwp_wait above to wait for the
1011 arrival of a SIGCHLD. */
1012 }
1013
1014 void
1015 _initialize_lin_lwp (void)
1016 {
1017 struct sigaction action;
1018
1019 extern void thread_db_init (struct target_ops *);
1020
1021 init_lin_lwp_ops ();
1022 add_target (&lin_lwp_ops);
1023 thread_db_init (&lin_lwp_ops);
1024
1025 /* Save the original signal mask. */
1026 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
1027
1028 action.sa_handler = sigchld_handler;
1029 sigemptyset (&action.sa_mask);
1030 action.sa_flags = 0;
1031 sigaction (SIGCHLD, &action, NULL);
1032
1033 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1034 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
1035 sigdelset (&suspend_mask, SIGCHLD);
1036
1037 sigemptyset (&blocked_mask);
1038 }
1039 \f
1040
1041 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
1042 the LinuxThreads library and therefore doesn't really belong here. */
1043
1044 /* Read variable NAME in the target and return its value if found.
1045 Otherwise return zero. It is assumed that the type of the variable
1046 is `int'. */
1047
1048 static int
1049 get_signo (const char *name)
1050 {
1051 struct minimal_symbol *ms;
1052 int signo;
1053
1054 ms = lookup_minimal_symbol (name, NULL, NULL);
1055 if (ms == NULL)
1056 return 0;
1057
1058 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
1059 sizeof (signo)) != 0)
1060 return 0;
1061
1062 return signo;
1063 }
1064
1065 /* Return the set of signals used by the threads library in *SET. */
1066
1067 void
1068 lin_thread_get_thread_signals (sigset_t *set)
1069 {
1070 struct sigaction action;
1071 int restart, cancel;
1072
1073 sigemptyset (set);
1074
1075 restart = get_signo ("__pthread_sig_restart");
1076 if (restart == 0)
1077 return;
1078
1079 cancel = get_signo ("__pthread_sig_cancel");
1080 if (cancel == 0)
1081 return;
1082
1083 sigaddset (set, restart);
1084 sigaddset (set, cancel);
1085
1086 /* The LinuxThreads library makes terminating threads send a special
1087 "cancel" signal instead of SIGCHLD. Make sure we catch those (to
1088 prevent them from terminating GDB itself, which is likely to be
1089 their default action) and treat them the same way as SIGCHLD. */
1090
1091 action.sa_handler = sigchld_handler;
1092 sigemptyset (&action.sa_mask);
1093 action.sa_flags = 0;
1094 sigaction (cancel, &action, NULL);
1095
1096 /* We block the "cancel" signal throughout this code ... */
1097 sigaddset (&blocked_mask, cancel);
1098 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1099
1100 /* ... except during a sigsuspend. */
1101 sigdelset (&suspend_mask, cancel);
1102 }