* testsuite/regress-demangle (failed test): Show result and
[binutils-gdb.git] / gdb / linux-thread.c
blobf25c763f13bcb2ec0e0a960b064a20aca34fc2c8
1 /* Low level interface for debugging GNU/Linux threads for GDB,
2 the GNU debugger.
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
48 them correct. */
50 #include "defs.h"
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>
57 #include "target.h"
58 #include "inferior.h"
59 #include "gdbcore.h"
60 #include "gdbthread.h"
61 #include "gdbcmd.h"
62 #include "breakpoint.h"
64 #ifndef PT_ATTACH
65 #define PT_ATTACH PTRACE_ATTACH
66 #endif
67 #ifndef PT_KILL
68 #define PT_KILL PTRACE_KILL
69 #endif
70 #ifndef PT_READ_U
71 #define PT_READ_U PTRACE_PEEKUSR
72 #endif
74 #ifdef NSIG
75 #define LINUXTHREAD_NSIG NSIG
76 #else
77 #ifdef _NSIG
78 #define LINUXTHREAD_NSIG _NSIG
79 #endif
80 #endif
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
100 handle */
101 static int linuxthreads_offset_pid; /* p_pid offset of the linuxthreads
102 descr */
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. */
140 char *var;
142 /* True if this variable must exist for us to debug properly. */
143 int required;
145 /* The variable's address in the inferior, or zero if the
146 LinuxThreads library hasn't been loaded into this inferior yet. */
147 CORE_ADDR addr;
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. */
153 int signal;
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. */
159 int stop, print;
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
205 #endif
206 /* Check to see if the given thread is alive. */
207 static int
208 linuxthreads_thread_alive (pid)
209 int pid;
211 errno = 0;
212 return ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE)0, 0) >= 0 || errno == 0;
215 /* On detach(), find a SIGTRAP status. If stop is non-zero, find a
216 SIGSTOP one, too.
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
227 it ourselves.
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. */
232 static int
233 linuxthreads_find_trap (pid, stop)
234 int pid;
235 int stop;
237 int i;
238 int rpid;
239 int status;
240 int found_stop = 0;
241 int found_trap = 0;
243 /* PID may have any number of signals pending. The kernel will
244 report each of them to us via wait, and then it's up to us to
245 pass them along to the process via ptrace, if we so choose.
247 We need to paw through the whole set until we've found a SIGTRAP
248 (or a SIGSTOP, if `stop' is set). We don't pass the SIGTRAP (or
249 SIGSTOP) through, but we do re-send all the others, so PID will
250 receive them when we resume it. */
251 int *wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
252 int last = 0;
254 /* Look at the pending status */
255 for (i = linuxthreads_wait_last; i >= 0; i--)
256 if (linuxthreads_wait_pid[i] == pid)
258 status = linuxthreads_wait_status[i];
260 /* Delete the i'th member of the table. Since the table is
261 unordered, we can do this simply by copying the table's
262 last element to the i'th position, and shrinking the table
263 by one element. */
264 if (i < linuxthreads_wait_last)
266 linuxthreads_wait_status[i] =
267 linuxthreads_wait_status[linuxthreads_wait_last];
268 linuxthreads_wait_pid[i] =
269 linuxthreads_wait_pid[linuxthreads_wait_last];
271 linuxthreads_wait_last--;
273 if (!WIFSTOPPED(status)) /* Thread has died */
274 return 0;
276 if (WSTOPSIG(status) == SIGTRAP)
278 if (stop)
279 found_trap = 1;
280 else
281 return 1;
283 else if (WSTOPSIG(status) == SIGSTOP)
285 if (stop)
286 found_stop = 1;
288 else
290 wstatus[0] = status;
291 last = 1;
294 break;
297 if (stop)
299 /* Make sure that we'll find what we're looking for. */
300 if (!found_trap)
302 kill (pid, SIGTRAP);
304 if (!found_stop)
306 kill (pid, SIGSTOP);
310 /* Catch all status until SIGTRAP and optionally SIGSTOP show up. */
311 for (;;)
313 /* resume the child every time... */
314 child_resume (pid, 1, TARGET_SIGNAL_0);
316 /* loop as long as errno == EINTR:
317 waitpid syscall may be aborted due to GDB receiving a signal.
318 FIXME: EINTR handling should no longer be necessary here, since
319 we now block SIGCHLD except in an explicit sigsuspend call. */
321 for (;;)
323 rpid = waitpid (pid, &status, __WCLONE);
324 if (rpid > 0)
326 break;
328 if (errno == EINTR)
330 continue;
333 /* There are a few reasons the wait call above may have
334 failed. If the thread manager dies, its children get
335 reparented, and this interferes with GDB waiting for
336 them, in some cases. Another possibility is that the
337 initial thread was not cloned, so calling wait with
338 __WCLONE won't find it. I think neither of these should
339 occur in modern Linux kernels --- they don't seem to in
340 2.0.36. */
341 rpid = waitpid (pid, &status, 0);
342 if (rpid > 0)
344 break;
346 if (errno != EINTR)
347 perror_with_name ("find_trap/waitpid");
350 if (!WIFSTOPPED(status)) /* Thread has died */
351 return 0;
353 if (WSTOPSIG(status) == SIGTRAP)
354 if (!stop || found_stop)
355 break;
356 else
357 found_trap = 1;
358 else if (WSTOPSIG(status) != SIGSTOP)
359 wstatus[last++] = status;
360 else if (stop)
362 if (found_trap)
363 break;
364 else
365 found_stop = 1;
369 /* Resend any other signals we noticed to the thread, to be received
370 when we continue it. */
371 while (--last >= 0)
373 kill (pid, WSTOPSIG(wstatus[last]));
376 return 1;
379 /* Cleanup stub for save_inferior_pid. */
380 static void
381 restore_inferior_pid (void *arg)
383 int *saved_pid_ptr = arg;
384 inferior_pid = *saved_pid_ptr;
385 free (arg);
388 /* Register a cleanup to restore the value of inferior_pid. */
389 static struct cleanup *
390 save_inferior_pid (void)
392 int *saved_pid_ptr;
394 saved_pid_ptr = xmalloc (sizeof (int));
395 *saved_pid_ptr = inferior_pid;
396 return make_cleanup (restore_inferior_pid, saved_pid_ptr);
399 static void
400 sigchld_handler (signo)
401 int signo;
403 /* This handler is used to get an EINTR while doing waitpid()
404 when an event is received */
407 /* Have we already collected a wait status for PID in the
408 linuxthreads_wait bag? */
409 static int
410 linuxthreads_pending_status (pid)
411 int pid;
413 int i;
414 for (i = linuxthreads_wait_last; i >= 0; i--)
415 if (linuxthreads_wait_pid[i] == pid)
416 return 1;
417 return 0;
421 /* Internal linuxthreads signal management */
423 /* Check in OBJFILE for the variable that holds the number for signal SIG.
424 We assume that we've already found other LinuxThreads-ish variables
425 in OBJFILE, so we complain if it's required, but not there.
426 Return true iff things are okay. */
427 static int
428 find_signal_var (sig, objfile)
429 struct linuxthreads_signal *sig;
430 struct objfile *objfile;
432 struct minimal_symbol *ms = lookup_minimal_symbol (sig->var, NULL, objfile);
434 if (! ms)
436 if (sig->required)
438 fprintf_unfiltered (gdb_stderr,
439 "Unable to find linuxthreads symbol \"%s\"\n",
440 sig->var);
441 return 0;
443 else
445 sig->addr = 0;
446 return 1;
450 sig->addr = SYMBOL_VALUE_ADDRESS (ms);
452 return 1;
455 static int
456 find_all_signal_vars (objfile)
457 struct objfile *objfile;
459 return ( find_signal_var (&linuxthreads_sig_restart, objfile)
460 && find_signal_var (&linuxthreads_sig_cancel, objfile)
461 && find_signal_var (&linuxthreads_sig_debug, objfile));
464 /* A struct complaint isn't appropriate here. */
465 static int complained_cannot_determine_thread_signal_number = 0;
467 /* Check to see if the variable holding the signal number for SIG has
468 been initialized yet. If it has, tell GDB to pass that signal
469 through to the inferior silently. */
470 static void
471 check_signal_number (sig)
472 struct linuxthreads_signal *sig;
474 int num;
476 if (sig->signal)
477 /* We already know this signal number. */
478 return;
480 if (! sig->addr)
481 /* We don't know the variable's address yet. */
482 return;
484 if (target_read_memory (sig->addr, (char *)&num, sizeof (num))
485 != 0)
487 /* If this happens once, it'll probably happen for all the
488 signals, so only complain once. */
489 if (! complained_cannot_determine_thread_signal_number)
490 warning ("Cannot determine thread signal number; "
491 "GDB may report spurious signals.");
492 complained_cannot_determine_thread_signal_number = 1;
493 return;
496 if (num == 0)
497 /* It hasn't been initialized yet. */
498 return;
500 /* We know sig->signal was zero, and is becoming non-zero, so it's
501 okay to sample GDB's original settings. */
502 sig->signal = num;
503 sig->stop = signal_stop_update (target_signal_from_host (num), 0);
504 sig->print = signal_print_update (target_signal_from_host (num), 0);
507 void
508 check_all_signal_numbers ()
510 /* If this isn't a LinuxThreads program, quit early. */
511 if (! linuxthreads_max)
512 return;
514 check_signal_number (&linuxthreads_sig_restart);
515 check_signal_number (&linuxthreads_sig_cancel);
516 check_signal_number (&linuxthreads_sig_debug);
518 /* handle linuxthread exit */
519 if (linuxthreads_sig_debug.signal
520 || linuxthreads_sig_restart.signal)
522 struct sigaction sact;
524 sact.sa_handler = sigchld_handler;
525 sigemptyset(&sact.sa_mask);
526 sact.sa_flags = 0;
528 if (linuxthreads_sig_debug.signal > 0)
529 sigaction(linuxthreads_sig_cancel.signal, &sact, NULL);
530 else
531 sigaction(linuxthreads_sig_restart.signal, &sact, NULL);
536 /* Restore GDB's original settings for SIG.
537 This should only be called when we're no longer sure if we're
538 talking to an executable that uses LinuxThreads, so we clear the
539 signal number and variable address too. */
540 static void
541 restore_signal (sig)
542 struct linuxthreads_signal *sig;
544 if (! sig->signal)
545 return;
547 /* We know sig->signal was non-zero, and is becoming zero, so it's
548 okay to restore GDB's original settings. */
549 signal_stop_update (target_signal_from_host (sig->signal), sig->stop);
550 signal_print_update (target_signal_from_host (sig->signal), sig->print);
552 sig->signal = 0;
553 sig->addr = 0;
557 /* Restore GDB's original settings for all LinuxThreads signals.
558 This should only be called when we're no longer sure if we're
559 talking to an executable that uses LinuxThreads, so we clear the
560 signal number and variable address too. */
561 static void
562 restore_all_signals ()
564 restore_signal (&linuxthreads_sig_restart);
565 restore_signal (&linuxthreads_sig_cancel);
566 restore_signal (&linuxthreads_sig_debug);
568 /* If it happens again, we should complain again. */
569 complained_cannot_determine_thread_signal_number = 0;
575 /* Apply FUNC to the pid of each active thread. This consults the
576 inferior's handle table to find active threads.
578 If ALL is non-zero, process all threads.
579 If ALL is zero, skip threads with pending status. */
580 static void
581 iterate_active_threads (func, all)
582 void (*func)(int);
583 int all;
585 CORE_ADDR descr;
586 int pid;
587 int i;
588 int num;
590 read_memory (linuxthreads_num, (char *)&num, sizeof (int));
592 for (i = 0; i < linuxthreads_max && num > 0; i++)
594 read_memory (linuxthreads_handles +
595 linuxthreads_sizeof_handle * i + linuxthreads_offset_descr,
596 (char *)&descr, sizeof (void *));
597 if (descr)
599 num--;
600 read_memory (descr + linuxthreads_offset_pid,
601 (char *)&pid, sizeof (pid_t));
602 if (pid > 0 && pid != linuxthreads_manager_pid
603 && (all || (!linuxthreads_pending_status (pid))))
604 (*func)(pid);
609 /* Insert a thread breakpoint at linuxthreads_breakpoint_addr.
610 This is the worker function for linuxthreads_insert_breakpoint,
611 which passes it to iterate_active_threads. */
612 static void
613 insert_breakpoint (pid)
614 int pid;
616 int j;
618 /* Remove (if any) the positive zombie breakpoint. */
619 for (j = linuxthreads_breakpoint_last; j >= 0; j--)
620 if (linuxthreads_breakpoint_zombie[j].pid == pid)
622 if ((linuxthreads_breakpoint_zombie[j].pc - DECR_PC_AFTER_BREAK
623 == linuxthreads_breakpoint_addr)
624 && !linuxthreads_breakpoint_zombie[j].step)
625 REMOVE_BREAKPOINT_ZOMBIE(j);
626 break;
630 /* Note that we're about to remove a thread breakpoint at
631 linuxthreads_breakpoint_addr.
633 This is the worker function for linuxthreads_remove_breakpoint,
634 which passes it to iterate_active_threads. The actual work of
635 overwriting the breakpoint instruction is done by
636 child_ops.to_remove_breakpoint; here, we simply create a zombie
637 breakpoint if the thread's PC is pointing at the breakpoint being
638 removed. */
639 static void
640 remove_breakpoint (pid)
641 int pid;
643 int j;
645 /* Insert a positive zombie breakpoint (if needed). */
646 for (j = 0; j <= linuxthreads_breakpoint_last; j++)
647 if (linuxthreads_breakpoint_zombie[j].pid == pid)
648 break;
650 if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
652 CORE_ADDR pc = read_pc_pid (pid);
653 if (linuxthreads_breakpoint_addr == pc - DECR_PC_AFTER_BREAK
654 && j > linuxthreads_breakpoint_last)
656 linuxthreads_breakpoint_zombie[j].pid = pid;
657 linuxthreads_breakpoint_zombie[j].pc = pc;
658 linuxthreads_breakpoint_zombie[j].step = 0;
659 linuxthreads_breakpoint_last++;
664 /* Kill a thread */
665 static void
666 kill_thread (pid)
667 int pid;
669 if (in_thread_list (pid))
671 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
673 else
675 kill (pid, SIGKILL);
679 /* Resume a thread */
680 static void
681 resume_thread (pid)
682 int pid;
684 if (pid != inferior_pid
685 && in_thread_list (pid)
686 && linuxthreads_thread_alive (pid))
688 if (pid == linuxthreads_step_pid)
690 child_resume (pid, 1, linuxthreads_step_signo);
692 else
694 child_resume (pid, 0, TARGET_SIGNAL_0);
699 /* Detach a thread */
700 static void
701 detach_thread (pid)
702 int pid;
704 if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
706 /* Remove pending SIGTRAP and SIGSTOP */
707 linuxthreads_find_trap (pid, 1);
709 inferior_pid = pid;
710 detach (TARGET_SIGNAL_0);
711 inferior_pid = linuxthreads_manager_pid;
715 /* Attach a thread */
716 void
717 attach_thread (pid)
718 int pid;
720 if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) != 0)
721 perror_with_name ("attach_thread");
724 /* Stop a thread */
725 static void
726 stop_thread (pid)
727 int pid;
729 if (pid != inferior_pid)
731 if (in_thread_list (pid))
733 kill (pid, SIGSTOP);
735 else if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) == 0)
737 if (!linuxthreads_attach_pending)
738 printf_filtered ("[New %s]\n", target_pid_to_str (pid));
739 add_thread (pid);
740 if (linuxthreads_sig_debug.signal)
742 /* After a new thread in glibc 2.1 signals gdb its existence,
743 it suspends itself and wait for linuxthreads_sig_restart,
744 now we can wake it up. */
745 kill (pid, linuxthreads_sig_restart.signal);
748 else
749 perror_with_name ("ptrace in stop_thread");
753 /* Wait for a thread */
754 static void
755 wait_thread (pid)
756 int pid;
758 int status;
759 int rpid;
761 if (pid != inferior_pid && in_thread_list (pid))
763 /* loop as long as errno == EINTR:
764 waitpid syscall may be aborted if GDB receives a signal.
765 FIXME: EINTR handling should no longer be necessary here, since
766 we now block SIGCHLD except during an explicit sigsuspend call. */
767 for (;;)
769 /* Get first pid status. */
770 rpid = waitpid(pid, &status, __WCLONE);
771 if (rpid > 0)
773 break;
775 if (errno == EINTR)
777 continue;
780 /* There are two reasons this might have failed:
782 1) PID is the initial thread, which wasn't cloned, so
783 passing the __WCLONE flag to waitpid prevented us from
784 finding it.
786 2) The manager thread is the parent of all but the
787 initial thread; if it dies, the children will all be
788 reparented to init, which will wait for them. This means
789 our call to waitpid won't find them.
791 Actually, based on a casual look at the 2.0.36 kernel
792 code, I don't think either of these cases happen. But I
793 don't have things set up for remotely debugging the
794 kernel, so I'm not sure. And perhaps older kernels
795 didn't work. */
796 rpid = waitpid(pid, &status, 0);
797 if (rpid > 0)
799 break;
801 if (errno != EINTR && linuxthreads_thread_alive (pid))
802 perror_with_name ("wait_thread/waitpid");
804 /* the thread is dead. */
805 return;
807 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
809 linuxthreads_wait_pid[++linuxthreads_wait_last] = pid;
810 linuxthreads_wait_status[linuxthreads_wait_last] = status;
815 /* Walk through the linuxthreads handles in order to detect all
816 threads and stop them */
817 static void
818 update_stop_threads (test_pid)
819 int test_pid;
821 struct cleanup *old_chain = NULL;
823 check_all_signal_numbers ();
825 if (linuxthreads_manager_pid == 0)
827 if (linuxthreads_manager)
829 if (test_pid > 0 && test_pid != inferior_pid)
831 old_chain = save_inferior_pid ();
832 inferior_pid = test_pid;
834 read_memory (linuxthreads_manager,
835 (char *)&linuxthreads_manager_pid, sizeof (pid_t));
837 if (linuxthreads_initial)
839 if (test_pid > 0 && test_pid != inferior_pid)
841 old_chain = save_inferior_pid ();
842 inferior_pid = test_pid;
844 read_memory(linuxthreads_initial,
845 (char *)&linuxthreads_initial_pid, sizeof (pid_t));
849 if (linuxthreads_manager_pid != 0)
851 if (old_chain == NULL && test_pid > 0 &&
852 test_pid != inferior_pid && linuxthreads_thread_alive (test_pid))
854 old_chain = save_inferior_pid ();
855 inferior_pid = test_pid;
858 if (linuxthreads_thread_alive (inferior_pid))
860 if (test_pid > 0)
862 if (test_pid != linuxthreads_manager_pid
863 && !linuxthreads_pending_status (linuxthreads_manager_pid))
865 stop_thread (linuxthreads_manager_pid);
866 wait_thread (linuxthreads_manager_pid);
868 if (!in_thread_list (test_pid))
870 if (!linuxthreads_attach_pending)
871 printf_filtered ("[New %s]\n",
872 target_pid_to_str (test_pid));
873 add_thread (test_pid);
874 if (linuxthreads_sig_debug.signal
875 && inferior_pid == test_pid)
877 /* After a new thread in glibc 2.1 signals gdb its
878 existence, it suspends itself and wait for
879 linuxthreads_sig_restart, now we can wake it up. */
880 kill (test_pid, linuxthreads_sig_restart.signal);
884 iterate_active_threads (stop_thread, 0);
885 iterate_active_threads (wait_thread, 0);
889 if (old_chain != NULL)
890 do_cleanups (old_chain);
893 /* This routine is called whenever a new symbol table is read in, or
894 when all symbol tables are removed. linux-thread event handling
895 can only be initialized when we find the right variables in
896 libpthread.so. Since it's a shared library, those variables don't
897 show up until the library gets mapped and the symbol table is read
898 in. */
900 /* This new_objfile event is now managed by a chained function pointer.
901 * It is the callee's responsability to call the next client on the chain.
904 /* Saved pointer to previous owner of the new_objfile event. */
905 static void (*target_new_objfile_chain) (struct objfile *);
907 void
908 linuxthreads_new_objfile (objfile)
909 struct objfile *objfile;
911 struct minimal_symbol *ms;
913 /* Call predecessor on chain, if any.
914 Calling the new module first allows it to dominate,
915 if it finds its compatible libraries. */
917 if (target_new_objfile_chain)
918 target_new_objfile_chain (objfile);
920 if (!objfile)
922 /* We're starting an entirely new executable, so we can no
923 longer be sure that it uses LinuxThreads. Restore the signal
924 flags to their original states. */
925 restore_all_signals ();
927 /* Indicate that we don't know anything's address any more. */
928 linuxthreads_max = 0;
930 goto quit;
933 /* If we've already found our variables in another objfile, don't
934 bother looking for them again. */
935 if (linuxthreads_max)
936 goto quit;
938 if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL, objfile))
939 /* This object file isn't the pthreads library. */
940 goto quit;
942 if ((ms = lookup_minimal_symbol ("__pthread_threads_debug",
943 NULL, objfile)) == NULL)
945 /* The debugging-aware libpthreads is not present in this objfile */
946 warning ("\
947 This program seems to use POSIX threads, but the thread library used\n\
948 does not support debugging. This may make using GDB difficult. Don't\n\
949 set breakpoints or single-step through code that might be executed by\n\
950 any thread other than the main thread.");
951 goto quit;
953 linuxthreads_debug = SYMBOL_VALUE_ADDRESS (ms);
955 /* Read internal structures configuration */
956 if ((ms = lookup_minimal_symbol ("__pthread_sizeof_handle",
957 NULL, objfile)) == NULL
958 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
959 (char *)&linuxthreads_sizeof_handle,
960 sizeof (linuxthreads_sizeof_handle)) != 0)
962 fprintf_unfiltered (gdb_stderr,
963 "Unable to find linuxthreads symbol \"%s\"\n",
964 "__pthread_sizeof_handle");
965 goto quit;
968 if ((ms = lookup_minimal_symbol ("__pthread_offsetof_descr",
969 NULL, objfile)) == NULL
970 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
971 (char *)&linuxthreads_offset_descr,
972 sizeof (linuxthreads_offset_descr)) != 0)
974 fprintf_unfiltered (gdb_stderr,
975 "Unable to find linuxthreads symbol \"%s\"\n",
976 "__pthread_offsetof_descr");
977 goto quit;
980 if ((ms = lookup_minimal_symbol ("__pthread_offsetof_pid",
981 NULL, objfile)) == NULL
982 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
983 (char *)&linuxthreads_offset_pid,
984 sizeof (linuxthreads_offset_pid)) != 0)
986 fprintf_unfiltered (gdb_stderr,
987 "Unable to find linuxthreads symbol \"%s\"\n",
988 "__pthread_offsetof_pid");
989 goto quit;
992 if (! find_all_signal_vars (objfile))
993 goto quit;
995 /* Read adresses of internal structures to access */
996 if ((ms = lookup_minimal_symbol ("__pthread_handles",
997 NULL, objfile)) == NULL)
999 fprintf_unfiltered (gdb_stderr,
1000 "Unable to find linuxthreads symbol \"%s\"\n",
1001 "__pthread_handles");
1002 goto quit;
1004 linuxthreads_handles = SYMBOL_VALUE_ADDRESS (ms);
1006 if ((ms = lookup_minimal_symbol ("__pthread_handles_num",
1007 NULL, objfile)) == NULL)
1009 fprintf_unfiltered (gdb_stderr,
1010 "Unable to find linuxthreads symbol \"%s\"\n",
1011 "__pthread_handles_num");
1012 goto quit;
1014 linuxthreads_num = SYMBOL_VALUE_ADDRESS (ms);
1016 if ((ms = lookup_minimal_symbol ("__pthread_manager_thread",
1017 NULL, objfile)) == NULL)
1019 fprintf_unfiltered (gdb_stderr,
1020 "Unable to find linuxthreads symbol \"%s\"\n",
1021 "__pthread_manager_thread");
1022 goto quit;
1024 linuxthreads_manager = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
1026 if ((ms = lookup_minimal_symbol ("__pthread_initial_thread",
1027 NULL, objfile)) == NULL)
1029 fprintf_unfiltered (gdb_stderr,
1030 "Unable to find linuxthreads symbol \"%s\"\n",
1031 "__pthread_initial_thread");
1032 goto quit;
1034 linuxthreads_initial = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
1036 /* Search for this last, so it won't be set to a non-zero value unless
1037 we successfully found all the symbols above. */
1038 if ((ms = lookup_minimal_symbol ("__pthread_threads_max",
1039 NULL, objfile)) == NULL
1040 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
1041 (char *)&linuxthreads_max,
1042 sizeof (linuxthreads_max)) != 0)
1044 fprintf_unfiltered (gdb_stderr,
1045 "Unable to find linuxthreads symbol \"%s\"\n",
1046 "__pthread_threads_max");
1047 goto quit;
1050 /* Allocate gdb internal structures */
1051 linuxthreads_wait_pid =
1052 (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
1053 linuxthreads_wait_status =
1054 (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
1055 linuxthreads_breakpoint_zombie = (struct linuxthreads_breakpoint *)
1056 xmalloc (sizeof (struct linuxthreads_breakpoint) * (linuxthreads_max + 1));
1058 if (inferior_pid &&
1059 !linuxthreads_attach_pending &&
1060 !using_thread_db) /* suppressed by thread_db module */
1062 int on = 1;
1064 target_write_memory (linuxthreads_debug, (char *)&on, sizeof (on));
1065 linuxthreads_attach_pending = 1;
1066 update_stop_threads (inferior_pid);
1067 linuxthreads_attach_pending = 0;
1070 check_all_signal_numbers ();
1072 quit:
1075 /* If we have switched threads from a one that stopped at breakpoint,
1076 return 1 otherwise 0. */
1079 linuxthreads_prepare_to_proceed (step)
1080 int step;
1082 if (!linuxthreads_max
1083 || !linuxthreads_manager_pid
1084 || !linuxthreads_breakpoint_pid
1085 || !breakpoint_here_p (read_pc_pid (linuxthreads_breakpoint_pid)))
1086 return 0;
1088 if (step)
1090 /* Mark the current inferior as single stepping process. */
1091 linuxthreads_step_pid = inferior_pid;
1094 linuxthreads_inferior_pid = linuxthreads_breakpoint_pid;
1095 return linuxthreads_breakpoint_pid;
1098 /* Convert a pid to printable form. */
1100 char *
1101 linuxthreads_pid_to_str (pid)
1102 int pid;
1104 static char buf[100];
1106 sprintf (buf, "%s %d%s", linuxthreads_max ? "Thread" : "Pid", pid,
1107 (pid == linuxthreads_manager_pid) ? " (manager thread)"
1108 : (pid == linuxthreads_initial_pid) ? " (initial thread)"
1109 : "");
1111 return buf;
1114 /* Attach to process PID, then initialize for debugging it
1115 and wait for the trace-trap that results from attaching. */
1117 static void
1118 linuxthreads_attach (args, from_tty)
1119 char *args;
1120 int from_tty;
1122 if (!args)
1123 error_no_arg ("process-id to attach");
1125 push_target (&linuxthreads_ops);
1126 linuxthreads_breakpoints_inserted = 1;
1127 linuxthreads_breakpoint_last = -1;
1128 linuxthreads_wait_last = -1;
1129 WSETSTOP (linuxthreads_exit_status, 0);
1131 child_ops.to_attach (args, from_tty);
1133 if (linuxthreads_max)
1134 linuxthreads_attach_pending = 1;
1137 /* Take a program previously attached to and detaches it.
1138 The program resumes execution and will no longer stop
1139 on signals, etc. We'd better not have left any breakpoints
1140 in the program or it'll die when it hits one. For this
1141 to work, it may be necessary for the process to have been
1142 previously attached. It *might* work if the program was
1143 started via the normal ptrace (PTRACE_TRACEME). */
1145 static void
1146 linuxthreads_detach (args, from_tty)
1147 char *args;
1148 int from_tty;
1150 if (linuxthreads_max)
1152 int i;
1153 int pid;
1154 int off = 0;
1155 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1157 /* Walk through linuxthreads array in order to detach known threads. */
1158 if (linuxthreads_manager_pid != 0)
1160 /* Get rid of all positive zombie breakpoints. */
1161 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1163 if (linuxthreads_breakpoint_zombie[i].step)
1164 continue;
1166 pid = linuxthreads_breakpoint_zombie[i].pid;
1167 if (!linuxthreads_thread_alive (pid))
1168 continue;
1170 if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (pid))
1171 continue;
1173 /* Continue in STEP mode until the thread pc has moved or
1174 until SIGTRAP is found on the same PC. */
1175 if (linuxthreads_find_trap (pid, 0)
1176 && linuxthreads_breakpoint_zombie[i].pc == read_pc_pid (pid))
1177 write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
1178 - DECR_PC_AFTER_BREAK, pid);
1181 /* Detach thread after thread. */
1182 inferior_pid = linuxthreads_manager_pid;
1183 iterate_active_threads (detach_thread, 1);
1185 /* Remove pending SIGTRAP and SIGSTOP */
1186 linuxthreads_find_trap (inferior_pid, 1);
1188 linuxthreads_wait_last = -1;
1189 WSETSTOP (linuxthreads_exit_status, 0);
1192 linuxthreads_inferior_pid = 0;
1193 linuxthreads_breakpoint_pid = 0;
1194 linuxthreads_step_pid = 0;
1195 linuxthreads_step_signo = TARGET_SIGNAL_0;
1196 linuxthreads_manager_pid = 0;
1197 linuxthreads_initial_pid = 0;
1198 linuxthreads_attach_pending = 0;
1199 init_thread_list (); /* Destroy thread info */
1202 child_ops.to_detach (args, from_tty);
1204 unpush_target (&linuxthreads_ops);
1207 /* Resume execution of process PID. If STEP is nozero, then
1208 just single step it. If SIGNAL is nonzero, restart it with that
1209 signal activated. */
1211 static void
1212 linuxthreads_resume (pid, step, signo)
1213 int pid;
1214 int step;
1215 enum target_signal signo;
1217 if (!linuxthreads_max || stop_soon_quietly || linuxthreads_manager_pid == 0)
1219 child_ops.to_resume (pid, step, signo);
1221 else
1223 int rpid;
1224 if (linuxthreads_inferior_pid)
1226 /* Prepare resume of the last thread that hit a breakpoint */
1227 linuxthreads_breakpoints_inserted = 0;
1228 rpid = linuxthreads_inferior_pid;
1229 linuxthreads_step_signo = signo;
1231 else
1233 struct cleanup *old_chain = NULL;
1234 int i;
1236 if (pid < 0)
1238 linuxthreads_step_pid = step ? inferior_pid : 0;
1239 linuxthreads_step_signo = signo;
1240 rpid = inferior_pid;
1242 else
1243 rpid = pid;
1245 if (pid < 0 || !step)
1247 linuxthreads_breakpoints_inserted = 1;
1249 /* Walk through linuxthreads array in order to resume threads */
1250 if (pid >= 0 && inferior_pid != pid)
1252 old_chain = save_inferior_pid ();
1253 inferior_pid = pid;
1256 iterate_active_threads (resume_thread, 0);
1257 if (linuxthreads_manager_pid != inferior_pid
1258 && !linuxthreads_pending_status (linuxthreads_manager_pid))
1259 resume_thread (linuxthreads_manager_pid);
1261 else
1262 linuxthreads_breakpoints_inserted = 0;
1264 /* Deal with zombie breakpoint */
1265 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1266 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1268 if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
1270 /* The current pc is out of zombie breakpoint. */
1271 REMOVE_BREAKPOINT_ZOMBIE(i);
1273 break;
1276 if (old_chain != NULL)
1277 do_cleanups (old_chain);
1280 /* Resume initial thread. */
1281 /* [unles it has a wait event pending] */
1282 if (!linuxthreads_pending_status (rpid))
1284 child_ops.to_resume (rpid, step, signo);
1289 /* Abstract out the child_wait functionality. */
1291 linux_child_wait (pid, rpid, status)
1292 int pid;
1293 int *rpid;
1294 int *status;
1296 int save_errno;
1298 /* Note: inftarg has these inside the loop. */
1299 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1300 attached process. */
1301 set_sigio_trap ();
1303 errno = save_errno = 0;
1304 for (;;)
1306 errno = 0;
1307 *rpid = waitpid (pid, status, __WCLONE | WNOHANG);
1308 save_errno = errno;
1310 if (*rpid > 0)
1312 /* Got an event -- break out */
1313 break;
1315 if (errno == EINTR) /* interrupted by signal, try again */
1317 continue;
1320 errno = 0;
1321 *rpid = waitpid (pid, status, WNOHANG);
1322 if (*rpid > 0)
1324 /* Got an event -- break out */
1325 break;
1327 if (errno == EINTR)
1329 continue;
1331 if (errno != 0 && save_errno != 0)
1333 break;
1335 sigsuspend(&linuxthreads_block_mask);
1337 clear_sigio_trap ();
1338 clear_sigint_trap ();
1340 return errno ? errno : save_errno;
1344 /* Wait for any threads to stop. We may have to convert PID from a thread id
1345 to a LWP id, and vice versa on the way out. */
1347 static int
1348 linuxthreads_wait (pid, ourstatus)
1349 int pid;
1350 struct target_waitstatus *ourstatus;
1352 int status;
1353 int rpid;
1354 int i;
1355 int last;
1356 int *wstatus;
1358 if (linuxthreads_max && !linuxthreads_breakpoints_inserted)
1359 wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
1361 /* See if the inferior has chosen values for its signals yet. By
1362 checking for them here, we can be sure we've updated GDB's signal
1363 handling table before the inferior ever gets one of them. (Well,
1364 before we notice, anyway.) */
1365 check_all_signal_numbers ();
1367 for (;;)
1369 if (!linuxthreads_max)
1370 rpid = 0;
1371 else if (!linuxthreads_breakpoints_inserted)
1373 if (linuxthreads_inferior_pid)
1374 pid = linuxthreads_inferior_pid;
1375 else if (pid < 0)
1376 pid = inferior_pid;
1377 last = rpid = 0;
1379 else if (pid < 0 && linuxthreads_wait_last >= 0)
1381 status = linuxthreads_wait_status[linuxthreads_wait_last];
1382 rpid = linuxthreads_wait_pid[linuxthreads_wait_last--];
1384 else if (pid > 0 && linuxthreads_pending_status (pid))
1386 for (i = linuxthreads_wait_last; i >= 0; i--)
1387 if (linuxthreads_wait_pid[i] == pid)
1388 break;
1389 if (i < 0)
1390 rpid = 0;
1391 else
1393 status = linuxthreads_wait_status[i];
1394 rpid = pid;
1395 if (i < linuxthreads_wait_last)
1397 linuxthreads_wait_status[i] =
1398 linuxthreads_wait_status[linuxthreads_wait_last];
1399 linuxthreads_wait_pid[i] =
1400 linuxthreads_wait_pid[linuxthreads_wait_last];
1402 linuxthreads_wait_last--;
1405 else
1406 rpid = 0;
1408 if (rpid == 0)
1410 int save_errno;
1412 save_errno = linux_child_wait (pid, &rpid, &status);
1414 if (rpid == -1)
1416 if (WIFEXITED(linuxthreads_exit_status))
1418 store_waitstatus (ourstatus, linuxthreads_exit_status);
1419 return inferior_pid;
1421 else
1423 fprintf_unfiltered
1424 (gdb_stderr, "Child process unexpectedly missing: %s.\n",
1425 safe_strerror (save_errno));
1426 /* Claim it exited with unknown signal. */
1427 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1428 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1429 return -1;
1433 /* We have now gotten a new event from waitpid above. */
1435 /* Signals arrive in any order. So get all signals until
1436 SIGTRAP and resend previous ones to be held after. */
1437 if (linuxthreads_max
1438 && !linuxthreads_breakpoints_inserted
1439 && WIFSTOPPED(status))
1440 if (WSTOPSIG(status) == SIGTRAP)
1442 while (--last >= 0)
1444 kill (rpid, WSTOPSIG(wstatus[last]));
1447 /* insert negative zombie breakpoint */
1448 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1449 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1450 break;
1451 if (i > linuxthreads_breakpoint_last)
1453 linuxthreads_breakpoint_zombie[i].pid = rpid;
1454 linuxthreads_breakpoint_last++;
1456 linuxthreads_breakpoint_zombie[i].pc = read_pc_pid (rpid);
1457 linuxthreads_breakpoint_zombie[i].step = 1;
1459 else
1461 if (WSTOPSIG(status) != SIGSTOP)
1463 for (i = 0; i < last; i++)
1464 if (wstatus[i] == status)
1465 break;
1466 if (i >= last)
1468 wstatus[last++] = status;
1471 child_resume (rpid, 1, TARGET_SIGNAL_0);
1472 continue;
1474 if (linuxthreads_inferior_pid)
1475 linuxthreads_inferior_pid = 0;
1478 if (linuxthreads_max && !stop_soon_quietly)
1480 if (linuxthreads_max
1481 && WIFSTOPPED(status)
1482 && WSTOPSIG(status) == SIGSTOP)
1484 /* Skip SIGSTOP signals. */
1485 if (!linuxthreads_pending_status (rpid))
1487 if (linuxthreads_step_pid == rpid)
1489 child_resume (rpid, 1, linuxthreads_step_signo);
1491 else
1493 child_resume (rpid, 0, TARGET_SIGNAL_0);
1496 continue;
1499 /* Do no report exit status of cloned threads. */
1500 if (WIFEXITED(status))
1502 if (rpid == linuxthreads_initial_pid)
1503 linuxthreads_exit_status = status;
1505 /* Remove any zombie breakpoint. */
1506 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1507 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1509 REMOVE_BREAKPOINT_ZOMBIE(i);
1510 break;
1512 if (pid > 0)
1513 pid = -1;
1514 continue;
1517 /* Deal with zombie breakpoint */
1518 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1519 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1520 break;
1522 if (i <= linuxthreads_breakpoint_last)
1524 /* There is a potential zombie breakpoint */
1525 if (WIFEXITED(status)
1526 || linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
1528 /* The current pc is out of zombie breakpoint. */
1529 REMOVE_BREAKPOINT_ZOMBIE(i);
1531 else if (!linuxthreads_breakpoint_zombie[i].step
1532 && WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
1534 /* This is a real one ==> decrement PC and restart. */
1535 write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
1536 - DECR_PC_AFTER_BREAK, rpid);
1537 if (linuxthreads_step_pid == rpid)
1539 child_resume (rpid, 1, linuxthreads_step_signo);
1541 else
1543 child_resume (rpid, 0, TARGET_SIGNAL_0);
1545 continue;
1549 /* Walk through linuxthreads array in order to stop them */
1550 if (linuxthreads_breakpoints_inserted)
1551 update_stop_threads (rpid);
1554 else if (rpid != inferior_pid)
1555 continue;
1557 store_waitstatus (ourstatus, status);
1559 if (linuxthreads_attach_pending && !stop_soon_quietly)
1561 int on = 1;
1562 if (!using_thread_db)
1564 target_write_memory (linuxthreads_debug,
1565 (char *) &on, sizeof (on));
1566 update_stop_threads (rpid);
1568 linuxthreads_attach_pending = 0;
1571 if (linuxthreads_breakpoints_inserted
1572 && WIFSTOPPED(status)
1573 && WSTOPSIG(status) == SIGTRAP)
1574 linuxthreads_breakpoint_pid = rpid;
1575 else if (linuxthreads_breakpoint_pid)
1576 linuxthreads_breakpoint_pid = 0;
1578 return rpid;
1582 /* Fork an inferior process, and start debugging it with ptrace. */
1584 static void
1585 linuxthreads_create_inferior (exec_file, allargs, env)
1586 char *exec_file;
1587 char *allargs;
1588 char **env;
1590 if (!exec_file && !exec_bfd)
1592 error ("No executable file specified.\n\
1593 Use the \"file\" or \"exec-file\" command.");
1594 return;
1597 push_target (&linuxthreads_ops);
1598 linuxthreads_breakpoints_inserted = 1;
1599 linuxthreads_breakpoint_last = -1;
1600 linuxthreads_wait_last = -1;
1601 WSETSTOP (linuxthreads_exit_status, 0);
1603 if (linuxthreads_max)
1604 linuxthreads_attach_pending = 1;
1606 child_ops.to_create_inferior (exec_file, allargs, env);
1609 void
1610 linuxthreads_discard_global_state ()
1612 linuxthreads_inferior_pid = 0;
1613 linuxthreads_breakpoint_pid = 0;
1614 linuxthreads_step_pid = 0;
1615 linuxthreads_step_signo = TARGET_SIGNAL_0;
1616 linuxthreads_manager_pid = 0;
1617 linuxthreads_initial_pid = 0;
1618 linuxthreads_attach_pending = 0;
1619 linuxthreads_max = 0;
1622 /* Clean up after the inferior dies. */
1624 static void
1625 linuxthreads_mourn_inferior ()
1627 if (linuxthreads_max)
1629 int off = 0;
1630 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1632 linuxthreads_discard_global_state ();
1633 init_thread_list(); /* Destroy thread info */
1636 child_ops.to_mourn_inferior ();
1638 unpush_target (&linuxthreads_ops);
1641 /* Kill the inferior process */
1643 static void
1644 linuxthreads_kill ()
1646 int rpid;
1647 int status;
1649 if (inferior_pid == 0)
1650 return;
1652 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1654 /* Remove all threads status. */
1655 inferior_pid = linuxthreads_manager_pid;
1656 iterate_active_threads (kill_thread, 1);
1659 kill_thread (inferior_pid);
1661 #if 0
1662 /* doing_quit_force solves a real problem, but I think a properly
1663 placed call to catch_errors would do the trick much more cleanly. */
1664 if (doing_quit_force >= 0)
1666 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1668 /* Wait for thread to complete */
1669 while ((rpid = waitpid (-1, &status, __WCLONE)) > 0)
1670 if (!WIFEXITED(status))
1671 kill_thread (rpid);
1673 while ((rpid = waitpid (-1, &status, 0)) > 0)
1674 if (!WIFEXITED(status))
1675 kill_thread (rpid);
1677 else
1678 while ((rpid = waitpid (inferior_pid, &status, 0)) > 0)
1679 if (!WIFEXITED(status))
1680 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
1682 #endif
1684 /* Wait for all threads. */
1687 rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
1689 while (rpid > 0 || errno == EINTR);
1690 /* FIXME: should no longer need to handle EINTR here. */
1694 rpid = waitpid (-1, &status, WNOHANG);
1696 while (rpid > 0 || errno == EINTR);
1697 /* FIXME: should no longer need to handle EINTR here. */
1699 linuxthreads_mourn_inferior ();
1702 /* Insert a breakpoint */
1704 static int
1705 linuxthreads_insert_breakpoint (addr, contents_cache)
1706 CORE_ADDR addr;
1707 char *contents_cache;
1709 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1711 linuxthreads_breakpoint_addr = addr;
1712 iterate_active_threads (insert_breakpoint, 1);
1713 insert_breakpoint (linuxthreads_manager_pid);
1716 return child_ops.to_insert_breakpoint (addr, contents_cache);
1719 /* Remove a breakpoint */
1721 static int
1722 linuxthreads_remove_breakpoint (addr, contents_cache)
1723 CORE_ADDR addr;
1724 char *contents_cache;
1726 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1728 linuxthreads_breakpoint_addr = addr;
1729 iterate_active_threads (remove_breakpoint, 1);
1730 remove_breakpoint (linuxthreads_manager_pid);
1733 return child_ops.to_remove_breakpoint (addr, contents_cache);
1736 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1738 static int
1739 linuxthreads_can_run ()
1741 return child_suppress_run;
1745 static void
1746 init_linuxthreads_ops ()
1748 linuxthreads_ops.to_shortname = "linuxthreads";
1749 linuxthreads_ops.to_longname = "LINUX threads and pthread.";
1750 linuxthreads_ops.to_doc = "LINUX threads and pthread support.";
1751 linuxthreads_ops.to_attach = linuxthreads_attach;
1752 linuxthreads_ops.to_detach = linuxthreads_detach;
1753 linuxthreads_ops.to_resume = linuxthreads_resume;
1754 linuxthreads_ops.to_wait = linuxthreads_wait;
1755 linuxthreads_ops.to_kill = linuxthreads_kill;
1756 linuxthreads_ops.to_can_run = linuxthreads_can_run;
1757 linuxthreads_ops.to_stratum = thread_stratum;
1758 linuxthreads_ops.to_insert_breakpoint = linuxthreads_insert_breakpoint;
1759 linuxthreads_ops.to_remove_breakpoint = linuxthreads_remove_breakpoint;
1760 linuxthreads_ops.to_create_inferior = linuxthreads_create_inferior;
1761 linuxthreads_ops.to_mourn_inferior = linuxthreads_mourn_inferior;
1762 linuxthreads_ops.to_thread_alive = linuxthreads_thread_alive;
1763 linuxthreads_ops.to_pid_to_str = linuxthreads_pid_to_str;
1764 linuxthreads_ops.to_magic = OPS_MAGIC;
1767 void
1768 _initialize_linuxthreads ()
1770 struct sigaction sact;
1771 sigset_t linuxthreads_wait_mask; /* sigset with SIGCHLD */
1773 init_linuxthreads_ops ();
1774 add_target (&linuxthreads_ops);
1775 child_suppress_run = 1;
1777 /* Hook onto the "new_objfile" event.
1778 * If someone else is already hooked onto the event,
1779 * then make sure he will be called after we are.
1781 target_new_objfile_chain = target_new_objfile_hook;
1782 target_new_objfile_hook = linuxthreads_new_objfile;
1784 /* Attach SIGCHLD handler */
1785 sact.sa_handler = sigchld_handler;
1786 sigemptyset (&sact.sa_mask);
1787 sact.sa_flags = 0;
1788 sigaction (SIGCHLD, &sact, NULL);
1790 /* initialize SIGCHLD mask */
1791 sigemptyset (&linuxthreads_wait_mask);
1792 sigaddset (&linuxthreads_wait_mask, SIGCHLD);
1794 /* Use SIG_BLOCK to block receipt of SIGCHLD.
1795 The block_mask will allow us to wait for this signal explicitly. */
1796 sigprocmask(SIG_BLOCK,
1797 &linuxthreads_wait_mask,
1798 &linuxthreads_block_mask);
1799 /* Make sure that linuxthreads_block_mask is not blocking SIGCHLD */
1800 sigdelset (&linuxthreads_block_mask, SIGCHLD);