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