1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "gdb_string.h"
27 #include "gdb_assert.h"
28 #ifdef HAVE_TKILL_SYSCALL
30 #include <sys/syscall.h>
32 #include <sys/ptrace.h>
33 #include "linux-nat.h"
34 #include "gdbthread.h"
37 #include <sys/param.h> /* for MAXPATHLEN */
38 #include <sys/procfs.h> /* for elf_gregset etc. */
39 #include "elf-bfd.h" /* for elfcore_write_* */
40 #include "gregset.h" /* for gregset */
41 #include "gdbcore.h" /* for get_exec_file */
42 #include <ctype.h> /* for isdigit */
43 #include "gdbthread.h" /* for struct thread_info etc. */
44 #include "gdb_stat.h" /* for struct stat */
45 #include <fcntl.h> /* for O_RDONLY */
51 /* If the system headers did not provide the constants, hard-code the normal
53 #ifndef PTRACE_EVENT_FORK
55 #define PTRACE_SETOPTIONS 0x4200
56 #define PTRACE_GETEVENTMSG 0x4201
58 /* options set using PTRACE_SETOPTIONS */
59 #define PTRACE_O_TRACESYSGOOD 0x00000001
60 #define PTRACE_O_TRACEFORK 0x00000002
61 #define PTRACE_O_TRACEVFORK 0x00000004
62 #define PTRACE_O_TRACECLONE 0x00000008
63 #define PTRACE_O_TRACEEXEC 0x00000010
64 #define PTRACE_O_TRACEVFORKDONE 0x00000020
65 #define PTRACE_O_TRACEEXIT 0x00000040
67 /* Wait extended result codes for the above trace options. */
68 #define PTRACE_EVENT_FORK 1
69 #define PTRACE_EVENT_VFORK 2
70 #define PTRACE_EVENT_CLONE 3
71 #define PTRACE_EVENT_EXEC 4
72 #define PTRACE_EVENT_VFORK_DONE 5
73 #define PTRACE_EVENT_EXIT 6
75 #endif /* PTRACE_EVENT_FORK */
77 /* We can't always assume that this flag is available, but all systems
78 with the ptrace event handlers also have __WALL, so it's safe to use
81 #define __WALL 0x40000000 /* Wait for any child. */
84 static int debug_linux_nat
;
86 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
87 struct cmd_list_element
*c
, const char *value
)
89 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
93 static int linux_parent_pid
;
95 struct simple_pid_list
98 struct simple_pid_list
*next
;
100 struct simple_pid_list
*stopped_pids
;
102 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
103 can not be used, 1 if it can. */
105 static int linux_supports_tracefork_flag
= -1;
107 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
108 PTRACE_O_TRACEVFORKDONE. */
110 static int linux_supports_tracevforkdone_flag
= -1;
113 /* Trivial list manipulation functions to keep track of a list of
114 new stopped processes. */
116 add_to_pid_list (struct simple_pid_list
**listp
, int pid
)
118 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
120 new_pid
->next
= *listp
;
125 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
)
127 struct simple_pid_list
**p
;
129 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
130 if ((*p
)->pid
== pid
)
132 struct simple_pid_list
*next
= (*p
)->next
;
141 linux_record_stopped_pid (int pid
)
143 add_to_pid_list (&stopped_pids
, pid
);
147 /* A helper function for linux_test_for_tracefork, called after fork (). */
150 linux_tracefork_child (void)
154 ptrace (PTRACE_TRACEME
, 0, 0, 0);
155 kill (getpid (), SIGSTOP
);
160 /* Wrapper function for waitpid which handles EINTR. */
163 my_waitpid (int pid
, int *status
, int flags
)
168 ret
= waitpid (pid
, status
, flags
);
170 while (ret
== -1 && errno
== EINTR
);
175 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
177 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
178 we know that the feature is not available. This may change the tracing
179 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
181 However, if it succeeds, we don't know for sure that the feature is
182 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
183 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
184 fork tracing, and let it fork. If the process exits, we assume that we
185 can't use TRACEFORK; if we get the fork notification, and we can extract
186 the new child's PID, then we assume that we can. */
189 linux_test_for_tracefork (int original_pid
)
191 int child_pid
, ret
, status
;
194 linux_supports_tracefork_flag
= 0;
195 linux_supports_tracevforkdone_flag
= 0;
197 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
203 perror_with_name (("fork"));
206 linux_tracefork_child ();
208 ret
= my_waitpid (child_pid
, &status
, 0);
210 perror_with_name (("waitpid"));
211 else if (ret
!= child_pid
)
212 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
213 if (! WIFSTOPPED (status
))
214 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
216 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
219 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
222 warning (_("linux_test_for_tracefork: failed to kill child"));
226 ret
= my_waitpid (child_pid
, &status
, 0);
227 if (ret
!= child_pid
)
228 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
229 else if (!WIFSIGNALED (status
))
230 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
231 "killed child"), status
);
236 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
237 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
238 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
239 linux_supports_tracevforkdone_flag
= (ret
== 0);
241 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
243 warning (_("linux_test_for_tracefork: failed to resume child"));
245 ret
= my_waitpid (child_pid
, &status
, 0);
247 if (ret
== child_pid
&& WIFSTOPPED (status
)
248 && status
>> 16 == PTRACE_EVENT_FORK
)
251 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
252 if (ret
== 0 && second_pid
!= 0)
256 linux_supports_tracefork_flag
= 1;
257 my_waitpid (second_pid
, &second_status
, 0);
258 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
260 warning (_("linux_test_for_tracefork: failed to kill second child"));
264 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
265 "(%d, status 0x%x)"), ret
, status
);
267 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
269 warning (_("linux_test_for_tracefork: failed to kill child"));
270 my_waitpid (child_pid
, &status
, 0);
273 /* Return non-zero iff we have tracefork functionality available.
274 This function also sets linux_supports_tracefork_flag. */
277 linux_supports_tracefork (int pid
)
279 if (linux_supports_tracefork_flag
== -1)
280 linux_test_for_tracefork (pid
);
281 return linux_supports_tracefork_flag
;
285 linux_supports_tracevforkdone (int pid
)
287 if (linux_supports_tracefork_flag
== -1)
288 linux_test_for_tracefork (pid
);
289 return linux_supports_tracevforkdone_flag
;
294 linux_enable_event_reporting (ptid_t ptid
)
296 int pid
= ptid_get_pid (ptid
);
299 if (! linux_supports_tracefork (pid
))
302 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
303 | PTRACE_O_TRACECLONE
;
304 if (linux_supports_tracevforkdone (pid
))
305 options
|= PTRACE_O_TRACEVFORKDONE
;
307 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
308 read-only process state. */
310 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
314 child_post_attach (int pid
)
316 linux_enable_event_reporting (pid_to_ptid (pid
));
320 linux_child_post_startup_inferior (ptid_t ptid
)
322 linux_enable_event_reporting (ptid
);
325 #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
327 child_post_startup_inferior (ptid_t ptid
)
329 linux_child_post_startup_inferior (ptid
);
334 child_follow_fork (int follow_child
)
337 struct target_waitstatus last_status
;
339 int parent_pid
, child_pid
;
341 get_last_target_status (&last_ptid
, &last_status
);
342 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
343 parent_pid
= ptid_get_pid (last_ptid
);
344 child_pid
= last_status
.value
.related_pid
;
348 /* We're already attached to the parent, by default. */
350 /* Before detaching from the child, remove all breakpoints from
351 it. (This won't actually modify the breakpoint list, but will
352 physically remove the breakpoints from the child.) */
353 /* If we vforked this will remove the breakpoints from the parent
354 also, but they'll be reinserted below. */
355 detach_breakpoints (child_pid
);
357 fprintf_filtered (gdb_stdout
,
358 "Detaching after fork from child process %d.\n",
361 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
365 gdb_assert (linux_supports_tracefork_flag
>= 0);
366 if (linux_supports_tracevforkdone (0))
370 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
371 waitpid (parent_pid
, &status
, __WALL
);
372 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
373 warning (_("Unexpected waitpid result %06x when waiting for "
374 "vfork-done"), status
);
378 /* We can't insert breakpoints until the child has
379 finished with the shared memory region. We need to
380 wait until that happens. Ideal would be to just
382 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
383 - waitpid (parent_pid, &status, __WALL);
384 However, most architectures can't handle a syscall
385 being traced on the way out if it wasn't traced on
388 We might also think to loop, continuing the child
389 until it exits or gets a SIGTRAP. One problem is
390 that the child might call ptrace with PTRACE_TRACEME.
392 There's no simple and reliable way to figure out when
393 the vforked child will be done with its copy of the
394 shared memory. We could step it out of the syscall,
395 two instructions, let it go, and then single-step the
396 parent once. When we have hardware single-step, this
397 would work; with software single-step it could still
398 be made to work but we'd have to be able to insert
399 single-step breakpoints in the child, and we'd have
400 to insert -just- the single-step breakpoint in the
401 parent. Very awkward.
403 In the end, the best we can do is to make sure it
404 runs for a little while. Hopefully it will be out of
405 range of any breakpoints we reinsert. Usually this
406 is only the single-step breakpoint at vfork's return
412 /* Since we vforked, breakpoints were removed in the parent
413 too. Put them back. */
414 reattach_breakpoints (parent_pid
);
419 char child_pid_spelling
[40];
421 /* Needed to keep the breakpoint lists in sync. */
423 detach_breakpoints (child_pid
);
425 /* Before detaching from the parent, remove all breakpoints from it. */
426 remove_breakpoints ();
428 fprintf_filtered (gdb_stdout
,
429 "Attaching after fork to child process %d.\n",
432 /* If we're vforking, we may want to hold on to the parent until
433 the child exits or execs. At exec time we can remove the old
434 breakpoints from the parent and detach it; at exit time we
435 could do the same (or even, sneakily, resume debugging it - the
436 child's exec has failed, or something similar).
438 This doesn't clean up "properly", because we can't call
439 target_detach, but that's OK; if the current target is "child",
440 then it doesn't need any further cleanups, and lin_lwp will
441 generally not encounter vfork (vfork is defined to fork
444 The holding part is very easy if we have VFORKDONE events;
445 but keeping track of both processes is beyond GDB at the
446 moment. So we don't expose the parent to the rest of GDB.
447 Instead we quietly hold onto it until such time as we can
451 linux_parent_pid
= parent_pid
;
453 target_detach (NULL
, 0);
455 inferior_ptid
= pid_to_ptid (child_pid
);
456 push_target (&deprecated_child_ops
);
458 /* Reset breakpoints in the child as appropriate. */
459 follow_inferior_reset_breakpoints ();
466 linux_handle_extended_wait (int pid
, int status
,
467 struct target_waitstatus
*ourstatus
)
469 int event
= status
>> 16;
471 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
472 || event
== PTRACE_EVENT_CLONE
)
474 unsigned long new_pid
;
477 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
479 /* If we haven't already seen the new PID stop, wait for it now. */
480 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
482 /* The new child has a pending SIGSTOP. We can't affect it until it
483 hits the SIGSTOP, but we're already attached. */
485 ret
= waitpid (new_pid
, &status
,
486 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
487 } while (ret
== -1 && errno
== EINTR
);
489 perror_with_name (_("waiting for new child"));
490 else if (ret
!= new_pid
)
491 internal_error (__FILE__
, __LINE__
,
492 _("wait returned unexpected PID %d"), ret
);
493 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
494 internal_error (__FILE__
, __LINE__
,
495 _("wait returned unexpected status 0x%x"), status
);
498 if (event
== PTRACE_EVENT_FORK
)
499 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
500 else if (event
== PTRACE_EVENT_VFORK
)
501 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
503 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
505 ourstatus
->value
.related_pid
= new_pid
;
506 return inferior_ptid
;
509 if (event
== PTRACE_EVENT_EXEC
)
511 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
512 ourstatus
->value
.execd_pathname
513 = xstrdup (child_pid_to_exec_file (pid
));
515 if (linux_parent_pid
)
517 detach_breakpoints (linux_parent_pid
);
518 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
520 linux_parent_pid
= 0;
523 return inferior_ptid
;
526 internal_error (__FILE__
, __LINE__
,
527 _("unknown ptrace event %d"), event
);
532 child_insert_fork_catchpoint (int pid
)
534 if (! linux_supports_tracefork (pid
))
535 error (_("Your system does not support fork catchpoints."));
539 child_insert_vfork_catchpoint (int pid
)
541 if (!linux_supports_tracefork (pid
))
542 error (_("Your system does not support vfork catchpoints."));
546 child_insert_exec_catchpoint (int pid
)
548 if (!linux_supports_tracefork (pid
))
549 error (_("Your system does not support exec catchpoints."));
556 int pid
= PIDGET (inferior_ptid
);
557 struct target_waitstatus last
;
564 /* If we're stopped while forking and we haven't followed yet, kill the
565 other task. We need to do this first because the parent will be
566 sleeping if this is a vfork. */
568 get_last_target_status (&last_ptid
, &last
);
570 if (last
.kind
== TARGET_WAITKIND_FORKED
571 || last
.kind
== TARGET_WAITKIND_VFORKED
)
573 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
577 /* Kill the current process. */
578 ptrace (PT_KILL
, pid
, 0, 0);
579 ret
= wait (&status
);
581 /* We might get a SIGCHLD instead of an exit status. This is
582 aggravated by the first kill above - a child has just died. */
584 while (ret
== pid
&& WIFSTOPPED (status
))
586 ptrace (PT_KILL
, pid
, 0, 0);
587 ret
= wait (&status
);
590 target_mourn_inferior ();
593 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
594 are processes sharing the same VM space. A multi-threaded process
595 is basically a group of such processes. However, such a grouping
596 is almost entirely a user-space issue; the kernel doesn't enforce
597 such a grouping at all (this might change in the future). In
598 general, we'll rely on the threads library (i.e. the GNU/Linux
599 Threads library) to provide such a grouping.
601 It is perfectly well possible to write a multi-threaded application
602 without the assistance of a threads library, by using the clone
603 system call directly. This module should be able to give some
604 rudimentary support for debugging such applications if developers
605 specify the CLONE_PTRACE flag in the clone system call, and are
606 using the Linux kernel 2.4 or above.
608 Note that there are some peculiarities in GNU/Linux that affect
611 - In general one should specify the __WCLONE flag to waitpid in
612 order to make it report events for any of the cloned processes
613 (and leave it out for the initial process). However, if a cloned
614 process has exited the exit status is only reported if the
615 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
616 we cannot use it since GDB must work on older systems too.
618 - When a traced, cloned process exits and is waited for by the
619 debugger, the kernel reassigns it to the original parent and
620 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
621 library doesn't notice this, which leads to the "zombie problem":
622 When debugged a multi-threaded process that spawns a lot of
623 threads will run out of processes, even if the threads exit,
624 because the "zombies" stay around. */
626 /* List of known LWPs. */
627 static struct lwp_info
*lwp_list
;
629 /* Number of LWPs in the list. */
632 /* Non-zero if we're running in "threaded" mode. */
636 #define GET_LWP(ptid) ptid_get_lwp (ptid)
637 #define GET_PID(ptid) ptid_get_pid (ptid)
638 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
639 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
641 /* If the last reported event was a SIGTRAP, this variable is set to
642 the process id of the LWP/thread that got it. */
646 /* This module's target-specific operations. */
647 static struct target_ops linux_nat_ops
;
649 /* Since we cannot wait (in linux_nat_wait) for the initial process and
650 any cloned processes with a single call to waitpid, we have to use
651 the WNOHANG flag and call waitpid in a loop. To optimize
652 things a bit we use `sigsuspend' to wake us up when a process has
653 something to report (it will send us a SIGCHLD if it has). To make
654 this work we have to juggle with the signal mask. We save the
655 original signal mask such that we can restore it before creating a
656 new process in order to avoid blocking certain signals in the
657 inferior. We then block SIGCHLD during the waitpid/sigsuspend
660 /* Original signal mask. */
661 static sigset_t normal_mask
;
663 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
664 _initialize_linux_nat. */
665 static sigset_t suspend_mask
;
667 /* Signals to block to make that sigsuspend work. */
668 static sigset_t blocked_mask
;
671 /* Prototypes for local functions. */
672 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
673 static int linux_nat_thread_alive (ptid_t ptid
);
675 /* Convert wait status STATUS to a string. Used for printing debug
679 status_to_str (int status
)
683 if (WIFSTOPPED (status
))
684 snprintf (buf
, sizeof (buf
), "%s (stopped)",
685 strsignal (WSTOPSIG (status
)));
686 else if (WIFSIGNALED (status
))
687 snprintf (buf
, sizeof (buf
), "%s (terminated)",
688 strsignal (WSTOPSIG (status
)));
690 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
695 /* Initialize the list of LWPs. Note that this module, contrary to
696 what GDB's generic threads layer does for its thread list,
697 re-initializes the LWP lists whenever we mourn or detach (which
698 doesn't involve mourning) the inferior. */
703 struct lwp_info
*lp
, *lpnext
;
705 for (lp
= lwp_list
; lp
; lp
= lpnext
)
716 /* Add the LWP specified by PID to the list. If this causes the
717 number of LWPs to become larger than one, go into "threaded" mode.
718 Return a pointer to the structure describing the new LWP. */
720 static struct lwp_info
*
721 add_lwp (ptid_t ptid
)
725 gdb_assert (is_lwp (ptid
));
727 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
729 memset (lp
, 0, sizeof (struct lwp_info
));
731 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
743 /* Remove the LWP specified by PID from the list. */
746 delete_lwp (ptid_t ptid
)
748 struct lwp_info
*lp
, *lpprev
;
752 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
753 if (ptid_equal (lp
->ptid
, ptid
))
759 /* We don't go back to "non-threaded" mode if the number of threads
760 becomes less than two. */
764 lpprev
->next
= lp
->next
;
771 /* Return a pointer to the structure describing the LWP corresponding
772 to PID. If no corresponding LWP could be found, return NULL. */
774 static struct lwp_info
*
775 find_lwp_pid (ptid_t ptid
)
781 lwp
= GET_LWP (ptid
);
783 lwp
= GET_PID (ptid
);
785 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
786 if (lwp
== GET_LWP (lp
->ptid
))
792 /* Call CALLBACK with its second argument set to DATA for every LWP in
793 the list. If CALLBACK returns 1 for a particular LWP, return a
794 pointer to the structure describing that LWP immediately.
795 Otherwise return NULL. */
798 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
800 struct lwp_info
*lp
, *lpnext
;
802 for (lp
= lwp_list
; lp
; lp
= lpnext
)
805 if ((*callback
) (lp
, data
))
812 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
813 a message telling the user that a new LWP has been added to the
817 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
819 struct lwp_info
*lp
, *found_lp
;
821 gdb_assert (is_lwp (ptid
));
823 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
824 to interrupt either the ptrace() or waitpid() calls below. */
825 if (!sigismember (&blocked_mask
, SIGCHLD
))
827 sigaddset (&blocked_mask
, SIGCHLD
);
828 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
832 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid
));
834 found_lp
= lp
= find_lwp_pid (ptid
);
838 /* We assume that we're already attached to any LWP that has an id
839 equal to the overall process id, and to any LWP that is already
840 in our list of LWPs. If we're not seeing exit events from threads
841 and we've had PID wraparound since we last tried to stop all threads,
842 this assumption might be wrong; fortunately, this is very unlikely
844 if (GET_LWP (ptid
) != GET_PID (ptid
) && found_lp
== NULL
)
849 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
850 error (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
851 safe_strerror (errno
));
854 fprintf_unfiltered (gdb_stdlog
,
855 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
856 target_pid_to_str (ptid
));
858 pid
= waitpid (GET_LWP (ptid
), &status
, 0);
859 if (pid
== -1 && errno
== ECHILD
)
861 /* Try again with __WCLONE to check cloned processes. */
862 pid
= waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
866 gdb_assert (pid
== GET_LWP (ptid
)
867 && WIFSTOPPED (status
) && WSTOPSIG (status
));
869 child_post_attach (pid
);
875 fprintf_unfiltered (gdb_stdlog
,
876 "LLAL: waitpid %s received %s\n",
877 target_pid_to_str (ptid
),
878 status_to_str (status
));
883 /* We assume that the LWP representing the original process is
884 already stopped. Mark it as stopped in the data structure
885 that the linux ptrace layer uses to keep track of threads.
886 Note that this won't have already been done since the main
887 thread will have, we assume, been stopped by an attach from a
894 linux_nat_attach (char *args
, int from_tty
)
900 /* FIXME: We should probably accept a list of process id's, and
901 attach all of them. */
902 deprecated_child_ops
.to_attach (args
, from_tty
);
904 /* Add the initial process as the first LWP to the list. */
905 lp
= add_lwp (BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
)));
907 /* Make sure the initial process is stopped. The user-level threads
908 layer might want to poke around in the inferior, and that won't
909 work if things haven't stabilized yet. */
910 pid
= waitpid (GET_PID (inferior_ptid
), &status
, 0);
911 if (pid
== -1 && errno
== ECHILD
)
913 warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid
));
915 /* Try again with __WCLONE to check cloned processes. */
916 pid
= waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
920 gdb_assert (pid
== GET_PID (inferior_ptid
)
921 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
925 /* Fake the SIGSTOP that core GDB expects. */
926 lp
->status
= W_STOPCODE (SIGSTOP
);
930 fprintf_unfiltered (gdb_stdlog
,
931 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid
);
936 detach_callback (struct lwp_info
*lp
, void *data
)
938 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
940 if (debug_linux_nat
&& lp
->status
)
941 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
942 strsignal (WSTOPSIG (lp
->status
)),
943 target_pid_to_str (lp
->ptid
));
945 while (lp
->signalled
&& lp
->stopped
)
948 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
949 WSTOPSIG (lp
->status
)) < 0)
950 error (_("Can't continue %s: %s"), target_pid_to_str (lp
->ptid
),
951 safe_strerror (errno
));
954 fprintf_unfiltered (gdb_stdlog
,
955 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
956 target_pid_to_str (lp
->ptid
),
957 status_to_str (lp
->status
));
962 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
963 here. But since lp->signalled was cleared above,
964 stop_wait_callback didn't do anything; the process was left
965 running. Shouldn't we be waiting for it to stop?
966 I've removed the call, since stop_wait_callback now does do
967 something when called with lp->signalled == 0. */
969 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
972 /* We don't actually detach from the LWP that has an id equal to the
973 overall process id just yet. */
974 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
977 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
978 WSTOPSIG (lp
->status
)) < 0)
979 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
980 safe_strerror (errno
));
983 fprintf_unfiltered (gdb_stdlog
,
984 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
985 target_pid_to_str (lp
->ptid
),
986 strsignal (WSTOPSIG (lp
->status
)));
988 delete_lwp (lp
->ptid
);
995 linux_nat_detach (char *args
, int from_tty
)
997 iterate_over_lwps (detach_callback
, NULL
);
999 /* Only the initial process should be left right now. */
1000 gdb_assert (num_lwps
== 1);
1002 trap_ptid
= null_ptid
;
1004 /* Destroy LWP info; it's no longer valid. */
1007 /* Restore the original signal mask. */
1008 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1009 sigemptyset (&blocked_mask
);
1011 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
1012 deprecated_child_ops
.to_detach (args
, from_tty
);
1018 resume_callback (struct lwp_info
*lp
, void *data
)
1020 if (lp
->stopped
&& lp
->status
== 0)
1022 struct thread_info
*tp
;
1024 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), 0, TARGET_SIGNAL_0
);
1025 if (debug_linux_nat
)
1026 fprintf_unfiltered (gdb_stdlog
,
1027 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1028 target_pid_to_str (lp
->ptid
));
1037 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1044 resume_set_callback (struct lwp_info
*lp
, void *data
)
1051 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1053 struct lwp_info
*lp
;
1056 /* A specific PTID means `step only this process id'. */
1057 resume_all
= (PIDGET (ptid
) == -1);
1060 iterate_over_lwps (resume_set_callback
, NULL
);
1062 iterate_over_lwps (resume_clear_callback
, NULL
);
1064 /* If PID is -1, it's the current inferior that should be
1065 handled specially. */
1066 if (PIDGET (ptid
) == -1)
1067 ptid
= inferior_ptid
;
1069 lp
= find_lwp_pid (ptid
);
1072 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1074 /* Remember if we're stepping. */
1077 /* Mark this LWP as resumed. */
1080 /* If we have a pending wait status for this thread, there is no
1081 point in resuming the process. */
1084 /* FIXME: What should we do if we are supposed to continue
1085 this thread with a signal? */
1086 gdb_assert (signo
== TARGET_SIGNAL_0
);
1090 /* Mark LWP as not stopped to prevent it from being continued by
1096 iterate_over_lwps (resume_callback
, NULL
);
1098 child_resume (ptid
, step
, signo
);
1099 if (debug_linux_nat
)
1100 fprintf_unfiltered (gdb_stdlog
,
1101 "LLR: %s %s, %s (resume event thread)\n",
1102 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1103 target_pid_to_str (ptid
),
1104 signo
? strsignal (signo
) : "0");
1107 /* Issue kill to specified lwp. */
1109 static int tkill_failed
;
1112 kill_lwp (int lwpid
, int signo
)
1116 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1117 fails, then we are not using nptl threads and we should be using kill. */
1119 #ifdef HAVE_TKILL_SYSCALL
1122 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1123 if (errno
!= ENOSYS
)
1130 return kill (lwpid
, signo
);
1133 /* Handle a GNU/Linux extended wait response. Most of the work we
1134 just pass off to linux_handle_extended_wait, but if it reports a
1135 clone event we need to add the new LWP to our list (and not report
1136 the trap to higher layers). This function returns non-zero if
1137 the event should be ignored and we should wait again. */
1140 linux_nat_handle_extended (struct lwp_info
*lp
, int status
)
1142 linux_handle_extended_wait (GET_LWP (lp
->ptid
), status
,
1145 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1146 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_SPURIOUS
)
1148 struct lwp_info
*new_lp
;
1149 new_lp
= add_lwp (BUILD_LWP (lp
->waitstatus
.value
.related_pid
,
1150 GET_PID (inferior_ptid
)));
1152 new_lp
->stopped
= 1;
1154 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1156 if (debug_linux_nat
)
1157 fprintf_unfiltered (gdb_stdlog
,
1158 "LLHE: Got clone event from LWP %ld, resuming\n",
1159 GET_LWP (lp
->ptid
));
1160 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1168 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1172 wait_lwp (struct lwp_info
*lp
)
1176 int thread_dead
= 0;
1178 gdb_assert (!lp
->stopped
);
1179 gdb_assert (lp
->status
== 0);
1181 pid
= waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1182 if (pid
== -1 && errno
== ECHILD
)
1184 pid
= waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1185 if (pid
== -1 && errno
== ECHILD
)
1187 /* The thread has previously exited. We need to delete it
1188 now because, for some vendor 2.4 kernels with NPTL
1189 support backported, there won't be an exit event unless
1190 it is the main thread. 2.6 kernels will report an exit
1191 event for each thread that exits, as expected. */
1193 if (debug_linux_nat
)
1194 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1195 target_pid_to_str (lp
->ptid
));
1201 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1203 if (debug_linux_nat
)
1205 fprintf_unfiltered (gdb_stdlog
,
1206 "WL: waitpid %s received %s\n",
1207 target_pid_to_str (lp
->ptid
),
1208 status_to_str (status
));
1212 /* Check if the thread has exited. */
1213 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1216 if (debug_linux_nat
)
1217 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1218 target_pid_to_str (lp
->ptid
));
1223 if (in_thread_list (lp
->ptid
))
1225 /* Core GDB cannot deal with us deleting the current thread. */
1226 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1227 delete_thread (lp
->ptid
);
1228 printf_unfiltered (_("[%s exited]\n"),
1229 target_pid_to_str (lp
->ptid
));
1232 delete_lwp (lp
->ptid
);
1236 gdb_assert (WIFSTOPPED (status
));
1238 /* Handle GNU/Linux's extended waitstatus for trace events. */
1239 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1241 if (debug_linux_nat
)
1242 fprintf_unfiltered (gdb_stdlog
,
1243 "WL: Handling extended status 0x%06x\n",
1245 if (linux_nat_handle_extended (lp
, status
))
1246 return wait_lwp (lp
);
1252 /* Send a SIGSTOP to LP. */
1255 stop_callback (struct lwp_info
*lp
, void *data
)
1257 if (!lp
->stopped
&& !lp
->signalled
)
1261 if (debug_linux_nat
)
1263 fprintf_unfiltered (gdb_stdlog
,
1264 "SC: kill %s **<SIGSTOP>**\n",
1265 target_pid_to_str (lp
->ptid
));
1268 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1269 if (debug_linux_nat
)
1271 fprintf_unfiltered (gdb_stdlog
,
1272 "SC: lwp kill %d %s\n",
1274 errno
? safe_strerror (errno
) : "ERRNO-OK");
1278 gdb_assert (lp
->status
== 0);
1284 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1285 a pointer to a set of signals to be flushed immediately. */
1288 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1290 sigset_t
*flush_mask
= data
;
1296 status
= wait_lwp (lp
);
1300 /* Ignore any signals in FLUSH_MASK. */
1301 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1310 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1311 if (debug_linux_nat
)
1312 fprintf_unfiltered (gdb_stdlog
,
1313 "PTRACE_CONT %s, 0, 0 (%s)\n",
1314 target_pid_to_str (lp
->ptid
),
1315 errno
? safe_strerror (errno
) : "OK");
1317 return stop_wait_callback (lp
, flush_mask
);
1320 if (WSTOPSIG (status
) != SIGSTOP
)
1322 if (WSTOPSIG (status
) == SIGTRAP
)
1324 /* If a LWP other than the LWP that we're reporting an
1325 event for has hit a GDB breakpoint (as opposed to
1326 some random trap signal), then just arrange for it to
1327 hit it again later. We don't keep the SIGTRAP status
1328 and don't forward the SIGTRAP signal to the LWP. We
1329 will handle the current event, eventually we will
1330 resume all LWPs, and this one will get its breakpoint
1333 If we do not do this, then we run the risk that the
1334 user will delete or disable the breakpoint, but the
1335 thread will have already tripped on it. */
1337 /* Now resume this LWP and get the SIGSTOP event. */
1339 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1340 if (debug_linux_nat
)
1342 fprintf_unfiltered (gdb_stdlog
,
1343 "PTRACE_CONT %s, 0, 0 (%s)\n",
1344 target_pid_to_str (lp
->ptid
),
1345 errno
? safe_strerror (errno
) : "OK");
1347 fprintf_unfiltered (gdb_stdlog
,
1348 "SWC: Candidate SIGTRAP event in %s\n",
1349 target_pid_to_str (lp
->ptid
));
1351 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1352 stop_wait_callback (lp
, data
);
1353 /* If there's another event, throw it back into the queue. */
1356 if (debug_linux_nat
)
1358 fprintf_unfiltered (gdb_stdlog
,
1359 "SWC: kill %s, %s\n",
1360 target_pid_to_str (lp
->ptid
),
1361 status_to_str ((int) status
));
1363 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1365 /* Save the sigtrap event. */
1366 lp
->status
= status
;
1371 /* The thread was stopped with a signal other than
1372 SIGSTOP, and didn't accidentally trip a breakpoint. */
1374 if (debug_linux_nat
)
1376 fprintf_unfiltered (gdb_stdlog
,
1377 "SWC: Pending event %s in %s\n",
1378 status_to_str ((int) status
),
1379 target_pid_to_str (lp
->ptid
));
1381 /* Now resume this LWP and get the SIGSTOP event. */
1383 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1384 if (debug_linux_nat
)
1385 fprintf_unfiltered (gdb_stdlog
,
1386 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1387 target_pid_to_str (lp
->ptid
),
1388 errno
? safe_strerror (errno
) : "OK");
1390 /* Hold this event/waitstatus while we check to see if
1391 there are any more (we still want to get that SIGSTOP). */
1392 stop_wait_callback (lp
, data
);
1393 /* If the lp->status field is still empty, use it to hold
1394 this event. If not, then this event must be returned
1395 to the event queue of the LWP. */
1396 if (lp
->status
== 0)
1397 lp
->status
= status
;
1400 if (debug_linux_nat
)
1402 fprintf_unfiltered (gdb_stdlog
,
1403 "SWC: kill %s, %s\n",
1404 target_pid_to_str (lp
->ptid
),
1405 status_to_str ((int) status
));
1407 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1414 /* We caught the SIGSTOP that we intended to catch, so
1415 there's no SIGSTOP pending. */
1424 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1425 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1428 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
1430 sigset_t blocked
, ignored
;
1433 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
1438 for (i
= 1; i
< NSIG
; i
++)
1439 if (sigismember (pending
, i
))
1440 if (!sigismember (flush_mask
, i
)
1441 || sigismember (&blocked
, i
)
1442 || sigismember (&ignored
, i
))
1443 sigdelset (pending
, i
);
1445 if (sigisemptyset (pending
))
1451 /* DATA is interpreted as a mask of signals to flush. If LP has
1452 signals pending, and they are all in the flush mask, then arrange
1453 to flush them. LP should be stopped, as should all other threads
1454 it might share a signal queue with. */
1457 flush_callback (struct lwp_info
*lp
, void *data
)
1459 sigset_t
*flush_mask
= data
;
1460 sigset_t pending
, intersection
, blocked
, ignored
;
1463 /* Normally, when an LWP exits, it is removed from the LWP list. The
1464 last LWP isn't removed till later, however. So if there is only
1465 one LWP on the list, make sure it's alive. */
1466 if (lwp_list
== lp
&& lp
->next
== NULL
)
1467 if (!linux_nat_thread_alive (lp
->ptid
))
1470 /* Just because the LWP is stopped doesn't mean that new signals
1471 can't arrive from outside, so this function must be careful of
1472 race conditions. However, because all threads are stopped, we
1473 can assume that the pending mask will not shrink unless we resume
1474 the LWP, and that it will then get another signal. We can't
1475 control which one, however. */
1479 if (debug_linux_nat
)
1480 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp
->status
);
1481 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
1485 while (linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
1490 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1491 if (debug_linux_nat
)
1492 fprintf_unfiltered (gdb_stderr
,
1493 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
1496 stop_wait_callback (lp
, flush_mask
);
1497 if (debug_linux_nat
)
1498 fprintf_unfiltered (gdb_stderr
,
1499 "FC: Wait finished; saved status is %d\n",
1506 /* Return non-zero if LP has a wait status pending. */
1509 status_callback (struct lwp_info
*lp
, void *data
)
1511 /* Only report a pending wait status if we pretend that this has
1512 indeed been resumed. */
1513 return (lp
->status
!= 0 && lp
->resumed
);
1516 /* Return non-zero if LP isn't stopped. */
1519 running_callback (struct lwp_info
*lp
, void *data
)
1521 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
1524 /* Count the LWP's that have had events. */
1527 count_events_callback (struct lwp_info
*lp
, void *data
)
1531 gdb_assert (count
!= NULL
);
1533 /* Count only LWPs that have a SIGTRAP event pending. */
1535 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1541 /* Select the LWP (if any) that is currently being single-stepped. */
1544 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
1546 if (lp
->step
&& lp
->status
!= 0)
1552 /* Select the Nth LWP that has had a SIGTRAP event. */
1555 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
1557 int *selector
= data
;
1559 gdb_assert (selector
!= NULL
);
1561 /* Select only LWPs that have a SIGTRAP event pending. */
1563 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1564 if ((*selector
)-- == 0)
1571 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
1573 struct lwp_info
*event_lp
= data
;
1575 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1579 /* If a LWP other than the LWP that we're reporting an event for has
1580 hit a GDB breakpoint (as opposed to some random trap signal),
1581 then just arrange for it to hit it again later. We don't keep
1582 the SIGTRAP status and don't forward the SIGTRAP signal to the
1583 LWP. We will handle the current event, eventually we will resume
1584 all LWPs, and this one will get its breakpoint trap again.
1586 If we do not do this, then we run the risk that the user will
1587 delete or disable the breakpoint, but the LWP will have already
1591 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
1592 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
1593 DECR_PC_AFTER_BREAK
))
1595 if (debug_linux_nat
)
1596 fprintf_unfiltered (gdb_stdlog
,
1597 "CBC: Push back breakpoint for %s\n",
1598 target_pid_to_str (lp
->ptid
));
1600 /* Back up the PC if necessary. */
1601 if (DECR_PC_AFTER_BREAK
)
1602 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
1604 /* Throw away the SIGTRAP. */
1611 /* Select one LWP out of those that have events pending. */
1614 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
1617 int random_selector
;
1618 struct lwp_info
*event_lp
;
1620 /* Record the wait status for the origional LWP. */
1621 (*orig_lp
)->status
= *status
;
1623 /* Give preference to any LWP that is being single-stepped. */
1624 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
1625 if (event_lp
!= NULL
)
1627 if (debug_linux_nat
)
1628 fprintf_unfiltered (gdb_stdlog
,
1629 "SEL: Select single-step %s\n",
1630 target_pid_to_str (event_lp
->ptid
));
1634 /* No single-stepping LWP. Select one at random, out of those
1635 which have had SIGTRAP events. */
1637 /* First see how many SIGTRAP events we have. */
1638 iterate_over_lwps (count_events_callback
, &num_events
);
1640 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1641 random_selector
= (int)
1642 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1644 if (debug_linux_nat
&& num_events
> 1)
1645 fprintf_unfiltered (gdb_stdlog
,
1646 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1647 num_events
, random_selector
);
1649 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
1653 if (event_lp
!= NULL
)
1655 /* Switch the event LWP. */
1656 *orig_lp
= event_lp
;
1657 *status
= event_lp
->status
;
1660 /* Flush the wait status for the event LWP. */
1661 (*orig_lp
)->status
= 0;
1664 /* Return non-zero if LP has been resumed. */
1667 resumed_callback (struct lwp_info
*lp
, void *data
)
1674 /* We need to override child_wait to support attaching to cloned
1675 processes, since a normal wait (as done by the default version)
1676 ignores those processes. */
1678 /* Wait for child PTID to do something. Return id of the child,
1679 minus_one_ptid in case of error; store status into *OURSTATUS. */
1682 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1688 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1692 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1693 attached process. */
1696 pid
= waitpid (GET_PID (ptid
), &status
, 0);
1697 if (pid
== -1 && errno
== ECHILD
)
1698 /* Try again with __WCLONE to check cloned processes. */
1699 pid
= waitpid (GET_PID (ptid
), &status
, __WCLONE
);
1701 if (debug_linux_nat
)
1703 fprintf_unfiltered (gdb_stdlog
,
1704 "CW: waitpid %ld received %s\n",
1705 (long) pid
, status_to_str (status
));
1710 /* Make sure we don't report an event for the exit of the
1711 original program, if we've detached from it. */
1712 if (pid
!= -1 && !WIFSTOPPED (status
) && pid
!= GET_PID (inferior_ptid
))
1718 /* Check for stop events reported by a process we didn't already
1719 know about - in this case, anything other than inferior_ptid.
1721 If we're expecting to receive stopped processes after fork,
1722 vfork, and clone events, then we'll just add the new one to
1723 our list and go back to waiting for the event to be reported
1724 - the stopped process might be returned from waitpid before
1725 or after the event is. If we want to handle debugging of
1726 CLONE_PTRACE processes we need to do more here, i.e. switch
1727 to multi-threaded mode. */
1728 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
1729 && pid
!= GET_PID (inferior_ptid
))
1731 linux_record_stopped_pid (pid
);
1736 /* Handle GNU/Linux's extended waitstatus for trace events. */
1737 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
1738 && status
>> 16 != 0)
1740 linux_handle_extended_wait (pid
, status
, ourstatus
);
1742 /* If we see a clone event, detach the child, and don't
1743 report the event. It would be nice to offer some way to
1744 switch into a non-thread-db based threaded mode at this
1746 if (ourstatus
->kind
== TARGET_WAITKIND_SPURIOUS
)
1748 ptrace (PTRACE_DETACH
, ourstatus
->value
.related_pid
, 0, 0);
1749 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1750 ptrace (PTRACE_CONT
, pid
, 0, 0);
1756 clear_sigio_trap ();
1757 clear_sigint_trap ();
1759 while (pid
== -1 && save_errno
== EINTR
);
1763 warning (_("Child process unexpectedly missing: %s"),
1764 safe_strerror (errno
));
1766 /* Claim it exited with unknown signal. */
1767 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1768 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1769 return minus_one_ptid
;
1772 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1773 store_waitstatus (ourstatus
, status
);
1775 return pid_to_ptid (pid
);
1780 /* Stop an active thread, verify it still exists, then resume it. */
1783 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
1785 struct lwp_info
*ptr
;
1787 if (!lp
->stopped
&& !lp
->signalled
)
1789 stop_callback (lp
, NULL
);
1790 stop_wait_callback (lp
, NULL
);
1791 /* Resume if the lwp still exists. */
1792 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
1795 resume_callback (lp
, NULL
);
1796 resume_set_callback (lp
, NULL
);
1803 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1805 struct lwp_info
*lp
= NULL
;
1808 pid_t pid
= PIDGET (ptid
);
1809 sigset_t flush_mask
;
1811 sigemptyset (&flush_mask
);
1813 /* Make sure SIGCHLD is blocked. */
1814 if (!sigismember (&blocked_mask
, SIGCHLD
))
1816 sigaddset (&blocked_mask
, SIGCHLD
);
1817 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1822 /* Make sure there is at least one LWP that has been resumed, at
1823 least if there are any LWPs at all. */
1824 gdb_assert (num_lwps
== 0 || iterate_over_lwps (resumed_callback
, NULL
));
1826 /* First check if there is a LWP with a wait status pending. */
1829 /* Any LWP that's been resumed will do. */
1830 lp
= iterate_over_lwps (status_callback
, NULL
);
1833 status
= lp
->status
;
1836 if (debug_linux_nat
&& status
)
1837 fprintf_unfiltered (gdb_stdlog
,
1838 "LLW: Using pending wait status %s for %s.\n",
1839 status_to_str (status
),
1840 target_pid_to_str (lp
->ptid
));
1843 /* But if we don't fine one, we'll have to wait, and check both
1844 cloned and uncloned processes. We start with the cloned
1846 options
= __WCLONE
| WNOHANG
;
1848 else if (is_lwp (ptid
))
1850 if (debug_linux_nat
)
1851 fprintf_unfiltered (gdb_stdlog
,
1852 "LLW: Waiting for specific LWP %s.\n",
1853 target_pid_to_str (ptid
));
1855 /* We have a specific LWP to check. */
1856 lp
= find_lwp_pid (ptid
);
1858 status
= lp
->status
;
1861 if (debug_linux_nat
&& status
)
1862 fprintf_unfiltered (gdb_stdlog
,
1863 "LLW: Using pending wait status %s for %s.\n",
1864 status_to_str (status
),
1865 target_pid_to_str (lp
->ptid
));
1867 /* If we have to wait, take into account whether PID is a cloned
1868 process or not. And we have to convert it to something that
1869 the layer beneath us can understand. */
1870 options
= lp
->cloned
? __WCLONE
: 0;
1871 pid
= GET_LWP (ptid
);
1874 if (status
&& lp
->signalled
)
1876 /* A pending SIGSTOP may interfere with the normal stream of
1877 events. In a typical case where interference is a problem,
1878 we have a SIGSTOP signal pending for LWP A while
1879 single-stepping it, encounter an event in LWP B, and take the
1880 pending SIGSTOP while trying to stop LWP A. After processing
1881 the event in LWP B, LWP A is continued, and we'll never see
1882 the SIGTRAP associated with the last time we were
1883 single-stepping LWP A. */
1885 /* Resume the thread. It should halt immediately returning the
1887 registers_changed ();
1888 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
1890 if (debug_linux_nat
)
1891 fprintf_unfiltered (gdb_stdlog
,
1892 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1893 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1894 target_pid_to_str (lp
->ptid
));
1896 gdb_assert (lp
->resumed
);
1898 /* This should catch the pending SIGSTOP. */
1899 stop_wait_callback (lp
, NULL
);
1902 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1903 attached process. */
1910 lwpid
= waitpid (pid
, &status
, options
);
1913 gdb_assert (pid
== -1 || lwpid
== pid
);
1915 if (debug_linux_nat
)
1917 fprintf_unfiltered (gdb_stdlog
,
1918 "LLW: waitpid %ld received %s\n",
1919 (long) lwpid
, status_to_str (status
));
1922 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
1924 /* Check for stop events reported by a process we didn't
1925 already know about - anything not already in our LWP
1928 If we're expecting to receive stopped processes after
1929 fork, vfork, and clone events, then we'll just add the
1930 new one to our list and go back to waiting for the event
1931 to be reported - the stopped process might be returned
1932 from waitpid before or after the event is. */
1933 if (WIFSTOPPED (status
) && !lp
)
1935 linux_record_stopped_pid (lwpid
);
1940 /* Make sure we don't report an event for the exit of an LWP not in
1941 our list, i.e. not part of the current process. This can happen
1942 if we detach from a program we original forked and then it
1944 if (!WIFSTOPPED (status
) && !lp
)
1950 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1951 CLONE_PTRACE processes which do not use the thread library -
1952 otherwise we wouldn't find the new LWP this way. That doesn't
1953 currently work, and the following code is currently unreachable
1954 due to the two blocks above. If it's fixed some day, this code
1955 should be broken out into a function so that we can also pick up
1956 LWPs from the new interface. */
1959 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
1960 if (options
& __WCLONE
)
1965 gdb_assert (WIFSTOPPED (status
)
1966 && WSTOPSIG (status
) == SIGSTOP
);
1969 if (!in_thread_list (inferior_ptid
))
1971 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
1972 GET_PID (inferior_ptid
));
1973 add_thread (inferior_ptid
);
1976 add_thread (lp
->ptid
);
1977 printf_unfiltered (_("[New %s]\n"),
1978 target_pid_to_str (lp
->ptid
));
1982 /* Handle GNU/Linux's extended waitstatus for trace events. */
1983 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1985 if (debug_linux_nat
)
1986 fprintf_unfiltered (gdb_stdlog
,
1987 "LLW: Handling extended status 0x%06x\n",
1989 if (linux_nat_handle_extended (lp
, status
))
1996 /* Check if the thread has exited. */
1997 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
1999 if (in_thread_list (lp
->ptid
))
2001 /* Core GDB cannot deal with us deleting the current
2003 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2004 delete_thread (lp
->ptid
);
2005 printf_unfiltered (_("[%s exited]\n"),
2006 target_pid_to_str (lp
->ptid
));
2009 /* If this is the main thread, we must stop all threads and
2010 verify if they are still alive. This is because in the nptl
2011 thread model, there is no signal issued for exiting LWPs
2012 other than the main thread. We only get the main thread
2013 exit signal once all child threads have already exited.
2014 If we stop all the threads and use the stop_wait_callback
2015 to check if they have exited we can determine whether this
2016 signal should be ignored or whether it means the end of the
2017 debugged application, regardless of which threading model
2019 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2022 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2025 if (debug_linux_nat
)
2026 fprintf_unfiltered (gdb_stdlog
,
2027 "LLW: %s exited.\n",
2028 target_pid_to_str (lp
->ptid
));
2030 delete_lwp (lp
->ptid
);
2032 /* If there is at least one more LWP, then the exit signal
2033 was not the end of the debugged application and should be
2037 /* Make sure there is at least one thread running. */
2038 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2040 /* Discard the event. */
2046 /* Check if the current LWP has previously exited. In the nptl
2047 thread model, LWPs other than the main thread do not issue
2048 signals when they exit so we must check whenever the thread
2049 has stopped. A similar check is made in stop_wait_callback(). */
2050 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2052 if (in_thread_list (lp
->ptid
))
2054 /* Core GDB cannot deal with us deleting the current
2056 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2057 delete_thread (lp
->ptid
);
2058 printf_unfiltered (_("[%s exited]\n"),
2059 target_pid_to_str (lp
->ptid
));
2061 if (debug_linux_nat
)
2062 fprintf_unfiltered (gdb_stdlog
,
2063 "LLW: %s exited.\n",
2064 target_pid_to_str (lp
->ptid
));
2066 delete_lwp (lp
->ptid
);
2068 /* Make sure there is at least one thread running. */
2069 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2071 /* Discard the event. */
2076 /* Make sure we don't report a SIGSTOP that we sent
2077 ourselves in an attempt to stop an LWP. */
2079 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2081 if (debug_linux_nat
)
2082 fprintf_unfiltered (gdb_stdlog
,
2083 "LLW: Delayed SIGSTOP caught for %s.\n",
2084 target_pid_to_str (lp
->ptid
));
2086 /* This is a delayed SIGSTOP. */
2089 registers_changed ();
2090 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
2092 if (debug_linux_nat
)
2093 fprintf_unfiltered (gdb_stdlog
,
2094 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2096 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2097 target_pid_to_str (lp
->ptid
));
2100 gdb_assert (lp
->resumed
);
2102 /* Discard the event. */
2112 /* Alternate between checking cloned and uncloned processes. */
2113 options
^= __WCLONE
;
2115 /* And suspend every time we have checked both. */
2116 if (options
& __WCLONE
)
2117 sigsuspend (&suspend_mask
);
2120 /* We shouldn't end up here unless we want to try again. */
2121 gdb_assert (status
== 0);
2124 clear_sigio_trap ();
2125 clear_sigint_trap ();
2129 /* Don't report signals that GDB isn't interested in, such as
2130 signals that are neither printed nor stopped upon. Stopping all
2131 threads can be a bit time-consuming so if we want decent
2132 performance with heavily multi-threaded programs, especially when
2133 they're using a high frequency timer, we'd better avoid it if we
2136 if (WIFSTOPPED (status
))
2138 int signo
= target_signal_from_host (WSTOPSIG (status
));
2140 if (signal_stop_state (signo
) == 0
2141 && signal_print_state (signo
) == 0
2142 && signal_pass_state (signo
) == 1)
2144 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2145 here? It is not clear we should. GDB may not expect
2146 other threads to run. On the other hand, not resuming
2147 newly attached threads may cause an unwanted delay in
2148 getting them running. */
2149 registers_changed ();
2150 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
, signo
);
2151 if (debug_linux_nat
)
2152 fprintf_unfiltered (gdb_stdlog
,
2153 "LLW: %s %s, %s (preempt 'handle')\n",
2155 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2156 target_pid_to_str (lp
->ptid
),
2157 signo
? strsignal (signo
) : "0");
2163 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2165 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2166 forwarded to the entire process group, that is, all LWP's
2167 will receive it. Since we only want to report it once,
2168 we try to flush it from all LWPs except this one. */
2169 sigaddset (&flush_mask
, SIGINT
);
2173 /* This LWP is stopped now. */
2176 if (debug_linux_nat
)
2177 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2178 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2180 /* Now stop all other LWP's ... */
2181 iterate_over_lwps (stop_callback
, NULL
);
2183 /* ... and wait until all of them have reported back that they're no
2185 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2186 iterate_over_lwps (flush_callback
, &flush_mask
);
2188 /* If we're not waiting for a specific LWP, choose an event LWP from
2189 among those that have had events. Giving equal priority to all
2190 LWPs that have had events helps prevent starvation. */
2192 select_event_lwp (&lp
, &status
);
2194 /* Now that we've selected our final event LWP, cancel any
2195 breakpoints in other LWPs that have hit a GDB breakpoint. See
2196 the comment in cancel_breakpoints_callback to find out why. */
2197 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2199 /* If we're not running in "threaded" mode, we'll report the bare
2202 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2204 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2205 if (debug_linux_nat
)
2206 fprintf_unfiltered (gdb_stdlog
,
2207 "LLW: trap_ptid is %s.\n",
2208 target_pid_to_str (trap_ptid
));
2211 trap_ptid
= null_ptid
;
2213 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2215 *ourstatus
= lp
->waitstatus
;
2216 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2219 store_waitstatus (ourstatus
, status
);
2221 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2225 kill_callback (struct lwp_info
*lp
, void *data
)
2228 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2229 if (debug_linux_nat
)
2230 fprintf_unfiltered (gdb_stdlog
,
2231 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2232 target_pid_to_str (lp
->ptid
),
2233 errno
? safe_strerror (errno
) : "OK");
2239 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2243 /* We must make sure that there are no pending events (delayed
2244 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2245 program doesn't interfere with any following debugging session. */
2247 /* For cloned processes we must check both with __WCLONE and
2248 without, since the exit status of a cloned process isn't reported
2254 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2255 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2257 fprintf_unfiltered (gdb_stdlog
,
2258 "KWC: wait %s received unknown.\n",
2259 target_pid_to_str (lp
->ptid
));
2262 while (pid
== GET_LWP (lp
->ptid
));
2264 gdb_assert (pid
== -1 && errno
== ECHILD
);
2269 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2270 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2272 fprintf_unfiltered (gdb_stdlog
,
2273 "KWC: wait %s received unk.\n",
2274 target_pid_to_str (lp
->ptid
));
2277 while (pid
== GET_LWP (lp
->ptid
));
2279 gdb_assert (pid
== -1 && errno
== ECHILD
);
2284 linux_nat_kill (void)
2286 /* Kill all LWP's ... */
2287 iterate_over_lwps (kill_callback
, NULL
);
2289 /* ... and wait until we've flushed all events. */
2290 iterate_over_lwps (kill_wait_callback
, NULL
);
2292 target_mourn_inferior ();
2296 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
2299 deprecated_child_ops
.to_create_inferior (exec_file
, allargs
, env
, from_tty
);
2303 linux_nat_mourn_inferior (void)
2305 trap_ptid
= null_ptid
;
2307 /* Destroy LWP info; it's no longer valid. */
2310 /* Restore the original signal mask. */
2311 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
2312 sigemptyset (&blocked_mask
);
2314 deprecated_child_ops
.to_mourn_inferior ();
2318 linux_nat_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
2319 struct mem_attrib
*attrib
, struct target_ops
*target
)
2321 struct cleanup
*old_chain
= save_inferior_ptid ();
2324 if (is_lwp (inferior_ptid
))
2325 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2327 xfer
= linux_proc_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2329 xfer
= child_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2331 do_cleanups (old_chain
);
2336 linux_nat_thread_alive (ptid_t ptid
)
2338 gdb_assert (is_lwp (ptid
));
2341 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2342 if (debug_linux_nat
)
2343 fprintf_unfiltered (gdb_stdlog
,
2344 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2345 target_pid_to_str (ptid
),
2346 errno
? safe_strerror (errno
) : "OK");
2354 linux_nat_pid_to_str (ptid_t ptid
)
2356 static char buf
[64];
2360 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2364 return normal_pid_to_str (ptid
);
2368 init_linux_nat_ops (void)
2371 linux_nat_ops
.to_open
= linux_nat_open
;
2373 linux_nat_ops
.to_shortname
= "lwp-layer";
2374 linux_nat_ops
.to_longname
= "lwp-layer";
2375 linux_nat_ops
.to_doc
= "Low level threads support (LWP layer)";
2376 linux_nat_ops
.to_attach
= linux_nat_attach
;
2377 linux_nat_ops
.to_detach
= linux_nat_detach
;
2378 linux_nat_ops
.to_resume
= linux_nat_resume
;
2379 linux_nat_ops
.to_wait
= linux_nat_wait
;
2380 /* fetch_inferior_registers and store_inferior_registers will
2381 honor the LWP id, so we can use them directly. */
2382 linux_nat_ops
.to_fetch_registers
= fetch_inferior_registers
;
2383 linux_nat_ops
.to_store_registers
= store_inferior_registers
;
2384 linux_nat_ops
.deprecated_xfer_memory
= linux_nat_xfer_memory
;
2385 linux_nat_ops
.to_kill
= linux_nat_kill
;
2386 linux_nat_ops
.to_create_inferior
= linux_nat_create_inferior
;
2387 linux_nat_ops
.to_mourn_inferior
= linux_nat_mourn_inferior
;
2388 linux_nat_ops
.to_thread_alive
= linux_nat_thread_alive
;
2389 linux_nat_ops
.to_pid_to_str
= linux_nat_pid_to_str
;
2390 linux_nat_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
2391 linux_nat_ops
.to_post_attach
= child_post_attach
;
2392 linux_nat_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
2393 linux_nat_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
2394 linux_nat_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
2396 linux_nat_ops
.to_stratum
= thread_stratum
;
2397 linux_nat_ops
.to_has_thread_control
= tc_schedlock
;
2398 linux_nat_ops
.to_magic
= OPS_MAGIC
;
2402 sigchld_handler (int signo
)
2404 /* Do nothing. The only reason for this handler is that it allows
2405 us to use sigsuspend in linux_nat_wait above to wait for the
2406 arrival of a SIGCHLD. */
2409 /* Accepts an integer PID; Returns a string representing a file that
2410 can be opened to get the symbols for the child process. */
2413 child_pid_to_exec_file (int pid
)
2415 char *name1
, *name2
;
2417 name1
= xmalloc (MAXPATHLEN
);
2418 name2
= xmalloc (MAXPATHLEN
);
2419 make_cleanup (xfree
, name1
);
2420 make_cleanup (xfree
, name2
);
2421 memset (name2
, 0, MAXPATHLEN
);
2423 sprintf (name1
, "/proc/%d/exe", pid
);
2424 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
2430 /* Service function for corefiles and info proc. */
2433 read_mapping (FILE *mapfile
,
2438 char *device
, long long *inode
, char *filename
)
2440 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
2441 addr
, endaddr
, permissions
, offset
, device
, inode
);
2443 if (ret
> 0 && ret
!= EOF
&& *inode
!= 0)
2445 /* Eat everything up to EOL for the filename. This will prevent
2446 weird filenames (such as one with embedded whitespace) from
2447 confusing this code. It also makes this code more robust in
2448 respect to annotations the kernel may add after the filename.
2450 Note the filename is used for informational purposes
2452 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
2456 filename
[0] = '\0'; /* no filename */
2457 fscanf (mapfile
, "\n");
2459 return (ret
!= 0 && ret
!= EOF
);
2462 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2463 regions in the inferior for a corefile. */
2466 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
2468 int, int, int, void *), void *obfd
)
2470 long long pid
= PIDGET (inferior_ptid
);
2471 char mapsfilename
[MAXPATHLEN
];
2473 long long addr
, endaddr
, size
, offset
, inode
;
2474 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2475 int read
, write
, exec
;
2478 /* Compose the filename for the /proc memory map, and open it. */
2479 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
2480 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
2481 error (_("Could not open %s."), mapsfilename
);
2484 fprintf_filtered (gdb_stdout
,
2485 "Reading memory regions from %s\n", mapsfilename
);
2487 /* Now iterate until end-of-file. */
2488 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
2489 &offset
, &device
[0], &inode
, &filename
[0]))
2491 size
= endaddr
- addr
;
2493 /* Get the segment's permissions. */
2494 read
= (strchr (permissions
, 'r') != 0);
2495 write
= (strchr (permissions
, 'w') != 0);
2496 exec
= (strchr (permissions
, 'x') != 0);
2500 fprintf_filtered (gdb_stdout
,
2501 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2502 size
, paddr_nz (addr
),
2504 write
? 'w' : ' ', exec
? 'x' : ' ');
2505 if (filename
&& filename
[0])
2506 fprintf_filtered (gdb_stdout
, " for %s", filename
);
2507 fprintf_filtered (gdb_stdout
, "\n");
2510 /* Invoke the callback function to create the corefile
2512 func (addr
, size
, read
, write
, exec
, obfd
);
2518 /* Records the thread's register state for the corefile note
2522 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
2523 char *note_data
, int *note_size
)
2525 gdb_gregset_t gregs
;
2526 gdb_fpregset_t fpregs
;
2527 #ifdef FILL_FPXREGSET
2528 gdb_fpxregset_t fpxregs
;
2530 unsigned long lwp
= ptid_get_lwp (ptid
);
2532 fill_gregset (&gregs
, -1);
2533 note_data
= (char *) elfcore_write_prstatus (obfd
,
2537 stop_signal
, &gregs
);
2539 fill_fpregset (&fpregs
, -1);
2540 note_data
= (char *) elfcore_write_prfpreg (obfd
,
2543 &fpregs
, sizeof (fpregs
));
2544 #ifdef FILL_FPXREGSET
2545 fill_fpxregset (&fpxregs
, -1);
2546 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
2549 &fpxregs
, sizeof (fpxregs
));
2554 struct linux_nat_corefile_thread_data
2562 /* Called by gdbthread.c once per thread. Records the thread's
2563 register state for the corefile note section. */
2566 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
2568 struct linux_nat_corefile_thread_data
*args
= data
;
2569 ptid_t saved_ptid
= inferior_ptid
;
2571 inferior_ptid
= ti
->ptid
;
2572 registers_changed ();
2573 target_fetch_registers (-1); /* FIXME should not be necessary;
2574 fill_gregset should do it automatically. */
2575 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
2580 inferior_ptid
= saved_ptid
;
2581 registers_changed ();
2582 target_fetch_registers (-1); /* FIXME should not be necessary;
2583 fill_gregset should do it automatically. */
2587 /* Records the register state for the corefile note section. */
2590 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
2591 char *note_data
, int *note_size
)
2593 registers_changed ();
2594 target_fetch_registers (-1); /* FIXME should not be necessary;
2595 fill_gregset should do it automatically. */
2596 return linux_nat_do_thread_registers (obfd
,
2597 ptid_build (ptid_get_pid (inferior_ptid
),
2598 ptid_get_pid (inferior_ptid
),
2600 note_data
, note_size
);
2604 /* Fills the "to_make_corefile_note" target vector. Builds the note
2605 section for a corefile, and returns it in a malloc buffer. */
2608 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
2610 struct linux_nat_corefile_thread_data thread_args
;
2611 struct cleanup
*old_chain
;
2612 char fname
[16] = { '\0' };
2613 char psargs
[80] = { '\0' };
2614 char *note_data
= NULL
;
2615 ptid_t current_ptid
= inferior_ptid
;
2619 if (get_exec_file (0))
2621 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
2622 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
2623 if (get_inferior_args ())
2625 strncat (psargs
, " ", sizeof (psargs
) - strlen (psargs
));
2626 strncat (psargs
, get_inferior_args (),
2627 sizeof (psargs
) - strlen (psargs
));
2629 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
2631 note_size
, fname
, psargs
);
2634 /* Dump information for threads. */
2635 thread_args
.obfd
= obfd
;
2636 thread_args
.note_data
= note_data
;
2637 thread_args
.note_size
= note_size
;
2638 thread_args
.num_notes
= 0;
2639 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
2640 if (thread_args
.num_notes
== 0)
2642 /* iterate_over_threads didn't come up with any threads; just
2643 use inferior_ptid. */
2644 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
2645 note_data
, note_size
);
2649 note_data
= thread_args
.note_data
;
2652 auxv_len
= target_auxv_read (¤t_target
, &auxv
);
2655 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
2656 "CORE", NT_AUXV
, auxv
, auxv_len
);
2660 make_cleanup (xfree
, note_data
);
2664 /* Implement the "info proc" command. */
2667 linux_nat_info_proc_cmd (char *args
, int from_tty
)
2669 long long pid
= PIDGET (inferior_ptid
);
2672 char buffer
[MAXPATHLEN
];
2673 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
2686 /* Break up 'args' into an argv array. */
2687 if ((argv
= buildargv (args
)) == NULL
)
2690 make_cleanup_freeargv (argv
);
2692 while (argv
!= NULL
&& *argv
!= NULL
)
2694 if (isdigit (argv
[0][0]))
2696 pid
= strtoul (argv
[0], NULL
, 10);
2698 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
2702 else if (strcmp (argv
[0], "status") == 0)
2706 else if (strcmp (argv
[0], "stat") == 0)
2710 else if (strcmp (argv
[0], "cmd") == 0)
2714 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
2718 else if (strcmp (argv
[0], "cwd") == 0)
2722 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
2728 /* [...] (future options here) */
2733 error (_("No current process: you must name one."));
2735 sprintf (fname1
, "/proc/%lld", pid
);
2736 if (stat (fname1
, &dummy
) != 0)
2737 error (_("No /proc directory: '%s'"), fname1
);
2739 printf_filtered (_("process %lld\n"), pid
);
2740 if (cmdline_f
|| all
)
2742 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
2743 if ((procfile
= fopen (fname1
, "r")) > 0)
2745 fgets (buffer
, sizeof (buffer
), procfile
);
2746 printf_filtered ("cmdline = '%s'\n", buffer
);
2750 warning (_("unable to open /proc file '%s'"), fname1
);
2754 sprintf (fname1
, "/proc/%lld/cwd", pid
);
2755 memset (fname2
, 0, sizeof (fname2
));
2756 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2757 printf_filtered ("cwd = '%s'\n", fname2
);
2759 warning (_("unable to read link '%s'"), fname1
);
2763 sprintf (fname1
, "/proc/%lld/exe", pid
);
2764 memset (fname2
, 0, sizeof (fname2
));
2765 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2766 printf_filtered ("exe = '%s'\n", fname2
);
2768 warning (_("unable to read link '%s'"), fname1
);
2770 if (mappings_f
|| all
)
2772 sprintf (fname1
, "/proc/%lld/maps", pid
);
2773 if ((procfile
= fopen (fname1
, "r")) > 0)
2775 long long addr
, endaddr
, size
, offset
, inode
;
2776 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2778 printf_filtered (_("Mapped address spaces:\n\n"));
2779 if (TARGET_ADDR_BIT
== 32)
2781 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2784 " Size", " Offset", "objfile");
2788 printf_filtered (" %18s %18s %10s %10s %7s\n",
2791 " Size", " Offset", "objfile");
2794 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
2795 &offset
, &device
[0], &inode
, &filename
[0]))
2797 size
= endaddr
- addr
;
2799 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2800 calls here (and possibly above) should be abstracted
2801 out into their own functions? Andrew suggests using
2802 a generic local_address_string instead to print out
2803 the addresses; that makes sense to me, too. */
2805 if (TARGET_ADDR_BIT
== 32)
2807 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2808 (unsigned long) addr
, /* FIXME: pr_addr */
2809 (unsigned long) endaddr
,
2811 (unsigned int) offset
,
2812 filename
[0] ? filename
: "");
2816 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2817 (unsigned long) addr
, /* FIXME: pr_addr */
2818 (unsigned long) endaddr
,
2820 (unsigned int) offset
,
2821 filename
[0] ? filename
: "");
2828 warning (_("unable to open /proc file '%s'"), fname1
);
2830 if (status_f
|| all
)
2832 sprintf (fname1
, "/proc/%lld/status", pid
);
2833 if ((procfile
= fopen (fname1
, "r")) > 0)
2835 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
2836 puts_filtered (buffer
);
2840 warning (_("unable to open /proc file '%s'"), fname1
);
2844 sprintf (fname1
, "/proc/%lld/stat", pid
);
2845 if ((procfile
= fopen (fname1
, "r")) > 0)
2850 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2851 printf_filtered (_("Process: %d\n"), itmp
);
2852 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
2853 printf_filtered (_("Exec file: %s\n"), buffer
);
2854 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
2855 printf_filtered (_("State: %c\n"), ctmp
);
2856 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2857 printf_filtered (_("Parent process: %d\n"), itmp
);
2858 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2859 printf_filtered (_("Process group: %d\n"), itmp
);
2860 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2861 printf_filtered (_("Session id: %d\n"), itmp
);
2862 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2863 printf_filtered (_("TTY: %d\n"), itmp
);
2864 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2865 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
2866 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2867 printf_filtered (_("Flags: 0x%x\n"), itmp
);
2868 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2869 printf_filtered (_("Minor faults (no memory page): %u\n"),
2870 (unsigned int) itmp
);
2871 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2872 printf_filtered (_("Minor faults, children: %u\n"),
2873 (unsigned int) itmp
);
2874 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2875 printf_filtered (_("Major faults (memory page faults): %u\n"),
2876 (unsigned int) itmp
);
2877 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2878 printf_filtered (_("Major faults, children: %u\n"),
2879 (unsigned int) itmp
);
2880 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2881 printf_filtered ("utime: %d\n", itmp
);
2882 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2883 printf_filtered ("stime: %d\n", itmp
);
2884 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2885 printf_filtered ("utime, children: %d\n", itmp
);
2886 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2887 printf_filtered ("stime, children: %d\n", itmp
);
2888 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2889 printf_filtered (_("jiffies remaining in current time slice: %d\n"),
2891 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2892 printf_filtered ("'nice' value: %d\n", itmp
);
2893 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2894 printf_filtered (_("jiffies until next timeout: %u\n"),
2895 (unsigned int) itmp
);
2896 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2897 printf_filtered ("jiffies until next SIGALRM: %u\n",
2898 (unsigned int) itmp
);
2899 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2900 printf_filtered (_("start time (jiffies since system boot): %d\n"),
2902 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2903 printf_filtered (_("Virtual memory size: %u\n"),
2904 (unsigned int) itmp
);
2905 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2906 printf_filtered (_("Resident set size: %u\n"), (unsigned int) itmp
);
2907 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2908 printf_filtered ("rlim: %u\n", (unsigned int) itmp
);
2909 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2910 printf_filtered (_("Start of text: 0x%x\n"), itmp
);
2911 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2912 printf_filtered (_("End of text: 0x%x\n"), itmp
);
2913 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2914 printf_filtered (_("Start of stack: 0x%x\n"), itmp
);
2915 #if 0 /* Don't know how architecture-dependent the rest is...
2916 Anyway the signal bitmap info is available from "status". */
2917 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2918 printf_filtered (_("Kernel stack pointer: 0x%x\n"), itmp
);
2919 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2920 printf_filtered (_("Kernel instr pointer: 0x%x\n"), itmp
);
2921 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2922 printf_filtered (_("Pending signals bitmap: 0x%x\n"), itmp
);
2923 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2924 printf_filtered (_("Blocked signals bitmap: 0x%x\n"), itmp
);
2925 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2926 printf_filtered (_("Ignored signals bitmap: 0x%x\n"), itmp
);
2927 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2928 printf_filtered (_("Catched signals bitmap: 0x%x\n"), itmp
);
2929 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2930 printf_filtered (_("wchan (system call): 0x%x\n"), itmp
);
2935 warning (_("unable to open /proc file '%s'"), fname1
);
2940 linux_proc_xfer_memory (CORE_ADDR addr
, char *myaddr
, int len
, int write
,
2941 struct mem_attrib
*attrib
, struct target_ops
*target
)
2949 /* Don't bother for one word. */
2950 if (len
< 3 * sizeof (long))
2953 /* We could keep this file open and cache it - possibly one per
2954 thread. That requires some juggling, but is even faster. */
2955 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
2956 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
2960 /* If pread64 is available, use it. It's faster if the kernel
2961 supports it (only one syscall), and it's 64-bit safe even on
2962 32-bit platforms (for instance, SPARC debugging a SPARC64
2965 if (pread64 (fd
, myaddr
, len
, addr
) != len
)
2967 if (lseek (fd
, addr
, SEEK_SET
) == -1 || read (fd
, myaddr
, len
) != len
)
2977 /* Parse LINE as a signal set and add its set bits to SIGS. */
2980 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
2982 int len
= strlen (line
) - 1;
2986 if (line
[len
] != '\n')
2987 error (_("Could not parse signal set: %s"), line
);
2995 if (*p
>= '0' && *p
<= '9')
2997 else if (*p
>= 'a' && *p
<= 'f')
2998 digit
= *p
- 'a' + 10;
3000 error (_("Could not parse signal set: %s"), line
);
3005 sigaddset (sigs
, signum
+ 1);
3007 sigaddset (sigs
, signum
+ 2);
3009 sigaddset (sigs
, signum
+ 3);
3011 sigaddset (sigs
, signum
+ 4);
3017 /* Find process PID's pending signals from /proc/pid/status and set
3021 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3024 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3027 sigemptyset (pending
);
3028 sigemptyset (blocked
);
3029 sigemptyset (ignored
);
3030 sprintf (fname
, "/proc/%d/status", pid
);
3031 procfile
= fopen (fname
, "r");
3032 if (procfile
== NULL
)
3033 error (_("Could not open %s"), fname
);
3035 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3037 /* Normal queued signals are on the SigPnd line in the status
3038 file. However, 2.6 kernels also have a "shared" pending
3039 queue for delivering signals to a thread group, so check for
3042 Unfortunately some Red Hat kernels include the shared pending
3043 queue but not the ShdPnd status field. */
3045 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3046 add_line_to_sigset (buffer
+ 8, pending
);
3047 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3048 add_line_to_sigset (buffer
+ 8, pending
);
3049 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3050 add_line_to_sigset (buffer
+ 8, blocked
);
3051 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3052 add_line_to_sigset (buffer
+ 8, ignored
);
3059 _initialize_linux_nat (void)
3061 struct sigaction action
;
3062 extern void thread_db_init (struct target_ops
*);
3064 deprecated_child_ops
.to_find_memory_regions
= linux_nat_find_memory_regions
;
3065 deprecated_child_ops
.to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3067 add_info ("proc", linux_nat_info_proc_cmd
, _("\
3068 Show /proc process information about any running process.\n\
3069 Specify any process id, or use the program being debugged by default.\n\
3070 Specify any of the following keywords for detailed info:\n\
3071 mappings -- list of mapped memory regions.\n\
3072 stat -- list a bunch of random process info.\n\
3073 status -- list a different bunch of random process info.\n\
3074 all -- list all available /proc info."));
3076 init_linux_nat_ops ();
3077 add_target (&linux_nat_ops
);
3078 thread_db_init (&linux_nat_ops
);
3080 /* Save the original signal mask. */
3081 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
3083 action
.sa_handler
= sigchld_handler
;
3084 sigemptyset (&action
.sa_mask
);
3085 action
.sa_flags
= 0;
3086 sigaction (SIGCHLD
, &action
, NULL
);
3088 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3089 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
3090 sigdelset (&suspend_mask
, SIGCHLD
);
3092 sigemptyset (&blocked_mask
);
3094 add_setshow_zinteger_cmd ("lin-lwp", no_class
, &debug_linux_nat
, _("\
3095 Set debugging of GNU/Linux lwp module."), _("\
3096 Show debugging of GNU/Linux lwp module."), _("\
3097 Enables printf debugging output."),
3099 show_debug_linux_nat
,
3100 &setdebuglist
, &showdebuglist
);
3104 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3105 the GNU/Linux Threads library and therefore doesn't really belong
3108 /* Read variable NAME in the target and return its value if found.
3109 Otherwise return zero. It is assumed that the type of the variable
3113 get_signo (const char *name
)
3115 struct minimal_symbol
*ms
;
3118 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
3122 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (char *) &signo
,
3123 sizeof (signo
)) != 0)
3129 /* Return the set of signals used by the threads library in *SET. */
3132 lin_thread_get_thread_signals (sigset_t
*set
)
3134 struct sigaction action
;
3135 int restart
, cancel
;
3139 restart
= get_signo ("__pthread_sig_restart");
3143 cancel
= get_signo ("__pthread_sig_cancel");
3147 sigaddset (set
, restart
);
3148 sigaddset (set
, cancel
);
3150 /* The GNU/Linux Threads library makes terminating threads send a
3151 special "cancel" signal instead of SIGCHLD. Make sure we catch
3152 those (to prevent them from terminating GDB itself, which is
3153 likely to be their default action) and treat them the same way as
3156 action
.sa_handler
= sigchld_handler
;
3157 sigemptyset (&action
.sa_mask
);
3158 action
.sa_flags
= 0;
3159 sigaction (cancel
, &action
, NULL
);
3161 /* We block the "cancel" signal throughout this code ... */
3162 sigaddset (&blocked_mask
, cancel
);
3163 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
3165 /* ... except during a sigsuspend. */
3166 sigdelset (&suspend_mask
, cancel
);