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