1 /* Multi-threaded debugging support for Linux (LWP layer).
2 Copyright 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
23 #include "gdb_assert.h"
26 #include <sys/ptrace.h>
29 #include "gdbthread.h"
35 static int debug_lin_lwp
;
36 extern const char *strsignal (int sig
);
38 /* On Linux there are no real LWP's. The closest thing to LWP's are
39 processes sharing the same VM space. A multi-threaded process is
40 basically a group of such processes. However, such a grouping is
41 almost entirely a user-space issue; the kernel doesn't enforce such
42 a grouping at all (this might change in the future). In general,
43 we'll rely on the threads library (i.e. the LinuxThreads library)
44 to provide such a grouping.
46 It is perfectly well possible to write a multi-threaded application
47 without the assistance of a threads library, by using the clone
48 system call directly. This module should be able to give some
49 rudimentary support for debugging such applications if developers
50 specify the CLONE_PTRACE flag in the clone system call, and are
51 using Linux 2.4 or above.
53 Note that there are some peculiarities in Linux that affect this
56 - In general one should specify the __WCLONE flag to waitpid in
57 order to make it report events for any of the cloned processes
58 (and leave it out for the initial process). However, if a cloned
59 process has exited the exit status is only reported if the
60 __WCLONE flag is absent. Linux 2.4 has a __WALL flag, but we
61 cannot use it since GDB must work on older systems too.
63 - When a traced, cloned process exits and is waited for by the
64 debugger, the kernel reassigns it to the original parent and
65 keeps it around as a "zombie". Somehow, the LinuxThreads library
66 doesn't notice this, which leads to the "zombie problem": When
67 debugged a multi-threaded process that spawns a lot of threads
68 will run out of processes, even if the threads exit, because the
69 "zombies" stay around. */
71 /* Structure describing a LWP. */
74 /* The process id of the LWP. This is a combination of the LWP id
75 and overall process id. */
78 /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report
82 /* Non-zero if this LWP is stopped. */
85 /* If non-zero, a pending wait status. */
88 /* Non-zero if we were stepping this LWP. */
91 /* Next LWP in list. */
92 struct lwp_info
*next
;
95 /* List of known LWPs. */
96 static struct lwp_info
*lwp_list
;
98 /* Number of LWPs in the list. */
101 /* Non-zero if we're running in "threaded" mode. */
105 #define GET_LWP(ptid) ptid_get_lwp (ptid)
106 #define GET_PID(ptid) ptid_get_pid (ptid)
107 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
108 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
110 #define is_cloned(pid) (GET_LWP (pid) != GET_PID (pid))
112 /* If the last reported event was a SIGTRAP, this variable is set to
113 the process id of the LWP/thread that got it. */
117 /* This module's target-specific operations. */
118 static struct target_ops lin_lwp_ops
;
120 /* The standard child operations. */
121 extern struct target_ops child_ops
;
123 /* Since we cannot wait (in lin_lwp_wait) for the initial process and
124 any cloned processes with a single call to waitpid, we have to use
125 the WNOHANG flag and call waitpid in a loop. To optimize
126 things a bit we use `sigsuspend' to wake us up when a process has
127 something to report (it will send us a SIGCHLD if it has). To make
128 this work we have to juggle with the signal mask. We save the
129 original signal mask such that we can restore it before creating a
130 new process in order to avoid blocking certain signals in the
131 inferior. We then block SIGCHLD during the waitpid/sigsuspend
134 /* Original signal mask. */
135 static sigset_t normal_mask
;
137 /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
138 _initialize_lin_lwp. */
139 static sigset_t suspend_mask
;
141 /* Signals to block to make that sigsuspend work. */
142 static sigset_t blocked_mask
;
145 /* Prototypes for local functions. */
146 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
149 /* Initialize the list of LWPs. Note that this module, contrary to
150 what GDB's generic threads layer does for its thread list,
151 re-initializes the LWP lists whenever we mourn or detach (which
152 doesn't involve mourning) the inferior. */
157 struct lwp_info
*lp
, *lpnext
;
159 for (lp
= lwp_list
; lp
; lp
= lpnext
)
170 /* Add the LWP specified by PID to the list. If this causes the
171 number of LWPs to become larger than one, go into "threaded" mode.
172 Return a pointer to the structure describing the new LWP. */
174 static struct lwp_info
*
175 add_lwp (ptid_t ptid
)
179 gdb_assert (is_lwp (ptid
));
181 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
183 memset (lp
, 0, sizeof (struct lwp_info
));
195 /* Remove the LWP specified by PID from the list. */
198 delete_lwp (ptid_t ptid
)
200 struct lwp_info
*lp
, *lpprev
;
204 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
205 if (ptid_equal (lp
->ptid
, ptid
))
211 /* We don't go back to "non-threaded" mode if the number of threads
212 becomes less than two. */
216 lpprev
->next
= lp
->next
;
223 /* Return a pointer to the structure describing the LWP corresponding
224 to PID. If no corresponding LWP could be found, return NULL. */
226 static struct lwp_info
*
227 find_lwp_pid (ptid_t ptid
)
233 lwp
= GET_LWP (ptid
);
235 lwp
= GET_PID (ptid
);
237 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
238 if (lwp
== GET_LWP (lp
->ptid
))
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. */
250 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
254 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
255 if ((*callback
) (lp
, data
))
262 /* Implementation of the PREPARE_TO_PROCEED hook for the Linux LWP
265 Note that this implementation is potentially redundant now that
266 default_prepare_to_proceed() has been added. */
269 lin_lwp_prepare_to_proceed (void)
271 if (! ptid_equal (trap_ptid
, null_ptid
)
272 && ! ptid_equal (inferior_ptid
, trap_ptid
))
274 /* Switched over from TRAP_PID. */
275 CORE_ADDR stop_pc
= read_pc ();
278 /* Avoid switching where it wouldn't do any good, i.e. if both
279 threads are at the same breakpoint. */
280 trap_pc
= read_pc_pid (trap_ptid
);
281 if (trap_pc
!= stop_pc
&& breakpoint_here_p (trap_pc
))
283 /* User hasn't deleted the breakpoint. Return non-zero, and
284 switch back to TRAP_PID. */
285 inferior_ptid
= trap_ptid
;
287 /* FIXME: Is this stuff really necessary? */
288 flush_cached_frames ();
289 registers_changed ();
301 lin_lwp_open (char *args
, int from_tty
)
303 push_target (&lin_lwp_ops
);
307 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
308 a message telling the user that a new LWP has been added to the
312 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
316 gdb_assert (is_lwp (ptid
));
319 printf_filtered ("[New %s]\n", target_pid_to_str (ptid
));
321 /* We assume that we're already tracing the initial process. */
322 if (is_cloned (ptid
) && ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
323 error ("Can't attach %s: %s", target_pid_to_str (ptid
), strerror (errno
));
325 lp
= find_lwp_pid (ptid
);
329 if (is_cloned (ptid
))
334 lin_lwp_attach (char *args
, int from_tty
)
338 /* FIXME: We should probably accept a list of process id's, and
339 attach all of them. */
340 child_ops
.to_attach (args
, from_tty
);
342 /* Add the initial process as the first LWP to the list. */
343 lp
= add_lwp (BUILD_LWP (PIDGET (inferior_ptid
), PIDGET (inferior_ptid
)));
345 /* Make sure the initial process is stopped. The user-level threads
346 layer might want to poke around in the inferior, and that won't
347 work if things haven't stabilized yet. */
349 stop_wait_callback (lp
, NULL
);
350 gdb_assert (lp
->status
== 0);
352 /* Fake the SIGSTOP that core GDB expects. */
353 lp
->status
= W_STOPCODE (SIGSTOP
);
357 detach_callback (struct lwp_info
*lp
, void *data
)
359 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
361 if (debug_lin_lwp
&& lp
->status
)
362 fprintf_unfiltered (gdb_stdlog
, "Pending %s for LWP %ld on detach.\n",
363 strsignal (WSTOPSIG (lp
->status
)), GET_LWP (lp
->ptid
));
365 while (lp
->signalled
&& lp
->stopped
)
367 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
368 WSTOPSIG (lp
->status
)) < 0)
369 error ("Can't continue %s: %s", target_pid_to_str (lp
->ptid
),
374 stop_wait_callback (lp
, NULL
);
376 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
379 if (is_cloned (lp
->ptid
))
381 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
382 WSTOPSIG (lp
->status
)) < 0)
383 error ("Can't detach %s: %s", target_pid_to_str (lp
->ptid
),
386 delete_lwp (lp
->ptid
);
393 lin_lwp_detach (char *args
, int from_tty
)
395 iterate_over_lwps (detach_callback
, NULL
);
397 /* Only the initial (uncloned) process should be left right now. */
398 gdb_assert (num_lwps
== 1);
400 trap_ptid
= null_ptid
;
402 /* Destroy LWP info; it's no longer valid. */
405 /* Restore the original signal mask. */
406 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
407 sigemptyset (&blocked_mask
);
409 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
410 child_ops
.to_detach (args
, from_tty
);
414 struct private_thread_info
419 /* Return non-zero if TP corresponds to the LWP specified by DATA
420 (which is assumed to be a pointer to a `struct lwp_info'. */
423 find_lwp_callback (struct thread_info
*tp
, void *data
)
425 struct lwp_info
*lp
= data
;
427 if (tp
->private->lwpid
== GET_LWP (lp
->ptid
))
436 resume_callback (struct lwp_info
*lp
, void *data
)
438 if (lp
->stopped
&& lp
->status
== 0)
440 struct thread_info
*tp
;
443 /* FIXME: kettenis/2000-08-26: This should really be handled
444 properly by core GDB. */
446 tp
= find_thread_pid (lp
->ptid
);
448 tp
= iterate_over_threads (find_lwp_callback
, lp
);
451 /* If we were previously stepping the thread, and now continue
452 the thread we must invalidate the stepping range. However,
453 if there is a step_resume breakpoint for this thread, we must
454 preserve the stepping range to make it possible to continue
455 stepping once we hit it. */
456 if (tp
->step_range_end
&& tp
->step_resume_breakpoint
== NULL
)
458 gdb_assert (lp
->step
);
459 tp
->step_range_start
= tp
->step_range_end
= 0;
463 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), 0, TARGET_SIGNAL_0
);
472 lin_lwp_resume (ptid_t ptid
, int step
, enum target_signal signo
)
477 /* Apparently the interpretation of PID is dependent on STEP: If
478 STEP is non-zero, a specific PID means `step only this process
479 id'. But if STEP is zero, then PID means `continue *all*
480 processes, but give the signal only to this one'. */
481 resume_all
= (PIDGET (ptid
) == -1) || !step
;
483 /* If PID is -1, it's the current inferior that should be
485 if (PIDGET (ptid
) == -1)
486 ptid
= inferior_ptid
;
488 lp
= find_lwp_pid (ptid
);
491 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
493 /* Remember if we're stepping. */
496 /* If we have a pending wait status for this thread, there is no
497 point in resuming the process. */
500 /* FIXME: What should we do if we are supposed to continue
501 this thread with a signal? */
502 gdb_assert (signo
== TARGET_SIGNAL_0
);
506 /* Mark LWP as not stopped to prevent it from being continued by
512 iterate_over_lwps (resume_callback
, NULL
);
514 child_resume (ptid
, step
, signo
);
518 /* Send a SIGSTOP to LP. */
521 stop_callback (struct lwp_info
*lp
, void *data
)
523 if (! lp
->stopped
&& ! lp
->signalled
)
527 ret
= kill (GET_LWP (lp
->ptid
), SIGSTOP
);
528 gdb_assert (ret
== 0);
531 gdb_assert (lp
->status
== 0);
537 /* Wait until LP is stopped. */
540 stop_wait_callback (struct lwp_info
*lp
, void *data
)
542 if (! lp
->stopped
&& lp
->signalled
)
547 gdb_assert (lp
->status
== 0);
549 pid
= waitpid (GET_LWP (lp
->ptid
), &status
,
550 is_cloned (lp
->ptid
) ? __WCLONE
: 0);
551 if (pid
== -1 && errno
== ECHILD
)
552 /* OK, the proccess has disappeared. We'll catch the actual
553 exit event in lin_lwp_wait. */
556 gdb_assert (pid
== GET_LWP (lp
->ptid
));
558 if (WIFEXITED (status
) || WIFSIGNALED (status
))
560 gdb_assert (num_lwps
> 1);
562 if (in_thread_list (lp
->ptid
))
564 /* Core GDB cannot deal with us deleting the current
566 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
567 delete_thread (lp
->ptid
);
568 printf_unfiltered ("[%s exited]\n",
569 target_pid_to_str (lp
->ptid
));
572 fprintf_unfiltered (gdb_stdlog
,
573 "%s exited.\n", target_pid_to_str (lp
->ptid
));
575 delete_lwp (lp
->ptid
);
579 gdb_assert (WIFSTOPPED (status
));
582 if (WSTOPSIG (status
) != SIGSTOP
)
584 if (WSTOPSIG (status
) == SIGTRAP
585 && breakpoint_inserted_here_p (read_pc_pid (pid_to_ptid (pid
))
586 - DECR_PC_AFTER_BREAK
))
588 /* If a LWP other than the LWP that we're reporting an
589 event for has hit a GDB breakpoint (as opposed to
590 some random trap signal), then just arrange for it to
591 hit it again later. We don't keep the SIGTRAP status
592 and don't forward the SIGTRAP signal to the LWP. We
593 will handle the current event, eventually we will
594 resume all LWPs, and this one will get its breakpoint
597 If we do not do this, then we run the risk that the
598 user will delete or disable the breakpoint, but the
599 thread will have already tripped on it. */
602 fprintf_unfiltered (gdb_stdlog
,
603 "Tripped breakpoint at %lx in LWP %d"
604 " while waiting for SIGSTOP.\n",
605 (long) read_pc_pid (lp
->ptid
), pid
);
607 /* Set the PC to before the trap. */
608 if (DECR_PC_AFTER_BREAK
)
609 write_pc_pid (read_pc_pid (pid_to_ptid (pid
))
610 - DECR_PC_AFTER_BREAK
,
613 else if (WSTOPSIG (status
) == SIGINT
&&
614 signal_pass_state (SIGINT
) == 0)
616 /* Since SIGINT gets forwarded to the entire process group
617 (in the case where ^C/BREAK is typed at the tty/console),
618 just ignore all SIGINT events from all lwp's except for
619 the one that was caught by lin_lwp_wait. */
620 ; /* Don't save. Signal will disappear into oblivion. */
625 fprintf_unfiltered (gdb_stdlog
,
626 "Received %s in LWP %d while waiting for SIGSTOP.\n",
627 strsignal (WSTOPSIG (status
)), pid
);
629 /* The thread was stopped with a signal other than
630 SIGSTOP, and didn't accidentally trip a breakpoint.
631 Record the wait status. */
637 /* We caught the SIGSTOP that we intended to catch, so
638 there's no SIGSTOP pending. */
646 /* Return non-zero if LP has a wait status pending. */
649 status_callback (struct lwp_info
*lp
, void *data
)
651 return (lp
->status
!= 0);
654 /* Return non-zero if LP isn't stopped. */
657 running_callback (struct lwp_info
*lp
, void *data
)
659 return (lp
->stopped
== 0);
663 lin_lwp_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
665 struct lwp_info
*lp
= NULL
;
668 pid_t pid
= PIDGET (ptid
);
670 /* Make sure SIGCHLD is blocked. */
671 if (! sigismember (&blocked_mask
, SIGCHLD
))
673 sigaddset (&blocked_mask
, SIGCHLD
);
674 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
679 /* First check if there is a LWP with a wait status pending. */
682 /* Any LWP will do. */
683 lp
= iterate_over_lwps (status_callback
, NULL
);
687 fprintf_unfiltered (gdb_stdlog
,
688 "Using pending wait status for LWP %ld.\n",
695 /* But if we don't fine one, we'll have to wait, and check both
696 cloned and uncloned processes. We start with the cloned
698 options
= __WCLONE
| WNOHANG
;
700 else if (is_lwp (ptid
))
703 fprintf_unfiltered (gdb_stdlog
,
704 "Waiting for specific LWP %ld.\n",
707 /* We have a specific LWP to check. */
708 lp
= find_lwp_pid (ptid
);
715 fprintf_unfiltered (gdb_stdlog
,
716 "Using pending wait status for LWP %ld.\n",
719 /* If we have to wait, take into account whether PID is a cloned
720 process or not. And we have to convert it to something that
721 the layer beneath us can understand. */
722 options
= is_cloned (lp
->ptid
) ? __WCLONE
: 0;
723 pid
= GET_LWP (ptid
);
726 if (status
&& lp
->signalled
)
728 /* A pending SIGSTOP may interfere with the normal stream of
729 events. In a typical case where interference is a problem,
730 we have a SIGSTOP signal pending for LWP A while
731 single-stepping it, encounter an event in LWP B, and take the
732 pending SIGSTOP while trying to stop LWP A. After processing
733 the event in LWP B, LWP A is continued, and we'll never see
734 the SIGTRAP associated with the last time we were
735 single-stepping LWP A. */
737 /* Resume the thread. It should halt immediately returning the
739 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
743 /* This should catch the pending SIGSTOP. */
744 stop_wait_callback (lp
, NULL
);
747 set_sigint_trap (); /* Causes SIGINT to be passed on to the
755 lwpid
= waitpid (pid
, &status
, options
);
758 gdb_assert (pid
== -1 || lwpid
== pid
);
760 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
763 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
766 gdb_assert (WIFSTOPPED (status
)
767 && WSTOPSIG (status
) == SIGSTOP
);
770 if (! in_thread_list (inferior_ptid
))
772 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
773 GET_PID (inferior_ptid
));
774 add_thread (inferior_ptid
);
777 add_thread (lp
->ptid
);
778 printf_unfiltered ("[New %s]\n",
779 target_pid_to_str (lp
->ptid
));
783 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
784 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
785 left in the process. */
786 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
788 if (in_thread_list (lp
->ptid
))
790 /* Core GDB cannot deal with us deleting the current
792 if (! ptid_equal (lp
->ptid
, inferior_ptid
))
793 delete_thread (lp
->ptid
);
794 printf_unfiltered ("[%s exited]\n",
795 target_pid_to_str (lp
->ptid
));
798 fprintf_unfiltered (gdb_stdlog
,
800 target_pid_to_str (lp
->ptid
));
802 delete_lwp (lp
->ptid
);
804 /* Make sure there is at least one thread running. */
805 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
807 /* Discard the event. */
812 /* Make sure we don't report a SIGSTOP that we sent
813 ourselves in an attempt to stop an LWP. */
814 if (lp
->signalled
&& WIFSTOPPED (status
)
815 && WSTOPSIG (status
) == SIGSTOP
)
818 fprintf_unfiltered (gdb_stdlog
,
819 "Delayed SIGSTOP caught for %s.\n",
820 target_pid_to_str (lp
->ptid
));
822 /* This is a delayed SIGSTOP. */
825 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
829 /* Discard the event. */
839 /* Alternate between checking cloned and uncloned processes. */
842 /* And suspend every time we have checked both. */
843 if (options
& __WCLONE
)
844 sigsuspend (&suspend_mask
);
847 /* We shouldn't end up here unless we want to try again. */
848 gdb_assert (status
== 0);
852 clear_sigint_trap ();
856 /* Don't report signals that GDB isn't interested in, such as
857 signals that are neither printed nor stopped upon. Stopping all
858 threads can be a bit time-consuming so if we want decent
859 performance with heavily multi-threaded programs, especially when
860 they're using a high frequency timer, we'd better avoid it if we
863 if (WIFSTOPPED (status
))
865 int signo
= target_signal_from_host (WSTOPSIG (status
));
867 if (signal_stop_state (signo
) == 0
868 && signal_print_state (signo
) == 0
869 && signal_pass_state (signo
) == 1)
871 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
, signo
);
878 /* This LWP is stopped now. */
881 /* Now stop all other LWP's ... */
882 iterate_over_lwps (stop_callback
, NULL
);
884 /* ... and wait until all of them have reported back that they're no
886 iterate_over_lwps (stop_wait_callback
, NULL
);
888 /* If we're not running in "threaded" mode, we'll report the bare
891 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
892 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
894 trap_ptid
= null_ptid
;
896 store_waitstatus (ourstatus
, status
);
897 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
901 kill_callback (struct lwp_info
*lp
, void *data
)
903 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
908 kill_wait_callback (struct lwp_info
*lp
, void *data
)
912 /* We must make sure that there are no pending events (delayed
913 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
914 program doesn't interfere with any following debugging session. */
916 /* For cloned processes we must check both with __WCLONE and
917 without, since the exit status of a cloned process isn't reported
919 if (is_cloned (lp
->ptid
))
923 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
925 while (pid
== GET_LWP (lp
->ptid
));
927 gdb_assert (pid
== -1 && errno
== ECHILD
);
932 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
934 while (pid
== GET_LWP (lp
->ptid
));
936 gdb_assert (pid
== -1 && errno
== ECHILD
);
943 /* Kill all LWP's ... */
944 iterate_over_lwps (kill_callback
, NULL
);
946 /* ... and wait until we've flushed all events. */
947 iterate_over_lwps (kill_wait_callback
, NULL
);
949 target_mourn_inferior ();
953 lin_lwp_create_inferior (char *exec_file
, char *allargs
, char **env
)
955 child_ops
.to_create_inferior (exec_file
, allargs
, env
);
959 lin_lwp_mourn_inferior (void)
961 trap_ptid
= null_ptid
;
963 /* Destroy LWP info; it's no longer valid. */
966 /* Restore the original signal mask. */
967 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
968 sigemptyset (&blocked_mask
);
970 child_ops
.to_mourn_inferior ();
974 lin_lwp_fetch_registers (int regno
)
976 struct cleanup
*old_chain
= save_inferior_ptid ();
978 if (is_lwp (inferior_ptid
))
979 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
981 fetch_inferior_registers (regno
);
983 do_cleanups (old_chain
);
987 lin_lwp_store_registers (int regno
)
989 struct cleanup
*old_chain
= save_inferior_ptid ();
991 if (is_lwp (inferior_ptid
))
992 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
994 store_inferior_registers (regno
);
996 do_cleanups (old_chain
);
1000 lin_lwp_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
1001 struct mem_attrib
*attrib
,
1002 struct target_ops
*target
)
1004 struct cleanup
*old_chain
= save_inferior_ptid ();
1007 if (is_lwp (inferior_ptid
))
1008 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
1010 xfer
= child_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
1012 do_cleanups (old_chain
);
1017 lin_lwp_thread_alive (ptid_t ptid
)
1019 gdb_assert (is_lwp (ptid
));
1022 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
1030 lin_lwp_pid_to_str (ptid_t ptid
)
1032 static char buf
[64];
1036 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
1040 return normal_pid_to_str (ptid
);
1044 init_lin_lwp_ops (void)
1047 lin_lwp_ops
.to_open
= lin_lwp_open
;
1049 lin_lwp_ops
.to_shortname
= "lwp-layer";
1050 lin_lwp_ops
.to_longname
= "lwp-layer";
1051 lin_lwp_ops
.to_doc
= "Low level threads support (LWP layer)";
1052 lin_lwp_ops
.to_attach
= lin_lwp_attach
;
1053 lin_lwp_ops
.to_detach
= lin_lwp_detach
;
1054 lin_lwp_ops
.to_resume
= lin_lwp_resume
;
1055 lin_lwp_ops
.to_wait
= lin_lwp_wait
;
1056 lin_lwp_ops
.to_fetch_registers
= lin_lwp_fetch_registers
;
1057 lin_lwp_ops
.to_store_registers
= lin_lwp_store_registers
;
1058 lin_lwp_ops
.to_xfer_memory
= lin_lwp_xfer_memory
;
1059 lin_lwp_ops
.to_kill
= lin_lwp_kill
;
1060 lin_lwp_ops
.to_create_inferior
= lin_lwp_create_inferior
;
1061 lin_lwp_ops
.to_mourn_inferior
= lin_lwp_mourn_inferior
;
1062 lin_lwp_ops
.to_thread_alive
= lin_lwp_thread_alive
;
1063 lin_lwp_ops
.to_pid_to_str
= lin_lwp_pid_to_str
;
1064 lin_lwp_ops
.to_stratum
= thread_stratum
;
1065 lin_lwp_ops
.to_has_thread_control
= tc_schedlock
;
1066 lin_lwp_ops
.to_magic
= OPS_MAGIC
;
1070 sigchld_handler (int signo
)
1072 /* Do nothing. The only reason for this handler is that it allows
1073 us to use sigsuspend in lin_lwp_wait above to wait for the
1074 arrival of a SIGCHLD. */
1078 _initialize_lin_lwp (void)
1080 struct sigaction action
;
1082 extern void thread_db_init (struct target_ops
*);
1084 init_lin_lwp_ops ();
1085 add_target (&lin_lwp_ops
);
1086 thread_db_init (&lin_lwp_ops
);
1088 /* Save the original signal mask. */
1089 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
1091 action
.sa_handler
= sigchld_handler
;
1092 sigemptyset (&action
.sa_mask
);
1093 action
.sa_flags
= 0;
1094 sigaction (SIGCHLD
, &action
, NULL
);
1096 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1097 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
1098 sigdelset (&suspend_mask
, SIGCHLD
);
1100 sigemptyset (&blocked_mask
);
1102 add_show_from_set (add_set_cmd ("lin-lwp", no_class
, var_zinteger
,
1103 (char *) &debug_lin_lwp
,
1104 "Set debugging of linux lwp module.\n\
1105 Enables printf debugging output.\n",
1111 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
1112 the LinuxThreads library and therefore doesn't really belong here. */
1114 /* Read variable NAME in the target and return its value if found.
1115 Otherwise return zero. It is assumed that the type of the variable
1119 get_signo (const char *name
)
1121 struct minimal_symbol
*ms
;
1124 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
1128 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (char *) &signo
,
1129 sizeof (signo
)) != 0)
1135 /* Return the set of signals used by the threads library in *SET. */
1138 lin_thread_get_thread_signals (sigset_t
*set
)
1140 struct sigaction action
;
1141 int restart
, cancel
;
1145 restart
= get_signo ("__pthread_sig_restart");
1149 cancel
= get_signo ("__pthread_sig_cancel");
1153 sigaddset (set
, restart
);
1154 sigaddset (set
, cancel
);
1156 /* The LinuxThreads library makes terminating threads send a special
1157 "cancel" signal instead of SIGCHLD. Make sure we catch those (to
1158 prevent them from terminating GDB itself, which is likely to be
1159 their default action) and treat them the same way as SIGCHLD. */
1161 action
.sa_handler
= sigchld_handler
;
1162 sigemptyset (&action
.sa_mask
);
1163 action
.sa_flags
= 0;
1164 sigaction (cancel
, &action
, NULL
);
1166 /* We block the "cancel" signal throughout this code ... */
1167 sigaddset (&blocked_mask
, cancel
);
1168 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1170 /* ... except during a sigsuspend. */
1171 sigdelset (&suspend_mask
, cancel
);
This page took 0.077839 seconds and 5 git commands to generate.