* symfile.c (compare_psymbols, compare_symbols): Declare using
[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.
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
1043 default_prepare_to_proceed() has been added. */
d4f3574e
SS
1044
1045int
fba45db2 1046linuxthreads_prepare_to_proceed (int step)
d4f3574e
SS
1047{
1048 if (!linuxthreads_max
1049 || !linuxthreads_manager_pid
1050 || !linuxthreads_breakpoint_pid
39f77062
KB
1051 || !breakpoint_here_p (
1052 read_pc_pid (pid_to_ptid (linuxthreads_breakpoint_pid))))
d4f3574e
SS
1053 return 0;
1054
1055 if (step)
1056 {
1057 /* Mark the current inferior as single stepping process. */
39f77062 1058 linuxthreads_step_pid = PIDGET (inferior_ptid);
d4f3574e
SS
1059 }
1060
1061 linuxthreads_inferior_pid = linuxthreads_breakpoint_pid;
1062 return linuxthreads_breakpoint_pid;
1063}
1064
1065/* Convert a pid to printable form. */
1066
1067char *
39f77062 1068linuxthreads_pid_to_str (ptid_t ptid)
d4f3574e
SS
1069{
1070 static char buf[100];
39f77062 1071 int pid = PIDGET (ptid);
d4f3574e
SS
1072
1073 sprintf (buf, "%s %d%s", linuxthreads_max ? "Thread" : "Pid", pid,
1074 (pid == linuxthreads_manager_pid) ? " (manager thread)"
1075 : (pid == linuxthreads_initial_pid) ? " (initial thread)"
1076 : "");
1077
1078 return buf;
1079}
1080
1081/* Attach to process PID, then initialize for debugging it
1082 and wait for the trace-trap that results from attaching. */
1083
1084static void
fba45db2 1085linuxthreads_attach (char *args, int from_tty)
d4f3574e
SS
1086{
1087 if (!args)
1088 error_no_arg ("process-id to attach");
1089
1090 push_target (&linuxthreads_ops);
1091 linuxthreads_breakpoints_inserted = 1;
1092 linuxthreads_breakpoint_last = -1;
1093 linuxthreads_wait_last = -1;
67aaefa2 1094 WSETSTOP (linuxthreads_exit_status, 0);
d4f3574e
SS
1095
1096 child_ops.to_attach (args, from_tty);
1097
1098 if (linuxthreads_max)
1099 linuxthreads_attach_pending = 1;
1100}
1101
1102/* Take a program previously attached to and detaches it.
1103 The program resumes execution and will no longer stop
1104 on signals, etc. We'd better not have left any breakpoints
1105 in the program or it'll die when it hits one. For this
1106 to work, it may be necessary for the process to have been
1107 previously attached. It *might* work if the program was
1108 started via the normal ptrace (PTRACE_TRACEME). */
1109
1110static void
fba45db2 1111linuxthreads_detach (char *args, int from_tty)
d4f3574e
SS
1112{
1113 if (linuxthreads_max)
1114 {
1115 int i;
1116 int pid;
1117 int off = 0;
1118 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1119
1120 /* Walk through linuxthreads array in order to detach known threads. */
1121 if (linuxthreads_manager_pid != 0)
1122 {
1123 /* Get rid of all positive zombie breakpoints. */
1124 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1125 {
1126 if (linuxthreads_breakpoint_zombie[i].step)
1127 continue;
1128
1129 pid = linuxthreads_breakpoint_zombie[i].pid;
39f77062 1130 if (!linuxthreads_thread_alive (pid_to_ptid (pid)))
d4f3574e
SS
1131 continue;
1132
39f77062
KB
1133 if (linuxthreads_breakpoint_zombie[i].pc
1134 != read_pc_pid (pid_to_ptid (pid)))
d4f3574e
SS
1135 continue;
1136
1137 /* Continue in STEP mode until the thread pc has moved or
1138 until SIGTRAP is found on the same PC. */
1139 if (linuxthreads_find_trap (pid, 0)
39f77062
KB
1140 && linuxthreads_breakpoint_zombie[i].pc
1141 == read_pc_pid (pid_to_ptid (pid)))
d4f3574e 1142 write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
39f77062 1143 - DECR_PC_AFTER_BREAK, pid_to_ptid (pid));
d4f3574e
SS
1144 }
1145
1146 /* Detach thread after thread. */
39f77062 1147 inferior_ptid = pid_to_ptid (linuxthreads_manager_pid);
d4f3574e
SS
1148 iterate_active_threads (detach_thread, 1);
1149
1150 /* Remove pending SIGTRAP and SIGSTOP */
39f77062 1151 linuxthreads_find_trap (PIDGET (inferior_ptid), 1);
d4f3574e
SS
1152
1153 linuxthreads_wait_last = -1;
67aaefa2 1154 WSETSTOP (linuxthreads_exit_status, 0);
d4f3574e
SS
1155 }
1156
1157 linuxthreads_inferior_pid = 0;
1158 linuxthreads_breakpoint_pid = 0;
1159 linuxthreads_step_pid = 0;
1160 linuxthreads_step_signo = TARGET_SIGNAL_0;
1161 linuxthreads_manager_pid = 0;
1162 linuxthreads_initial_pid = 0;
1163 linuxthreads_attach_pending = 0;
1164 init_thread_list (); /* Destroy thread info */
1165 }
1166
1167 child_ops.to_detach (args, from_tty);
1168
1169 unpush_target (&linuxthreads_ops);
1170}
1171
1172/* Resume execution of process PID. If STEP is nozero, then
1173 just single step it. If SIGNAL is nonzero, restart it with that
1174 signal activated. */
1175
1176static void
39f77062 1177linuxthreads_resume (ptid_t ptid, int step, enum target_signal signo)
d4f3574e
SS
1178{
1179 if (!linuxthreads_max || stop_soon_quietly || linuxthreads_manager_pid == 0)
ed9a39eb 1180 {
39f77062 1181 child_ops.to_resume (ptid, step, signo);
ed9a39eb 1182 }
d4f3574e
SS
1183 else
1184 {
1185 int rpid;
1186 if (linuxthreads_inferior_pid)
1187 {
1188 /* Prepare resume of the last thread that hit a breakpoint */
1189 linuxthreads_breakpoints_inserted = 0;
1190 rpid = linuxthreads_inferior_pid;
1191 linuxthreads_step_signo = signo;
1192 }
1193 else
1194 {
1195 struct cleanup *old_chain = NULL;
1196 int i;
1197
39f77062 1198 if (PIDGET (ptid) < 0)
d4f3574e 1199 {
39f77062 1200 linuxthreads_step_pid = step ? PIDGET (inferior_ptid) : 0;
d4f3574e 1201 linuxthreads_step_signo = signo;
39f77062 1202 rpid = PIDGET (inferior_ptid);
d4f3574e
SS
1203 }
1204 else
39f77062 1205 rpid = PIDGET (ptid);
d4f3574e 1206
39f77062 1207 if (PIDGET (ptid) < 0 || !step)
d4f3574e
SS
1208 {
1209 linuxthreads_breakpoints_inserted = 1;
1210
1211 /* Walk through linuxthreads array in order to resume threads */
39f77062 1212 if (PIDGET (ptid) >= 0 && !ptid_equal (inferior_ptid, ptid))
d4f3574e 1213 {
39f77062
KB
1214 old_chain = save_inferior_ptid ();
1215 inferior_ptid = ptid;
d4f3574e
SS
1216 }
1217
1218 iterate_active_threads (resume_thread, 0);
39f77062 1219 if (linuxthreads_manager_pid != PIDGET (inferior_ptid)
d4f3574e
SS
1220 && !linuxthreads_pending_status (linuxthreads_manager_pid))
1221 resume_thread (linuxthreads_manager_pid);
1222 }
1223 else
1224 linuxthreads_breakpoints_inserted = 0;
1225
1226 /* Deal with zombie breakpoint */
1227 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1228 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1229 {
39f77062
KB
1230 if (linuxthreads_breakpoint_zombie[i].pc
1231 != read_pc_pid (pid_to_ptid (rpid)))
d4f3574e
SS
1232 {
1233 /* The current pc is out of zombie breakpoint. */
1234 REMOVE_BREAKPOINT_ZOMBIE(i);
1235 }
1236 break;
1237 }
1238
1239 if (old_chain != NULL)
1240 do_cleanups (old_chain);
1241 }
1242
1243 /* Resume initial thread. */
ed9a39eb 1244 /* [unles it has a wait event pending] */
d4f3574e 1245 if (!linuxthreads_pending_status (rpid))
ed9a39eb 1246 {
39f77062 1247 child_ops.to_resume (pid_to_ptid (rpid), step, signo);
ed9a39eb 1248 }
d4f3574e
SS
1249 }
1250}
1251
ed9a39eb
JM
1252/* Abstract out the child_wait functionality. */
1253int
fba45db2 1254linux_child_wait (int pid, int *rpid, int *status)
ed9a39eb
JM
1255{
1256 int save_errno;
1257
1258 /* Note: inftarg has these inside the loop. */
1259 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1260 attached process. */
1261 set_sigio_trap ();
1262
1263 errno = save_errno = 0;
1264 for (;;)
1265 {
1266 errno = 0;
1267 *rpid = waitpid (pid, status, __WCLONE | WNOHANG);
1268 save_errno = errno;
1269
1270 if (*rpid > 0)
1271 {
1272 /* Got an event -- break out */
1273 break;
1274 }
1275 if (errno == EINTR) /* interrupted by signal, try again */
1276 {
1277 continue;
1278 }
1279
1280 errno = 0;
1281 *rpid = waitpid (pid, status, WNOHANG);
1282 if (*rpid > 0)
1283 {
1284 /* Got an event -- break out */
1285 break;
1286 }
1287 if (errno == EINTR)
1288 {
1289 continue;
1290 }
1291 if (errno != 0 && save_errno != 0)
1292 {
1293 break;
1294 }
1295 sigsuspend(&linuxthreads_block_mask);
1296 }
1297 clear_sigio_trap ();
1298 clear_sigint_trap ();
1299
1300 return errno ? errno : save_errno;
1301}
1302
1303
d4f3574e
SS
1304/* Wait for any threads to stop. We may have to convert PID from a thread id
1305 to a LWP id, and vice versa on the way out. */
1306
39f77062
KB
1307static ptid_t
1308linuxthreads_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
d4f3574e
SS
1309{
1310 int status;
1311 int rpid;
1312 int i;
1313 int last;
1314 int *wstatus;
39f77062 1315 int pid = PIDGET (ptid);
d4f3574e
SS
1316
1317 if (linuxthreads_max && !linuxthreads_breakpoints_inserted)
1318 wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
1319
1320 /* See if the inferior has chosen values for its signals yet. By
1321 checking for them here, we can be sure we've updated GDB's signal
1322 handling table before the inferior ever gets one of them. (Well,
1323 before we notice, anyway.) */
1324 check_all_signal_numbers ();
1325
1326 for (;;)
1327 {
1328 if (!linuxthreads_max)
1329 rpid = 0;
1330 else if (!linuxthreads_breakpoints_inserted)
1331 {
1332 if (linuxthreads_inferior_pid)
1333 pid = linuxthreads_inferior_pid;
1334 else if (pid < 0)
39f77062 1335 pid = PIDGET (inferior_ptid);
d4f3574e
SS
1336 last = rpid = 0;
1337 }
1338 else if (pid < 0 && linuxthreads_wait_last >= 0)
1339 {
1340 status = linuxthreads_wait_status[linuxthreads_wait_last];
1341 rpid = linuxthreads_wait_pid[linuxthreads_wait_last--];
1342 }
1343 else if (pid > 0 && linuxthreads_pending_status (pid))
1344 {
1345 for (i = linuxthreads_wait_last; i >= 0; i--)
1346 if (linuxthreads_wait_pid[i] == pid)
1347 break;
1348 if (i < 0)
1349 rpid = 0;
1350 else
1351 {
1352 status = linuxthreads_wait_status[i];
1353 rpid = pid;
1354 if (i < linuxthreads_wait_last)
1355 {
1356 linuxthreads_wait_status[i] =
1357 linuxthreads_wait_status[linuxthreads_wait_last];
1358 linuxthreads_wait_pid[i] =
1359 linuxthreads_wait_pid[linuxthreads_wait_last];
1360 }
1361 linuxthreads_wait_last--;
1362 }
1363 }
1364 else
1365 rpid = 0;
1366
1367 if (rpid == 0)
1368 {
1369 int save_errno;
d4f3574e 1370
ed9a39eb 1371 save_errno = linux_child_wait (pid, &rpid, &status);
d4f3574e
SS
1372
1373 if (rpid == -1)
1374 {
1375 if (WIFEXITED(linuxthreads_exit_status))
1376 {
1377 store_waitstatus (ourstatus, linuxthreads_exit_status);
39f77062 1378 return inferior_ptid;
d4f3574e
SS
1379 }
1380 else
1381 {
1382 fprintf_unfiltered
1383 (gdb_stderr, "Child process unexpectedly missing: %s.\n",
1384 safe_strerror (save_errno));
1385 /* Claim it exited with unknown signal. */
1386 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1387 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
39f77062 1388 return pid_to_ptid (-1);
d4f3574e
SS
1389 }
1390 }
1391
ed9a39eb
JM
1392 /* We have now gotten a new event from waitpid above. */
1393
1394 /* Signals arrive in any order. So get all signals until
1395 SIGTRAP and resend previous ones to be held after. */
d4f3574e
SS
1396 if (linuxthreads_max
1397 && !linuxthreads_breakpoints_inserted
1398 && WIFSTOPPED(status))
1399 if (WSTOPSIG(status) == SIGTRAP)
1400 {
1401 while (--last >= 0)
ed9a39eb
JM
1402 {
1403 kill (rpid, WSTOPSIG(wstatus[last]));
1404 }
d4f3574e
SS
1405
1406 /* insert negative zombie breakpoint */
1407 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1408 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1409 break;
1410 if (i > linuxthreads_breakpoint_last)
1411 {
1412 linuxthreads_breakpoint_zombie[i].pid = rpid;
1413 linuxthreads_breakpoint_last++;
1414 }
39f77062
KB
1415 linuxthreads_breakpoint_zombie[i].pc
1416 = read_pc_pid (pid_to_ptid (rpid));
d4f3574e
SS
1417 linuxthreads_breakpoint_zombie[i].step = 1;
1418 }
1419 else
1420 {
1421 if (WSTOPSIG(status) != SIGSTOP)
1422 {
1423 for (i = 0; i < last; i++)
1424 if (wstatus[i] == status)
1425 break;
1426 if (i >= last)
ed9a39eb
JM
1427 {
1428 wstatus[last++] = status;
1429 }
d4f3574e 1430 }
39f77062 1431 child_resume (pid_to_ptid (rpid), 1, TARGET_SIGNAL_0);
d4f3574e
SS
1432 continue;
1433 }
1434 if (linuxthreads_inferior_pid)
1435 linuxthreads_inferior_pid = 0;
1436 }
1437
1438 if (linuxthreads_max && !stop_soon_quietly)
1439 {
1440 if (linuxthreads_max
1441 && WIFSTOPPED(status)
1442 && WSTOPSIG(status) == SIGSTOP)
1443 {
1444 /* Skip SIGSTOP signals. */
1445 if (!linuxthreads_pending_status (rpid))
5c44784c
JM
1446 {
1447 if (linuxthreads_step_pid == rpid)
ed9a39eb 1448 {
39f77062
KB
1449 child_resume (pid_to_ptid (rpid), 1,
1450 linuxthreads_step_signo);
ed9a39eb 1451 }
5c44784c 1452 else
ed9a39eb 1453 {
39f77062 1454 child_resume (pid_to_ptid (rpid), 0, TARGET_SIGNAL_0);
ed9a39eb 1455 }
5c44784c 1456 }
d4f3574e
SS
1457 continue;
1458 }
1459
1460 /* Do no report exit status of cloned threads. */
1461 if (WIFEXITED(status))
1462 {
1463 if (rpid == linuxthreads_initial_pid)
1464 linuxthreads_exit_status = status;
1465
1466 /* Remove any zombie breakpoint. */
1467 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1468 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1469 {
1470 REMOVE_BREAKPOINT_ZOMBIE(i);
1471 break;
1472 }
1473 if (pid > 0)
1474 pid = -1;
1475 continue;
1476 }
1477
1478 /* Deal with zombie breakpoint */
1479 for (i = 0; i <= linuxthreads_breakpoint_last; i++)
1480 if (linuxthreads_breakpoint_zombie[i].pid == rpid)
1481 break;
1482
1483 if (i <= linuxthreads_breakpoint_last)
1484 {
1485 /* There is a potential zombie breakpoint */
1486 if (WIFEXITED(status)
39f77062
KB
1487 || linuxthreads_breakpoint_zombie[i].pc
1488 != read_pc_pid (pid_to_ptid (rpid)))
d4f3574e
SS
1489 {
1490 /* The current pc is out of zombie breakpoint. */
1491 REMOVE_BREAKPOINT_ZOMBIE(i);
1492 }
1493 else if (!linuxthreads_breakpoint_zombie[i].step
1494 && WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
1495 {
1496 /* This is a real one ==> decrement PC and restart. */
1497 write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
39f77062 1498 - DECR_PC_AFTER_BREAK, pid_to_ptid (rpid));
d4f3574e 1499 if (linuxthreads_step_pid == rpid)
ed9a39eb 1500 {
39f77062 1501 child_resume (pid_to_ptid (rpid), 1, linuxthreads_step_signo);
ed9a39eb 1502 }
d4f3574e 1503 else
ed9a39eb 1504 {
39f77062 1505 child_resume (pid_to_ptid (rpid), 0, TARGET_SIGNAL_0);
ed9a39eb 1506 }
d4f3574e
SS
1507 continue;
1508 }
1509 }
1510
1511 /* Walk through linuxthreads array in order to stop them */
1512 if (linuxthreads_breakpoints_inserted)
1513 update_stop_threads (rpid);
1514
1515 }
39f77062 1516 else if (rpid != PIDGET (inferior_ptid))
d4f3574e
SS
1517 continue;
1518
1519 store_waitstatus (ourstatus, status);
1520
1521 if (linuxthreads_attach_pending && !stop_soon_quietly)
1522 {
1523 int on = 1;
ed9a39eb
JM
1524 if (!using_thread_db)
1525 {
1526 target_write_memory (linuxthreads_debug,
1527 (char *) &on, sizeof (on));
1528 update_stop_threads (rpid);
1529 }
d4f3574e
SS
1530 linuxthreads_attach_pending = 0;
1531 }
1532
1533 if (linuxthreads_breakpoints_inserted
1534 && WIFSTOPPED(status)
1535 && WSTOPSIG(status) == SIGTRAP)
1536 linuxthreads_breakpoint_pid = rpid;
1537 else if (linuxthreads_breakpoint_pid)
1538 linuxthreads_breakpoint_pid = 0;
1539
39f77062 1540 return pid_to_ptid (rpid);
d4f3574e
SS
1541 }
1542}
1543
1544/* Fork an inferior process, and start debugging it with ptrace. */
1545
1546static void
fba45db2 1547linuxthreads_create_inferior (char *exec_file, char *allargs, char **env)
d4f3574e
SS
1548{
1549 if (!exec_file && !exec_bfd)
1550 {
1551 error ("No executable file specified.\n\
1552Use the \"file\" or \"exec-file\" command.");
1553 return;
1554 }
1555
1556 push_target (&linuxthreads_ops);
1557 linuxthreads_breakpoints_inserted = 1;
1558 linuxthreads_breakpoint_last = -1;
1559 linuxthreads_wait_last = -1;
67aaefa2 1560 WSETSTOP (linuxthreads_exit_status, 0);
d4f3574e
SS
1561
1562 if (linuxthreads_max)
1563 linuxthreads_attach_pending = 1;
1564
1565 child_ops.to_create_inferior (exec_file, allargs, env);
1566}
1567
ed9a39eb 1568void
fba45db2 1569linuxthreads_discard_global_state (void)
ed9a39eb
JM
1570{
1571 linuxthreads_inferior_pid = 0;
1572 linuxthreads_breakpoint_pid = 0;
1573 linuxthreads_step_pid = 0;
1574 linuxthreads_step_signo = TARGET_SIGNAL_0;
1575 linuxthreads_manager_pid = 0;
1576 linuxthreads_initial_pid = 0;
1577 linuxthreads_attach_pending = 0;
1578 linuxthreads_max = 0;
1579}
1580
d4f3574e
SS
1581/* Clean up after the inferior dies. */
1582
1583static void
fba45db2 1584linuxthreads_mourn_inferior (void)
d4f3574e
SS
1585{
1586 if (linuxthreads_max)
1587 {
1588 int off = 0;
1589 target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
1590
ed9a39eb 1591 linuxthreads_discard_global_state ();
d4f3574e
SS
1592 init_thread_list(); /* Destroy thread info */
1593 }
1594
1595 child_ops.to_mourn_inferior ();
1596
1597 unpush_target (&linuxthreads_ops);
1598}
1599
1600/* Kill the inferior process */
1601
1602static void
fba45db2 1603linuxthreads_kill (void)
d4f3574e
SS
1604{
1605 int rpid;
1606 int status;
1607
39f77062 1608 if (PIDGET (inferior_ptid) == 0)
d4f3574e
SS
1609 return;
1610
1611 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1612 {
1613 /* Remove all threads status. */
39f77062 1614 inferior_ptid = pid_to_ptid (linuxthreads_manager_pid);
d4f3574e
SS
1615 iterate_active_threads (kill_thread, 1);
1616 }
1617
39f77062 1618 kill_thread (PIDGET (inferior_ptid));
d4f3574e
SS
1619
1620#if 0
1621 /* doing_quit_force solves a real problem, but I think a properly
1622 placed call to catch_errors would do the trick much more cleanly. */
1623 if (doing_quit_force >= 0)
1624 {
1625 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1626 {
1627 /* Wait for thread to complete */
1628 while ((rpid = waitpid (-1, &status, __WCLONE)) > 0)
1629 if (!WIFEXITED(status))
1630 kill_thread (rpid);
1631
1632 while ((rpid = waitpid (-1, &status, 0)) > 0)
1633 if (!WIFEXITED(status))
1634 kill_thread (rpid);
1635 }
1636 else
39f77062 1637 while ((rpid = waitpid (PIDGET (inferior_ptid), &status, 0)) > 0)
d4f3574e 1638 if (!WIFEXITED(status))
39f77062 1639 ptrace (PT_KILL, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) 0, 0);
d4f3574e
SS
1640 }
1641#endif
1642
1643 /* Wait for all threads. */
1644 do
ed9a39eb
JM
1645 {
1646 rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
1647 }
d4f3574e 1648 while (rpid > 0 || errno == EINTR);
ed9a39eb 1649 /* FIXME: should no longer need to handle EINTR here. */
d4f3574e
SS
1650
1651 do
ed9a39eb
JM
1652 {
1653 rpid = waitpid (-1, &status, WNOHANG);
1654 }
d4f3574e 1655 while (rpid > 0 || errno == EINTR);
ed9a39eb 1656 /* FIXME: should no longer need to handle EINTR here. */
d4f3574e
SS
1657
1658 linuxthreads_mourn_inferior ();
1659}
1660
1661/* Insert a breakpoint */
1662
1663static int
fba45db2 1664linuxthreads_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
d4f3574e
SS
1665{
1666 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1667 {
1668 linuxthreads_breakpoint_addr = addr;
1669 iterate_active_threads (insert_breakpoint, 1);
1670 insert_breakpoint (linuxthreads_manager_pid);
1671 }
1672
1673 return child_ops.to_insert_breakpoint (addr, contents_cache);
1674}
1675
1676/* Remove a breakpoint */
1677
1678static int
fba45db2 1679linuxthreads_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
d4f3574e
SS
1680{
1681 if (linuxthreads_max && linuxthreads_manager_pid != 0)
1682 {
1683 linuxthreads_breakpoint_addr = addr;
1684 iterate_active_threads (remove_breakpoint, 1);
1685 remove_breakpoint (linuxthreads_manager_pid);
1686 }
1687
1688 return child_ops.to_remove_breakpoint (addr, contents_cache);
1689}
1690
1691/* Mark our target-struct as eligible for stray "run" and "attach" commands. */
1692
1693static int
fba45db2 1694linuxthreads_can_run (void)
d4f3574e
SS
1695{
1696 return child_suppress_run;
1697}
ed9a39eb 1698
d4f3574e
SS
1699\f
1700static void
fba45db2 1701init_linuxthreads_ops (void)
d4f3574e
SS
1702{
1703 linuxthreads_ops.to_shortname = "linuxthreads";
1704 linuxthreads_ops.to_longname = "LINUX threads and pthread.";
1705 linuxthreads_ops.to_doc = "LINUX threads and pthread support.";
1706 linuxthreads_ops.to_attach = linuxthreads_attach;
1707 linuxthreads_ops.to_detach = linuxthreads_detach;
1708 linuxthreads_ops.to_resume = linuxthreads_resume;
1709 linuxthreads_ops.to_wait = linuxthreads_wait;
1710 linuxthreads_ops.to_kill = linuxthreads_kill;
1711 linuxthreads_ops.to_can_run = linuxthreads_can_run;
1712 linuxthreads_ops.to_stratum = thread_stratum;
1713 linuxthreads_ops.to_insert_breakpoint = linuxthreads_insert_breakpoint;
1714 linuxthreads_ops.to_remove_breakpoint = linuxthreads_remove_breakpoint;
1715 linuxthreads_ops.to_create_inferior = linuxthreads_create_inferior;
1716 linuxthreads_ops.to_mourn_inferior = linuxthreads_mourn_inferior;
1717 linuxthreads_ops.to_thread_alive = linuxthreads_thread_alive;
ed9a39eb 1718 linuxthreads_ops.to_pid_to_str = linuxthreads_pid_to_str;
d4f3574e
SS
1719 linuxthreads_ops.to_magic = OPS_MAGIC;
1720}
1721
1722void
fba45db2 1723_initialize_linuxthreads (void)
d4f3574e
SS
1724{
1725 struct sigaction sact;
ed9a39eb 1726 sigset_t linuxthreads_wait_mask; /* sigset with SIGCHLD */
d4f3574e
SS
1727
1728 init_linuxthreads_ops ();
1729 add_target (&linuxthreads_ops);
1730 child_suppress_run = 1;
1731
11cf8741
JM
1732 /* Hook onto the "new_objfile" event.
1733 * If someone else is already hooked onto the event,
1734 * then make sure he will be called after we are.
1735 */
1736 target_new_objfile_chain = target_new_objfile_hook;
1737 target_new_objfile_hook = linuxthreads_new_objfile;
1738
d4f3574e
SS
1739 /* Attach SIGCHLD handler */
1740 sact.sa_handler = sigchld_handler;
1741 sigemptyset (&sact.sa_mask);
1742 sact.sa_flags = 0;
1743 sigaction (SIGCHLD, &sact, NULL);
1744
1745 /* initialize SIGCHLD mask */
1746 sigemptyset (&linuxthreads_wait_mask);
1747 sigaddset (&linuxthreads_wait_mask, SIGCHLD);
ed9a39eb
JM
1748
1749 /* Use SIG_BLOCK to block receipt of SIGCHLD.
1750 The block_mask will allow us to wait for this signal explicitly. */
1751 sigprocmask(SIG_BLOCK,
1752 &linuxthreads_wait_mask,
1753 &linuxthreads_block_mask);
0fda6bd2
JM
1754 /* Make sure that linuxthreads_block_mask is not blocking SIGCHLD */
1755 sigdelset (&linuxthreads_block_mask, SIGCHLD);
d4f3574e 1756}
This page took 0.162479 seconds and 4 git commands to generate.