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