1 /* Low level interface for debugging GNU/Linux threads for GDB,
3 Copyright 1998, 1999 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This module implements the debugging interface of the linuxthreads package
22 of the glibc. This package implements a simple clone()-based implementation
23 of Posix threads for Linux. To use this module, be sure that you have at
24 least the version of the linuxthreads package that holds the support of
25 GDB (currently 0.8 included in the glibc-2.0.7).
27 Right now, the linuxthreads package does not care of priority scheduling,
28 so, neither this module does; In particular, the threads are resumed
29 in any order, which could lead to different scheduling than the one
30 happening when GDB does not control the execution.
32 The latest point is that ptrace(PT_ATTACH, ...) is intrusive in Linux:
33 When a process is attached, then the attaching process becomes the current
34 parent of the attached process, and the old parent has lost this child.
35 If the old parent does a wait[...](), then this child is no longer
36 considered by the kernel as a child of the old parent, thus leading to
37 results of the call different when the child is attached and when it's not.
39 A fix has been submitted to the Linux community to solve this problem,
40 which consequences are not visible to the application itself, but on the
41 process which may wait() for the completion of the application (mostly,
42 it may consider that the application no longer exists (errno == ECHILD),
43 although it does, and thus being unable to get the exit status and resource
44 usage of the child. If by chance, it is able to wait() for the application
45 after it has died (by receiving first a SIGCHILD, and then doing a wait(),
46 then the exit status and resource usage may be wrong, because the
47 linuxthreads package heavily relies on wait() synchronization to keep
51 #include <sys/types.h> /* for pid_t */
52 #include <sys/ptrace.h> /* for PT_* flags */
53 #include "gdb_wait.h" /* for WUNTRACED and __WCLONE flags */
54 #include <signal.h> /* for struct sigaction and NSIG */
55 #include <sys/utsname.h>
60 #include "gdbthread.h"
62 #include "breakpoint.h"
65 #define PT_ATTACH PTRACE_ATTACH
68 #define PT_KILL PTRACE_KILL
71 #define PT_READ_U PTRACE_PEEKUSR
75 #define LINUXTHREAD_NSIG NSIG
78 #define LINUXTHREAD_NSIG _NSIG
82 extern int child_suppress_run
; /* make inftarg.c non-runnable */
83 struct target_ops linuxthreads_ops
; /* Forward declaration */
84 extern struct target_ops child_ops
; /* target vector for inftarg.c */
86 static CORE_ADDR linuxthreads_handles
; /* array of linuxthreads handles */
87 static CORE_ADDR linuxthreads_manager
; /* pid of linuxthreads manager thread */
88 static CORE_ADDR linuxthreads_initial
; /* pid of linuxthreads initial thread */
89 static CORE_ADDR linuxthreads_debug
; /* linuxthreads internal debug flag */
90 static CORE_ADDR linuxthreads_num
; /* number of valid handle entries */
92 static int linuxthreads_max
; /* Maximum number of linuxthreads.
93 Zero if this executable doesn't use
94 threads, or wasn't linked with a
95 debugger-friendly version of the
96 linuxthreads library. */
98 static int linuxthreads_sizeof_handle
; /* size of a linuxthreads handle */
99 static int linuxthreads_offset_descr
; /* h_descr offset of the linuxthreads
101 static int linuxthreads_offset_pid
; /* p_pid offset of the linuxthreads
104 static int linuxthreads_manager_pid
; /* manager pid */
105 static int linuxthreads_initial_pid
; /* initial pid */
107 /* These variables form a bag of threads with interesting status. If
108 wait_thread (PID) finds that PID stopped for some interesting
109 reason (i.e. anything other than stopped with SIGSTOP), then it
110 records its status in this queue. linuxthreads_wait and
111 linuxthreads_find_trap extract processes from here. */
112 static int *linuxthreads_wait_pid
; /* wait array of pid */
113 static int *linuxthreads_wait_status
; /* wait array of status */
114 static int linuxthreads_wait_last
; /* index of last valid elt in
115 linuxthreads_wait_{pid,status} */
117 static sigset_t linuxthreads_block_mask
; /* sigset without SIGCHLD */
119 static int linuxthreads_step_pid
; /* current stepped pid */
120 static int linuxthreads_step_signo
; /* current stepped target signal */
121 static int linuxthreads_exit_status
; /* exit status of initial thread */
123 static int linuxthreads_inferior_pid
; /* temporary internal inferior pid */
124 static int linuxthreads_breakpoint_pid
; /* last pid that hit a breakpoint */
125 static int linuxthreads_attach_pending
; /* attach command without wait */
127 static int linuxthreads_breakpoints_inserted
; /* any breakpoints inserted */
129 /* LinuxThreads uses certain signals for communication between
130 processes; we need to tell GDB to pass them through silently to the
131 inferior. The LinuxThreads library has global variables we can
132 read containing the relevant signal numbers, but since the signal
133 numbers are chosen at run-time, those variables aren't initialized
134 until the shared library's constructors have had a chance to run. */
136 struct linuxthreads_signal
{
138 /* The name of the LinuxThreads library variable that contains
139 the signal number. */
142 /* True if this variable must exist for us to debug properly. */
145 /* The variable's address in the inferior, or zero if the
146 LinuxThreads library hasn't been loaded into this inferior yet. */
149 /* The signal number, or zero if we don't know yet (either because
150 we haven't found the variable, or it hasn't been initialized).
151 This is an actual target signal number that you could pass to
152 `kill', not a GDB signal number. */
155 /* GDB's original settings for `stop' and `print' for this signal.
156 We restore them when the user selects a different executable.
157 Invariant: if sig->signal != 0, then sig->{stop,print} contain
158 the original settings. */
162 struct linuxthreads_signal linuxthreads_sig_restart
= {
163 "__pthread_sig_restart", 1, 0, 0, 0, 0
165 struct linuxthreads_signal linuxthreads_sig_cancel
= {
166 "__pthread_sig_cancel", 1, 0, 0, 0, 0
168 struct linuxthreads_signal linuxthreads_sig_debug
= {
169 "__pthread_sig_debug", 0, 0, 0, 0, 0
172 /* Set by thread_db module when it takes over the thread_stratum.
173 In that case we must:
174 a) refrain from turning on the debug signal, and
175 b) refrain from calling add_thread. */
177 int using_thread_db
= 0;
179 /* A table of breakpoint locations, one per PID. */
180 static struct linuxthreads_breakpoint
{
181 CORE_ADDR pc
; /* PC of breakpoint */
182 int pid
; /* pid of breakpoint */
183 int step
; /* whether the pc has been reached after sstep */
184 } *linuxthreads_breakpoint_zombie
; /* Zombie breakpoints array */
185 static int linuxthreads_breakpoint_last
; /* Last zombie breakpoint */
187 /* linuxthreads_{insert,remove}_breakpoint pass the breakpoint address
188 to {insert,remove}_breakpoint via this variable, since
189 iterate_active_threads doesn't provide any way to pass values
190 through to the worker function. */
191 static CORE_ADDR linuxthreads_breakpoint_addr
;
193 #define REMOVE_BREAKPOINT_ZOMBIE(_i) \
195 if ((_i) < linuxthreads_breakpoint_last) \
196 linuxthreads_breakpoint_zombie[(_i)] = \
197 linuxthreads_breakpoint_zombie[linuxthreads_breakpoint_last]; \
198 linuxthreads_breakpoint_last--; \
203 #ifndef PTRACE_XFER_TYPE
204 #define PTRACE_XFER_TYPE int
206 /* Check to see if the given thread is alive. */
208 linuxthreads_thread_alive (int pid
)
211 return ptrace (PT_READ_U
, pid
, (PTRACE_ARG3_TYPE
)0, 0) >= 0 || errno
== 0;
214 /* On detach(), find a SIGTRAP status. If stop is non-zero, find a
217 Make sure PID is ready to run, and free of interference from our
218 efforts to debug it (e.g., pending SIGSTOP or SIGTRAP signals). If
219 STOP is zero, just look for a SIGTRAP. If STOP is non-zero, look
220 for a SIGSTOP, too. Return non-zero if PID is alive and ready to
221 run; return zero if PID is dead.
223 PID may or may not be stopped at the moment, and we may or may not
224 have waited for it already. We check the linuxthreads_wait bag in
225 case we've already got a status for it. We may possibly wait for
228 PID may have signals waiting to be delivered. If they're caused by
229 our efforts to debug it, accept them with wait, but don't pass them
230 through to PID. Do pass all other signals through. */
232 linuxthreads_find_trap (int pid
, int stop
)
240 /* PID may have any number of signals pending. The kernel will
241 report each of them to us via wait, and then it's up to us to
242 pass them along to the process via ptrace, if we so choose.
244 We need to paw through the whole set until we've found a SIGTRAP
245 (or a SIGSTOP, if `stop' is set). We don't pass the SIGTRAP (or
246 SIGSTOP) through, but we do re-send all the others, so PID will
247 receive them when we resume it. */
248 int *wstatus
= alloca (LINUXTHREAD_NSIG
* sizeof (int));
251 /* Look at the pending status */
252 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
253 if (linuxthreads_wait_pid
[i
] == pid
)
255 status
= linuxthreads_wait_status
[i
];
257 /* Delete the i'th member of the table. Since the table is
258 unordered, we can do this simply by copying the table's
259 last element to the i'th position, and shrinking the table
261 if (i
< linuxthreads_wait_last
)
263 linuxthreads_wait_status
[i
] =
264 linuxthreads_wait_status
[linuxthreads_wait_last
];
265 linuxthreads_wait_pid
[i
] =
266 linuxthreads_wait_pid
[linuxthreads_wait_last
];
268 linuxthreads_wait_last
--;
270 if (!WIFSTOPPED(status
)) /* Thread has died */
273 if (WSTOPSIG(status
) == SIGTRAP
)
280 else if (WSTOPSIG(status
) == SIGSTOP
)
296 /* Make sure that we'll find what we're looking for. */
307 /* Catch all status until SIGTRAP and optionally SIGSTOP show up. */
310 /* resume the child every time... */
311 child_resume (pid
, 1, TARGET_SIGNAL_0
);
313 /* loop as long as errno == EINTR:
314 waitpid syscall may be aborted due to GDB receiving a signal.
315 FIXME: EINTR handling should no longer be necessary here, since
316 we now block SIGCHLD except in an explicit sigsuspend call. */
320 rpid
= waitpid (pid
, &status
, __WCLONE
);
330 /* There are a few reasons the wait call above may have
331 failed. If the thread manager dies, its children get
332 reparented, and this interferes with GDB waiting for
333 them, in some cases. Another possibility is that the
334 initial thread was not cloned, so calling wait with
335 __WCLONE won't find it. I think neither of these should
336 occur in modern Linux kernels --- they don't seem to in
338 rpid
= waitpid (pid
, &status
, 0);
344 perror_with_name ("find_trap/waitpid");
347 if (!WIFSTOPPED(status
)) /* Thread has died */
350 if (WSTOPSIG(status
) == SIGTRAP
)
351 if (!stop
|| found_stop
)
355 else if (WSTOPSIG(status
) != SIGSTOP
)
356 wstatus
[last
++] = status
;
366 /* Resend any other signals we noticed to the thread, to be received
367 when we continue it. */
370 kill (pid
, WSTOPSIG(wstatus
[last
]));
376 /* Cleanup stub for save_inferior_pid. */
378 restore_inferior_pid (void *arg
)
380 int *saved_pid_ptr
= arg
;
381 inferior_pid
= *saved_pid_ptr
;
385 /* Register a cleanup to restore the value of inferior_pid. */
386 static struct cleanup
*
387 save_inferior_pid (void)
391 saved_pid_ptr
= xmalloc (sizeof (int));
392 *saved_pid_ptr
= inferior_pid
;
393 return make_cleanup (restore_inferior_pid
, saved_pid_ptr
);
397 sigchld_handler (int signo
)
399 /* This handler is used to get an EINTR while doing waitpid()
400 when an event is received */
403 /* Have we already collected a wait status for PID in the
404 linuxthreads_wait bag? */
406 linuxthreads_pending_status (int pid
)
409 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
410 if (linuxthreads_wait_pid
[i
] == pid
)
416 /* Internal linuxthreads signal management */
418 /* Check in OBJFILE for the variable that holds the number for signal SIG.
419 We assume that we've already found other LinuxThreads-ish variables
420 in OBJFILE, so we complain if it's required, but not there.
421 Return true iff things are okay. */
423 find_signal_var (struct linuxthreads_signal
*sig
, struct objfile
*objfile
)
425 struct minimal_symbol
*ms
= lookup_minimal_symbol (sig
->var
, NULL
, objfile
);
431 fprintf_unfiltered (gdb_stderr
,
432 "Unable to find linuxthreads symbol \"%s\"\n",
443 sig
->addr
= SYMBOL_VALUE_ADDRESS (ms
);
449 find_all_signal_vars (struct objfile
*objfile
)
451 return ( find_signal_var (&linuxthreads_sig_restart
, objfile
)
452 && find_signal_var (&linuxthreads_sig_cancel
, objfile
)
453 && find_signal_var (&linuxthreads_sig_debug
, objfile
));
456 /* A struct complaint isn't appropriate here. */
457 static int complained_cannot_determine_thread_signal_number
= 0;
459 /* Check to see if the variable holding the signal number for SIG has
460 been initialized yet. If it has, tell GDB to pass that signal
461 through to the inferior silently. */
463 check_signal_number (struct linuxthreads_signal
*sig
)
468 /* We already know this signal number. */
472 /* We don't know the variable's address yet. */
475 if (target_read_memory (sig
->addr
, (char *)&num
, sizeof (num
))
478 /* If this happens once, it'll probably happen for all the
479 signals, so only complain once. */
480 if (! complained_cannot_determine_thread_signal_number
)
481 warning ("Cannot determine thread signal number; "
482 "GDB may report spurious signals.");
483 complained_cannot_determine_thread_signal_number
= 1;
488 /* It hasn't been initialized yet. */
491 /* We know sig->signal was zero, and is becoming non-zero, so it's
492 okay to sample GDB's original settings. */
494 sig
->stop
= signal_stop_update (target_signal_from_host (num
), 0);
495 sig
->print
= signal_print_update (target_signal_from_host (num
), 0);
499 check_all_signal_numbers (void)
501 /* If this isn't a LinuxThreads program, quit early. */
502 if (! linuxthreads_max
)
505 check_signal_number (&linuxthreads_sig_restart
);
506 check_signal_number (&linuxthreads_sig_cancel
);
507 check_signal_number (&linuxthreads_sig_debug
);
509 /* handle linuxthread exit */
510 if (linuxthreads_sig_debug
.signal
511 || linuxthreads_sig_restart
.signal
)
513 struct sigaction sact
;
515 sact
.sa_handler
= sigchld_handler
;
516 sigemptyset(&sact
.sa_mask
);
519 if (linuxthreads_sig_debug
.signal
> 0)
520 sigaction(linuxthreads_sig_cancel
.signal
, &sact
, NULL
);
522 sigaction(linuxthreads_sig_restart
.signal
, &sact
, NULL
);
527 /* Restore GDB's original settings for SIG.
528 This should only be called when we're no longer sure if we're
529 talking to an executable that uses LinuxThreads, so we clear the
530 signal number and variable address too. */
532 restore_signal (struct linuxthreads_signal
*sig
)
537 /* We know sig->signal was non-zero, and is becoming zero, so it's
538 okay to restore GDB's original settings. */
539 signal_stop_update (target_signal_from_host (sig
->signal
), sig
->stop
);
540 signal_print_update (target_signal_from_host (sig
->signal
), sig
->print
);
547 /* Restore GDB's original settings for all LinuxThreads signals.
548 This should only be called when we're no longer sure if we're
549 talking to an executable that uses LinuxThreads, so we clear the
550 signal number and variable address too. */
552 restore_all_signals (void)
554 restore_signal (&linuxthreads_sig_restart
);
555 restore_signal (&linuxthreads_sig_cancel
);
556 restore_signal (&linuxthreads_sig_debug
);
558 /* If it happens again, we should complain again. */
559 complained_cannot_determine_thread_signal_number
= 0;
565 /* Apply FUNC to the pid of each active thread. This consults the
566 inferior's handle table to find active threads.
568 If ALL is non-zero, process all threads.
569 If ALL is zero, skip threads with pending status. */
571 iterate_active_threads (func
, all
)
580 read_memory (linuxthreads_num
, (char *)&num
, sizeof (int));
582 for (i
= 0; i
< linuxthreads_max
&& num
> 0; i
++)
584 read_memory (linuxthreads_handles
+
585 linuxthreads_sizeof_handle
* i
+ linuxthreads_offset_descr
,
586 (char *)&descr
, sizeof (void *));
590 read_memory (descr
+ linuxthreads_offset_pid
,
591 (char *)&pid
, sizeof (pid_t
));
592 if (pid
> 0 && pid
!= linuxthreads_manager_pid
593 && (all
|| (!linuxthreads_pending_status (pid
))))
599 /* Insert a thread breakpoint at linuxthreads_breakpoint_addr.
600 This is the worker function for linuxthreads_insert_breakpoint,
601 which passes it to iterate_active_threads. */
603 insert_breakpoint (int pid
)
607 /* Remove (if any) the positive zombie breakpoint. */
608 for (j
= linuxthreads_breakpoint_last
; j
>= 0; j
--)
609 if (linuxthreads_breakpoint_zombie
[j
].pid
== pid
)
611 if ((linuxthreads_breakpoint_zombie
[j
].pc
- DECR_PC_AFTER_BREAK
612 == linuxthreads_breakpoint_addr
)
613 && !linuxthreads_breakpoint_zombie
[j
].step
)
614 REMOVE_BREAKPOINT_ZOMBIE(j
);
619 /* Note that we're about to remove a thread breakpoint at
620 linuxthreads_breakpoint_addr.
622 This is the worker function for linuxthreads_remove_breakpoint,
623 which passes it to iterate_active_threads. The actual work of
624 overwriting the breakpoint instruction is done by
625 child_ops.to_remove_breakpoint; here, we simply create a zombie
626 breakpoint if the thread's PC is pointing at the breakpoint being
629 remove_breakpoint (int pid
)
633 /* Insert a positive zombie breakpoint (if needed). */
634 for (j
= 0; j
<= linuxthreads_breakpoint_last
; j
++)
635 if (linuxthreads_breakpoint_zombie
[j
].pid
== pid
)
638 if (in_thread_list (pid
) && linuxthreads_thread_alive (pid
))
640 CORE_ADDR pc
= read_pc_pid (pid
);
641 if (linuxthreads_breakpoint_addr
== pc
- DECR_PC_AFTER_BREAK
642 && j
> linuxthreads_breakpoint_last
)
644 linuxthreads_breakpoint_zombie
[j
].pid
= pid
;
645 linuxthreads_breakpoint_zombie
[j
].pc
= pc
;
646 linuxthreads_breakpoint_zombie
[j
].step
= 0;
647 linuxthreads_breakpoint_last
++;
654 kill_thread (int pid
)
656 if (in_thread_list (pid
))
658 ptrace (PT_KILL
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
666 /* Resume a thread */
668 resume_thread (int pid
)
670 if (pid
!= inferior_pid
671 && in_thread_list (pid
)
672 && linuxthreads_thread_alive (pid
))
674 if (pid
== linuxthreads_step_pid
)
676 child_resume (pid
, 1, linuxthreads_step_signo
);
680 child_resume (pid
, 0, TARGET_SIGNAL_0
);
685 /* Detach a thread */
687 detach_thread (int pid
)
689 if (in_thread_list (pid
) && linuxthreads_thread_alive (pid
))
691 /* Remove pending SIGTRAP and SIGSTOP */
692 linuxthreads_find_trap (pid
, 1);
695 detach (TARGET_SIGNAL_0
);
696 inferior_pid
= linuxthreads_manager_pid
;
700 /* Attach a thread */
702 attach_thread (int pid
)
704 if (ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0) != 0)
705 perror_with_name ("attach_thread");
710 stop_thread (int pid
)
712 if (pid
!= inferior_pid
)
714 if (in_thread_list (pid
))
718 else if (ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0) == 0)
720 if (!linuxthreads_attach_pending
)
721 printf_filtered ("[New %s]\n", target_pid_to_str (pid
));
723 if (linuxthreads_sig_debug
.signal
)
725 /* After a new thread in glibc 2.1 signals gdb its existence,
726 it suspends itself and wait for linuxthreads_sig_restart,
727 now we can wake it up. */
728 kill (pid
, linuxthreads_sig_restart
.signal
);
732 perror_with_name ("ptrace in stop_thread");
736 /* Wait for a thread */
738 wait_thread (int pid
)
743 if (pid
!= inferior_pid
&& in_thread_list (pid
))
745 /* loop as long as errno == EINTR:
746 waitpid syscall may be aborted if GDB receives a signal.
747 FIXME: EINTR handling should no longer be necessary here, since
748 we now block SIGCHLD except during an explicit sigsuspend call. */
751 /* Get first pid status. */
752 rpid
= waitpid(pid
, &status
, __WCLONE
);
762 /* There are two reasons this might have failed:
764 1) PID is the initial thread, which wasn't cloned, so
765 passing the __WCLONE flag to waitpid prevented us from
768 2) The manager thread is the parent of all but the
769 initial thread; if it dies, the children will all be
770 reparented to init, which will wait for them. This means
771 our call to waitpid won't find them.
773 Actually, based on a casual look at the 2.0.36 kernel
774 code, I don't think either of these cases happen. But I
775 don't have things set up for remotely debugging the
776 kernel, so I'm not sure. And perhaps older kernels
778 rpid
= waitpid(pid
, &status
, 0);
783 if (errno
!= EINTR
&& linuxthreads_thread_alive (pid
))
784 perror_with_name ("wait_thread/waitpid");
786 /* the thread is dead. */
789 if (!WIFSTOPPED(status
) || WSTOPSIG(status
) != SIGSTOP
)
791 linuxthreads_wait_pid
[++linuxthreads_wait_last
] = pid
;
792 linuxthreads_wait_status
[linuxthreads_wait_last
] = status
;
797 /* Walk through the linuxthreads handles in order to detect all
798 threads and stop them */
800 update_stop_threads (int test_pid
)
802 struct cleanup
*old_chain
= NULL
;
804 check_all_signal_numbers ();
806 if (linuxthreads_manager_pid
== 0)
808 if (linuxthreads_manager
)
810 if (test_pid
> 0 && test_pid
!= inferior_pid
)
812 old_chain
= save_inferior_pid ();
813 inferior_pid
= test_pid
;
815 read_memory (linuxthreads_manager
,
816 (char *)&linuxthreads_manager_pid
, sizeof (pid_t
));
818 if (linuxthreads_initial
)
820 if (test_pid
> 0 && test_pid
!= inferior_pid
)
822 old_chain
= save_inferior_pid ();
823 inferior_pid
= test_pid
;
825 read_memory(linuxthreads_initial
,
826 (char *)&linuxthreads_initial_pid
, sizeof (pid_t
));
830 if (linuxthreads_manager_pid
!= 0)
832 if (old_chain
== NULL
&& test_pid
> 0 &&
833 test_pid
!= inferior_pid
&& linuxthreads_thread_alive (test_pid
))
835 old_chain
= save_inferior_pid ();
836 inferior_pid
= test_pid
;
839 if (linuxthreads_thread_alive (inferior_pid
))
843 if (test_pid
!= linuxthreads_manager_pid
844 && !linuxthreads_pending_status (linuxthreads_manager_pid
))
846 stop_thread (linuxthreads_manager_pid
);
847 wait_thread (linuxthreads_manager_pid
);
849 if (!in_thread_list (test_pid
))
851 if (!linuxthreads_attach_pending
)
852 printf_filtered ("[New %s]\n",
853 target_pid_to_str (test_pid
));
854 add_thread (test_pid
);
855 if (linuxthreads_sig_debug
.signal
856 && inferior_pid
== test_pid
)
858 /* After a new thread in glibc 2.1 signals gdb its
859 existence, it suspends itself and wait for
860 linuxthreads_sig_restart, now we can wake it up. */
861 kill (test_pid
, linuxthreads_sig_restart
.signal
);
865 iterate_active_threads (stop_thread
, 0);
866 iterate_active_threads (wait_thread
, 0);
870 if (old_chain
!= NULL
)
871 do_cleanups (old_chain
);
874 /* This routine is called whenever a new symbol table is read in, or
875 when all symbol tables are removed. linux-thread event handling
876 can only be initialized when we find the right variables in
877 libpthread.so. Since it's a shared library, those variables don't
878 show up until the library gets mapped and the symbol table is read
881 /* This new_objfile event is now managed by a chained function pointer.
882 * It is the callee's responsability to call the next client on the chain.
885 /* Saved pointer to previous owner of the new_objfile event. */
886 static void (*target_new_objfile_chain
) (struct objfile
*);
889 linuxthreads_new_objfile (struct objfile
*objfile
)
891 struct minimal_symbol
*ms
;
893 /* Call predecessor on chain, if any.
894 Calling the new module first allows it to dominate,
895 if it finds its compatible libraries. */
897 if (target_new_objfile_chain
)
898 target_new_objfile_chain (objfile
);
902 /* We're starting an entirely new executable, so we can no
903 longer be sure that it uses LinuxThreads. Restore the signal
904 flags to their original states. */
905 restore_all_signals ();
907 /* Indicate that we don't know anything's address any more. */
908 linuxthreads_max
= 0;
913 /* If we've already found our variables in another objfile, don't
914 bother looking for them again. */
915 if (linuxthreads_max
)
918 if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL
, objfile
))
919 /* This object file isn't the pthreads library. */
922 if ((ms
= lookup_minimal_symbol ("__pthread_threads_debug",
923 NULL
, objfile
)) == NULL
)
925 /* The debugging-aware libpthreads is not present in this objfile */
927 This program seems to use POSIX threads, but the thread library used\n\
928 does not support debugging. This may make using GDB difficult. Don't\n\
929 set breakpoints or single-step through code that might be executed by\n\
930 any thread other than the main thread.");
933 linuxthreads_debug
= SYMBOL_VALUE_ADDRESS (ms
);
935 /* Read internal structures configuration */
936 if ((ms
= lookup_minimal_symbol ("__pthread_sizeof_handle",
937 NULL
, objfile
)) == NULL
938 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
939 (char *)&linuxthreads_sizeof_handle
,
940 sizeof (linuxthreads_sizeof_handle
)) != 0)
942 fprintf_unfiltered (gdb_stderr
,
943 "Unable to find linuxthreads symbol \"%s\"\n",
944 "__pthread_sizeof_handle");
948 if ((ms
= lookup_minimal_symbol ("__pthread_offsetof_descr",
949 NULL
, objfile
)) == NULL
950 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
951 (char *)&linuxthreads_offset_descr
,
952 sizeof (linuxthreads_offset_descr
)) != 0)
954 fprintf_unfiltered (gdb_stderr
,
955 "Unable to find linuxthreads symbol \"%s\"\n",
956 "__pthread_offsetof_descr");
960 if ((ms
= lookup_minimal_symbol ("__pthread_offsetof_pid",
961 NULL
, objfile
)) == NULL
962 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
963 (char *)&linuxthreads_offset_pid
,
964 sizeof (linuxthreads_offset_pid
)) != 0)
966 fprintf_unfiltered (gdb_stderr
,
967 "Unable to find linuxthreads symbol \"%s\"\n",
968 "__pthread_offsetof_pid");
972 if (! find_all_signal_vars (objfile
))
975 /* Read adresses of internal structures to access */
976 if ((ms
= lookup_minimal_symbol ("__pthread_handles",
977 NULL
, objfile
)) == NULL
)
979 fprintf_unfiltered (gdb_stderr
,
980 "Unable to find linuxthreads symbol \"%s\"\n",
981 "__pthread_handles");
984 linuxthreads_handles
= SYMBOL_VALUE_ADDRESS (ms
);
986 if ((ms
= lookup_minimal_symbol ("__pthread_handles_num",
987 NULL
, objfile
)) == NULL
)
989 fprintf_unfiltered (gdb_stderr
,
990 "Unable to find linuxthreads symbol \"%s\"\n",
991 "__pthread_handles_num");
994 linuxthreads_num
= SYMBOL_VALUE_ADDRESS (ms
);
996 if ((ms
= lookup_minimal_symbol ("__pthread_manager_thread",
997 NULL
, objfile
)) == NULL
)
999 fprintf_unfiltered (gdb_stderr
,
1000 "Unable to find linuxthreads symbol \"%s\"\n",
1001 "__pthread_manager_thread");
1004 linuxthreads_manager
= SYMBOL_VALUE_ADDRESS (ms
) + linuxthreads_offset_pid
;
1006 if ((ms
= lookup_minimal_symbol ("__pthread_initial_thread",
1007 NULL
, objfile
)) == NULL
)
1009 fprintf_unfiltered (gdb_stderr
,
1010 "Unable to find linuxthreads symbol \"%s\"\n",
1011 "__pthread_initial_thread");
1014 linuxthreads_initial
= SYMBOL_VALUE_ADDRESS (ms
) + linuxthreads_offset_pid
;
1016 /* Search for this last, so it won't be set to a non-zero value unless
1017 we successfully found all the symbols above. */
1018 if ((ms
= lookup_minimal_symbol ("__pthread_threads_max",
1019 NULL
, objfile
)) == NULL
1020 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
1021 (char *)&linuxthreads_max
,
1022 sizeof (linuxthreads_max
)) != 0)
1024 fprintf_unfiltered (gdb_stderr
,
1025 "Unable to find linuxthreads symbol \"%s\"\n",
1026 "__pthread_threads_max");
1030 /* Allocate gdb internal structures */
1031 linuxthreads_wait_pid
=
1032 (int *) xmalloc (sizeof (int) * (linuxthreads_max
+ 1));
1033 linuxthreads_wait_status
=
1034 (int *) xmalloc (sizeof (int) * (linuxthreads_max
+ 1));
1035 linuxthreads_breakpoint_zombie
= (struct linuxthreads_breakpoint
*)
1036 xmalloc (sizeof (struct linuxthreads_breakpoint
) * (linuxthreads_max
+ 1));
1039 !linuxthreads_attach_pending
&&
1040 !using_thread_db
) /* suppressed by thread_db module */
1044 target_write_memory (linuxthreads_debug
, (char *)&on
, sizeof (on
));
1045 linuxthreads_attach_pending
= 1;
1046 update_stop_threads (inferior_pid
);
1047 linuxthreads_attach_pending
= 0;
1050 check_all_signal_numbers ();
1055 /* If we have switched threads from a one that stopped at breakpoint,
1056 return 1 otherwise 0. */
1059 linuxthreads_prepare_to_proceed (int step
)
1061 if (!linuxthreads_max
1062 || !linuxthreads_manager_pid
1063 || !linuxthreads_breakpoint_pid
1064 || !breakpoint_here_p (read_pc_pid (linuxthreads_breakpoint_pid
)))
1069 /* Mark the current inferior as single stepping process. */
1070 linuxthreads_step_pid
= inferior_pid
;
1073 linuxthreads_inferior_pid
= linuxthreads_breakpoint_pid
;
1074 return linuxthreads_breakpoint_pid
;
1077 /* Convert a pid to printable form. */
1080 linuxthreads_pid_to_str (int pid
)
1082 static char buf
[100];
1084 sprintf (buf
, "%s %d%s", linuxthreads_max
? "Thread" : "Pid", pid
,
1085 (pid
== linuxthreads_manager_pid
) ? " (manager thread)"
1086 : (pid
== linuxthreads_initial_pid
) ? " (initial thread)"
1092 /* Attach to process PID, then initialize for debugging it
1093 and wait for the trace-trap that results from attaching. */
1096 linuxthreads_attach (char *args
, int from_tty
)
1099 error_no_arg ("process-id to attach");
1101 push_target (&linuxthreads_ops
);
1102 linuxthreads_breakpoints_inserted
= 1;
1103 linuxthreads_breakpoint_last
= -1;
1104 linuxthreads_wait_last
= -1;
1105 WSETSTOP (linuxthreads_exit_status
, 0);
1107 child_ops
.to_attach (args
, from_tty
);
1109 if (linuxthreads_max
)
1110 linuxthreads_attach_pending
= 1;
1113 /* Take a program previously attached to and detaches it.
1114 The program resumes execution and will no longer stop
1115 on signals, etc. We'd better not have left any breakpoints
1116 in the program or it'll die when it hits one. For this
1117 to work, it may be necessary for the process to have been
1118 previously attached. It *might* work if the program was
1119 started via the normal ptrace (PTRACE_TRACEME). */
1122 linuxthreads_detach (char *args
, int from_tty
)
1124 if (linuxthreads_max
)
1129 target_write_memory (linuxthreads_debug
, (char *)&off
, sizeof (off
));
1131 /* Walk through linuxthreads array in order to detach known threads. */
1132 if (linuxthreads_manager_pid
!= 0)
1134 /* Get rid of all positive zombie breakpoints. */
1135 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1137 if (linuxthreads_breakpoint_zombie
[i
].step
)
1140 pid
= linuxthreads_breakpoint_zombie
[i
].pid
;
1141 if (!linuxthreads_thread_alive (pid
))
1144 if (linuxthreads_breakpoint_zombie
[i
].pc
!= read_pc_pid (pid
))
1147 /* Continue in STEP mode until the thread pc has moved or
1148 until SIGTRAP is found on the same PC. */
1149 if (linuxthreads_find_trap (pid
, 0)
1150 && linuxthreads_breakpoint_zombie
[i
].pc
== read_pc_pid (pid
))
1151 write_pc_pid (linuxthreads_breakpoint_zombie
[i
].pc
1152 - DECR_PC_AFTER_BREAK
, pid
);
1155 /* Detach thread after thread. */
1156 inferior_pid
= linuxthreads_manager_pid
;
1157 iterate_active_threads (detach_thread
, 1);
1159 /* Remove pending SIGTRAP and SIGSTOP */
1160 linuxthreads_find_trap (inferior_pid
, 1);
1162 linuxthreads_wait_last
= -1;
1163 WSETSTOP (linuxthreads_exit_status
, 0);
1166 linuxthreads_inferior_pid
= 0;
1167 linuxthreads_breakpoint_pid
= 0;
1168 linuxthreads_step_pid
= 0;
1169 linuxthreads_step_signo
= TARGET_SIGNAL_0
;
1170 linuxthreads_manager_pid
= 0;
1171 linuxthreads_initial_pid
= 0;
1172 linuxthreads_attach_pending
= 0;
1173 init_thread_list (); /* Destroy thread info */
1176 child_ops
.to_detach (args
, from_tty
);
1178 unpush_target (&linuxthreads_ops
);
1181 /* Resume execution of process PID. If STEP is nozero, then
1182 just single step it. If SIGNAL is nonzero, restart it with that
1183 signal activated. */
1186 linuxthreads_resume (int pid
, int step
, enum target_signal signo
)
1188 if (!linuxthreads_max
|| stop_soon_quietly
|| linuxthreads_manager_pid
== 0)
1190 child_ops
.to_resume (pid
, step
, signo
);
1195 if (linuxthreads_inferior_pid
)
1197 /* Prepare resume of the last thread that hit a breakpoint */
1198 linuxthreads_breakpoints_inserted
= 0;
1199 rpid
= linuxthreads_inferior_pid
;
1200 linuxthreads_step_signo
= signo
;
1204 struct cleanup
*old_chain
= NULL
;
1209 linuxthreads_step_pid
= step
? inferior_pid
: 0;
1210 linuxthreads_step_signo
= signo
;
1211 rpid
= inferior_pid
;
1216 if (pid
< 0 || !step
)
1218 linuxthreads_breakpoints_inserted
= 1;
1220 /* Walk through linuxthreads array in order to resume threads */
1221 if (pid
>= 0 && inferior_pid
!= pid
)
1223 old_chain
= save_inferior_pid ();
1227 iterate_active_threads (resume_thread
, 0);
1228 if (linuxthreads_manager_pid
!= inferior_pid
1229 && !linuxthreads_pending_status (linuxthreads_manager_pid
))
1230 resume_thread (linuxthreads_manager_pid
);
1233 linuxthreads_breakpoints_inserted
= 0;
1235 /* Deal with zombie breakpoint */
1236 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1237 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1239 if (linuxthreads_breakpoint_zombie
[i
].pc
!= read_pc_pid (rpid
))
1241 /* The current pc is out of zombie breakpoint. */
1242 REMOVE_BREAKPOINT_ZOMBIE(i
);
1247 if (old_chain
!= NULL
)
1248 do_cleanups (old_chain
);
1251 /* Resume initial thread. */
1252 /* [unles it has a wait event pending] */
1253 if (!linuxthreads_pending_status (rpid
))
1255 child_ops
.to_resume (rpid
, step
, signo
);
1260 /* Abstract out the child_wait functionality. */
1262 linux_child_wait (int pid
, int *rpid
, int *status
)
1266 /* Note: inftarg has these inside the loop. */
1267 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1268 attached process. */
1271 errno
= save_errno
= 0;
1275 *rpid
= waitpid (pid
, status
, __WCLONE
| WNOHANG
);
1280 /* Got an event -- break out */
1283 if (errno
== EINTR
) /* interrupted by signal, try again */
1289 *rpid
= waitpid (pid
, status
, WNOHANG
);
1292 /* Got an event -- break out */
1299 if (errno
!= 0 && save_errno
!= 0)
1303 sigsuspend(&linuxthreads_block_mask
);
1305 clear_sigio_trap ();
1306 clear_sigint_trap ();
1308 return errno
? errno
: save_errno
;
1312 /* Wait for any threads to stop. We may have to convert PID from a thread id
1313 to a LWP id, and vice versa on the way out. */
1316 linuxthreads_wait (int pid
, struct target_waitstatus
*ourstatus
)
1324 if (linuxthreads_max
&& !linuxthreads_breakpoints_inserted
)
1325 wstatus
= alloca (LINUXTHREAD_NSIG
* sizeof (int));
1327 /* See if the inferior has chosen values for its signals yet. By
1328 checking for them here, we can be sure we've updated GDB's signal
1329 handling table before the inferior ever gets one of them. (Well,
1330 before we notice, anyway.) */
1331 check_all_signal_numbers ();
1335 if (!linuxthreads_max
)
1337 else if (!linuxthreads_breakpoints_inserted
)
1339 if (linuxthreads_inferior_pid
)
1340 pid
= linuxthreads_inferior_pid
;
1345 else if (pid
< 0 && linuxthreads_wait_last
>= 0)
1347 status
= linuxthreads_wait_status
[linuxthreads_wait_last
];
1348 rpid
= linuxthreads_wait_pid
[linuxthreads_wait_last
--];
1350 else if (pid
> 0 && linuxthreads_pending_status (pid
))
1352 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
1353 if (linuxthreads_wait_pid
[i
] == pid
)
1359 status
= linuxthreads_wait_status
[i
];
1361 if (i
< linuxthreads_wait_last
)
1363 linuxthreads_wait_status
[i
] =
1364 linuxthreads_wait_status
[linuxthreads_wait_last
];
1365 linuxthreads_wait_pid
[i
] =
1366 linuxthreads_wait_pid
[linuxthreads_wait_last
];
1368 linuxthreads_wait_last
--;
1378 save_errno
= linux_child_wait (pid
, &rpid
, &status
);
1382 if (WIFEXITED(linuxthreads_exit_status
))
1384 store_waitstatus (ourstatus
, linuxthreads_exit_status
);
1385 return inferior_pid
;
1390 (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
1391 safe_strerror (save_errno
));
1392 /* Claim it exited with unknown signal. */
1393 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1394 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1399 /* We have now gotten a new event from waitpid above. */
1401 /* Signals arrive in any order. So get all signals until
1402 SIGTRAP and resend previous ones to be held after. */
1403 if (linuxthreads_max
1404 && !linuxthreads_breakpoints_inserted
1405 && WIFSTOPPED(status
))
1406 if (WSTOPSIG(status
) == SIGTRAP
)
1410 kill (rpid
, WSTOPSIG(wstatus
[last
]));
1413 /* insert negative zombie breakpoint */
1414 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1415 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1417 if (i
> linuxthreads_breakpoint_last
)
1419 linuxthreads_breakpoint_zombie
[i
].pid
= rpid
;
1420 linuxthreads_breakpoint_last
++;
1422 linuxthreads_breakpoint_zombie
[i
].pc
= read_pc_pid (rpid
);
1423 linuxthreads_breakpoint_zombie
[i
].step
= 1;
1427 if (WSTOPSIG(status
) != SIGSTOP
)
1429 for (i
= 0; i
< last
; i
++)
1430 if (wstatus
[i
] == status
)
1434 wstatus
[last
++] = status
;
1437 child_resume (rpid
, 1, TARGET_SIGNAL_0
);
1440 if (linuxthreads_inferior_pid
)
1441 linuxthreads_inferior_pid
= 0;
1444 if (linuxthreads_max
&& !stop_soon_quietly
)
1446 if (linuxthreads_max
1447 && WIFSTOPPED(status
)
1448 && WSTOPSIG(status
) == SIGSTOP
)
1450 /* Skip SIGSTOP signals. */
1451 if (!linuxthreads_pending_status (rpid
))
1453 if (linuxthreads_step_pid
== rpid
)
1455 child_resume (rpid
, 1, linuxthreads_step_signo
);
1459 child_resume (rpid
, 0, TARGET_SIGNAL_0
);
1465 /* Do no report exit status of cloned threads. */
1466 if (WIFEXITED(status
))
1468 if (rpid
== linuxthreads_initial_pid
)
1469 linuxthreads_exit_status
= status
;
1471 /* Remove any zombie breakpoint. */
1472 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1473 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1475 REMOVE_BREAKPOINT_ZOMBIE(i
);
1483 /* Deal with zombie breakpoint */
1484 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1485 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1488 if (i
<= linuxthreads_breakpoint_last
)
1490 /* There is a potential zombie breakpoint */
1491 if (WIFEXITED(status
)
1492 || linuxthreads_breakpoint_zombie
[i
].pc
!= read_pc_pid (rpid
))
1494 /* The current pc is out of zombie breakpoint. */
1495 REMOVE_BREAKPOINT_ZOMBIE(i
);
1497 else if (!linuxthreads_breakpoint_zombie
[i
].step
1498 && WIFSTOPPED(status
) && WSTOPSIG(status
) == SIGTRAP
)
1500 /* This is a real one ==> decrement PC and restart. */
1501 write_pc_pid (linuxthreads_breakpoint_zombie
[i
].pc
1502 - DECR_PC_AFTER_BREAK
, rpid
);
1503 if (linuxthreads_step_pid
== rpid
)
1505 child_resume (rpid
, 1, linuxthreads_step_signo
);
1509 child_resume (rpid
, 0, TARGET_SIGNAL_0
);
1515 /* Walk through linuxthreads array in order to stop them */
1516 if (linuxthreads_breakpoints_inserted
)
1517 update_stop_threads (rpid
);
1520 else if (rpid
!= inferior_pid
)
1523 store_waitstatus (ourstatus
, status
);
1525 if (linuxthreads_attach_pending
&& !stop_soon_quietly
)
1528 if (!using_thread_db
)
1530 target_write_memory (linuxthreads_debug
,
1531 (char *) &on
, sizeof (on
));
1532 update_stop_threads (rpid
);
1534 linuxthreads_attach_pending
= 0;
1537 if (linuxthreads_breakpoints_inserted
1538 && WIFSTOPPED(status
)
1539 && WSTOPSIG(status
) == SIGTRAP
)
1540 linuxthreads_breakpoint_pid
= rpid
;
1541 else if (linuxthreads_breakpoint_pid
)
1542 linuxthreads_breakpoint_pid
= 0;
1548 /* Fork an inferior process, and start debugging it with ptrace. */
1551 linuxthreads_create_inferior (char *exec_file
, char *allargs
, char **env
)
1553 if (!exec_file
&& !exec_bfd
)
1555 error ("No executable file specified.\n\
1556 Use the \"file\" or \"exec-file\" command.");
1560 push_target (&linuxthreads_ops
);
1561 linuxthreads_breakpoints_inserted
= 1;
1562 linuxthreads_breakpoint_last
= -1;
1563 linuxthreads_wait_last
= -1;
1564 WSETSTOP (linuxthreads_exit_status
, 0);
1566 if (linuxthreads_max
)
1567 linuxthreads_attach_pending
= 1;
1569 child_ops
.to_create_inferior (exec_file
, allargs
, env
);
1573 linuxthreads_discard_global_state (void)
1575 linuxthreads_inferior_pid
= 0;
1576 linuxthreads_breakpoint_pid
= 0;
1577 linuxthreads_step_pid
= 0;
1578 linuxthreads_step_signo
= TARGET_SIGNAL_0
;
1579 linuxthreads_manager_pid
= 0;
1580 linuxthreads_initial_pid
= 0;
1581 linuxthreads_attach_pending
= 0;
1582 linuxthreads_max
= 0;
1585 /* Clean up after the inferior dies. */
1588 linuxthreads_mourn_inferior (void)
1590 if (linuxthreads_max
)
1593 target_write_memory (linuxthreads_debug
, (char *)&off
, sizeof (off
));
1595 linuxthreads_discard_global_state ();
1596 init_thread_list(); /* Destroy thread info */
1599 child_ops
.to_mourn_inferior ();
1601 unpush_target (&linuxthreads_ops
);
1604 /* Kill the inferior process */
1607 linuxthreads_kill (void)
1612 if (inferior_pid
== 0)
1615 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1617 /* Remove all threads status. */
1618 inferior_pid
= linuxthreads_manager_pid
;
1619 iterate_active_threads (kill_thread
, 1);
1622 kill_thread (inferior_pid
);
1625 /* doing_quit_force solves a real problem, but I think a properly
1626 placed call to catch_errors would do the trick much more cleanly. */
1627 if (doing_quit_force
>= 0)
1629 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1631 /* Wait for thread to complete */
1632 while ((rpid
= waitpid (-1, &status
, __WCLONE
)) > 0)
1633 if (!WIFEXITED(status
))
1636 while ((rpid
= waitpid (-1, &status
, 0)) > 0)
1637 if (!WIFEXITED(status
))
1641 while ((rpid
= waitpid (inferior_pid
, &status
, 0)) > 0)
1642 if (!WIFEXITED(status
))
1643 ptrace (PT_KILL
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0);
1647 /* Wait for all threads. */
1650 rpid
= waitpid (-1, &status
, __WCLONE
| WNOHANG
);
1652 while (rpid
> 0 || errno
== EINTR
);
1653 /* FIXME: should no longer need to handle EINTR here. */
1657 rpid
= waitpid (-1, &status
, WNOHANG
);
1659 while (rpid
> 0 || errno
== EINTR
);
1660 /* FIXME: should no longer need to handle EINTR here. */
1662 linuxthreads_mourn_inferior ();
1665 /* Insert a breakpoint */
1668 linuxthreads_insert_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1670 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1672 linuxthreads_breakpoint_addr
= addr
;
1673 iterate_active_threads (insert_breakpoint
, 1);
1674 insert_breakpoint (linuxthreads_manager_pid
);
1677 return child_ops
.to_insert_breakpoint (addr
, contents_cache
);
1680 /* Remove a breakpoint */
1683 linuxthreads_remove_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1685 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1687 linuxthreads_breakpoint_addr
= addr
;
1688 iterate_active_threads (remove_breakpoint
, 1);
1689 remove_breakpoint (linuxthreads_manager_pid
);
1692 return child_ops
.to_remove_breakpoint (addr
, contents_cache
);
1695 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1698 linuxthreads_can_run (void)
1700 return child_suppress_run
;
1705 init_linuxthreads_ops (void)
1707 linuxthreads_ops
.to_shortname
= "linuxthreads";
1708 linuxthreads_ops
.to_longname
= "LINUX threads and pthread.";
1709 linuxthreads_ops
.to_doc
= "LINUX threads and pthread support.";
1710 linuxthreads_ops
.to_attach
= linuxthreads_attach
;
1711 linuxthreads_ops
.to_detach
= linuxthreads_detach
;
1712 linuxthreads_ops
.to_resume
= linuxthreads_resume
;
1713 linuxthreads_ops
.to_wait
= linuxthreads_wait
;
1714 linuxthreads_ops
.to_kill
= linuxthreads_kill
;
1715 linuxthreads_ops
.to_can_run
= linuxthreads_can_run
;
1716 linuxthreads_ops
.to_stratum
= thread_stratum
;
1717 linuxthreads_ops
.to_insert_breakpoint
= linuxthreads_insert_breakpoint
;
1718 linuxthreads_ops
.to_remove_breakpoint
= linuxthreads_remove_breakpoint
;
1719 linuxthreads_ops
.to_create_inferior
= linuxthreads_create_inferior
;
1720 linuxthreads_ops
.to_mourn_inferior
= linuxthreads_mourn_inferior
;
1721 linuxthreads_ops
.to_thread_alive
= linuxthreads_thread_alive
;
1722 linuxthreads_ops
.to_pid_to_str
= linuxthreads_pid_to_str
;
1723 linuxthreads_ops
.to_magic
= OPS_MAGIC
;
1727 _initialize_linuxthreads (void)
1729 struct sigaction sact
;
1730 sigset_t linuxthreads_wait_mask
; /* sigset with SIGCHLD */
1732 init_linuxthreads_ops ();
1733 add_target (&linuxthreads_ops
);
1734 child_suppress_run
= 1;
1736 /* Hook onto the "new_objfile" event.
1737 * If someone else is already hooked onto the event,
1738 * then make sure he will be called after we are.
1740 target_new_objfile_chain
= target_new_objfile_hook
;
1741 target_new_objfile_hook
= linuxthreads_new_objfile
;
1743 /* Attach SIGCHLD handler */
1744 sact
.sa_handler
= sigchld_handler
;
1745 sigemptyset (&sact
.sa_mask
);
1747 sigaction (SIGCHLD
, &sact
, NULL
);
1749 /* initialize SIGCHLD mask */
1750 sigemptyset (&linuxthreads_wait_mask
);
1751 sigaddset (&linuxthreads_wait_mask
, SIGCHLD
);
1753 /* Use SIG_BLOCK to block receipt of SIGCHLD.
1754 The block_mask will allow us to wait for this signal explicitly. */
1755 sigprocmask(SIG_BLOCK
,
1756 &linuxthreads_wait_mask
,
1757 &linuxthreads_block_mask
);
1758 /* Make sure that linuxthreads_block_mask is not blocking SIGCHLD */
1759 sigdelset (&linuxthreads_block_mask
, SIGCHLD
);