1 /* Low level interface for debugging GNU/Linux threads for GDB,
3 Copyright 1998, 1999, 2000 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 (ptid_t ptid
)
211 return ptrace (PT_READ_U
, PIDGET (ptid
), (PTRACE_ARG3_TYPE
)0, 0) >= 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 (int pid
, int stop
)
241 /* PID may have any number of signals pending. The kernel will
242 report each of them to us via wait, and then it's up to us to
243 pass them along to the process via ptrace, if we so choose.
245 We need to paw through the whole set until we've found a SIGTRAP
246 (or a SIGSTOP, if `stop' is set). We don't pass the SIGTRAP (or
247 SIGSTOP) through, but we do re-send all the others, so PID will
248 receive them when we resume it. */
249 int *wstatus
= alloca (LINUXTHREAD_NSIG
* sizeof (int));
252 /* Look at the pending status */
253 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
254 if (linuxthreads_wait_pid
[i
] == pid
)
256 status
= linuxthreads_wait_status
[i
];
258 /* Delete the i'th member of the table. Since the table is
259 unordered, we can do this simply by copying the table's
260 last element to the i'th position, and shrinking the table
262 if (i
< linuxthreads_wait_last
)
264 linuxthreads_wait_status
[i
] =
265 linuxthreads_wait_status
[linuxthreads_wait_last
];
266 linuxthreads_wait_pid
[i
] =
267 linuxthreads_wait_pid
[linuxthreads_wait_last
];
269 linuxthreads_wait_last
--;
271 if (!WIFSTOPPED(status
)) /* Thread has died */
274 if (WSTOPSIG(status
) == SIGTRAP
)
281 else if (WSTOPSIG(status
) == SIGSTOP
)
297 /* Make sure that we'll find what we're looking for. */
308 /* Catch all status until SIGTRAP and optionally SIGSTOP show up. */
311 /* resume the child every time... */
312 child_resume (pid_to_ptid (pid
), 1, TARGET_SIGNAL_0
);
314 /* loop as long as errno == EINTR:
315 waitpid syscall may be aborted due to GDB receiving a signal.
316 FIXME: EINTR handling should no longer be necessary here, since
317 we now block SIGCHLD except in an explicit sigsuspend call. */
321 rpid
= waitpid (pid
, &status
, __WCLONE
);
331 /* There are a few reasons the wait call above may have
332 failed. If the thread manager dies, its children get
333 reparented, and this interferes with GDB waiting for
334 them, in some cases. Another possibility is that the
335 initial thread was not cloned, so calling wait with
336 __WCLONE won't find it. I think neither of these should
337 occur in modern Linux kernels --- they don't seem to in
339 rpid
= waitpid (pid
, &status
, 0);
345 perror_with_name ("find_trap/waitpid");
348 if (!WIFSTOPPED(status
)) /* Thread has died */
351 if (WSTOPSIG(status
) == SIGTRAP
)
352 if (!stop
|| found_stop
)
356 else if (WSTOPSIG(status
) != SIGSTOP
)
357 wstatus
[last
++] = status
;
367 /* Resend any other signals we noticed to the thread, to be received
368 when we continue it. */
371 kill (pid
, WSTOPSIG(wstatus
[last
]));
378 sigchld_handler (int signo
)
380 /* This handler is used to get an EINTR while doing waitpid()
381 when an event is received */
384 /* Have we already collected a wait status for PID in the
385 linuxthreads_wait bag? */
387 linuxthreads_pending_status (int pid
)
390 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
391 if (linuxthreads_wait_pid
[i
] == pid
)
397 /* Internal linuxthreads signal management */
399 /* Check in OBJFILE for the variable that holds the number for signal SIG.
400 We assume that we've already found other LinuxThreads-ish variables
401 in OBJFILE, so we complain if it's required, but not there.
402 Return true iff things are okay. */
404 find_signal_var (struct linuxthreads_signal
*sig
, struct objfile
*objfile
)
406 struct minimal_symbol
*ms
= lookup_minimal_symbol (sig
->var
, NULL
, objfile
);
412 fprintf_unfiltered (gdb_stderr
,
413 "Unable to find linuxthreads symbol \"%s\"\n",
424 sig
->addr
= SYMBOL_VALUE_ADDRESS (ms
);
430 find_all_signal_vars (struct objfile
*objfile
)
432 return ( find_signal_var (&linuxthreads_sig_restart
, objfile
)
433 && find_signal_var (&linuxthreads_sig_cancel
, objfile
)
434 && find_signal_var (&linuxthreads_sig_debug
, objfile
));
437 /* A struct complaint isn't appropriate here. */
438 static int complained_cannot_determine_thread_signal_number
= 0;
440 /* Check to see if the variable holding the signal number for SIG has
441 been initialized yet. If it has, tell GDB to pass that signal
442 through to the inferior silently. */
444 check_signal_number (struct linuxthreads_signal
*sig
)
449 /* We already know this signal number. */
453 /* We don't know the variable's address yet. */
456 if (target_read_memory (sig
->addr
, (char *)&num
, sizeof (num
))
459 /* If this happens once, it'll probably happen for all the
460 signals, so only complain once. */
461 if (! complained_cannot_determine_thread_signal_number
)
462 warning ("Cannot determine thread signal number; "
463 "GDB may report spurious signals.");
464 complained_cannot_determine_thread_signal_number
= 1;
469 /* It hasn't been initialized yet. */
472 /* We know sig->signal was zero, and is becoming non-zero, so it's
473 okay to sample GDB's original settings. */
475 sig
->stop
= signal_stop_update (target_signal_from_host (num
), 0);
476 sig
->print
= signal_print_update (target_signal_from_host (num
), 0);
480 check_all_signal_numbers (void)
482 /* If this isn't a LinuxThreads program, quit early. */
483 if (! linuxthreads_max
)
486 check_signal_number (&linuxthreads_sig_restart
);
487 check_signal_number (&linuxthreads_sig_cancel
);
488 check_signal_number (&linuxthreads_sig_debug
);
490 /* handle linuxthread exit */
491 if (linuxthreads_sig_debug
.signal
492 || linuxthreads_sig_restart
.signal
)
494 struct sigaction sact
;
496 sact
.sa_handler
= sigchld_handler
;
497 sigemptyset(&sact
.sa_mask
);
500 if (linuxthreads_sig_debug
.signal
> 0)
501 sigaction(linuxthreads_sig_cancel
.signal
, &sact
, NULL
);
503 sigaction(linuxthreads_sig_restart
.signal
, &sact
, NULL
);
508 /* Restore GDB's original settings for SIG.
509 This should only be called when we're no longer sure if we're
510 talking to an executable that uses LinuxThreads, so we clear the
511 signal number and variable address too. */
513 restore_signal (struct linuxthreads_signal
*sig
)
518 /* We know sig->signal was non-zero, and is becoming zero, so it's
519 okay to restore GDB's original settings. */
520 signal_stop_update (target_signal_from_host (sig
->signal
), sig
->stop
);
521 signal_print_update (target_signal_from_host (sig
->signal
), sig
->print
);
528 /* Restore GDB's original settings for all LinuxThreads signals.
529 This should only be called when we're no longer sure if we're
530 talking to an executable that uses LinuxThreads, so we clear the
531 signal number and variable address too. */
533 restore_all_signals (void)
535 restore_signal (&linuxthreads_sig_restart
);
536 restore_signal (&linuxthreads_sig_cancel
);
537 restore_signal (&linuxthreads_sig_debug
);
539 /* If it happens again, we should complain again. */
540 complained_cannot_determine_thread_signal_number
= 0;
546 /* Apply FUNC to the pid of each active thread. This consults the
547 inferior's handle table to find active threads.
549 If ALL is non-zero, process all threads.
550 If ALL is zero, skip threads with pending status. */
552 iterate_active_threads (void (*func
) (int), int all
)
559 read_memory (linuxthreads_num
, (char *)&num
, sizeof (int));
561 for (i
= 0; i
< linuxthreads_max
&& num
> 0; i
++)
563 read_memory (linuxthreads_handles
+
564 linuxthreads_sizeof_handle
* i
+ linuxthreads_offset_descr
,
565 (char *)&descr
, sizeof (void *));
569 read_memory (descr
+ linuxthreads_offset_pid
,
570 (char *)&pid
, sizeof (pid_t
));
571 if (pid
> 0 && pid
!= linuxthreads_manager_pid
572 && (all
|| (!linuxthreads_pending_status (pid
))))
578 /* Insert a thread breakpoint at linuxthreads_breakpoint_addr.
579 This is the worker function for linuxthreads_insert_breakpoint,
580 which passes it to iterate_active_threads. */
582 insert_breakpoint (int pid
)
586 /* Remove (if any) the positive zombie breakpoint. */
587 for (j
= linuxthreads_breakpoint_last
; j
>= 0; j
--)
588 if (linuxthreads_breakpoint_zombie
[j
].pid
== pid
)
590 if ((linuxthreads_breakpoint_zombie
[j
].pc
- DECR_PC_AFTER_BREAK
591 == linuxthreads_breakpoint_addr
)
592 && !linuxthreads_breakpoint_zombie
[j
].step
)
593 REMOVE_BREAKPOINT_ZOMBIE(j
);
598 /* Note that we're about to remove a thread breakpoint at
599 linuxthreads_breakpoint_addr.
601 This is the worker function for linuxthreads_remove_breakpoint,
602 which passes it to iterate_active_threads. The actual work of
603 overwriting the breakpoint instruction is done by
604 child_ops.to_remove_breakpoint; here, we simply create a zombie
605 breakpoint if the thread's PC is pointing at the breakpoint being
608 remove_breakpoint (int pid
)
612 /* Insert a positive zombie breakpoint (if needed). */
613 for (j
= 0; j
<= linuxthreads_breakpoint_last
; j
++)
614 if (linuxthreads_breakpoint_zombie
[j
].pid
== pid
)
617 if (in_thread_list (pid_to_ptid (pid
))
618 && linuxthreads_thread_alive (pid_to_ptid (pid
)))
620 CORE_ADDR pc
= read_pc_pid (pid_to_ptid (pid
));
621 if (linuxthreads_breakpoint_addr
== pc
- DECR_PC_AFTER_BREAK
622 && j
> linuxthreads_breakpoint_last
)
624 linuxthreads_breakpoint_zombie
[j
].pid
= pid
;
625 linuxthreads_breakpoint_zombie
[j
].pc
= pc
;
626 linuxthreads_breakpoint_zombie
[j
].step
= 0;
627 linuxthreads_breakpoint_last
++;
634 kill_thread (int pid
)
636 if (in_thread_list (pid_to_ptid (pid
)))
638 ptrace (PT_KILL
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
646 /* Resume a thread */
648 resume_thread (int pid
)
650 if (pid
!= PIDGET (inferior_ptid
)
651 && in_thread_list (pid_to_ptid (pid
))
652 && linuxthreads_thread_alive (pid_to_ptid (pid
)))
654 if (pid
== linuxthreads_step_pid
)
656 child_resume (pid_to_ptid (pid
), 1, linuxthreads_step_signo
);
660 child_resume (pid_to_ptid (pid
), 0, TARGET_SIGNAL_0
);
665 /* Detach a thread */
667 detach_thread (int pid
)
669 ptid_t ptid
= pid_to_ptid (pid
);
671 if (in_thread_list (ptid
) && linuxthreads_thread_alive (ptid
))
673 /* Remove pending SIGTRAP and SIGSTOP */
674 linuxthreads_find_trap (pid
, 1);
676 inferior_ptid
= ptid
;
677 detach (TARGET_SIGNAL_0
);
678 inferior_ptid
= pid_to_ptid (linuxthreads_manager_pid
);
682 /* Attach a thread */
684 attach_thread (int pid
)
686 if (ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0) != 0)
687 perror_with_name ("attach_thread");
692 stop_thread (int pid
)
694 if (pid
!= PIDGET (inferior_ptid
))
696 if (in_thread_list (pid_to_ptid (pid
)))
700 else if (ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0) == 0)
702 if (!linuxthreads_attach_pending
)
703 printf_filtered ("[New %s]\n",
704 target_pid_to_str (pid_to_ptid (pid
)));
705 add_thread (pid_to_ptid (pid
));
706 if (linuxthreads_sig_debug
.signal
)
708 /* After a new thread in glibc 2.1 signals gdb its existence,
709 it suspends itself and wait for linuxthreads_sig_restart,
710 now we can wake it up. */
711 kill (pid
, linuxthreads_sig_restart
.signal
);
715 perror_with_name ("ptrace in stop_thread");
719 /* Wait for a thread */
721 wait_thread (int pid
)
726 if (pid
!= PIDGET (inferior_ptid
) && in_thread_list (pid_to_ptid (pid
)))
728 /* loop as long as errno == EINTR:
729 waitpid syscall may be aborted if GDB receives a signal.
730 FIXME: EINTR handling should no longer be necessary here, since
731 we now block SIGCHLD except during an explicit sigsuspend call. */
734 /* Get first pid status. */
735 rpid
= waitpid(pid
, &status
, __WCLONE
);
745 /* There are two reasons this might have failed:
747 1) PID is the initial thread, which wasn't cloned, so
748 passing the __WCLONE flag to waitpid prevented us from
751 2) The manager thread is the parent of all but the
752 initial thread; if it dies, the children will all be
753 reparented to init, which will wait for them. This means
754 our call to waitpid won't find them.
756 Actually, based on a casual look at the 2.0.36 kernel
757 code, I don't think either of these cases happen. But I
758 don't have things set up for remotely debugging the
759 kernel, so I'm not sure. And perhaps older kernels
761 rpid
= waitpid(pid
, &status
, 0);
766 if (errno
!= EINTR
&& linuxthreads_thread_alive (pid_to_ptid (pid
)))
767 perror_with_name ("wait_thread/waitpid");
769 /* the thread is dead. */
772 if (!WIFSTOPPED(status
) || WSTOPSIG(status
) != SIGSTOP
)
774 linuxthreads_wait_pid
[++linuxthreads_wait_last
] = pid
;
775 linuxthreads_wait_status
[linuxthreads_wait_last
] = status
;
780 /* Walk through the linuxthreads handles in order to detect all
781 threads and stop them */
783 update_stop_threads (int test_pid
)
785 struct cleanup
*old_chain
= NULL
;
787 check_all_signal_numbers ();
789 if (linuxthreads_manager_pid
== 0)
791 if (linuxthreads_manager
)
793 if (test_pid
> 0 && test_pid
!= PIDGET (inferior_ptid
))
795 old_chain
= save_inferior_ptid ();
796 inferior_ptid
= pid_to_ptid (test_pid
);
798 read_memory (linuxthreads_manager
,
799 (char *)&linuxthreads_manager_pid
, sizeof (pid_t
));
801 if (linuxthreads_initial
)
803 if (test_pid
> 0 && test_pid
!= PIDGET (inferior_ptid
))
805 old_chain
= save_inferior_ptid ();
806 inferior_ptid
= pid_to_ptid (test_pid
);
808 read_memory(linuxthreads_initial
,
809 (char *)&linuxthreads_initial_pid
, sizeof (pid_t
));
813 if (linuxthreads_manager_pid
!= 0)
815 if (old_chain
== NULL
&& test_pid
> 0 &&
816 test_pid
!= PIDGET (inferior_ptid
)
817 && linuxthreads_thread_alive (pid_to_ptid (test_pid
)))
819 old_chain
= save_inferior_ptid ();
820 inferior_ptid
= pid_to_ptid (test_pid
);
823 if (linuxthreads_thread_alive (inferior_ptid
))
827 if (test_pid
!= linuxthreads_manager_pid
828 && !linuxthreads_pending_status (linuxthreads_manager_pid
))
830 stop_thread (linuxthreads_manager_pid
);
831 wait_thread (linuxthreads_manager_pid
);
833 if (!in_thread_list (pid_to_ptid (test_pid
)))
835 if (!linuxthreads_attach_pending
)
836 printf_filtered ("[New %s]\n",
837 target_pid_to_str (pid_to_ptid (test_pid
)));
838 add_thread (pid_to_ptid (test_pid
));
839 if (linuxthreads_sig_debug
.signal
840 && PIDGET (inferior_ptid
) == test_pid
)
842 /* After a new thread in glibc 2.1 signals gdb its
843 existence, it suspends itself and wait for
844 linuxthreads_sig_restart, now we can wake it up. */
845 kill (test_pid
, linuxthreads_sig_restart
.signal
);
849 iterate_active_threads (stop_thread
, 0);
850 iterate_active_threads (wait_thread
, 0);
854 if (old_chain
!= NULL
)
855 do_cleanups (old_chain
);
858 /* This routine is called whenever a new symbol table is read in, or
859 when all symbol tables are removed. linux-thread event handling
860 can only be initialized when we find the right variables in
861 libpthread.so. Since it's a shared library, those variables don't
862 show up until the library gets mapped and the symbol table is read
865 /* This new_objfile event is now managed by a chained function pointer.
866 * It is the callee's responsability to call the next client on the chain.
869 /* Saved pointer to previous owner of the new_objfile event. */
870 static void (*target_new_objfile_chain
) (struct objfile
*);
873 linuxthreads_new_objfile (struct objfile
*objfile
)
875 struct minimal_symbol
*ms
;
877 /* Call predecessor on chain, if any.
878 Calling the new module first allows it to dominate,
879 if it finds its compatible libraries. */
881 if (target_new_objfile_chain
)
882 target_new_objfile_chain (objfile
);
886 /* We're starting an entirely new executable, so we can no
887 longer be sure that it uses LinuxThreads. Restore the signal
888 flags to their original states. */
889 restore_all_signals ();
891 /* Indicate that we don't know anything's address any more. */
892 linuxthreads_max
= 0;
897 /* If we've already found our variables in another objfile, don't
898 bother looking for them again. */
899 if (linuxthreads_max
)
902 if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL
, objfile
))
903 /* This object file isn't the pthreads library. */
906 if ((ms
= lookup_minimal_symbol ("__pthread_threads_debug",
907 NULL
, objfile
)) == NULL
)
909 /* The debugging-aware libpthreads is not present in this objfile */
911 This program seems to use POSIX threads, but the thread library used\n\
912 does not support debugging. This may make using GDB difficult. Don't\n\
913 set breakpoints or single-step through code that might be executed by\n\
914 any thread other than the main thread.");
917 linuxthreads_debug
= SYMBOL_VALUE_ADDRESS (ms
);
919 /* Read internal structures configuration */
920 if ((ms
= lookup_minimal_symbol ("__pthread_sizeof_handle",
921 NULL
, objfile
)) == NULL
922 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
923 (char *)&linuxthreads_sizeof_handle
,
924 sizeof (linuxthreads_sizeof_handle
)) != 0)
926 fprintf_unfiltered (gdb_stderr
,
927 "Unable to find linuxthreads symbol \"%s\"\n",
928 "__pthread_sizeof_handle");
932 if ((ms
= lookup_minimal_symbol ("__pthread_offsetof_descr",
933 NULL
, objfile
)) == NULL
934 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
935 (char *)&linuxthreads_offset_descr
,
936 sizeof (linuxthreads_offset_descr
)) != 0)
938 fprintf_unfiltered (gdb_stderr
,
939 "Unable to find linuxthreads symbol \"%s\"\n",
940 "__pthread_offsetof_descr");
944 if ((ms
= lookup_minimal_symbol ("__pthread_offsetof_pid",
945 NULL
, objfile
)) == NULL
946 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
947 (char *)&linuxthreads_offset_pid
,
948 sizeof (linuxthreads_offset_pid
)) != 0)
950 fprintf_unfiltered (gdb_stderr
,
951 "Unable to find linuxthreads symbol \"%s\"\n",
952 "__pthread_offsetof_pid");
956 if (! find_all_signal_vars (objfile
))
959 /* Read adresses of internal structures to access */
960 if ((ms
= lookup_minimal_symbol ("__pthread_handles",
961 NULL
, objfile
)) == NULL
)
963 fprintf_unfiltered (gdb_stderr
,
964 "Unable to find linuxthreads symbol \"%s\"\n",
965 "__pthread_handles");
968 linuxthreads_handles
= SYMBOL_VALUE_ADDRESS (ms
);
970 if ((ms
= lookup_minimal_symbol ("__pthread_handles_num",
971 NULL
, objfile
)) == NULL
)
973 fprintf_unfiltered (gdb_stderr
,
974 "Unable to find linuxthreads symbol \"%s\"\n",
975 "__pthread_handles_num");
978 linuxthreads_num
= SYMBOL_VALUE_ADDRESS (ms
);
980 if ((ms
= lookup_minimal_symbol ("__pthread_manager_thread",
981 NULL
, objfile
)) == NULL
)
983 fprintf_unfiltered (gdb_stderr
,
984 "Unable to find linuxthreads symbol \"%s\"\n",
985 "__pthread_manager_thread");
988 linuxthreads_manager
= SYMBOL_VALUE_ADDRESS (ms
) + linuxthreads_offset_pid
;
990 if ((ms
= lookup_minimal_symbol ("__pthread_initial_thread",
991 NULL
, objfile
)) == NULL
)
993 fprintf_unfiltered (gdb_stderr
,
994 "Unable to find linuxthreads symbol \"%s\"\n",
995 "__pthread_initial_thread");
998 linuxthreads_initial
= SYMBOL_VALUE_ADDRESS (ms
) + linuxthreads_offset_pid
;
1000 /* Search for this last, so it won't be set to a non-zero value unless
1001 we successfully found all the symbols above. */
1002 if ((ms
= lookup_minimal_symbol ("__pthread_threads_max",
1003 NULL
, objfile
)) == NULL
1004 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
1005 (char *)&linuxthreads_max
,
1006 sizeof (linuxthreads_max
)) != 0)
1008 fprintf_unfiltered (gdb_stderr
,
1009 "Unable to find linuxthreads symbol \"%s\"\n",
1010 "__pthread_threads_max");
1014 /* Allocate gdb internal structures */
1015 linuxthreads_wait_pid
=
1016 (int *) xmalloc (sizeof (int) * (linuxthreads_max
+ 1));
1017 linuxthreads_wait_status
=
1018 (int *) xmalloc (sizeof (int) * (linuxthreads_max
+ 1));
1019 linuxthreads_breakpoint_zombie
= (struct linuxthreads_breakpoint
*)
1020 xmalloc (sizeof (struct linuxthreads_breakpoint
) * (linuxthreads_max
+ 1));
1022 if (PIDGET (inferior_ptid
) != 0 &&
1023 !linuxthreads_attach_pending
&&
1024 !using_thread_db
) /* suppressed by thread_db module */
1028 target_write_memory (linuxthreads_debug
, (char *)&on
, sizeof (on
));
1029 linuxthreads_attach_pending
= 1;
1030 update_stop_threads (PIDGET (inferior_ptid
));
1031 linuxthreads_attach_pending
= 0;
1034 check_all_signal_numbers ();
1039 /* If we have switched threads from a one that stopped at breakpoint,
1040 return 1 otherwise 0.
1042 Note that this implementation is potentially redundant now that
1043 default_prepare_to_proceed() has been added.
1045 FIXME This may not support switching threads after Ctrl-C
1046 correctly. The default implementation does support this. */
1049 linuxthreads_prepare_to_proceed (int step
)
1051 if (!linuxthreads_max
1052 || !linuxthreads_manager_pid
1053 || !linuxthreads_breakpoint_pid
1054 || !breakpoint_here_p (
1055 read_pc_pid (pid_to_ptid (linuxthreads_breakpoint_pid
))))
1060 /* Mark the current inferior as single stepping process. */
1061 linuxthreads_step_pid
= PIDGET (inferior_ptid
);
1064 linuxthreads_inferior_pid
= linuxthreads_breakpoint_pid
;
1065 return linuxthreads_breakpoint_pid
;
1068 /* Convert a pid to printable form. */
1071 linuxthreads_pid_to_str (ptid_t ptid
)
1073 static char buf
[100];
1074 int pid
= PIDGET (ptid
);
1076 sprintf (buf
, "%s %d%s", linuxthreads_max
? "Thread" : "Pid", pid
,
1077 (pid
== linuxthreads_manager_pid
) ? " (manager thread)"
1078 : (pid
== linuxthreads_initial_pid
) ? " (initial thread)"
1084 /* Attach to process PID, then initialize for debugging it
1085 and wait for the trace-trap that results from attaching. */
1088 linuxthreads_attach (char *args
, int from_tty
)
1091 error_no_arg ("process-id to attach");
1093 push_target (&linuxthreads_ops
);
1094 linuxthreads_breakpoints_inserted
= 1;
1095 linuxthreads_breakpoint_last
= -1;
1096 linuxthreads_wait_last
= -1;
1097 WSETSTOP (linuxthreads_exit_status
, 0);
1099 child_ops
.to_attach (args
, from_tty
);
1101 if (linuxthreads_max
)
1102 linuxthreads_attach_pending
= 1;
1105 /* Take a program previously attached to and detaches it.
1106 The program resumes execution and will no longer stop
1107 on signals, etc. We'd better not have left any breakpoints
1108 in the program or it'll die when it hits one. For this
1109 to work, it may be necessary for the process to have been
1110 previously attached. It *might* work if the program was
1111 started via the normal ptrace (PTRACE_TRACEME). */
1114 linuxthreads_detach (char *args
, int from_tty
)
1116 if (linuxthreads_max
)
1121 target_write_memory (linuxthreads_debug
, (char *)&off
, sizeof (off
));
1123 /* Walk through linuxthreads array in order to detach known threads. */
1124 if (linuxthreads_manager_pid
!= 0)
1126 /* Get rid of all positive zombie breakpoints. */
1127 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1129 if (linuxthreads_breakpoint_zombie
[i
].step
)
1132 pid
= linuxthreads_breakpoint_zombie
[i
].pid
;
1133 if (!linuxthreads_thread_alive (pid_to_ptid (pid
)))
1136 if (linuxthreads_breakpoint_zombie
[i
].pc
1137 != read_pc_pid (pid_to_ptid (pid
)))
1140 /* Continue in STEP mode until the thread pc has moved or
1141 until SIGTRAP is found on the same PC. */
1142 if (linuxthreads_find_trap (pid
, 0)
1143 && linuxthreads_breakpoint_zombie
[i
].pc
1144 == read_pc_pid (pid_to_ptid (pid
)))
1145 write_pc_pid (linuxthreads_breakpoint_zombie
[i
].pc
1146 - DECR_PC_AFTER_BREAK
, pid_to_ptid (pid
));
1149 /* Detach thread after thread. */
1150 inferior_ptid
= pid_to_ptid (linuxthreads_manager_pid
);
1151 iterate_active_threads (detach_thread
, 1);
1153 /* Remove pending SIGTRAP and SIGSTOP */
1154 linuxthreads_find_trap (PIDGET (inferior_ptid
), 1);
1156 linuxthreads_wait_last
= -1;
1157 WSETSTOP (linuxthreads_exit_status
, 0);
1160 linuxthreads_inferior_pid
= 0;
1161 linuxthreads_breakpoint_pid
= 0;
1162 linuxthreads_step_pid
= 0;
1163 linuxthreads_step_signo
= TARGET_SIGNAL_0
;
1164 linuxthreads_manager_pid
= 0;
1165 linuxthreads_initial_pid
= 0;
1166 linuxthreads_attach_pending
= 0;
1167 init_thread_list (); /* Destroy thread info */
1170 child_ops
.to_detach (args
, from_tty
);
1172 unpush_target (&linuxthreads_ops
);
1175 /* Resume execution of process PID. If STEP is nozero, then
1176 just single step it. If SIGNAL is nonzero, restart it with that
1177 signal activated. */
1180 linuxthreads_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1182 if (!linuxthreads_max
|| stop_soon_quietly
|| linuxthreads_manager_pid
== 0)
1184 child_ops
.to_resume (ptid
, step
, signo
);
1189 if (linuxthreads_inferior_pid
)
1191 /* Prepare resume of the last thread that hit a breakpoint */
1192 linuxthreads_breakpoints_inserted
= 0;
1193 rpid
= linuxthreads_inferior_pid
;
1194 linuxthreads_step_signo
= signo
;
1198 struct cleanup
*old_chain
= NULL
;
1201 if (PIDGET (ptid
) < 0)
1203 linuxthreads_step_pid
= step
? PIDGET (inferior_ptid
) : 0;
1204 linuxthreads_step_signo
= signo
;
1205 rpid
= PIDGET (inferior_ptid
);
1208 rpid
= PIDGET (ptid
);
1210 if (PIDGET (ptid
) < 0 || !step
)
1212 linuxthreads_breakpoints_inserted
= 1;
1214 /* Walk through linuxthreads array in order to resume threads */
1215 if (PIDGET (ptid
) >= 0 && !ptid_equal (inferior_ptid
, ptid
))
1217 old_chain
= save_inferior_ptid ();
1218 inferior_ptid
= ptid
;
1221 iterate_active_threads (resume_thread
, 0);
1222 if (linuxthreads_manager_pid
!= PIDGET (inferior_ptid
)
1223 && !linuxthreads_pending_status (linuxthreads_manager_pid
))
1224 resume_thread (linuxthreads_manager_pid
);
1227 linuxthreads_breakpoints_inserted
= 0;
1229 /* Deal with zombie breakpoint */
1230 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1231 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1233 if (linuxthreads_breakpoint_zombie
[i
].pc
1234 != read_pc_pid (pid_to_ptid (rpid
)))
1236 /* The current pc is out of zombie breakpoint. */
1237 REMOVE_BREAKPOINT_ZOMBIE(i
);
1242 if (old_chain
!= NULL
)
1243 do_cleanups (old_chain
);
1246 /* Resume initial thread. */
1247 /* [unles it has a wait event pending] */
1248 if (!linuxthreads_pending_status (rpid
))
1250 child_ops
.to_resume (pid_to_ptid (rpid
), step
, signo
);
1255 /* Abstract out the child_wait functionality. */
1257 linux_child_wait (int pid
, int *rpid
, int *status
)
1261 /* Note: inftarg has these inside the loop. */
1262 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1263 attached process. */
1266 errno
= save_errno
= 0;
1270 *rpid
= waitpid (pid
, status
, __WCLONE
| WNOHANG
);
1275 /* Got an event -- break out */
1278 if (errno
== EINTR
) /* interrupted by signal, try again */
1284 *rpid
= waitpid (pid
, status
, WNOHANG
);
1287 /* Got an event -- break out */
1294 if (errno
!= 0 && save_errno
!= 0)
1298 sigsuspend(&linuxthreads_block_mask
);
1300 clear_sigio_trap ();
1301 clear_sigint_trap ();
1303 return errno
? errno
: save_errno
;
1307 /* Wait for any threads to stop. We may have to convert PID from a thread id
1308 to a LWP id, and vice versa on the way out. */
1311 linuxthreads_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1318 int pid
= PIDGET (ptid
);
1320 if (linuxthreads_max
&& !linuxthreads_breakpoints_inserted
)
1321 wstatus
= alloca (LINUXTHREAD_NSIG
* sizeof (int));
1323 /* See if the inferior has chosen values for its signals yet. By
1324 checking for them here, we can be sure we've updated GDB's signal
1325 handling table before the inferior ever gets one of them. (Well,
1326 before we notice, anyway.) */
1327 check_all_signal_numbers ();
1331 if (!linuxthreads_max
)
1333 else if (!linuxthreads_breakpoints_inserted
)
1335 if (linuxthreads_inferior_pid
)
1336 pid
= linuxthreads_inferior_pid
;
1338 pid
= PIDGET (inferior_ptid
);
1341 else if (pid
< 0 && linuxthreads_wait_last
>= 0)
1343 status
= linuxthreads_wait_status
[linuxthreads_wait_last
];
1344 rpid
= linuxthreads_wait_pid
[linuxthreads_wait_last
--];
1346 else if (pid
> 0 && linuxthreads_pending_status (pid
))
1348 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
1349 if (linuxthreads_wait_pid
[i
] == pid
)
1355 status
= linuxthreads_wait_status
[i
];
1357 if (i
< linuxthreads_wait_last
)
1359 linuxthreads_wait_status
[i
] =
1360 linuxthreads_wait_status
[linuxthreads_wait_last
];
1361 linuxthreads_wait_pid
[i
] =
1362 linuxthreads_wait_pid
[linuxthreads_wait_last
];
1364 linuxthreads_wait_last
--;
1374 save_errno
= linux_child_wait (pid
, &rpid
, &status
);
1378 if (WIFEXITED(linuxthreads_exit_status
))
1380 store_waitstatus (ourstatus
, linuxthreads_exit_status
);
1381 return inferior_ptid
;
1386 (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
1387 safe_strerror (save_errno
));
1388 /* Claim it exited with unknown signal. */
1389 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1390 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1391 return pid_to_ptid (-1);
1395 /* We have now gotten a new event from waitpid above. */
1397 /* Signals arrive in any order. So get all signals until
1398 SIGTRAP and resend previous ones to be held after. */
1399 if (linuxthreads_max
1400 && !linuxthreads_breakpoints_inserted
1401 && WIFSTOPPED(status
))
1402 if (WSTOPSIG(status
) == SIGTRAP
)
1406 kill (rpid
, WSTOPSIG(wstatus
[last
]));
1409 /* insert negative zombie breakpoint */
1410 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1411 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1413 if (i
> linuxthreads_breakpoint_last
)
1415 linuxthreads_breakpoint_zombie
[i
].pid
= rpid
;
1416 linuxthreads_breakpoint_last
++;
1418 linuxthreads_breakpoint_zombie
[i
].pc
1419 = read_pc_pid (pid_to_ptid (rpid
));
1420 linuxthreads_breakpoint_zombie
[i
].step
= 1;
1424 if (WSTOPSIG(status
) != SIGSTOP
)
1426 for (i
= 0; i
< last
; i
++)
1427 if (wstatus
[i
] == status
)
1431 wstatus
[last
++] = status
;
1434 child_resume (pid_to_ptid (rpid
), 1, TARGET_SIGNAL_0
);
1437 if (linuxthreads_inferior_pid
)
1438 linuxthreads_inferior_pid
= 0;
1441 if (linuxthreads_max
&& !stop_soon_quietly
)
1443 if (linuxthreads_max
1444 && WIFSTOPPED(status
)
1445 && WSTOPSIG(status
) == SIGSTOP
)
1447 /* Skip SIGSTOP signals. */
1448 if (!linuxthreads_pending_status (rpid
))
1450 if (linuxthreads_step_pid
== rpid
)
1452 child_resume (pid_to_ptid (rpid
), 1,
1453 linuxthreads_step_signo
);
1457 child_resume (pid_to_ptid (rpid
), 0, TARGET_SIGNAL_0
);
1463 /* Do no report exit status of cloned threads. */
1464 if (WIFEXITED(status
))
1466 if (rpid
== linuxthreads_initial_pid
)
1467 linuxthreads_exit_status
= status
;
1469 /* Remove any zombie breakpoint. */
1470 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1471 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1473 REMOVE_BREAKPOINT_ZOMBIE(i
);
1481 /* Deal with zombie breakpoint */
1482 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1483 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1486 if (i
<= linuxthreads_breakpoint_last
)
1488 /* There is a potential zombie breakpoint */
1489 if (WIFEXITED(status
)
1490 || linuxthreads_breakpoint_zombie
[i
].pc
1491 != read_pc_pid (pid_to_ptid (rpid
)))
1493 /* The current pc is out of zombie breakpoint. */
1494 REMOVE_BREAKPOINT_ZOMBIE(i
);
1496 else if (!linuxthreads_breakpoint_zombie
[i
].step
1497 && WIFSTOPPED(status
) && WSTOPSIG(status
) == SIGTRAP
)
1499 /* This is a real one ==> decrement PC and restart. */
1500 write_pc_pid (linuxthreads_breakpoint_zombie
[i
].pc
1501 - DECR_PC_AFTER_BREAK
, pid_to_ptid (rpid
));
1502 if (linuxthreads_step_pid
== rpid
)
1504 child_resume (pid_to_ptid (rpid
), 1, linuxthreads_step_signo
);
1508 child_resume (pid_to_ptid (rpid
), 0, TARGET_SIGNAL_0
);
1514 /* Walk through linuxthreads array in order to stop them */
1515 if (linuxthreads_breakpoints_inserted
)
1516 update_stop_threads (rpid
);
1519 else if (rpid
!= PIDGET (inferior_ptid
))
1522 store_waitstatus (ourstatus
, status
);
1524 if (linuxthreads_attach_pending
&& !stop_soon_quietly
)
1527 if (!using_thread_db
)
1529 target_write_memory (linuxthreads_debug
,
1530 (char *) &on
, sizeof (on
));
1531 update_stop_threads (rpid
);
1533 linuxthreads_attach_pending
= 0;
1536 if (linuxthreads_breakpoints_inserted
1537 && WIFSTOPPED(status
)
1538 && WSTOPSIG(status
) == SIGTRAP
)
1539 linuxthreads_breakpoint_pid
= rpid
;
1540 else if (linuxthreads_breakpoint_pid
)
1541 linuxthreads_breakpoint_pid
= 0;
1543 return pid_to_ptid (rpid
);
1547 /* Fork an inferior process, and start debugging it with ptrace. */
1550 linuxthreads_create_inferior (char *exec_file
, char *allargs
, char **env
)
1552 if (!exec_file
&& !exec_bfd
)
1554 error ("No executable file specified.\n\
1555 Use the \"file\" or \"exec-file\" command.");
1559 push_target (&linuxthreads_ops
);
1560 linuxthreads_breakpoints_inserted
= 1;
1561 linuxthreads_breakpoint_last
= -1;
1562 linuxthreads_wait_last
= -1;
1563 WSETSTOP (linuxthreads_exit_status
, 0);
1565 if (linuxthreads_max
)
1566 linuxthreads_attach_pending
= 1;
1568 child_ops
.to_create_inferior (exec_file
, allargs
, env
);
1572 linuxthreads_discard_global_state (void)
1574 linuxthreads_inferior_pid
= 0;
1575 linuxthreads_breakpoint_pid
= 0;
1576 linuxthreads_step_pid
= 0;
1577 linuxthreads_step_signo
= TARGET_SIGNAL_0
;
1578 linuxthreads_manager_pid
= 0;
1579 linuxthreads_initial_pid
= 0;
1580 linuxthreads_attach_pending
= 0;
1581 linuxthreads_max
= 0;
1584 /* Clean up after the inferior dies. */
1587 linuxthreads_mourn_inferior (void)
1589 if (linuxthreads_max
)
1592 target_write_memory (linuxthreads_debug
, (char *)&off
, sizeof (off
));
1594 linuxthreads_discard_global_state ();
1595 init_thread_list(); /* Destroy thread info */
1598 child_ops
.to_mourn_inferior ();
1600 unpush_target (&linuxthreads_ops
);
1603 /* Kill the inferior process */
1606 linuxthreads_kill (void)
1611 if (PIDGET (inferior_ptid
) == 0)
1614 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1616 /* Remove all threads status. */
1617 inferior_ptid
= pid_to_ptid (linuxthreads_manager_pid
);
1618 iterate_active_threads (kill_thread
, 1);
1621 kill_thread (PIDGET (inferior_ptid
));
1624 /* doing_quit_force solves a real problem, but I think a properly
1625 placed call to catch_errors would do the trick much more cleanly. */
1626 if (doing_quit_force
>= 0)
1628 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1630 /* Wait for thread to complete */
1631 while ((rpid
= waitpid (-1, &status
, __WCLONE
)) > 0)
1632 if (!WIFEXITED(status
))
1635 while ((rpid
= waitpid (-1, &status
, 0)) > 0)
1636 if (!WIFEXITED(status
))
1640 while ((rpid
= waitpid (PIDGET (inferior_ptid
), &status
, 0)) > 0)
1641 if (!WIFEXITED(status
))
1642 ptrace (PT_KILL
, PIDGET (inferior_ptid
), (PTRACE_ARG3_TYPE
) 0, 0);
1646 /* Wait for all threads. */
1649 rpid
= waitpid (-1, &status
, __WCLONE
| WNOHANG
);
1651 while (rpid
> 0 || errno
== EINTR
);
1652 /* FIXME: should no longer need to handle EINTR here. */
1656 rpid
= waitpid (-1, &status
, WNOHANG
);
1658 while (rpid
> 0 || errno
== EINTR
);
1659 /* FIXME: should no longer need to handle EINTR here. */
1661 linuxthreads_mourn_inferior ();
1664 /* Insert a breakpoint */
1667 linuxthreads_insert_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1669 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1671 linuxthreads_breakpoint_addr
= addr
;
1672 iterate_active_threads (insert_breakpoint
, 1);
1673 insert_breakpoint (linuxthreads_manager_pid
);
1676 return child_ops
.to_insert_breakpoint (addr
, contents_cache
);
1679 /* Remove a breakpoint */
1682 linuxthreads_remove_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1684 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1686 linuxthreads_breakpoint_addr
= addr
;
1687 iterate_active_threads (remove_breakpoint
, 1);
1688 remove_breakpoint (linuxthreads_manager_pid
);
1691 return child_ops
.to_remove_breakpoint (addr
, contents_cache
);
1694 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1697 linuxthreads_can_run (void)
1699 return child_suppress_run
;
1704 init_linuxthreads_ops (void)
1706 linuxthreads_ops
.to_shortname
= "linuxthreads";
1707 linuxthreads_ops
.to_longname
= "LINUX threads and pthread.";
1708 linuxthreads_ops
.to_doc
= "LINUX threads and pthread support.";
1709 linuxthreads_ops
.to_attach
= linuxthreads_attach
;
1710 linuxthreads_ops
.to_detach
= linuxthreads_detach
;
1711 linuxthreads_ops
.to_resume
= linuxthreads_resume
;
1712 linuxthreads_ops
.to_wait
= linuxthreads_wait
;
1713 linuxthreads_ops
.to_kill
= linuxthreads_kill
;
1714 linuxthreads_ops
.to_can_run
= linuxthreads_can_run
;
1715 linuxthreads_ops
.to_stratum
= thread_stratum
;
1716 linuxthreads_ops
.to_insert_breakpoint
= linuxthreads_insert_breakpoint
;
1717 linuxthreads_ops
.to_remove_breakpoint
= linuxthreads_remove_breakpoint
;
1718 linuxthreads_ops
.to_create_inferior
= linuxthreads_create_inferior
;
1719 linuxthreads_ops
.to_mourn_inferior
= linuxthreads_mourn_inferior
;
1720 linuxthreads_ops
.to_thread_alive
= linuxthreads_thread_alive
;
1721 linuxthreads_ops
.to_pid_to_str
= linuxthreads_pid_to_str
;
1722 linuxthreads_ops
.to_magic
= OPS_MAGIC
;
1726 _initialize_linuxthreads (void)
1728 struct sigaction sact
;
1729 sigset_t linuxthreads_wait_mask
; /* sigset with SIGCHLD */
1731 init_linuxthreads_ops ();
1732 add_target (&linuxthreads_ops
);
1733 child_suppress_run
= 1;
1735 /* Hook onto the "new_objfile" event.
1736 * If someone else is already hooked onto the event,
1737 * then make sure he will be called after we are.
1739 target_new_objfile_chain
= target_new_objfile_hook
;
1740 target_new_objfile_hook
= linuxthreads_new_objfile
;
1742 /* Attach SIGCHLD handler */
1743 sact
.sa_handler
= sigchld_handler
;
1744 sigemptyset (&sact
.sa_mask
);
1746 sigaction (SIGCHLD
, &sact
, NULL
);
1748 /* initialize SIGCHLD mask */
1749 sigemptyset (&linuxthreads_wait_mask
);
1750 sigaddset (&linuxthreads_wait_mask
, SIGCHLD
);
1752 /* Use SIG_BLOCK to block receipt of SIGCHLD.
1753 The block_mask will allow us to wait for this signal explicitly. */
1754 sigprocmask(SIG_BLOCK
,
1755 &linuxthreads_wait_mask
,
1756 &linuxthreads_block_mask
);
1757 /* Make sure that linuxthreads_block_mask is not blocking SIGCHLD */
1758 sigdelset (&linuxthreads_block_mask
, SIGCHLD
);