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