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