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. */
1046 linuxthreads_prepare_to_proceed (int step
)
1048 if (!linuxthreads_max
1049 || !linuxthreads_manager_pid
1050 || !linuxthreads_breakpoint_pid
1051 || !breakpoint_here_p (
1052 read_pc_pid (pid_to_ptid (linuxthreads_breakpoint_pid
))))
1057 /* Mark the current inferior as single stepping process. */
1058 linuxthreads_step_pid
= PIDGET (inferior_ptid
);
1061 linuxthreads_inferior_pid
= linuxthreads_breakpoint_pid
;
1062 return linuxthreads_breakpoint_pid
;
1065 /* Convert a pid to printable form. */
1068 linuxthreads_pid_to_str (ptid_t ptid
)
1070 static char buf
[100];
1071 int pid
= PIDGET (ptid
);
1073 sprintf (buf
, "%s %d%s", linuxthreads_max
? "Thread" : "Pid", pid
,
1074 (pid
== linuxthreads_manager_pid
) ? " (manager thread)"
1075 : (pid
== linuxthreads_initial_pid
) ? " (initial thread)"
1081 /* Attach to process PID, then initialize for debugging it
1082 and wait for the trace-trap that results from attaching. */
1085 linuxthreads_attach (char *args
, int from_tty
)
1088 error_no_arg ("process-id to attach");
1090 push_target (&linuxthreads_ops
);
1091 linuxthreads_breakpoints_inserted
= 1;
1092 linuxthreads_breakpoint_last
= -1;
1093 linuxthreads_wait_last
= -1;
1094 WSETSTOP (linuxthreads_exit_status
, 0);
1096 child_ops
.to_attach (args
, from_tty
);
1098 if (linuxthreads_max
)
1099 linuxthreads_attach_pending
= 1;
1102 /* Take a program previously attached to and detaches it.
1103 The program resumes execution and will no longer stop
1104 on signals, etc. We'd better not have left any breakpoints
1105 in the program or it'll die when it hits one. For this
1106 to work, it may be necessary for the process to have been
1107 previously attached. It *might* work if the program was
1108 started via the normal ptrace (PTRACE_TRACEME). */
1111 linuxthreads_detach (char *args
, int from_tty
)
1113 if (linuxthreads_max
)
1118 target_write_memory (linuxthreads_debug
, (char *)&off
, sizeof (off
));
1120 /* Walk through linuxthreads array in order to detach known threads. */
1121 if (linuxthreads_manager_pid
!= 0)
1123 /* Get rid of all positive zombie breakpoints. */
1124 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1126 if (linuxthreads_breakpoint_zombie
[i
].step
)
1129 pid
= linuxthreads_breakpoint_zombie
[i
].pid
;
1130 if (!linuxthreads_thread_alive (pid_to_ptid (pid
)))
1133 if (linuxthreads_breakpoint_zombie
[i
].pc
1134 != read_pc_pid (pid_to_ptid (pid
)))
1137 /* Continue in STEP mode until the thread pc has moved or
1138 until SIGTRAP is found on the same PC. */
1139 if (linuxthreads_find_trap (pid
, 0)
1140 && linuxthreads_breakpoint_zombie
[i
].pc
1141 == read_pc_pid (pid_to_ptid (pid
)))
1142 write_pc_pid (linuxthreads_breakpoint_zombie
[i
].pc
1143 - DECR_PC_AFTER_BREAK
, pid_to_ptid (pid
));
1146 /* Detach thread after thread. */
1147 inferior_ptid
= pid_to_ptid (linuxthreads_manager_pid
);
1148 iterate_active_threads (detach_thread
, 1);
1150 /* Remove pending SIGTRAP and SIGSTOP */
1151 linuxthreads_find_trap (PIDGET (inferior_ptid
), 1);
1153 linuxthreads_wait_last
= -1;
1154 WSETSTOP (linuxthreads_exit_status
, 0);
1157 linuxthreads_inferior_pid
= 0;
1158 linuxthreads_breakpoint_pid
= 0;
1159 linuxthreads_step_pid
= 0;
1160 linuxthreads_step_signo
= TARGET_SIGNAL_0
;
1161 linuxthreads_manager_pid
= 0;
1162 linuxthreads_initial_pid
= 0;
1163 linuxthreads_attach_pending
= 0;
1164 init_thread_list (); /* Destroy thread info */
1167 child_ops
.to_detach (args
, from_tty
);
1169 unpush_target (&linuxthreads_ops
);
1172 /* Resume execution of process PID. If STEP is nozero, then
1173 just single step it. If SIGNAL is nonzero, restart it with that
1174 signal activated. */
1177 linuxthreads_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1179 if (!linuxthreads_max
|| stop_soon_quietly
|| linuxthreads_manager_pid
== 0)
1181 child_ops
.to_resume (ptid
, step
, signo
);
1186 if (linuxthreads_inferior_pid
)
1188 /* Prepare resume of the last thread that hit a breakpoint */
1189 linuxthreads_breakpoints_inserted
= 0;
1190 rpid
= linuxthreads_inferior_pid
;
1191 linuxthreads_step_signo
= signo
;
1195 struct cleanup
*old_chain
= NULL
;
1198 if (PIDGET (ptid
) < 0)
1200 linuxthreads_step_pid
= step
? PIDGET (inferior_ptid
) : 0;
1201 linuxthreads_step_signo
= signo
;
1202 rpid
= PIDGET (inferior_ptid
);
1205 rpid
= PIDGET (ptid
);
1207 if (PIDGET (ptid
) < 0 || !step
)
1209 linuxthreads_breakpoints_inserted
= 1;
1211 /* Walk through linuxthreads array in order to resume threads */
1212 if (PIDGET (ptid
) >= 0 && !ptid_equal (inferior_ptid
, ptid
))
1214 old_chain
= save_inferior_ptid ();
1215 inferior_ptid
= ptid
;
1218 iterate_active_threads (resume_thread
, 0);
1219 if (linuxthreads_manager_pid
!= PIDGET (inferior_ptid
)
1220 && !linuxthreads_pending_status (linuxthreads_manager_pid
))
1221 resume_thread (linuxthreads_manager_pid
);
1224 linuxthreads_breakpoints_inserted
= 0;
1226 /* Deal with zombie breakpoint */
1227 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1228 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1230 if (linuxthreads_breakpoint_zombie
[i
].pc
1231 != read_pc_pid (pid_to_ptid (rpid
)))
1233 /* The current pc is out of zombie breakpoint. */
1234 REMOVE_BREAKPOINT_ZOMBIE(i
);
1239 if (old_chain
!= NULL
)
1240 do_cleanups (old_chain
);
1243 /* Resume initial thread. */
1244 /* [unles it has a wait event pending] */
1245 if (!linuxthreads_pending_status (rpid
))
1247 child_ops
.to_resume (pid_to_ptid (rpid
), step
, signo
);
1252 /* Abstract out the child_wait functionality. */
1254 linux_child_wait (int pid
, int *rpid
, int *status
)
1258 /* Note: inftarg has these inside the loop. */
1259 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1260 attached process. */
1263 errno
= save_errno
= 0;
1267 *rpid
= waitpid (pid
, status
, __WCLONE
| WNOHANG
);
1272 /* Got an event -- break out */
1275 if (errno
== EINTR
) /* interrupted by signal, try again */
1281 *rpid
= waitpid (pid
, status
, WNOHANG
);
1284 /* Got an event -- break out */
1291 if (errno
!= 0 && save_errno
!= 0)
1295 sigsuspend(&linuxthreads_block_mask
);
1297 clear_sigio_trap ();
1298 clear_sigint_trap ();
1300 return errno
? errno
: save_errno
;
1304 /* Wait for any threads to stop. We may have to convert PID from a thread id
1305 to a LWP id, and vice versa on the way out. */
1308 linuxthreads_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1315 int pid
= PIDGET (ptid
);
1317 if (linuxthreads_max
&& !linuxthreads_breakpoints_inserted
)
1318 wstatus
= alloca (LINUXTHREAD_NSIG
* sizeof (int));
1320 /* See if the inferior has chosen values for its signals yet. By
1321 checking for them here, we can be sure we've updated GDB's signal
1322 handling table before the inferior ever gets one of them. (Well,
1323 before we notice, anyway.) */
1324 check_all_signal_numbers ();
1328 if (!linuxthreads_max
)
1330 else if (!linuxthreads_breakpoints_inserted
)
1332 if (linuxthreads_inferior_pid
)
1333 pid
= linuxthreads_inferior_pid
;
1335 pid
= PIDGET (inferior_ptid
);
1338 else if (pid
< 0 && linuxthreads_wait_last
>= 0)
1340 status
= linuxthreads_wait_status
[linuxthreads_wait_last
];
1341 rpid
= linuxthreads_wait_pid
[linuxthreads_wait_last
--];
1343 else if (pid
> 0 && linuxthreads_pending_status (pid
))
1345 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
1346 if (linuxthreads_wait_pid
[i
] == pid
)
1352 status
= linuxthreads_wait_status
[i
];
1354 if (i
< linuxthreads_wait_last
)
1356 linuxthreads_wait_status
[i
] =
1357 linuxthreads_wait_status
[linuxthreads_wait_last
];
1358 linuxthreads_wait_pid
[i
] =
1359 linuxthreads_wait_pid
[linuxthreads_wait_last
];
1361 linuxthreads_wait_last
--;
1371 save_errno
= linux_child_wait (pid
, &rpid
, &status
);
1375 if (WIFEXITED(linuxthreads_exit_status
))
1377 store_waitstatus (ourstatus
, linuxthreads_exit_status
);
1378 return inferior_ptid
;
1383 (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
1384 safe_strerror (save_errno
));
1385 /* Claim it exited with unknown signal. */
1386 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1387 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1388 return pid_to_ptid (-1);
1392 /* We have now gotten a new event from waitpid above. */
1394 /* Signals arrive in any order. So get all signals until
1395 SIGTRAP and resend previous ones to be held after. */
1396 if (linuxthreads_max
1397 && !linuxthreads_breakpoints_inserted
1398 && WIFSTOPPED(status
))
1399 if (WSTOPSIG(status
) == SIGTRAP
)
1403 kill (rpid
, WSTOPSIG(wstatus
[last
]));
1406 /* insert negative zombie breakpoint */
1407 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1408 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1410 if (i
> linuxthreads_breakpoint_last
)
1412 linuxthreads_breakpoint_zombie
[i
].pid
= rpid
;
1413 linuxthreads_breakpoint_last
++;
1415 linuxthreads_breakpoint_zombie
[i
].pc
1416 = read_pc_pid (pid_to_ptid (rpid
));
1417 linuxthreads_breakpoint_zombie
[i
].step
= 1;
1421 if (WSTOPSIG(status
) != SIGSTOP
)
1423 for (i
= 0; i
< last
; i
++)
1424 if (wstatus
[i
] == status
)
1428 wstatus
[last
++] = status
;
1431 child_resume (pid_to_ptid (rpid
), 1, TARGET_SIGNAL_0
);
1434 if (linuxthreads_inferior_pid
)
1435 linuxthreads_inferior_pid
= 0;
1438 if (linuxthreads_max
&& !stop_soon_quietly
)
1440 if (linuxthreads_max
1441 && WIFSTOPPED(status
)
1442 && WSTOPSIG(status
) == SIGSTOP
)
1444 /* Skip SIGSTOP signals. */
1445 if (!linuxthreads_pending_status (rpid
))
1447 if (linuxthreads_step_pid
== rpid
)
1449 child_resume (pid_to_ptid (rpid
), 1,
1450 linuxthreads_step_signo
);
1454 child_resume (pid_to_ptid (rpid
), 0, TARGET_SIGNAL_0
);
1460 /* Do no report exit status of cloned threads. */
1461 if (WIFEXITED(status
))
1463 if (rpid
== linuxthreads_initial_pid
)
1464 linuxthreads_exit_status
= status
;
1466 /* Remove any zombie breakpoint. */
1467 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1468 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1470 REMOVE_BREAKPOINT_ZOMBIE(i
);
1478 /* Deal with zombie breakpoint */
1479 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1480 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1483 if (i
<= linuxthreads_breakpoint_last
)
1485 /* There is a potential zombie breakpoint */
1486 if (WIFEXITED(status
)
1487 || linuxthreads_breakpoint_zombie
[i
].pc
1488 != read_pc_pid (pid_to_ptid (rpid
)))
1490 /* The current pc is out of zombie breakpoint. */
1491 REMOVE_BREAKPOINT_ZOMBIE(i
);
1493 else if (!linuxthreads_breakpoint_zombie
[i
].step
1494 && WIFSTOPPED(status
) && WSTOPSIG(status
) == SIGTRAP
)
1496 /* This is a real one ==> decrement PC and restart. */
1497 write_pc_pid (linuxthreads_breakpoint_zombie
[i
].pc
1498 - DECR_PC_AFTER_BREAK
, pid_to_ptid (rpid
));
1499 if (linuxthreads_step_pid
== rpid
)
1501 child_resume (pid_to_ptid (rpid
), 1, linuxthreads_step_signo
);
1505 child_resume (pid_to_ptid (rpid
), 0, TARGET_SIGNAL_0
);
1511 /* Walk through linuxthreads array in order to stop them */
1512 if (linuxthreads_breakpoints_inserted
)
1513 update_stop_threads (rpid
);
1516 else if (rpid
!= PIDGET (inferior_ptid
))
1519 store_waitstatus (ourstatus
, status
);
1521 if (linuxthreads_attach_pending
&& !stop_soon_quietly
)
1524 if (!using_thread_db
)
1526 target_write_memory (linuxthreads_debug
,
1527 (char *) &on
, sizeof (on
));
1528 update_stop_threads (rpid
);
1530 linuxthreads_attach_pending
= 0;
1533 if (linuxthreads_breakpoints_inserted
1534 && WIFSTOPPED(status
)
1535 && WSTOPSIG(status
) == SIGTRAP
)
1536 linuxthreads_breakpoint_pid
= rpid
;
1537 else if (linuxthreads_breakpoint_pid
)
1538 linuxthreads_breakpoint_pid
= 0;
1540 return pid_to_ptid (rpid
);
1544 /* Fork an inferior process, and start debugging it with ptrace. */
1547 linuxthreads_create_inferior (char *exec_file
, char *allargs
, char **env
)
1549 if (!exec_file
&& !exec_bfd
)
1551 error ("No executable file specified.\n\
1552 Use the \"file\" or \"exec-file\" command.");
1556 push_target (&linuxthreads_ops
);
1557 linuxthreads_breakpoints_inserted
= 1;
1558 linuxthreads_breakpoint_last
= -1;
1559 linuxthreads_wait_last
= -1;
1560 WSETSTOP (linuxthreads_exit_status
, 0);
1562 if (linuxthreads_max
)
1563 linuxthreads_attach_pending
= 1;
1565 child_ops
.to_create_inferior (exec_file
, allargs
, env
);
1569 linuxthreads_discard_global_state (void)
1571 linuxthreads_inferior_pid
= 0;
1572 linuxthreads_breakpoint_pid
= 0;
1573 linuxthreads_step_pid
= 0;
1574 linuxthreads_step_signo
= TARGET_SIGNAL_0
;
1575 linuxthreads_manager_pid
= 0;
1576 linuxthreads_initial_pid
= 0;
1577 linuxthreads_attach_pending
= 0;
1578 linuxthreads_max
= 0;
1581 /* Clean up after the inferior dies. */
1584 linuxthreads_mourn_inferior (void)
1586 if (linuxthreads_max
)
1589 target_write_memory (linuxthreads_debug
, (char *)&off
, sizeof (off
));
1591 linuxthreads_discard_global_state ();
1592 init_thread_list(); /* Destroy thread info */
1595 child_ops
.to_mourn_inferior ();
1597 unpush_target (&linuxthreads_ops
);
1600 /* Kill the inferior process */
1603 linuxthreads_kill (void)
1608 if (PIDGET (inferior_ptid
) == 0)
1611 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1613 /* Remove all threads status. */
1614 inferior_ptid
= pid_to_ptid (linuxthreads_manager_pid
);
1615 iterate_active_threads (kill_thread
, 1);
1618 kill_thread (PIDGET (inferior_ptid
));
1621 /* doing_quit_force solves a real problem, but I think a properly
1622 placed call to catch_errors would do the trick much more cleanly. */
1623 if (doing_quit_force
>= 0)
1625 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1627 /* Wait for thread to complete */
1628 while ((rpid
= waitpid (-1, &status
, __WCLONE
)) > 0)
1629 if (!WIFEXITED(status
))
1632 while ((rpid
= waitpid (-1, &status
, 0)) > 0)
1633 if (!WIFEXITED(status
))
1637 while ((rpid
= waitpid (PIDGET (inferior_ptid
), &status
, 0)) > 0)
1638 if (!WIFEXITED(status
))
1639 ptrace (PT_KILL
, PIDGET (inferior_ptid
), (PTRACE_ARG3_TYPE
) 0, 0);
1643 /* Wait for all threads. */
1646 rpid
= waitpid (-1, &status
, __WCLONE
| WNOHANG
);
1648 while (rpid
> 0 || errno
== EINTR
);
1649 /* FIXME: should no longer need to handle EINTR here. */
1653 rpid
= waitpid (-1, &status
, WNOHANG
);
1655 while (rpid
> 0 || errno
== EINTR
);
1656 /* FIXME: should no longer need to handle EINTR here. */
1658 linuxthreads_mourn_inferior ();
1661 /* Insert a breakpoint */
1664 linuxthreads_insert_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1666 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1668 linuxthreads_breakpoint_addr
= addr
;
1669 iterate_active_threads (insert_breakpoint
, 1);
1670 insert_breakpoint (linuxthreads_manager_pid
);
1673 return child_ops
.to_insert_breakpoint (addr
, contents_cache
);
1676 /* Remove a breakpoint */
1679 linuxthreads_remove_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1681 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1683 linuxthreads_breakpoint_addr
= addr
;
1684 iterate_active_threads (remove_breakpoint
, 1);
1685 remove_breakpoint (linuxthreads_manager_pid
);
1688 return child_ops
.to_remove_breakpoint (addr
, contents_cache
);
1691 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1694 linuxthreads_can_run (void)
1696 return child_suppress_run
;
1701 init_linuxthreads_ops (void)
1703 linuxthreads_ops
.to_shortname
= "linuxthreads";
1704 linuxthreads_ops
.to_longname
= "LINUX threads and pthread.";
1705 linuxthreads_ops
.to_doc
= "LINUX threads and pthread support.";
1706 linuxthreads_ops
.to_attach
= linuxthreads_attach
;
1707 linuxthreads_ops
.to_detach
= linuxthreads_detach
;
1708 linuxthreads_ops
.to_resume
= linuxthreads_resume
;
1709 linuxthreads_ops
.to_wait
= linuxthreads_wait
;
1710 linuxthreads_ops
.to_kill
= linuxthreads_kill
;
1711 linuxthreads_ops
.to_can_run
= linuxthreads_can_run
;
1712 linuxthreads_ops
.to_stratum
= thread_stratum
;
1713 linuxthreads_ops
.to_insert_breakpoint
= linuxthreads_insert_breakpoint
;
1714 linuxthreads_ops
.to_remove_breakpoint
= linuxthreads_remove_breakpoint
;
1715 linuxthreads_ops
.to_create_inferior
= linuxthreads_create_inferior
;
1716 linuxthreads_ops
.to_mourn_inferior
= linuxthreads_mourn_inferior
;
1717 linuxthreads_ops
.to_thread_alive
= linuxthreads_thread_alive
;
1718 linuxthreads_ops
.to_pid_to_str
= linuxthreads_pid_to_str
;
1719 linuxthreads_ops
.to_magic
= OPS_MAGIC
;
1723 _initialize_linuxthreads (void)
1725 struct sigaction sact
;
1726 sigset_t linuxthreads_wait_mask
; /* sigset with SIGCHLD */
1728 init_linuxthreads_ops ();
1729 add_target (&linuxthreads_ops
);
1730 child_suppress_run
= 1;
1732 /* Hook onto the "new_objfile" event.
1733 * If someone else is already hooked onto the event,
1734 * then make sure he will be called after we are.
1736 target_new_objfile_chain
= target_new_objfile_hook
;
1737 target_new_objfile_hook
= linuxthreads_new_objfile
;
1739 /* Attach SIGCHLD handler */
1740 sact
.sa_handler
= sigchld_handler
;
1741 sigemptyset (&sact
.sa_mask
);
1743 sigaction (SIGCHLD
, &sact
, NULL
);
1745 /* initialize SIGCHLD mask */
1746 sigemptyset (&linuxthreads_wait_mask
);
1747 sigaddset (&linuxthreads_wait_mask
, SIGCHLD
);
1749 /* Use SIG_BLOCK to block receipt of SIGCHLD.
1750 The block_mask will allow us to wait for this signal explicitly. */
1751 sigprocmask(SIG_BLOCK
,
1752 &linuxthreads_wait_mask
,
1753 &linuxthreads_block_mask
);
1754 /* Make sure that linuxthreads_block_mask is not blocking SIGCHLD */
1755 sigdelset (&linuxthreads_block_mask
, SIGCHLD
);