1 /* Low level interface for debugging GNU/Linux threads for GDB,
3 Copyright 1998, 1999 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This module implements the debugging interface of the linuxthreads package
22 of the glibc. This package implements a simple clone()-based implementation
23 of Posix threads for Linux. To use this module, be sure that you have at
24 least the version of the linuxthreads package that holds the support of
25 GDB (currently 0.8 included in the glibc-2.0.7).
27 Right now, the linuxthreads package does not care of priority scheduling,
28 so, neither this module does; In particular, the threads are resumed
29 in any order, which could lead to different scheduling than the one
30 happening when GDB does not control the execution.
32 The latest point is that ptrace(PT_ATTACH, ...) is intrusive in Linux:
33 When a process is attached, then the attaching process becomes the current
34 parent of the attached process, and the old parent has lost this child.
35 If the old parent does a wait[...](), then this child is no longer
36 considered by the kernel as a child of the old parent, thus leading to
37 results of the call different when the child is attached and when it's not.
39 A fix has been submitted to the Linux community to solve this problem,
40 which consequences are not visible to the application itself, but on the
41 process which may wait() for the completion of the application (mostly,
42 it may consider that the application no longer exists (errno == ECHILD),
43 although it does, and thus being unable to get the exit status and resource
44 usage of the child. If by chance, it is able to wait() for the application
45 after it has died (by receiving first a SIGCHILD, and then doing a wait(),
46 then the exit status and resource usage may be wrong, because the
47 linuxthreads package heavily relies on wait() synchronization to keep
51 #include <sys/types.h> /* for pid_t */
52 #include <sys/ptrace.h> /* for PT_* flags */
53 #include "gdb_wait.h" /* for WUNTRACED and __WCLONE flags */
54 #include <signal.h> /* for struct sigaction and NSIG */
55 #include <sys/utsname.h>
60 #include "gdbthread.h"
62 #include "breakpoint.h"
65 #define PT_ATTACH PTRACE_ATTACH
68 #define PT_KILL PTRACE_KILL
71 #define PT_READ_U PTRACE_PEEKUSR
75 #define LINUXTHREAD_NSIG NSIG
78 #define LINUXTHREAD_NSIG _NSIG
82 extern int child_suppress_run; /* make inftarg.c non-runnable */
83 struct target_ops linuxthreads_ops; /* Forward declaration */
84 extern struct target_ops child_ops; /* target vector for inftarg.c */
86 static CORE_ADDR linuxthreads_handles; /* array of linuxthreads handles */
87 static CORE_ADDR linuxthreads_manager; /* pid of linuxthreads manager thread */
88 static CORE_ADDR linuxthreads_initial; /* pid of linuxthreads initial thread */
89 static CORE_ADDR linuxthreads_debug; /* linuxthreads internal debug flag */
90 static CORE_ADDR linuxthreads_num; /* number of valid handle entries */
92 static int linuxthreads_max; /* Maximum number of linuxthreads.
93 Zero if this executable doesn't use
94 threads, or wasn't linked with a
95 debugger-friendly version of the
96 linuxthreads library. */
98 static int linuxthreads_sizeof_handle; /* size of a linuxthreads handle */
99 static int linuxthreads_offset_descr; /* h_descr offset of the linuxthreads
101 static int linuxthreads_offset_pid; /* p_pid offset of the linuxthreads
104 static int linuxthreads_manager_pid; /* manager pid */
105 static int linuxthreads_initial_pid; /* initial pid */
107 /* These variables form a bag of threads with interesting status. If
108 wait_thread (PID) finds that PID stopped for some interesting
109 reason (i.e. anything other than stopped with SIGSTOP), then it
110 records its status in this queue. linuxthreads_wait and
111 linuxthreads_find_trap extract processes from here. */
112 static int *linuxthreads_wait_pid; /* wait array of pid */
113 static int *linuxthreads_wait_status; /* wait array of status */
114 static int linuxthreads_wait_last; /* index of last valid elt in
115 linuxthreads_wait_{pid,status} */
117 static sigset_t linuxthreads_block_mask; /* sigset without SIGCHLD */
119 static int linuxthreads_step_pid; /* current stepped pid */
120 static int linuxthreads_step_signo; /* current stepped target signal */
121 static int linuxthreads_exit_status; /* exit status of initial thread */
123 static int linuxthreads_inferior_pid; /* temporary internal inferior pid */
124 static int linuxthreads_breakpoint_pid; /* last pid that hit a breakpoint */
125 static int linuxthreads_attach_pending; /* attach command without wait */
127 static int linuxthreads_breakpoints_inserted; /* any breakpoints inserted */
129 /* LinuxThreads uses certain signals for communication between
130 processes; we need to tell GDB to pass them through silently to the
131 inferior. The LinuxThreads library has global variables we can
132 read containing the relevant signal numbers, but since the signal
133 numbers are chosen at run-time, those variables aren't initialized
134 until the shared library's constructors have had a chance to run. */
136 struct linuxthreads_signal {
138 /* The name of the LinuxThreads library variable that contains
139 the signal number. */
142 /* True if this variable must exist for us to debug properly. */
145 /* The variable's address in the inferior, or zero if the
146 LinuxThreads library hasn't been loaded into this inferior yet. */
149 /* The signal number, or zero if we don't know yet (either because
150 we haven't found the variable, or it hasn't been initialized).
151 This is an actual target signal number that you could pass to
152 `kill', not a GDB signal number. */
155 /* GDB's original settings for `stop' and `print' for this signal.
156 We restore them when the user selects a different executable.
157 Invariant: if sig->signal != 0, then sig->{stop,print} contain
158 the original settings. */
162 struct linuxthreads_signal linuxthreads_sig_restart = {
163 "__pthread_sig_restart", 1, 0, 0, 0, 0
165 struct linuxthreads_signal linuxthreads_sig_cancel = {
166 "__pthread_sig_cancel", 1, 0, 0, 0, 0
168 struct linuxthreads_signal linuxthreads_sig_debug = {
169 "__pthread_sig_debug", 0, 0, 0, 0, 0
172 /* Set by thread_db module when it takes over the thread_stratum.
173 In that case we must:
174 a) refrain from turning on the debug signal, and
175 b) refrain from calling add_thread. */
177 int using_thread_db = 0;
179 /* A table of breakpoint locations, one per PID. */
180 static struct linuxthreads_breakpoint {
181 CORE_ADDR pc; /* PC of breakpoint */
182 int pid; /* pid of breakpoint */
183 int step; /* whether the pc has been reached after sstep */
184 } *linuxthreads_breakpoint_zombie; /* Zombie breakpoints array */
185 static int linuxthreads_breakpoint_last; /* Last zombie breakpoint */
187 /* linuxthreads_{insert,remove}_breakpoint pass the breakpoint address
188 to {insert,remove}_breakpoint via this variable, since
189 iterate_active_threads doesn't provide any way to pass values
190 through to the worker function. */
191 static CORE_ADDR linuxthreads_breakpoint_addr;
193 #define REMOVE_BREAKPOINT_ZOMBIE(_i) \
195 if ((_i) < linuxthreads_breakpoint_last) \
196 linuxthreads_breakpoint_zombie[(_i)] = \
197 linuxthreads_breakpoint_zombie[linuxthreads_breakpoint_last]; \
198 linuxthreads_breakpoint_last--; \
203 #ifndef PTRACE_XFER_TYPE
204 #define PTRACE_XFER_TYPE int
206 /* Check to see if the given thread is alive. */
208 linuxthreads_thread_alive (pid)
212 return ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE)0, 0) >= 0 || errno == 0;
215 /* On detach(), find a SIGTRAP status. If stop is non-zero, find a
218 Make sure PID is ready to run, and free of interference from our
219 efforts to debug it (e.g., pending SIGSTOP or SIGTRAP signals). If
220 STOP is zero, just look for a SIGTRAP. If STOP is non-zero, look
221 for a SIGSTOP, too. Return non-zero if PID is alive and ready to
222 run; return zero if PID is dead.
224 PID may or may not be stopped at the moment, and we may or may not
225 have waited for it already. We check the linuxthreads_wait bag in
226 case we've already got a status for it. We may possibly wait for
229 PID may have signals waiting to be delivered. If they're caused by
230 our efforts to debug it, accept them with wait, but don't pass them
231 through to PID. Do pass all other signals through. */
233 linuxthreads_find_trap (pid, stop)
243 /* PID may have any number of signals pending. The kernel will
244 report each of them to us via wait, and then it's up to us to
245 pass them along to the process via ptrace, if we so choose.
247 We need to paw through the whole set until we've found a SIGTRAP
248 (or a SIGSTOP, if `stop' is set). We don't pass the SIGTRAP (or
249 SIGSTOP) through, but we do re-send all the others, so PID will
250 receive them when we resume it. */
251 int *wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
254 /* Look at the pending status */
255 for (i = linuxthreads_wait_last; i >= 0; i--)
256 if (linuxthreads_wait_pid[i] == pid)
258 status = linuxthreads_wait_status[i];
260 /* Delete the i'th member of the table. Since the table is
261 unordered, we can do this simply by copying the table's
262 last element to the i'th position, and shrinking the table
264 if (i < linuxthreads_wait_last)
266 linuxthreads_wait_status[i] =
267 linuxthreads_wait_status[linuxthreads_wait_last];
268 linuxthreads_wait_pid[i] =
269 linuxthreads_wait_pid[linuxthreads_wait_last];
271 linuxthreads_wait_last--;
273 if (!WIFSTOPPED(status)) /* Thread has died */
276 if (WSTOPSIG(status) == SIGTRAP)
283 else if (WSTOPSIG(status) == SIGSTOP)
299 /* Make sure that we'll find what we're looking for. */
310 /* Catch all status until SIGTRAP and optionally SIGSTOP show up. */
313 /* resume the child every time... */
314 child_resume (pid, 1, TARGET_SIGNAL_0);
316 /* loop as long as errno == EINTR:
317 waitpid syscall may be aborted due to GDB receiving a signal.
318 FIXME: EINTR handling should no longer be necessary here, since
319 we now block SIGCHLD except in an explicit sigsuspend call. */
323 rpid = waitpid (pid, &status, __WCLONE);
333 /* There are a few reasons the wait call above may have
334 failed. If the thread manager dies, its children get
335 reparented, and this interferes with GDB waiting for
336 them, in some cases. Another possibility is that the
337 initial thread was not cloned, so calling wait with
338 __WCLONE won't find it. I think neither of these should
339 occur in modern Linux kernels --- they don't seem to in
341 rpid = waitpid (pid, &status, 0);
347 perror_with_name ("find_trap/waitpid");
350 if (!WIFSTOPPED(status)) /* Thread has died */
353 if (WSTOPSIG(status) == SIGTRAP)
354 if (!stop || found_stop)
358 else if (WSTOPSIG(status) != SIGSTOP)
359 wstatus[last++] = status;
369 /* Resend any other signals we noticed to the thread, to be received
370 when we continue it. */
373 kill (pid, WSTOPSIG(wstatus[last]));
379 /* Cleanup stub for save_inferior_pid. */
381 restore_inferior_pid (arg)
384 #if TARGET_PTR_BIT > TARGET_INT_BIT
385 inferior_pid = (int) ((long) arg);
387 inferior_pid = (int) arg;
391 /* Register a cleanup to restore the value of inferior_pid. */
392 static struct cleanup *
395 #if TARGET_PTR_BIT > TARGET_INT_BIT
396 return make_cleanup (restore_inferior_pid, (void *) ((long) inferior_pid));
398 return make_cleanup (restore_inferior_pid, (void *) inferior_pid);
403 sigchld_handler (signo)
406 /* This handler is used to get an EINTR while doing waitpid()
407 when an event is received */
410 /* Have we already collected a wait status for PID in the
411 linuxthreads_wait bag? */
413 linuxthreads_pending_status (pid)
417 for (i = linuxthreads_wait_last; i >= 0; i--)
418 if (linuxthreads_wait_pid[i] == pid)
424 /* Internal linuxthreads signal management */
426 /* Check in OBJFILE for the variable that holds the number for signal SIG.
427 We assume that we've already found other LinuxThreads-ish variables
428 in OBJFILE, so we complain if it's required, but not there.
429 Return true iff things are okay. */
431 find_signal_var (sig, objfile)
432 struct linuxthreads_signal *sig;
433 struct objfile *objfile;
435 struct minimal_symbol *ms = lookup_minimal_symbol (sig->var, NULL, objfile);
441 fprintf_unfiltered (gdb_stderr,
442 "Unable to find linuxthreads symbol \"%s\"\n",
453 sig->addr = SYMBOL_VALUE_ADDRESS (ms);
459 find_all_signal_vars (objfile)
460 struct objfile *objfile;
462 return ( find_signal_var (&linuxthreads_sig_restart, objfile)
463 && find_signal_var (&linuxthreads_sig_cancel, objfile)
464 && find_signal_var (&linuxthreads_sig_debug, objfile));
467 /* A struct complaint isn't appropriate here. */
468 static int complained_cannot_determine_thread_signal_number = 0;
470 /* Check to see if the variable holding the signal number for SIG has
471 been initialized yet. If it has, tell GDB to pass that signal
472 through to the inferior silently. */
474 check_signal_number (sig)
475 struct linuxthreads_signal *sig;
480 /* We already know this signal number. */
484 /* We don't know the variable's address yet. */
487 if (target_read_memory (sig->addr, (char *)&num, sizeof (num))
490 /* If this happens once, it'll probably happen for all the
491 signals, so only complain once. */
492 if (! complained_cannot_determine_thread_signal_number)
493 warning ("Cannot determine thread signal number; "
494 "GDB may report spurious signals.");
495 complained_cannot_determine_thread_signal_number = 1;
500 /* It hasn't been initialized yet. */
503 /* We know sig->signal was zero, and is becoming non-zero, so it's
504 okay to sample GDB's original settings. */
506 sig->stop = signal_stop_update (target_signal_from_host (num), 0);
507 sig->print = signal_print_update (target_signal_from_host (num), 0);
511 check_all_signal_numbers ()
513 /* If this isn't a LinuxThreads program, quit early. */
514 if (! linuxthreads_max)
517 check_signal_number (&linuxthreads_sig_restart);
518 check_signal_number (&linuxthreads_sig_cancel);
519 check_signal_number (&linuxthreads_sig_debug);
521 /* handle linuxthread exit */
522 if (linuxthreads_sig_debug.signal
523 || linuxthreads_sig_restart.signal)
525 struct sigaction sact;
527 sact.sa_handler = sigchld_handler;
528 sigemptyset(&sact.sa_mask);
531 if (linuxthreads_sig_debug.signal > 0)
532 sigaction(linuxthreads_sig_cancel.signal, &sact, NULL);
534 sigaction(linuxthreads_sig_restart.signal, &sact, NULL);
539 /* Restore GDB's original settings for SIG.
540 This should only be called when we're no longer sure if we're
541 talking to an executable that uses LinuxThreads, so we clear the
542 signal number and variable address too. */
545 struct linuxthreads_signal *sig;
550 /* We know sig->signal was non-zero, and is becoming zero, so it's
551 okay to restore GDB's original settings. */
552 signal_stop_update (target_signal_from_host (sig->signal), sig->stop);
553 signal_print_update (target_signal_from_host (sig->signal), sig->print);
560 /* Restore GDB's original settings for all LinuxThreads signals.
561 This should only be called when we're no longer sure if we're
562 talking to an executable that uses LinuxThreads, so we clear the
563 signal number and variable address too. */
565 restore_all_signals ()
567 restore_signal (&linuxthreads_sig_restart);
568 restore_signal (&linuxthreads_sig_cancel);
569 restore_signal (&linuxthreads_sig_debug);
571 /* If it happens again, we should complain again. */
572 complained_cannot_determine_thread_signal_number = 0;
578 /* Apply FUNC to the pid of each active thread. This consults the
579 inferior's handle table to find active threads.
581 If ALL is non-zero, process all threads.
582 If ALL is zero, skip threads with pending status. */
584 iterate_active_threads (func, all)
593 read_memory (linuxthreads_num, (char *)&num, sizeof (int));
595 for (i = 0; i < linuxthreads_max && num > 0; i++)
597 read_memory (linuxthreads_handles +
598 linuxthreads_sizeof_handle * i + linuxthreads_offset_descr,
599 (char *)&descr, sizeof (void *));
603 read_memory (descr + linuxthreads_offset_pid,
604 (char *)&pid, sizeof (pid_t));
605 if (pid > 0 && pid != linuxthreads_manager_pid
606 && (all || (!linuxthreads_pending_status (pid))))
612 /* Insert a thread breakpoint at linuxthreads_breakpoint_addr.
613 This is the worker function for linuxthreads_insert_breakpoint,
614 which passes it to iterate_active_threads. */
616 insert_breakpoint (pid)
621 /* Remove (if any) the positive zombie breakpoint. */
622 for (j = linuxthreads_breakpoint_last; j >= 0; j--)
623 if (linuxthreads_breakpoint_zombie[j].pid == pid)
625 if ((linuxthreads_breakpoint_zombie[j].pc - DECR_PC_AFTER_BREAK
626 == linuxthreads_breakpoint_addr)
627 && !linuxthreads_breakpoint_zombie[j].step)
628 REMOVE_BREAKPOINT_ZOMBIE(j);
633 /* Note that we're about to remove a thread breakpoint at
634 linuxthreads_breakpoint_addr.
636 This is the worker function for linuxthreads_remove_breakpoint,
637 which passes it to iterate_active_threads. The actual work of
638 overwriting the breakpoint instruction is done by
639 child_ops.to_remove_breakpoint; here, we simply create a zombie
640 breakpoint if the thread's PC is pointing at the breakpoint being
643 remove_breakpoint (pid)
648 /* Insert a positive zombie breakpoint (if needed). */
649 for (j = 0; j <= linuxthreads_breakpoint_last; j++)
650 if (linuxthreads_breakpoint_zombie[j].pid == pid)
653 if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
655 CORE_ADDR pc = read_pc_pid (pid);
656 if (linuxthreads_breakpoint_addr == pc - DECR_PC_AFTER_BREAK
657 && j > linuxthreads_breakpoint_last)
659 linuxthreads_breakpoint_zombie[j].pid = pid;
660 linuxthreads_breakpoint_zombie[j].pc = pc;
661 linuxthreads_breakpoint_zombie[j].step = 0;
662 linuxthreads_breakpoint_last++;
672 if (in_thread_list (pid))
674 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
682 /* Resume a thread */
687 if (pid != inferior_pid
688 && in_thread_list (pid)
689 && linuxthreads_thread_alive (pid))
691 if (pid == linuxthreads_step_pid)
693 child_resume (pid, 1, linuxthreads_step_signo);
697 child_resume (pid, 0, TARGET_SIGNAL_0);
702 /* Detach a thread */
707 if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
709 /* Remove pending SIGTRAP and SIGSTOP */
710 linuxthreads_find_trap (pid, 1);
713 detach (TARGET_SIGNAL_0);
714 inferior_pid = linuxthreads_manager_pid;
718 /* Attach a thread */
723 if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) != 0)
724 perror_with_name ("attach_thread");
732 if (pid != inferior_pid)
734 if (in_thread_list (pid))
738 else if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) == 0)
740 if (!linuxthreads_attach_pending)
741 printf_filtered ("[New %s]\n", target_pid_to_str (pid));
743 if (linuxthreads_sig_debug.signal)
745 /* After a new thread in glibc 2.1 signals gdb its existence,
746 it suspends itself and wait for linuxthreads_sig_restart,
747 now we can wake it up. */
748 kill (pid, linuxthreads_sig_restart.signal);
752 perror_with_name ("ptrace in stop_thread");
756 /* Wait for a thread */
764 if (pid != inferior_pid && in_thread_list (pid))
766 /* loop as long as errno == EINTR:
767 waitpid syscall may be aborted if GDB receives a signal.
768 FIXME: EINTR handling should no longer be necessary here, since
769 we now block SIGCHLD except during an explicit sigsuspend call. */
772 /* Get first pid status. */
773 rpid = waitpid(pid, &status, __WCLONE);
783 /* There are two reasons this might have failed:
785 1) PID is the initial thread, which wasn't cloned, so
786 passing the __WCLONE flag to waitpid prevented us from
789 2) The manager thread is the parent of all but the
790 initial thread; if it dies, the children will all be
791 reparented to init, which will wait for them. This means
792 our call to waitpid won't find them.
794 Actually, based on a casual look at the 2.0.36 kernel
795 code, I don't think either of these cases happen. But I
796 don't have things set up for remotely debugging the
797 kernel, so I'm not sure. And perhaps older kernels
799 rpid = waitpid(pid, &status, 0);
804 if (errno != EINTR && linuxthreads_thread_alive (pid))
805 perror_with_name ("wait_thread/waitpid");
807 /* the thread is dead. */
810 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
812 linuxthreads_wait_pid[++linuxthreads_wait_last] = pid;
813 linuxthreads_wait_status[linuxthreads_wait_last] = status;
818 /* Walk through the linuxthreads handles in order to detect all
819 threads and stop them */
821 update_stop_threads (test_pid)
824 struct cleanup *old_chain = NULL;
826 check_all_signal_numbers ();
828 if (linuxthreads_manager_pid == 0)
830 if (linuxthreads_manager)
832 if (test_pid > 0 && test_pid != inferior_pid)
834 old_chain = save_inferior_pid ();
835 inferior_pid = test_pid;
837 read_memory (linuxthreads_manager,
838 (char *)&linuxthreads_manager_pid, sizeof (pid_t));
840 if (linuxthreads_initial)
842 if (test_pid > 0 && test_pid != inferior_pid)
844 old_chain = save_inferior_pid ();
845 inferior_pid = test_pid;
847 read_memory(linuxthreads_initial,
848 (char *)&linuxthreads_initial_pid, sizeof (pid_t));
852 if (linuxthreads_manager_pid != 0)
854 if (old_chain == NULL && test_pid > 0 &&
855 test_pid != inferior_pid && linuxthreads_thread_alive (test_pid))
857 old_chain = save_inferior_pid ();
858 inferior_pid = test_pid;
861 if (linuxthreads_thread_alive (inferior_pid))
865 if (test_pid != linuxthreads_manager_pid
866 && !linuxthreads_pending_status (linuxthreads_manager_pid))
868 stop_thread (linuxthreads_manager_pid);
869 wait_thread (linuxthreads_manager_pid);
871 if (!in_thread_list (test_pid))
873 if (!linuxthreads_attach_pending)
874 printf_filtered ("[New %s]\n",
875 target_pid_to_str (test_pid));
876 add_thread (test_pid);
877 if (linuxthreads_sig_debug.signal
878 && inferior_pid == test_pid)
880 /* After a new thread in glibc 2.1 signals gdb its
881 existence, it suspends itself and wait for
882 linuxthreads_sig_restart, now we can wake it up. */
883 kill (test_pid, linuxthreads_sig_restart.signal);
887 iterate_active_threads (stop_thread, 0);
888 iterate_active_threads (wait_thread, 0);
892 if (old_chain != NULL)
893 do_cleanups (old_chain);
896 /* This routine is called whenever a new symbol table is read in, or
897 when all symbol tables are removed. linux-thread event handling
898 can only be initialized when we find the right variables in
899 libpthread.so. Since it's a shared library, those variables don't
900 show up until the library gets mapped and the symbol table is read
903 /* This new_objfile event is now managed by a chained function pointer.
904 * It is the callee's responsability to call the next client on the chain.
907 /* Saved pointer to previous owner of the new_objfile event. */
908 static void (*target_new_objfile_chain) PARAMS ((struct objfile *));
911 linuxthreads_new_objfile (objfile)
912 struct objfile *objfile;
914 struct minimal_symbol *ms;
916 /* Call predecessor on chain, if any.
917 Calling the new module first allows it to dominate,
918 if it finds its compatible libraries. */
920 if (target_new_objfile_chain)
921 target_new_objfile_chain (objfile);
925 /* We're starting an entirely new executable, so we can no
926 longer be sure that it uses LinuxThreads. Restore the signal
927 flags to their original states. */
928 restore_all_signals ();
930 /* Indicate that we don't know anything's address any more. */
931 linuxthreads_max = 0;
936 /* If we've already found our variables in another objfile, don't
937 bother looking for them again. */
938 if (linuxthreads_max)
941 if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL, objfile))
942 /* This object file isn't the pthreads library. */
945 if ((ms = lookup_minimal_symbol ("__pthread_threads_debug",
946 NULL, objfile)) == NULL)
948 /* The debugging-aware libpthreads is not present in this objfile */
950 This program seems to use POSIX threads, but the thread library used\n\
951 does not support debugging. This may make using GDB difficult. Don't\n\
952 set breakpoints or single-step through code that might be executed by\n\
953 any thread other than the main thread.");
956 linuxthreads_debug = SYMBOL_VALUE_ADDRESS (ms);
958 /* Read internal structures configuration */
959 if ((ms = lookup_minimal_symbol ("__pthread_sizeof_handle",
960 NULL, objfile)) == NULL
961 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
962 (char *)&linuxthreads_sizeof_handle,
963 sizeof (linuxthreads_sizeof_handle)) != 0)
965 fprintf_unfiltered (gdb_stderr,
966 "Unable to find linuxthreads symbol \"%s\"\n",
967 "__pthread_sizeof_handle");
971 if ((ms = lookup_minimal_symbol ("__pthread_offsetof_descr",
972 NULL, objfile)) == NULL
973 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
974 (char *)&linuxthreads_offset_descr,
975 sizeof (linuxthreads_offset_descr)) != 0)
977 fprintf_unfiltered (gdb_stderr,
978 "Unable to find linuxthreads symbol \"%s\"\n",
979 "__pthread_offsetof_descr");
983 if ((ms = lookup_minimal_symbol ("__pthread_offsetof_pid",
984 NULL, objfile)) == NULL
985 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
986 (char *)&linuxthreads_offset_pid,
987 sizeof (linuxthreads_offset_pid)) != 0)
989 fprintf_unfiltered (gdb_stderr,
990 "Unable to find linuxthreads symbol \"%s\"\n",
991 "__pthread_offsetof_pid");
995 if (! find_all_signal_vars (objfile))
998 /* Read adresses of internal structures to access */
999 if ((ms = lookup_minimal_symbol ("__pthread_handles",
1000 NULL, objfile)) == NULL)
1002 fprintf_unfiltered (gdb_stderr,
1003 "Unable to find linuxthreads symbol \"%s\"\n",
1004 "__pthread_handles");
1007 linuxthreads_handles = SYMBOL_VALUE_ADDRESS (ms);
1009 if ((ms = lookup_minimal_symbol ("__pthread_handles_num",
1010 NULL, objfile)) == NULL)
1012 fprintf_unfiltered (gdb_stderr,
1013 "Unable to find linuxthreads symbol \"%s\"\n",
1014 "__pthread_handles_num");
1017 linuxthreads_num = SYMBOL_VALUE_ADDRESS (ms);
1019 if ((ms = lookup_minimal_symbol ("__pthread_manager_thread",
1020 NULL, objfile)) == NULL)
1022 fprintf_unfiltered (gdb_stderr,
1023 "Unable to find linuxthreads symbol \"%s\"\n",
1024 "__pthread_manager_thread");
1027 linuxthreads_manager = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
1029 if ((ms = lookup_minimal_symbol ("__pthread_initial_thread",
1030 NULL, objfile)) == NULL)
1032 fprintf_unfiltered (gdb_stderr,
1033 "Unable to find linuxthreads symbol \"%s\"\n",
1034 "__pthread_initial_thread");
1037 linuxthreads_initial = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
1039 /* Search for this last, so it won't be set to a non-zero value unless
1040 we successfully found all the symbols above. */
1041 if ((ms = lookup_minimal_symbol ("__pthread_threads_max",
1042 NULL, objfile)) == NULL
1043 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
1044 (char *)&linuxthreads_max,
1045 sizeof (linuxthreads_max)) != 0)
1047 fprintf_unfiltered (gdb_stderr,
1048 "Unable to find linuxthreads symbol \"%s\"\n",
1049 "__pthread_threads_max");
1053 /* Allocate gdb internal structures */
1054 linuxthreads_wait_pid =
1055 (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
1056 linuxthreads_wait_status =
1057 (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
1058 linuxthreads_breakpoint_zombie = (struct linuxthreads_breakpoint *)
1059 xmalloc (sizeof (struct linuxthreads_breakpoint) * (linuxthreads_max + 1));
1062 !linuxthreads_attach_pending &&
1063 !using_thread_db) /* suppressed by thread_db module */
1067 target_write_memory (linuxthreads_debug, (char *)&on, sizeof (on));
1068 linuxthreads_attach_pending = 1;
1069 update_stop_threads (inferior_pid);
1070 linuxthreads_attach_pending = 0;
1073 check_all_signal_numbers ();
1078 /* If we have switched threads from a one that stopped at breakpoint,
1079 return 1 otherwise 0. */
1082 linuxthreads_prepare_to_proceed (step)
1085 if (!linuxthreads_max
1086 || !linuxthreads_manager_pid
1087 || !linuxthreads_breakpoint_pid
1088 || !breakpoint_here_p (read_pc_pid (linuxthreads_breakpoint_pid)))
1093 /* Mark the current inferior as single stepping process. */
1094 linuxthreads_step_pid = inferior_pid;
1097 linuxthreads_inferior_pid = linuxthreads_breakpoint_pid;
1098 return linuxthreads_breakpoint_pid;
1101 /* Convert a pid to printable form. */
1104 linuxthreads_pid_to_str (pid)
1107 static char buf[100];
1109 sprintf (buf, "%s %d%s", linuxthreads_max ? "Thread" : "Pid", pid,
1110 (pid == linuxthreads_manager_pid) ? " (manager thread)"
1111 : (pid == linuxthreads_initial_pid) ? " (initial thread)"
1117 /* Attach to process PID, then initialize for debugging it
1118 and wait for the trace-trap that results from attaching. */
1121 linuxthreads_attach (args, from_tty)
1126 error_no_arg ("process-id to attach");
1128 push_target (&linuxthreads_ops);
1129 linuxthreads_breakpoints_inserted = 1;
1130 linuxthreads_breakpoint_last = -1;
1131 linuxthreads_wait_last = -1;
1132 linuxthreads_exit_status = __W_STOPCODE(0);
1134 child_ops.to_attach (args, from_tty);
1136 if (linuxthreads_max)
1137 linuxthreads_attach_pending = 1;
1140 /* Take a program previously attached to and detaches it.
1141 The program resumes execution and will no longer stop
1142 on signals, etc. We'd better not have left any breakpoints
1143 in the program or it'll die when it hits one. For this
1144 to work, it may be necessary for the process to have been
1145 previously attached. It *might* work if the program was
1146 started via the normal ptrace (PTRACE_TRACEME). */
1149 linuxthreads_detach (args, from_tty)
1153 if (linuxthreads_max)
1158 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1160 /* Walk through linuxthreads array in order to detach known threads. */
1161 if (linuxthreads_manager_pid != 0)
1163 /* Get rid of all positive zombie breakpoints. */
1164 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1166 if (linuxthreads_breakpoint_zombie[i].step)
1169 pid = linuxthreads_breakpoint_zombie[i].pid;
1170 if (!linuxthreads_thread_alive (pid))
1173 if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (pid))
1176 /* Continue in STEP mode until the thread pc has moved or
1177 until SIGTRAP is found on the same PC. */
1178 if (linuxthreads_find_trap (pid, 0)
1179 && linuxthreads_breakpoint_zombie[i].pc == read_pc_pid (pid))
1180 write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
1181 - DECR_PC_AFTER_BREAK, pid);
1184 /* Detach thread after thread. */
1185 inferior_pid = linuxthreads_manager_pid;
1186 iterate_active_threads (detach_thread, 1);
1188 /* Remove pending SIGTRAP and SIGSTOP */
1189 linuxthreads_find_trap (inferior_pid, 1);
1191 linuxthreads_wait_last = -1;
1192 linuxthreads_exit_status = __W_STOPCODE(0);
1195 linuxthreads_inferior_pid = 0;
1196 linuxthreads_breakpoint_pid = 0;
1197 linuxthreads_step_pid = 0;
1198 linuxthreads_step_signo = TARGET_SIGNAL_0;
1199 linuxthreads_manager_pid = 0;
1200 linuxthreads_initial_pid = 0;
1201 linuxthreads_attach_pending = 0;
1202 init_thread_list (); /* Destroy thread info */
1205 child_ops.to_detach (args, from_tty);
1207 unpush_target (&linuxthreads_ops);
1210 /* Resume execution of process PID. If STEP is nozero, then
1211 just single step it. If SIGNAL is nonzero, restart it with that
1212 signal activated. */
1215 linuxthreads_resume (pid, step, signo)
1218 enum target_signal signo;
1220 if (!linuxthreads_max || stop_soon_quietly || linuxthreads_manager_pid == 0)
1222 child_ops.to_resume (pid, step, signo);
1227 if (linuxthreads_inferior_pid)
1229 /* Prepare resume of the last thread that hit a breakpoint */
1230 linuxthreads_breakpoints_inserted = 0;
1231 rpid = linuxthreads_inferior_pid;
1232 linuxthreads_step_signo = signo;
1236 struct cleanup *old_chain = NULL;
1241 linuxthreads_step_pid = step ? inferior_pid : 0;
1242 linuxthreads_step_signo = signo;
1243 rpid = inferior_pid;
1248 if (pid < 0 || !step)
1250 linuxthreads_breakpoints_inserted = 1;
1252 /* Walk through linuxthreads array in order to resume threads */
1253 if (pid >= 0 && inferior_pid != pid)
1255 old_chain = save_inferior_pid ();
1259 iterate_active_threads (resume_thread, 0);
1260 if (linuxthreads_manager_pid != inferior_pid
1261 && !linuxthreads_pending_status (linuxthreads_manager_pid))
1262 resume_thread (linuxthreads_manager_pid);
1265 linuxthreads_breakpoints_inserted = 0;
1267 /* Deal with zombie breakpoint */
1268 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1269 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1271 if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
1273 /* The current pc is out of zombie breakpoint. */
1274 REMOVE_BREAKPOINT_ZOMBIE(i);
1279 if (old_chain != NULL)
1280 do_cleanups (old_chain);
1283 /* Resume initial thread. */
1284 /* [unles it has a wait event pending] */
1285 if (!linuxthreads_pending_status (rpid))
1287 child_ops.to_resume (rpid, step, signo);
1292 /* Abstract out the child_wait functionality. */
1294 linux_child_wait (pid, rpid, status)
1301 /* Note: inftarg has these inside the loop. */
1302 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1303 attached process. */
1306 errno = save_errno = 0;
1310 *rpid = waitpid (pid, status, __WCLONE | WNOHANG);
1315 /* Got an event -- break out */
1318 if (errno == EINTR) /* interrupted by signal, try again */
1324 *rpid = waitpid (pid, status, WNOHANG);
1327 /* Got an event -- break out */
1334 if (errno != 0 && save_errno != 0)
1338 sigsuspend(&linuxthreads_block_mask);
1340 clear_sigio_trap ();
1341 clear_sigint_trap ();
1343 return errno ? errno : save_errno;
1347 /* Wait for any threads to stop. We may have to convert PID from a thread id
1348 to a LWP id, and vice versa on the way out. */
1351 linuxthreads_wait (pid, ourstatus)
1353 struct target_waitstatus *ourstatus;
1361 if (linuxthreads_max && !linuxthreads_breakpoints_inserted)
1362 wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
1364 /* See if the inferior has chosen values for its signals yet. By
1365 checking for them here, we can be sure we've updated GDB's signal
1366 handling table before the inferior ever gets one of them. (Well,
1367 before we notice, anyway.) */
1368 check_all_signal_numbers ();
1372 if (!linuxthreads_max)
1374 else if (!linuxthreads_breakpoints_inserted)
1376 if (linuxthreads_inferior_pid)
1377 pid = linuxthreads_inferior_pid;
1382 else if (pid < 0 && linuxthreads_wait_last >= 0)
1384 status = linuxthreads_wait_status[linuxthreads_wait_last];
1385 rpid = linuxthreads_wait_pid[linuxthreads_wait_last--];
1387 else if (pid > 0 && linuxthreads_pending_status (pid))
1389 for (i = linuxthreads_wait_last; i >= 0; i--)
1390 if (linuxthreads_wait_pid[i] == pid)
1396 status = linuxthreads_wait_status[i];
1398 if (i < linuxthreads_wait_last)
1400 linuxthreads_wait_status[i] =
1401 linuxthreads_wait_status[linuxthreads_wait_last];
1402 linuxthreads_wait_pid[i] =
1403 linuxthreads_wait_pid[linuxthreads_wait_last];
1405 linuxthreads_wait_last--;
1415 save_errno = linux_child_wait (pid, &rpid, &status);
1419 if (WIFEXITED(linuxthreads_exit_status))
1421 store_waitstatus (ourstatus, linuxthreads_exit_status);
1422 return inferior_pid;
1427 (gdb_stderr, "Child process unexpectedly missing: %s.\n",
1428 safe_strerror (save_errno));
1429 /* Claim it exited with unknown signal. */
1430 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1431 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1436 /* We have now gotten a new event from waitpid above. */
1438 /* Signals arrive in any order. So get all signals until
1439 SIGTRAP and resend previous ones to be held after. */
1440 if (linuxthreads_max
1441 && !linuxthreads_breakpoints_inserted
1442 && WIFSTOPPED(status))
1443 if (WSTOPSIG(status) == SIGTRAP)
1447 kill (rpid, WSTOPSIG(wstatus[last]));
1450 /* insert negative zombie breakpoint */
1451 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1452 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1454 if (i > linuxthreads_breakpoint_last)
1456 linuxthreads_breakpoint_zombie[i].pid = rpid;
1457 linuxthreads_breakpoint_last++;
1459 linuxthreads_breakpoint_zombie[i].pc = read_pc_pid (rpid);
1460 linuxthreads_breakpoint_zombie[i].step = 1;
1464 if (WSTOPSIG(status) != SIGSTOP)
1466 for (i = 0; i < last; i++)
1467 if (wstatus[i] == status)
1471 wstatus[last++] = status;
1474 child_resume (rpid, 1, TARGET_SIGNAL_0);
1477 if (linuxthreads_inferior_pid)
1478 linuxthreads_inferior_pid = 0;
1481 if (linuxthreads_max && !stop_soon_quietly)
1483 if (linuxthreads_max
1484 && WIFSTOPPED(status)
1485 && WSTOPSIG(status) == SIGSTOP)
1487 /* Skip SIGSTOP signals. */
1488 if (!linuxthreads_pending_status (rpid))
1490 if (linuxthreads_step_pid == rpid)
1492 child_resume (rpid, 1, linuxthreads_step_signo);
1496 child_resume (rpid, 0, TARGET_SIGNAL_0);
1502 /* Do no report exit status of cloned threads. */
1503 if (WIFEXITED(status))
1505 if (rpid == linuxthreads_initial_pid)
1506 linuxthreads_exit_status = status;
1508 /* Remove any zombie breakpoint. */
1509 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1510 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1512 REMOVE_BREAKPOINT_ZOMBIE(i);
1520 /* Deal with zombie breakpoint */
1521 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1522 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1525 if (i <= linuxthreads_breakpoint_last)
1527 /* There is a potential zombie breakpoint */
1528 if (WIFEXITED(status)
1529 || linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
1531 /* The current pc is out of zombie breakpoint. */
1532 REMOVE_BREAKPOINT_ZOMBIE(i);
1534 else if (!linuxthreads_breakpoint_zombie[i].step
1535 && WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
1537 /* This is a real one ==> decrement PC and restart. */
1538 write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
1539 - DECR_PC_AFTER_BREAK, rpid);
1540 if (linuxthreads_step_pid == rpid)
1542 child_resume (rpid, 1, linuxthreads_step_signo);
1546 child_resume (rpid, 0, TARGET_SIGNAL_0);
1552 /* Walk through linuxthreads array in order to stop them */
1553 if (linuxthreads_breakpoints_inserted)
1554 update_stop_threads (rpid);
1557 else if (rpid != inferior_pid)
1560 store_waitstatus (ourstatus, status);
1562 if (linuxthreads_attach_pending && !stop_soon_quietly)
1565 if (!using_thread_db)
1567 target_write_memory (linuxthreads_debug,
1568 (char *) &on, sizeof (on));
1569 update_stop_threads (rpid);
1571 linuxthreads_attach_pending = 0;
1574 if (linuxthreads_breakpoints_inserted
1575 && WIFSTOPPED(status)
1576 && WSTOPSIG(status) == SIGTRAP)
1577 linuxthreads_breakpoint_pid = rpid;
1578 else if (linuxthreads_breakpoint_pid)
1579 linuxthreads_breakpoint_pid = 0;
1585 /* Fork an inferior process, and start debugging it with ptrace. */
1588 linuxthreads_create_inferior (exec_file, allargs, env)
1593 if (!exec_file && !exec_bfd)
1595 error ("No executable file specified.\n\
1596 Use the \"file\" or \"exec-file\" command.");
1600 push_target (&linuxthreads_ops);
1601 linuxthreads_breakpoints_inserted = 1;
1602 linuxthreads_breakpoint_last = -1;
1603 linuxthreads_wait_last = -1;
1604 linuxthreads_exit_status = __W_STOPCODE(0);
1606 if (linuxthreads_max)
1607 linuxthreads_attach_pending = 1;
1609 child_ops.to_create_inferior (exec_file, allargs, env);
1613 linuxthreads_discard_global_state ()
1615 linuxthreads_inferior_pid = 0;
1616 linuxthreads_breakpoint_pid = 0;
1617 linuxthreads_step_pid = 0;
1618 linuxthreads_step_signo = TARGET_SIGNAL_0;
1619 linuxthreads_manager_pid = 0;
1620 linuxthreads_initial_pid = 0;
1621 linuxthreads_attach_pending = 0;
1622 linuxthreads_max = 0;
1625 /* Clean up after the inferior dies. */
1628 linuxthreads_mourn_inferior ()
1630 if (linuxthreads_max)
1633 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1635 linuxthreads_discard_global_state ();
1636 init_thread_list(); /* Destroy thread info */
1639 child_ops.to_mourn_inferior ();
1641 unpush_target (&linuxthreads_ops);
1644 /* Kill the inferior process */
1647 linuxthreads_kill ()
1652 if (inferior_pid == 0)
1655 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1657 /* Remove all threads status. */
1658 inferior_pid = linuxthreads_manager_pid;
1659 iterate_active_threads (kill_thread, 1);
1662 kill_thread (inferior_pid);
1665 /* doing_quit_force solves a real problem, but I think a properly
1666 placed call to catch_errors would do the trick much more cleanly. */
1667 if (doing_quit_force >= 0)
1669 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1671 /* Wait for thread to complete */
1672 while ((rpid = waitpid (-1, &status, __WCLONE)) > 0)
1673 if (!WIFEXITED(status))
1676 while ((rpid = waitpid (-1, &status, 0)) > 0)
1677 if (!WIFEXITED(status))
1681 while ((rpid = waitpid (inferior_pid, &status, 0)) > 0)
1682 if (!WIFEXITED(status))
1683 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
1687 /* Wait for all threads. */
1690 rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
1692 while (rpid > 0 || errno == EINTR);
1693 /* FIXME: should no longer need to handle EINTR here. */
1697 rpid = waitpid (-1, &status, WNOHANG);
1699 while (rpid > 0 || errno == EINTR);
1700 /* FIXME: should no longer need to handle EINTR here. */
1702 linuxthreads_mourn_inferior ();
1705 /* Insert a breakpoint */
1708 linuxthreads_insert_breakpoint (addr, contents_cache)
1710 char *contents_cache;
1712 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1714 linuxthreads_breakpoint_addr = addr;
1715 iterate_active_threads (insert_breakpoint, 1);
1716 insert_breakpoint (linuxthreads_manager_pid);
1719 return child_ops.to_insert_breakpoint (addr, contents_cache);
1722 /* Remove a breakpoint */
1725 linuxthreads_remove_breakpoint (addr, contents_cache)
1727 char *contents_cache;
1729 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1731 linuxthreads_breakpoint_addr = addr;
1732 iterate_active_threads (remove_breakpoint, 1);
1733 remove_breakpoint (linuxthreads_manager_pid);
1736 return child_ops.to_remove_breakpoint (addr, contents_cache);
1739 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1742 linuxthreads_can_run ()
1744 return child_suppress_run;
1749 init_linuxthreads_ops ()
1751 linuxthreads_ops.to_shortname = "linuxthreads";
1752 linuxthreads_ops.to_longname = "LINUX threads and pthread.";
1753 linuxthreads_ops.to_doc = "LINUX threads and pthread support.";
1754 linuxthreads_ops.to_attach = linuxthreads_attach;
1755 linuxthreads_ops.to_detach = linuxthreads_detach;
1756 linuxthreads_ops.to_resume = linuxthreads_resume;
1757 linuxthreads_ops.to_wait = linuxthreads_wait;
1758 linuxthreads_ops.to_kill = linuxthreads_kill;
1759 linuxthreads_ops.to_can_run = linuxthreads_can_run;
1760 linuxthreads_ops.to_stratum = thread_stratum;
1761 linuxthreads_ops.to_insert_breakpoint = linuxthreads_insert_breakpoint;
1762 linuxthreads_ops.to_remove_breakpoint = linuxthreads_remove_breakpoint;
1763 linuxthreads_ops.to_create_inferior = linuxthreads_create_inferior;
1764 linuxthreads_ops.to_mourn_inferior = linuxthreads_mourn_inferior;
1765 linuxthreads_ops.to_thread_alive = linuxthreads_thread_alive;
1766 linuxthreads_ops.to_pid_to_str = linuxthreads_pid_to_str;
1767 linuxthreads_ops.to_magic = OPS_MAGIC;
1771 _initialize_linuxthreads ()
1773 struct sigaction sact;
1774 sigset_t linuxthreads_wait_mask; /* sigset with SIGCHLD */
1776 init_linuxthreads_ops ();
1777 add_target (&linuxthreads_ops);
1778 child_suppress_run = 1;
1780 /* Hook onto the "new_objfile" event.
1781 * If someone else is already hooked onto the event,
1782 * then make sure he will be called after we are.
1784 target_new_objfile_chain = target_new_objfile_hook;
1785 target_new_objfile_hook = linuxthreads_new_objfile;
1787 /* Attach SIGCHLD handler */
1788 sact.sa_handler = sigchld_handler;
1789 sigemptyset (&sact.sa_mask);
1791 sigaction (SIGCHLD, &sact, NULL);
1793 /* initialize SIGCHLD mask */
1794 sigemptyset (&linuxthreads_wait_mask);
1795 sigaddset (&linuxthreads_wait_mask, SIGCHLD);
1797 /* Use SIG_BLOCK to block receipt of SIGCHLD.
1798 The block_mask will allow us to wait for this signal explicitly. */
1799 sigprocmask(SIG_BLOCK,
1800 &linuxthreads_wait_mask,
1801 &linuxthreads_block_mask);
1802 /* Make sure that linuxthreads_block_mask is not blocking SIGCHLD */
1803 sigdelset (&linuxthreads_block_mask, SIGCHLD);