Implement attach/detach for multi-threaded programs on Linux.
[binutils-gdb.git] / gdb / lin-lwp.c
1 /* Multi-threaded debugging support for Linux (LWP layer).
2 Copyright 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22
23 #include "gdb_assert.h"
24 #include <errno.h>
25 #include <signal.h>
26 #include <sys/ptrace.h>
27 #include "gdb_wait.h"
28
29 #include "gdbthread.h"
30 #include "inferior.h"
31 #include "target.h"
32 #include "regcache.h"
33 #include "gdbcmd.h"
34
35 static int debug_lin_lwp;
36 extern const char *strsignal (int sig);
37
38 /* On Linux there are no real LWP's. The closest thing to LWP's are
39 processes sharing the same VM space. A multi-threaded process is
40 basically a group of such processes. However, such a grouping is
41 almost entirely a user-space issue; the kernel doesn't enforce such
42 a grouping at all (this might change in the future). In general,
43 we'll rely on the threads library (i.e. the LinuxThreads library)
44 to provide such a grouping.
45
46 It is perfectly well possible to write a multi-threaded application
47 without the assistance of a threads library, by using the clone
48 system call directly. This module should be able to give some
49 rudimentary support for debugging such applications if developers
50 specify the CLONE_PTRACE flag in the clone system call, and are
51 using Linux 2.4 or above.
52
53 Note that there are some peculiarities in Linux that affect this
54 code:
55
56 - In general one should specify the __WCLONE flag to waitpid in
57 order to make it report events for any of the cloned processes
58 (and leave it out for the initial process). However, if a cloned
59 process has exited the exit status is only reported if the
60 __WCLONE flag is absent. Linux 2.4 has a __WALL flag, but we
61 cannot use it since GDB must work on older systems too.
62
63 - When a traced, cloned process exits and is waited for by the
64 debugger, the kernel reassigns it to the original parent and
65 keeps it around as a "zombie". Somehow, the LinuxThreads library
66 doesn't notice this, which leads to the "zombie problem": When
67 debugged a multi-threaded process that spawns a lot of threads
68 will run out of processes, even if the threads exit, because the
69 "zombies" stay around. */
70
71 /* Structure describing a LWP. */
72 struct lwp_info
73 {
74 /* The process id of the LWP. This is a combination of the LWP id
75 and overall process id. */
76 ptid_t ptid;
77
78 /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report
79 it back yet). */
80 int signalled;
81
82 /* Non-zero if this LWP is stopped. */
83 int stopped;
84
85 /* If non-zero, a pending wait status. */
86 int status;
87
88 /* Non-zero if we were stepping this LWP. */
89 int step;
90
91 /* Next LWP in list. */
92 struct lwp_info *next;
93 };
94
95 /* List of known LWPs. */
96 static struct lwp_info *lwp_list;
97
98 /* Number of LWPs in the list. */
99 static int num_lwps;
100
101 /* Non-zero if we're running in "threaded" mode. */
102 static int threaded;
103 \f
104
105 #ifndef TIDGET
106 #define TIDGET(PID) (((PID) & 0x7fffffff) >> 16)
107 #define PIDGET0(PID) (((PID) & 0xffff))
108 #define PIDGET(PID) ((PIDGET0 (PID) == 0xffff) ? -1 : PIDGET0 (PID))
109 #define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16))
110 #endif
111
112 #define THREAD_FLAG 0x80000000
113 #define is_lwp(pid) (((pid) & THREAD_FLAG) == 0 && TIDGET (pid))
114 #define GET_LWP(pid) TIDGET (pid)
115 #define GET_PID(pid) PIDGET (pid)
116 #define BUILD_LWP(tid, pid) MERGEPID (pid, tid)
117
118 #define is_cloned(pid) (GET_LWP (pid) != GET_PID (pid))
119
120 /* If the last reported event was a SIGTRAP, this variable is set to
121 the process id of the LWP/thread that got it. */
122 ptid_t trap_ptid;
123 \f
124
125 /* This module's target-specific operations. */
126 static struct target_ops lin_lwp_ops;
127
128 /* The standard child operations. */
129 extern struct target_ops child_ops;
130
131 /* Since we cannot wait (in lin_lwp_wait) for the initial process and
132 any cloned processes with a single call to waitpid, we have to use
133 the WNOHANG flag and call waitpid in a loop. To optimize
134 things a bit we use `sigsuspend' to wake us up when a process has
135 something to report (it will send us a SIGCHLD if it has). To make
136 this work we have to juggle with the signal mask. We save the
137 original signal mask such that we can restore it before creating a
138 new process in order to avoid blocking certain signals in the
139 inferior. We then block SIGCHLD during the waitpid/sigsuspend
140 loop. */
141
142 /* Original signal mask. */
143 static sigset_t normal_mask;
144
145 /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
146 _initialize_lin_lwp. */
147 static sigset_t suspend_mask;
148
149 /* Signals to block to make that sigsuspend work. */
150 static sigset_t blocked_mask;
151 \f
152
153 /* Prototypes for local functions. */
154 static int stop_wait_callback (struct lwp_info *lp, void *data);
155 \f
156
157 /* Initialize the list of LWPs. Note that this module, contrary to
158 what GDB's generic threads layer does for its thread list,
159 re-initializes the LWP lists whenever we mourn or detach (which
160 doesn't involve mourning) the inferior. */
161
162 static void
163 init_lwp_list (void)
164 {
165 struct lwp_info *lp, *lpnext;
166
167 for (lp = lwp_list; lp; lp = lpnext)
168 {
169 lpnext = lp->next;
170 xfree (lp);
171 }
172
173 lwp_list = NULL;
174 num_lwps = 0;
175 threaded = 0;
176 }
177
178 /* Add the LWP specified by PID to the list. If this causes the
179 number of LWPs to become larger than one, go into "threaded" mode.
180 Return a pointer to the structure describing the new LWP. */
181
182 static struct lwp_info *
183 add_lwp (ptid_t ptid)
184 {
185 struct lwp_info *lp;
186
187 gdb_assert (is_lwp (ptid));
188
189 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
190
191 memset (lp, 0, sizeof (struct lwp_info));
192
193 lp->ptid = ptid;
194
195 lp->next = lwp_list;
196 lwp_list = lp;
197 if (++num_lwps > 1)
198 threaded = 1;
199
200 return lp;
201 }
202
203 /* Remove the LWP specified by PID from the list. */
204
205 static void
206 delete_lwp (ptid_t ptid)
207 {
208 struct lwp_info *lp, *lpprev;
209
210 lpprev = NULL;
211
212 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
213 if (ptid_equal (lp->ptid, ptid))
214 break;
215
216 if (!lp)
217 return;
218
219 /* We don't go back to "non-threaded" mode if the number of threads
220 becomes less than two. */
221 num_lwps--;
222
223 if (lpprev)
224 lpprev->next = lp->next;
225 else
226 lwp_list = lp->next;
227
228 xfree (lp);
229 }
230
231 /* Return a pointer to the structure describing the LWP corresponding
232 to PID. If no corresponding LWP could be found, return NULL. */
233
234 static struct lwp_info *
235 find_lwp_pid (ptid_t ptid)
236 {
237 struct lwp_info *lp;
238 int lwp;
239
240 if (is_lwp (ptid))
241 lwp = GET_LWP (ptid);
242 else
243 lwp = GET_PID (ptid);
244
245 for (lp = lwp_list; lp; lp = lp->next)
246 if (lwp == GET_LWP (lp->ptid))
247 return lp;
248
249 return NULL;
250 }
251
252 /* Call CALLBACK with its second argument set to DATA for every LWP in
253 the list. If CALLBACK returns 1 for a particular LWP, return a
254 pointer to the structure describing that LWP immediately.
255 Otherwise return NULL. */
256
257 struct lwp_info *
258 iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
259 {
260 struct lwp_info *lp;
261
262 for (lp = lwp_list; lp; lp = lp->next)
263 if ((*callback) (lp, data))
264 return lp;
265
266 return NULL;
267 }
268 \f
269
270 /* Helper functions. */
271
272 static void
273 restore_inferior_ptid (void *arg)
274 {
275 ptid_t *saved_ptid_ptr = arg;
276 inferior_ptid = *saved_ptid_ptr;
277 xfree (arg);
278 }
279
280 static struct cleanup *
281 save_inferior_ptid (void)
282 {
283 ptid_t *saved_ptid_ptr;
284
285 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
286 *saved_ptid_ptr = inferior_ptid;
287 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
288 }
289 \f
290
291 /* Implementation of the PREPARE_TO_PROCEED hook for the Linux LWP
292 layer.
293
294 Note that this implementation is potentially redundant now that
295 default_prepare_to_proceed() has been added. */
296
297 int
298 lin_lwp_prepare_to_proceed (void)
299 {
300 if (! ptid_equal (trap_ptid, null_ptid)
301 && ! ptid_equal (inferior_ptid, trap_ptid))
302 {
303 /* Switched over from TRAP_PID. */
304 CORE_ADDR stop_pc = read_pc ();
305 CORE_ADDR trap_pc;
306
307 /* Avoid switching where it wouldn't do any good, i.e. if both
308 threads are at the same breakpoint. */
309 trap_pc = read_pc_pid (trap_ptid);
310 if (trap_pc != stop_pc && breakpoint_here_p (trap_pc))
311 {
312 /* User hasn't deleted the breakpoint. Return non-zero, and
313 switch back to TRAP_PID. */
314 inferior_ptid = trap_ptid;
315
316 /* FIXME: Is this stuff really necessary? */
317 flush_cached_frames ();
318 registers_changed ();
319
320 return 1;
321 }
322 }
323
324 return 0;
325 }
326 \f
327
328 #if 0
329 static void
330 lin_lwp_open (char *args, int from_tty)
331 {
332 push_target (&lin_lwp_ops);
333 }
334 #endif
335
336 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
337 a message telling the user that a new LWP has been added to the
338 process. */
339
340 void
341 lin_lwp_attach_lwp (ptid_t ptid, int verbose)
342 {
343 struct lwp_info *lp;
344
345 gdb_assert (is_lwp (ptid));
346
347 if (verbose)
348 printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
349
350 /* We assume that we're already tracing the initial process. */
351 if (is_cloned (ptid) && ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
352 error ("Can't attach %s: %s", target_pid_to_str (ptid), strerror (errno));
353
354 lp = find_lwp_pid (ptid);
355 if (lp == NULL)
356 lp = add_lwp (ptid);
357
358 if (is_cloned (ptid))
359 lp->signalled = 1;
360 }
361
362 static void
363 lin_lwp_attach (char *args, int from_tty)
364 {
365 struct lwp_info *lp;
366
367 /* FIXME: We should probably accept a list of process id's, and
368 attach all of them. */
369 child_ops.to_attach (args, from_tty);
370
371 /* Add the initial process as the first LWP to the list. */
372 lp = add_lwp (BUILD_LWP (inferior_ptid, inferior_ptid));
373
374 /* Make sure the initial process is stopped. The user-level threads
375 layer might want to poke around in the inferior, and that won't
376 work if things haven't stabilized yet. */
377 lp->signalled = 1;
378 stop_wait_callback (lp, NULL);
379 gdb_assert (lp->status == 0);
380
381 /* Fake the SIGSTOP that core GDB expects. */
382 lp->status = W_STOPCODE (SIGSTOP);
383 }
384
385 static int
386 detach_callback (struct lwp_info *lp, void *data)
387 {
388 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
389
390 if (debug_lin_lwp && lp->status)
391 fprintf_unfiltered (gdb_stdlog, "Pending %s for LWP %d on detach.\n",
392 strsignal (WSTOPSIG (lp->status)), GET_LWP (lp->ptid));
393
394 while (lp->signalled && lp->stopped)
395 {
396 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
397 WSTOPSIG (lp->status)) < 0)
398 error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
399 strerror (errno));
400
401 lp->stopped = 0;
402 lp->status = 0;
403 stop_wait_callback (lp, NULL);
404
405 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
406 }
407
408 if (is_cloned (lp->ptid))
409 {
410 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
411 WSTOPSIG (lp->status)) < 0)
412 error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
413 strerror (errno));
414
415 delete_lwp (lp->ptid);
416 }
417
418 return 0;
419 }
420
421 static void
422 lin_lwp_detach (char *args, int from_tty)
423 {
424 iterate_over_lwps (detach_callback, NULL);
425
426 /* Only the initial (uncloned) process should be left right now. */
427 gdb_assert (num_lwps == 1);
428
429 trap_ptid = null_ptid;
430
431 /* Destroy LWP info; it's no longer valid. */
432 init_lwp_list ();
433
434 /* Restore the original signal mask. */
435 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
436 sigemptyset (&blocked_mask);
437
438 inferior_ptid = GET_PID (inferior_ptid);
439 child_ops.to_detach (args, from_tty);
440 }
441 \f
442
443 struct private_thread_info
444 {
445 int lwpid;
446 };
447
448 /* Return non-zero if TP corresponds to the LWP specified by DATA
449 (which is assumed to be a pointer to a `struct lwp_info'. */
450
451 static int
452 find_lwp_callback (struct thread_info *tp, void *data)
453 {
454 struct lwp_info *lp = data;
455
456 if (tp->private->lwpid == GET_LWP (lp->ptid))
457 return 1;
458
459 return 0;
460 }
461
462 /* Resume LP. */
463
464 static int
465 resume_callback (struct lwp_info *lp, void *data)
466 {
467 if (lp->stopped && lp->status == 0)
468 {
469 struct thread_info *tp;
470
471 #if 1
472 /* FIXME: kettenis/2000-08-26: This should really be handled
473 properly by core GDB. */
474
475 tp = find_thread_pid (lp->ptid);
476 if (tp == NULL)
477 tp = iterate_over_threads (find_lwp_callback, lp);
478 gdb_assert (tp);
479
480 /* If we were previously stepping the thread, and now continue
481 the thread we must invalidate the stepping range. However,
482 if there is a step_resume breakpoint for this thread, we must
483 preserve the stepping range to make it possible to continue
484 stepping once we hit it. */
485 if (tp->step_range_end && tp->step_resume_breakpoint == NULL)
486 {
487 gdb_assert (lp->step);
488 tp->step_range_start = tp->step_range_end = 0;
489 }
490 #endif
491
492 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
493 lp->stopped = 0;
494 lp->step = 0;
495 }
496
497 return 0;
498 }
499
500 static void
501 lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
502 {
503 struct lwp_info *lp;
504 int resume_all;
505
506 /* Apparently the interpretation of PID is dependent on STEP: If
507 STEP is non-zero, a specific PID means `step only this process
508 id'. But if STEP is zero, then PID means `continue *all*
509 processes, but give the signal only to this one'. */
510 resume_all = (PIDGET (ptid) == -1) || !step;
511
512 /* If PID is -1, it's the current inferior that should be
513 handled special. */
514 if (PIDGET (ptid) == -1)
515 ptid = inferior_ptid;
516
517 lp = find_lwp_pid (ptid);
518 if (lp)
519 {
520 ptid = pid_to_ptid (GET_LWP (lp->ptid));
521
522 /* Remember if we're stepping. */
523 lp->step = step;
524
525 /* If we have a pending wait status for this thread, there is no
526 point in resuming the process. */
527 if (lp->status)
528 {
529 /* FIXME: What should we do if we are supposed to continue
530 this thread with a signal? */
531 gdb_assert (signo == TARGET_SIGNAL_0);
532 return;
533 }
534
535 /* Mark LWP as not stopped to prevent it from being continued by
536 resume_callback. */
537 lp->stopped = 0;
538 }
539
540 if (resume_all)
541 iterate_over_lwps (resume_callback, NULL);
542
543 child_resume (ptid, step, signo);
544 }
545 \f
546
547 /* Send a SIGSTOP to LP. */
548
549 static int
550 stop_callback (struct lwp_info *lp, void *data)
551 {
552 if (! lp->stopped && ! lp->signalled)
553 {
554 int ret;
555
556 ret = kill (GET_LWP (lp->ptid), SIGSTOP);
557 gdb_assert (ret == 0);
558
559 lp->signalled = 1;
560 gdb_assert (lp->status == 0);
561 }
562
563 return 0;
564 }
565
566 /* Wait until LP is stopped. */
567
568 static int
569 stop_wait_callback (struct lwp_info *lp, void *data)
570 {
571 if (! lp->stopped && lp->signalled)
572 {
573 pid_t pid;
574 int status;
575
576 gdb_assert (lp->status == 0);
577
578 pid = waitpid (GET_LWP (lp->ptid), &status,
579 is_cloned (lp->ptid) ? __WCLONE : 0);
580 if (pid == -1 && errno == ECHILD)
581 /* OK, the proccess has disappeared. We'll catch the actual
582 exit event in lin_lwp_wait. */
583 return 0;
584
585 gdb_assert (pid == GET_LWP (lp->ptid));
586
587 if (WIFEXITED (status) || WIFSIGNALED (status))
588 {
589 gdb_assert (num_lwps > 1);
590
591 if (in_thread_list (lp->ptid))
592 {
593 /* Core GDB cannot deal with us deleting the current
594 thread. */
595 if (!ptid_equal (lp->ptid, inferior_ptid))
596 delete_thread (lp->ptid);
597 printf_unfiltered ("[%s exited]\n",
598 target_pid_to_str (lp->ptid));
599 }
600 if (debug_lin_lwp)
601 fprintf_unfiltered (gdb_stdlog,
602 "%s exited.\n", target_pid_to_str (lp->ptid));
603
604 delete_lwp (lp->ptid);
605 return 0;
606 }
607
608 gdb_assert (WIFSTOPPED (status));
609 lp->stopped = 1;
610
611 if (WSTOPSIG (status) != SIGSTOP)
612 {
613 if (WSTOPSIG (status) == SIGTRAP
614 && breakpoint_inserted_here_p (read_pc_pid (pid_to_ptid (pid))
615 - DECR_PC_AFTER_BREAK))
616 {
617 /* If a LWP other than the LWP that we're reporting an
618 event for has hit a GDB breakpoint (as opposed to
619 some random trap signal), then just arrange for it to
620 hit it again later. We don't keep the SIGTRAP status
621 and don't forward the SIGTRAP signal to the LWP. We
622 will handle the current event, eventually we will
623 resume all LWPs, and this one will get its breakpoint
624 trap again.
625
626 If we do not do this, then we run the risk that the
627 user will delete or disable the breakpoint, but the
628 thread will have already tripped on it. */
629
630 if (debug_lin_lwp)
631 fprintf_unfiltered (gdb_stdlog,
632 "Tripped breakpoint at %lx in LWP %d"
633 " while waiting for SIGSTOP.\n",
634 (long) read_pc_pid (lp->ptid), pid);
635
636 /* Set the PC to before the trap. */
637 if (DECR_PC_AFTER_BREAK)
638 write_pc_pid (read_pc_pid (pid_to_ptid (pid))
639 - DECR_PC_AFTER_BREAK,
640 pid_to_ptid (pid));
641 }
642 else
643 {
644 if (debug_lin_lwp)
645 fprintf_unfiltered (gdb_stdlog,
646 "Received %s in LWP %d while waiting for SIGSTOP.\n",
647 strsignal (WSTOPSIG (status)), pid);
648
649 /* The thread was stopped with a signal other than
650 SIGSTOP, and didn't accidentiliy trip a breakpoint.
651 Record the wait status. */
652 lp->status = status;
653 }
654 }
655 else
656 {
657 /* We caught the SIGSTOP that we intended to catch, so
658 there's no SIGSTOP pending. */
659 lp->signalled = 0;
660 }
661 }
662
663 return 0;
664 }
665
666 /* Return non-zero if LP has a wait status pending. */
667
668 static int
669 status_callback (struct lwp_info *lp, void *data)
670 {
671 return (lp->status != 0);
672 }
673
674 /* Return non-zero if LP isn't stopped. */
675
676 static int
677 running_callback (struct lwp_info *lp, void *data)
678 {
679 return (lp->stopped == 0);
680 }
681
682 static ptid_t
683 lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
684 {
685 struct lwp_info *lp = NULL;
686 int options = 0;
687 int status = 0;
688 pid_t pid = PIDGET (ptid);
689
690 /* Make sure SIGCHLD is blocked. */
691 if (! sigismember (&blocked_mask, SIGCHLD))
692 {
693 sigaddset (&blocked_mask, SIGCHLD);
694 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
695 }
696
697 retry:
698
699 /* First check if there is a LWP with a wait status pending. */
700 if (pid == -1)
701 {
702 /* Any LWP will do. */
703 lp = iterate_over_lwps (status_callback, NULL);
704 if (lp)
705 {
706 if (debug_lin_lwp)
707 fprintf_unfiltered (gdb_stdlog,
708 "Using pending wait status for LWP %d.\n",
709 (int) GET_LWP (lp->ptid));
710
711 status = lp->status;
712 lp->status = 0;
713 }
714
715 /* But if we don't fine one, we'll have to wait, and check both
716 cloned and uncloned processes. We start with the cloned
717 processes. */
718 options = __WCLONE | WNOHANG;
719 }
720 else if (is_lwp (ptid))
721 {
722 if (debug_lin_lwp)
723 fprintf_unfiltered (gdb_stdlog,
724 "Waiting for specific LWP %d.\n",
725 (int) GET_LWP (ptid));
726
727 /* We have a specific LWP to check. */
728 lp = find_lwp_pid (ptid);
729 gdb_assert (lp);
730 status = lp->status;
731 lp->status = 0;
732
733 if (debug_lin_lwp)
734 if (status)
735 fprintf_unfiltered (gdb_stdlog,
736 "Using pending wait status for LWP %d.\n",
737 GET_LWP (lp->ptid));
738
739 /* If we have to wait, take into account whether PID is a cloned
740 process or not. And we have to convert it to something that
741 the layer beneath us can understand. */
742 options = is_cloned (lp->ptid) ? __WCLONE : 0;
743 pid = GET_LWP (ptid);
744 }
745
746 if (status && lp->signalled)
747 {
748 /* A pending SIGSTOP may interfere with the normal stream of
749 events. In a typical case where interference is a problem,
750 we have a SIGSTOP signal pending for LWP A while
751 single-stepping it, encounter an event in LWP B, and take the
752 pending SIGSTOP while trying to stop LWP A. After processing
753 the event in LWP B, LWP A is continued, and we'll never see
754 the SIGTRAP associated with the last time we were
755 single-stepping LWP A. */
756
757 /* Resume the thread. It should halt immediately returning the
758 pending SIGSTOP. */
759 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
760 TARGET_SIGNAL_0);
761 lp->stopped = 0;
762
763 /* This should catch the pending SIGSTOP. */
764 stop_wait_callback (lp, NULL);
765 }
766
767 set_sigint_trap (); /* Causes SIGINT to be passed on to the
768 attached process. */
769 set_sigio_trap ();
770
771 while (status == 0)
772 {
773 pid_t lwpid;
774
775 lwpid = waitpid (pid, &status, options);
776 if (lwpid > 0)
777 {
778 gdb_assert (pid == -1 || lwpid == pid);
779
780 lp = find_lwp_pid (pid_to_ptid (lwpid));
781 if (! lp)
782 {
783 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
784 if (threaded)
785 {
786 gdb_assert (WIFSTOPPED (status)
787 && WSTOPSIG (status) == SIGSTOP);
788 lp->signalled = 1;
789
790 if (! in_thread_list (inferior_ptid))
791 {
792 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
793 GET_PID (inferior_ptid));
794 add_thread (inferior_ptid);
795 }
796
797 add_thread (lp->ptid);
798 printf_unfiltered ("[New %s]\n",
799 target_pid_to_str (lp->ptid));
800 }
801 }
802
803 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
804 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
805 left in the process. */
806 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
807 {
808 if (in_thread_list (lp->ptid))
809 {
810 /* Core GDB cannot deal with us deleting the current
811 thread. */
812 if (! ptid_equal (lp->ptid, inferior_ptid))
813 delete_thread (lp->ptid);
814 printf_unfiltered ("[%s exited]\n",
815 target_pid_to_str (lp->ptid));
816 }
817 if (debug_lin_lwp)
818 fprintf_unfiltered (gdb_stdlog,
819 "%s exited.\n",
820 target_pid_to_str (lp->ptid));
821
822 delete_lwp (lp->ptid);
823
824 /* Make sure there is at least one thread running. */
825 gdb_assert (iterate_over_lwps (running_callback, NULL));
826
827 /* Discard the event. */
828 status = 0;
829 continue;
830 }
831
832 /* Make sure we don't report a SIGSTOP that we sent
833 ourselves in an attempt to stop an LWP. */
834 if (lp->signalled && WIFSTOPPED (status)
835 && WSTOPSIG (status) == SIGSTOP)
836 {
837 if (debug_lin_lwp)
838 fprintf_unfiltered (gdb_stdlog,
839 "Delayed SIGSTOP caught for %s.\n",
840 target_pid_to_str (lp->ptid));
841
842 /* This is a delayed SIGSTOP. */
843 lp->signalled = 0;
844
845 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
846 TARGET_SIGNAL_0);
847 lp->stopped = 0;
848
849 /* Discard the event. */
850 status = 0;
851 continue;
852 }
853
854 break;
855 }
856
857 if (pid == -1)
858 {
859 /* Alternate between checking cloned and uncloned processes. */
860 options ^= __WCLONE;
861
862 /* And suspend every time we have checked both. */
863 if (options & __WCLONE)
864 sigsuspend (&suspend_mask);
865 }
866
867 /* We shouldn't end up here unless we want to try again. */
868 gdb_assert (status == 0);
869 }
870
871 clear_sigio_trap ();
872 clear_sigint_trap ();
873
874 gdb_assert (lp);
875
876 /* Don't report signals that GDB isn't interested in, such as
877 signals that are neither printed nor stopped upon. Stopping all
878 threads can be a bit time-consuming so if we want decent
879 performance with heavily multi-threaded programs, especially when
880 they're using a high frequency timer, we'd better avoid it if we
881 can. */
882
883 if (WIFSTOPPED (status))
884 {
885 int signo = target_signal_from_host (WSTOPSIG (status));
886
887 if (signal_stop_state (signo) == 0
888 && signal_print_state (signo) == 0
889 && signal_pass_state (signo) == 1)
890 {
891 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
892 lp->stopped = 0;
893 status = 0;
894 goto retry;
895 }
896 }
897
898 /* This LWP is stopped now. */
899 lp->stopped = 1;
900
901 /* Now stop all other LWP's ... */
902 iterate_over_lwps (stop_callback, NULL);
903
904 /* ... and wait until all of them have reported back that they're no
905 longer running. */
906 iterate_over_lwps (stop_wait_callback, NULL);
907
908 /* If we're not running in "threaded" mode, we'll report the bare
909 process id. */
910
911 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
912 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
913 else
914 trap_ptid = null_ptid;
915
916 store_waitstatus (ourstatus, status);
917 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
918 }
919
920 static int
921 kill_callback (struct lwp_info *lp, void *data)
922 {
923 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
924 return 0;
925 }
926
927 static int
928 kill_wait_callback (struct lwp_info *lp, void *data)
929 {
930 pid_t pid;
931
932 /* We must make sure that there are no pending events (delayed
933 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
934 program doesn't interfere with any following debugging session. */
935
936 /* For cloned processes we must check both with __WCLONE and
937 without, since the exit status of a cloned process isn't reported
938 with __WCLONE. */
939 if (is_cloned (lp->ptid))
940 {
941 do
942 {
943 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
944 }
945 while (pid == GET_LWP (lp->ptid));
946
947 gdb_assert (pid == -1 && errno == ECHILD);
948 }
949
950 do
951 {
952 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
953 }
954 while (pid == GET_LWP (lp->ptid));
955
956 gdb_assert (pid == -1 && errno == ECHILD);
957 return 0;
958 }
959
960 static void
961 lin_lwp_kill (void)
962 {
963 /* Kill all LWP's ... */
964 iterate_over_lwps (kill_callback, NULL);
965
966 /* ... and wait until we've flushed all events. */
967 iterate_over_lwps (kill_wait_callback, NULL);
968
969 target_mourn_inferior ();
970 }
971
972 static void
973 lin_lwp_create_inferior (char *exec_file, char *allargs, char **env)
974 {
975 child_ops.to_create_inferior (exec_file, allargs, env);
976 }
977
978 static void
979 lin_lwp_mourn_inferior (void)
980 {
981 trap_ptid = null_ptid;
982
983 /* Destroy LWP info; it's no longer valid. */
984 init_lwp_list ();
985
986 /* Restore the original signal mask. */
987 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
988 sigemptyset (&blocked_mask);
989
990 child_ops.to_mourn_inferior ();
991 }
992
993 static void
994 lin_lwp_fetch_registers (int regno)
995 {
996 struct cleanup *old_chain = save_inferior_ptid ();
997
998 if (is_lwp (inferior_ptid))
999 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
1000
1001 fetch_inferior_registers (regno);
1002
1003 do_cleanups (old_chain);
1004 }
1005
1006 static void
1007 lin_lwp_store_registers (int regno)
1008 {
1009 struct cleanup *old_chain = save_inferior_ptid ();
1010
1011 if (is_lwp (inferior_ptid))
1012 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
1013
1014 store_inferior_registers (regno);
1015
1016 do_cleanups (old_chain);
1017 }
1018
1019 static int
1020 lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
1021 struct mem_attrib *attrib,
1022 struct target_ops *target)
1023 {
1024 struct cleanup *old_chain = save_inferior_ptid ();
1025 int xfer;
1026
1027 if (is_lwp (inferior_ptid))
1028 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
1029
1030 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1031
1032 do_cleanups (old_chain);
1033 return xfer;
1034 }
1035
1036 static int
1037 lin_lwp_thread_alive (ptid_t ptid)
1038 {
1039 gdb_assert (is_lwp (ptid));
1040
1041 errno = 0;
1042 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
1043 if (errno)
1044 return 0;
1045
1046 return 1;
1047 }
1048
1049 static char *
1050 lin_lwp_pid_to_str (ptid_t ptid)
1051 {
1052 static char buf[64];
1053
1054 if (is_lwp (ptid))
1055 {
1056 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
1057 return buf;
1058 }
1059
1060 return normal_pid_to_str (ptid);
1061 }
1062
1063 static void
1064 init_lin_lwp_ops (void)
1065 {
1066 #if 0
1067 lin_lwp_ops.to_open = lin_lwp_open;
1068 #endif
1069 lin_lwp_ops.to_shortname = "lwp-layer";
1070 lin_lwp_ops.to_longname = "lwp-layer";
1071 lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
1072 lin_lwp_ops.to_attach = lin_lwp_attach;
1073 lin_lwp_ops.to_detach = lin_lwp_detach;
1074 lin_lwp_ops.to_resume = lin_lwp_resume;
1075 lin_lwp_ops.to_wait = lin_lwp_wait;
1076 lin_lwp_ops.to_fetch_registers = lin_lwp_fetch_registers;
1077 lin_lwp_ops.to_store_registers = lin_lwp_store_registers;
1078 lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
1079 lin_lwp_ops.to_kill = lin_lwp_kill;
1080 lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
1081 lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
1082 lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
1083 lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
1084 lin_lwp_ops.to_stratum = thread_stratum;
1085 lin_lwp_ops.to_has_thread_control = tc_schedlock;
1086 lin_lwp_ops.to_magic = OPS_MAGIC;
1087 }
1088
1089 static void
1090 sigchld_handler (int signo)
1091 {
1092 /* Do nothing. The only reason for this handler is that it allows
1093 us to use sigsuspend in lin_lwp_wait above to wait for the
1094 arrival of a SIGCHLD. */
1095 }
1096
1097 void
1098 _initialize_lin_lwp (void)
1099 {
1100 struct sigaction action;
1101
1102 extern void thread_db_init (struct target_ops *);
1103
1104 init_lin_lwp_ops ();
1105 add_target (&lin_lwp_ops);
1106 thread_db_init (&lin_lwp_ops);
1107
1108 /* Save the original signal mask. */
1109 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
1110
1111 action.sa_handler = sigchld_handler;
1112 sigemptyset (&action.sa_mask);
1113 action.sa_flags = 0;
1114 sigaction (SIGCHLD, &action, NULL);
1115
1116 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1117 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
1118 sigdelset (&suspend_mask, SIGCHLD);
1119
1120 sigemptyset (&blocked_mask);
1121
1122 add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
1123 (char *) &debug_lin_lwp,
1124 "Set debugging of linux lwp module.\n\
1125 Enables printf debugging output.\n",
1126 &setdebuglist),
1127 &showdebuglist);
1128 }
1129 \f
1130
1131 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
1132 the LinuxThreads library and therefore doesn't really belong here. */
1133
1134 /* Read variable NAME in the target and return its value if found.
1135 Otherwise return zero. It is assumed that the type of the variable
1136 is `int'. */
1137
1138 static int
1139 get_signo (const char *name)
1140 {
1141 struct minimal_symbol *ms;
1142 int signo;
1143
1144 ms = lookup_minimal_symbol (name, NULL, NULL);
1145 if (ms == NULL)
1146 return 0;
1147
1148 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
1149 sizeof (signo)) != 0)
1150 return 0;
1151
1152 return signo;
1153 }
1154
1155 /* Return the set of signals used by the threads library in *SET. */
1156
1157 void
1158 lin_thread_get_thread_signals (sigset_t *set)
1159 {
1160 struct sigaction action;
1161 int restart, cancel;
1162
1163 sigemptyset (set);
1164
1165 restart = get_signo ("__pthread_sig_restart");
1166 if (restart == 0)
1167 return;
1168
1169 cancel = get_signo ("__pthread_sig_cancel");
1170 if (cancel == 0)
1171 return;
1172
1173 sigaddset (set, restart);
1174 sigaddset (set, cancel);
1175
1176 /* The LinuxThreads library makes terminating threads send a special
1177 "cancel" signal instead of SIGCHLD. Make sure we catch those (to
1178 prevent them from terminating GDB itself, which is likely to be
1179 their default action) and treat them the same way as SIGCHLD. */
1180
1181 action.sa_handler = sigchld_handler;
1182 sigemptyset (&action.sa_mask);
1183 action.sa_flags = 0;
1184 sigaction (cancel, &action, NULL);
1185
1186 /* We block the "cancel" signal throughout this code ... */
1187 sigaddset (&blocked_mask, cancel);
1188 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1189
1190 /* ... except during a sigsuspend. */
1191 sigdelset (&suspend_mask, cancel);
1192 }