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 (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 (void (*func
) (int), int all
)
578 read_memory (linuxthreads_num
, (char *)&num
, sizeof (int));
580 for (i
= 0; i
< linuxthreads_max
&& num
> 0; i
++)
582 read_memory (linuxthreads_handles
+
583 linuxthreads_sizeof_handle
* i
+ linuxthreads_offset_descr
,
584 (char *)&descr
, sizeof (void *));
588 read_memory (descr
+ linuxthreads_offset_pid
,
589 (char *)&pid
, sizeof (pid_t
));
590 if (pid
> 0 && pid
!= linuxthreads_manager_pid
591 && (all
|| (!linuxthreads_pending_status (pid
))))
597 /* Insert a thread breakpoint at linuxthreads_breakpoint_addr.
598 This is the worker function for linuxthreads_insert_breakpoint,
599 which passes it to iterate_active_threads. */
601 insert_breakpoint (int pid
)
605 /* Remove (if any) the positive zombie breakpoint. */
606 for (j
= linuxthreads_breakpoint_last
; j
>= 0; j
--)
607 if (linuxthreads_breakpoint_zombie
[j
].pid
== pid
)
609 if ((linuxthreads_breakpoint_zombie
[j
].pc
- DECR_PC_AFTER_BREAK
610 == linuxthreads_breakpoint_addr
)
611 && !linuxthreads_breakpoint_zombie
[j
].step
)
612 REMOVE_BREAKPOINT_ZOMBIE(j
);
617 /* Note that we're about to remove a thread breakpoint at
618 linuxthreads_breakpoint_addr.
620 This is the worker function for linuxthreads_remove_breakpoint,
621 which passes it to iterate_active_threads. The actual work of
622 overwriting the breakpoint instruction is done by
623 child_ops.to_remove_breakpoint; here, we simply create a zombie
624 breakpoint if the thread's PC is pointing at the breakpoint being
627 remove_breakpoint (int pid
)
631 /* Insert a positive zombie breakpoint (if needed). */
632 for (j
= 0; j
<= linuxthreads_breakpoint_last
; j
++)
633 if (linuxthreads_breakpoint_zombie
[j
].pid
== pid
)
636 if (in_thread_list (pid
) && linuxthreads_thread_alive (pid
))
638 CORE_ADDR pc
= read_pc_pid (pid
);
639 if (linuxthreads_breakpoint_addr
== pc
- DECR_PC_AFTER_BREAK
640 && j
> linuxthreads_breakpoint_last
)
642 linuxthreads_breakpoint_zombie
[j
].pid
= pid
;
643 linuxthreads_breakpoint_zombie
[j
].pc
= pc
;
644 linuxthreads_breakpoint_zombie
[j
].step
= 0;
645 linuxthreads_breakpoint_last
++;
652 kill_thread (int pid
)
654 if (in_thread_list (pid
))
656 ptrace (PT_KILL
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
664 /* Resume a thread */
666 resume_thread (int pid
)
668 if (pid
!= inferior_pid
669 && in_thread_list (pid
)
670 && linuxthreads_thread_alive (pid
))
672 if (pid
== linuxthreads_step_pid
)
674 child_resume (pid
, 1, linuxthreads_step_signo
);
678 child_resume (pid
, 0, TARGET_SIGNAL_0
);
683 /* Detach a thread */
685 detach_thread (int pid
)
687 if (in_thread_list (pid
) && linuxthreads_thread_alive (pid
))
689 /* Remove pending SIGTRAP and SIGSTOP */
690 linuxthreads_find_trap (pid
, 1);
693 detach (TARGET_SIGNAL_0
);
694 inferior_pid
= linuxthreads_manager_pid
;
698 /* Attach a thread */
700 attach_thread (int pid
)
702 if (ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0) != 0)
703 perror_with_name ("attach_thread");
708 stop_thread (int pid
)
710 if (pid
!= inferior_pid
)
712 if (in_thread_list (pid
))
716 else if (ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0) == 0)
718 if (!linuxthreads_attach_pending
)
719 printf_filtered ("[New %s]\n", target_pid_to_str (pid
));
721 if (linuxthreads_sig_debug
.signal
)
723 /* After a new thread in glibc 2.1 signals gdb its existence,
724 it suspends itself and wait for linuxthreads_sig_restart,
725 now we can wake it up. */
726 kill (pid
, linuxthreads_sig_restart
.signal
);
730 perror_with_name ("ptrace in stop_thread");
734 /* Wait for a thread */
736 wait_thread (int pid
)
741 if (pid
!= inferior_pid
&& in_thread_list (pid
))
743 /* loop as long as errno == EINTR:
744 waitpid syscall may be aborted if GDB receives a signal.
745 FIXME: EINTR handling should no longer be necessary here, since
746 we now block SIGCHLD except during an explicit sigsuspend call. */
749 /* Get first pid status. */
750 rpid
= waitpid(pid
, &status
, __WCLONE
);
760 /* There are two reasons this might have failed:
762 1) PID is the initial thread, which wasn't cloned, so
763 passing the __WCLONE flag to waitpid prevented us from
766 2) The manager thread is the parent of all but the
767 initial thread; if it dies, the children will all be
768 reparented to init, which will wait for them. This means
769 our call to waitpid won't find them.
771 Actually, based on a casual look at the 2.0.36 kernel
772 code, I don't think either of these cases happen. But I
773 don't have things set up for remotely debugging the
774 kernel, so I'm not sure. And perhaps older kernels
776 rpid
= waitpid(pid
, &status
, 0);
781 if (errno
!= EINTR
&& linuxthreads_thread_alive (pid
))
782 perror_with_name ("wait_thread/waitpid");
784 /* the thread is dead. */
787 if (!WIFSTOPPED(status
) || WSTOPSIG(status
) != SIGSTOP
)
789 linuxthreads_wait_pid
[++linuxthreads_wait_last
] = pid
;
790 linuxthreads_wait_status
[linuxthreads_wait_last
] = status
;
795 /* Walk through the linuxthreads handles in order to detect all
796 threads and stop them */
798 update_stop_threads (int test_pid
)
800 struct cleanup
*old_chain
= NULL
;
802 check_all_signal_numbers ();
804 if (linuxthreads_manager_pid
== 0)
806 if (linuxthreads_manager
)
808 if (test_pid
> 0 && test_pid
!= inferior_pid
)
810 old_chain
= save_inferior_pid ();
811 inferior_pid
= test_pid
;
813 read_memory (linuxthreads_manager
,
814 (char *)&linuxthreads_manager_pid
, sizeof (pid_t
));
816 if (linuxthreads_initial
)
818 if (test_pid
> 0 && test_pid
!= inferior_pid
)
820 old_chain
= save_inferior_pid ();
821 inferior_pid
= test_pid
;
823 read_memory(linuxthreads_initial
,
824 (char *)&linuxthreads_initial_pid
, sizeof (pid_t
));
828 if (linuxthreads_manager_pid
!= 0)
830 if (old_chain
== NULL
&& test_pid
> 0 &&
831 test_pid
!= inferior_pid
&& linuxthreads_thread_alive (test_pid
))
833 old_chain
= save_inferior_pid ();
834 inferior_pid
= test_pid
;
837 if (linuxthreads_thread_alive (inferior_pid
))
841 if (test_pid
!= linuxthreads_manager_pid
842 && !linuxthreads_pending_status (linuxthreads_manager_pid
))
844 stop_thread (linuxthreads_manager_pid
);
845 wait_thread (linuxthreads_manager_pid
);
847 if (!in_thread_list (test_pid
))
849 if (!linuxthreads_attach_pending
)
850 printf_filtered ("[New %s]\n",
851 target_pid_to_str (test_pid
));
852 add_thread (test_pid
);
853 if (linuxthreads_sig_debug
.signal
854 && inferior_pid
== test_pid
)
856 /* After a new thread in glibc 2.1 signals gdb its
857 existence, it suspends itself and wait for
858 linuxthreads_sig_restart, now we can wake it up. */
859 kill (test_pid
, linuxthreads_sig_restart
.signal
);
863 iterate_active_threads (stop_thread
, 0);
864 iterate_active_threads (wait_thread
, 0);
868 if (old_chain
!= NULL
)
869 do_cleanups (old_chain
);
872 /* This routine is called whenever a new symbol table is read in, or
873 when all symbol tables are removed. linux-thread event handling
874 can only be initialized when we find the right variables in
875 libpthread.so. Since it's a shared library, those variables don't
876 show up until the library gets mapped and the symbol table is read
879 /* This new_objfile event is now managed by a chained function pointer.
880 * It is the callee's responsability to call the next client on the chain.
883 /* Saved pointer to previous owner of the new_objfile event. */
884 static void (*target_new_objfile_chain
) (struct objfile
*);
887 linuxthreads_new_objfile (struct objfile
*objfile
)
889 struct minimal_symbol
*ms
;
891 /* Call predecessor on chain, if any.
892 Calling the new module first allows it to dominate,
893 if it finds its compatible libraries. */
895 if (target_new_objfile_chain
)
896 target_new_objfile_chain (objfile
);
900 /* We're starting an entirely new executable, so we can no
901 longer be sure that it uses LinuxThreads. Restore the signal
902 flags to their original states. */
903 restore_all_signals ();
905 /* Indicate that we don't know anything's address any more. */
906 linuxthreads_max
= 0;
911 /* If we've already found our variables in another objfile, don't
912 bother looking for them again. */
913 if (linuxthreads_max
)
916 if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL
, objfile
))
917 /* This object file isn't the pthreads library. */
920 if ((ms
= lookup_minimal_symbol ("__pthread_threads_debug",
921 NULL
, objfile
)) == NULL
)
923 /* The debugging-aware libpthreads is not present in this objfile */
925 This program seems to use POSIX threads, but the thread library used\n\
926 does not support debugging. This may make using GDB difficult. Don't\n\
927 set breakpoints or single-step through code that might be executed by\n\
928 any thread other than the main thread.");
931 linuxthreads_debug
= SYMBOL_VALUE_ADDRESS (ms
);
933 /* Read internal structures configuration */
934 if ((ms
= lookup_minimal_symbol ("__pthread_sizeof_handle",
935 NULL
, objfile
)) == NULL
936 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
937 (char *)&linuxthreads_sizeof_handle
,
938 sizeof (linuxthreads_sizeof_handle
)) != 0)
940 fprintf_unfiltered (gdb_stderr
,
941 "Unable to find linuxthreads symbol \"%s\"\n",
942 "__pthread_sizeof_handle");
946 if ((ms
= lookup_minimal_symbol ("__pthread_offsetof_descr",
947 NULL
, objfile
)) == NULL
948 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
949 (char *)&linuxthreads_offset_descr
,
950 sizeof (linuxthreads_offset_descr
)) != 0)
952 fprintf_unfiltered (gdb_stderr
,
953 "Unable to find linuxthreads symbol \"%s\"\n",
954 "__pthread_offsetof_descr");
958 if ((ms
= lookup_minimal_symbol ("__pthread_offsetof_pid",
959 NULL
, objfile
)) == NULL
960 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
961 (char *)&linuxthreads_offset_pid
,
962 sizeof (linuxthreads_offset_pid
)) != 0)
964 fprintf_unfiltered (gdb_stderr
,
965 "Unable to find linuxthreads symbol \"%s\"\n",
966 "__pthread_offsetof_pid");
970 if (! find_all_signal_vars (objfile
))
973 /* Read adresses of internal structures to access */
974 if ((ms
= lookup_minimal_symbol ("__pthread_handles",
975 NULL
, objfile
)) == NULL
)
977 fprintf_unfiltered (gdb_stderr
,
978 "Unable to find linuxthreads symbol \"%s\"\n",
979 "__pthread_handles");
982 linuxthreads_handles
= SYMBOL_VALUE_ADDRESS (ms
);
984 if ((ms
= lookup_minimal_symbol ("__pthread_handles_num",
985 NULL
, objfile
)) == NULL
)
987 fprintf_unfiltered (gdb_stderr
,
988 "Unable to find linuxthreads symbol \"%s\"\n",
989 "__pthread_handles_num");
992 linuxthreads_num
= SYMBOL_VALUE_ADDRESS (ms
);
994 if ((ms
= lookup_minimal_symbol ("__pthread_manager_thread",
995 NULL
, objfile
)) == NULL
)
997 fprintf_unfiltered (gdb_stderr
,
998 "Unable to find linuxthreads symbol \"%s\"\n",
999 "__pthread_manager_thread");
1002 linuxthreads_manager
= SYMBOL_VALUE_ADDRESS (ms
) + linuxthreads_offset_pid
;
1004 if ((ms
= lookup_minimal_symbol ("__pthread_initial_thread",
1005 NULL
, objfile
)) == NULL
)
1007 fprintf_unfiltered (gdb_stderr
,
1008 "Unable to find linuxthreads symbol \"%s\"\n",
1009 "__pthread_initial_thread");
1012 linuxthreads_initial
= SYMBOL_VALUE_ADDRESS (ms
) + linuxthreads_offset_pid
;
1014 /* Search for this last, so it won't be set to a non-zero value unless
1015 we successfully found all the symbols above. */
1016 if ((ms
= lookup_minimal_symbol ("__pthread_threads_max",
1017 NULL
, objfile
)) == NULL
1018 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms
),
1019 (char *)&linuxthreads_max
,
1020 sizeof (linuxthreads_max
)) != 0)
1022 fprintf_unfiltered (gdb_stderr
,
1023 "Unable to find linuxthreads symbol \"%s\"\n",
1024 "__pthread_threads_max");
1028 /* Allocate gdb internal structures */
1029 linuxthreads_wait_pid
=
1030 (int *) xmalloc (sizeof (int) * (linuxthreads_max
+ 1));
1031 linuxthreads_wait_status
=
1032 (int *) xmalloc (sizeof (int) * (linuxthreads_max
+ 1));
1033 linuxthreads_breakpoint_zombie
= (struct linuxthreads_breakpoint
*)
1034 xmalloc (sizeof (struct linuxthreads_breakpoint
) * (linuxthreads_max
+ 1));
1037 !linuxthreads_attach_pending
&&
1038 !using_thread_db
) /* suppressed by thread_db module */
1042 target_write_memory (linuxthreads_debug
, (char *)&on
, sizeof (on
));
1043 linuxthreads_attach_pending
= 1;
1044 update_stop_threads (inferior_pid
);
1045 linuxthreads_attach_pending
= 0;
1048 check_all_signal_numbers ();
1053 /* If we have switched threads from a one that stopped at breakpoint,
1054 return 1 otherwise 0.
1056 Note that this implementation is potentially redundant now that
1057 default_prepare_to_proceed() has been added. */
1060 linuxthreads_prepare_to_proceed (int step
)
1062 if (!linuxthreads_max
1063 || !linuxthreads_manager_pid
1064 || !linuxthreads_breakpoint_pid
1065 || !breakpoint_here_p (read_pc_pid (linuxthreads_breakpoint_pid
)))
1070 /* Mark the current inferior as single stepping process. */
1071 linuxthreads_step_pid
= inferior_pid
;
1074 linuxthreads_inferior_pid
= linuxthreads_breakpoint_pid
;
1075 return linuxthreads_breakpoint_pid
;
1078 /* Convert a pid to printable form. */
1081 linuxthreads_pid_to_str (int pid
)
1083 static char buf
[100];
1085 sprintf (buf
, "%s %d%s", linuxthreads_max
? "Thread" : "Pid", pid
,
1086 (pid
== linuxthreads_manager_pid
) ? " (manager thread)"
1087 : (pid
== linuxthreads_initial_pid
) ? " (initial thread)"
1093 /* Attach to process PID, then initialize for debugging it
1094 and wait for the trace-trap that results from attaching. */
1097 linuxthreads_attach (char *args
, int from_tty
)
1100 error_no_arg ("process-id to attach");
1102 push_target (&linuxthreads_ops
);
1103 linuxthreads_breakpoints_inserted
= 1;
1104 linuxthreads_breakpoint_last
= -1;
1105 linuxthreads_wait_last
= -1;
1106 WSETSTOP (linuxthreads_exit_status
, 0);
1108 child_ops
.to_attach (args
, from_tty
);
1110 if (linuxthreads_max
)
1111 linuxthreads_attach_pending
= 1;
1114 /* Take a program previously attached to and detaches it.
1115 The program resumes execution and will no longer stop
1116 on signals, etc. We'd better not have left any breakpoints
1117 in the program or it'll die when it hits one. For this
1118 to work, it may be necessary for the process to have been
1119 previously attached. It *might* work if the program was
1120 started via the normal ptrace (PTRACE_TRACEME). */
1123 linuxthreads_detach (char *args
, int from_tty
)
1125 if (linuxthreads_max
)
1130 target_write_memory (linuxthreads_debug
, (char *)&off
, sizeof (off
));
1132 /* Walk through linuxthreads array in order to detach known threads. */
1133 if (linuxthreads_manager_pid
!= 0)
1135 /* Get rid of all positive zombie breakpoints. */
1136 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1138 if (linuxthreads_breakpoint_zombie
[i
].step
)
1141 pid
= linuxthreads_breakpoint_zombie
[i
].pid
;
1142 if (!linuxthreads_thread_alive (pid
))
1145 if (linuxthreads_breakpoint_zombie
[i
].pc
!= read_pc_pid (pid
))
1148 /* Continue in STEP mode until the thread pc has moved or
1149 until SIGTRAP is found on the same PC. */
1150 if (linuxthreads_find_trap (pid
, 0)
1151 && linuxthreads_breakpoint_zombie
[i
].pc
== read_pc_pid (pid
))
1152 write_pc_pid (linuxthreads_breakpoint_zombie
[i
].pc
1153 - DECR_PC_AFTER_BREAK
, pid
);
1156 /* Detach thread after thread. */
1157 inferior_pid
= linuxthreads_manager_pid
;
1158 iterate_active_threads (detach_thread
, 1);
1160 /* Remove pending SIGTRAP and SIGSTOP */
1161 linuxthreads_find_trap (inferior_pid
, 1);
1163 linuxthreads_wait_last
= -1;
1164 WSETSTOP (linuxthreads_exit_status
, 0);
1167 linuxthreads_inferior_pid
= 0;
1168 linuxthreads_breakpoint_pid
= 0;
1169 linuxthreads_step_pid
= 0;
1170 linuxthreads_step_signo
= TARGET_SIGNAL_0
;
1171 linuxthreads_manager_pid
= 0;
1172 linuxthreads_initial_pid
= 0;
1173 linuxthreads_attach_pending
= 0;
1174 init_thread_list (); /* Destroy thread info */
1177 child_ops
.to_detach (args
, from_tty
);
1179 unpush_target (&linuxthreads_ops
);
1182 /* Resume execution of process PID. If STEP is nozero, then
1183 just single step it. If SIGNAL is nonzero, restart it with that
1184 signal activated. */
1187 linuxthreads_resume (int pid
, int step
, enum target_signal signo
)
1189 if (!linuxthreads_max
|| stop_soon_quietly
|| linuxthreads_manager_pid
== 0)
1191 child_ops
.to_resume (pid
, step
, signo
);
1196 if (linuxthreads_inferior_pid
)
1198 /* Prepare resume of the last thread that hit a breakpoint */
1199 linuxthreads_breakpoints_inserted
= 0;
1200 rpid
= linuxthreads_inferior_pid
;
1201 linuxthreads_step_signo
= signo
;
1205 struct cleanup
*old_chain
= NULL
;
1210 linuxthreads_step_pid
= step
? inferior_pid
: 0;
1211 linuxthreads_step_signo
= signo
;
1212 rpid
= inferior_pid
;
1217 if (pid
< 0 || !step
)
1219 linuxthreads_breakpoints_inserted
= 1;
1221 /* Walk through linuxthreads array in order to resume threads */
1222 if (pid
>= 0 && inferior_pid
!= pid
)
1224 old_chain
= save_inferior_pid ();
1228 iterate_active_threads (resume_thread
, 0);
1229 if (linuxthreads_manager_pid
!= inferior_pid
1230 && !linuxthreads_pending_status (linuxthreads_manager_pid
))
1231 resume_thread (linuxthreads_manager_pid
);
1234 linuxthreads_breakpoints_inserted
= 0;
1236 /* Deal with zombie breakpoint */
1237 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1238 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1240 if (linuxthreads_breakpoint_zombie
[i
].pc
!= read_pc_pid (rpid
))
1242 /* The current pc is out of zombie breakpoint. */
1243 REMOVE_BREAKPOINT_ZOMBIE(i
);
1248 if (old_chain
!= NULL
)
1249 do_cleanups (old_chain
);
1252 /* Resume initial thread. */
1253 /* [unles it has a wait event pending] */
1254 if (!linuxthreads_pending_status (rpid
))
1256 child_ops
.to_resume (rpid
, step
, signo
);
1261 /* Abstract out the child_wait functionality. */
1263 linux_child_wait (int pid
, int *rpid
, int *status
)
1267 /* Note: inftarg has these inside the loop. */
1268 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1269 attached process. */
1272 errno
= save_errno
= 0;
1276 *rpid
= waitpid (pid
, status
, __WCLONE
| WNOHANG
);
1281 /* Got an event -- break out */
1284 if (errno
== EINTR
) /* interrupted by signal, try again */
1290 *rpid
= waitpid (pid
, status
, WNOHANG
);
1293 /* Got an event -- break out */
1300 if (errno
!= 0 && save_errno
!= 0)
1304 sigsuspend(&linuxthreads_block_mask
);
1306 clear_sigio_trap ();
1307 clear_sigint_trap ();
1309 return errno
? errno
: save_errno
;
1313 /* Wait for any threads to stop. We may have to convert PID from a thread id
1314 to a LWP id, and vice versa on the way out. */
1317 linuxthreads_wait (int pid
, struct target_waitstatus
*ourstatus
)
1325 if (linuxthreads_max
&& !linuxthreads_breakpoints_inserted
)
1326 wstatus
= alloca (LINUXTHREAD_NSIG
* sizeof (int));
1328 /* See if the inferior has chosen values for its signals yet. By
1329 checking for them here, we can be sure we've updated GDB's signal
1330 handling table before the inferior ever gets one of them. (Well,
1331 before we notice, anyway.) */
1332 check_all_signal_numbers ();
1336 if (!linuxthreads_max
)
1338 else if (!linuxthreads_breakpoints_inserted
)
1340 if (linuxthreads_inferior_pid
)
1341 pid
= linuxthreads_inferior_pid
;
1346 else if (pid
< 0 && linuxthreads_wait_last
>= 0)
1348 status
= linuxthreads_wait_status
[linuxthreads_wait_last
];
1349 rpid
= linuxthreads_wait_pid
[linuxthreads_wait_last
--];
1351 else if (pid
> 0 && linuxthreads_pending_status (pid
))
1353 for (i
= linuxthreads_wait_last
; i
>= 0; i
--)
1354 if (linuxthreads_wait_pid
[i
] == pid
)
1360 status
= linuxthreads_wait_status
[i
];
1362 if (i
< linuxthreads_wait_last
)
1364 linuxthreads_wait_status
[i
] =
1365 linuxthreads_wait_status
[linuxthreads_wait_last
];
1366 linuxthreads_wait_pid
[i
] =
1367 linuxthreads_wait_pid
[linuxthreads_wait_last
];
1369 linuxthreads_wait_last
--;
1379 save_errno
= linux_child_wait (pid
, &rpid
, &status
);
1383 if (WIFEXITED(linuxthreads_exit_status
))
1385 store_waitstatus (ourstatus
, linuxthreads_exit_status
);
1386 return inferior_pid
;
1391 (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
1392 safe_strerror (save_errno
));
1393 /* Claim it exited with unknown signal. */
1394 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1395 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1400 /* We have now gotten a new event from waitpid above. */
1402 /* Signals arrive in any order. So get all signals until
1403 SIGTRAP and resend previous ones to be held after. */
1404 if (linuxthreads_max
1405 && !linuxthreads_breakpoints_inserted
1406 && WIFSTOPPED(status
))
1407 if (WSTOPSIG(status
) == SIGTRAP
)
1411 kill (rpid
, WSTOPSIG(wstatus
[last
]));
1414 /* insert negative zombie breakpoint */
1415 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1416 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1418 if (i
> linuxthreads_breakpoint_last
)
1420 linuxthreads_breakpoint_zombie
[i
].pid
= rpid
;
1421 linuxthreads_breakpoint_last
++;
1423 linuxthreads_breakpoint_zombie
[i
].pc
= read_pc_pid (rpid
);
1424 linuxthreads_breakpoint_zombie
[i
].step
= 1;
1428 if (WSTOPSIG(status
) != SIGSTOP
)
1430 for (i
= 0; i
< last
; i
++)
1431 if (wstatus
[i
] == status
)
1435 wstatus
[last
++] = status
;
1438 child_resume (rpid
, 1, TARGET_SIGNAL_0
);
1441 if (linuxthreads_inferior_pid
)
1442 linuxthreads_inferior_pid
= 0;
1445 if (linuxthreads_max
&& !stop_soon_quietly
)
1447 if (linuxthreads_max
1448 && WIFSTOPPED(status
)
1449 && WSTOPSIG(status
) == SIGSTOP
)
1451 /* Skip SIGSTOP signals. */
1452 if (!linuxthreads_pending_status (rpid
))
1454 if (linuxthreads_step_pid
== rpid
)
1456 child_resume (rpid
, 1, linuxthreads_step_signo
);
1460 child_resume (rpid
, 0, TARGET_SIGNAL_0
);
1466 /* Do no report exit status of cloned threads. */
1467 if (WIFEXITED(status
))
1469 if (rpid
== linuxthreads_initial_pid
)
1470 linuxthreads_exit_status
= status
;
1472 /* Remove any zombie breakpoint. */
1473 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1474 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1476 REMOVE_BREAKPOINT_ZOMBIE(i
);
1484 /* Deal with zombie breakpoint */
1485 for (i
= 0; i
<= linuxthreads_breakpoint_last
; i
++)
1486 if (linuxthreads_breakpoint_zombie
[i
].pid
== rpid
)
1489 if (i
<= linuxthreads_breakpoint_last
)
1491 /* There is a potential zombie breakpoint */
1492 if (WIFEXITED(status
)
1493 || linuxthreads_breakpoint_zombie
[i
].pc
!= read_pc_pid (rpid
))
1495 /* The current pc is out of zombie breakpoint. */
1496 REMOVE_BREAKPOINT_ZOMBIE(i
);
1498 else if (!linuxthreads_breakpoint_zombie
[i
].step
1499 && WIFSTOPPED(status
) && WSTOPSIG(status
) == SIGTRAP
)
1501 /* This is a real one ==> decrement PC and restart. */
1502 write_pc_pid (linuxthreads_breakpoint_zombie
[i
].pc
1503 - DECR_PC_AFTER_BREAK
, rpid
);
1504 if (linuxthreads_step_pid
== rpid
)
1506 child_resume (rpid
, 1, linuxthreads_step_signo
);
1510 child_resume (rpid
, 0, TARGET_SIGNAL_0
);
1516 /* Walk through linuxthreads array in order to stop them */
1517 if (linuxthreads_breakpoints_inserted
)
1518 update_stop_threads (rpid
);
1521 else if (rpid
!= inferior_pid
)
1524 store_waitstatus (ourstatus
, status
);
1526 if (linuxthreads_attach_pending
&& !stop_soon_quietly
)
1529 if (!using_thread_db
)
1531 target_write_memory (linuxthreads_debug
,
1532 (char *) &on
, sizeof (on
));
1533 update_stop_threads (rpid
);
1535 linuxthreads_attach_pending
= 0;
1538 if (linuxthreads_breakpoints_inserted
1539 && WIFSTOPPED(status
)
1540 && WSTOPSIG(status
) == SIGTRAP
)
1541 linuxthreads_breakpoint_pid
= rpid
;
1542 else if (linuxthreads_breakpoint_pid
)
1543 linuxthreads_breakpoint_pid
= 0;
1549 /* Fork an inferior process, and start debugging it with ptrace. */
1552 linuxthreads_create_inferior (char *exec_file
, char *allargs
, char **env
)
1554 if (!exec_file
&& !exec_bfd
)
1556 error ("No executable file specified.\n\
1557 Use the \"file\" or \"exec-file\" command.");
1561 push_target (&linuxthreads_ops
);
1562 linuxthreads_breakpoints_inserted
= 1;
1563 linuxthreads_breakpoint_last
= -1;
1564 linuxthreads_wait_last
= -1;
1565 WSETSTOP (linuxthreads_exit_status
, 0);
1567 if (linuxthreads_max
)
1568 linuxthreads_attach_pending
= 1;
1570 child_ops
.to_create_inferior (exec_file
, allargs
, env
);
1574 linuxthreads_discard_global_state (void)
1576 linuxthreads_inferior_pid
= 0;
1577 linuxthreads_breakpoint_pid
= 0;
1578 linuxthreads_step_pid
= 0;
1579 linuxthreads_step_signo
= TARGET_SIGNAL_0
;
1580 linuxthreads_manager_pid
= 0;
1581 linuxthreads_initial_pid
= 0;
1582 linuxthreads_attach_pending
= 0;
1583 linuxthreads_max
= 0;
1586 /* Clean up after the inferior dies. */
1589 linuxthreads_mourn_inferior (void)
1591 if (linuxthreads_max
)
1594 target_write_memory (linuxthreads_debug
, (char *)&off
, sizeof (off
));
1596 linuxthreads_discard_global_state ();
1597 init_thread_list(); /* Destroy thread info */
1600 child_ops
.to_mourn_inferior ();
1602 unpush_target (&linuxthreads_ops
);
1605 /* Kill the inferior process */
1608 linuxthreads_kill (void)
1613 if (inferior_pid
== 0)
1616 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1618 /* Remove all threads status. */
1619 inferior_pid
= linuxthreads_manager_pid
;
1620 iterate_active_threads (kill_thread
, 1);
1623 kill_thread (inferior_pid
);
1626 /* doing_quit_force solves a real problem, but I think a properly
1627 placed call to catch_errors would do the trick much more cleanly. */
1628 if (doing_quit_force
>= 0)
1630 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1632 /* Wait for thread to complete */
1633 while ((rpid
= waitpid (-1, &status
, __WCLONE
)) > 0)
1634 if (!WIFEXITED(status
))
1637 while ((rpid
= waitpid (-1, &status
, 0)) > 0)
1638 if (!WIFEXITED(status
))
1642 while ((rpid
= waitpid (inferior_pid
, &status
, 0)) > 0)
1643 if (!WIFEXITED(status
))
1644 ptrace (PT_KILL
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0);
1648 /* Wait for all threads. */
1651 rpid
= waitpid (-1, &status
, __WCLONE
| WNOHANG
);
1653 while (rpid
> 0 || errno
== EINTR
);
1654 /* FIXME: should no longer need to handle EINTR here. */
1658 rpid
= waitpid (-1, &status
, WNOHANG
);
1660 while (rpid
> 0 || errno
== EINTR
);
1661 /* FIXME: should no longer need to handle EINTR here. */
1663 linuxthreads_mourn_inferior ();
1666 /* Insert a breakpoint */
1669 linuxthreads_insert_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1671 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1673 linuxthreads_breakpoint_addr
= addr
;
1674 iterate_active_threads (insert_breakpoint
, 1);
1675 insert_breakpoint (linuxthreads_manager_pid
);
1678 return child_ops
.to_insert_breakpoint (addr
, contents_cache
);
1681 /* Remove a breakpoint */
1684 linuxthreads_remove_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1686 if (linuxthreads_max
&& linuxthreads_manager_pid
!= 0)
1688 linuxthreads_breakpoint_addr
= addr
;
1689 iterate_active_threads (remove_breakpoint
, 1);
1690 remove_breakpoint (linuxthreads_manager_pid
);
1693 return child_ops
.to_remove_breakpoint (addr
, contents_cache
);
1696 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1699 linuxthreads_can_run (void)
1701 return child_suppress_run
;
1706 init_linuxthreads_ops (void)
1708 linuxthreads_ops
.to_shortname
= "linuxthreads";
1709 linuxthreads_ops
.to_longname
= "LINUX threads and pthread.";
1710 linuxthreads_ops
.to_doc
= "LINUX threads and pthread support.";
1711 linuxthreads_ops
.to_attach
= linuxthreads_attach
;
1712 linuxthreads_ops
.to_detach
= linuxthreads_detach
;
1713 linuxthreads_ops
.to_resume
= linuxthreads_resume
;
1714 linuxthreads_ops
.to_wait
= linuxthreads_wait
;
1715 linuxthreads_ops
.to_kill
= linuxthreads_kill
;
1716 linuxthreads_ops
.to_can_run
= linuxthreads_can_run
;
1717 linuxthreads_ops
.to_stratum
= thread_stratum
;
1718 linuxthreads_ops
.to_insert_breakpoint
= linuxthreads_insert_breakpoint
;
1719 linuxthreads_ops
.to_remove_breakpoint
= linuxthreads_remove_breakpoint
;
1720 linuxthreads_ops
.to_create_inferior
= linuxthreads_create_inferior
;
1721 linuxthreads_ops
.to_mourn_inferior
= linuxthreads_mourn_inferior
;
1722 linuxthreads_ops
.to_thread_alive
= linuxthreads_thread_alive
;
1723 linuxthreads_ops
.to_pid_to_str
= linuxthreads_pid_to_str
;
1724 linuxthreads_ops
.to_magic
= OPS_MAGIC
;
1728 _initialize_linuxthreads (void)
1730 struct sigaction sact
;
1731 sigset_t linuxthreads_wait_mask
; /* sigset with SIGCHLD */
1733 init_linuxthreads_ops ();
1734 add_target (&linuxthreads_ops
);
1735 child_suppress_run
= 1;
1737 /* Hook onto the "new_objfile" event.
1738 * If someone else is already hooked onto the event,
1739 * then make sure he will be called after we are.
1741 target_new_objfile_chain
= target_new_objfile_hook
;
1742 target_new_objfile_hook
= linuxthreads_new_objfile
;
1744 /* Attach SIGCHLD handler */
1745 sact
.sa_handler
= sigchld_handler
;
1746 sigemptyset (&sact
.sa_mask
);
1748 sigaction (SIGCHLD
, &sact
, NULL
);
1750 /* initialize SIGCHLD mask */
1751 sigemptyset (&linuxthreads_wait_mask
);
1752 sigaddset (&linuxthreads_wait_mask
, SIGCHLD
);
1754 /* Use SIG_BLOCK to block receipt of SIGCHLD.
1755 The block_mask will allow us to wait for this signal explicitly. */
1756 sigprocmask(SIG_BLOCK
,
1757 &linuxthreads_wait_mask
,
1758 &linuxthreads_block_mask
);
1759 /* Make sure that linuxthreads_block_mask is not blocking SIGCHLD */
1760 sigdelset (&linuxthreads_block_mask
, SIGCHLD
);