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. */
106 #define TIDGET(PID) (((PID) & 0x7fffffff) >> 16)
107 #define PIDGET0(PID) (((PID) & 0xffff))
108 #define PIDGET(PID) ((PIDGET0 (PID) == 0xffff) ? -1 : PIDGET0 (PID))
109 #define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16))
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)
118 #define is_cloned(pid) (GET_LWP (pid) != GET_PID (pid))
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. */
125 /* This module's target-specific operations. */
126 static struct target_ops lin_lwp_ops
;
128 /* The standard child operations. */
129 extern struct target_ops child_ops
;
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 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 original 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
142 /* Original signal mask. */
143 static sigset_t normal_mask
;
145 /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
146 _initialize_lin_lwp. */
147 static sigset_t suspend_mask
;
149 /* Signals to block to make that sigsuspend work. */
150 static sigset_t blocked_mask
;
153 /* Prototypes for local functions. */
154 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
157 /* Initialize the list of LWPs. Note that this module, contrary to
158 what GDB's generic threads layer does for its thread list,
159 re-initializes the LWP lists whenever we mourn or detach (which
160 doesn't involve mourning) the inferior. */
165 struct lwp_info
*lp
, *lpnext
;
167 for (lp
= lwp_list
; lp
; lp
= lpnext
)
178 /* Add the LWP specified by PID to the list. If this causes the
179 number of LWPs to become larger than one, go into "threaded" mode.
180 Return a pointer to the structure describing the new LWP. */
182 static struct lwp_info
*
183 add_lwp (ptid_t ptid
)
187 gdb_assert (is_lwp (ptid
));
189 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
191 memset (lp
, 0, sizeof (struct lwp_info
));
203 /* Remove the LWP specified by PID from the list. */
206 delete_lwp (ptid_t ptid
)
208 struct lwp_info
*lp
, *lpprev
;
212 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
213 if (ptid_equal (lp
->ptid
, ptid
))
219 /* We don't go back to "non-threaded" mode if the number of threads
220 becomes less than two. */
224 lpprev
->next
= lp
->next
;
231 /* Return a pointer to the structure describing the LWP corresponding
232 to PID. If no corresponding LWP could be found, return NULL. */
234 static struct lwp_info
*
235 find_lwp_pid (ptid_t ptid
)
241 lwp
= GET_LWP (ptid
);
243 lwp
= GET_PID (ptid
);
245 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
246 if (lwp
== GET_LWP (lp
->ptid
))
252 /* Call CALLBACK with its second argument set to DATA for every LWP in
253 the list. If CALLBACK returns 1 for a particular LWP, return a
254 pointer to the structure describing that LWP immediately.
255 Otherwise return NULL. */
258 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
262 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
263 if ((*callback
) (lp
, data
))
270 /* Implementation of the PREPARE_TO_PROCEED hook for the Linux LWP
273 Note that this implementation is potentially redundant now that
274 default_prepare_to_proceed() has been added. */
277 lin_lwp_prepare_to_proceed (void)
279 if (! ptid_equal (trap_ptid
, null_ptid
)
280 && ! ptid_equal (inferior_ptid
, trap_ptid
))
282 /* Switched over from TRAP_PID. */
283 CORE_ADDR stop_pc
= read_pc ();
286 /* Avoid switching where it wouldn't do any good, i.e. if both
287 threads are at the same breakpoint. */
288 trap_pc
= read_pc_pid (trap_ptid
);
289 if (trap_pc
!= stop_pc
&& breakpoint_here_p (trap_pc
))
291 /* User hasn't deleted the breakpoint. Return non-zero, and
292 switch back to TRAP_PID. */
293 inferior_ptid
= trap_ptid
;
295 /* FIXME: Is this stuff really necessary? */
296 flush_cached_frames ();
297 registers_changed ();
309 lin_lwp_open (char *args
, int from_tty
)
311 push_target (&lin_lwp_ops
);
315 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
316 a message telling the user that a new LWP has been added to the
320 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
324 gdb_assert (is_lwp (ptid
));
327 printf_filtered ("[New %s]\n", target_pid_to_str (ptid
));
329 /* We assume that we're already tracing the initial process. */
330 if (is_cloned (ptid
) && ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
331 error ("Can't attach %s: %s", target_pid_to_str (ptid
), strerror (errno
));
333 lp
= find_lwp_pid (ptid
);
337 if (is_cloned (ptid
))
342 lin_lwp_attach (char *args
, int from_tty
)
346 /* FIXME: We should probably accept a list of process id's, and
347 attach all of them. */
348 child_ops
.to_attach (args
, from_tty
);
350 /* Add the initial process as the first LWP to the list. */
351 lp
= add_lwp (BUILD_LWP (inferior_ptid
, inferior_ptid
));
353 /* Make sure the initial process is stopped. The user-level threads
354 layer might want to poke around in the inferior, and that won't
355 work if things haven't stabilized yet. */
357 stop_wait_callback (lp
, NULL
);
358 gdb_assert (lp
->status
== 0);
360 /* Fake the SIGSTOP that core GDB expects. */
361 lp
->status
= W_STOPCODE (SIGSTOP
);
365 detach_callback (struct lwp_info
*lp
, void *data
)
367 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
369 if (debug_lin_lwp
&& lp
->status
)
370 fprintf_unfiltered (gdb_stdlog
, "Pending %s for LWP %d on detach.\n",
371 strsignal (WSTOPSIG (lp
->status
)), GET_LWP (lp
->ptid
));
373 while (lp
->signalled
&& lp
->stopped
)
375 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
376 WSTOPSIG (lp
->status
)) < 0)
377 error ("Can't continue %s: %s", target_pid_to_str (lp
->ptid
),
382 stop_wait_callback (lp
, NULL
);
384 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
387 if (is_cloned (lp
->ptid
))
389 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
390 WSTOPSIG (lp
->status
)) < 0)
391 error ("Can't detach %s: %s", target_pid_to_str (lp
->ptid
),
394 delete_lwp (lp
->ptid
);
401 lin_lwp_detach (char *args
, int from_tty
)
403 iterate_over_lwps (detach_callback
, NULL
);
405 /* Only the initial (uncloned) process should be left right now. */
406 gdb_assert (num_lwps
== 1);
408 trap_ptid
= null_ptid
;
410 /* Destroy LWP info; it's no longer valid. */
413 /* Restore the original signal mask. */
414 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
415 sigemptyset (&blocked_mask
);
417 inferior_ptid
= GET_PID (inferior_ptid
);
418 child_ops
.to_detach (args
, from_tty
);
422 struct private_thread_info
427 /* Return non-zero if TP corresponds to the LWP specified by DATA
428 (which is assumed to be a pointer to a `struct lwp_info'. */
431 find_lwp_callback (struct thread_info
*tp
, void *data
)
433 struct lwp_info
*lp
= data
;
435 if (tp
->private->lwpid
== GET_LWP (lp
->ptid
))
444 resume_callback (struct lwp_info
*lp
, void *data
)
446 if (lp
->stopped
&& lp
->status
== 0)
448 struct thread_info
*tp
;
451 /* FIXME: kettenis/2000-08-26: This should really be handled
452 properly by core GDB. */
454 tp
= find_thread_pid (lp
->ptid
);
456 tp
= iterate_over_threads (find_lwp_callback
, lp
);
459 /* If we were previously stepping the thread, and now continue
460 the thread we must invalidate the stepping range. However,
461 if there is a step_resume breakpoint for this thread, we must
462 preserve the stepping range to make it possible to continue
463 stepping once we hit it. */
464 if (tp
->step_range_end
&& tp
->step_resume_breakpoint
== NULL
)
466 gdb_assert (lp
->step
);
467 tp
->step_range_start
= tp
->step_range_end
= 0;
471 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), 0, TARGET_SIGNAL_0
);
480 lin_lwp_resume (ptid_t ptid
, int step
, enum target_signal signo
)
485 /* Apparently the interpretation of PID is dependent on STEP: If
486 STEP is non-zero, a specific PID means `step only this process
487 id'. But if STEP is zero, then PID means `continue *all*
488 processes, but give the signal only to this one'. */
489 resume_all
= (PIDGET (ptid
) == -1) || !step
;
491 /* If PID is -1, it's the current inferior that should be
493 if (PIDGET (ptid
) == -1)
494 ptid
= inferior_ptid
;
496 lp
= find_lwp_pid (ptid
);
499 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
501 /* Remember if we're stepping. */
504 /* If we have a pending wait status for this thread, there is no
505 point in resuming the process. */
508 /* FIXME: What should we do if we are supposed to continue
509 this thread with a signal? */
510 gdb_assert (signo
== TARGET_SIGNAL_0
);
514 /* Mark LWP as not stopped to prevent it from being continued by
520 iterate_over_lwps (resume_callback
, NULL
);
522 child_resume (ptid
, step
, signo
);
526 /* Send a SIGSTOP to LP. */
529 stop_callback (struct lwp_info
*lp
, void *data
)
531 if (! lp
->stopped
&& ! lp
->signalled
)
535 ret
= kill (GET_LWP (lp
->ptid
), SIGSTOP
);
536 gdb_assert (ret
== 0);
539 gdb_assert (lp
->status
== 0);
545 /* Wait until LP is stopped. */
548 stop_wait_callback (struct lwp_info
*lp
, void *data
)
550 if (! lp
->stopped
&& lp
->signalled
)
555 gdb_assert (lp
->status
== 0);
557 pid
= waitpid (GET_LWP (lp
->ptid
), &status
,
558 is_cloned (lp
->ptid
) ? __WCLONE
: 0);
559 if (pid
== -1 && errno
== ECHILD
)
560 /* OK, the proccess has disappeared. We'll catch the actual
561 exit event in lin_lwp_wait. */
564 gdb_assert (pid
== GET_LWP (lp
->ptid
));
566 if (WIFEXITED (status
) || WIFSIGNALED (status
))
568 gdb_assert (num_lwps
> 1);
570 if (in_thread_list (lp
->ptid
))
572 /* Core GDB cannot deal with us deleting the current
574 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
575 delete_thread (lp
->ptid
);
576 printf_unfiltered ("[%s exited]\n",
577 target_pid_to_str (lp
->ptid
));
580 fprintf_unfiltered (gdb_stdlog
,
581 "%s exited.\n", target_pid_to_str (lp
->ptid
));
583 delete_lwp (lp
->ptid
);
587 gdb_assert (WIFSTOPPED (status
));
590 if (WSTOPSIG (status
) != SIGSTOP
)
592 if (WSTOPSIG (status
) == SIGTRAP
593 && breakpoint_inserted_here_p (read_pc_pid (pid_to_ptid (pid
))
594 - DECR_PC_AFTER_BREAK
))
596 /* If a LWP other than the LWP that we're reporting an
597 event for has hit a GDB breakpoint (as opposed to
598 some random trap signal), then just arrange for it to
599 hit it again later. We don't keep the SIGTRAP status
600 and don't forward the SIGTRAP signal to the LWP. We
601 will handle the current event, eventually we will
602 resume all LWPs, and this one will get its breakpoint
605 If we do not do this, then we run the risk that the
606 user will delete or disable the breakpoint, but the
607 thread will have already tripped on it. */
610 fprintf_unfiltered (gdb_stdlog
,
611 "Tripped breakpoint at %lx in LWP %d"
612 " while waiting for SIGSTOP.\n",
613 (long) read_pc_pid (lp
->ptid
), pid
);
615 /* Set the PC to before the trap. */
616 if (DECR_PC_AFTER_BREAK
)
617 write_pc_pid (read_pc_pid (pid_to_ptid (pid
))
618 - DECR_PC_AFTER_BREAK
,
624 fprintf_unfiltered (gdb_stdlog
,
625 "Received %s in LWP %d while waiting for SIGSTOP.\n",
626 strsignal (WSTOPSIG (status
)), pid
);
628 /* The thread was stopped with a signal other than
629 SIGSTOP, and didn't accidentiliy trip a breakpoint.
630 Record the wait status. */
636 /* We caught the SIGSTOP that we intended to catch, so
637 there's no SIGSTOP pending. */
645 /* Return non-zero if LP has a wait status pending. */
648 status_callback (struct lwp_info
*lp
, void *data
)
650 return (lp
->status
!= 0);
653 /* Return non-zero if LP isn't stopped. */
656 running_callback (struct lwp_info
*lp
, void *data
)
658 return (lp
->stopped
== 0);
662 lin_lwp_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
664 struct lwp_info
*lp
= NULL
;
667 pid_t pid
= PIDGET (ptid
);
669 /* Make sure SIGCHLD is blocked. */
670 if (! sigismember (&blocked_mask
, SIGCHLD
))
672 sigaddset (&blocked_mask
, SIGCHLD
);
673 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
678 /* First check if there is a LWP with a wait status pending. */
681 /* Any LWP will do. */
682 lp
= iterate_over_lwps (status_callback
, NULL
);
686 fprintf_unfiltered (gdb_stdlog
,
687 "Using pending wait status for LWP %d.\n",
688 (int) GET_LWP (lp
->ptid
));
694 /* But if we don't fine one, we'll have to wait, and check both
695 cloned and uncloned processes. We start with the cloned
697 options
= __WCLONE
| WNOHANG
;
699 else if (is_lwp (ptid
))
702 fprintf_unfiltered (gdb_stdlog
,
703 "Waiting for specific LWP %d.\n",
706 /* We have a specific LWP to check. */
707 lp
= find_lwp_pid (ptid
);
714 fprintf_unfiltered (gdb_stdlog
,
715 "Using pending wait status for LWP %d.\n",
718 /* If we have to wait, take into account whether PID is a cloned
719 process or not. And we have to convert it to something that
720 the layer beneath us can understand. */
721 options
= is_cloned (lp
->ptid
) ? __WCLONE
: 0;
722 pid
= GET_LWP (ptid
);
725 if (status
&& lp
->signalled
)
727 /* A pending SIGSTOP may interfere with the normal stream of
728 events. In a typical case where interference is a problem,
729 we have a SIGSTOP signal pending for LWP A while
730 single-stepping it, encounter an event in LWP B, and take the
731 pending SIGSTOP while trying to stop LWP A. After processing
732 the event in LWP B, LWP A is continued, and we'll never see
733 the SIGTRAP associated with the last time we were
734 single-stepping LWP A. */
736 /* Resume the thread. It should halt immediately returning the
738 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
742 /* This should catch the pending SIGSTOP. */
743 stop_wait_callback (lp
, NULL
);
746 set_sigint_trap (); /* Causes SIGINT to be passed on to the
754 lwpid
= waitpid (pid
, &status
, options
);
757 gdb_assert (pid
== -1 || lwpid
== pid
);
759 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
762 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
765 gdb_assert (WIFSTOPPED (status
)
766 && WSTOPSIG (status
) == SIGSTOP
);
769 if (! in_thread_list (inferior_ptid
))
771 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
772 GET_PID (inferior_ptid
));
773 add_thread (inferior_ptid
);
776 add_thread (lp
->ptid
);
777 printf_unfiltered ("[New %s]\n",
778 target_pid_to_str (lp
->ptid
));
782 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
783 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
784 left in the process. */
785 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
787 if (in_thread_list (lp
->ptid
))
789 /* Core GDB cannot deal with us deleting the current
791 if (! ptid_equal (lp
->ptid
, inferior_ptid
))
792 delete_thread (lp
->ptid
);
793 printf_unfiltered ("[%s exited]\n",
794 target_pid_to_str (lp
->ptid
));
797 fprintf_unfiltered (gdb_stdlog
,
799 target_pid_to_str (lp
->ptid
));
801 delete_lwp (lp
->ptid
);
803 /* Make sure there is at least one thread running. */
804 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
806 /* Discard the event. */
811 /* Make sure we don't report a SIGSTOP that we sent
812 ourselves in an attempt to stop an LWP. */
813 if (lp
->signalled
&& WIFSTOPPED (status
)
814 && WSTOPSIG (status
) == SIGSTOP
)
817 fprintf_unfiltered (gdb_stdlog
,
818 "Delayed SIGSTOP caught for %s.\n",
819 target_pid_to_str (lp
->ptid
));
821 /* This is a delayed SIGSTOP. */
824 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
828 /* Discard the event. */
838 /* Alternate between checking cloned and uncloned processes. */
841 /* And suspend every time we have checked both. */
842 if (options
& __WCLONE
)
843 sigsuspend (&suspend_mask
);
846 /* We shouldn't end up here unless we want to try again. */
847 gdb_assert (status
== 0);
851 clear_sigint_trap ();
855 /* Don't report signals that GDB isn't interested in, such as
856 signals that are neither printed nor stopped upon. Stopping all
857 threads can be a bit time-consuming so if we want decent
858 performance with heavily multi-threaded programs, especially when
859 they're using a high frequency timer, we'd better avoid it if we
862 if (WIFSTOPPED (status
))
864 int signo
= target_signal_from_host (WSTOPSIG (status
));
866 if (signal_stop_state (signo
) == 0
867 && signal_print_state (signo
) == 0
868 && signal_pass_state (signo
) == 1)
870 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
, signo
);
877 /* This LWP is stopped now. */
880 /* Now stop all other LWP's ... */
881 iterate_over_lwps (stop_callback
, NULL
);
883 /* ... and wait until all of them have reported back that they're no
885 iterate_over_lwps (stop_wait_callback
, NULL
);
887 /* If we're not running in "threaded" mode, we'll report the bare
890 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
891 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
893 trap_ptid
= null_ptid
;
895 store_waitstatus (ourstatus
, status
);
896 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
900 kill_callback (struct lwp_info
*lp
, void *data
)
902 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
907 kill_wait_callback (struct lwp_info
*lp
, void *data
)
911 /* We must make sure that there are no pending events (delayed
912 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
913 program doesn't interfere with any following debugging session. */
915 /* For cloned processes we must check both with __WCLONE and
916 without, since the exit status of a cloned process isn't reported
918 if (is_cloned (lp
->ptid
))
922 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
924 while (pid
== GET_LWP (lp
->ptid
));
926 gdb_assert (pid
== -1 && errno
== ECHILD
);
931 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
933 while (pid
== GET_LWP (lp
->ptid
));
935 gdb_assert (pid
== -1 && errno
== ECHILD
);
942 /* Kill all LWP's ... */
943 iterate_over_lwps (kill_callback
, NULL
);
945 /* ... and wait until we've flushed all events. */
946 iterate_over_lwps (kill_wait_callback
, NULL
);
948 target_mourn_inferior ();
952 lin_lwp_create_inferior (char *exec_file
, char *allargs
, char **env
)
954 child_ops
.to_create_inferior (exec_file
, allargs
, env
);
958 lin_lwp_mourn_inferior (void)
960 trap_ptid
= null_ptid
;
962 /* Destroy LWP info; it's no longer valid. */
965 /* Restore the original signal mask. */
966 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
967 sigemptyset (&blocked_mask
);
969 child_ops
.to_mourn_inferior ();
973 lin_lwp_fetch_registers (int regno
)
975 struct cleanup
*old_chain
= save_inferior_ptid ();
977 if (is_lwp (inferior_ptid
))
978 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
980 fetch_inferior_registers (regno
);
982 do_cleanups (old_chain
);
986 lin_lwp_store_registers (int regno
)
988 struct cleanup
*old_chain
= save_inferior_ptid ();
990 if (is_lwp (inferior_ptid
))
991 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
993 store_inferior_registers (regno
);
995 do_cleanups (old_chain
);
999 lin_lwp_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
1000 struct mem_attrib
*attrib
,
1001 struct target_ops
*target
)
1003 struct cleanup
*old_chain
= save_inferior_ptid ();
1006 if (is_lwp (inferior_ptid
))
1007 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
1009 xfer
= child_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
1011 do_cleanups (old_chain
);
1016 lin_lwp_thread_alive (ptid_t ptid
)
1018 gdb_assert (is_lwp (ptid
));
1021 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
1029 lin_lwp_pid_to_str (ptid_t ptid
)
1031 static char buf
[64];
1035 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
1039 return normal_pid_to_str (ptid
);
1043 init_lin_lwp_ops (void)
1046 lin_lwp_ops
.to_open
= lin_lwp_open
;
1048 lin_lwp_ops
.to_shortname
= "lwp-layer";
1049 lin_lwp_ops
.to_longname
= "lwp-layer";
1050 lin_lwp_ops
.to_doc
= "Low level threads support (LWP layer)";
1051 lin_lwp_ops
.to_attach
= lin_lwp_attach
;
1052 lin_lwp_ops
.to_detach
= lin_lwp_detach
;
1053 lin_lwp_ops
.to_resume
= lin_lwp_resume
;
1054 lin_lwp_ops
.to_wait
= lin_lwp_wait
;
1055 lin_lwp_ops
.to_fetch_registers
= lin_lwp_fetch_registers
;
1056 lin_lwp_ops
.to_store_registers
= lin_lwp_store_registers
;
1057 lin_lwp_ops
.to_xfer_memory
= lin_lwp_xfer_memory
;
1058 lin_lwp_ops
.to_kill
= lin_lwp_kill
;
1059 lin_lwp_ops
.to_create_inferior
= lin_lwp_create_inferior
;
1060 lin_lwp_ops
.to_mourn_inferior
= lin_lwp_mourn_inferior
;
1061 lin_lwp_ops
.to_thread_alive
= lin_lwp_thread_alive
;
1062 lin_lwp_ops
.to_pid_to_str
= lin_lwp_pid_to_str
;
1063 lin_lwp_ops
.to_stratum
= thread_stratum
;
1064 lin_lwp_ops
.to_has_thread_control
= tc_schedlock
;
1065 lin_lwp_ops
.to_magic
= OPS_MAGIC
;
1069 sigchld_handler (int signo
)
1071 /* Do nothing. The only reason for this handler is that it allows
1072 us to use sigsuspend in lin_lwp_wait above to wait for the
1073 arrival of a SIGCHLD. */
1077 _initialize_lin_lwp (void)
1079 struct sigaction action
;
1081 extern void thread_db_init (struct target_ops
*);
1083 init_lin_lwp_ops ();
1084 add_target (&lin_lwp_ops
);
1085 thread_db_init (&lin_lwp_ops
);
1087 /* Save the original signal mask. */
1088 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
1090 action
.sa_handler
= sigchld_handler
;
1091 sigemptyset (&action
.sa_mask
);
1092 action
.sa_flags
= 0;
1093 sigaction (SIGCHLD
, &action
, NULL
);
1095 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1096 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
1097 sigdelset (&suspend_mask
, SIGCHLD
);
1099 sigemptyset (&blocked_mask
);
1101 add_show_from_set (add_set_cmd ("lin-lwp", no_class
, var_zinteger
,
1102 (char *) &debug_lin_lwp
,
1103 "Set debugging of linux lwp module.\n\
1104 Enables printf debugging output.\n",
1110 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
1111 the LinuxThreads library and therefore doesn't really belong here. */
1113 /* Read variable NAME in the target and return its value if found.
1114 Otherwise return zero. It is assumed that the type of the variable
1118 get_signo (const char *name
)
1120 struct minimal_symbol
*ms
;
1123 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
1127 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (char *) &signo
,
1128 sizeof (signo
)) != 0)
1134 /* Return the set of signals used by the threads library in *SET. */
1137 lin_thread_get_thread_signals (sigset_t
*set
)
1139 struct sigaction action
;
1140 int restart
, cancel
;
1144 restart
= get_signo ("__pthread_sig_restart");
1148 cancel
= get_signo ("__pthread_sig_cancel");
1152 sigaddset (set
, restart
);
1153 sigaddset (set
, cancel
);
1155 /* The LinuxThreads library makes terminating threads send a special
1156 "cancel" signal instead of SIGCHLD. Make sure we catch those (to
1157 prevent them from terminating GDB itself, which is likely to be
1158 their default action) and treat them the same way as SIGCHLD. */
1160 action
.sa_handler
= sigchld_handler
;
1161 sigemptyset (&action
.sa_mask
);
1162 action
.sa_flags
= 0;
1163 sigaction (cancel
, &action
, NULL
);
1165 /* We block the "cancel" signal throughout this code ... */
1166 sigaddset (&blocked_mask
, cancel
);
1167 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1169 /* ... except during a sigsuspend. */
1170 sigdelset (&suspend_mask
, cancel
);
This page took 0.05262 seconds and 5 git commands to generate.