1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright 2001, 2002, 2003, 2004, 2005 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_lwp (ptid
);
300 pid
= ptid_get_pid (ptid
);
302 if (! linux_supports_tracefork (pid
))
305 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
306 | PTRACE_O_TRACECLONE
;
307 if (linux_supports_tracevforkdone (pid
))
308 options
|= PTRACE_O_TRACEVFORKDONE
;
310 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
311 read-only process state. */
313 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
317 child_post_attach (int pid
)
319 linux_enable_event_reporting (pid_to_ptid (pid
));
323 linux_child_post_startup_inferior (ptid_t ptid
)
325 linux_enable_event_reporting (ptid
);
328 #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
330 child_post_startup_inferior (ptid_t ptid
)
332 linux_child_post_startup_inferior (ptid
);
337 child_follow_fork (struct target_ops
*ops
, int follow_child
)
340 struct target_waitstatus last_status
;
342 int parent_pid
, child_pid
;
344 get_last_target_status (&last_ptid
, &last_status
);
345 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
346 parent_pid
= ptid_get_lwp (last_ptid
);
348 parent_pid
= ptid_get_pid (last_ptid
);
349 child_pid
= last_status
.value
.related_pid
;
353 /* We're already attached to the parent, by default. */
355 /* Before detaching from the child, remove all breakpoints from
356 it. (This won't actually modify the breakpoint list, but will
357 physically remove the breakpoints from the child.) */
358 /* If we vforked this will remove the breakpoints from the parent
359 also, but they'll be reinserted below. */
360 detach_breakpoints (child_pid
);
364 target_terminal_ours ();
365 fprintf_unfiltered (gdb_stdlog
,
366 "Detaching after fork from child process %d.\n",
370 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
374 gdb_assert (linux_supports_tracefork_flag
>= 0);
375 if (linux_supports_tracevforkdone (0))
379 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
380 my_waitpid (parent_pid
, &status
, __WALL
);
381 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
382 warning (_("Unexpected waitpid result %06x when waiting for "
383 "vfork-done"), status
);
387 /* We can't insert breakpoints until the child has
388 finished with the shared memory region. We need to
389 wait until that happens. Ideal would be to just
391 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
392 - waitpid (parent_pid, &status, __WALL);
393 However, most architectures can't handle a syscall
394 being traced on the way out if it wasn't traced on
397 We might also think to loop, continuing the child
398 until it exits or gets a SIGTRAP. One problem is
399 that the child might call ptrace with PTRACE_TRACEME.
401 There's no simple and reliable way to figure out when
402 the vforked child will be done with its copy of the
403 shared memory. We could step it out of the syscall,
404 two instructions, let it go, and then single-step the
405 parent once. When we have hardware single-step, this
406 would work; with software single-step it could still
407 be made to work but we'd have to be able to insert
408 single-step breakpoints in the child, and we'd have
409 to insert -just- the single-step breakpoint in the
410 parent. Very awkward.
412 In the end, the best we can do is to make sure it
413 runs for a little while. Hopefully it will be out of
414 range of any breakpoints we reinsert. Usually this
415 is only the single-step breakpoint at vfork's return
421 /* Since we vforked, breakpoints were removed in the parent
422 too. Put them back. */
423 reattach_breakpoints (parent_pid
);
428 char child_pid_spelling
[40];
430 /* Needed to keep the breakpoint lists in sync. */
432 detach_breakpoints (child_pid
);
434 /* Before detaching from the parent, remove all breakpoints from it. */
435 remove_breakpoints ();
439 target_terminal_ours ();
440 fprintf_unfiltered (gdb_stdlog
,
441 "Attaching after fork to child process %d.\n",
445 /* If we're vforking, we may want to hold on to the parent until
446 the child exits or execs. At exec time we can remove the old
447 breakpoints from the parent and detach it; at exit time we
448 could do the same (or even, sneakily, resume debugging it - the
449 child's exec has failed, or something similar).
451 This doesn't clean up "properly", because we can't call
452 target_detach, but that's OK; if the current target is "child",
453 then it doesn't need any further cleanups, and lin_lwp will
454 generally not encounter vfork (vfork is defined to fork
457 The holding part is very easy if we have VFORKDONE events;
458 but keeping track of both processes is beyond GDB at the
459 moment. So we don't expose the parent to the rest of GDB.
460 Instead we quietly hold onto it until such time as we can
464 linux_parent_pid
= parent_pid
;
466 target_detach (NULL
, 0);
468 inferior_ptid
= pid_to_ptid (child_pid
);
470 /* Reinstall ourselves, since we might have been removed in
471 target_detach (which does other necessary cleanup). */
474 /* Reset breakpoints in the child as appropriate. */
475 follow_inferior_reset_breakpoints ();
482 linux_handle_extended_wait (int pid
, int status
,
483 struct target_waitstatus
*ourstatus
)
485 int event
= status
>> 16;
487 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
488 || event
== PTRACE_EVENT_CLONE
)
490 unsigned long new_pid
;
493 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
495 /* If we haven't already seen the new PID stop, wait for it now. */
496 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
498 /* The new child has a pending SIGSTOP. We can't affect it until it
499 hits the SIGSTOP, but we're already attached. */
500 ret
= my_waitpid (new_pid
, &status
,
501 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
503 perror_with_name (_("waiting for new child"));
504 else if (ret
!= new_pid
)
505 internal_error (__FILE__
, __LINE__
,
506 _("wait returned unexpected PID %d"), ret
);
507 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
508 internal_error (__FILE__
, __LINE__
,
509 _("wait returned unexpected status 0x%x"), status
);
512 if (event
== PTRACE_EVENT_FORK
)
513 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
514 else if (event
== PTRACE_EVENT_VFORK
)
515 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
517 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
519 ourstatus
->value
.related_pid
= new_pid
;
520 return inferior_ptid
;
523 if (event
== PTRACE_EVENT_EXEC
)
525 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
526 ourstatus
->value
.execd_pathname
527 = xstrdup (child_pid_to_exec_file (pid
));
529 if (linux_parent_pid
)
531 detach_breakpoints (linux_parent_pid
);
532 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
534 linux_parent_pid
= 0;
537 return inferior_ptid
;
540 internal_error (__FILE__
, __LINE__
,
541 _("unknown ptrace event %d"), event
);
546 child_insert_fork_catchpoint (int pid
)
548 if (! linux_supports_tracefork (pid
))
549 error (_("Your system does not support fork catchpoints."));
553 child_insert_vfork_catchpoint (int pid
)
555 if (!linux_supports_tracefork (pid
))
556 error (_("Your system does not support vfork catchpoints."));
560 child_insert_exec_catchpoint (int pid
)
562 if (!linux_supports_tracefork (pid
))
563 error (_("Your system does not support exec catchpoints."));
570 int pid
= PIDGET (inferior_ptid
);
571 struct target_waitstatus last
;
578 /* If we're stopped while forking and we haven't followed yet, kill the
579 other task. We need to do this first because the parent will be
580 sleeping if this is a vfork. */
582 get_last_target_status (&last_ptid
, &last
);
584 if (last
.kind
== TARGET_WAITKIND_FORKED
585 || last
.kind
== TARGET_WAITKIND_VFORKED
)
587 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
591 /* Kill the current process. */
592 ptrace (PT_KILL
, pid
, 0, 0);
593 ret
= wait (&status
);
595 /* We might get a SIGCHLD instead of an exit status. This is
596 aggravated by the first kill above - a child has just died. */
598 while (ret
== pid
&& WIFSTOPPED (status
))
600 ptrace (PT_KILL
, pid
, 0, 0);
601 ret
= wait (&status
);
604 target_mourn_inferior ();
607 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
608 are processes sharing the same VM space. A multi-threaded process
609 is basically a group of such processes. However, such a grouping
610 is almost entirely a user-space issue; the kernel doesn't enforce
611 such a grouping at all (this might change in the future). In
612 general, we'll rely on the threads library (i.e. the GNU/Linux
613 Threads library) to provide such a grouping.
615 It is perfectly well possible to write a multi-threaded application
616 without the assistance of a threads library, by using the clone
617 system call directly. This module should be able to give some
618 rudimentary support for debugging such applications if developers
619 specify the CLONE_PTRACE flag in the clone system call, and are
620 using the Linux kernel 2.4 or above.
622 Note that there are some peculiarities in GNU/Linux that affect
625 - In general one should specify the __WCLONE flag to waitpid in
626 order to make it report events for any of the cloned processes
627 (and leave it out for the initial process). However, if a cloned
628 process has exited the exit status is only reported if the
629 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
630 we cannot use it since GDB must work on older systems too.
632 - When a traced, cloned process exits and is waited for by the
633 debugger, the kernel reassigns it to the original parent and
634 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
635 library doesn't notice this, which leads to the "zombie problem":
636 When debugged a multi-threaded process that spawns a lot of
637 threads will run out of processes, even if the threads exit,
638 because the "zombies" stay around. */
640 /* List of known LWPs. */
641 static struct lwp_info
*lwp_list
;
643 /* Number of LWPs in the list. */
646 /* Non-zero if we're running in "threaded" mode. */
650 #define GET_LWP(ptid) ptid_get_lwp (ptid)
651 #define GET_PID(ptid) ptid_get_pid (ptid)
652 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
653 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
655 /* If the last reported event was a SIGTRAP, this variable is set to
656 the process id of the LWP/thread that got it. */
660 /* This module's target-specific operations. */
661 static struct target_ops linux_nat_ops
;
663 /* Since we cannot wait (in linux_nat_wait) for the initial process and
664 any cloned processes with a single call to waitpid, we have to use
665 the WNOHANG flag and call waitpid in a loop. To optimize
666 things a bit we use `sigsuspend' to wake us up when a process has
667 something to report (it will send us a SIGCHLD if it has). To make
668 this work we have to juggle with the signal mask. We save the
669 original signal mask such that we can restore it before creating a
670 new process in order to avoid blocking certain signals in the
671 inferior. We then block SIGCHLD during the waitpid/sigsuspend
674 /* Original signal mask. */
675 static sigset_t normal_mask
;
677 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
678 _initialize_linux_nat. */
679 static sigset_t suspend_mask
;
681 /* Signals to block to make that sigsuspend work. */
682 static sigset_t blocked_mask
;
685 /* Prototypes for local functions. */
686 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
687 static int linux_nat_thread_alive (ptid_t ptid
);
689 /* Convert wait status STATUS to a string. Used for printing debug
693 status_to_str (int status
)
697 if (WIFSTOPPED (status
))
698 snprintf (buf
, sizeof (buf
), "%s (stopped)",
699 strsignal (WSTOPSIG (status
)));
700 else if (WIFSIGNALED (status
))
701 snprintf (buf
, sizeof (buf
), "%s (terminated)",
702 strsignal (WSTOPSIG (status
)));
704 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
709 /* Initialize the list of LWPs. Note that this module, contrary to
710 what GDB's generic threads layer does for its thread list,
711 re-initializes the LWP lists whenever we mourn or detach (which
712 doesn't involve mourning) the inferior. */
717 struct lwp_info
*lp
, *lpnext
;
719 for (lp
= lwp_list
; lp
; lp
= lpnext
)
730 /* Add the LWP specified by PID to the list. If this causes the
731 number of LWPs to become larger than one, go into "threaded" mode.
732 Return a pointer to the structure describing the new LWP. */
734 static struct lwp_info
*
735 add_lwp (ptid_t ptid
)
739 gdb_assert (is_lwp (ptid
));
741 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
743 memset (lp
, 0, sizeof (struct lwp_info
));
745 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
757 /* Remove the LWP specified by PID from the list. */
760 delete_lwp (ptid_t ptid
)
762 struct lwp_info
*lp
, *lpprev
;
766 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
767 if (ptid_equal (lp
->ptid
, ptid
))
773 /* We don't go back to "non-threaded" mode if the number of threads
774 becomes less than two. */
778 lpprev
->next
= lp
->next
;
785 /* Return a pointer to the structure describing the LWP corresponding
786 to PID. If no corresponding LWP could be found, return NULL. */
788 static struct lwp_info
*
789 find_lwp_pid (ptid_t ptid
)
795 lwp
= GET_LWP (ptid
);
797 lwp
= GET_PID (ptid
);
799 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
800 if (lwp
== GET_LWP (lp
->ptid
))
806 /* Call CALLBACK with its second argument set to DATA for every LWP in
807 the list. If CALLBACK returns 1 for a particular LWP, return a
808 pointer to the structure describing that LWP immediately.
809 Otherwise return NULL. */
812 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
814 struct lwp_info
*lp
, *lpnext
;
816 for (lp
= lwp_list
; lp
; lp
= lpnext
)
819 if ((*callback
) (lp
, data
))
826 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
827 a message telling the user that a new LWP has been added to the
831 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
833 struct lwp_info
*lp
, *found_lp
;
835 gdb_assert (is_lwp (ptid
));
837 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
838 to interrupt either the ptrace() or waitpid() calls below. */
839 if (!sigismember (&blocked_mask
, SIGCHLD
))
841 sigaddset (&blocked_mask
, SIGCHLD
);
842 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
846 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid
));
848 found_lp
= lp
= find_lwp_pid (ptid
);
852 /* We assume that we're already attached to any LWP that has an id
853 equal to the overall process id, and to any LWP that is already
854 in our list of LWPs. If we're not seeing exit events from threads
855 and we've had PID wraparound since we last tried to stop all threads,
856 this assumption might be wrong; fortunately, this is very unlikely
858 if (GET_LWP (ptid
) != GET_PID (ptid
) && found_lp
== NULL
)
863 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
864 error (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
865 safe_strerror (errno
));
868 fprintf_unfiltered (gdb_stdlog
,
869 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
870 target_pid_to_str (ptid
));
872 pid
= my_waitpid (GET_LWP (ptid
), &status
, 0);
873 if (pid
== -1 && errno
== ECHILD
)
875 /* Try again with __WCLONE to check cloned processes. */
876 pid
= my_waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
880 gdb_assert (pid
== GET_LWP (ptid
)
881 && WIFSTOPPED (status
) && WSTOPSIG (status
));
883 child_post_attach (pid
);
889 fprintf_unfiltered (gdb_stdlog
,
890 "LLAL: waitpid %s received %s\n",
891 target_pid_to_str (ptid
),
892 status_to_str (status
));
897 /* We assume that the LWP representing the original process is
898 already stopped. Mark it as stopped in the data structure
899 that the linux ptrace layer uses to keep track of threads.
900 Note that this won't have already been done since the main
901 thread will have, we assume, been stopped by an attach from a
908 linux_nat_attach (char *args
, int from_tty
)
914 /* FIXME: We should probably accept a list of process id's, and
915 attach all of them. */
916 deprecated_child_ops
.to_attach (args
, from_tty
);
918 /* Add the initial process as the first LWP to the list. */
919 lp
= add_lwp (BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
)));
921 /* Make sure the initial process is stopped. The user-level threads
922 layer might want to poke around in the inferior, and that won't
923 work if things haven't stabilized yet. */
924 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, 0);
925 if (pid
== -1 && errno
== ECHILD
)
927 warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid
));
929 /* Try again with __WCLONE to check cloned processes. */
930 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
934 gdb_assert (pid
== GET_PID (inferior_ptid
)
935 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
939 /* Fake the SIGSTOP that core GDB expects. */
940 lp
->status
= W_STOPCODE (SIGSTOP
);
944 fprintf_unfiltered (gdb_stdlog
,
945 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid
);
950 detach_callback (struct lwp_info
*lp
, void *data
)
952 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
954 if (debug_linux_nat
&& lp
->status
)
955 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
956 strsignal (WSTOPSIG (lp
->status
)),
957 target_pid_to_str (lp
->ptid
));
959 while (lp
->signalled
&& lp
->stopped
)
962 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
963 WSTOPSIG (lp
->status
)) < 0)
964 error (_("Can't continue %s: %s"), target_pid_to_str (lp
->ptid
),
965 safe_strerror (errno
));
968 fprintf_unfiltered (gdb_stdlog
,
969 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
970 target_pid_to_str (lp
->ptid
),
971 status_to_str (lp
->status
));
976 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
977 here. But since lp->signalled was cleared above,
978 stop_wait_callback didn't do anything; the process was left
979 running. Shouldn't we be waiting for it to stop?
980 I've removed the call, since stop_wait_callback now does do
981 something when called with lp->signalled == 0. */
983 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
986 /* We don't actually detach from the LWP that has an id equal to the
987 overall process id just yet. */
988 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
991 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
992 WSTOPSIG (lp
->status
)) < 0)
993 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
994 safe_strerror (errno
));
997 fprintf_unfiltered (gdb_stdlog
,
998 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
999 target_pid_to_str (lp
->ptid
),
1000 strsignal (WSTOPSIG (lp
->status
)));
1002 delete_lwp (lp
->ptid
);
1009 linux_nat_detach (char *args
, int from_tty
)
1011 iterate_over_lwps (detach_callback
, NULL
);
1013 /* Only the initial process should be left right now. */
1014 gdb_assert (num_lwps
== 1);
1016 trap_ptid
= null_ptid
;
1018 /* Destroy LWP info; it's no longer valid. */
1021 /* Restore the original signal mask. */
1022 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1023 sigemptyset (&blocked_mask
);
1025 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
1026 deprecated_child_ops
.to_detach (args
, from_tty
);
1032 resume_callback (struct lwp_info
*lp
, void *data
)
1034 if (lp
->stopped
&& lp
->status
== 0)
1036 struct thread_info
*tp
;
1038 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), 0, TARGET_SIGNAL_0
);
1039 if (debug_linux_nat
)
1040 fprintf_unfiltered (gdb_stdlog
,
1041 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1042 target_pid_to_str (lp
->ptid
));
1051 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1058 resume_set_callback (struct lwp_info
*lp
, void *data
)
1065 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1067 struct lwp_info
*lp
;
1070 /* A specific PTID means `step only this process id'. */
1071 resume_all
= (PIDGET (ptid
) == -1);
1074 iterate_over_lwps (resume_set_callback
, NULL
);
1076 iterate_over_lwps (resume_clear_callback
, NULL
);
1078 /* If PID is -1, it's the current inferior that should be
1079 handled specially. */
1080 if (PIDGET (ptid
) == -1)
1081 ptid
= inferior_ptid
;
1083 lp
= find_lwp_pid (ptid
);
1086 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1088 /* Remember if we're stepping. */
1091 /* Mark this LWP as resumed. */
1094 /* If we have a pending wait status for this thread, there is no
1095 point in resuming the process. */
1098 /* FIXME: What should we do if we are supposed to continue
1099 this thread with a signal? */
1100 gdb_assert (signo
== TARGET_SIGNAL_0
);
1104 /* Mark LWP as not stopped to prevent it from being continued by
1110 iterate_over_lwps (resume_callback
, NULL
);
1112 child_resume (ptid
, step
, signo
);
1113 if (debug_linux_nat
)
1114 fprintf_unfiltered (gdb_stdlog
,
1115 "LLR: %s %s, %s (resume event thread)\n",
1116 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1117 target_pid_to_str (ptid
),
1118 signo
? strsignal (signo
) : "0");
1121 /* Issue kill to specified lwp. */
1123 static int tkill_failed
;
1126 kill_lwp (int lwpid
, int signo
)
1130 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1131 fails, then we are not using nptl threads and we should be using kill. */
1133 #ifdef HAVE_TKILL_SYSCALL
1136 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1137 if (errno
!= ENOSYS
)
1144 return kill (lwpid
, signo
);
1147 /* Handle a GNU/Linux extended wait response. Most of the work we
1148 just pass off to linux_handle_extended_wait, but if it reports a
1149 clone event we need to add the new LWP to our list (and not report
1150 the trap to higher layers). This function returns non-zero if
1151 the event should be ignored and we should wait again. */
1154 linux_nat_handle_extended (struct lwp_info
*lp
, int status
)
1156 linux_handle_extended_wait (GET_LWP (lp
->ptid
), status
,
1159 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1160 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_SPURIOUS
)
1162 struct lwp_info
*new_lp
;
1163 new_lp
= add_lwp (BUILD_LWP (lp
->waitstatus
.value
.related_pid
,
1164 GET_PID (inferior_ptid
)));
1166 new_lp
->stopped
= 1;
1168 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1170 if (debug_linux_nat
)
1171 fprintf_unfiltered (gdb_stdlog
,
1172 "LLHE: Got clone event from LWP %ld, resuming\n",
1173 GET_LWP (lp
->ptid
));
1174 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1182 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1186 wait_lwp (struct lwp_info
*lp
)
1190 int thread_dead
= 0;
1192 gdb_assert (!lp
->stopped
);
1193 gdb_assert (lp
->status
== 0);
1195 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1196 if (pid
== -1 && errno
== ECHILD
)
1198 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1199 if (pid
== -1 && errno
== ECHILD
)
1201 /* The thread has previously exited. We need to delete it
1202 now because, for some vendor 2.4 kernels with NPTL
1203 support backported, there won't be an exit event unless
1204 it is the main thread. 2.6 kernels will report an exit
1205 event for each thread that exits, as expected. */
1207 if (debug_linux_nat
)
1208 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1209 target_pid_to_str (lp
->ptid
));
1215 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1217 if (debug_linux_nat
)
1219 fprintf_unfiltered (gdb_stdlog
,
1220 "WL: waitpid %s received %s\n",
1221 target_pid_to_str (lp
->ptid
),
1222 status_to_str (status
));
1226 /* Check if the thread has exited. */
1227 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1230 if (debug_linux_nat
)
1231 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1232 target_pid_to_str (lp
->ptid
));
1237 if (in_thread_list (lp
->ptid
))
1239 /* Core GDB cannot deal with us deleting the current thread. */
1240 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1241 delete_thread (lp
->ptid
);
1242 printf_unfiltered (_("[%s exited]\n"),
1243 target_pid_to_str (lp
->ptid
));
1246 delete_lwp (lp
->ptid
);
1250 gdb_assert (WIFSTOPPED (status
));
1252 /* Handle GNU/Linux's extended waitstatus for trace events. */
1253 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1255 if (debug_linux_nat
)
1256 fprintf_unfiltered (gdb_stdlog
,
1257 "WL: Handling extended status 0x%06x\n",
1259 if (linux_nat_handle_extended (lp
, status
))
1260 return wait_lwp (lp
);
1266 /* Send a SIGSTOP to LP. */
1269 stop_callback (struct lwp_info
*lp
, void *data
)
1271 if (!lp
->stopped
&& !lp
->signalled
)
1275 if (debug_linux_nat
)
1277 fprintf_unfiltered (gdb_stdlog
,
1278 "SC: kill %s **<SIGSTOP>**\n",
1279 target_pid_to_str (lp
->ptid
));
1282 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1283 if (debug_linux_nat
)
1285 fprintf_unfiltered (gdb_stdlog
,
1286 "SC: lwp kill %d %s\n",
1288 errno
? safe_strerror (errno
) : "ERRNO-OK");
1292 gdb_assert (lp
->status
== 0);
1298 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1299 a pointer to a set of signals to be flushed immediately. */
1302 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1304 sigset_t
*flush_mask
= data
;
1310 status
= wait_lwp (lp
);
1314 /* Ignore any signals in FLUSH_MASK. */
1315 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1324 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1325 if (debug_linux_nat
)
1326 fprintf_unfiltered (gdb_stdlog
,
1327 "PTRACE_CONT %s, 0, 0 (%s)\n",
1328 target_pid_to_str (lp
->ptid
),
1329 errno
? safe_strerror (errno
) : "OK");
1331 return stop_wait_callback (lp
, flush_mask
);
1334 if (WSTOPSIG (status
) != SIGSTOP
)
1336 if (WSTOPSIG (status
) == SIGTRAP
)
1338 /* If a LWP other than the LWP that we're reporting an
1339 event for has hit a GDB breakpoint (as opposed to
1340 some random trap signal), then just arrange for it to
1341 hit it again later. We don't keep the SIGTRAP status
1342 and don't forward the SIGTRAP signal to the LWP. We
1343 will handle the current event, eventually we will
1344 resume all LWPs, and this one will get its breakpoint
1347 If we do not do this, then we run the risk that the
1348 user will delete or disable the breakpoint, but the
1349 thread will have already tripped on it. */
1351 /* Now resume this LWP and get the SIGSTOP event. */
1353 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1354 if (debug_linux_nat
)
1356 fprintf_unfiltered (gdb_stdlog
,
1357 "PTRACE_CONT %s, 0, 0 (%s)\n",
1358 target_pid_to_str (lp
->ptid
),
1359 errno
? safe_strerror (errno
) : "OK");
1361 fprintf_unfiltered (gdb_stdlog
,
1362 "SWC: Candidate SIGTRAP event in %s\n",
1363 target_pid_to_str (lp
->ptid
));
1365 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1366 stop_wait_callback (lp
, data
);
1367 /* If there's another event, throw it back into the queue. */
1370 if (debug_linux_nat
)
1372 fprintf_unfiltered (gdb_stdlog
,
1373 "SWC: kill %s, %s\n",
1374 target_pid_to_str (lp
->ptid
),
1375 status_to_str ((int) status
));
1377 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1379 /* Save the sigtrap event. */
1380 lp
->status
= status
;
1385 /* The thread was stopped with a signal other than
1386 SIGSTOP, and didn't accidentally trip a breakpoint. */
1388 if (debug_linux_nat
)
1390 fprintf_unfiltered (gdb_stdlog
,
1391 "SWC: Pending event %s in %s\n",
1392 status_to_str ((int) status
),
1393 target_pid_to_str (lp
->ptid
));
1395 /* Now resume this LWP and get the SIGSTOP event. */
1397 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1398 if (debug_linux_nat
)
1399 fprintf_unfiltered (gdb_stdlog
,
1400 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1401 target_pid_to_str (lp
->ptid
),
1402 errno
? safe_strerror (errno
) : "OK");
1404 /* Hold this event/waitstatus while we check to see if
1405 there are any more (we still want to get that SIGSTOP). */
1406 stop_wait_callback (lp
, data
);
1407 /* If the lp->status field is still empty, use it to hold
1408 this event. If not, then this event must be returned
1409 to the event queue of the LWP. */
1410 if (lp
->status
== 0)
1411 lp
->status
= status
;
1414 if (debug_linux_nat
)
1416 fprintf_unfiltered (gdb_stdlog
,
1417 "SWC: kill %s, %s\n",
1418 target_pid_to_str (lp
->ptid
),
1419 status_to_str ((int) status
));
1421 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1428 /* We caught the SIGSTOP that we intended to catch, so
1429 there's no SIGSTOP pending. */
1438 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1439 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1442 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
1444 sigset_t blocked
, ignored
;
1447 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
1452 for (i
= 1; i
< NSIG
; i
++)
1453 if (sigismember (pending
, i
))
1454 if (!sigismember (flush_mask
, i
)
1455 || sigismember (&blocked
, i
)
1456 || sigismember (&ignored
, i
))
1457 sigdelset (pending
, i
);
1459 if (sigisemptyset (pending
))
1465 /* DATA is interpreted as a mask of signals to flush. If LP has
1466 signals pending, and they are all in the flush mask, then arrange
1467 to flush them. LP should be stopped, as should all other threads
1468 it might share a signal queue with. */
1471 flush_callback (struct lwp_info
*lp
, void *data
)
1473 sigset_t
*flush_mask
= data
;
1474 sigset_t pending
, intersection
, blocked
, ignored
;
1477 /* Normally, when an LWP exits, it is removed from the LWP list. The
1478 last LWP isn't removed till later, however. So if there is only
1479 one LWP on the list, make sure it's alive. */
1480 if (lwp_list
== lp
&& lp
->next
== NULL
)
1481 if (!linux_nat_thread_alive (lp
->ptid
))
1484 /* Just because the LWP is stopped doesn't mean that new signals
1485 can't arrive from outside, so this function must be careful of
1486 race conditions. However, because all threads are stopped, we
1487 can assume that the pending mask will not shrink unless we resume
1488 the LWP, and that it will then get another signal. We can't
1489 control which one, however. */
1493 if (debug_linux_nat
)
1494 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp
->status
);
1495 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
1499 while (linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
1504 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1505 if (debug_linux_nat
)
1506 fprintf_unfiltered (gdb_stderr
,
1507 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
1510 stop_wait_callback (lp
, flush_mask
);
1511 if (debug_linux_nat
)
1512 fprintf_unfiltered (gdb_stderr
,
1513 "FC: Wait finished; saved status is %d\n",
1520 /* Return non-zero if LP has a wait status pending. */
1523 status_callback (struct lwp_info
*lp
, void *data
)
1525 /* Only report a pending wait status if we pretend that this has
1526 indeed been resumed. */
1527 return (lp
->status
!= 0 && lp
->resumed
);
1530 /* Return non-zero if LP isn't stopped. */
1533 running_callback (struct lwp_info
*lp
, void *data
)
1535 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
1538 /* Count the LWP's that have had events. */
1541 count_events_callback (struct lwp_info
*lp
, void *data
)
1545 gdb_assert (count
!= NULL
);
1547 /* Count only LWPs that have a SIGTRAP event pending. */
1549 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1555 /* Select the LWP (if any) that is currently being single-stepped. */
1558 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
1560 if (lp
->step
&& lp
->status
!= 0)
1566 /* Select the Nth LWP that has had a SIGTRAP event. */
1569 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
1571 int *selector
= data
;
1573 gdb_assert (selector
!= NULL
);
1575 /* Select only LWPs that have a SIGTRAP event pending. */
1577 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1578 if ((*selector
)-- == 0)
1585 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
1587 struct lwp_info
*event_lp
= data
;
1589 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1593 /* If a LWP other than the LWP that we're reporting an event for has
1594 hit a GDB breakpoint (as opposed to some random trap signal),
1595 then just arrange for it to hit it again later. We don't keep
1596 the SIGTRAP status and don't forward the SIGTRAP signal to the
1597 LWP. We will handle the current event, eventually we will resume
1598 all LWPs, and this one will get its breakpoint trap again.
1600 If we do not do this, then we run the risk that the user will
1601 delete or disable the breakpoint, but the LWP will have already
1605 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
1606 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
1607 DECR_PC_AFTER_BREAK
))
1609 if (debug_linux_nat
)
1610 fprintf_unfiltered (gdb_stdlog
,
1611 "CBC: Push back breakpoint for %s\n",
1612 target_pid_to_str (lp
->ptid
));
1614 /* Back up the PC if necessary. */
1615 if (DECR_PC_AFTER_BREAK
)
1616 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
1618 /* Throw away the SIGTRAP. */
1625 /* Select one LWP out of those that have events pending. */
1628 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
1631 int random_selector
;
1632 struct lwp_info
*event_lp
;
1634 /* Record the wait status for the origional LWP. */
1635 (*orig_lp
)->status
= *status
;
1637 /* Give preference to any LWP that is being single-stepped. */
1638 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
1639 if (event_lp
!= NULL
)
1641 if (debug_linux_nat
)
1642 fprintf_unfiltered (gdb_stdlog
,
1643 "SEL: Select single-step %s\n",
1644 target_pid_to_str (event_lp
->ptid
));
1648 /* No single-stepping LWP. Select one at random, out of those
1649 which have had SIGTRAP events. */
1651 /* First see how many SIGTRAP events we have. */
1652 iterate_over_lwps (count_events_callback
, &num_events
);
1654 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1655 random_selector
= (int)
1656 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1658 if (debug_linux_nat
&& num_events
> 1)
1659 fprintf_unfiltered (gdb_stdlog
,
1660 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1661 num_events
, random_selector
);
1663 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
1667 if (event_lp
!= NULL
)
1669 /* Switch the event LWP. */
1670 *orig_lp
= event_lp
;
1671 *status
= event_lp
->status
;
1674 /* Flush the wait status for the event LWP. */
1675 (*orig_lp
)->status
= 0;
1678 /* Return non-zero if LP has been resumed. */
1681 resumed_callback (struct lwp_info
*lp
, void *data
)
1688 /* We need to override child_wait to support attaching to cloned
1689 processes, since a normal wait (as done by the default version)
1690 ignores those processes. */
1692 /* Wait for child PTID to do something. Return id of the child,
1693 minus_one_ptid in case of error; store status into *OURSTATUS. */
1696 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1702 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1706 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1707 attached process. */
1710 pid
= my_waitpid (GET_PID (ptid
), &status
, 0);
1711 if (pid
== -1 && errno
== ECHILD
)
1712 /* Try again with __WCLONE to check cloned processes. */
1713 pid
= my_waitpid (GET_PID (ptid
), &status
, __WCLONE
);
1715 if (debug_linux_nat
)
1717 fprintf_unfiltered (gdb_stdlog
,
1718 "CW: waitpid %ld received %s\n",
1719 (long) pid
, status_to_str (status
));
1724 /* Make sure we don't report an event for the exit of the
1725 original program, if we've detached from it. */
1726 if (pid
!= -1 && !WIFSTOPPED (status
) && pid
!= GET_PID (inferior_ptid
))
1732 /* Check for stop events reported by a process we didn't already
1733 know about - in this case, anything other than inferior_ptid.
1735 If we're expecting to receive stopped processes after fork,
1736 vfork, and clone events, then we'll just add the new one to
1737 our list and go back to waiting for the event to be reported
1738 - the stopped process might be returned from waitpid before
1739 or after the event is. If we want to handle debugging of
1740 CLONE_PTRACE processes we need to do more here, i.e. switch
1741 to multi-threaded mode. */
1742 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
1743 && pid
!= GET_PID (inferior_ptid
))
1745 linux_record_stopped_pid (pid
);
1750 /* Handle GNU/Linux's extended waitstatus for trace events. */
1751 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
1752 && status
>> 16 != 0)
1754 linux_handle_extended_wait (pid
, status
, ourstatus
);
1756 /* If we see a clone event, detach the child, and don't
1757 report the event. It would be nice to offer some way to
1758 switch into a non-thread-db based threaded mode at this
1760 if (ourstatus
->kind
== TARGET_WAITKIND_SPURIOUS
)
1762 ptrace (PTRACE_DETACH
, ourstatus
->value
.related_pid
, 0, 0);
1763 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1764 ptrace (PTRACE_CONT
, pid
, 0, 0);
1770 clear_sigio_trap ();
1771 clear_sigint_trap ();
1773 while (pid
== -1 && save_errno
== EINTR
);
1777 warning (_("Child process unexpectedly missing: %s"),
1778 safe_strerror (errno
));
1780 /* Claim it exited with unknown signal. */
1781 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1782 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1783 return minus_one_ptid
;
1786 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1787 store_waitstatus (ourstatus
, status
);
1789 return pid_to_ptid (pid
);
1794 /* Stop an active thread, verify it still exists, then resume it. */
1797 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
1799 struct lwp_info
*ptr
;
1801 if (!lp
->stopped
&& !lp
->signalled
)
1803 stop_callback (lp
, NULL
);
1804 stop_wait_callback (lp
, NULL
);
1805 /* Resume if the lwp still exists. */
1806 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
1809 resume_callback (lp
, NULL
);
1810 resume_set_callback (lp
, NULL
);
1817 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1819 struct lwp_info
*lp
= NULL
;
1822 pid_t pid
= PIDGET (ptid
);
1823 sigset_t flush_mask
;
1825 sigemptyset (&flush_mask
);
1827 /* Make sure SIGCHLD is blocked. */
1828 if (!sigismember (&blocked_mask
, SIGCHLD
))
1830 sigaddset (&blocked_mask
, SIGCHLD
);
1831 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1836 /* Make sure there is at least one LWP that has been resumed, at
1837 least if there are any LWPs at all. */
1838 gdb_assert (num_lwps
== 0 || iterate_over_lwps (resumed_callback
, NULL
));
1840 /* First check if there is a LWP with a wait status pending. */
1843 /* Any LWP that's been resumed will do. */
1844 lp
= iterate_over_lwps (status_callback
, NULL
);
1847 status
= lp
->status
;
1850 if (debug_linux_nat
&& status
)
1851 fprintf_unfiltered (gdb_stdlog
,
1852 "LLW: Using pending wait status %s for %s.\n",
1853 status_to_str (status
),
1854 target_pid_to_str (lp
->ptid
));
1857 /* But if we don't fine one, we'll have to wait, and check both
1858 cloned and uncloned processes. We start with the cloned
1860 options
= __WCLONE
| WNOHANG
;
1862 else if (is_lwp (ptid
))
1864 if (debug_linux_nat
)
1865 fprintf_unfiltered (gdb_stdlog
,
1866 "LLW: Waiting for specific LWP %s.\n",
1867 target_pid_to_str (ptid
));
1869 /* We have a specific LWP to check. */
1870 lp
= find_lwp_pid (ptid
);
1872 status
= lp
->status
;
1875 if (debug_linux_nat
&& status
)
1876 fprintf_unfiltered (gdb_stdlog
,
1877 "LLW: Using pending wait status %s for %s.\n",
1878 status_to_str (status
),
1879 target_pid_to_str (lp
->ptid
));
1881 /* If we have to wait, take into account whether PID is a cloned
1882 process or not. And we have to convert it to something that
1883 the layer beneath us can understand. */
1884 options
= lp
->cloned
? __WCLONE
: 0;
1885 pid
= GET_LWP (ptid
);
1888 if (status
&& lp
->signalled
)
1890 /* A pending SIGSTOP may interfere with the normal stream of
1891 events. In a typical case where interference is a problem,
1892 we have a SIGSTOP signal pending for LWP A while
1893 single-stepping it, encounter an event in LWP B, and take the
1894 pending SIGSTOP while trying to stop LWP A. After processing
1895 the event in LWP B, LWP A is continued, and we'll never see
1896 the SIGTRAP associated with the last time we were
1897 single-stepping LWP A. */
1899 /* Resume the thread. It should halt immediately returning the
1901 registers_changed ();
1902 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
1904 if (debug_linux_nat
)
1905 fprintf_unfiltered (gdb_stdlog
,
1906 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1907 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1908 target_pid_to_str (lp
->ptid
));
1910 gdb_assert (lp
->resumed
);
1912 /* This should catch the pending SIGSTOP. */
1913 stop_wait_callback (lp
, NULL
);
1916 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1917 attached process. */
1924 lwpid
= my_waitpid (pid
, &status
, options
);
1927 gdb_assert (pid
== -1 || lwpid
== pid
);
1929 if (debug_linux_nat
)
1931 fprintf_unfiltered (gdb_stdlog
,
1932 "LLW: waitpid %ld received %s\n",
1933 (long) lwpid
, status_to_str (status
));
1936 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
1938 /* Check for stop events reported by a process we didn't
1939 already know about - anything not already in our LWP
1942 If we're expecting to receive stopped processes after
1943 fork, vfork, and clone events, then we'll just add the
1944 new one to our list and go back to waiting for the event
1945 to be reported - the stopped process might be returned
1946 from waitpid before or after the event is. */
1947 if (WIFSTOPPED (status
) && !lp
)
1949 linux_record_stopped_pid (lwpid
);
1954 /* Make sure we don't report an event for the exit of an LWP not in
1955 our list, i.e. not part of the current process. This can happen
1956 if we detach from a program we original forked and then it
1958 if (!WIFSTOPPED (status
) && !lp
)
1964 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1965 CLONE_PTRACE processes which do not use the thread library -
1966 otherwise we wouldn't find the new LWP this way. That doesn't
1967 currently work, and the following code is currently unreachable
1968 due to the two blocks above. If it's fixed some day, this code
1969 should be broken out into a function so that we can also pick up
1970 LWPs from the new interface. */
1973 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
1974 if (options
& __WCLONE
)
1979 gdb_assert (WIFSTOPPED (status
)
1980 && WSTOPSIG (status
) == SIGSTOP
);
1983 if (!in_thread_list (inferior_ptid
))
1985 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
1986 GET_PID (inferior_ptid
));
1987 add_thread (inferior_ptid
);
1990 add_thread (lp
->ptid
);
1991 printf_unfiltered (_("[New %s]\n"),
1992 target_pid_to_str (lp
->ptid
));
1996 /* Handle GNU/Linux's extended waitstatus for trace events. */
1997 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1999 if (debug_linux_nat
)
2000 fprintf_unfiltered (gdb_stdlog
,
2001 "LLW: Handling extended status 0x%06x\n",
2003 if (linux_nat_handle_extended (lp
, status
))
2010 /* Check if the thread has exited. */
2011 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
2013 if (in_thread_list (lp
->ptid
))
2015 /* Core GDB cannot deal with us deleting the current
2017 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2018 delete_thread (lp
->ptid
);
2019 printf_unfiltered (_("[%s exited]\n"),
2020 target_pid_to_str (lp
->ptid
));
2023 /* If this is the main thread, we must stop all threads and
2024 verify if they are still alive. This is because in the nptl
2025 thread model, there is no signal issued for exiting LWPs
2026 other than the main thread. We only get the main thread
2027 exit signal once all child threads have already exited.
2028 If we stop all the threads and use the stop_wait_callback
2029 to check if they have exited we can determine whether this
2030 signal should be ignored or whether it means the end of the
2031 debugged application, regardless of which threading model
2033 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2036 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2039 if (debug_linux_nat
)
2040 fprintf_unfiltered (gdb_stdlog
,
2041 "LLW: %s exited.\n",
2042 target_pid_to_str (lp
->ptid
));
2044 delete_lwp (lp
->ptid
);
2046 /* If there is at least one more LWP, then the exit signal
2047 was not the end of the debugged application and should be
2051 /* Make sure there is at least one thread running. */
2052 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2054 /* Discard the event. */
2060 /* Check if the current LWP has previously exited. In the nptl
2061 thread model, LWPs other than the main thread do not issue
2062 signals when they exit so we must check whenever the thread
2063 has stopped. A similar check is made in stop_wait_callback(). */
2064 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2066 if (in_thread_list (lp
->ptid
))
2068 /* Core GDB cannot deal with us deleting the current
2070 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2071 delete_thread (lp
->ptid
);
2072 printf_unfiltered (_("[%s exited]\n"),
2073 target_pid_to_str (lp
->ptid
));
2075 if (debug_linux_nat
)
2076 fprintf_unfiltered (gdb_stdlog
,
2077 "LLW: %s exited.\n",
2078 target_pid_to_str (lp
->ptid
));
2080 delete_lwp (lp
->ptid
);
2082 /* Make sure there is at least one thread running. */
2083 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2085 /* Discard the event. */
2090 /* Make sure we don't report a SIGSTOP that we sent
2091 ourselves in an attempt to stop an LWP. */
2093 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2095 if (debug_linux_nat
)
2096 fprintf_unfiltered (gdb_stdlog
,
2097 "LLW: Delayed SIGSTOP caught for %s.\n",
2098 target_pid_to_str (lp
->ptid
));
2100 /* This is a delayed SIGSTOP. */
2103 registers_changed ();
2104 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
2106 if (debug_linux_nat
)
2107 fprintf_unfiltered (gdb_stdlog
,
2108 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2110 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2111 target_pid_to_str (lp
->ptid
));
2114 gdb_assert (lp
->resumed
);
2116 /* Discard the event. */
2126 /* Alternate between checking cloned and uncloned processes. */
2127 options
^= __WCLONE
;
2129 /* And suspend every time we have checked both. */
2130 if (options
& __WCLONE
)
2131 sigsuspend (&suspend_mask
);
2134 /* We shouldn't end up here unless we want to try again. */
2135 gdb_assert (status
== 0);
2138 clear_sigio_trap ();
2139 clear_sigint_trap ();
2143 /* Don't report signals that GDB isn't interested in, such as
2144 signals that are neither printed nor stopped upon. Stopping all
2145 threads can be a bit time-consuming so if we want decent
2146 performance with heavily multi-threaded programs, especially when
2147 they're using a high frequency timer, we'd better avoid it if we
2150 if (WIFSTOPPED (status
))
2152 int signo
= target_signal_from_host (WSTOPSIG (status
));
2154 if (signal_stop_state (signo
) == 0
2155 && signal_print_state (signo
) == 0
2156 && signal_pass_state (signo
) == 1)
2158 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2159 here? It is not clear we should. GDB may not expect
2160 other threads to run. On the other hand, not resuming
2161 newly attached threads may cause an unwanted delay in
2162 getting them running. */
2163 registers_changed ();
2164 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
, signo
);
2165 if (debug_linux_nat
)
2166 fprintf_unfiltered (gdb_stdlog
,
2167 "LLW: %s %s, %s (preempt 'handle')\n",
2169 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2170 target_pid_to_str (lp
->ptid
),
2171 signo
? strsignal (signo
) : "0");
2177 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2179 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2180 forwarded to the entire process group, that is, all LWP's
2181 will receive it. Since we only want to report it once,
2182 we try to flush it from all LWPs except this one. */
2183 sigaddset (&flush_mask
, SIGINT
);
2187 /* This LWP is stopped now. */
2190 if (debug_linux_nat
)
2191 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2192 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2194 /* Now stop all other LWP's ... */
2195 iterate_over_lwps (stop_callback
, NULL
);
2197 /* ... and wait until all of them have reported back that they're no
2199 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2200 iterate_over_lwps (flush_callback
, &flush_mask
);
2202 /* If we're not waiting for a specific LWP, choose an event LWP from
2203 among those that have had events. Giving equal priority to all
2204 LWPs that have had events helps prevent starvation. */
2206 select_event_lwp (&lp
, &status
);
2208 /* Now that we've selected our final event LWP, cancel any
2209 breakpoints in other LWPs that have hit a GDB breakpoint. See
2210 the comment in cancel_breakpoints_callback to find out why. */
2211 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2213 /* If we're not running in "threaded" mode, we'll report the bare
2216 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2218 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2219 if (debug_linux_nat
)
2220 fprintf_unfiltered (gdb_stdlog
,
2221 "LLW: trap_ptid is %s.\n",
2222 target_pid_to_str (trap_ptid
));
2225 trap_ptid
= null_ptid
;
2227 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2229 *ourstatus
= lp
->waitstatus
;
2230 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2233 store_waitstatus (ourstatus
, status
);
2235 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2239 kill_callback (struct lwp_info
*lp
, void *data
)
2242 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2243 if (debug_linux_nat
)
2244 fprintf_unfiltered (gdb_stdlog
,
2245 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2246 target_pid_to_str (lp
->ptid
),
2247 errno
? safe_strerror (errno
) : "OK");
2253 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2257 /* We must make sure that there are no pending events (delayed
2258 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2259 program doesn't interfere with any following debugging session. */
2261 /* For cloned processes we must check both with __WCLONE and
2262 without, since the exit status of a cloned process isn't reported
2268 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2269 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2271 fprintf_unfiltered (gdb_stdlog
,
2272 "KWC: wait %s received unknown.\n",
2273 target_pid_to_str (lp
->ptid
));
2276 while (pid
== GET_LWP (lp
->ptid
));
2278 gdb_assert (pid
== -1 && errno
== ECHILD
);
2283 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2284 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2286 fprintf_unfiltered (gdb_stdlog
,
2287 "KWC: wait %s received unk.\n",
2288 target_pid_to_str (lp
->ptid
));
2291 while (pid
== GET_LWP (lp
->ptid
));
2293 gdb_assert (pid
== -1 && errno
== ECHILD
);
2298 linux_nat_kill (void)
2300 /* Kill all LWP's ... */
2301 iterate_over_lwps (kill_callback
, NULL
);
2303 /* ... and wait until we've flushed all events. */
2304 iterate_over_lwps (kill_wait_callback
, NULL
);
2306 target_mourn_inferior ();
2310 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
2313 deprecated_child_ops
.to_create_inferior (exec_file
, allargs
, env
, from_tty
);
2317 linux_nat_mourn_inferior (void)
2319 trap_ptid
= null_ptid
;
2321 /* Destroy LWP info; it's no longer valid. */
2324 /* Restore the original signal mask. */
2325 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
2326 sigemptyset (&blocked_mask
);
2328 deprecated_child_ops
.to_mourn_inferior ();
2332 linux_nat_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
,
2333 int write
, struct mem_attrib
*attrib
,
2334 struct target_ops
*target
)
2336 struct cleanup
*old_chain
= save_inferior_ptid ();
2339 if (is_lwp (inferior_ptid
))
2340 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2342 xfer
= linux_proc_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2344 xfer
= child_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2346 do_cleanups (old_chain
);
2351 linux_nat_thread_alive (ptid_t ptid
)
2353 gdb_assert (is_lwp (ptid
));
2356 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2357 if (debug_linux_nat
)
2358 fprintf_unfiltered (gdb_stdlog
,
2359 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2360 target_pid_to_str (ptid
),
2361 errno
? safe_strerror (errno
) : "OK");
2369 linux_nat_pid_to_str (ptid_t ptid
)
2371 static char buf
[64];
2375 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2379 return normal_pid_to_str (ptid
);
2383 init_linux_nat_ops (void)
2386 linux_nat_ops
.to_open
= linux_nat_open
;
2388 linux_nat_ops
.to_shortname
= "lwp-layer";
2389 linux_nat_ops
.to_longname
= "lwp-layer";
2390 linux_nat_ops
.to_doc
= "Low level threads support (LWP layer)";
2391 linux_nat_ops
.to_attach
= linux_nat_attach
;
2392 linux_nat_ops
.to_detach
= linux_nat_detach
;
2393 linux_nat_ops
.to_resume
= linux_nat_resume
;
2394 linux_nat_ops
.to_wait
= linux_nat_wait
;
2395 /* fetch_inferior_registers and store_inferior_registers will
2396 honor the LWP id, so we can use them directly. */
2397 linux_nat_ops
.to_fetch_registers
= fetch_inferior_registers
;
2398 linux_nat_ops
.to_store_registers
= store_inferior_registers
;
2399 linux_nat_ops
.deprecated_xfer_memory
= linux_nat_xfer_memory
;
2400 linux_nat_ops
.to_kill
= linux_nat_kill
;
2401 linux_nat_ops
.to_create_inferior
= linux_nat_create_inferior
;
2402 linux_nat_ops
.to_mourn_inferior
= linux_nat_mourn_inferior
;
2403 linux_nat_ops
.to_thread_alive
= linux_nat_thread_alive
;
2404 linux_nat_ops
.to_pid_to_str
= linux_nat_pid_to_str
;
2405 linux_nat_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
2406 linux_nat_ops
.to_post_attach
= child_post_attach
;
2407 linux_nat_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
2408 linux_nat_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
2409 linux_nat_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
2411 linux_nat_ops
.to_stratum
= thread_stratum
;
2412 linux_nat_ops
.to_has_thread_control
= tc_schedlock
;
2413 linux_nat_ops
.to_magic
= OPS_MAGIC
;
2417 sigchld_handler (int signo
)
2419 /* Do nothing. The only reason for this handler is that it allows
2420 us to use sigsuspend in linux_nat_wait above to wait for the
2421 arrival of a SIGCHLD. */
2424 /* Accepts an integer PID; Returns a string representing a file that
2425 can be opened to get the symbols for the child process. */
2428 child_pid_to_exec_file (int pid
)
2430 char *name1
, *name2
;
2432 name1
= xmalloc (MAXPATHLEN
);
2433 name2
= xmalloc (MAXPATHLEN
);
2434 make_cleanup (xfree
, name1
);
2435 make_cleanup (xfree
, name2
);
2436 memset (name2
, 0, MAXPATHLEN
);
2438 sprintf (name1
, "/proc/%d/exe", pid
);
2439 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
2445 /* Service function for corefiles and info proc. */
2448 read_mapping (FILE *mapfile
,
2453 char *device
, long long *inode
, char *filename
)
2455 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
2456 addr
, endaddr
, permissions
, offset
, device
, inode
);
2459 if (ret
> 0 && ret
!= EOF
)
2461 /* Eat everything up to EOL for the filename. This will prevent
2462 weird filenames (such as one with embedded whitespace) from
2463 confusing this code. It also makes this code more robust in
2464 respect to annotations the kernel may add after the filename.
2466 Note the filename is used for informational purposes
2468 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
2471 return (ret
!= 0 && ret
!= EOF
);
2474 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2475 regions in the inferior for a corefile. */
2478 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
2480 int, int, int, void *), void *obfd
)
2482 long long pid
= PIDGET (inferior_ptid
);
2483 char mapsfilename
[MAXPATHLEN
];
2485 long long addr
, endaddr
, size
, offset
, inode
;
2486 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2487 int read
, write
, exec
;
2490 /* Compose the filename for the /proc memory map, and open it. */
2491 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
2492 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
2493 error (_("Could not open %s."), mapsfilename
);
2496 fprintf_filtered (gdb_stdout
,
2497 "Reading memory regions from %s\n", mapsfilename
);
2499 /* Now iterate until end-of-file. */
2500 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
2501 &offset
, &device
[0], &inode
, &filename
[0]))
2503 size
= endaddr
- addr
;
2505 /* Get the segment's permissions. */
2506 read
= (strchr (permissions
, 'r') != 0);
2507 write
= (strchr (permissions
, 'w') != 0);
2508 exec
= (strchr (permissions
, 'x') != 0);
2512 fprintf_filtered (gdb_stdout
,
2513 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2514 size
, paddr_nz (addr
),
2516 write
? 'w' : ' ', exec
? 'x' : ' ');
2517 if (filename
&& filename
[0])
2518 fprintf_filtered (gdb_stdout
, " for %s", filename
);
2519 fprintf_filtered (gdb_stdout
, "\n");
2522 /* Invoke the callback function to create the corefile
2524 func (addr
, size
, read
, write
, exec
, obfd
);
2530 /* Records the thread's register state for the corefile note
2534 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
2535 char *note_data
, int *note_size
)
2537 gdb_gregset_t gregs
;
2538 gdb_fpregset_t fpregs
;
2539 #ifdef FILL_FPXREGSET
2540 gdb_fpxregset_t fpxregs
;
2542 unsigned long lwp
= ptid_get_lwp (ptid
);
2544 fill_gregset (&gregs
, -1);
2545 note_data
= (char *) elfcore_write_prstatus (obfd
,
2549 stop_signal
, &gregs
);
2551 fill_fpregset (&fpregs
, -1);
2552 note_data
= (char *) elfcore_write_prfpreg (obfd
,
2555 &fpregs
, sizeof (fpregs
));
2556 #ifdef FILL_FPXREGSET
2557 fill_fpxregset (&fpxregs
, -1);
2558 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
2561 &fpxregs
, sizeof (fpxregs
));
2566 struct linux_nat_corefile_thread_data
2574 /* Called by gdbthread.c once per thread. Records the thread's
2575 register state for the corefile note section. */
2578 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
2580 struct linux_nat_corefile_thread_data
*args
= data
;
2581 ptid_t saved_ptid
= inferior_ptid
;
2583 inferior_ptid
= ti
->ptid
;
2584 registers_changed ();
2585 target_fetch_registers (-1); /* FIXME should not be necessary;
2586 fill_gregset should do it automatically. */
2587 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
2592 inferior_ptid
= saved_ptid
;
2593 registers_changed ();
2594 target_fetch_registers (-1); /* FIXME should not be necessary;
2595 fill_gregset should do it automatically. */
2599 /* Records the register state for the corefile note section. */
2602 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
2603 char *note_data
, int *note_size
)
2605 registers_changed ();
2606 target_fetch_registers (-1); /* FIXME should not be necessary;
2607 fill_gregset should do it automatically. */
2608 return linux_nat_do_thread_registers (obfd
,
2609 ptid_build (ptid_get_pid (inferior_ptid
),
2610 ptid_get_pid (inferior_ptid
),
2612 note_data
, note_size
);
2616 /* Fills the "to_make_corefile_note" target vector. Builds the note
2617 section for a corefile, and returns it in a malloc buffer. */
2620 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
2622 struct linux_nat_corefile_thread_data thread_args
;
2623 struct cleanup
*old_chain
;
2624 char fname
[16] = { '\0' };
2625 char psargs
[80] = { '\0' };
2626 char *note_data
= NULL
;
2627 ptid_t current_ptid
= inferior_ptid
;
2631 if (get_exec_file (0))
2633 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
2634 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
2635 if (get_inferior_args ())
2637 strncat (psargs
, " ", sizeof (psargs
) - strlen (psargs
));
2638 strncat (psargs
, get_inferior_args (),
2639 sizeof (psargs
) - strlen (psargs
));
2641 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
2643 note_size
, fname
, psargs
);
2646 /* Dump information for threads. */
2647 thread_args
.obfd
= obfd
;
2648 thread_args
.note_data
= note_data
;
2649 thread_args
.note_size
= note_size
;
2650 thread_args
.num_notes
= 0;
2651 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
2652 if (thread_args
.num_notes
== 0)
2654 /* iterate_over_threads didn't come up with any threads; just
2655 use inferior_ptid. */
2656 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
2657 note_data
, note_size
);
2661 note_data
= thread_args
.note_data
;
2664 auxv_len
= target_auxv_read (¤t_target
, &auxv
);
2667 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
2668 "CORE", NT_AUXV
, auxv
, auxv_len
);
2672 make_cleanup (xfree
, note_data
);
2676 /* Implement the "info proc" command. */
2679 linux_nat_info_proc_cmd (char *args
, int from_tty
)
2681 long long pid
= PIDGET (inferior_ptid
);
2684 char buffer
[MAXPATHLEN
];
2685 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
2698 /* Break up 'args' into an argv array. */
2699 if ((argv
= buildargv (args
)) == NULL
)
2702 make_cleanup_freeargv (argv
);
2704 while (argv
!= NULL
&& *argv
!= NULL
)
2706 if (isdigit (argv
[0][0]))
2708 pid
= strtoul (argv
[0], NULL
, 10);
2710 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
2714 else if (strcmp (argv
[0], "status") == 0)
2718 else if (strcmp (argv
[0], "stat") == 0)
2722 else if (strcmp (argv
[0], "cmd") == 0)
2726 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
2730 else if (strcmp (argv
[0], "cwd") == 0)
2734 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
2740 /* [...] (future options here) */
2745 error (_("No current process: you must name one."));
2747 sprintf (fname1
, "/proc/%lld", pid
);
2748 if (stat (fname1
, &dummy
) != 0)
2749 error (_("No /proc directory: '%s'"), fname1
);
2751 printf_filtered (_("process %lld\n"), pid
);
2752 if (cmdline_f
|| all
)
2754 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
2755 if ((procfile
= fopen (fname1
, "r")) > 0)
2757 fgets (buffer
, sizeof (buffer
), procfile
);
2758 printf_filtered ("cmdline = '%s'\n", buffer
);
2762 warning (_("unable to open /proc file '%s'"), fname1
);
2766 sprintf (fname1
, "/proc/%lld/cwd", pid
);
2767 memset (fname2
, 0, sizeof (fname2
));
2768 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2769 printf_filtered ("cwd = '%s'\n", fname2
);
2771 warning (_("unable to read link '%s'"), fname1
);
2775 sprintf (fname1
, "/proc/%lld/exe", pid
);
2776 memset (fname2
, 0, sizeof (fname2
));
2777 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2778 printf_filtered ("exe = '%s'\n", fname2
);
2780 warning (_("unable to read link '%s'"), fname1
);
2782 if (mappings_f
|| all
)
2784 sprintf (fname1
, "/proc/%lld/maps", pid
);
2785 if ((procfile
= fopen (fname1
, "r")) > 0)
2787 long long addr
, endaddr
, size
, offset
, inode
;
2788 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2790 printf_filtered (_("Mapped address spaces:\n\n"));
2791 if (TARGET_ADDR_BIT
== 32)
2793 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2796 " Size", " Offset", "objfile");
2800 printf_filtered (" %18s %18s %10s %10s %7s\n",
2803 " Size", " Offset", "objfile");
2806 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
2807 &offset
, &device
[0], &inode
, &filename
[0]))
2809 size
= endaddr
- addr
;
2811 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2812 calls here (and possibly above) should be abstracted
2813 out into their own functions? Andrew suggests using
2814 a generic local_address_string instead to print out
2815 the addresses; that makes sense to me, too. */
2817 if (TARGET_ADDR_BIT
== 32)
2819 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2820 (unsigned long) addr
, /* FIXME: pr_addr */
2821 (unsigned long) endaddr
,
2823 (unsigned int) offset
,
2824 filename
[0] ? filename
: "");
2828 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2829 (unsigned long) addr
, /* FIXME: pr_addr */
2830 (unsigned long) endaddr
,
2832 (unsigned int) offset
,
2833 filename
[0] ? filename
: "");
2840 warning (_("unable to open /proc file '%s'"), fname1
);
2842 if (status_f
|| all
)
2844 sprintf (fname1
, "/proc/%lld/status", pid
);
2845 if ((procfile
= fopen (fname1
, "r")) > 0)
2847 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
2848 puts_filtered (buffer
);
2852 warning (_("unable to open /proc file '%s'"), fname1
);
2856 sprintf (fname1
, "/proc/%lld/stat", pid
);
2857 if ((procfile
= fopen (fname1
, "r")) > 0)
2862 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2863 printf_filtered (_("Process: %d\n"), itmp
);
2864 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
2865 printf_filtered (_("Exec file: %s\n"), buffer
);
2866 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
2867 printf_filtered (_("State: %c\n"), ctmp
);
2868 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2869 printf_filtered (_("Parent process: %d\n"), itmp
);
2870 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2871 printf_filtered (_("Process group: %d\n"), itmp
);
2872 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2873 printf_filtered (_("Session id: %d\n"), itmp
);
2874 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2875 printf_filtered (_("TTY: %d\n"), itmp
);
2876 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2877 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
2878 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2879 printf_filtered (_("Flags: 0x%x\n"), itmp
);
2880 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2881 printf_filtered (_("Minor faults (no memory page): %u\n"),
2882 (unsigned int) itmp
);
2883 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2884 printf_filtered (_("Minor faults, children: %u\n"),
2885 (unsigned int) itmp
);
2886 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2887 printf_filtered (_("Major faults (memory page faults): %u\n"),
2888 (unsigned int) itmp
);
2889 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2890 printf_filtered (_("Major faults, children: %u\n"),
2891 (unsigned int) itmp
);
2892 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2893 printf_filtered ("utime: %d\n", itmp
);
2894 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2895 printf_filtered ("stime: %d\n", itmp
);
2896 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2897 printf_filtered ("utime, children: %d\n", itmp
);
2898 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2899 printf_filtered ("stime, children: %d\n", itmp
);
2900 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2901 printf_filtered (_("jiffies remaining in current time slice: %d\n"),
2903 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2904 printf_filtered ("'nice' value: %d\n", itmp
);
2905 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2906 printf_filtered (_("jiffies until next timeout: %u\n"),
2907 (unsigned int) itmp
);
2908 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2909 printf_filtered ("jiffies until next SIGALRM: %u\n",
2910 (unsigned int) itmp
);
2911 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2912 printf_filtered (_("start time (jiffies since system boot): %d\n"),
2914 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2915 printf_filtered (_("Virtual memory size: %u\n"),
2916 (unsigned int) itmp
);
2917 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2918 printf_filtered (_("Resident set size: %u\n"), (unsigned int) itmp
);
2919 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2920 printf_filtered ("rlim: %u\n", (unsigned int) itmp
);
2921 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2922 printf_filtered (_("Start of text: 0x%x\n"), itmp
);
2923 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2924 printf_filtered (_("End of text: 0x%x\n"), itmp
);
2925 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2926 printf_filtered (_("Start of stack: 0x%x\n"), itmp
);
2927 #if 0 /* Don't know how architecture-dependent the rest is...
2928 Anyway the signal bitmap info is available from "status". */
2929 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2930 printf_filtered (_("Kernel stack pointer: 0x%x\n"), itmp
);
2931 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2932 printf_filtered (_("Kernel instr pointer: 0x%x\n"), itmp
);
2933 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2934 printf_filtered (_("Pending signals bitmap: 0x%x\n"), itmp
);
2935 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2936 printf_filtered (_("Blocked signals bitmap: 0x%x\n"), itmp
);
2937 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2938 printf_filtered (_("Ignored signals bitmap: 0x%x\n"), itmp
);
2939 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2940 printf_filtered (_("Catched signals bitmap: 0x%x\n"), itmp
);
2941 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2942 printf_filtered (_("wchan (system call): 0x%x\n"), itmp
);
2947 warning (_("unable to open /proc file '%s'"), fname1
);
2952 linux_proc_xfer_memory (CORE_ADDR addr
, gdb_byte
*myaddr
, int len
, int write
,
2953 struct mem_attrib
*attrib
, struct target_ops
*target
)
2961 /* Don't bother for one word. */
2962 if (len
< 3 * sizeof (long))
2965 /* We could keep this file open and cache it - possibly one per
2966 thread. That requires some juggling, but is even faster. */
2967 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
2968 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
2972 /* If pread64 is available, use it. It's faster if the kernel
2973 supports it (only one syscall), and it's 64-bit safe even on
2974 32-bit platforms (for instance, SPARC debugging a SPARC64
2977 if (pread64 (fd
, myaddr
, len
, addr
) != len
)
2979 if (lseek (fd
, addr
, SEEK_SET
) == -1 || read (fd
, myaddr
, len
) != len
)
2989 /* Parse LINE as a signal set and add its set bits to SIGS. */
2992 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
2994 int len
= strlen (line
) - 1;
2998 if (line
[len
] != '\n')
2999 error (_("Could not parse signal set: %s"), line
);
3007 if (*p
>= '0' && *p
<= '9')
3009 else if (*p
>= 'a' && *p
<= 'f')
3010 digit
= *p
- 'a' + 10;
3012 error (_("Could not parse signal set: %s"), line
);
3017 sigaddset (sigs
, signum
+ 1);
3019 sigaddset (sigs
, signum
+ 2);
3021 sigaddset (sigs
, signum
+ 3);
3023 sigaddset (sigs
, signum
+ 4);
3029 /* Find process PID's pending signals from /proc/pid/status and set
3033 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3036 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3039 sigemptyset (pending
);
3040 sigemptyset (blocked
);
3041 sigemptyset (ignored
);
3042 sprintf (fname
, "/proc/%d/status", pid
);
3043 procfile
= fopen (fname
, "r");
3044 if (procfile
== NULL
)
3045 error (_("Could not open %s"), fname
);
3047 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3049 /* Normal queued signals are on the SigPnd line in the status
3050 file. However, 2.6 kernels also have a "shared" pending
3051 queue for delivering signals to a thread group, so check for
3054 Unfortunately some Red Hat kernels include the shared pending
3055 queue but not the ShdPnd status field. */
3057 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3058 add_line_to_sigset (buffer
+ 8, pending
);
3059 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3060 add_line_to_sigset (buffer
+ 8, pending
);
3061 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3062 add_line_to_sigset (buffer
+ 8, blocked
);
3063 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3064 add_line_to_sigset (buffer
+ 8, ignored
);
3071 _initialize_linux_nat (void)
3073 struct sigaction action
;
3074 extern void thread_db_init (struct target_ops
*);
3076 deprecated_child_ops
.to_find_memory_regions
= linux_nat_find_memory_regions
;
3077 deprecated_child_ops
.to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3079 add_info ("proc", linux_nat_info_proc_cmd
, _("\
3080 Show /proc process information about any running process.\n\
3081 Specify any process id, or use the program being debugged by default.\n\
3082 Specify any of the following keywords for detailed info:\n\
3083 mappings -- list of mapped memory regions.\n\
3084 stat -- list a bunch of random process info.\n\
3085 status -- list a different bunch of random process info.\n\
3086 all -- list all available /proc info."));
3088 init_linux_nat_ops ();
3089 add_target (&linux_nat_ops
);
3090 thread_db_init (&linux_nat_ops
);
3092 /* Save the original signal mask. */
3093 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
3095 action
.sa_handler
= sigchld_handler
;
3096 sigemptyset (&action
.sa_mask
);
3097 action
.sa_flags
= SA_RESTART
;
3098 sigaction (SIGCHLD
, &action
, NULL
);
3100 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3101 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
3102 sigdelset (&suspend_mask
, SIGCHLD
);
3104 sigemptyset (&blocked_mask
);
3106 add_setshow_zinteger_cmd ("lin-lwp", no_class
, &debug_linux_nat
, _("\
3107 Set debugging of GNU/Linux lwp module."), _("\
3108 Show debugging of GNU/Linux lwp module."), _("\
3109 Enables printf debugging output."),
3111 show_debug_linux_nat
,
3112 &setdebuglist
, &showdebuglist
);
3116 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3117 the GNU/Linux Threads library and therefore doesn't really belong
3120 /* Read variable NAME in the target and return its value if found.
3121 Otherwise return zero. It is assumed that the type of the variable
3125 get_signo (const char *name
)
3127 struct minimal_symbol
*ms
;
3130 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
3134 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
3135 sizeof (signo
)) != 0)
3141 /* Return the set of signals used by the threads library in *SET. */
3144 lin_thread_get_thread_signals (sigset_t
*set
)
3146 struct sigaction action
;
3147 int restart
, cancel
;
3151 restart
= get_signo ("__pthread_sig_restart");
3155 cancel
= get_signo ("__pthread_sig_cancel");
3159 sigaddset (set
, restart
);
3160 sigaddset (set
, cancel
);
3162 /* The GNU/Linux Threads library makes terminating threads send a
3163 special "cancel" signal instead of SIGCHLD. Make sure we catch
3164 those (to prevent them from terminating GDB itself, which is
3165 likely to be their default action) and treat them the same way as
3168 action
.sa_handler
= sigchld_handler
;
3169 sigemptyset (&action
.sa_mask
);
3170 action
.sa_flags
= SA_RESTART
;
3171 sigaction (cancel
, &action
, NULL
);
3173 /* We block the "cancel" signal throughout this code ... */
3174 sigaddset (&blocked_mask
, cancel
);
3175 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
3177 /* ... except during a sigsuspend. */
3178 sigdelset (&suspend_mask
, cancel
);