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