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