1 /* Low level interface for debugging GNU/Linux threads for GDB,
3 Copyright 1998, 1999 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This module implements the debugging interface of the linuxthreads package
22 of the glibc. This package implements a simple clone()-based implementation
23 of Posix threads for Linux. To use this module, be sure that you have at
24 least the version of the linuxthreads package that holds the support of
25 GDB (currently 0.8 included in the glibc-2.0.7).
27 Right now, the linuxthreads package does not care of priority scheduling,
28 so, neither this module does; In particular, the threads are resumed
29 in any order, which could lead to different scheduling than the one
30 happening when GDB does not control the execution.
32 The latest point is that ptrace(PT_ATTACH, ...) is intrusive in Linux:
33 When a process is attached, then the attaching process becomes the current
34 parent of the attached process, and the old parent has lost this child.
35 If the old parent does a wait[...](), then this child is no longer
36 considered by the kernel as a child of the old parent, thus leading to
37 results of the call different when the child is attached and when it's not.
39 A fix has been submitted to the Linux community to solve this problem,
40 which consequences are not visible to the application itself, but on the
41 process which may wait() for the completion of the application (mostly,
42 it may consider that the application no longer exists (errno == ECHILD),
43 although it does, and thus being unable to get the exit status and resource
44 usage of the child. If by chance, it is able to wait() for the application
45 after it has died (by receiving first a SIGCHILD, and then doing a wait(),
46 then the exit status and resource usage may be wrong, because the
47 linuxthreads package heavily relies on wait() synchronization to keep
50 #include <sys/types.h> /* for pid_t */
51 #include <sys/ptrace.h> /* for PT_* flags */
52 #include <sys/wait.h> /* for WUNTRACED and __WCLONE flags */
53 #include <signal.h> /* for struct sigaction and NSIG */
54 #include <sys/utsname.h>
60 #include "gdbthread.h"
63 #include "breakpoint.h"
66 #define PT_ATTACH PTRACE_ATTACH
69 #define PT_KILL PTRACE_KILL
72 #define PT_READ_U PTRACE_PEEKUSR
76 #define LINUXTHREAD_NSIG NSIG
79 #define LINUXTHREAD_NSIG _NSIG
83 extern int child_suppress_run; /* make inftarg.c non-runnable */
84 struct target_ops linuxthreads_ops; /* Forward declaration */
85 extern struct target_ops child_ops; /* target vector for inftarg.c */
87 static CORE_ADDR linuxthreads_handles; /* array of linuxthreads handles */
88 static CORE_ADDR linuxthreads_manager; /* pid of linuxthreads manager thread */
89 static CORE_ADDR linuxthreads_initial; /* pid of linuxthreads initial thread */
90 static CORE_ADDR linuxthreads_debug; /* linuxthreads internal debug flag */
91 static CORE_ADDR linuxthreads_num; /* number of valid handle entries */
93 static int linuxthreads_max; /* Maximum number of linuxthreads.
94 Zero if this executable doesn't use
95 threads, or wasn't linked with a
96 debugger-friendly version of the
97 linuxthreads library. */
99 static int linuxthreads_sizeof_handle; /* size of a linuxthreads handle */
100 static int linuxthreads_offset_descr; /* h_descr offset of the linuxthreads
102 static int linuxthreads_offset_pid; /* p_pid offset of the linuxthreads
105 static int linuxthreads_manager_pid; /* manager pid */
106 static int linuxthreads_initial_pid; /* initial pid */
108 /* These variables form a bag of threads with interesting status. If
109 wait_thread (PID) finds that PID stopped for some interesting
110 reason (i.e. anything other than stopped with SIGSTOP), then it
111 records its status in this queue. linuxthreads_wait and
112 linuxthreads_find_trap extract processes from here. */
113 static int *linuxthreads_wait_pid; /* wait array of pid */
114 static int *linuxthreads_wait_status; /* wait array of status */
115 static int linuxthreads_wait_last; /* index of last valid elt in
116 linuxthreads_wait_{pid,status} */
118 static sigset_t linuxthreads_wait_mask; /* sigset with SIGCHLD */
120 static int linuxthreads_step_pid; /* current stepped pid */
121 static int linuxthreads_step_signo; /* current stepped target signal */
122 static int linuxthreads_exit_status; /* exit status of initial thread */
124 static int linuxthreads_inferior_pid; /* temporary internal inferior pid */
125 static int linuxthreads_breakpoint_pid; /* last pid that hit a breakpoint */
126 static int linuxthreads_attach_pending; /* attach command without wait */
128 static int linuxthreads_breakpoints_inserted; /* any breakpoints inserted */
130 /* LinuxThreads uses certain signals for communication between
131 processes; we need to tell GDB to pass them through silently to the
132 inferior. The LinuxThreads library has global variables we can
133 read containing the relevant signal numbers, but since the signal
134 numbers are chosen at run-time, those variables aren't initialized
135 until the shared library's constructors have had a chance to run. */
137 struct linuxthreads_signal {
139 /* The name of the LinuxThreads library variable that contains
140 the signal number. */
143 /* True if this variable must exist for us to debug properly. */
146 /* The variable's address in the inferior, or zero if the
147 LinuxThreads library hasn't been loaded into this inferior yet. */
150 /* The signal number, or zero if we don't know yet (either because
151 we haven't found the variable, or it hasn't been initialized).
152 This is an actual target signal number that you could pass to
153 `kill', not a GDB signal number. */
156 /* GDB's original settings for `stop' and `print' for this signal.
157 We restore them when the user selects a different executable.
158 Invariant: if sig->signal != 0, then sig->{stop,print} contain
159 the original settings. */
163 struct linuxthreads_signal linuxthreads_sig_restart = {
164 "__pthread_sig_restart", 1, 0, 0, 0, 0
166 struct linuxthreads_signal linuxthreads_sig_cancel = {
167 "__pthread_sig_cancel", 1, 0, 0, 0, 0
169 struct linuxthreads_signal linuxthreads_sig_debug = {
170 "__pthread_sig_debug", 0, 0, 0, 0, 0
173 /* A table of breakpoint locations, one per PID. */
174 static struct linuxthreads_breakpoint {
175 CORE_ADDR pc; /* PC of breakpoint */
176 int pid; /* pid of breakpoint */
177 int step; /* whether the pc has been reached after sstep */
178 } *linuxthreads_breakpoint_zombie; /* Zombie breakpoints array */
179 static int linuxthreads_breakpoint_last; /* Last zombie breakpoint */
181 /* linuxthreads_{insert,remove}_breakpoint pass the breakpoint address
182 to {insert,remove}_breakpoint via this variable, since
183 iterate_active_threads doesn't provide any way to pass values
184 through to the worker function. */
185 static CORE_ADDR linuxthreads_breakpoint_addr;
187 #define REMOVE_BREAKPOINT_ZOMBIE(_i) \
189 if ((_i) < linuxthreads_breakpoint_last) \
190 linuxthreads_breakpoint_zombie[(_i)] = \
191 linuxthreads_breakpoint_zombie[linuxthreads_breakpoint_last]; \
192 linuxthreads_breakpoint_last--; \
197 #ifndef PTRACE_XFER_TYPE
198 #define PTRACE_XFER_TYPE int
200 /* Check to see if the given thread is alive. */
202 linuxthreads_thread_alive (pid)
206 return ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE)0, 0) >= 0 || errno == 0;
209 /* On detach(), find a SIGTRAP status. If stop is non-zero, find a
212 Make sure PID is ready to run, and free of interference from our
213 efforts to debug it (e.g., pending SIGSTOP or SIGTRAP signals). If
214 STOP is zero, just look for a SIGTRAP. If STOP is non-zero, look
215 for a SIGSTOP, too. Return non-zero if PID is alive and ready to
216 run; return zero if PID is dead.
218 PID may or may not be stopped at the moment, and we may or may not
219 have waited for it already. We check the linuxthreads_wait bag in
220 case we've already got a status for it. We may possibly wait for
223 PID may have signals waiting to be delivered. If they're caused by
224 our efforts to debug it, accept them with wait, but don't pass them
225 through to PID. Do pass all other signals through. */
227 linuxthreads_find_trap (pid, stop)
237 /* PID may have any number of signals pending. The kernel will
238 report each of them to us via wait, and then it's up to us to
239 pass them along to the process via ptrace, if we so choose.
241 We need to paw through the whole set until we've found a SIGTRAP
242 (or a SIGSTOP, if `stop' is set). We don't pass the SIGTRAP (or
243 SIGSTOP) through, but we do re-send all the others, so PID will
244 receive them when we resume it. */
245 int *wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
248 /* Look at the pending status */
249 for (i = linuxthreads_wait_last; i >= 0; i--)
250 if (linuxthreads_wait_pid[i] == pid)
252 status = linuxthreads_wait_status[i];
254 /* Delete the i'th member of the table. Since the table is
255 unordered, we can do this simply by copying the table's
256 last element to the i'th position, and shrinking the table
258 if (i < linuxthreads_wait_last)
260 linuxthreads_wait_status[i] =
261 linuxthreads_wait_status[linuxthreads_wait_last];
262 linuxthreads_wait_pid[i] =
263 linuxthreads_wait_pid[linuxthreads_wait_last];
265 linuxthreads_wait_last--;
267 if (!WIFSTOPPED(status)) /* Thread has died */
270 if (WSTOPSIG(status) == SIGTRAP)
277 else if (WSTOPSIG(status) == SIGSTOP)
293 /* Make sure that we'll find what we're looking for. */
300 /* Catch all status until SIGTRAP and optionally SIGSTOP show up. */
303 child_resume (pid, 1, TARGET_SIGNAL_0);
307 rpid = waitpid (pid, &status, __WCLONE);
313 /* There are a few reasons the wait call above may have
314 failed. If the thread manager dies, its children get
315 reparented, and this interferes with GDB waiting for
316 them, in some cases. Another possibility is that the
317 initial thread was not cloned, so calling wait with
318 __WCLONE won't find it. I think neither of these should
319 occur in modern Linux kernels --- they don't seem to in
321 rpid = waitpid (pid, &status, 0);
325 perror_with_name ("waitpid");
328 if (!WIFSTOPPED(status)) /* Thread has died */
331 if (WSTOPSIG(status) == SIGTRAP)
332 if (!stop || found_stop)
336 else if (WSTOPSIG(status) != SIGSTOP)
337 wstatus[last++] = status;
347 /* Resend any other signals we noticed to the thread, to be received
348 when we continue it. */
350 kill (pid, WSTOPSIG(wstatus[last]));
355 /* Cleanup stub for save_inferior_pid. */
357 restore_inferior_pid (arg)
364 /* Register a cleanup to restore the value of inferior_pid. */
365 static struct cleanup *
368 return make_cleanup (restore_inferior_pid, (void *) inferior_pid);
372 sigchld_handler (signo)
375 /* This handler is used to get an EINTR while doing waitpid()
376 when an event is received */
379 /* Have we already collected a wait status for PID in the
380 linuxthreads_wait bag? */
382 linuxthreads_pending_status (pid)
386 for (i = linuxthreads_wait_last; i >= 0; i--)
387 if (linuxthreads_wait_pid[i] == pid)
393 /* Internal linuxthreads signal management */
395 /* Check in OBJFILE for the variable that holds the number for signal SIG.
396 We assume that we've already found other LinuxThreads-ish variables
397 in OBJFILE, so we complain if it's required, but not there.
398 Return true iff things are okay. */
400 find_signal_var (sig, objfile)
401 struct linuxthreads_signal *sig;
402 struct objfile *objfile;
404 struct minimal_symbol *ms = lookup_minimal_symbol (sig->var, NULL, objfile);
410 fprintf_unfiltered (gdb_stderr,
411 "Unable to find linuxthreads symbol \"%s\"\n",
422 sig->addr = SYMBOL_VALUE_ADDRESS (ms);
428 find_all_signal_vars (objfile)
429 struct objfile *objfile;
431 return ( find_signal_var (&linuxthreads_sig_restart, objfile)
432 && find_signal_var (&linuxthreads_sig_cancel, objfile)
433 && find_signal_var (&linuxthreads_sig_debug, objfile));
436 /* A struct complaint isn't appropriate here. */
437 static int complained_cannot_determine_thread_signal_number = 0;
439 /* Check to see if the variable holding the signal number for SIG has
440 been initialized yet. If it has, tell GDB to pass that signal
441 through to the inferior silently. */
443 check_signal_number (sig)
444 struct linuxthreads_signal *sig;
449 /* We already know this signal number. */
453 /* We don't know the variable's address yet. */
456 if (target_read_memory (sig->addr, (char *)&num, sizeof (num))
459 /* If this happens once, it'll probably happen for all the
460 signals, so only complain once. */
461 if (! complained_cannot_determine_thread_signal_number)
462 warning ("Cannot determine thread signal number; "
463 "GDB may report spurious signals.");
464 complained_cannot_determine_thread_signal_number = 1;
469 /* It hasn't been initialized yet. */
472 /* We know sig->signal was zero, and is becoming non-zero, so it's
473 okay to sample GDB's original settings. */
475 sig->stop = signal_stop_update (target_signal_from_host (num), 0);
476 sig->print = signal_print_update (target_signal_from_host (num), 0);
481 check_all_signal_numbers ()
483 /* If this isn't a LinuxThreads program, quit early. */
484 if (! linuxthreads_max)
487 check_signal_number (&linuxthreads_sig_restart);
488 check_signal_number (&linuxthreads_sig_cancel);
489 check_signal_number (&linuxthreads_sig_debug);
491 /* handle linuxthread exit */
492 if (linuxthreads_sig_debug.signal
493 || linuxthreads_sig_restart.signal)
495 struct sigaction sact;
497 sact.sa_handler = sigchld_handler;
498 sigemptyset(&sact.sa_mask);
500 if (linuxthreads_sig_debug.signal > 0)
501 sigaction(linuxthreads_sig_cancel.signal, &sact, NULL);
503 sigaction(linuxthreads_sig_restart.signal, &sact, NULL);
508 /* Restore GDB's original settings for SIG.
509 This should only be called when we're no longer sure if we're
510 talking to an executable that uses LinuxThreads, so we clear the
511 signal number and variable address too. */
514 struct linuxthreads_signal *sig;
519 /* We know sig->signal was non-zero, and is becoming zero, so it's
520 okay to restore GDB's original settings. */
521 signal_stop_update (target_signal_from_host (sig->signal), sig->stop);
522 signal_print_update (target_signal_from_host (sig->signal), sig->print);
529 /* Restore GDB's original settings for all LinuxThreads signals.
530 This should only be called when we're no longer sure if we're
531 talking to an executable that uses LinuxThreads, so we clear the
532 signal number and variable address too. */
534 restore_all_signals ()
536 restore_signal (&linuxthreads_sig_restart);
537 restore_signal (&linuxthreads_sig_cancel);
538 restore_signal (&linuxthreads_sig_debug);
540 /* If it happens again, we should complain again. */
541 complained_cannot_determine_thread_signal_number = 0;
547 /* Apply FUNC to the pid of each active thread. This consults the
548 inferior's handle table to find active threads.
550 If ALL is non-zero, process all threads.
551 If ALL is zero, skip threads with pending status. */
553 iterate_active_threads (func, all)
562 read_memory (linuxthreads_num, (char *)&num, sizeof (int));
564 for (i = 0; i < linuxthreads_max && num > 0; i++)
566 read_memory (linuxthreads_handles +
567 linuxthreads_sizeof_handle * i + linuxthreads_offset_descr,
568 (char *)&descr, sizeof (void *));
572 read_memory (descr + linuxthreads_offset_pid,
573 (char *)&pid, sizeof (pid_t));
574 if (pid > 0 && pid != linuxthreads_manager_pid
575 && (all || (!linuxthreads_pending_status (pid))))
582 /* Insert a thread breakpoint at linuxthreads_breakpoint_addr.
583 This is the worker function for linuxthreads_insert_breakpoint,
584 which passes it to iterate_active_threads. */
586 insert_breakpoint (pid)
591 /* Remove (if any) the positive zombie breakpoint. */
592 for (j = linuxthreads_breakpoint_last; j >= 0; j--)
593 if (linuxthreads_breakpoint_zombie[j].pid == pid)
595 if ((linuxthreads_breakpoint_zombie[j].pc - DECR_PC_AFTER_BREAK
596 == linuxthreads_breakpoint_addr)
597 && !linuxthreads_breakpoint_zombie[j].step)
598 REMOVE_BREAKPOINT_ZOMBIE(j);
603 /* Note that we're about to remove a thread breakpoint at
604 linuxthreads_breakpoint_addr.
606 This is the worker function for linuxthreads_remove_breakpoint,
607 which passes it to iterate_active_threads. The actual work of
608 overwriting the breakpoint instruction is done by
609 child_ops.to_remove_breakpoint; here, we simply create a zombie
610 breakpoint if the thread's PC is pointing at the breakpoint being
613 remove_breakpoint (pid)
618 /* Insert a positive zombie breakpoint (if needed). */
619 for (j = 0; j <= linuxthreads_breakpoint_last; j++)
620 if (linuxthreads_breakpoint_zombie[j].pid == pid)
623 if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
625 CORE_ADDR pc = read_pc_pid (pid);
626 if (linuxthreads_breakpoint_addr == pc - DECR_PC_AFTER_BREAK
627 && j > linuxthreads_breakpoint_last)
629 linuxthreads_breakpoint_zombie[j].pid = pid;
630 linuxthreads_breakpoint_zombie[j].pc = pc;
631 linuxthreads_breakpoint_zombie[j].step = 0;
632 linuxthreads_breakpoint_last++;
642 if (in_thread_list (pid))
643 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
648 /* Resume a thread */
653 if (pid != inferior_pid
654 && in_thread_list (pid)
655 && linuxthreads_thread_alive (pid))
657 if (pid == linuxthreads_step_pid)
658 child_resume (pid, 1, linuxthreads_step_signo);
660 child_resume (pid, 0, TARGET_SIGNAL_0);
664 /* Detach a thread */
669 if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
671 /* Remove pending SIGTRAP and SIGSTOP */
672 linuxthreads_find_trap (pid, 1);
675 detach (TARGET_SIGNAL_0);
676 inferior_pid = linuxthreads_manager_pid;
685 if (pid != inferior_pid)
687 if (in_thread_list (pid))
689 else if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) == 0)
691 if (!linuxthreads_attach_pending)
692 printf_unfiltered ("[New %s]\n", target_pid_to_str (pid));
694 if (linuxthreads_sig_debug.signal)
695 /* After a new thread in glibc 2.1 signals gdb its existence,
696 it suspends itself and wait for linuxthreads_sig_restart,
697 now we can wake up it. */
698 kill (pid, linuxthreads_sig_restart.signal);
701 perror_with_name ("ptrace in stop_thread");
705 /* Wait for a thread */
713 if (pid != inferior_pid && in_thread_list (pid))
717 /* Get first pid status. */
718 rpid = waitpid(pid, &status, __WCLONE);
724 /* There are two reasons this might have failed:
726 1) PID is the initial thread, which wasn't cloned, so
727 passing the __WCLONE flag to waitpid prevented us from
730 2) The manager thread is the parent of all but the
731 initial thread; if it dies, the children will all be
732 reparented to init, which will wait for them. This means
733 our call to waitpid won't find them.
735 Actually, based on a casual look at the 2.0.36 kernel
736 code, I don't think either of these cases happen. But I
737 don't have things set up for remotely debugging the
738 kernel, so I'm not sure. And perhaps older kernels
740 rpid = waitpid(pid, &status, 0);
743 if (errno != EINTR && linuxthreads_thread_alive (pid))
744 perror_with_name ("waitpid");
746 /* the thread is dead. */
749 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
751 linuxthreads_wait_pid[++linuxthreads_wait_last] = pid;
752 linuxthreads_wait_status[linuxthreads_wait_last] = status;
757 /* Walk through the linuxthreads handles in order to detect all
758 threads and stop them */
760 update_stop_threads (test_pid)
763 struct cleanup *old_chain = NULL;
765 check_all_signal_numbers ();
767 if (linuxthreads_manager_pid == 0)
769 if (linuxthreads_manager)
771 if (test_pid > 0 && test_pid != inferior_pid)
773 old_chain = save_inferior_pid ();
774 inferior_pid = test_pid;
776 read_memory (linuxthreads_manager,
777 (char *)&linuxthreads_manager_pid, sizeof (pid_t));
779 if (linuxthreads_initial)
781 if (test_pid > 0 && test_pid != inferior_pid)
783 old_chain = save_inferior_pid ();
784 inferior_pid = test_pid;
786 read_memory(linuxthreads_initial,
787 (char *)&linuxthreads_initial_pid, sizeof (pid_t));
791 if (linuxthreads_manager_pid != 0)
793 if (old_chain == NULL && test_pid > 0 &&
794 test_pid != inferior_pid && linuxthreads_thread_alive (test_pid))
796 old_chain = save_inferior_pid ();
797 inferior_pid = test_pid;
800 if (linuxthreads_thread_alive (inferior_pid))
804 if (test_pid != linuxthreads_manager_pid
805 && !linuxthreads_pending_status (linuxthreads_manager_pid))
807 stop_thread (linuxthreads_manager_pid);
808 wait_thread (linuxthreads_manager_pid);
810 if (!in_thread_list (test_pid))
812 if (!linuxthreads_attach_pending)
813 printf_unfiltered ("[New %s]\n",
814 target_pid_to_str (test_pid));
815 add_thread (test_pid);
816 if (linuxthreads_sig_debug.signal
817 && inferior_pid == test_pid)
818 /* After a new thread in glibc 2.1 signals gdb its
819 existence, it suspends itself and wait for
820 linuxthreads_sig_restart, now we can wake up
822 kill (test_pid, linuxthreads_sig_restart.signal);
825 iterate_active_threads (stop_thread, 0);
826 iterate_active_threads (wait_thread, 0);
830 if (old_chain != NULL)
831 do_cleanups (old_chain);
834 /* This routine is called whenever a new symbol table is read in, or
835 when all symbol tables are removed. linux-thread event handling
836 can only be initialized when we find the right variables in
837 libpthread.so. Since it's a shared library, those variables don't
838 show up until the library gets mapped and the symbol table is read
841 /* This new_objfile event is now managed by a chained function pointer.
842 * It is the callee's responsability to call the next client on the chain.
845 /* Saved pointer to previous owner of the new_objfile event. */
846 static void (*target_new_objfile_chain) PARAMS ((struct objfile *));
849 linuxthreads_new_objfile (objfile)
850 struct objfile *objfile;
852 struct minimal_symbol *ms;
856 /* We're starting an entirely new executable, so we can no
857 longer be sure that it uses LinuxThreads. Restore the signal
858 flags to their original states. */
859 restore_all_signals ();
861 /* Indicate that we don't know anything's address any more. */
862 linuxthreads_max = 0;
867 /* If we've already found our variables in another objfile, don't
868 bother looking for them again. */
869 if (linuxthreads_max)
872 if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL, objfile))
873 /* This object file isn't the pthreads library. */
876 if ((ms = lookup_minimal_symbol ("__pthread_threads_debug",
877 NULL, objfile)) == NULL)
879 /* The debugging-aware libpthreads is not present in this objfile */
881 This program seems to use POSIX threads, but the thread library used\n\
882 does not support debugging. This may make using GDB difficult. Don't\n\
883 set breakpoints or single-step through code that might be executed by\n\
884 any thread other than the main thread.");
887 linuxthreads_debug = SYMBOL_VALUE_ADDRESS (ms);
889 /* Read internal structures configuration */
890 if ((ms = lookup_minimal_symbol ("__pthread_sizeof_handle",
891 NULL, objfile)) == NULL
892 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
893 (char *)&linuxthreads_sizeof_handle,
894 sizeof (linuxthreads_sizeof_handle)) != 0)
896 fprintf_unfiltered (gdb_stderr,
897 "Unable to find linuxthreads symbol \"%s\"\n",
898 "__pthread_sizeof_handle");
902 if ((ms = lookup_minimal_symbol ("__pthread_offsetof_descr",
903 NULL, objfile)) == NULL
904 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
905 (char *)&linuxthreads_offset_descr,
906 sizeof (linuxthreads_offset_descr)) != 0)
908 fprintf_unfiltered (gdb_stderr,
909 "Unable to find linuxthreads symbol \"%s\"\n",
910 "__pthread_offsetof_descr");
914 if ((ms = lookup_minimal_symbol ("__pthread_offsetof_pid",
915 NULL, objfile)) == NULL
916 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
917 (char *)&linuxthreads_offset_pid,
918 sizeof (linuxthreads_offset_pid)) != 0)
920 fprintf_unfiltered (gdb_stderr,
921 "Unable to find linuxthreads symbol \"%s\"\n",
922 "__pthread_offsetof_pid");
926 if (! find_all_signal_vars (objfile))
929 /* Read adresses of internal structures to access */
930 if ((ms = lookup_minimal_symbol ("__pthread_handles",
931 NULL, objfile)) == NULL)
933 fprintf_unfiltered (gdb_stderr,
934 "Unable to find linuxthreads symbol \"%s\"\n",
935 "__pthread_handles");
938 linuxthreads_handles = SYMBOL_VALUE_ADDRESS (ms);
940 if ((ms = lookup_minimal_symbol ("__pthread_handles_num",
941 NULL, objfile)) == NULL)
943 fprintf_unfiltered (gdb_stderr,
944 "Unable to find linuxthreads symbol \"%s\"\n",
945 "__pthread_handles_num");
948 linuxthreads_num = SYMBOL_VALUE_ADDRESS (ms);
950 if ((ms = lookup_minimal_symbol ("__pthread_manager_thread",
951 NULL, objfile)) == NULL)
953 fprintf_unfiltered (gdb_stderr,
954 "Unable to find linuxthreads symbol \"%s\"\n",
955 "__pthread_manager_thread");
958 linuxthreads_manager = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
960 if ((ms = lookup_minimal_symbol ("__pthread_initial_thread",
961 NULL, objfile)) == NULL)
963 fprintf_unfiltered (gdb_stderr,
964 "Unable to find linuxthreads symbol \"%s\"\n",
965 "__pthread_initial_thread");
968 linuxthreads_initial = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
970 /* Search for this last, so it won't be set to a non-zero value unless
971 we successfully found all the symbols above. */
972 if ((ms = lookup_minimal_symbol ("__pthread_threads_max",
973 NULL, objfile)) == NULL
974 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
975 (char *)&linuxthreads_max,
976 sizeof (linuxthreads_max)) != 0)
978 fprintf_unfiltered (gdb_stderr,
979 "Unable to find linuxthreads symbol \"%s\"\n",
980 "__pthread_threads_max");
984 /* Allocate gdb internal structures */
985 linuxthreads_wait_pid =
986 (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
987 linuxthreads_wait_status =
988 (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
989 linuxthreads_breakpoint_zombie = (struct linuxthreads_breakpoint *)
990 xmalloc (sizeof (struct linuxthreads_breakpoint) * (linuxthreads_max + 1));
992 if (inferior_pid && !linuxthreads_attach_pending)
995 target_write_memory (linuxthreads_debug, (char *)&on, sizeof (on));
996 linuxthreads_attach_pending = 1;
997 update_stop_threads (inferior_pid);
998 linuxthreads_attach_pending = 0;
1002 /* Call predecessor on chain, if any. */
1003 if (target_new_objfile_chain)
1004 target_new_objfile_chain (objfile);
1007 /* If we have switched threads from a one that stopped at breakpoint,
1008 return 1 otherwise 0. */
1011 linuxthreads_prepare_to_proceed (step)
1014 if (!linuxthreads_max
1015 || !linuxthreads_manager_pid
1016 || !linuxthreads_breakpoint_pid
1017 || !breakpoint_here_p (read_pc_pid (linuxthreads_breakpoint_pid)))
1022 /* Mark the current inferior as single stepping process. */
1023 linuxthreads_step_pid = inferior_pid;
1026 linuxthreads_inferior_pid = linuxthreads_breakpoint_pid;
1027 return linuxthreads_breakpoint_pid;
1030 /* Convert a pid to printable form. */
1033 linuxthreads_pid_to_str (pid)
1036 static char buf[100];
1038 sprintf (buf, "%s %d%s", linuxthreads_max ? "Thread" : "Pid", pid,
1039 (pid == linuxthreads_manager_pid) ? " (manager thread)"
1040 : (pid == linuxthreads_initial_pid) ? " (initial thread)"
1046 /* Attach to process PID, then initialize for debugging it
1047 and wait for the trace-trap that results from attaching. */
1050 linuxthreads_attach (args, from_tty)
1055 error_no_arg ("process-id to attach");
1057 push_target (&linuxthreads_ops);
1058 linuxthreads_breakpoints_inserted = 1;
1059 linuxthreads_breakpoint_last = -1;
1060 linuxthreads_wait_last = -1;
1061 linuxthreads_exit_status = __W_STOPCODE(0);
1063 child_ops.to_attach (args, from_tty);
1065 if (linuxthreads_max)
1066 linuxthreads_attach_pending = 1;
1069 /* Take a program previously attached to and detaches it.
1070 The program resumes execution and will no longer stop
1071 on signals, etc. We'd better not have left any breakpoints
1072 in the program or it'll die when it hits one. For this
1073 to work, it may be necessary for the process to have been
1074 previously attached. It *might* work if the program was
1075 started via the normal ptrace (PTRACE_TRACEME). */
1078 linuxthreads_detach (args, from_tty)
1082 if (linuxthreads_max)
1087 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1089 /* Walk through linuxthreads array in order to detach known threads. */
1090 if (linuxthreads_manager_pid != 0)
1092 /* Get rid of all positive zombie breakpoints. */
1093 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1095 if (linuxthreads_breakpoint_zombie[i].step)
1098 pid = linuxthreads_breakpoint_zombie[i].pid;
1099 if (!linuxthreads_thread_alive (pid))
1102 if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (pid))
1105 /* Continue in STEP mode until the thread pc has moved or
1106 until SIGTRAP is found on the same PC. */
1107 if (linuxthreads_find_trap (pid, 0)
1108 && linuxthreads_breakpoint_zombie[i].pc == read_pc_pid (pid))
1109 write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
1110 - DECR_PC_AFTER_BREAK, pid);
1113 /* Detach thread after thread. */
1114 inferior_pid = linuxthreads_manager_pid;
1115 iterate_active_threads (detach_thread, 1);
1117 /* Remove pending SIGTRAP and SIGSTOP */
1118 linuxthreads_find_trap (inferior_pid, 1);
1120 linuxthreads_wait_last = -1;
1121 linuxthreads_exit_status = __W_STOPCODE(0);
1124 linuxthreads_inferior_pid = 0;
1125 linuxthreads_breakpoint_pid = 0;
1126 linuxthreads_step_pid = 0;
1127 linuxthreads_step_signo = TARGET_SIGNAL_0;
1128 linuxthreads_manager_pid = 0;
1129 linuxthreads_initial_pid = 0;
1130 linuxthreads_attach_pending = 0;
1131 init_thread_list (); /* Destroy thread info */
1134 child_ops.to_detach (args, from_tty);
1136 unpush_target (&linuxthreads_ops);
1139 /* Resume execution of process PID. If STEP is nozero, then
1140 just single step it. If SIGNAL is nonzero, restart it with that
1141 signal activated. */
1144 linuxthreads_resume (pid, step, signo)
1147 enum target_signal signo;
1149 if (!linuxthreads_max || stop_soon_quietly || linuxthreads_manager_pid == 0)
1150 child_ops.to_resume (pid, step, signo);
1154 if (linuxthreads_inferior_pid)
1156 /* Prepare resume of the last thread that hit a breakpoint */
1157 linuxthreads_breakpoints_inserted = 0;
1158 rpid = linuxthreads_inferior_pid;
1159 linuxthreads_step_signo = signo;
1163 struct cleanup *old_chain = NULL;
1168 linuxthreads_step_pid = step ? inferior_pid : 0;
1169 linuxthreads_step_signo = signo;
1170 rpid = inferior_pid;
1175 if (pid < 0 || !step)
1177 linuxthreads_breakpoints_inserted = 1;
1179 /* Walk through linuxthreads array in order to resume threads */
1180 if (pid >= 0 && inferior_pid != pid)
1182 old_chain = save_inferior_pid ();
1186 iterate_active_threads (resume_thread, 0);
1187 if (linuxthreads_manager_pid != inferior_pid
1188 && !linuxthreads_pending_status (linuxthreads_manager_pid))
1189 resume_thread (linuxthreads_manager_pid);
1192 linuxthreads_breakpoints_inserted = 0;
1194 /* Deal with zombie breakpoint */
1195 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1196 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1198 if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
1200 /* The current pc is out of zombie breakpoint. */
1201 REMOVE_BREAKPOINT_ZOMBIE(i);
1206 if (old_chain != NULL)
1207 do_cleanups (old_chain);
1210 /* Resume initial thread. */
1211 if (!linuxthreads_pending_status (rpid))
1212 child_ops.to_resume (rpid, step, signo);
1216 /* Wait for any threads to stop. We may have to convert PID from a thread id
1217 to a LWP id, and vice versa on the way out. */
1220 linuxthreads_wait (pid, ourstatus)
1222 struct target_waitstatus *ourstatus;
1230 if (linuxthreads_max && !linuxthreads_breakpoints_inserted)
1231 wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
1233 /* See if the inferior has chosen values for its signals yet. By
1234 checking for them here, we can be sure we've updated GDB's signal
1235 handling table before the inferior ever gets one of them. (Well,
1236 before we notice, anyway.) */
1237 check_all_signal_numbers ();
1241 if (!linuxthreads_max)
1243 else if (!linuxthreads_breakpoints_inserted)
1245 if (linuxthreads_inferior_pid)
1246 pid = linuxthreads_inferior_pid;
1251 else if (pid < 0 && linuxthreads_wait_last >= 0)
1253 status = linuxthreads_wait_status[linuxthreads_wait_last];
1254 rpid = linuxthreads_wait_pid[linuxthreads_wait_last--];
1256 else if (pid > 0 && linuxthreads_pending_status (pid))
1258 for (i = linuxthreads_wait_last; i >= 0; i--)
1259 if (linuxthreads_wait_pid[i] == pid)
1265 status = linuxthreads_wait_status[i];
1267 if (i < linuxthreads_wait_last)
1269 linuxthreads_wait_status[i] =
1270 linuxthreads_wait_status[linuxthreads_wait_last];
1271 linuxthreads_wait_pid[i] =
1272 linuxthreads_wait_pid[linuxthreads_wait_last];
1274 linuxthreads_wait_last--;
1285 set_sigint_trap(); /* Causes SIGINT to be passed on to the
1286 attached process. */
1289 sigprocmask(SIG_BLOCK, &linuxthreads_wait_mask, &omask);
1292 rpid = waitpid (pid, &status, __WCLONE | WNOHANG);
1297 else if (errno != EINTR)
1302 rpid = waitpid (pid, &status, WNOHANG);
1309 else if (save_errno != 0)
1315 sigprocmask(SIG_SETMASK, &omask, NULL);
1318 clear_sigio_trap ();
1320 clear_sigint_trap();
1324 if (WIFEXITED(linuxthreads_exit_status))
1326 store_waitstatus (ourstatus, linuxthreads_exit_status);
1327 return inferior_pid;
1332 (gdb_stderr, "Child process unexpectedly missing: %s.\n",
1333 safe_strerror (save_errno));
1334 /* Claim it exited with unknown signal. */
1335 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1336 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1341 /* Signals arrive in any order. So get all signals until SIGTRAP
1342 and resend previous ones to be held after. */
1343 if (linuxthreads_max
1344 && !linuxthreads_breakpoints_inserted
1345 && WIFSTOPPED(status))
1346 if (WSTOPSIG(status) == SIGTRAP)
1349 kill (rpid, WSTOPSIG(wstatus[last]));
1351 /* insert negative zombie breakpoint */
1352 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1353 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1355 if (i > linuxthreads_breakpoint_last)
1357 linuxthreads_breakpoint_zombie[i].pid = rpid;
1358 linuxthreads_breakpoint_last++;
1360 linuxthreads_breakpoint_zombie[i].pc = read_pc_pid (rpid);
1361 linuxthreads_breakpoint_zombie[i].step = 1;
1365 if (WSTOPSIG(status) != SIGSTOP)
1367 for (i = 0; i < last; i++)
1368 if (wstatus[i] == status)
1371 wstatus[last++] = status;
1373 child_resume (rpid, 1, TARGET_SIGNAL_0);
1376 if (linuxthreads_inferior_pid)
1377 linuxthreads_inferior_pid = 0;
1380 if (linuxthreads_max && !stop_soon_quietly)
1382 if (linuxthreads_max
1383 && WIFSTOPPED(status)
1384 && WSTOPSIG(status) == SIGSTOP)
1386 /* Skip SIGSTOP signals. */
1387 if (!linuxthreads_pending_status (rpid))
1389 if (linuxthreads_step_pid == rpid)
1390 child_resume (rpid, 1, linuxthreads_step_signo);
1392 child_resume (rpid, 0, TARGET_SIGNAL_0);
1397 /* Do no report exit status of cloned threads. */
1398 if (WIFEXITED(status))
1400 if (rpid == linuxthreads_initial_pid)
1401 linuxthreads_exit_status = status;
1403 /* Remove any zombie breakpoint. */
1404 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1405 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1407 REMOVE_BREAKPOINT_ZOMBIE(i);
1415 /* Deal with zombie breakpoint */
1416 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1417 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1420 if (i <= linuxthreads_breakpoint_last)
1422 /* There is a potential zombie breakpoint */
1423 if (WIFEXITED(status)
1424 || linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
1426 /* The current pc is out of zombie breakpoint. */
1427 REMOVE_BREAKPOINT_ZOMBIE(i);
1429 else if (!linuxthreads_breakpoint_zombie[i].step
1430 && WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
1432 /* This is a real one ==> decrement PC and restart. */
1433 write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
1434 - DECR_PC_AFTER_BREAK, rpid);
1435 if (linuxthreads_step_pid == rpid)
1436 child_resume (rpid, 1, linuxthreads_step_signo);
1438 child_resume (rpid, 0, TARGET_SIGNAL_0);
1443 /* Walk through linuxthreads array in order to stop them */
1444 if (linuxthreads_breakpoints_inserted)
1445 update_stop_threads (rpid);
1448 else if (rpid != inferior_pid)
1451 store_waitstatus (ourstatus, status);
1453 if (linuxthreads_attach_pending && !stop_soon_quietly)
1456 target_write_memory (linuxthreads_debug, (char *)&on, sizeof (on));
1457 update_stop_threads (rpid);
1458 linuxthreads_attach_pending = 0;
1461 if (linuxthreads_breakpoints_inserted
1462 && WIFSTOPPED(status)
1463 && WSTOPSIG(status) == SIGTRAP)
1464 linuxthreads_breakpoint_pid = rpid;
1465 else if (linuxthreads_breakpoint_pid)
1466 linuxthreads_breakpoint_pid = 0;
1472 /* Fork an inferior process, and start debugging it with ptrace. */
1475 linuxthreads_create_inferior (exec_file, allargs, env)
1480 if (!exec_file && !exec_bfd)
1482 error ("No executable file specified.\n\
1483 Use the \"file\" or \"exec-file\" command.");
1487 push_target (&linuxthreads_ops);
1488 linuxthreads_breakpoints_inserted = 1;
1489 linuxthreads_breakpoint_last = -1;
1490 linuxthreads_wait_last = -1;
1491 linuxthreads_exit_status = __W_STOPCODE(0);
1493 if (linuxthreads_max)
1494 linuxthreads_attach_pending = 1;
1496 child_ops.to_create_inferior (exec_file, allargs, env);
1499 /* Clean up after the inferior dies. */
1502 linuxthreads_mourn_inferior ()
1504 if (linuxthreads_max)
1507 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1509 linuxthreads_inferior_pid = 0;
1510 linuxthreads_breakpoint_pid = 0;
1511 linuxthreads_step_pid = 0;
1512 linuxthreads_step_signo = TARGET_SIGNAL_0;
1513 linuxthreads_manager_pid = 0;
1514 linuxthreads_initial_pid = 0;
1515 linuxthreads_attach_pending = 0;
1516 init_thread_list(); /* Destroy thread info */
1519 child_ops.to_mourn_inferior ();
1521 unpush_target (&linuxthreads_ops);
1524 /* Kill the inferior process */
1527 linuxthreads_kill ()
1532 if (inferior_pid == 0)
1535 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1537 /* Remove all threads status. */
1538 inferior_pid = linuxthreads_manager_pid;
1539 iterate_active_threads (kill_thread, 1);
1542 kill_thread (inferior_pid);
1545 /* doing_quit_force solves a real problem, but I think a properly
1546 placed call to catch_errors would do the trick much more cleanly. */
1547 if (doing_quit_force >= 0)
1549 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1551 /* Wait for thread to complete */
1552 while ((rpid = waitpid (-1, &status, __WCLONE)) > 0)
1553 if (!WIFEXITED(status))
1556 while ((rpid = waitpid (-1, &status, 0)) > 0)
1557 if (!WIFEXITED(status))
1561 while ((rpid = waitpid (inferior_pid, &status, 0)) > 0)
1562 if (!WIFEXITED(status))
1563 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
1567 /* Wait for all threads. */
1569 rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
1570 while (rpid > 0 || errno == EINTR);
1573 rpid = waitpid (-1, &status, WNOHANG);
1574 while (rpid > 0 || errno == EINTR);
1576 linuxthreads_mourn_inferior ();
1579 /* Insert a breakpoint */
1582 linuxthreads_insert_breakpoint (addr, contents_cache)
1584 char *contents_cache;
1586 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1588 linuxthreads_breakpoint_addr = addr;
1589 iterate_active_threads (insert_breakpoint, 1);
1590 insert_breakpoint (linuxthreads_manager_pid);
1593 return child_ops.to_insert_breakpoint (addr, contents_cache);
1596 /* Remove a breakpoint */
1599 linuxthreads_remove_breakpoint (addr, contents_cache)
1601 char *contents_cache;
1603 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1605 linuxthreads_breakpoint_addr = addr;
1606 iterate_active_threads (remove_breakpoint, 1);
1607 remove_breakpoint (linuxthreads_manager_pid);
1610 return child_ops.to_remove_breakpoint (addr, contents_cache);
1613 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1616 linuxthreads_can_run ()
1618 return child_suppress_run;
1622 init_linuxthreads_ops ()
1624 linuxthreads_ops.to_shortname = "linuxthreads";
1625 linuxthreads_ops.to_longname = "LINUX threads and pthread.";
1626 linuxthreads_ops.to_doc = "LINUX threads and pthread support.";
1627 linuxthreads_ops.to_attach = linuxthreads_attach;
1628 linuxthreads_ops.to_detach = linuxthreads_detach;
1629 linuxthreads_ops.to_resume = linuxthreads_resume;
1630 linuxthreads_ops.to_wait = linuxthreads_wait;
1631 linuxthreads_ops.to_kill = linuxthreads_kill;
1632 linuxthreads_ops.to_can_run = linuxthreads_can_run;
1633 linuxthreads_ops.to_stratum = thread_stratum;
1634 linuxthreads_ops.to_insert_breakpoint = linuxthreads_insert_breakpoint;
1635 linuxthreads_ops.to_remove_breakpoint = linuxthreads_remove_breakpoint;
1636 linuxthreads_ops.to_create_inferior = linuxthreads_create_inferior;
1637 linuxthreads_ops.to_mourn_inferior = linuxthreads_mourn_inferior;
1638 linuxthreads_ops.to_thread_alive = linuxthreads_thread_alive;
1639 linuxthreads_ops.to_magic = OPS_MAGIC;
1643 _initialize_linuxthreads ()
1645 struct sigaction sact;
1647 init_linuxthreads_ops ();
1648 add_target (&linuxthreads_ops);
1649 child_suppress_run = 1;
1651 /* Hook onto the "new_objfile" event.
1652 * If someone else is already hooked onto the event,
1653 * then make sure he will be called after we are.
1655 target_new_objfile_chain = target_new_objfile_hook;
1656 target_new_objfile_hook = linuxthreads_new_objfile;
1658 /* Attach SIGCHLD handler */
1659 sact.sa_handler = sigchld_handler;
1660 sigemptyset (&sact.sa_mask);
1662 sigaction (SIGCHLD, &sact, NULL);
1664 /* initialize SIGCHLD mask */
1665 sigemptyset (&linuxthreads_wait_mask);
1666 sigaddset (&linuxthreads_wait_mask, SIGCHLD);