* sol-thread.c (GET_LWP, GET_THREAD, BUILD_LWP, BUILD_THREAD):
[deliverable/binutils-gdb.git] / gdb / linux-thread.c
CommitLineData
d4f3574e
SS
1/* Low level interface for debugging GNU/Linux threads for GDB,
2 the GNU debugger.
3 Copyright 1998, 1999 Free Software Foundation, Inc.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
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).
26
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.
31
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.
38
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. */
49
203051af 50#include "defs.h"
d4f3574e
SS
51#include <sys/types.h> /* for pid_t */
52#include <sys/ptrace.h> /* for PT_* flags */
03f2053f 53#include "gdb_wait.h" /* for WUNTRACED and __WCLONE flags */
d4f3574e
SS
54#include <signal.h> /* for struct sigaction and NSIG */
55#include <sys/utsname.h>
56
d4f3574e
SS
57#include "target.h"
58#include "inferior.h"
59#include "gdbcore.h"
60#include "gdbthread.h"
d4f3574e
SS
61#include "gdbcmd.h"
62#include "breakpoint.h"
63
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
73
74#ifdef NSIG
75#define LINUXTHREAD_NSIG NSIG
76#else
77#ifdef _NSIG
78#define LINUXTHREAD_NSIG _NSIG
79#endif
80#endif
81
82extern int child_suppress_run; /* make inftarg.c non-runnable */
83struct target_ops linuxthreads_ops; /* Forward declaration */
84extern struct target_ops child_ops; /* target vector for inftarg.c */
85
86static CORE_ADDR linuxthreads_handles; /* array of linuxthreads handles */
87static CORE_ADDR linuxthreads_manager; /* pid of linuxthreads manager thread */
88static CORE_ADDR linuxthreads_initial; /* pid of linuxthreads initial thread */
89static CORE_ADDR linuxthreads_debug; /* linuxthreads internal debug flag */
90static CORE_ADDR linuxthreads_num; /* number of valid handle entries */
91
92static 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. */
97
98static int linuxthreads_sizeof_handle; /* size of a linuxthreads handle */
99static int linuxthreads_offset_descr; /* h_descr offset of the linuxthreads
100 handle */
101static int linuxthreads_offset_pid; /* p_pid offset of the linuxthreads
102 descr */
103
104static int linuxthreads_manager_pid; /* manager pid */
105static int linuxthreads_initial_pid; /* initial pid */
106
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. */
112static int *linuxthreads_wait_pid; /* wait array of pid */
113static int *linuxthreads_wait_status; /* wait array of status */
114static int linuxthreads_wait_last; /* index of last valid elt in
115 linuxthreads_wait_{pid,status} */
116
ed9a39eb 117static sigset_t linuxthreads_block_mask; /* sigset without SIGCHLD */
d4f3574e
SS
118
119static int linuxthreads_step_pid; /* current stepped pid */
120static int linuxthreads_step_signo; /* current stepped target signal */
121static int linuxthreads_exit_status; /* exit status of initial thread */
122
123static int linuxthreads_inferior_pid; /* temporary internal inferior pid */
124static int linuxthreads_breakpoint_pid; /* last pid that hit a breakpoint */
125static int linuxthreads_attach_pending; /* attach command without wait */
126
127static int linuxthreads_breakpoints_inserted; /* any breakpoints inserted */
128
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. */
135
136struct linuxthreads_signal {
137
138 /* The name of the LinuxThreads library variable that contains
139 the signal number. */
140 char *var;
141
142 /* True if this variable must exist for us to debug properly. */
143 int required;
144
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;
148
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;
154
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;
160};
161
162struct linuxthreads_signal linuxthreads_sig_restart = {
5c44784c 163 "__pthread_sig_restart", 1, 0, 0, 0, 0
d4f3574e
SS
164};
165struct linuxthreads_signal linuxthreads_sig_cancel = {
5c44784c 166 "__pthread_sig_cancel", 1, 0, 0, 0, 0
d4f3574e
SS
167};
168struct linuxthreads_signal linuxthreads_sig_debug = {
5c44784c 169 "__pthread_sig_debug", 0, 0, 0, 0, 0
d4f3574e
SS
170};
171
ed9a39eb
JM
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. */
176
177int using_thread_db = 0;
178
d4f3574e
SS
179/* A table of breakpoint locations, one per PID. */
180static 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 */
185static int linuxthreads_breakpoint_last; /* Last zombie breakpoint */
186
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. */
191static CORE_ADDR linuxthreads_breakpoint_addr;
192
193#define REMOVE_BREAKPOINT_ZOMBIE(_i) \
194{ \
195 if ((_i) < linuxthreads_breakpoint_last) \
196 linuxthreads_breakpoint_zombie[(_i)] = \
197 linuxthreads_breakpoint_zombie[linuxthreads_breakpoint_last]; \
198 linuxthreads_breakpoint_last--; \
199}
200
201
202\f
203#ifndef PTRACE_XFER_TYPE
204#define PTRACE_XFER_TYPE int
205#endif
206/* Check to see if the given thread is alive. */
207static int
208linuxthreads_thread_alive (pid)
209 int pid;
210{
211 errno = 0;
212 return ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE)0, 0) >= 0 || errno == 0;
213}
214
215/* On detach(), find a SIGTRAP status. If stop is non-zero, find a
216 SIGSTOP one, too.
217
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.
223
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.
228
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. */
232static int
233linuxthreads_find_trap (pid, stop)
234 int pid;
235 int stop;
236{
237 int i;
238 int rpid;
239 int status;
240 int found_stop = 0;
241 int found_trap = 0;
242
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.
246
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;
253
254 /* Look at the pending status */
255 for (i = linuxthreads_wait_last; i >= 0; i--)
256 if (linuxthreads_wait_pid[i] == pid)
257 {
258 status = linuxthreads_wait_status[i];
259
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)
265 {
266 linuxthreads_wait_status[i] =
267 linuxthreads_wait_status[linuxthreads_wait_last];
268 linuxthreads_wait_pid[i] =
269 linuxthreads_wait_pid[linuxthreads_wait_last];
270 }
271 linuxthreads_wait_last--;
272
273 if (!WIFSTOPPED(status)) /* Thread has died */
274 return 0;
275
276 if (WSTOPSIG(status) == SIGTRAP)
277 {
278 if (stop)
279 found_trap = 1;
280 else
281 return 1;
282 }
283 else if (WSTOPSIG(status) == SIGSTOP)
284 {
285 if (stop)
286 found_stop = 1;
287 }
288 else
289 {
290 wstatus[0] = status;
291 last = 1;
292 }
293
294 break;
295 }
296
297 if (stop)
298 {
299 /* Make sure that we'll find what we're looking for. */
300 if (!found_trap)
ed9a39eb
JM
301 {
302 kill (pid, SIGTRAP);
303 }
d4f3574e 304 if (!found_stop)
ed9a39eb
JM
305 {
306 kill (pid, SIGSTOP);
307 }
d4f3574e
SS
308 }
309
310 /* Catch all status until SIGTRAP and optionally SIGSTOP show up. */
311 for (;;)
312 {
ed9a39eb 313 /* resume the child every time... */
d4f3574e
SS
314 child_resume (pid, 1, TARGET_SIGNAL_0);
315
ed9a39eb
JM
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. */
320
d4f3574e
SS
321 for (;;)
322 {
323 rpid = waitpid (pid, &status, __WCLONE);
324 if (rpid > 0)
ed9a39eb
JM
325 {
326 break;
327 }
d4f3574e 328 if (errno == EINTR)
ed9a39eb
JM
329 {
330 continue;
331 }
d4f3574e
SS
332
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)
ed9a39eb
JM
343 {
344 break;
345 }
d4f3574e 346 if (errno != EINTR)
ed9a39eb 347 perror_with_name ("find_trap/waitpid");
d4f3574e
SS
348 }
349
350 if (!WIFSTOPPED(status)) /* Thread has died */
351 return 0;
352
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)
5c44784c
JM
361 {
362 if (found_trap)
363 break;
364 else
365 found_stop = 1;
366 }
d4f3574e
SS
367 }
368
369 /* Resend any other signals we noticed to the thread, to be received
370 when we continue it. */
371 while (--last >= 0)
ed9a39eb
JM
372 {
373 kill (pid, WSTOPSIG(wstatus[last]));
374 }
d4f3574e
SS
375
376 return 1;
377}
378
379/* Cleanup stub for save_inferior_pid. */
380static void
a91f7ea9 381restore_inferior_pid (void *arg)
d4f3574e 382{
a91f7ea9
KB
383 int *saved_pid_ptr = arg;
384 inferior_pid = *saved_pid_ptr;
385 free (arg);
d4f3574e
SS
386}
387
388/* Register a cleanup to restore the value of inferior_pid. */
389static struct cleanup *
a91f7ea9 390save_inferior_pid (void)
d4f3574e 391{
a91f7ea9
KB
392 int *saved_pid_ptr;
393
394 saved_pid_ptr = xmalloc (sizeof (int));
395 *saved_pid_ptr = inferior_pid;
396 return make_cleanup (restore_inferior_pid, saved_pid_ptr);
d4f3574e
SS
397}
398
399static void
400sigchld_handler (signo)
401 int signo;
402{
403 /* This handler is used to get an EINTR while doing waitpid()
404 when an event is received */
405}
406
407/* Have we already collected a wait status for PID in the
408 linuxthreads_wait bag? */
409static int
410linuxthreads_pending_status (pid)
411 int pid;
412{
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;
418}
419
420\f
421/* Internal linuxthreads signal management */
422
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. */
427static int
428find_signal_var (sig, objfile)
429 struct linuxthreads_signal *sig;
430 struct objfile *objfile;
431{
432 struct minimal_symbol *ms = lookup_minimal_symbol (sig->var, NULL, objfile);
433
434 if (! ms)
435 {
436 if (sig->required)
437 {
438 fprintf_unfiltered (gdb_stderr,
439 "Unable to find linuxthreads symbol \"%s\"\n",
440 sig->var);
441 return 0;
442 }
443 else
444 {
445 sig->addr = 0;
446 return 1;
447 }
448 }
449
450 sig->addr = SYMBOL_VALUE_ADDRESS (ms);
451
452 return 1;
453}
454
455static int
456find_all_signal_vars (objfile)
457 struct objfile *objfile;
458{
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));
462}
463
464/* A struct complaint isn't appropriate here. */
465static int complained_cannot_determine_thread_signal_number = 0;
466
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. */
470static void
471check_signal_number (sig)
472 struct linuxthreads_signal *sig;
473{
474 int num;
475
476 if (sig->signal)
477 /* We already know this signal number. */
478 return;
479
480 if (! sig->addr)
481 /* We don't know the variable's address yet. */
482 return;
483
484 if (target_read_memory (sig->addr, (char *)&num, sizeof (num))
485 != 0)
486 {
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;
494 }
495
496 if (num == 0)
497 /* It hasn't been initialized yet. */
498 return;
499
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);
505}
506
ed9a39eb 507void
d4f3574e
SS
508check_all_signal_numbers ()
509{
510 /* If this isn't a LinuxThreads program, quit early. */
511 if (! linuxthreads_max)
512 return;
513
514 check_signal_number (&linuxthreads_sig_restart);
515 check_signal_number (&linuxthreads_sig_cancel);
516 check_signal_number (&linuxthreads_sig_debug);
517
518 /* handle linuxthread exit */
519 if (linuxthreads_sig_debug.signal
520 || linuxthreads_sig_restart.signal)
521 {
522 struct sigaction sact;
523
524 sact.sa_handler = sigchld_handler;
525 sigemptyset(&sact.sa_mask);
526 sact.sa_flags = 0;
ed9a39eb 527
d4f3574e
SS
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);
532 }
533}
534
535
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. */
540static void
541restore_signal (sig)
542 struct linuxthreads_signal *sig;
543{
544 if (! sig->signal)
545 return;
546
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);
551
552 sig->signal = 0;
553 sig->addr = 0;
554}
555
556
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. */
561static void
562restore_all_signals ()
563{
564 restore_signal (&linuxthreads_sig_restart);
565 restore_signal (&linuxthreads_sig_cancel);
566 restore_signal (&linuxthreads_sig_debug);
567
568 /* If it happens again, we should complain again. */
569 complained_cannot_determine_thread_signal_number = 0;
570}
571
572
573\f
574
575/* Apply FUNC to the pid of each active thread. This consults the
576 inferior's handle table to find active threads.
577
578 If ALL is non-zero, process all threads.
579 If ALL is zero, skip threads with pending status. */
580static void
581iterate_active_threads (func, all)
582 void (*func)(int);
583 int all;
584{
585 CORE_ADDR descr;
586 int pid;
587 int i;
588 int num;
589
590 read_memory (linuxthreads_num, (char *)&num, sizeof (int));
591
592 for (i = 0; i < linuxthreads_max && num > 0; i++)
593 {
594 read_memory (linuxthreads_handles +
595 linuxthreads_sizeof_handle * i + linuxthreads_offset_descr,
596 (char *)&descr, sizeof (void *));
597 if (descr)
598 {
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);
605 }
606 }
d4f3574e
SS
607}
608
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. */
612static void
613insert_breakpoint (pid)
614 int pid;
615{
616 int j;
617
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)
621 {
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;
627 }
628}
629
630/* Note that we're about to remove a thread breakpoint at
631 linuxthreads_breakpoint_addr.
632
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. */
639static void
640remove_breakpoint (pid)
641 int pid;
642{
643 int j;
644
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;
649
650 if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
651 {
652 CORE_ADDR pc = read_pc_pid (pid);
653 if (linuxthreads_breakpoint_addr == pc - DECR_PC_AFTER_BREAK
654 && j > linuxthreads_breakpoint_last)
655 {
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++;
660 }
661 }
662}
663
664/* Kill a thread */
665static void
666kill_thread (pid)
667 int pid;
668{
669 if (in_thread_list (pid))
ed9a39eb
JM
670 {
671 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
672 }
d4f3574e 673 else
ed9a39eb
JM
674 {
675 kill (pid, SIGKILL);
676 }
d4f3574e
SS
677}
678
679/* Resume a thread */
680static void
681resume_thread (pid)
682 int pid;
683{
684 if (pid != inferior_pid
685 && in_thread_list (pid)
686 && linuxthreads_thread_alive (pid))
5c44784c
JM
687 {
688 if (pid == linuxthreads_step_pid)
ed9a39eb
JM
689 {
690 child_resume (pid, 1, linuxthreads_step_signo);
691 }
5c44784c 692 else
ed9a39eb
JM
693 {
694 child_resume (pid, 0, TARGET_SIGNAL_0);
695 }
5c44784c 696 }
d4f3574e
SS
697}
698
699/* Detach a thread */
700static void
701detach_thread (pid)
702 int pid;
703{
704 if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
705 {
706 /* Remove pending SIGTRAP and SIGSTOP */
707 linuxthreads_find_trap (pid, 1);
708
709 inferior_pid = pid;
710 detach (TARGET_SIGNAL_0);
711 inferior_pid = linuxthreads_manager_pid;
712 }
713}
714
ed9a39eb
JM
715/* Attach a thread */
716void
717attach_thread (pid)
718 int pid;
719{
720 if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) != 0)
721 perror_with_name ("attach_thread");
722}
723
d4f3574e
SS
724/* Stop a thread */
725static void
726stop_thread (pid)
727 int pid;
728{
729 if (pid != inferior_pid)
5c44784c
JM
730 {
731 if (in_thread_list (pid))
ed9a39eb
JM
732 {
733 kill (pid, SIGSTOP);
734 }
5c44784c
JM
735 else if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) == 0)
736 {
737 if (!linuxthreads_attach_pending)
ed9a39eb 738 printf_filtered ("[New %s]\n", target_pid_to_str (pid));
5c44784c
JM
739 add_thread (pid);
740 if (linuxthreads_sig_debug.signal)
ed9a39eb
JM
741 {
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);
746 }
5c44784c
JM
747 }
748 else
749 perror_with_name ("ptrace in stop_thread");
750 }
d4f3574e
SS
751}
752
753/* Wait for a thread */
754static void
755wait_thread (pid)
756 int pid;
757{
758 int status;
759 int rpid;
760
761 if (pid != inferior_pid && in_thread_list (pid))
762 {
ed9a39eb
JM
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. */
d4f3574e
SS
767 for (;;)
768 {
769 /* Get first pid status. */
770 rpid = waitpid(pid, &status, __WCLONE);
771 if (rpid > 0)
ed9a39eb
JM
772 {
773 break;
774 }
d4f3574e 775 if (errno == EINTR)
ed9a39eb
JM
776 {
777 continue;
778 }
d4f3574e
SS
779
780 /* There are two reasons this might have failed:
781
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.
785
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.
790
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)
ed9a39eb
JM
798 {
799 break;
800 }
d4f3574e 801 if (errno != EINTR && linuxthreads_thread_alive (pid))
ed9a39eb 802 perror_with_name ("wait_thread/waitpid");
d4f3574e
SS
803
804 /* the thread is dead. */
805 return;
806 }
807 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
808 {
809 linuxthreads_wait_pid[++linuxthreads_wait_last] = pid;
810 linuxthreads_wait_status[linuxthreads_wait_last] = status;
811 }
812 }
813}
814
815/* Walk through the linuxthreads handles in order to detect all
816 threads and stop them */
817static void
818update_stop_threads (test_pid)
819 int test_pid;
820{
821 struct cleanup *old_chain = NULL;
822
823 check_all_signal_numbers ();
824
825 if (linuxthreads_manager_pid == 0)
826 {
827 if (linuxthreads_manager)
828 {
829 if (test_pid > 0 && test_pid != inferior_pid)
830 {
831 old_chain = save_inferior_pid ();
832 inferior_pid = test_pid;
833 }
834 read_memory (linuxthreads_manager,
835 (char *)&linuxthreads_manager_pid, sizeof (pid_t));
836 }
837 if (linuxthreads_initial)
838 {
839 if (test_pid > 0 && test_pid != inferior_pid)
840 {
841 old_chain = save_inferior_pid ();
842 inferior_pid = test_pid;
843 }
844 read_memory(linuxthreads_initial,
845 (char *)&linuxthreads_initial_pid, sizeof (pid_t));
846 }
847 }
848
849 if (linuxthreads_manager_pid != 0)
850 {
851 if (old_chain == NULL && test_pid > 0 &&
852 test_pid != inferior_pid && linuxthreads_thread_alive (test_pid))
853 {
854 old_chain = save_inferior_pid ();
855 inferior_pid = test_pid;
856 }
857
858 if (linuxthreads_thread_alive (inferior_pid))
859 {
860 if (test_pid > 0)
861 {
862 if (test_pid != linuxthreads_manager_pid
863 && !linuxthreads_pending_status (linuxthreads_manager_pid))
864 {
865 stop_thread (linuxthreads_manager_pid);
866 wait_thread (linuxthreads_manager_pid);
867 }
868 if (!in_thread_list (test_pid))
869 {
870 if (!linuxthreads_attach_pending)
ed9a39eb
JM
871 printf_filtered ("[New %s]\n",
872 target_pid_to_str (test_pid));
d4f3574e
SS
873 add_thread (test_pid);
874 if (linuxthreads_sig_debug.signal
875 && inferior_pid == test_pid)
ed9a39eb
JM
876 {
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);
881 }
d4f3574e
SS
882 }
883 }
884 iterate_active_threads (stop_thread, 0);
885 iterate_active_threads (wait_thread, 0);
886 }
887 }
888
889 if (old_chain != NULL)
890 do_cleanups (old_chain);
891}
892
11cf8741
JM
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. */
899
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.
902 */
903
904/* Saved pointer to previous owner of the new_objfile event. */
905static void (*target_new_objfile_chain) PARAMS ((struct objfile *));
d4f3574e
SS
906
907void
908linuxthreads_new_objfile (objfile)
909 struct objfile *objfile;
910{
911 struct minimal_symbol *ms;
912
ed9a39eb
JM
913 /* Call predecessor on chain, if any.
914 Calling the new module first allows it to dominate,
915 if it finds its compatible libraries. */
916
917 if (target_new_objfile_chain)
918 target_new_objfile_chain (objfile);
919
d4f3574e
SS
920 if (!objfile)
921 {
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 ();
926
927 /* Indicate that we don't know anything's address any more. */
928 linuxthreads_max = 0;
929
11cf8741 930 goto quit;
d4f3574e
SS
931 }
932
933 /* If we've already found our variables in another objfile, don't
934 bother looking for them again. */
935 if (linuxthreads_max)
11cf8741 936 goto quit;
d4f3574e
SS
937
938 if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL, objfile))
939 /* This object file isn't the pthreads library. */
11cf8741 940 goto quit;
d4f3574e
SS
941
942 if ((ms = lookup_minimal_symbol ("__pthread_threads_debug",
943 NULL, objfile)) == NULL)
944 {
945 /* The debugging-aware libpthreads is not present in this objfile */
946 warning ("\
947This program seems to use POSIX threads, but the thread library used\n\
948does not support debugging. This may make using GDB difficult. Don't\n\
949set breakpoints or single-step through code that might be executed by\n\
950any thread other than the main thread.");
11cf8741 951 goto quit;
d4f3574e
SS
952 }
953 linuxthreads_debug = SYMBOL_VALUE_ADDRESS (ms);
954
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)
961 {
962 fprintf_unfiltered (gdb_stderr,
963 "Unable to find linuxthreads symbol \"%s\"\n",
964 "__pthread_sizeof_handle");
11cf8741 965 goto quit;
d4f3574e
SS
966 }
967
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)
973 {
974 fprintf_unfiltered (gdb_stderr,
975 "Unable to find linuxthreads symbol \"%s\"\n",
976 "__pthread_offsetof_descr");
11cf8741 977 goto quit;
d4f3574e
SS
978 }
979
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)
985 {
986 fprintf_unfiltered (gdb_stderr,
987 "Unable to find linuxthreads symbol \"%s\"\n",
988 "__pthread_offsetof_pid");
11cf8741 989 goto quit;
d4f3574e
SS
990 }
991
992 if (! find_all_signal_vars (objfile))
11cf8741 993 goto quit;
d4f3574e
SS
994
995 /* Read adresses of internal structures to access */
996 if ((ms = lookup_minimal_symbol ("__pthread_handles",
997 NULL, objfile)) == NULL)
998 {
999 fprintf_unfiltered (gdb_stderr,
1000 "Unable to find linuxthreads symbol \"%s\"\n",
1001 "__pthread_handles");
11cf8741 1002 goto quit;
d4f3574e
SS
1003 }
1004 linuxthreads_handles = SYMBOL_VALUE_ADDRESS (ms);
1005
1006 if ((ms = lookup_minimal_symbol ("__pthread_handles_num",
1007 NULL, objfile)) == NULL)
1008 {
1009 fprintf_unfiltered (gdb_stderr,
1010 "Unable to find linuxthreads symbol \"%s\"\n",
1011 "__pthread_handles_num");
11cf8741 1012 goto quit;
d4f3574e
SS
1013 }
1014 linuxthreads_num = SYMBOL_VALUE_ADDRESS (ms);
1015
1016 if ((ms = lookup_minimal_symbol ("__pthread_manager_thread",
1017 NULL, objfile)) == NULL)
1018 {
1019 fprintf_unfiltered (gdb_stderr,
1020 "Unable to find linuxthreads symbol \"%s\"\n",
1021 "__pthread_manager_thread");
11cf8741 1022 goto quit;
d4f3574e
SS
1023 }
1024 linuxthreads_manager = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
1025
1026 if ((ms = lookup_minimal_symbol ("__pthread_initial_thread",
1027 NULL, objfile)) == NULL)
1028 {
1029 fprintf_unfiltered (gdb_stderr,
1030 "Unable to find linuxthreads symbol \"%s\"\n",
1031 "__pthread_initial_thread");
11cf8741 1032 goto quit;
d4f3574e
SS
1033 }
1034 linuxthreads_initial = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
1035
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)
1043 {
1044 fprintf_unfiltered (gdb_stderr,
1045 "Unable to find linuxthreads symbol \"%s\"\n",
1046 "__pthread_threads_max");
11cf8741 1047 goto quit;
d4f3574e
SS
1048 }
1049
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));
1057
ed9a39eb
JM
1058 if (inferior_pid &&
1059 !linuxthreads_attach_pending &&
1060 !using_thread_db) /* suppressed by thread_db module */
d4f3574e
SS
1061 {
1062 int on = 1;
ed9a39eb 1063
d4f3574e
SS
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;
1068 }
11cf8741 1069
ed9a39eb
JM
1070 check_all_signal_numbers ();
1071
11cf8741 1072quit:
d4f3574e
SS
1073}
1074
1075/* If we have switched threads from a one that stopped at breakpoint,
1076 return 1 otherwise 0. */
1077
1078int
1079linuxthreads_prepare_to_proceed (step)
1080 int step;
1081{
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;
1087
1088 if (step)
1089 {
1090 /* Mark the current inferior as single stepping process. */
1091 linuxthreads_step_pid = inferior_pid;
1092 }
1093
1094 linuxthreads_inferior_pid = linuxthreads_breakpoint_pid;
1095 return linuxthreads_breakpoint_pid;
1096}
1097
1098/* Convert a pid to printable form. */
1099
1100char *
1101linuxthreads_pid_to_str (pid)
1102 int pid;
1103{
1104 static char buf[100];
1105
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 : "");
1110
1111 return buf;
1112}
1113
1114/* Attach to process PID, then initialize for debugging it
1115 and wait for the trace-trap that results from attaching. */
1116
1117static void
1118linuxthreads_attach (args, from_tty)
1119 char *args;
1120 int from_tty;
1121{
1122 if (!args)
1123 error_no_arg ("process-id to attach");
1124
1125 push_target (&linuxthreads_ops);
1126 linuxthreads_breakpoints_inserted = 1;
1127 linuxthreads_breakpoint_last = -1;
1128 linuxthreads_wait_last = -1;
67aaefa2 1129 WSETSTOP (linuxthreads_exit_status, 0);
d4f3574e
SS
1130
1131 child_ops.to_attach (args, from_tty);
1132
1133 if (linuxthreads_max)
1134 linuxthreads_attach_pending = 1;
1135}
1136
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). */
1144
1145static void
1146linuxthreads_detach (args, from_tty)
1147 char *args;
1148 int from_tty;
1149{
1150 if (linuxthreads_max)
1151 {
1152 int i;
1153 int pid;
1154 int off = 0;
1155 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1156
1157 /* Walk through linuxthreads array in order to detach known threads. */
1158 if (linuxthreads_manager_pid != 0)
1159 {
1160 /* Get rid of all positive zombie breakpoints. */
1161 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1162 {
1163 if (linuxthreads_breakpoint_zombie[i].step)
1164 continue;
1165
1166 pid = linuxthreads_breakpoint_zombie[i].pid;
1167 if (!linuxthreads_thread_alive (pid))
1168 continue;
1169
1170 if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (pid))
1171 continue;
1172
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);
1179 }
1180
1181 /* Detach thread after thread. */
1182 inferior_pid = linuxthreads_manager_pid;
1183 iterate_active_threads (detach_thread, 1);
1184
1185 /* Remove pending SIGTRAP and SIGSTOP */
1186 linuxthreads_find_trap (inferior_pid, 1);
1187
1188 linuxthreads_wait_last = -1;
67aaefa2 1189 WSETSTOP (linuxthreads_exit_status, 0);
d4f3574e
SS
1190 }
1191
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 */
1200 }
1201
1202 child_ops.to_detach (args, from_tty);
1203
1204 unpush_target (&linuxthreads_ops);
1205}
1206
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. */
1210
1211static void
1212linuxthreads_resume (pid, step, signo)
1213 int pid;
1214 int step;
1215 enum target_signal signo;
1216{
1217 if (!linuxthreads_max || stop_soon_quietly || linuxthreads_manager_pid == 0)
ed9a39eb
JM
1218 {
1219 child_ops.to_resume (pid, step, signo);
1220 }
d4f3574e
SS
1221 else
1222 {
1223 int rpid;
1224 if (linuxthreads_inferior_pid)
1225 {
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;
1230 }
1231 else
1232 {
1233 struct cleanup *old_chain = NULL;
1234 int i;
1235
1236 if (pid < 0)
1237 {
1238 linuxthreads_step_pid = step ? inferior_pid : 0;
1239 linuxthreads_step_signo = signo;
1240 rpid = inferior_pid;
1241 }
1242 else
1243 rpid = pid;
1244
1245 if (pid < 0 || !step)
1246 {
1247 linuxthreads_breakpoints_inserted = 1;
1248
1249 /* Walk through linuxthreads array in order to resume threads */
1250 if (pid >= 0 && inferior_pid != pid)
1251 {
1252 old_chain = save_inferior_pid ();
1253 inferior_pid = pid;
1254 }
1255
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);
1260 }
1261 else
1262 linuxthreads_breakpoints_inserted = 0;
1263
1264 /* Deal with zombie breakpoint */
1265 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1266 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1267 {
1268 if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
1269 {
1270 /* The current pc is out of zombie breakpoint. */
1271 REMOVE_BREAKPOINT_ZOMBIE(i);
1272 }
1273 break;
1274 }
1275
1276 if (old_chain != NULL)
1277 do_cleanups (old_chain);
1278 }
1279
1280 /* Resume initial thread. */
ed9a39eb 1281 /* [unles it has a wait event pending] */
d4f3574e 1282 if (!linuxthreads_pending_status (rpid))
ed9a39eb
JM
1283 {
1284 child_ops.to_resume (rpid, step, signo);
1285 }
d4f3574e
SS
1286 }
1287}
1288
ed9a39eb
JM
1289/* Abstract out the child_wait functionality. */
1290int
1291linux_child_wait (pid, rpid, status)
1292 int pid;
1293 int *rpid;
1294 int *status;
1295{
1296 int save_errno;
1297
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 ();
1302
1303 errno = save_errno = 0;
1304 for (;;)
1305 {
1306 errno = 0;
1307 *rpid = waitpid (pid, status, __WCLONE | WNOHANG);
1308 save_errno = errno;
1309
1310 if (*rpid > 0)
1311 {
1312 /* Got an event -- break out */
1313 break;
1314 }
1315 if (errno == EINTR) /* interrupted by signal, try again */
1316 {
1317 continue;
1318 }
1319
1320 errno = 0;
1321 *rpid = waitpid (pid, status, WNOHANG);
1322 if (*rpid > 0)
1323 {
1324 /* Got an event -- break out */
1325 break;
1326 }
1327 if (errno == EINTR)
1328 {
1329 continue;
1330 }
1331 if (errno != 0 && save_errno != 0)
1332 {
1333 break;
1334 }
1335 sigsuspend(&linuxthreads_block_mask);
1336 }
1337 clear_sigio_trap ();
1338 clear_sigint_trap ();
1339
1340 return errno ? errno : save_errno;
1341}
1342
1343
d4f3574e
SS
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. */
1346
1347static int
1348linuxthreads_wait (pid, ourstatus)
1349 int pid;
1350 struct target_waitstatus *ourstatus;
1351{
1352 int status;
1353 int rpid;
1354 int i;
1355 int last;
1356 int *wstatus;
1357
1358 if (linuxthreads_max && !linuxthreads_breakpoints_inserted)
1359 wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
1360
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 ();
1366
1367 for (;;)
1368 {
1369 if (!linuxthreads_max)
1370 rpid = 0;
1371 else if (!linuxthreads_breakpoints_inserted)
1372 {
1373 if (linuxthreads_inferior_pid)
1374 pid = linuxthreads_inferior_pid;
1375 else if (pid < 0)
1376 pid = inferior_pid;
1377 last = rpid = 0;
1378 }
1379 else if (pid < 0 && linuxthreads_wait_last >= 0)
1380 {
1381 status = linuxthreads_wait_status[linuxthreads_wait_last];
1382 rpid = linuxthreads_wait_pid[linuxthreads_wait_last--];
1383 }
1384 else if (pid > 0 && linuxthreads_pending_status (pid))
1385 {
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
1392 {
1393 status = linuxthreads_wait_status[i];
1394 rpid = pid;
1395 if (i < linuxthreads_wait_last)
1396 {
1397 linuxthreads_wait_status[i] =
1398 linuxthreads_wait_status[linuxthreads_wait_last];
1399 linuxthreads_wait_pid[i] =
1400 linuxthreads_wait_pid[linuxthreads_wait_last];
1401 }
1402 linuxthreads_wait_last--;
1403 }
1404 }
1405 else
1406 rpid = 0;
1407
1408 if (rpid == 0)
1409 {
1410 int save_errno;
d4f3574e 1411
ed9a39eb 1412 save_errno = linux_child_wait (pid, &rpid, &status);
d4f3574e
SS
1413
1414 if (rpid == -1)
1415 {
1416 if (WIFEXITED(linuxthreads_exit_status))
1417 {
1418 store_waitstatus (ourstatus, linuxthreads_exit_status);
1419 return inferior_pid;
1420 }
1421 else
1422 {
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;
1430 }
1431 }
1432
ed9a39eb
JM
1433 /* We have now gotten a new event from waitpid above. */
1434
1435 /* Signals arrive in any order. So get all signals until
1436 SIGTRAP and resend previous ones to be held after. */
d4f3574e
SS
1437 if (linuxthreads_max
1438 && !linuxthreads_breakpoints_inserted
1439 && WIFSTOPPED(status))
1440 if (WSTOPSIG(status) == SIGTRAP)
1441 {
1442 while (--last >= 0)
ed9a39eb
JM
1443 {
1444 kill (rpid, WSTOPSIG(wstatus[last]));
1445 }
d4f3574e
SS
1446
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)
1452 {
1453 linuxthreads_breakpoint_zombie[i].pid = rpid;
1454 linuxthreads_breakpoint_last++;
1455 }
1456 linuxthreads_breakpoint_zombie[i].pc = read_pc_pid (rpid);
1457 linuxthreads_breakpoint_zombie[i].step = 1;
1458 }
1459 else
1460 {
1461 if (WSTOPSIG(status) != SIGSTOP)
1462 {
1463 for (i = 0; i < last; i++)
1464 if (wstatus[i] == status)
1465 break;
1466 if (i >= last)
ed9a39eb
JM
1467 {
1468 wstatus[last++] = status;
1469 }
d4f3574e
SS
1470 }
1471 child_resume (rpid, 1, TARGET_SIGNAL_0);
1472 continue;
1473 }
1474 if (linuxthreads_inferior_pid)
1475 linuxthreads_inferior_pid = 0;
1476 }
1477
1478 if (linuxthreads_max && !stop_soon_quietly)
1479 {
1480 if (linuxthreads_max
1481 && WIFSTOPPED(status)
1482 && WSTOPSIG(status) == SIGSTOP)
1483 {
1484 /* Skip SIGSTOP signals. */
1485 if (!linuxthreads_pending_status (rpid))
5c44784c
JM
1486 {
1487 if (linuxthreads_step_pid == rpid)
ed9a39eb
JM
1488 {
1489 child_resume (rpid, 1, linuxthreads_step_signo);
1490 }
5c44784c 1491 else
ed9a39eb
JM
1492 {
1493 child_resume (rpid, 0, TARGET_SIGNAL_0);
1494 }
5c44784c 1495 }
d4f3574e
SS
1496 continue;
1497 }
1498
1499 /* Do no report exit status of cloned threads. */
1500 if (WIFEXITED(status))
1501 {
1502 if (rpid == linuxthreads_initial_pid)
1503 linuxthreads_exit_status = status;
1504
1505 /* Remove any zombie breakpoint. */
1506 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1507 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1508 {
1509 REMOVE_BREAKPOINT_ZOMBIE(i);
1510 break;
1511 }
1512 if (pid > 0)
1513 pid = -1;
1514 continue;
1515 }
1516
1517 /* Deal with zombie breakpoint */
1518 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1519 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1520 break;
1521
1522 if (i <= linuxthreads_breakpoint_last)
1523 {
1524 /* There is a potential zombie breakpoint */
1525 if (WIFEXITED(status)
1526 || linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
1527 {
1528 /* The current pc is out of zombie breakpoint. */
1529 REMOVE_BREAKPOINT_ZOMBIE(i);
1530 }
1531 else if (!linuxthreads_breakpoint_zombie[i].step
1532 && WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
1533 {
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)
ed9a39eb
JM
1538 {
1539 child_resume (rpid, 1, linuxthreads_step_signo);
1540 }
d4f3574e 1541 else
ed9a39eb
JM
1542 {
1543 child_resume (rpid, 0, TARGET_SIGNAL_0);
1544 }
d4f3574e
SS
1545 continue;
1546 }
1547 }
1548
1549 /* Walk through linuxthreads array in order to stop them */
1550 if (linuxthreads_breakpoints_inserted)
1551 update_stop_threads (rpid);
1552
1553 }
1554 else if (rpid != inferior_pid)
1555 continue;
1556
1557 store_waitstatus (ourstatus, status);
1558
1559 if (linuxthreads_attach_pending && !stop_soon_quietly)
1560 {
1561 int on = 1;
ed9a39eb
JM
1562 if (!using_thread_db)
1563 {
1564 target_write_memory (linuxthreads_debug,
1565 (char *) &on, sizeof (on));
1566 update_stop_threads (rpid);
1567 }
d4f3574e
SS
1568 linuxthreads_attach_pending = 0;
1569 }
1570
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;
1577
1578 return rpid;
1579 }
1580}
1581
1582/* Fork an inferior process, and start debugging it with ptrace. */
1583
1584static void
1585linuxthreads_create_inferior (exec_file, allargs, env)
1586 char *exec_file;
1587 char *allargs;
1588 char **env;
1589{
1590 if (!exec_file && !exec_bfd)
1591 {
1592 error ("No executable file specified.\n\
1593Use the \"file\" or \"exec-file\" command.");
1594 return;
1595 }
1596
1597 push_target (&linuxthreads_ops);
1598 linuxthreads_breakpoints_inserted = 1;
1599 linuxthreads_breakpoint_last = -1;
1600 linuxthreads_wait_last = -1;
67aaefa2 1601 WSETSTOP (linuxthreads_exit_status, 0);
d4f3574e
SS
1602
1603 if (linuxthreads_max)
1604 linuxthreads_attach_pending = 1;
1605
1606 child_ops.to_create_inferior (exec_file, allargs, env);
1607}
1608
ed9a39eb
JM
1609void
1610linuxthreads_discard_global_state ()
1611{
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;
1620}
1621
d4f3574e
SS
1622/* Clean up after the inferior dies. */
1623
1624static void
1625linuxthreads_mourn_inferior ()
1626{
1627 if (linuxthreads_max)
1628 {
1629 int off = 0;
1630 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1631
ed9a39eb 1632 linuxthreads_discard_global_state ();
d4f3574e
SS
1633 init_thread_list(); /* Destroy thread info */
1634 }
1635
1636 child_ops.to_mourn_inferior ();
1637
1638 unpush_target (&linuxthreads_ops);
1639}
1640
1641/* Kill the inferior process */
1642
1643static void
1644linuxthreads_kill ()
1645{
1646 int rpid;
1647 int status;
1648
1649 if (inferior_pid == 0)
1650 return;
1651
1652 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1653 {
1654 /* Remove all threads status. */
1655 inferior_pid = linuxthreads_manager_pid;
1656 iterate_active_threads (kill_thread, 1);
1657 }
1658
1659 kill_thread (inferior_pid);
1660
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)
1665 {
1666 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1667 {
1668 /* Wait for thread to complete */
1669 while ((rpid = waitpid (-1, &status, __WCLONE)) > 0)
1670 if (!WIFEXITED(status))
1671 kill_thread (rpid);
1672
1673 while ((rpid = waitpid (-1, &status, 0)) > 0)
1674 if (!WIFEXITED(status))
1675 kill_thread (rpid);
1676 }
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);
1681 }
1682#endif
1683
1684 /* Wait for all threads. */
1685 do
ed9a39eb
JM
1686 {
1687 rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
1688 }
d4f3574e 1689 while (rpid > 0 || errno == EINTR);
ed9a39eb 1690 /* FIXME: should no longer need to handle EINTR here. */
d4f3574e
SS
1691
1692 do
ed9a39eb
JM
1693 {
1694 rpid = waitpid (-1, &status, WNOHANG);
1695 }
d4f3574e 1696 while (rpid > 0 || errno == EINTR);
ed9a39eb 1697 /* FIXME: should no longer need to handle EINTR here. */
d4f3574e
SS
1698
1699 linuxthreads_mourn_inferior ();
1700}
1701
1702/* Insert a breakpoint */
1703
1704static int
1705linuxthreads_insert_breakpoint (addr, contents_cache)
1706 CORE_ADDR addr;
1707 char *contents_cache;
1708{
1709 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1710 {
1711 linuxthreads_breakpoint_addr = addr;
1712 iterate_active_threads (insert_breakpoint, 1);
1713 insert_breakpoint (linuxthreads_manager_pid);
1714 }
1715
1716 return child_ops.to_insert_breakpoint (addr, contents_cache);
1717}
1718
1719/* Remove a breakpoint */
1720
1721static int
1722linuxthreads_remove_breakpoint (addr, contents_cache)
1723 CORE_ADDR addr;
1724 char *contents_cache;
1725{
1726 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1727 {
1728 linuxthreads_breakpoint_addr = addr;
1729 iterate_active_threads (remove_breakpoint, 1);
1730 remove_breakpoint (linuxthreads_manager_pid);
1731 }
1732
1733 return child_ops.to_remove_breakpoint (addr, contents_cache);
1734}
1735
1736/* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1737
1738static int
1739linuxthreads_can_run ()
1740{
1741 return child_suppress_run;
1742}
ed9a39eb 1743
d4f3574e
SS
1744\f
1745static void
1746init_linuxthreads_ops ()
1747{
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;
ed9a39eb 1763 linuxthreads_ops.to_pid_to_str = linuxthreads_pid_to_str;
d4f3574e
SS
1764 linuxthreads_ops.to_magic = OPS_MAGIC;
1765}
1766
1767void
1768_initialize_linuxthreads ()
1769{
1770 struct sigaction sact;
ed9a39eb 1771 sigset_t linuxthreads_wait_mask; /* sigset with SIGCHLD */
d4f3574e
SS
1772
1773 init_linuxthreads_ops ();
1774 add_target (&linuxthreads_ops);
1775 child_suppress_run = 1;
1776
11cf8741
JM
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.
1780 */
1781 target_new_objfile_chain = target_new_objfile_hook;
1782 target_new_objfile_hook = linuxthreads_new_objfile;
1783
d4f3574e
SS
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);
1789
1790 /* initialize SIGCHLD mask */
1791 sigemptyset (&linuxthreads_wait_mask);
1792 sigaddset (&linuxthreads_wait_mask, SIGCHLD);
ed9a39eb
JM
1793
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);
0fda6bd2
JM
1799 /* Make sure that linuxthreads_block_mask is not blocking SIGCHLD */
1800 sigdelset (&linuxthreads_block_mask, SIGCHLD);
d4f3574e 1801}
This page took 0.152819 seconds and 4 git commands to generate.