import gdb-1999-12-21 snapshot
[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
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>
55
56#include "defs.h"
57#include "target.h"
58#include "inferior.h"
59#include "gdbcore.h"
60#include "gdbthread.h"
61#include "wait.h"
62#include "gdbcmd.h"
63#include "breakpoint.h"
64
65#ifndef PT_ATTACH
66#define PT_ATTACH PTRACE_ATTACH
67#endif
68#ifndef PT_KILL
69#define PT_KILL PTRACE_KILL
70#endif
71#ifndef PT_READ_U
72#define PT_READ_U PTRACE_PEEKUSR
73#endif
74
75#ifdef NSIG
76#define LINUXTHREAD_NSIG NSIG
77#else
78#ifdef _NSIG
79#define LINUXTHREAD_NSIG _NSIG
80#endif
81#endif
82
83extern int child_suppress_run; /* make inftarg.c non-runnable */
84struct target_ops linuxthreads_ops; /* Forward declaration */
85extern struct target_ops child_ops; /* target vector for inftarg.c */
86
87static CORE_ADDR linuxthreads_handles; /* array of linuxthreads handles */
88static CORE_ADDR linuxthreads_manager; /* pid of linuxthreads manager thread */
89static CORE_ADDR linuxthreads_initial; /* pid of linuxthreads initial thread */
90static CORE_ADDR linuxthreads_debug; /* linuxthreads internal debug flag */
91static CORE_ADDR linuxthreads_num; /* number of valid handle entries */
92
93static 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. */
98
99static int linuxthreads_sizeof_handle; /* size of a linuxthreads handle */
100static int linuxthreads_offset_descr; /* h_descr offset of the linuxthreads
101 handle */
102static int linuxthreads_offset_pid; /* p_pid offset of the linuxthreads
103 descr */
104
105static int linuxthreads_manager_pid; /* manager pid */
106static int linuxthreads_initial_pid; /* initial pid */
107
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. */
113static int *linuxthreads_wait_pid; /* wait array of pid */
114static int *linuxthreads_wait_status; /* wait array of status */
115static int linuxthreads_wait_last; /* index of last valid elt in
116 linuxthreads_wait_{pid,status} */
117
ed9a39eb 118static sigset_t linuxthreads_block_mask; /* sigset without SIGCHLD */
d4f3574e
SS
119
120static int linuxthreads_step_pid; /* current stepped pid */
121static int linuxthreads_step_signo; /* current stepped target signal */
122static int linuxthreads_exit_status; /* exit status of initial thread */
123
124static int linuxthreads_inferior_pid; /* temporary internal inferior pid */
125static int linuxthreads_breakpoint_pid; /* last pid that hit a breakpoint */
126static int linuxthreads_attach_pending; /* attach command without wait */
127
128static int linuxthreads_breakpoints_inserted; /* any breakpoints inserted */
129
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. */
136
137struct linuxthreads_signal {
138
139 /* The name of the LinuxThreads library variable that contains
140 the signal number. */
141 char *var;
142
143 /* True if this variable must exist for us to debug properly. */
144 int required;
145
146 /* The variable's address in the inferior, or zero if the
147 LinuxThreads library hasn't been loaded into this inferior yet. */
148 CORE_ADDR addr;
149
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. */
154 int signal;
155
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. */
160 int stop, print;
161};
162
163struct linuxthreads_signal linuxthreads_sig_restart = {
5c44784c 164 "__pthread_sig_restart", 1, 0, 0, 0, 0
d4f3574e
SS
165};
166struct linuxthreads_signal linuxthreads_sig_cancel = {
5c44784c 167 "__pthread_sig_cancel", 1, 0, 0, 0, 0
d4f3574e
SS
168};
169struct linuxthreads_signal linuxthreads_sig_debug = {
5c44784c 170 "__pthread_sig_debug", 0, 0, 0, 0, 0
d4f3574e
SS
171};
172
ed9a39eb
JM
173/* Set by thread_db module when it takes over the thread_stratum.
174 In that case we must:
175 a) refrain from turning on the debug signal, and
176 b) refrain from calling add_thread. */
177
178int using_thread_db = 0;
179
d4f3574e
SS
180/* A table of breakpoint locations, one per PID. */
181static struct linuxthreads_breakpoint {
182 CORE_ADDR pc; /* PC of breakpoint */
183 int pid; /* pid of breakpoint */
184 int step; /* whether the pc has been reached after sstep */
185} *linuxthreads_breakpoint_zombie; /* Zombie breakpoints array */
186static int linuxthreads_breakpoint_last; /* Last zombie breakpoint */
187
188/* linuxthreads_{insert,remove}_breakpoint pass the breakpoint address
189 to {insert,remove}_breakpoint via this variable, since
190 iterate_active_threads doesn't provide any way to pass values
191 through to the worker function. */
192static CORE_ADDR linuxthreads_breakpoint_addr;
193
194#define REMOVE_BREAKPOINT_ZOMBIE(_i) \
195{ \
196 if ((_i) < linuxthreads_breakpoint_last) \
197 linuxthreads_breakpoint_zombie[(_i)] = \
198 linuxthreads_breakpoint_zombie[linuxthreads_breakpoint_last]; \
199 linuxthreads_breakpoint_last--; \
200}
201
202
203\f
204#ifndef PTRACE_XFER_TYPE
205#define PTRACE_XFER_TYPE int
206#endif
207/* Check to see if the given thread is alive. */
208static int
209linuxthreads_thread_alive (pid)
210 int pid;
211{
212 errno = 0;
213 return ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE)0, 0) >= 0 || errno == 0;
214}
215
216/* On detach(), find a SIGTRAP status. If stop is non-zero, find a
217 SIGSTOP one, too.
218
219 Make sure PID is ready to run, and free of interference from our
220 efforts to debug it (e.g., pending SIGSTOP or SIGTRAP signals). If
221 STOP is zero, just look for a SIGTRAP. If STOP is non-zero, look
222 for a SIGSTOP, too. Return non-zero if PID is alive and ready to
223 run; return zero if PID is dead.
224
225 PID may or may not be stopped at the moment, and we may or may not
226 have waited for it already. We check the linuxthreads_wait bag in
227 case we've already got a status for it. We may possibly wait for
228 it ourselves.
229
230 PID may have signals waiting to be delivered. If they're caused by
231 our efforts to debug it, accept them with wait, but don't pass them
232 through to PID. Do pass all other signals through. */
233static int
234linuxthreads_find_trap (pid, stop)
235 int pid;
236 int stop;
237{
238 int i;
239 int rpid;
240 int status;
241 int found_stop = 0;
242 int found_trap = 0;
243
244 /* PID may have any number of signals pending. The kernel will
245 report each of them to us via wait, and then it's up to us to
246 pass them along to the process via ptrace, if we so choose.
247
248 We need to paw through the whole set until we've found a SIGTRAP
249 (or a SIGSTOP, if `stop' is set). We don't pass the SIGTRAP (or
250 SIGSTOP) through, but we do re-send all the others, so PID will
251 receive them when we resume it. */
252 int *wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
253 int last = 0;
254
255 /* Look at the pending status */
256 for (i = linuxthreads_wait_last; i >= 0; i--)
257 if (linuxthreads_wait_pid[i] == pid)
258 {
259 status = linuxthreads_wait_status[i];
260
261 /* Delete the i'th member of the table. Since the table is
262 unordered, we can do this simply by copying the table's
263 last element to the i'th position, and shrinking the table
264 by one element. */
265 if (i < linuxthreads_wait_last)
266 {
267 linuxthreads_wait_status[i] =
268 linuxthreads_wait_status[linuxthreads_wait_last];
269 linuxthreads_wait_pid[i] =
270 linuxthreads_wait_pid[linuxthreads_wait_last];
271 }
272 linuxthreads_wait_last--;
273
274 if (!WIFSTOPPED(status)) /* Thread has died */
275 return 0;
276
277 if (WSTOPSIG(status) == SIGTRAP)
278 {
279 if (stop)
280 found_trap = 1;
281 else
282 return 1;
283 }
284 else if (WSTOPSIG(status) == SIGSTOP)
285 {
286 if (stop)
287 found_stop = 1;
288 }
289 else
290 {
291 wstatus[0] = status;
292 last = 1;
293 }
294
295 break;
296 }
297
298 if (stop)
299 {
300 /* Make sure that we'll find what we're looking for. */
301 if (!found_trap)
ed9a39eb
JM
302 {
303 kill (pid, SIGTRAP);
304 }
d4f3574e 305 if (!found_stop)
ed9a39eb
JM
306 {
307 kill (pid, SIGSTOP);
308 }
d4f3574e
SS
309 }
310
311 /* Catch all status until SIGTRAP and optionally SIGSTOP show up. */
312 for (;;)
313 {
ed9a39eb 314 /* resume the child every time... */
d4f3574e
SS
315 child_resume (pid, 1, TARGET_SIGNAL_0);
316
ed9a39eb
JM
317 /* loop as long as errno == EINTR:
318 waitpid syscall may be aborted due to GDB receiving a signal.
319 FIXME: EINTR handling should no longer be necessary here, since
320 we now block SIGCHLD except in an explicit sigsuspend call. */
321
d4f3574e
SS
322 for (;;)
323 {
324 rpid = waitpid (pid, &status, __WCLONE);
325 if (rpid > 0)
ed9a39eb
JM
326 {
327 break;
328 }
d4f3574e 329 if (errno == EINTR)
ed9a39eb
JM
330 {
331 continue;
332 }
d4f3574e
SS
333
334 /* There are a few reasons the wait call above may have
335 failed. If the thread manager dies, its children get
336 reparented, and this interferes with GDB waiting for
337 them, in some cases. Another possibility is that the
338 initial thread was not cloned, so calling wait with
339 __WCLONE won't find it. I think neither of these should
340 occur in modern Linux kernels --- they don't seem to in
341 2.0.36. */
342 rpid = waitpid (pid, &status, 0);
343 if (rpid > 0)
ed9a39eb
JM
344 {
345 break;
346 }
d4f3574e 347 if (errno != EINTR)
ed9a39eb 348 perror_with_name ("find_trap/waitpid");
d4f3574e
SS
349 }
350
351 if (!WIFSTOPPED(status)) /* Thread has died */
352 return 0;
353
354 if (WSTOPSIG(status) == SIGTRAP)
355 if (!stop || found_stop)
356 break;
357 else
358 found_trap = 1;
359 else if (WSTOPSIG(status) != SIGSTOP)
360 wstatus[last++] = status;
361 else if (stop)
5c44784c
JM
362 {
363 if (found_trap)
364 break;
365 else
366 found_stop = 1;
367 }
d4f3574e
SS
368 }
369
370 /* Resend any other signals we noticed to the thread, to be received
371 when we continue it. */
372 while (--last >= 0)
ed9a39eb
JM
373 {
374 kill (pid, WSTOPSIG(wstatus[last]));
375 }
d4f3574e
SS
376
377 return 1;
378}
379
380/* Cleanup stub for save_inferior_pid. */
381static void
382restore_inferior_pid (arg)
383 void *arg;
384{
ed9a39eb
JM
385#if TARGET_PTR_BIT > TARGET_INT_BIT
386 inferior_pid = (int) ((long) arg);
387#else
388 inferior_pid = (int) arg;
389#endif
d4f3574e
SS
390}
391
392/* Register a cleanup to restore the value of inferior_pid. */
393static struct cleanup *
394save_inferior_pid ()
395{
ed9a39eb
JM
396#if TARGET_PTR_BIT > TARGET_INT_BIT
397 return make_cleanup (restore_inferior_pid, (void *) ((long) inferior_pid));
398#else
d4f3574e 399 return make_cleanup (restore_inferior_pid, (void *) inferior_pid);
ed9a39eb 400#endif
d4f3574e
SS
401}
402
403static void
404sigchld_handler (signo)
405 int signo;
406{
407 /* This handler is used to get an EINTR while doing waitpid()
408 when an event is received */
409}
410
411/* Have we already collected a wait status for PID in the
412 linuxthreads_wait bag? */
413static int
414linuxthreads_pending_status (pid)
415 int pid;
416{
417 int i;
418 for (i = linuxthreads_wait_last; i >= 0; i--)
419 if (linuxthreads_wait_pid[i] == pid)
420 return 1;
421 return 0;
422}
423
424\f
425/* Internal linuxthreads signal management */
426
427/* Check in OBJFILE for the variable that holds the number for signal SIG.
428 We assume that we've already found other LinuxThreads-ish variables
429 in OBJFILE, so we complain if it's required, but not there.
430 Return true iff things are okay. */
431static int
432find_signal_var (sig, objfile)
433 struct linuxthreads_signal *sig;
434 struct objfile *objfile;
435{
436 struct minimal_symbol *ms = lookup_minimal_symbol (sig->var, NULL, objfile);
437
438 if (! ms)
439 {
440 if (sig->required)
441 {
442 fprintf_unfiltered (gdb_stderr,
443 "Unable to find linuxthreads symbol \"%s\"\n",
444 sig->var);
445 return 0;
446 }
447 else
448 {
449 sig->addr = 0;
450 return 1;
451 }
452 }
453
454 sig->addr = SYMBOL_VALUE_ADDRESS (ms);
455
456 return 1;
457}
458
459static int
460find_all_signal_vars (objfile)
461 struct objfile *objfile;
462{
463 return ( find_signal_var (&linuxthreads_sig_restart, objfile)
464 && find_signal_var (&linuxthreads_sig_cancel, objfile)
465 && find_signal_var (&linuxthreads_sig_debug, objfile));
466}
467
468/* A struct complaint isn't appropriate here. */
469static int complained_cannot_determine_thread_signal_number = 0;
470
471/* Check to see if the variable holding the signal number for SIG has
472 been initialized yet. If it has, tell GDB to pass that signal
473 through to the inferior silently. */
474static void
475check_signal_number (sig)
476 struct linuxthreads_signal *sig;
477{
478 int num;
479
480 if (sig->signal)
481 /* We already know this signal number. */
482 return;
483
484 if (! sig->addr)
485 /* We don't know the variable's address yet. */
486 return;
487
488 if (target_read_memory (sig->addr, (char *)&num, sizeof (num))
489 != 0)
490 {
491 /* If this happens once, it'll probably happen for all the
492 signals, so only complain once. */
493 if (! complained_cannot_determine_thread_signal_number)
494 warning ("Cannot determine thread signal number; "
495 "GDB may report spurious signals.");
496 complained_cannot_determine_thread_signal_number = 1;
497 return;
498 }
499
500 if (num == 0)
501 /* It hasn't been initialized yet. */
502 return;
503
504 /* We know sig->signal was zero, and is becoming non-zero, so it's
505 okay to sample GDB's original settings. */
506 sig->signal = num;
507 sig->stop = signal_stop_update (target_signal_from_host (num), 0);
508 sig->print = signal_print_update (target_signal_from_host (num), 0);
509}
510
ed9a39eb 511void
d4f3574e
SS
512check_all_signal_numbers ()
513{
514 /* If this isn't a LinuxThreads program, quit early. */
515 if (! linuxthreads_max)
516 return;
517
518 check_signal_number (&linuxthreads_sig_restart);
519 check_signal_number (&linuxthreads_sig_cancel);
520 check_signal_number (&linuxthreads_sig_debug);
521
522 /* handle linuxthread exit */
523 if (linuxthreads_sig_debug.signal
524 || linuxthreads_sig_restart.signal)
525 {
526 struct sigaction sact;
527
528 sact.sa_handler = sigchld_handler;
529 sigemptyset(&sact.sa_mask);
530 sact.sa_flags = 0;
ed9a39eb 531
d4f3574e
SS
532 if (linuxthreads_sig_debug.signal > 0)
533 sigaction(linuxthreads_sig_cancel.signal, &sact, NULL);
534 else
535 sigaction(linuxthreads_sig_restart.signal, &sact, NULL);
536 }
537}
538
539
540/* Restore GDB's original settings for SIG.
541 This should only be called when we're no longer sure if we're
542 talking to an executable that uses LinuxThreads, so we clear the
543 signal number and variable address too. */
544static void
545restore_signal (sig)
546 struct linuxthreads_signal *sig;
547{
548 if (! sig->signal)
549 return;
550
551 /* We know sig->signal was non-zero, and is becoming zero, so it's
552 okay to restore GDB's original settings. */
553 signal_stop_update (target_signal_from_host (sig->signal), sig->stop);
554 signal_print_update (target_signal_from_host (sig->signal), sig->print);
555
556 sig->signal = 0;
557 sig->addr = 0;
558}
559
560
561/* Restore GDB's original settings for all LinuxThreads signals.
562 This should only be called when we're no longer sure if we're
563 talking to an executable that uses LinuxThreads, so we clear the
564 signal number and variable address too. */
565static void
566restore_all_signals ()
567{
568 restore_signal (&linuxthreads_sig_restart);
569 restore_signal (&linuxthreads_sig_cancel);
570 restore_signal (&linuxthreads_sig_debug);
571
572 /* If it happens again, we should complain again. */
573 complained_cannot_determine_thread_signal_number = 0;
574}
575
576
577\f
578
579/* Apply FUNC to the pid of each active thread. This consults the
580 inferior's handle table to find active threads.
581
582 If ALL is non-zero, process all threads.
583 If ALL is zero, skip threads with pending status. */
584static void
585iterate_active_threads (func, all)
586 void (*func)(int);
587 int all;
588{
589 CORE_ADDR descr;
590 int pid;
591 int i;
592 int num;
593
594 read_memory (linuxthreads_num, (char *)&num, sizeof (int));
595
596 for (i = 0; i < linuxthreads_max && num > 0; i++)
597 {
598 read_memory (linuxthreads_handles +
599 linuxthreads_sizeof_handle * i + linuxthreads_offset_descr,
600 (char *)&descr, sizeof (void *));
601 if (descr)
602 {
603 num--;
604 read_memory (descr + linuxthreads_offset_pid,
605 (char *)&pid, sizeof (pid_t));
606 if (pid > 0 && pid != linuxthreads_manager_pid
607 && (all || (!linuxthreads_pending_status (pid))))
608 (*func)(pid);
609 }
610 }
d4f3574e
SS
611}
612
613/* Insert a thread breakpoint at linuxthreads_breakpoint_addr.
614 This is the worker function for linuxthreads_insert_breakpoint,
615 which passes it to iterate_active_threads. */
616static void
617insert_breakpoint (pid)
618 int pid;
619{
620 int j;
621
622 /* Remove (if any) the positive zombie breakpoint. */
623 for (j = linuxthreads_breakpoint_last; j >= 0; j--)
624 if (linuxthreads_breakpoint_zombie[j].pid == pid)
625 {
626 if ((linuxthreads_breakpoint_zombie[j].pc - DECR_PC_AFTER_BREAK
627 == linuxthreads_breakpoint_addr)
628 && !linuxthreads_breakpoint_zombie[j].step)
629 REMOVE_BREAKPOINT_ZOMBIE(j);
630 break;
631 }
632}
633
634/* Note that we're about to remove a thread breakpoint at
635 linuxthreads_breakpoint_addr.
636
637 This is the worker function for linuxthreads_remove_breakpoint,
638 which passes it to iterate_active_threads. The actual work of
639 overwriting the breakpoint instruction is done by
640 child_ops.to_remove_breakpoint; here, we simply create a zombie
641 breakpoint if the thread's PC is pointing at the breakpoint being
642 removed. */
643static void
644remove_breakpoint (pid)
645 int pid;
646{
647 int j;
648
649 /* Insert a positive zombie breakpoint (if needed). */
650 for (j = 0; j <= linuxthreads_breakpoint_last; j++)
651 if (linuxthreads_breakpoint_zombie[j].pid == pid)
652 break;
653
654 if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
655 {
656 CORE_ADDR pc = read_pc_pid (pid);
657 if (linuxthreads_breakpoint_addr == pc - DECR_PC_AFTER_BREAK
658 && j > linuxthreads_breakpoint_last)
659 {
660 linuxthreads_breakpoint_zombie[j].pid = pid;
661 linuxthreads_breakpoint_zombie[j].pc = pc;
662 linuxthreads_breakpoint_zombie[j].step = 0;
663 linuxthreads_breakpoint_last++;
664 }
665 }
666}
667
668/* Kill a thread */
669static void
670kill_thread (pid)
671 int pid;
672{
673 if (in_thread_list (pid))
ed9a39eb
JM
674 {
675 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
676 }
d4f3574e 677 else
ed9a39eb
JM
678 {
679 kill (pid, SIGKILL);
680 }
d4f3574e
SS
681}
682
683/* Resume a thread */
684static void
685resume_thread (pid)
686 int pid;
687{
688 if (pid != inferior_pid
689 && in_thread_list (pid)
690 && linuxthreads_thread_alive (pid))
5c44784c
JM
691 {
692 if (pid == linuxthreads_step_pid)
ed9a39eb
JM
693 {
694 child_resume (pid, 1, linuxthreads_step_signo);
695 }
5c44784c 696 else
ed9a39eb
JM
697 {
698 child_resume (pid, 0, TARGET_SIGNAL_0);
699 }
5c44784c 700 }
d4f3574e
SS
701}
702
703/* Detach a thread */
704static void
705detach_thread (pid)
706 int pid;
707{
708 if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
709 {
710 /* Remove pending SIGTRAP and SIGSTOP */
711 linuxthreads_find_trap (pid, 1);
712
713 inferior_pid = pid;
714 detach (TARGET_SIGNAL_0);
715 inferior_pid = linuxthreads_manager_pid;
716 }
717}
718
ed9a39eb
JM
719/* Attach a thread */
720void
721attach_thread (pid)
722 int pid;
723{
724 if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) != 0)
725 perror_with_name ("attach_thread");
726}
727
d4f3574e
SS
728/* Stop a thread */
729static void
730stop_thread (pid)
731 int pid;
732{
733 if (pid != inferior_pid)
5c44784c
JM
734 {
735 if (in_thread_list (pid))
ed9a39eb
JM
736 {
737 kill (pid, SIGSTOP);
738 }
5c44784c
JM
739 else if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) == 0)
740 {
741 if (!linuxthreads_attach_pending)
ed9a39eb 742 printf_filtered ("[New %s]\n", target_pid_to_str (pid));
5c44784c
JM
743 add_thread (pid);
744 if (linuxthreads_sig_debug.signal)
ed9a39eb
JM
745 {
746 /* After a new thread in glibc 2.1 signals gdb its existence,
747 it suspends itself and wait for linuxthreads_sig_restart,
748 now we can wake it up. */
749 kill (pid, linuxthreads_sig_restart.signal);
750 }
5c44784c
JM
751 }
752 else
753 perror_with_name ("ptrace in stop_thread");
754 }
d4f3574e
SS
755}
756
757/* Wait for a thread */
758static void
759wait_thread (pid)
760 int pid;
761{
762 int status;
763 int rpid;
764
765 if (pid != inferior_pid && in_thread_list (pid))
766 {
ed9a39eb
JM
767 /* loop as long as errno == EINTR:
768 waitpid syscall may be aborted if GDB receives a signal.
769 FIXME: EINTR handling should no longer be necessary here, since
770 we now block SIGCHLD except during an explicit sigsuspend call. */
d4f3574e
SS
771 for (;;)
772 {
773 /* Get first pid status. */
774 rpid = waitpid(pid, &status, __WCLONE);
775 if (rpid > 0)
ed9a39eb
JM
776 {
777 break;
778 }
d4f3574e 779 if (errno == EINTR)
ed9a39eb
JM
780 {
781 continue;
782 }
d4f3574e
SS
783
784 /* There are two reasons this might have failed:
785
786 1) PID is the initial thread, which wasn't cloned, so
787 passing the __WCLONE flag to waitpid prevented us from
788 finding it.
789
790 2) The manager thread is the parent of all but the
791 initial thread; if it dies, the children will all be
792 reparented to init, which will wait for them. This means
793 our call to waitpid won't find them.
794
795 Actually, based on a casual look at the 2.0.36 kernel
796 code, I don't think either of these cases happen. But I
797 don't have things set up for remotely debugging the
798 kernel, so I'm not sure. And perhaps older kernels
799 didn't work. */
800 rpid = waitpid(pid, &status, 0);
801 if (rpid > 0)
ed9a39eb
JM
802 {
803 break;
804 }
d4f3574e 805 if (errno != EINTR && linuxthreads_thread_alive (pid))
ed9a39eb 806 perror_with_name ("wait_thread/waitpid");
d4f3574e
SS
807
808 /* the thread is dead. */
809 return;
810 }
811 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
812 {
813 linuxthreads_wait_pid[++linuxthreads_wait_last] = pid;
814 linuxthreads_wait_status[linuxthreads_wait_last] = status;
815 }
816 }
817}
818
819/* Walk through the linuxthreads handles in order to detect all
820 threads and stop them */
821static void
822update_stop_threads (test_pid)
823 int test_pid;
824{
825 struct cleanup *old_chain = NULL;
826
827 check_all_signal_numbers ();
828
829 if (linuxthreads_manager_pid == 0)
830 {
831 if (linuxthreads_manager)
832 {
833 if (test_pid > 0 && test_pid != inferior_pid)
834 {
835 old_chain = save_inferior_pid ();
836 inferior_pid = test_pid;
837 }
838 read_memory (linuxthreads_manager,
839 (char *)&linuxthreads_manager_pid, sizeof (pid_t));
840 }
841 if (linuxthreads_initial)
842 {
843 if (test_pid > 0 && test_pid != inferior_pid)
844 {
845 old_chain = save_inferior_pid ();
846 inferior_pid = test_pid;
847 }
848 read_memory(linuxthreads_initial,
849 (char *)&linuxthreads_initial_pid, sizeof (pid_t));
850 }
851 }
852
853 if (linuxthreads_manager_pid != 0)
854 {
855 if (old_chain == NULL && test_pid > 0 &&
856 test_pid != inferior_pid && linuxthreads_thread_alive (test_pid))
857 {
858 old_chain = save_inferior_pid ();
859 inferior_pid = test_pid;
860 }
861
862 if (linuxthreads_thread_alive (inferior_pid))
863 {
864 if (test_pid > 0)
865 {
866 if (test_pid != linuxthreads_manager_pid
867 && !linuxthreads_pending_status (linuxthreads_manager_pid))
868 {
869 stop_thread (linuxthreads_manager_pid);
870 wait_thread (linuxthreads_manager_pid);
871 }
872 if (!in_thread_list (test_pid))
873 {
874 if (!linuxthreads_attach_pending)
ed9a39eb
JM
875 printf_filtered ("[New %s]\n",
876 target_pid_to_str (test_pid));
d4f3574e
SS
877 add_thread (test_pid);
878 if (linuxthreads_sig_debug.signal
879 && inferior_pid == test_pid)
ed9a39eb
JM
880 {
881 /* After a new thread in glibc 2.1 signals gdb its
882 existence, it suspends itself and wait for
883 linuxthreads_sig_restart, now we can wake it up. */
884 kill (test_pid, linuxthreads_sig_restart.signal);
885 }
d4f3574e
SS
886 }
887 }
888 iterate_active_threads (stop_thread, 0);
889 iterate_active_threads (wait_thread, 0);
890 }
891 }
892
893 if (old_chain != NULL)
894 do_cleanups (old_chain);
895}
896
11cf8741
JM
897/* This routine is called whenever a new symbol table is read in, or
898 when all symbol tables are removed. linux-thread event handling
899 can only be initialized when we find the right variables in
900 libpthread.so. Since it's a shared library, those variables don't
901 show up until the library gets mapped and the symbol table is read
902 in. */
903
904/* This new_objfile event is now managed by a chained function pointer.
905 * It is the callee's responsability to call the next client on the chain.
906 */
907
908/* Saved pointer to previous owner of the new_objfile event. */
909static void (*target_new_objfile_chain) PARAMS ((struct objfile *));
d4f3574e
SS
910
911void
912linuxthreads_new_objfile (objfile)
913 struct objfile *objfile;
914{
915 struct minimal_symbol *ms;
916
ed9a39eb
JM
917 /* Call predecessor on chain, if any.
918 Calling the new module first allows it to dominate,
919 if it finds its compatible libraries. */
920
921 if (target_new_objfile_chain)
922 target_new_objfile_chain (objfile);
923
d4f3574e
SS
924 if (!objfile)
925 {
926 /* We're starting an entirely new executable, so we can no
927 longer be sure that it uses LinuxThreads. Restore the signal
928 flags to their original states. */
929 restore_all_signals ();
930
931 /* Indicate that we don't know anything's address any more. */
932 linuxthreads_max = 0;
933
11cf8741 934 goto quit;
d4f3574e
SS
935 }
936
937 /* If we've already found our variables in another objfile, don't
938 bother looking for them again. */
939 if (linuxthreads_max)
11cf8741 940 goto quit;
d4f3574e
SS
941
942 if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL, objfile))
943 /* This object file isn't the pthreads library. */
11cf8741 944 goto quit;
d4f3574e
SS
945
946 if ((ms = lookup_minimal_symbol ("__pthread_threads_debug",
947 NULL, objfile)) == NULL)
948 {
949 /* The debugging-aware libpthreads is not present in this objfile */
950 warning ("\
951This program seems to use POSIX threads, but the thread library used\n\
952does not support debugging. This may make using GDB difficult. Don't\n\
953set breakpoints or single-step through code that might be executed by\n\
954any thread other than the main thread.");
11cf8741 955 goto quit;
d4f3574e
SS
956 }
957 linuxthreads_debug = SYMBOL_VALUE_ADDRESS (ms);
958
959 /* Read internal structures configuration */
960 if ((ms = lookup_minimal_symbol ("__pthread_sizeof_handle",
961 NULL, objfile)) == NULL
962 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
963 (char *)&linuxthreads_sizeof_handle,
964 sizeof (linuxthreads_sizeof_handle)) != 0)
965 {
966 fprintf_unfiltered (gdb_stderr,
967 "Unable to find linuxthreads symbol \"%s\"\n",
968 "__pthread_sizeof_handle");
11cf8741 969 goto quit;
d4f3574e
SS
970 }
971
972 if ((ms = lookup_minimal_symbol ("__pthread_offsetof_descr",
973 NULL, objfile)) == NULL
974 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
975 (char *)&linuxthreads_offset_descr,
976 sizeof (linuxthreads_offset_descr)) != 0)
977 {
978 fprintf_unfiltered (gdb_stderr,
979 "Unable to find linuxthreads symbol \"%s\"\n",
980 "__pthread_offsetof_descr");
11cf8741 981 goto quit;
d4f3574e
SS
982 }
983
984 if ((ms = lookup_minimal_symbol ("__pthread_offsetof_pid",
985 NULL, objfile)) == NULL
986 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
987 (char *)&linuxthreads_offset_pid,
988 sizeof (linuxthreads_offset_pid)) != 0)
989 {
990 fprintf_unfiltered (gdb_stderr,
991 "Unable to find linuxthreads symbol \"%s\"\n",
992 "__pthread_offsetof_pid");
11cf8741 993 goto quit;
d4f3574e
SS
994 }
995
996 if (! find_all_signal_vars (objfile))
11cf8741 997 goto quit;
d4f3574e
SS
998
999 /* Read adresses of internal structures to access */
1000 if ((ms = lookup_minimal_symbol ("__pthread_handles",
1001 NULL, objfile)) == NULL)
1002 {
1003 fprintf_unfiltered (gdb_stderr,
1004 "Unable to find linuxthreads symbol \"%s\"\n",
1005 "__pthread_handles");
11cf8741 1006 goto quit;
d4f3574e
SS
1007 }
1008 linuxthreads_handles = SYMBOL_VALUE_ADDRESS (ms);
1009
1010 if ((ms = lookup_minimal_symbol ("__pthread_handles_num",
1011 NULL, objfile)) == NULL)
1012 {
1013 fprintf_unfiltered (gdb_stderr,
1014 "Unable to find linuxthreads symbol \"%s\"\n",
1015 "__pthread_handles_num");
11cf8741 1016 goto quit;
d4f3574e
SS
1017 }
1018 linuxthreads_num = SYMBOL_VALUE_ADDRESS (ms);
1019
1020 if ((ms = lookup_minimal_symbol ("__pthread_manager_thread",
1021 NULL, objfile)) == NULL)
1022 {
1023 fprintf_unfiltered (gdb_stderr,
1024 "Unable to find linuxthreads symbol \"%s\"\n",
1025 "__pthread_manager_thread");
11cf8741 1026 goto quit;
d4f3574e
SS
1027 }
1028 linuxthreads_manager = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
1029
1030 if ((ms = lookup_minimal_symbol ("__pthread_initial_thread",
1031 NULL, objfile)) == NULL)
1032 {
1033 fprintf_unfiltered (gdb_stderr,
1034 "Unable to find linuxthreads symbol \"%s\"\n",
1035 "__pthread_initial_thread");
11cf8741 1036 goto quit;
d4f3574e
SS
1037 }
1038 linuxthreads_initial = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
1039
1040 /* Search for this last, so it won't be set to a non-zero value unless
1041 we successfully found all the symbols above. */
1042 if ((ms = lookup_minimal_symbol ("__pthread_threads_max",
1043 NULL, objfile)) == NULL
1044 || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
1045 (char *)&linuxthreads_max,
1046 sizeof (linuxthreads_max)) != 0)
1047 {
1048 fprintf_unfiltered (gdb_stderr,
1049 "Unable to find linuxthreads symbol \"%s\"\n",
1050 "__pthread_threads_max");
11cf8741 1051 goto quit;
d4f3574e
SS
1052 }
1053
1054 /* Allocate gdb internal structures */
1055 linuxthreads_wait_pid =
1056 (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
1057 linuxthreads_wait_status =
1058 (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
1059 linuxthreads_breakpoint_zombie = (struct linuxthreads_breakpoint *)
1060 xmalloc (sizeof (struct linuxthreads_breakpoint) * (linuxthreads_max + 1));
1061
ed9a39eb
JM
1062 if (inferior_pid &&
1063 !linuxthreads_attach_pending &&
1064 !using_thread_db) /* suppressed by thread_db module */
d4f3574e
SS
1065 {
1066 int on = 1;
ed9a39eb 1067
d4f3574e
SS
1068 target_write_memory (linuxthreads_debug, (char *)&on, sizeof (on));
1069 linuxthreads_attach_pending = 1;
1070 update_stop_threads (inferior_pid);
1071 linuxthreads_attach_pending = 0;
1072 }
11cf8741 1073
ed9a39eb
JM
1074 check_all_signal_numbers ();
1075
11cf8741 1076quit:
d4f3574e
SS
1077}
1078
1079/* If we have switched threads from a one that stopped at breakpoint,
1080 return 1 otherwise 0. */
1081
1082int
1083linuxthreads_prepare_to_proceed (step)
1084 int step;
1085{
1086 if (!linuxthreads_max
1087 || !linuxthreads_manager_pid
1088 || !linuxthreads_breakpoint_pid
1089 || !breakpoint_here_p (read_pc_pid (linuxthreads_breakpoint_pid)))
1090 return 0;
1091
1092 if (step)
1093 {
1094 /* Mark the current inferior as single stepping process. */
1095 linuxthreads_step_pid = inferior_pid;
1096 }
1097
1098 linuxthreads_inferior_pid = linuxthreads_breakpoint_pid;
1099 return linuxthreads_breakpoint_pid;
1100}
1101
1102/* Convert a pid to printable form. */
1103
1104char *
1105linuxthreads_pid_to_str (pid)
1106 int pid;
1107{
1108 static char buf[100];
1109
1110 sprintf (buf, "%s %d%s", linuxthreads_max ? "Thread" : "Pid", pid,
1111 (pid == linuxthreads_manager_pid) ? " (manager thread)"
1112 : (pid == linuxthreads_initial_pid) ? " (initial thread)"
1113 : "");
1114
1115 return buf;
1116}
1117
1118/* Attach to process PID, then initialize for debugging it
1119 and wait for the trace-trap that results from attaching. */
1120
1121static void
1122linuxthreads_attach (args, from_tty)
1123 char *args;
1124 int from_tty;
1125{
1126 if (!args)
1127 error_no_arg ("process-id to attach");
1128
1129 push_target (&linuxthreads_ops);
1130 linuxthreads_breakpoints_inserted = 1;
1131 linuxthreads_breakpoint_last = -1;
1132 linuxthreads_wait_last = -1;
1133 linuxthreads_exit_status = __W_STOPCODE(0);
1134
1135 child_ops.to_attach (args, from_tty);
1136
1137 if (linuxthreads_max)
1138 linuxthreads_attach_pending = 1;
1139}
1140
1141/* Take a program previously attached to and detaches it.
1142 The program resumes execution and will no longer stop
1143 on signals, etc. We'd better not have left any breakpoints
1144 in the program or it'll die when it hits one. For this
1145 to work, it may be necessary for the process to have been
1146 previously attached. It *might* work if the program was
1147 started via the normal ptrace (PTRACE_TRACEME). */
1148
1149static void
1150linuxthreads_detach (args, from_tty)
1151 char *args;
1152 int from_tty;
1153{
1154 if (linuxthreads_max)
1155 {
1156 int i;
1157 int pid;
1158 int off = 0;
1159 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1160
1161 /* Walk through linuxthreads array in order to detach known threads. */
1162 if (linuxthreads_manager_pid != 0)
1163 {
1164 /* Get rid of all positive zombie breakpoints. */
1165 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1166 {
1167 if (linuxthreads_breakpoint_zombie[i].step)
1168 continue;
1169
1170 pid = linuxthreads_breakpoint_zombie[i].pid;
1171 if (!linuxthreads_thread_alive (pid))
1172 continue;
1173
1174 if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (pid))
1175 continue;
1176
1177 /* Continue in STEP mode until the thread pc has moved or
1178 until SIGTRAP is found on the same PC. */
1179 if (linuxthreads_find_trap (pid, 0)
1180 && linuxthreads_breakpoint_zombie[i].pc == read_pc_pid (pid))
1181 write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
1182 - DECR_PC_AFTER_BREAK, pid);
1183 }
1184
1185 /* Detach thread after thread. */
1186 inferior_pid = linuxthreads_manager_pid;
1187 iterate_active_threads (detach_thread, 1);
1188
1189 /* Remove pending SIGTRAP and SIGSTOP */
1190 linuxthreads_find_trap (inferior_pid, 1);
1191
1192 linuxthreads_wait_last = -1;
1193 linuxthreads_exit_status = __W_STOPCODE(0);
1194 }
1195
1196 linuxthreads_inferior_pid = 0;
1197 linuxthreads_breakpoint_pid = 0;
1198 linuxthreads_step_pid = 0;
1199 linuxthreads_step_signo = TARGET_SIGNAL_0;
1200 linuxthreads_manager_pid = 0;
1201 linuxthreads_initial_pid = 0;
1202 linuxthreads_attach_pending = 0;
1203 init_thread_list (); /* Destroy thread info */
1204 }
1205
1206 child_ops.to_detach (args, from_tty);
1207
1208 unpush_target (&linuxthreads_ops);
1209}
1210
1211/* Resume execution of process PID. If STEP is nozero, then
1212 just single step it. If SIGNAL is nonzero, restart it with that
1213 signal activated. */
1214
1215static void
1216linuxthreads_resume (pid, step, signo)
1217 int pid;
1218 int step;
1219 enum target_signal signo;
1220{
1221 if (!linuxthreads_max || stop_soon_quietly || linuxthreads_manager_pid == 0)
ed9a39eb
JM
1222 {
1223 child_ops.to_resume (pid, step, signo);
1224 }
d4f3574e
SS
1225 else
1226 {
1227 int rpid;
1228 if (linuxthreads_inferior_pid)
1229 {
1230 /* Prepare resume of the last thread that hit a breakpoint */
1231 linuxthreads_breakpoints_inserted = 0;
1232 rpid = linuxthreads_inferior_pid;
1233 linuxthreads_step_signo = signo;
1234 }
1235 else
1236 {
1237 struct cleanup *old_chain = NULL;
1238 int i;
1239
1240 if (pid < 0)
1241 {
1242 linuxthreads_step_pid = step ? inferior_pid : 0;
1243 linuxthreads_step_signo = signo;
1244 rpid = inferior_pid;
1245 }
1246 else
1247 rpid = pid;
1248
1249 if (pid < 0 || !step)
1250 {
1251 linuxthreads_breakpoints_inserted = 1;
1252
1253 /* Walk through linuxthreads array in order to resume threads */
1254 if (pid >= 0 && inferior_pid != pid)
1255 {
1256 old_chain = save_inferior_pid ();
1257 inferior_pid = pid;
1258 }
1259
1260 iterate_active_threads (resume_thread, 0);
1261 if (linuxthreads_manager_pid != inferior_pid
1262 && !linuxthreads_pending_status (linuxthreads_manager_pid))
1263 resume_thread (linuxthreads_manager_pid);
1264 }
1265 else
1266 linuxthreads_breakpoints_inserted = 0;
1267
1268 /* Deal with zombie breakpoint */
1269 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1270 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1271 {
1272 if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
1273 {
1274 /* The current pc is out of zombie breakpoint. */
1275 REMOVE_BREAKPOINT_ZOMBIE(i);
1276 }
1277 break;
1278 }
1279
1280 if (old_chain != NULL)
1281 do_cleanups (old_chain);
1282 }
1283
1284 /* Resume initial thread. */
ed9a39eb 1285 /* [unles it has a wait event pending] */
d4f3574e 1286 if (!linuxthreads_pending_status (rpid))
ed9a39eb
JM
1287 {
1288 child_ops.to_resume (rpid, step, signo);
1289 }
d4f3574e
SS
1290 }
1291}
1292
ed9a39eb
JM
1293/* Abstract out the child_wait functionality. */
1294int
1295linux_child_wait (pid, rpid, status)
1296 int pid;
1297 int *rpid;
1298 int *status;
1299{
1300 int save_errno;
1301
1302 /* Note: inftarg has these inside the loop. */
1303 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1304 attached process. */
1305 set_sigio_trap ();
1306
1307 errno = save_errno = 0;
1308 for (;;)
1309 {
1310 errno = 0;
1311 *rpid = waitpid (pid, status, __WCLONE | WNOHANG);
1312 save_errno = errno;
1313
1314 if (*rpid > 0)
1315 {
1316 /* Got an event -- break out */
1317 break;
1318 }
1319 if (errno == EINTR) /* interrupted by signal, try again */
1320 {
1321 continue;
1322 }
1323
1324 errno = 0;
1325 *rpid = waitpid (pid, status, WNOHANG);
1326 if (*rpid > 0)
1327 {
1328 /* Got an event -- break out */
1329 break;
1330 }
1331 if (errno == EINTR)
1332 {
1333 continue;
1334 }
1335 if (errno != 0 && save_errno != 0)
1336 {
1337 break;
1338 }
1339 sigsuspend(&linuxthreads_block_mask);
1340 }
1341 clear_sigio_trap ();
1342 clear_sigint_trap ();
1343
1344 return errno ? errno : save_errno;
1345}
1346
1347
d4f3574e
SS
1348/* Wait for any threads to stop. We may have to convert PID from a thread id
1349 to a LWP id, and vice versa on the way out. */
1350
1351static int
1352linuxthreads_wait (pid, ourstatus)
1353 int pid;
1354 struct target_waitstatus *ourstatus;
1355{
1356 int status;
1357 int rpid;
1358 int i;
1359 int last;
1360 int *wstatus;
1361
1362 if (linuxthreads_max && !linuxthreads_breakpoints_inserted)
1363 wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
1364
1365 /* See if the inferior has chosen values for its signals yet. By
1366 checking for them here, we can be sure we've updated GDB's signal
1367 handling table before the inferior ever gets one of them. (Well,
1368 before we notice, anyway.) */
1369 check_all_signal_numbers ();
1370
1371 for (;;)
1372 {
1373 if (!linuxthreads_max)
1374 rpid = 0;
1375 else if (!linuxthreads_breakpoints_inserted)
1376 {
1377 if (linuxthreads_inferior_pid)
1378 pid = linuxthreads_inferior_pid;
1379 else if (pid < 0)
1380 pid = inferior_pid;
1381 last = rpid = 0;
1382 }
1383 else if (pid < 0 && linuxthreads_wait_last >= 0)
1384 {
1385 status = linuxthreads_wait_status[linuxthreads_wait_last];
1386 rpid = linuxthreads_wait_pid[linuxthreads_wait_last--];
1387 }
1388 else if (pid > 0 && linuxthreads_pending_status (pid))
1389 {
1390 for (i = linuxthreads_wait_last; i >= 0; i--)
1391 if (linuxthreads_wait_pid[i] == pid)
1392 break;
1393 if (i < 0)
1394 rpid = 0;
1395 else
1396 {
1397 status = linuxthreads_wait_status[i];
1398 rpid = pid;
1399 if (i < linuxthreads_wait_last)
1400 {
1401 linuxthreads_wait_status[i] =
1402 linuxthreads_wait_status[linuxthreads_wait_last];
1403 linuxthreads_wait_pid[i] =
1404 linuxthreads_wait_pid[linuxthreads_wait_last];
1405 }
1406 linuxthreads_wait_last--;
1407 }
1408 }
1409 else
1410 rpid = 0;
1411
1412 if (rpid == 0)
1413 {
1414 int save_errno;
d4f3574e 1415
ed9a39eb 1416 save_errno = linux_child_wait (pid, &rpid, &status);
d4f3574e
SS
1417
1418 if (rpid == -1)
1419 {
1420 if (WIFEXITED(linuxthreads_exit_status))
1421 {
1422 store_waitstatus (ourstatus, linuxthreads_exit_status);
1423 return inferior_pid;
1424 }
1425 else
1426 {
1427 fprintf_unfiltered
1428 (gdb_stderr, "Child process unexpectedly missing: %s.\n",
1429 safe_strerror (save_errno));
1430 /* Claim it exited with unknown signal. */
1431 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1432 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1433 return -1;
1434 }
1435 }
1436
ed9a39eb
JM
1437 /* We have now gotten a new event from waitpid above. */
1438
1439 /* Signals arrive in any order. So get all signals until
1440 SIGTRAP and resend previous ones to be held after. */
d4f3574e
SS
1441 if (linuxthreads_max
1442 && !linuxthreads_breakpoints_inserted
1443 && WIFSTOPPED(status))
1444 if (WSTOPSIG(status) == SIGTRAP)
1445 {
1446 while (--last >= 0)
ed9a39eb
JM
1447 {
1448 kill (rpid, WSTOPSIG(wstatus[last]));
1449 }
d4f3574e
SS
1450
1451 /* insert negative zombie breakpoint */
1452 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1453 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1454 break;
1455 if (i > linuxthreads_breakpoint_last)
1456 {
1457 linuxthreads_breakpoint_zombie[i].pid = rpid;
1458 linuxthreads_breakpoint_last++;
1459 }
1460 linuxthreads_breakpoint_zombie[i].pc = read_pc_pid (rpid);
1461 linuxthreads_breakpoint_zombie[i].step = 1;
1462 }
1463 else
1464 {
1465 if (WSTOPSIG(status) != SIGSTOP)
1466 {
1467 for (i = 0; i < last; i++)
1468 if (wstatus[i] == status)
1469 break;
1470 if (i >= last)
ed9a39eb
JM
1471 {
1472 wstatus[last++] = status;
1473 }
d4f3574e
SS
1474 }
1475 child_resume (rpid, 1, TARGET_SIGNAL_0);
1476 continue;
1477 }
1478 if (linuxthreads_inferior_pid)
1479 linuxthreads_inferior_pid = 0;
1480 }
1481
1482 if (linuxthreads_max && !stop_soon_quietly)
1483 {
1484 if (linuxthreads_max
1485 && WIFSTOPPED(status)
1486 && WSTOPSIG(status) == SIGSTOP)
1487 {
1488 /* Skip SIGSTOP signals. */
1489 if (!linuxthreads_pending_status (rpid))
5c44784c
JM
1490 {
1491 if (linuxthreads_step_pid == rpid)
ed9a39eb
JM
1492 {
1493 child_resume (rpid, 1, linuxthreads_step_signo);
1494 }
5c44784c 1495 else
ed9a39eb
JM
1496 {
1497 child_resume (rpid, 0, TARGET_SIGNAL_0);
1498 }
5c44784c 1499 }
d4f3574e
SS
1500 continue;
1501 }
1502
1503 /* Do no report exit status of cloned threads. */
1504 if (WIFEXITED(status))
1505 {
1506 if (rpid == linuxthreads_initial_pid)
1507 linuxthreads_exit_status = status;
1508
1509 /* Remove any zombie breakpoint. */
1510 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1511 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1512 {
1513 REMOVE_BREAKPOINT_ZOMBIE(i);
1514 break;
1515 }
1516 if (pid > 0)
1517 pid = -1;
1518 continue;
1519 }
1520
1521 /* Deal with zombie breakpoint */
1522 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1523 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1524 break;
1525
1526 if (i <= linuxthreads_breakpoint_last)
1527 {
1528 /* There is a potential zombie breakpoint */
1529 if (WIFEXITED(status)
1530 || linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
1531 {
1532 /* The current pc is out of zombie breakpoint. */
1533 REMOVE_BREAKPOINT_ZOMBIE(i);
1534 }
1535 else if (!linuxthreads_breakpoint_zombie[i].step
1536 && WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
1537 {
1538 /* This is a real one ==> decrement PC and restart. */
1539 write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
1540 - DECR_PC_AFTER_BREAK, rpid);
1541 if (linuxthreads_step_pid == rpid)
ed9a39eb
JM
1542 {
1543 child_resume (rpid, 1, linuxthreads_step_signo);
1544 }
d4f3574e 1545 else
ed9a39eb
JM
1546 {
1547 child_resume (rpid, 0, TARGET_SIGNAL_0);
1548 }
d4f3574e
SS
1549 continue;
1550 }
1551 }
1552
1553 /* Walk through linuxthreads array in order to stop them */
1554 if (linuxthreads_breakpoints_inserted)
1555 update_stop_threads (rpid);
1556
1557 }
1558 else if (rpid != inferior_pid)
1559 continue;
1560
1561 store_waitstatus (ourstatus, status);
1562
1563 if (linuxthreads_attach_pending && !stop_soon_quietly)
1564 {
1565 int on = 1;
ed9a39eb
JM
1566 if (!using_thread_db)
1567 {
1568 target_write_memory (linuxthreads_debug,
1569 (char *) &on, sizeof (on));
1570 update_stop_threads (rpid);
1571 }
d4f3574e
SS
1572 linuxthreads_attach_pending = 0;
1573 }
1574
1575 if (linuxthreads_breakpoints_inserted
1576 && WIFSTOPPED(status)
1577 && WSTOPSIG(status) == SIGTRAP)
1578 linuxthreads_breakpoint_pid = rpid;
1579 else if (linuxthreads_breakpoint_pid)
1580 linuxthreads_breakpoint_pid = 0;
1581
1582 return rpid;
1583 }
1584}
1585
1586/* Fork an inferior process, and start debugging it with ptrace. */
1587
1588static void
1589linuxthreads_create_inferior (exec_file, allargs, env)
1590 char *exec_file;
1591 char *allargs;
1592 char **env;
1593{
1594 if (!exec_file && !exec_bfd)
1595 {
1596 error ("No executable file specified.\n\
1597Use the \"file\" or \"exec-file\" command.");
1598 return;
1599 }
1600
1601 push_target (&linuxthreads_ops);
1602 linuxthreads_breakpoints_inserted = 1;
1603 linuxthreads_breakpoint_last = -1;
1604 linuxthreads_wait_last = -1;
1605 linuxthreads_exit_status = __W_STOPCODE(0);
1606
1607 if (linuxthreads_max)
1608 linuxthreads_attach_pending = 1;
1609
1610 child_ops.to_create_inferior (exec_file, allargs, env);
1611}
1612
ed9a39eb
JM
1613void
1614linuxthreads_discard_global_state ()
1615{
1616 linuxthreads_inferior_pid = 0;
1617 linuxthreads_breakpoint_pid = 0;
1618 linuxthreads_step_pid = 0;
1619 linuxthreads_step_signo = TARGET_SIGNAL_0;
1620 linuxthreads_manager_pid = 0;
1621 linuxthreads_initial_pid = 0;
1622 linuxthreads_attach_pending = 0;
1623 linuxthreads_max = 0;
1624}
1625
d4f3574e
SS
1626/* Clean up after the inferior dies. */
1627
1628static void
1629linuxthreads_mourn_inferior ()
1630{
1631 if (linuxthreads_max)
1632 {
1633 int off = 0;
1634 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1635
ed9a39eb 1636 linuxthreads_discard_global_state ();
d4f3574e
SS
1637 init_thread_list(); /* Destroy thread info */
1638 }
1639
1640 child_ops.to_mourn_inferior ();
1641
1642 unpush_target (&linuxthreads_ops);
1643}
1644
1645/* Kill the inferior process */
1646
1647static void
1648linuxthreads_kill ()
1649{
1650 int rpid;
1651 int status;
1652
1653 if (inferior_pid == 0)
1654 return;
1655
1656 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1657 {
1658 /* Remove all threads status. */
1659 inferior_pid = linuxthreads_manager_pid;
1660 iterate_active_threads (kill_thread, 1);
1661 }
1662
1663 kill_thread (inferior_pid);
1664
1665#if 0
1666 /* doing_quit_force solves a real problem, but I think a properly
1667 placed call to catch_errors would do the trick much more cleanly. */
1668 if (doing_quit_force >= 0)
1669 {
1670 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1671 {
1672 /* Wait for thread to complete */
1673 while ((rpid = waitpid (-1, &status, __WCLONE)) > 0)
1674 if (!WIFEXITED(status))
1675 kill_thread (rpid);
1676
1677 while ((rpid = waitpid (-1, &status, 0)) > 0)
1678 if (!WIFEXITED(status))
1679 kill_thread (rpid);
1680 }
1681 else
1682 while ((rpid = waitpid (inferior_pid, &status, 0)) > 0)
1683 if (!WIFEXITED(status))
1684 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
1685 }
1686#endif
1687
1688 /* Wait for all threads. */
1689 do
ed9a39eb
JM
1690 {
1691 rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
1692 }
d4f3574e 1693 while (rpid > 0 || errno == EINTR);
ed9a39eb 1694 /* FIXME: should no longer need to handle EINTR here. */
d4f3574e
SS
1695
1696 do
ed9a39eb
JM
1697 {
1698 rpid = waitpid (-1, &status, WNOHANG);
1699 }
d4f3574e 1700 while (rpid > 0 || errno == EINTR);
ed9a39eb 1701 /* FIXME: should no longer need to handle EINTR here. */
d4f3574e
SS
1702
1703 linuxthreads_mourn_inferior ();
1704}
1705
1706/* Insert a breakpoint */
1707
1708static int
1709linuxthreads_insert_breakpoint (addr, contents_cache)
1710 CORE_ADDR addr;
1711 char *contents_cache;
1712{
1713 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1714 {
1715 linuxthreads_breakpoint_addr = addr;
1716 iterate_active_threads (insert_breakpoint, 1);
1717 insert_breakpoint (linuxthreads_manager_pid);
1718 }
1719
1720 return child_ops.to_insert_breakpoint (addr, contents_cache);
1721}
1722
1723/* Remove a breakpoint */
1724
1725static int
1726linuxthreads_remove_breakpoint (addr, contents_cache)
1727 CORE_ADDR addr;
1728 char *contents_cache;
1729{
1730 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1731 {
1732 linuxthreads_breakpoint_addr = addr;
1733 iterate_active_threads (remove_breakpoint, 1);
1734 remove_breakpoint (linuxthreads_manager_pid);
1735 }
1736
1737 return child_ops.to_remove_breakpoint (addr, contents_cache);
1738}
1739
1740/* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1741
1742static int
1743linuxthreads_can_run ()
1744{
1745 return child_suppress_run;
1746}
ed9a39eb 1747
d4f3574e
SS
1748\f
1749static void
1750init_linuxthreads_ops ()
1751{
1752 linuxthreads_ops.to_shortname = "linuxthreads";
1753 linuxthreads_ops.to_longname = "LINUX threads and pthread.";
1754 linuxthreads_ops.to_doc = "LINUX threads and pthread support.";
1755 linuxthreads_ops.to_attach = linuxthreads_attach;
1756 linuxthreads_ops.to_detach = linuxthreads_detach;
1757 linuxthreads_ops.to_resume = linuxthreads_resume;
1758 linuxthreads_ops.to_wait = linuxthreads_wait;
1759 linuxthreads_ops.to_kill = linuxthreads_kill;
1760 linuxthreads_ops.to_can_run = linuxthreads_can_run;
1761 linuxthreads_ops.to_stratum = thread_stratum;
1762 linuxthreads_ops.to_insert_breakpoint = linuxthreads_insert_breakpoint;
1763 linuxthreads_ops.to_remove_breakpoint = linuxthreads_remove_breakpoint;
1764 linuxthreads_ops.to_create_inferior = linuxthreads_create_inferior;
1765 linuxthreads_ops.to_mourn_inferior = linuxthreads_mourn_inferior;
1766 linuxthreads_ops.to_thread_alive = linuxthreads_thread_alive;
ed9a39eb 1767 linuxthreads_ops.to_pid_to_str = linuxthreads_pid_to_str;
d4f3574e
SS
1768 linuxthreads_ops.to_magic = OPS_MAGIC;
1769}
1770
1771void
1772_initialize_linuxthreads ()
1773{
1774 struct sigaction sact;
ed9a39eb 1775 sigset_t linuxthreads_wait_mask; /* sigset with SIGCHLD */
d4f3574e
SS
1776
1777 init_linuxthreads_ops ();
1778 add_target (&linuxthreads_ops);
1779 child_suppress_run = 1;
1780
11cf8741
JM
1781 /* Hook onto the "new_objfile" event.
1782 * If someone else is already hooked onto the event,
1783 * then make sure he will be called after we are.
1784 */
1785 target_new_objfile_chain = target_new_objfile_hook;
1786 target_new_objfile_hook = linuxthreads_new_objfile;
1787
d4f3574e
SS
1788 /* Attach SIGCHLD handler */
1789 sact.sa_handler = sigchld_handler;
1790 sigemptyset (&sact.sa_mask);
1791 sact.sa_flags = 0;
1792 sigaction (SIGCHLD, &sact, NULL);
1793
1794 /* initialize SIGCHLD mask */
1795 sigemptyset (&linuxthreads_wait_mask);
1796 sigaddset (&linuxthreads_wait_mask, SIGCHLD);
ed9a39eb
JM
1797
1798 /* Use SIG_BLOCK to block receipt of SIGCHLD.
1799 The block_mask will allow us to wait for this signal explicitly. */
1800 sigprocmask(SIG_BLOCK,
1801 &linuxthreads_wait_mask,
1802 &linuxthreads_block_mask);
d4f3574e 1803}
This page took 0.134881 seconds and 4 git commands to generate.