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
50 #include <sys/types.h> /* for pid_t */
51 #include <sys/ptrace.h> /* for PT_* flags */
52 #include <sys/wait.h> /* for WUNTRACED and __WCLONE flags */
53 #include <signal.h> /* for struct sigaction and NSIG */
54 #include <sys/utsname.h>
60 #include "gdbthread.h"
63 #include "breakpoint.h"
66 #define PT_ATTACH PTRACE_ATTACH
69 #define PT_KILL PTRACE_KILL
72 #define PT_READ_U PTRACE_PEEKUSR
76 #define LINUXTHREAD_NSIG NSIG
79 #define LINUXTHREAD_NSIG _NSIG
83 extern int child_suppress_run
; /* make inftarg.c non-runnable */
84 struct target_ops linuxthreads_ops
; /* Forward declaration */
85 extern struct target_ops child_ops
; /* target vector for inftarg.c */
87 static CORE_ADDR linuxthreads_handles
; /* array of linuxthreads handles */
88 static CORE_ADDR linuxthreads_manager
; /* pid of linuxthreads manager thread */
89 static CORE_ADDR linuxthreads_initial
; /* pid of linuxthreads initial thread */
90 static CORE_ADDR linuxthreads_debug
; /* linuxthreads internal debug flag */
91 static CORE_ADDR linuxthreads_num
; /* number of valid handle entries */
93 static int linuxthreads_max
; /* Maximum number of linuxthreads.
94 Zero if this executable doesn't use
95 threads, or wasn't linked with a
96 debugger-friendly version of the
97 linuxthreads library. */
99 static int linuxthreads_sizeof_handle
; /* size of a linuxthreads handle */
100 static int linuxthreads_offset_descr
; /* h_descr offset of the linuxthreads
102 static int linuxthreads_offset_pid
; /* p_pid offset of the linuxthreads
105 static int linuxthreads_manager_pid
; /* manager pid */
106 static int linuxthreads_initial_pid
; /* initial pid */
108 /* These variables form a bag of threads with interesting status. If
109 wait_thread (PID) finds that PID stopped for some interesting
110 reason (i.e. anything other than stopped with SIGSTOP), then it
111 records its status in this queue. linuxthreads_wait and
112 linuxthreads_find_trap extract processes from here. */
113 static int *linuxthreads_wait_pid
; /* wait array of pid */
114 static int *linuxthreads_wait_status
; /* wait array of status */
115 static int linuxthreads_wait_last
; /* index of last valid elt in
116 linuxthreads_wait_{pid,status} */
118 static sigset_t linuxthreads_block_mask
; /* sigset without SIGCHLD */
120 static int linuxthreads_step_pid
; /* current stepped pid */
121 static int linuxthreads_step_signo
; /* current stepped target signal */
122 static int linuxthreads_exit_status
; /* exit status of initial thread */
124 static int linuxthreads_inferior_pid
; /* temporary internal inferior pid */
125 static int linuxthreads_breakpoint_pid
; /* last pid that hit a breakpoint */
126 static int linuxthreads_attach_pending
; /* attach command without wait */
128 static int linuxthreads_breakpoints_inserted
; /* any breakpoints inserted */
130 /* LinuxThreads uses certain signals for communication between
131 processes; we need to tell GDB to pass them through silently to the
132 inferior. The LinuxThreads library has global variables we can
133 read containing the relevant signal numbers, but since the signal
134 numbers are chosen at run-time, those variables aren't initialized
135 until the shared library's constructors have had a chance to run. */
137 struct linuxthreads_signal
{
139 /* The name of the LinuxThreads library variable that contains
140 the signal number. */
143 /* True if this variable must exist for us to debug properly. */
146 /* The variable's address in the inferior, or zero if the
147 LinuxThreads library hasn't been loaded into this inferior yet. */
150 /* The signal number, or zero if we don't know yet (either because
151 we haven't found the variable, or it hasn't been initialized).
152 This is an actual target signal number that you could pass to
153 `kill', not a GDB signal number. */
156 /* GDB's original settings for `stop' and `print' for this signal.
157 We restore them when the user selects a different executable.
158 Invariant: if sig->signal != 0, then sig->{stop,print} contain
159 the original settings. */
163 struct linuxthreads_signal linuxthreads_sig_restart
= {
164 "__pthread_sig_restart", 1, 0, 0, 0, 0
166 struct linuxthreads_signal linuxthreads_sig_cancel
= {
167 "__pthread_sig_cancel", 1, 0, 0, 0, 0
169 struct linuxthreads_signal linuxthreads_sig_debug
= {
170 "__pthread_sig_debug", 0, 0, 0, 0, 0
173 /* Set by thread_db module when it takes over the thread_stratum.
174 In that case we must:
175 a) refrain from turning on the debug signal, and
176 b) refrain from calling add_thread. */
178 int using_thread_db
= 0;
180 /* A table of breakpoint locations, one per PID. */
181 static struct linuxthreads_breakpoint
{
182 CORE_ADDR pc
; /* PC of breakpoint */
183 int pid
; /* pid of breakpoint */
184 int step
; /* whether the pc has been reached after sstep */
185 } *linuxthreads_breakpoint_zombie
; /* Zombie breakpoints array */
186 static int linuxthreads_breakpoint_last
; /* Last zombie breakpoint */
188 /* linuxthreads_{insert,remove}_breakpoint pass the breakpoint address
189 to {insert,remove}_breakpoint via this variable, since
190 iterate_active_threads doesn't provide any way to pass values
191 through to the worker function. */
192 static CORE_ADDR linuxthreads_breakpoint_addr
;
194 #define REMOVE_BREAKPOINT_ZOMBIE(_i) \
196 if ((_i) < linuxthreads_breakpoint_last) \
197 linuxthreads_breakpoint_zombie[(_i)] = \
198 linuxthreads_breakpoint_zombie[linuxthreads_breakpoint_last]; \
199 linuxthreads_breakpoint_last--; \
204 #ifndef PTRACE_XFER_TYPE
205 #define PTRACE_XFER_TYPE int
207 /* Check to see if the given thread is alive. */
209 linuxthreads_thread_alive (pid
)
213 return ptrace (PT_READ_U
, pid
, (PTRACE_ARG3_TYPE
)0, 0) >= 0 || errno
== 0;
216 /* On detach(), find a SIGTRAP status. If stop is non-zero, find a
219 Make sure PID is ready to run, and free of interference from our
220 efforts to debug it (e.g., pending SIGSTOP or SIGTRAP signals). If
221 STOP is zero, just look for a SIGTRAP. If STOP is non-zero, look
222 for a SIGSTOP, too. Return non-zero if PID is alive and ready to
223 run; return zero if PID is dead.
225 PID may or may not be stopped at the moment, and we may or may not
226 have waited for it already. We check the linuxthreads_wait bag in
227 case we've already got a status for it. We may possibly wait for
230 PID may have signals waiting to be delivered. If they're caused by
231 our efforts to debug it, accept them with wait, but don't pass them
232 through to PID. Do pass all other signals through. */
234 linuxthreads_find_trap (pid
, stop
)
244 /* PID may have any number of signals pending. The kernel will
245 report each of them to us via wait, and then it's up to us to
246 pass them along to the process via ptrace, if we so choose.
248 We need to paw through the whole set until we've found a SIGTRAP
249 (or a SIGSTOP, if `stop' is set). We don't pass the SIGTRAP (or
250 SIGSTOP) through, but we do re-send all the others, so PID will
251 receive them when we resume it. */
252 int *wstatus
= alloca (LINUXTHREAD_NSIG
* sizeof (int));
255 /* Look at the pending status */
256 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
257 if (linuxthreads_wait_pid
[i
] == pid
)
259 status
= linuxthreads_wait_status
[i
];
261 /* Delete the i'th member of the table. Since the table is
262 unordered, we can do this simply by copying the table's
263 last element to the i'th position, and shrinking the table
265 if (i
< linuxthreads_wait_last
)
267 linuxthreads_wait_status
[i
] =
268 linuxthreads_wait_status
[linuxthreads_wait_last
];
269 linuxthreads_wait_pid
[i
] =
270 linuxthreads_wait_pid
[linuxthreads_wait_last
];
272 linuxthreads_wait_last
--;
274 if (!WIFSTOPPED(status
)) /* Thread has died */
277 if (WSTOPSIG(status
) == SIGTRAP
)
284 else if (WSTOPSIG(status
) == SIGSTOP
)
300 /* Make sure that we'll find what we're looking for. */
311 /* Catch all status until SIGTRAP and optionally SIGSTOP show up. */
314 /* resume the child every time... */
315 child_resume (pid
, 1, TARGET_SIGNAL_0
);
317 /* loop as long as errno == EINTR:
318 waitpid syscall may be aborted due to GDB receiving a signal.
319 FIXME: EINTR handling should no longer be necessary here, since
320 we now block SIGCHLD except in an explicit sigsuspend call. */
324 rpid
= waitpid (pid
, &status
, __WCLONE
);
334 /* There are a few reasons the wait call above may have
335 failed. If the thread manager dies, its children get
336 reparented, and this interferes with GDB waiting for
337 them, in some cases. Another possibility is that the
338 initial thread was not cloned, so calling wait with
339 __WCLONE won't find it. I think neither of these should
340 occur in modern Linux kernels --- they don't seem to in
342 rpid
= waitpid (pid
, &status
, 0);
348 perror_with_name ("find_trap/waitpid");
351 if (!WIFSTOPPED(status
)) /* Thread has died */
354 if (WSTOPSIG(status
) == SIGTRAP
)
355 if (!stop
|| found_stop
)
359 else if (WSTOPSIG(status
) != SIGSTOP
)
360 wstatus
[last
++] = status
;
370 /* Resend any other signals we noticed to the thread, to be received
371 when we continue it. */
374 kill (pid
, WSTOPSIG(wstatus
[last
]));
380 /* Cleanup stub for save_inferior_pid. */
382 restore_inferior_pid (arg
)
385 #if TARGET_PTR_BIT > TARGET_INT_BIT
386 inferior_pid
= (int) ((long) arg
);
388 inferior_pid
= (int) arg
;
392 /* Register a cleanup to restore the value of inferior_pid. */
393 static struct cleanup
*
396 #if TARGET_PTR_BIT > TARGET_INT_BIT
397 return make_cleanup (restore_inferior_pid
, (void *) ((long) inferior_pid
));
399 return make_cleanup (restore_inferior_pid
, (void *) inferior_pid
);
404 sigchld_handler (signo
)
407 /* This handler is used to get an EINTR while doing waitpid()
408 when an event is received */
411 /* Have we already collected a wait status for PID in the
412 linuxthreads_wait bag? */
414 linuxthreads_pending_status (pid
)
418 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
419 if (linuxthreads_wait_pid
[i
] == pid
)
425 /* Internal linuxthreads signal management */
427 /* Check in OBJFILE for the variable that holds the number for signal SIG.
428 We assume that we've already found other LinuxThreads-ish variables
429 in OBJFILE, so we complain if it's required, but not there.
430 Return true iff things are okay. */
432 find_signal_var (sig
, objfile
)
433 struct linuxthreads_signal
*sig
;
434 struct objfile
*objfile
;
436 struct minimal_symbol
*ms
= lookup_minimal_symbol (sig
->var
, NULL
, objfile
);
442 fprintf_unfiltered (gdb_stderr
,
443 "Unable to find linuxthreads symbol \"%s\"\n",
454 sig
->addr
= SYMBOL_VALUE_ADDRESS (ms
);
460 find_all_signal_vars (objfile
)
461 struct objfile
*objfile
;
463 return ( find_signal_var (&linuxthreads_sig_restart
, objfile
)
464 && find_signal_var (&linuxthreads_sig_cancel
, objfile
)
465 && find_signal_var (&linuxthreads_sig_debug
, objfile
));
468 /* A struct complaint isn't appropriate here. */
469 static int complained_cannot_determine_thread_signal_number
= 0;
471 /* Check to see if the variable holding the signal number for SIG has
472 been initialized yet. If it has, tell GDB to pass that signal
473 through to the inferior silently. */
475 check_signal_number (sig
)
476 struct linuxthreads_signal
*sig
;
481 /* We already know this signal number. */
485 /* We don't know the variable's address yet. */
488 if (target_read_memory (sig
->addr
, (char *)&num
, sizeof (num
))
491 /* If this happens once, it'll probably happen for all the
492 signals, so only complain once. */
493 if (! complained_cannot_determine_thread_signal_number
)
494 warning ("Cannot determine thread signal number; "
495 "GDB may report spurious signals.");
496 complained_cannot_determine_thread_signal_number
= 1;
501 /* It hasn't been initialized yet. */
504 /* We know sig->signal was zero, and is becoming non-zero, so it's
505 okay to sample GDB's original settings. */
507 sig
->stop
= signal_stop_update (target_signal_from_host (num
), 0);
508 sig
->print
= signal_print_update (target_signal_from_host (num
), 0);
512 check_all_signal_numbers ()
514 /* If this isn't a LinuxThreads program, quit early. */
515 if (! linuxthreads_max
)
518 check_signal_number (&linuxthreads_sig_restart
);
519 check_signal_number (&linuxthreads_sig_cancel
);
520 check_signal_number (&linuxthreads_sig_debug
);
522 /* handle linuxthread exit */
523 if (linuxthreads_sig_debug
.signal
524 || linuxthreads_sig_restart
.signal
)
526 struct sigaction sact
;
528 sact
.sa_handler
= sigchld_handler
;
529 sigemptyset(&sact
.sa_mask
);
532 if (linuxthreads_sig_debug
.signal
> 0)
533 sigaction(linuxthreads_sig_cancel
.signal
, &sact
, NULL
);
535 sigaction(linuxthreads_sig_restart
.signal
, &sact
, NULL
);
540 /* Restore GDB's original settings for SIG.
541 This should only be called when we're no longer sure if we're
542 talking to an executable that uses LinuxThreads, so we clear the
543 signal number and variable address too. */
546 struct linuxthreads_signal
*sig
;
551 /* We know sig->signal was non-zero, and is becoming zero, so it's
552 okay to restore GDB's original settings. */
553 signal_stop_update (target_signal_from_host (sig
->signal
), sig
->stop
);
554 signal_print_update (target_signal_from_host (sig
->signal
), sig
->print
);
561 /* Restore GDB's original settings for all LinuxThreads signals.
562 This should only be called when we're no longer sure if we're
563 talking to an executable that uses LinuxThreads, so we clear the
564 signal number and variable address too. */
566 restore_all_signals ()
568 restore_signal (&linuxthreads_sig_restart
);
569 restore_signal (&linuxthreads_sig_cancel
);
570 restore_signal (&linuxthreads_sig_debug
);
572 /* If it happens again, we should complain again. */
573 complained_cannot_determine_thread_signal_number
= 0;
579 /* Apply FUNC to the pid of each active thread. This consults the
580 inferior's handle table to find active threads.
582 If ALL is non-zero, process all threads.
583 If ALL is zero, skip threads with pending status. */
585 iterate_active_threads (func
, all
)
594 read_memory (linuxthreads_num
, (char *)&num
, sizeof (int));
596 for (i
= 0; i
< linuxthreads_max
&& num
> 0; i
++)
598 read_memory (linuxthreads_handles
+
599 linuxthreads_sizeof_handle
* i
+ linuxthreads_offset_descr
,
600 (char *)&descr
, sizeof (void *));
604 read_memory (descr
+ linuxthreads_offset_pid
,
605 (char *)&pid
, sizeof (pid_t
));
606 if (pid
> 0 && pid
!= linuxthreads_manager_pid
607 && (all
|| (!linuxthreads_pending_status (pid
))))
613 /* Insert a thread breakpoint at linuxthreads_breakpoint_addr.
614 This is the worker function for linuxthreads_insert_breakpoint,
615 which passes it to iterate_active_threads. */
617 insert_breakpoint (pid
)
622 /* Remove (if any) the positive zombie breakpoint. */
623 for (j
= linuxthreads_breakpoint_last
; j
>= 0; j
--)
624 if (linuxthreads_breakpoint_zombie
[j
].pid
== pid
)
626 if ((linuxthreads_breakpoint_zombie
[j
].pc
- DECR_PC_AFTER_BREAK
627 == linuxthreads_breakpoint_addr
)
628 && !linuxthreads_breakpoint_zombie
[j
].step
)
629 REMOVE_BREAKPOINT_ZOMBIE(j
);
634 /* Note that we're about to remove a thread breakpoint at
635 linuxthreads_breakpoint_addr.
637 This is the worker function for linuxthreads_remove_breakpoint,
638 which passes it to iterate_active_threads. The actual work of
639 overwriting the breakpoint instruction is done by
640 child_ops.to_remove_breakpoint; here, we simply create a zombie
641 breakpoint if the thread's PC is pointing at the breakpoint being
644 remove_breakpoint (pid
)
649 /* Insert a positive zombie breakpoint (if needed). */
650 for (j
= 0; j
<= linuxthreads_breakpoint_last
; j
++)
651 if (linuxthreads_breakpoint_zombie
[j
].pid
== pid
)
654 if (in_thread_list (pid
) && linuxthreads_thread_alive (pid
))
656 CORE_ADDR pc
= read_pc_pid (pid
);
657 if (linuxthreads_breakpoint_addr
== pc
- DECR_PC_AFTER_BREAK
658 && j
> linuxthreads_breakpoint_last
)
660 linuxthreads_breakpoint_zombie
[j
].pid
= pid
;
661 linuxthreads_breakpoint_zombie
[j
].pc
= pc
;
662 linuxthreads_breakpoint_zombie
[j
].step
= 0;
663 linuxthreads_breakpoint_last
++;
673 if (in_thread_list (pid
))
675 ptrace (PT_KILL
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
683 /* Resume a thread */
688 if (pid
!= inferior_pid
689 && in_thread_list (pid
)
690 && linuxthreads_thread_alive (pid
))
692 if (pid
== linuxthreads_step_pid
)
694 child_resume (pid
, 1, linuxthreads_step_signo
);
698 child_resume (pid
, 0, TARGET_SIGNAL_0
);
703 /* Detach a thread */
708 if (in_thread_list (pid
) && linuxthreads_thread_alive (pid
))
710 /* Remove pending SIGTRAP and SIGSTOP */
711 linuxthreads_find_trap (pid
, 1);
714 detach (TARGET_SIGNAL_0
);
715 inferior_pid
= linuxthreads_manager_pid
;
719 /* Attach a thread */
724 if (ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0) != 0)
725 perror_with_name ("attach_thread");
733 if (pid
!= inferior_pid
)
735 if (in_thread_list (pid
))
739 else if (ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0) == 0)
741 if (!linuxthreads_attach_pending
)
742 printf_filtered ("[New %s]\n", target_pid_to_str (pid
));
744 if (linuxthreads_sig_debug
.signal
)
746 /* After a new thread in glibc 2.1 signals gdb its existence,
747 it suspends itself and wait for linuxthreads_sig_restart,
748 now we can wake it up. */
749 kill (pid
, linuxthreads_sig_restart
.signal
);
753 perror_with_name ("ptrace in stop_thread");
757 /* Wait for a thread */
765 if (pid
!= inferior_pid
&& in_thread_list (pid
))
767 /* loop as long as errno == EINTR:
768 waitpid syscall may be aborted if GDB receives a signal.
769 FIXME: EINTR handling should no longer be necessary here, since
770 we now block SIGCHLD except during an explicit sigsuspend call. */
773 /* Get first pid status. */
774 rpid
= waitpid(pid
, &status
, __WCLONE
);
784 /* There are two reasons this might have failed:
786 1) PID is the initial thread, which wasn't cloned, so
787 passing the __WCLONE flag to waitpid prevented us from
790 2) The manager thread is the parent of all but the
791 initial thread; if it dies, the children will all be
792 reparented to init, which will wait for them. This means
793 our call to waitpid won't find them.
795 Actually, based on a casual look at the 2.0.36 kernel
796 code, I don't think either of these cases happen. But I
797 don't have things set up for remotely debugging the
798 kernel, so I'm not sure. And perhaps older kernels
800 rpid
= waitpid(pid
, &status
, 0);
805 if (errno
!= EINTR
&& linuxthreads_thread_alive (pid
))
806 perror_with_name ("wait_thread/waitpid");
808 /* the thread is dead. */
811 if (!WIFSTOPPED(status
) || WSTOPSIG(status
) != SIGSTOP
)
813 linuxthreads_wait_pid
[++linuxthreads_wait_last
] = pid
;
814 linuxthreads_wait_status
[linuxthreads_wait_last
] = status
;
819 /* Walk through the linuxthreads handles in order to detect all
820 threads and stop them */
822 update_stop_threads (test_pid
)
825 struct cleanup
*old_chain
= NULL
;
827 check_all_signal_numbers ();
829 if (linuxthreads_manager_pid
== 0)
831 if (linuxthreads_manager
)
833 if (test_pid
> 0 && test_pid
!= inferior_pid
)
835 old_chain
= save_inferior_pid ();
836 inferior_pid
= test_pid
;
838 read_memory (linuxthreads_manager
,
839 (char *)&linuxthreads_manager_pid
, sizeof (pid_t
));
841 if (linuxthreads_initial
)
843 if (test_pid
> 0 && test_pid
!= inferior_pid
)
845 old_chain
= save_inferior_pid ();
846 inferior_pid
= test_pid
;
848 read_memory(linuxthreads_initial
,
849 (char *)&linuxthreads_initial_pid
, sizeof (pid_t
));
853 if (linuxthreads_manager_pid
!= 0)
855 if (old_chain
== NULL
&& test_pid
> 0 &&
856 test_pid
!= inferior_pid
&& linuxthreads_thread_alive (test_pid
))
858 old_chain
= save_inferior_pid ();
859 inferior_pid
= test_pid
;
862 if (linuxthreads_thread_alive (inferior_pid
))
866 if (test_pid
!= linuxthreads_manager_pid
867 && !linuxthreads_pending_status (linuxthreads_manager_pid
))
869 stop_thread (linuxthreads_manager_pid
);
870 wait_thread (linuxthreads_manager_pid
);
872 if (!in_thread_list (test_pid
))
874 if (!linuxthreads_attach_pending
)
875 printf_filtered ("[New %s]\n",
876 target_pid_to_str (test_pid
));
877 add_thread (test_pid
);
878 if (linuxthreads_sig_debug
.signal
879 && inferior_pid
== test_pid
)
881 /* After a new thread in glibc 2.1 signals gdb its
882 existence, it suspends itself and wait for
883 linuxthreads_sig_restart, now we can wake it up. */
884 kill (test_pid
, linuxthreads_sig_restart
.signal
);
888 iterate_active_threads (stop_thread
, 0);
889 iterate_active_threads (wait_thread
, 0);
893 if (old_chain
!= NULL
)
894 do_cleanups (old_chain
);
897 /* This routine is called whenever a new symbol table is read in, or
898 when all symbol tables are removed. linux-thread event handling
899 can only be initialized when we find the right variables in
900 libpthread.so. Since it's a shared library, those variables don't
901 show up until the library gets mapped and the symbol table is read
904 /* This new_objfile event is now managed by a chained function pointer.
905 * It is the callee's responsability to call the next client on the chain.
908 /* Saved pointer to previous owner of the new_objfile event. */
909 static void (*target_new_objfile_chain
) PARAMS ((struct objfile
*));
912 linuxthreads_new_objfile (objfile
)
913 struct objfile
*objfile
;
915 struct minimal_symbol
*ms
;
917 /* Call predecessor on chain, if any.
918 Calling the new module first allows it to dominate,
919 if it finds its compatible libraries. */
921 if (target_new_objfile_chain
)
922 target_new_objfile_chain (objfile
);
926 /* We're starting an entirely new executable, so we can no
927 longer be sure that it uses LinuxThreads. Restore the signal
928 flags to their original states. */
929 restore_all_signals ();
931 /* Indicate that we don't know anything's address any more. */
932 linuxthreads_max
= 0;
937 /* If we've already found our variables in another objfile, don't
938 bother looking for them again. */
939 if (linuxthreads_max
)
942 if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL
, objfile
))
943 /* This object file isn't the pthreads library. */
946 if ((ms
= lookup_minimal_symbol ("__pthread_threads_debug",
947 NULL
, objfile
)) == NULL
)
949 /* The debugging-aware libpthreads is not present in this objfile */
951 This program seems to use POSIX threads, but the thread library used\n\
952 does not support debugging. This may make using GDB difficult. Don't\n\
953 set breakpoints or single-step through code that might be executed by\n\
954 any thread other than the main thread.");
957 linuxthreads_debug
= SYMBOL_VALUE_ADDRESS (ms
);
959 /* Read internal structures configuration */
960 if ((ms
= lookup_minimal_symbol ("__pthread_sizeof_handle",
961 NULL
, objfile
)) == NULL
962 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
963 (char *)&linuxthreads_sizeof_handle
,
964 sizeof (linuxthreads_sizeof_handle
)) != 0)
966 fprintf_unfiltered (gdb_stderr
,
967 "Unable to find linuxthreads symbol \"%s\"\n",
968 "__pthread_sizeof_handle");
972 if ((ms
= lookup_minimal_symbol ("__pthread_offsetof_descr",
973 NULL
, objfile
)) == NULL
974 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
975 (char *)&linuxthreads_offset_descr
,
976 sizeof (linuxthreads_offset_descr
)) != 0)
978 fprintf_unfiltered (gdb_stderr
,
979 "Unable to find linuxthreads symbol \"%s\"\n",
980 "__pthread_offsetof_descr");
984 if ((ms
= lookup_minimal_symbol ("__pthread_offsetof_pid",
985 NULL
, objfile
)) == NULL
986 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
987 (char *)&linuxthreads_offset_pid
,
988 sizeof (linuxthreads_offset_pid
)) != 0)
990 fprintf_unfiltered (gdb_stderr
,
991 "Unable to find linuxthreads symbol \"%s\"\n",
992 "__pthread_offsetof_pid");
996 if (! find_all_signal_vars (objfile
))
999 /* Read adresses of internal structures to access */
1000 if ((ms
= lookup_minimal_symbol ("__pthread_handles",
1001 NULL
, objfile
)) == NULL
)
1003 fprintf_unfiltered (gdb_stderr
,
1004 "Unable to find linuxthreads symbol \"%s\"\n",
1005 "__pthread_handles");
1008 linuxthreads_handles
= SYMBOL_VALUE_ADDRESS (ms
);
1010 if ((ms
= lookup_minimal_symbol ("__pthread_handles_num",
1011 NULL
, objfile
)) == NULL
)
1013 fprintf_unfiltered (gdb_stderr
,
1014 "Unable to find linuxthreads symbol \"%s\"\n",
1015 "__pthread_handles_num");
1018 linuxthreads_num
= SYMBOL_VALUE_ADDRESS (ms
);
1020 if ((ms
= lookup_minimal_symbol ("__pthread_manager_thread",
1021 NULL
, objfile
)) == NULL
)
1023 fprintf_unfiltered (gdb_stderr
,
1024 "Unable to find linuxthreads symbol \"%s\"\n",
1025 "__pthread_manager_thread");
1028 linuxthreads_manager
= SYMBOL_VALUE_ADDRESS (ms
) + linuxthreads_offset_pid
;
1030 if ((ms
= lookup_minimal_symbol ("__pthread_initial_thread",
1031 NULL
, objfile
)) == NULL
)
1033 fprintf_unfiltered (gdb_stderr
,
1034 "Unable to find linuxthreads symbol \"%s\"\n",
1035 "__pthread_initial_thread");
1038 linuxthreads_initial
= SYMBOL_VALUE_ADDRESS (ms
) + linuxthreads_offset_pid
;
1040 /* Search for this last, so it won't be set to a non-zero value unless
1041 we successfully found all the symbols above. */
1042 if ((ms
= lookup_minimal_symbol ("__pthread_threads_max",
1043 NULL
, objfile
)) == NULL
1044 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
1045 (char *)&linuxthreads_max
,
1046 sizeof (linuxthreads_max
)) != 0)
1048 fprintf_unfiltered (gdb_stderr
,
1049 "Unable to find linuxthreads symbol \"%s\"\n",
1050 "__pthread_threads_max");
1054 /* Allocate gdb internal structures */
1055 linuxthreads_wait_pid
=
1056 (int *) xmalloc (sizeof (int) * (linuxthreads_max
+ 1));
1057 linuxthreads_wait_status
=
1058 (int *) xmalloc (sizeof (int) * (linuxthreads_max
+ 1));
1059 linuxthreads_breakpoint_zombie
= (struct linuxthreads_breakpoint
*)
1060 xmalloc (sizeof (struct linuxthreads_breakpoint
) * (linuxthreads_max
+ 1));
1063 !linuxthreads_attach_pending
&&
1064 !using_thread_db
) /* suppressed by thread_db module */
1068 target_write_memory (linuxthreads_debug
, (char *)&on
, sizeof (on
));
1069 linuxthreads_attach_pending
= 1;
1070 update_stop_threads (inferior_pid
);
1071 linuxthreads_attach_pending
= 0;
1074 check_all_signal_numbers ();
1079 /* If we have switched threads from a one that stopped at breakpoint,
1080 return 1 otherwise 0. */
1083 linuxthreads_prepare_to_proceed (step
)
1086 if (!linuxthreads_max
1087 || !linuxthreads_manager_pid
1088 || !linuxthreads_breakpoint_pid
1089 || !breakpoint_here_p (read_pc_pid (linuxthreads_breakpoint_pid
)))
1094 /* Mark the current inferior as single stepping process. */
1095 linuxthreads_step_pid
= inferior_pid
;
1098 linuxthreads_inferior_pid
= linuxthreads_breakpoint_pid
;
1099 return linuxthreads_breakpoint_pid
;
1102 /* Convert a pid to printable form. */
1105 linuxthreads_pid_to_str (pid
)
1108 static char buf
[100];
1110 sprintf (buf
, "%s %d%s", linuxthreads_max
? "Thread" : "Pid", pid
,
1111 (pid
== linuxthreads_manager_pid
) ? " (manager thread)"
1112 : (pid
== linuxthreads_initial_pid
) ? " (initial thread)"
1118 /* Attach to process PID, then initialize for debugging it
1119 and wait for the trace-trap that results from attaching. */
1122 linuxthreads_attach (args
, from_tty
)
1127 error_no_arg ("process-id to attach");
1129 push_target (&linuxthreads_ops
);
1130 linuxthreads_breakpoints_inserted
= 1;
1131 linuxthreads_breakpoint_last
= -1;
1132 linuxthreads_wait_last
= -1;
1133 linuxthreads_exit_status
= __W_STOPCODE(0);
1135 child_ops
.to_attach (args
, from_tty
);
1137 if (linuxthreads_max
)
1138 linuxthreads_attach_pending
= 1;
1141 /* Take a program previously attached to and detaches it.
1142 The program resumes execution and will no longer stop
1143 on signals, etc. We'd better not have left any breakpoints
1144 in the program or it'll die when it hits one. For this
1145 to work, it may be necessary for the process to have been
1146 previously attached. It *might* work if the program was
1147 started via the normal ptrace (PTRACE_TRACEME). */
1150 linuxthreads_detach (args
, from_tty
)
1154 if (linuxthreads_max
)
1159 target_write_memory (linuxthreads_debug
, (char *)&off
, sizeof (off
));
1161 /* Walk through linuxthreads array in order to detach known threads. */
1162 if (linuxthreads_manager_pid
!= 0)
1164 /* Get rid of all positive zombie breakpoints. */
1165 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1167 if (linuxthreads_breakpoint_zombie
[i
].step
)
1170 pid
= linuxthreads_breakpoint_zombie
[i
].pid
;
1171 if (!linuxthreads_thread_alive (pid
))
1174 if (linuxthreads_breakpoint_zombie
[i
].pc
!= read_pc_pid (pid
))
1177 /* Continue in STEP mode until the thread pc has moved or
1178 until SIGTRAP is found on the same PC. */
1179 if (linuxthreads_find_trap (pid
, 0)
1180 && linuxthreads_breakpoint_zombie
[i
].pc
== read_pc_pid (pid
))
1181 write_pc_pid (linuxthreads_breakpoint_zombie
[i
].pc
1182 - DECR_PC_AFTER_BREAK
, pid
);
1185 /* Detach thread after thread. */
1186 inferior_pid
= linuxthreads_manager_pid
;
1187 iterate_active_threads (detach_thread
, 1);
1189 /* Remove pending SIGTRAP and SIGSTOP */
1190 linuxthreads_find_trap (inferior_pid
, 1);
1192 linuxthreads_wait_last
= -1;
1193 linuxthreads_exit_status
= __W_STOPCODE(0);
1196 linuxthreads_inferior_pid
= 0;
1197 linuxthreads_breakpoint_pid
= 0;
1198 linuxthreads_step_pid
= 0;
1199 linuxthreads_step_signo
= TARGET_SIGNAL_0
;
1200 linuxthreads_manager_pid
= 0;
1201 linuxthreads_initial_pid
= 0;
1202 linuxthreads_attach_pending
= 0;
1203 init_thread_list (); /* Destroy thread info */
1206 child_ops
.to_detach (args
, from_tty
);
1208 unpush_target (&linuxthreads_ops
);
1211 /* Resume execution of process PID. If STEP is nozero, then
1212 just single step it. If SIGNAL is nonzero, restart it with that
1213 signal activated. */
1216 linuxthreads_resume (pid
, step
, signo
)
1219 enum target_signal signo
;
1221 if (!linuxthreads_max
|| stop_soon_quietly
|| linuxthreads_manager_pid
== 0)
1223 child_ops
.to_resume (pid
, step
, signo
);
1228 if (linuxthreads_inferior_pid
)
1230 /* Prepare resume of the last thread that hit a breakpoint */
1231 linuxthreads_breakpoints_inserted
= 0;
1232 rpid
= linuxthreads_inferior_pid
;
1233 linuxthreads_step_signo
= signo
;
1237 struct cleanup
*old_chain
= NULL
;
1242 linuxthreads_step_pid
= step
? inferior_pid
: 0;
1243 linuxthreads_step_signo
= signo
;
1244 rpid
= inferior_pid
;
1249 if (pid
< 0 || !step
)
1251 linuxthreads_breakpoints_inserted
= 1;
1253 /* Walk through linuxthreads array in order to resume threads */
1254 if (pid
>= 0 && inferior_pid
!= pid
)
1256 old_chain
= save_inferior_pid ();
1260 iterate_active_threads (resume_thread
, 0);
1261 if (linuxthreads_manager_pid
!= inferior_pid
1262 && !linuxthreads_pending_status (linuxthreads_manager_pid
))
1263 resume_thread (linuxthreads_manager_pid
);
1266 linuxthreads_breakpoints_inserted
= 0;
1268 /* Deal with zombie breakpoint */
1269 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1270 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1272 if (linuxthreads_breakpoint_zombie
[i
].pc
!= read_pc_pid (rpid
))
1274 /* The current pc is out of zombie breakpoint. */
1275 REMOVE_BREAKPOINT_ZOMBIE(i
);
1280 if (old_chain
!= NULL
)
1281 do_cleanups (old_chain
);
1284 /* Resume initial thread. */
1285 /* [unles it has a wait event pending] */
1286 if (!linuxthreads_pending_status (rpid
))
1288 child_ops
.to_resume (rpid
, step
, signo
);
1293 /* Abstract out the child_wait functionality. */
1295 linux_child_wait (pid
, rpid
, status
)
1302 /* Note: inftarg has these inside the loop. */
1303 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1304 attached process. */
1307 errno
= save_errno
= 0;
1311 *rpid
= waitpid (pid
, status
, __WCLONE
| WNOHANG
);
1316 /* Got an event -- break out */
1319 if (errno
== EINTR
) /* interrupted by signal, try again */
1325 *rpid
= waitpid (pid
, status
, WNOHANG
);
1328 /* Got an event -- break out */
1335 if (errno
!= 0 && save_errno
!= 0)
1339 sigsuspend(&linuxthreads_block_mask
);
1341 clear_sigio_trap ();
1342 clear_sigint_trap ();
1344 return errno
? errno
: save_errno
;
1348 /* Wait for any threads to stop. We may have to convert PID from a thread id
1349 to a LWP id, and vice versa on the way out. */
1352 linuxthreads_wait (pid
, ourstatus
)
1354 struct target_waitstatus
*ourstatus
;
1362 if (linuxthreads_max
&& !linuxthreads_breakpoints_inserted
)
1363 wstatus
= alloca (LINUXTHREAD_NSIG
* sizeof (int));
1365 /* See if the inferior has chosen values for its signals yet. By
1366 checking for them here, we can be sure we've updated GDB's signal
1367 handling table before the inferior ever gets one of them. (Well,
1368 before we notice, anyway.) */
1369 check_all_signal_numbers ();
1373 if (!linuxthreads_max
)
1375 else if (!linuxthreads_breakpoints_inserted
)
1377 if (linuxthreads_inferior_pid
)
1378 pid
= linuxthreads_inferior_pid
;
1383 else if (pid
< 0 && linuxthreads_wait_last
>= 0)
1385 status
= linuxthreads_wait_status
[linuxthreads_wait_last
];
1386 rpid
= linuxthreads_wait_pid
[linuxthreads_wait_last
--];
1388 else if (pid
> 0 && linuxthreads_pending_status (pid
))
1390 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
1391 if (linuxthreads_wait_pid
[i
] == pid
)
1397 status
= linuxthreads_wait_status
[i
];
1399 if (i
< linuxthreads_wait_last
)
1401 linuxthreads_wait_status
[i
] =
1402 linuxthreads_wait_status
[linuxthreads_wait_last
];
1403 linuxthreads_wait_pid
[i
] =
1404 linuxthreads_wait_pid
[linuxthreads_wait_last
];
1406 linuxthreads_wait_last
--;
1416 save_errno
= linux_child_wait (pid
, &rpid
, &status
);
1420 if (WIFEXITED(linuxthreads_exit_status
))
1422 store_waitstatus (ourstatus
, linuxthreads_exit_status
);
1423 return inferior_pid
;
1428 (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
1429 safe_strerror (save_errno
));
1430 /* Claim it exited with unknown signal. */
1431 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1432 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1437 /* We have now gotten a new event from waitpid above. */
1439 /* Signals arrive in any order. So get all signals until
1440 SIGTRAP and resend previous ones to be held after. */
1441 if (linuxthreads_max
1442 && !linuxthreads_breakpoints_inserted
1443 && WIFSTOPPED(status
))
1444 if (WSTOPSIG(status
) == SIGTRAP
)
1448 kill (rpid
, WSTOPSIG(wstatus
[last
]));
1451 /* insert negative zombie breakpoint */
1452 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1453 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1455 if (i
> linuxthreads_breakpoint_last
)
1457 linuxthreads_breakpoint_zombie
[i
].pid
= rpid
;
1458 linuxthreads_breakpoint_last
++;
1460 linuxthreads_breakpoint_zombie
[i
].pc
= read_pc_pid (rpid
);
1461 linuxthreads_breakpoint_zombie
[i
].step
= 1;
1465 if (WSTOPSIG(status
) != SIGSTOP
)
1467 for (i
= 0; i
< last
; i
++)
1468 if (wstatus
[i
] == status
)
1472 wstatus
[last
++] = status
;
1475 child_resume (rpid
, 1, TARGET_SIGNAL_0
);
1478 if (linuxthreads_inferior_pid
)
1479 linuxthreads_inferior_pid
= 0;
1482 if (linuxthreads_max
&& !stop_soon_quietly
)
1484 if (linuxthreads_max
1485 && WIFSTOPPED(status
)
1486 && WSTOPSIG(status
) == SIGSTOP
)
1488 /* Skip SIGSTOP signals. */
1489 if (!linuxthreads_pending_status (rpid
))
1491 if (linuxthreads_step_pid
== rpid
)
1493 child_resume (rpid
, 1, linuxthreads_step_signo
);
1497 child_resume (rpid
, 0, TARGET_SIGNAL_0
);
1503 /* Do no report exit status of cloned threads. */
1504 if (WIFEXITED(status
))
1506 if (rpid
== linuxthreads_initial_pid
)
1507 linuxthreads_exit_status
= status
;
1509 /* Remove any zombie breakpoint. */
1510 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1511 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1513 REMOVE_BREAKPOINT_ZOMBIE(i
);
1521 /* Deal with zombie breakpoint */
1522 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1523 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1526 if (i
<= linuxthreads_breakpoint_last
)
1528 /* There is a potential zombie breakpoint */
1529 if (WIFEXITED(status
)
1530 || linuxthreads_breakpoint_zombie
[i
].pc
!= read_pc_pid (rpid
))
1532 /* The current pc is out of zombie breakpoint. */
1533 REMOVE_BREAKPOINT_ZOMBIE(i
);
1535 else if (!linuxthreads_breakpoint_zombie
[i
].step
1536 && WIFSTOPPED(status
) && WSTOPSIG(status
) == SIGTRAP
)
1538 /* This is a real one ==> decrement PC and restart. */
1539 write_pc_pid (linuxthreads_breakpoint_zombie
[i
].pc
1540 - DECR_PC_AFTER_BREAK
, rpid
);
1541 if (linuxthreads_step_pid
== rpid
)
1543 child_resume (rpid
, 1, linuxthreads_step_signo
);
1547 child_resume (rpid
, 0, TARGET_SIGNAL_0
);
1553 /* Walk through linuxthreads array in order to stop them */
1554 if (linuxthreads_breakpoints_inserted
)
1555 update_stop_threads (rpid
);
1558 else if (rpid
!= inferior_pid
)
1561 store_waitstatus (ourstatus
, status
);
1563 if (linuxthreads_attach_pending
&& !stop_soon_quietly
)
1566 if (!using_thread_db
)
1568 target_write_memory (linuxthreads_debug
,
1569 (char *) &on
, sizeof (on
));
1570 update_stop_threads (rpid
);
1572 linuxthreads_attach_pending
= 0;
1575 if (linuxthreads_breakpoints_inserted
1576 && WIFSTOPPED(status
)
1577 && WSTOPSIG(status
) == SIGTRAP
)
1578 linuxthreads_breakpoint_pid
= rpid
;
1579 else if (linuxthreads_breakpoint_pid
)
1580 linuxthreads_breakpoint_pid
= 0;
1586 /* Fork an inferior process, and start debugging it with ptrace. */
1589 linuxthreads_create_inferior (exec_file
, allargs
, env
)
1594 if (!exec_file
&& !exec_bfd
)
1596 error ("No executable file specified.\n\
1597 Use the \"file\" or \"exec-file\" command.");
1601 push_target (&linuxthreads_ops
);
1602 linuxthreads_breakpoints_inserted
= 1;
1603 linuxthreads_breakpoint_last
= -1;
1604 linuxthreads_wait_last
= -1;
1605 linuxthreads_exit_status
= __W_STOPCODE(0);
1607 if (linuxthreads_max
)
1608 linuxthreads_attach_pending
= 1;
1610 child_ops
.to_create_inferior (exec_file
, allargs
, env
);
1614 linuxthreads_discard_global_state ()
1616 linuxthreads_inferior_pid
= 0;
1617 linuxthreads_breakpoint_pid
= 0;
1618 linuxthreads_step_pid
= 0;
1619 linuxthreads_step_signo
= TARGET_SIGNAL_0
;
1620 linuxthreads_manager_pid
= 0;
1621 linuxthreads_initial_pid
= 0;
1622 linuxthreads_attach_pending
= 0;
1623 linuxthreads_max
= 0;
1626 /* Clean up after the inferior dies. */
1629 linuxthreads_mourn_inferior ()
1631 if (linuxthreads_max
)
1634 target_write_memory (linuxthreads_debug
, (char *)&off
, sizeof (off
));
1636 linuxthreads_discard_global_state ();
1637 init_thread_list(); /* Destroy thread info */
1640 child_ops
.to_mourn_inferior ();
1642 unpush_target (&linuxthreads_ops
);
1645 /* Kill the inferior process */
1648 linuxthreads_kill ()
1653 if (inferior_pid
== 0)
1656 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1658 /* Remove all threads status. */
1659 inferior_pid
= linuxthreads_manager_pid
;
1660 iterate_active_threads (kill_thread
, 1);
1663 kill_thread (inferior_pid
);
1666 /* doing_quit_force solves a real problem, but I think a properly
1667 placed call to catch_errors would do the trick much more cleanly. */
1668 if (doing_quit_force
>= 0)
1670 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1672 /* Wait for thread to complete */
1673 while ((rpid
= waitpid (-1, &status
, __WCLONE
)) > 0)
1674 if (!WIFEXITED(status
))
1677 while ((rpid
= waitpid (-1, &status
, 0)) > 0)
1678 if (!WIFEXITED(status
))
1682 while ((rpid
= waitpid (inferior_pid
, &status
, 0)) > 0)
1683 if (!WIFEXITED(status
))
1684 ptrace (PT_KILL
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0);
1688 /* Wait for all threads. */
1691 rpid
= waitpid (-1, &status
, __WCLONE
| WNOHANG
);
1693 while (rpid
> 0 || errno
== EINTR
);
1694 /* FIXME: should no longer need to handle EINTR here. */
1698 rpid
= waitpid (-1, &status
, WNOHANG
);
1700 while (rpid
> 0 || errno
== EINTR
);
1701 /* FIXME: should no longer need to handle EINTR here. */
1703 linuxthreads_mourn_inferior ();
1706 /* Insert a breakpoint */
1709 linuxthreads_insert_breakpoint (addr
, contents_cache
)
1711 char *contents_cache
;
1713 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1715 linuxthreads_breakpoint_addr
= addr
;
1716 iterate_active_threads (insert_breakpoint
, 1);
1717 insert_breakpoint (linuxthreads_manager_pid
);
1720 return child_ops
.to_insert_breakpoint (addr
, contents_cache
);
1723 /* Remove a breakpoint */
1726 linuxthreads_remove_breakpoint (addr
, contents_cache
)
1728 char *contents_cache
;
1730 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1732 linuxthreads_breakpoint_addr
= addr
;
1733 iterate_active_threads (remove_breakpoint
, 1);
1734 remove_breakpoint (linuxthreads_manager_pid
);
1737 return child_ops
.to_remove_breakpoint (addr
, contents_cache
);
1740 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1743 linuxthreads_can_run ()
1745 return child_suppress_run
;
1750 init_linuxthreads_ops ()
1752 linuxthreads_ops
.to_shortname
= "linuxthreads";
1753 linuxthreads_ops
.to_longname
= "LINUX threads and pthread.";
1754 linuxthreads_ops
.to_doc
= "LINUX threads and pthread support.";
1755 linuxthreads_ops
.to_attach
= linuxthreads_attach
;
1756 linuxthreads_ops
.to_detach
= linuxthreads_detach
;
1757 linuxthreads_ops
.to_resume
= linuxthreads_resume
;
1758 linuxthreads_ops
.to_wait
= linuxthreads_wait
;
1759 linuxthreads_ops
.to_kill
= linuxthreads_kill
;
1760 linuxthreads_ops
.to_can_run
= linuxthreads_can_run
;
1761 linuxthreads_ops
.to_stratum
= thread_stratum
;
1762 linuxthreads_ops
.to_insert_breakpoint
= linuxthreads_insert_breakpoint
;
1763 linuxthreads_ops
.to_remove_breakpoint
= linuxthreads_remove_breakpoint
;
1764 linuxthreads_ops
.to_create_inferior
= linuxthreads_create_inferior
;
1765 linuxthreads_ops
.to_mourn_inferior
= linuxthreads_mourn_inferior
;
1766 linuxthreads_ops
.to_thread_alive
= linuxthreads_thread_alive
;
1767 linuxthreads_ops
.to_pid_to_str
= linuxthreads_pid_to_str
;
1768 linuxthreads_ops
.to_magic
= OPS_MAGIC
;
1772 _initialize_linuxthreads ()
1774 struct sigaction sact
;
1775 sigset_t linuxthreads_wait_mask
; /* sigset with SIGCHLD */
1777 init_linuxthreads_ops ();
1778 add_target (&linuxthreads_ops
);
1779 child_suppress_run
= 1;
1781 /* Hook onto the "new_objfile" event.
1782 * If someone else is already hooked onto the event,
1783 * then make sure he will be called after we are.
1785 target_new_objfile_chain
= target_new_objfile_hook
;
1786 target_new_objfile_hook
= linuxthreads_new_objfile
;
1788 /* Attach SIGCHLD handler */
1789 sact
.sa_handler
= sigchld_handler
;
1790 sigemptyset (&sact
.sa_mask
);
1792 sigaction (SIGCHLD
, &sact
, NULL
);
1794 /* initialize SIGCHLD mask */
1795 sigemptyset (&linuxthreads_wait_mask
);
1796 sigaddset (&linuxthreads_wait_mask
, SIGCHLD
);
1798 /* Use SIG_BLOCK to block receipt of SIGCHLD.
1799 The block_mask will allow us to wait for this signal explicitly. */
1800 sigprocmask(SIG_BLOCK
,
1801 &linuxthreads_wait_mask
,
1802 &linuxthreads_block_mask
);