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 (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
);
469 push_target (&deprecated_child_ops
);
471 /* Reset breakpoints in the child as appropriate. */
472 follow_inferior_reset_breakpoints ();
479 linux_handle_extended_wait (int pid
, int status
,
480 struct target_waitstatus
*ourstatus
)
482 int event
= status
>> 16;
484 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
485 || event
== PTRACE_EVENT_CLONE
)
487 unsigned long new_pid
;
490 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
492 /* If we haven't already seen the new PID stop, wait for it now. */
493 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
495 /* The new child has a pending SIGSTOP. We can't affect it until it
496 hits the SIGSTOP, but we're already attached. */
497 ret
= my_waitpid (new_pid
, &status
,
498 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
500 perror_with_name (_("waiting for new child"));
501 else if (ret
!= new_pid
)
502 internal_error (__FILE__
, __LINE__
,
503 _("wait returned unexpected PID %d"), ret
);
504 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
505 internal_error (__FILE__
, __LINE__
,
506 _("wait returned unexpected status 0x%x"), status
);
509 if (event
== PTRACE_EVENT_FORK
)
510 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
511 else if (event
== PTRACE_EVENT_VFORK
)
512 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
514 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
516 ourstatus
->value
.related_pid
= new_pid
;
517 return inferior_ptid
;
520 if (event
== PTRACE_EVENT_EXEC
)
522 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
523 ourstatus
->value
.execd_pathname
524 = xstrdup (child_pid_to_exec_file (pid
));
526 if (linux_parent_pid
)
528 detach_breakpoints (linux_parent_pid
);
529 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
531 linux_parent_pid
= 0;
534 return inferior_ptid
;
537 internal_error (__FILE__
, __LINE__
,
538 _("unknown ptrace event %d"), event
);
543 child_insert_fork_catchpoint (int pid
)
545 if (! linux_supports_tracefork (pid
))
546 error (_("Your system does not support fork catchpoints."));
550 child_insert_vfork_catchpoint (int pid
)
552 if (!linux_supports_tracefork (pid
))
553 error (_("Your system does not support vfork catchpoints."));
557 child_insert_exec_catchpoint (int pid
)
559 if (!linux_supports_tracefork (pid
))
560 error (_("Your system does not support exec catchpoints."));
567 int pid
= PIDGET (inferior_ptid
);
568 struct target_waitstatus last
;
575 /* If we're stopped while forking and we haven't followed yet, kill the
576 other task. We need to do this first because the parent will be
577 sleeping if this is a vfork. */
579 get_last_target_status (&last_ptid
, &last
);
581 if (last
.kind
== TARGET_WAITKIND_FORKED
582 || last
.kind
== TARGET_WAITKIND_VFORKED
)
584 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
588 /* Kill the current process. */
589 ptrace (PT_KILL
, pid
, 0, 0);
590 ret
= wait (&status
);
592 /* We might get a SIGCHLD instead of an exit status. This is
593 aggravated by the first kill above - a child has just died. */
595 while (ret
== pid
&& WIFSTOPPED (status
))
597 ptrace (PT_KILL
, pid
, 0, 0);
598 ret
= wait (&status
);
601 target_mourn_inferior ();
604 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
605 are processes sharing the same VM space. A multi-threaded process
606 is basically a group of such processes. However, such a grouping
607 is almost entirely a user-space issue; the kernel doesn't enforce
608 such a grouping at all (this might change in the future). In
609 general, we'll rely on the threads library (i.e. the GNU/Linux
610 Threads library) to provide such a grouping.
612 It is perfectly well possible to write a multi-threaded application
613 without the assistance of a threads library, by using the clone
614 system call directly. This module should be able to give some
615 rudimentary support for debugging such applications if developers
616 specify the CLONE_PTRACE flag in the clone system call, and are
617 using the Linux kernel 2.4 or above.
619 Note that there are some peculiarities in GNU/Linux that affect
622 - In general one should specify the __WCLONE flag to waitpid in
623 order to make it report events for any of the cloned processes
624 (and leave it out for the initial process). However, if a cloned
625 process has exited the exit status is only reported if the
626 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
627 we cannot use it since GDB must work on older systems too.
629 - When a traced, cloned process exits and is waited for by the
630 debugger, the kernel reassigns it to the original parent and
631 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
632 library doesn't notice this, which leads to the "zombie problem":
633 When debugged a multi-threaded process that spawns a lot of
634 threads will run out of processes, even if the threads exit,
635 because the "zombies" stay around. */
637 /* List of known LWPs. */
638 static struct lwp_info
*lwp_list
;
640 /* Number of LWPs in the list. */
643 /* Non-zero if we're running in "threaded" mode. */
647 #define GET_LWP(ptid) ptid_get_lwp (ptid)
648 #define GET_PID(ptid) ptid_get_pid (ptid)
649 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
650 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
652 /* If the last reported event was a SIGTRAP, this variable is set to
653 the process id of the LWP/thread that got it. */
657 /* This module's target-specific operations. */
658 static struct target_ops linux_nat_ops
;
660 /* Since we cannot wait (in linux_nat_wait) for the initial process and
661 any cloned processes with a single call to waitpid, we have to use
662 the WNOHANG flag and call waitpid in a loop. To optimize
663 things a bit we use `sigsuspend' to wake us up when a process has
664 something to report (it will send us a SIGCHLD if it has). To make
665 this work we have to juggle with the signal mask. We save the
666 original signal mask such that we can restore it before creating a
667 new process in order to avoid blocking certain signals in the
668 inferior. We then block SIGCHLD during the waitpid/sigsuspend
671 /* Original signal mask. */
672 static sigset_t normal_mask
;
674 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
675 _initialize_linux_nat. */
676 static sigset_t suspend_mask
;
678 /* Signals to block to make that sigsuspend work. */
679 static sigset_t blocked_mask
;
682 /* Prototypes for local functions. */
683 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
684 static int linux_nat_thread_alive (ptid_t ptid
);
686 /* Convert wait status STATUS to a string. Used for printing debug
690 status_to_str (int status
)
694 if (WIFSTOPPED (status
))
695 snprintf (buf
, sizeof (buf
), "%s (stopped)",
696 strsignal (WSTOPSIG (status
)));
697 else if (WIFSIGNALED (status
))
698 snprintf (buf
, sizeof (buf
), "%s (terminated)",
699 strsignal (WSTOPSIG (status
)));
701 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
706 /* Initialize the list of LWPs. Note that this module, contrary to
707 what GDB's generic threads layer does for its thread list,
708 re-initializes the LWP lists whenever we mourn or detach (which
709 doesn't involve mourning) the inferior. */
714 struct lwp_info
*lp
, *lpnext
;
716 for (lp
= lwp_list
; lp
; lp
= lpnext
)
727 /* Add the LWP specified by PID to the list. If this causes the
728 number of LWPs to become larger than one, go into "threaded" mode.
729 Return a pointer to the structure describing the new LWP. */
731 static struct lwp_info
*
732 add_lwp (ptid_t ptid
)
736 gdb_assert (is_lwp (ptid
));
738 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
740 memset (lp
, 0, sizeof (struct lwp_info
));
742 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
754 /* Remove the LWP specified by PID from the list. */
757 delete_lwp (ptid_t ptid
)
759 struct lwp_info
*lp
, *lpprev
;
763 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
764 if (ptid_equal (lp
->ptid
, ptid
))
770 /* We don't go back to "non-threaded" mode if the number of threads
771 becomes less than two. */
775 lpprev
->next
= lp
->next
;
782 /* Return a pointer to the structure describing the LWP corresponding
783 to PID. If no corresponding LWP could be found, return NULL. */
785 static struct lwp_info
*
786 find_lwp_pid (ptid_t ptid
)
792 lwp
= GET_LWP (ptid
);
794 lwp
= GET_PID (ptid
);
796 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
797 if (lwp
== GET_LWP (lp
->ptid
))
803 /* Call CALLBACK with its second argument set to DATA for every LWP in
804 the list. If CALLBACK returns 1 for a particular LWP, return a
805 pointer to the structure describing that LWP immediately.
806 Otherwise return NULL. */
809 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
811 struct lwp_info
*lp
, *lpnext
;
813 for (lp
= lwp_list
; lp
; lp
= lpnext
)
816 if ((*callback
) (lp
, data
))
823 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
824 a message telling the user that a new LWP has been added to the
828 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
830 struct lwp_info
*lp
, *found_lp
;
832 gdb_assert (is_lwp (ptid
));
834 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
835 to interrupt either the ptrace() or waitpid() calls below. */
836 if (!sigismember (&blocked_mask
, SIGCHLD
))
838 sigaddset (&blocked_mask
, SIGCHLD
);
839 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
843 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid
));
845 found_lp
= lp
= find_lwp_pid (ptid
);
849 /* We assume that we're already attached to any LWP that has an id
850 equal to the overall process id, and to any LWP that is already
851 in our list of LWPs. If we're not seeing exit events from threads
852 and we've had PID wraparound since we last tried to stop all threads,
853 this assumption might be wrong; fortunately, this is very unlikely
855 if (GET_LWP (ptid
) != GET_PID (ptid
) && found_lp
== NULL
)
860 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
861 error (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
862 safe_strerror (errno
));
865 fprintf_unfiltered (gdb_stdlog
,
866 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
867 target_pid_to_str (ptid
));
869 pid
= my_waitpid (GET_LWP (ptid
), &status
, 0);
870 if (pid
== -1 && errno
== ECHILD
)
872 /* Try again with __WCLONE to check cloned processes. */
873 pid
= my_waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
877 gdb_assert (pid
== GET_LWP (ptid
)
878 && WIFSTOPPED (status
) && WSTOPSIG (status
));
880 child_post_attach (pid
);
886 fprintf_unfiltered (gdb_stdlog
,
887 "LLAL: waitpid %s received %s\n",
888 target_pid_to_str (ptid
),
889 status_to_str (status
));
894 /* We assume that the LWP representing the original process is
895 already stopped. Mark it as stopped in the data structure
896 that the linux ptrace layer uses to keep track of threads.
897 Note that this won't have already been done since the main
898 thread will have, we assume, been stopped by an attach from a
905 linux_nat_attach (char *args
, int from_tty
)
911 /* FIXME: We should probably accept a list of process id's, and
912 attach all of them. */
913 deprecated_child_ops
.to_attach (args
, from_tty
);
915 /* Add the initial process as the first LWP to the list. */
916 lp
= add_lwp (BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
)));
918 /* Make sure the initial process is stopped. The user-level threads
919 layer might want to poke around in the inferior, and that won't
920 work if things haven't stabilized yet. */
921 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, 0);
922 if (pid
== -1 && errno
== ECHILD
)
924 warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid
));
926 /* Try again with __WCLONE to check cloned processes. */
927 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
931 gdb_assert (pid
== GET_PID (inferior_ptid
)
932 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
936 /* Fake the SIGSTOP that core GDB expects. */
937 lp
->status
= W_STOPCODE (SIGSTOP
);
941 fprintf_unfiltered (gdb_stdlog
,
942 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid
);
947 detach_callback (struct lwp_info
*lp
, void *data
)
949 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
951 if (debug_linux_nat
&& lp
->status
)
952 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
953 strsignal (WSTOPSIG (lp
->status
)),
954 target_pid_to_str (lp
->ptid
));
956 while (lp
->signalled
&& lp
->stopped
)
959 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
960 WSTOPSIG (lp
->status
)) < 0)
961 error (_("Can't continue %s: %s"), target_pid_to_str (lp
->ptid
),
962 safe_strerror (errno
));
965 fprintf_unfiltered (gdb_stdlog
,
966 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
967 target_pid_to_str (lp
->ptid
),
968 status_to_str (lp
->status
));
973 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
974 here. But since lp->signalled was cleared above,
975 stop_wait_callback didn't do anything; the process was left
976 running. Shouldn't we be waiting for it to stop?
977 I've removed the call, since stop_wait_callback now does do
978 something when called with lp->signalled == 0. */
980 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
983 /* We don't actually detach from the LWP that has an id equal to the
984 overall process id just yet. */
985 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
988 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
989 WSTOPSIG (lp
->status
)) < 0)
990 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
991 safe_strerror (errno
));
994 fprintf_unfiltered (gdb_stdlog
,
995 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
996 target_pid_to_str (lp
->ptid
),
997 strsignal (WSTOPSIG (lp
->status
)));
999 delete_lwp (lp
->ptid
);
1006 linux_nat_detach (char *args
, int from_tty
)
1008 iterate_over_lwps (detach_callback
, NULL
);
1010 /* Only the initial process should be left right now. */
1011 gdb_assert (num_lwps
== 1);
1013 trap_ptid
= null_ptid
;
1015 /* Destroy LWP info; it's no longer valid. */
1018 /* Restore the original signal mask. */
1019 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1020 sigemptyset (&blocked_mask
);
1022 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
1023 deprecated_child_ops
.to_detach (args
, from_tty
);
1029 resume_callback (struct lwp_info
*lp
, void *data
)
1031 if (lp
->stopped
&& lp
->status
== 0)
1033 struct thread_info
*tp
;
1035 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), 0, TARGET_SIGNAL_0
);
1036 if (debug_linux_nat
)
1037 fprintf_unfiltered (gdb_stdlog
,
1038 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1039 target_pid_to_str (lp
->ptid
));
1048 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1055 resume_set_callback (struct lwp_info
*lp
, void *data
)
1062 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1064 struct lwp_info
*lp
;
1067 /* A specific PTID means `step only this process id'. */
1068 resume_all
= (PIDGET (ptid
) == -1);
1071 iterate_over_lwps (resume_set_callback
, NULL
);
1073 iterate_over_lwps (resume_clear_callback
, NULL
);
1075 /* If PID is -1, it's the current inferior that should be
1076 handled specially. */
1077 if (PIDGET (ptid
) == -1)
1078 ptid
= inferior_ptid
;
1080 lp
= find_lwp_pid (ptid
);
1083 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1085 /* Remember if we're stepping. */
1088 /* Mark this LWP as resumed. */
1091 /* If we have a pending wait status for this thread, there is no
1092 point in resuming the process. */
1095 /* FIXME: What should we do if we are supposed to continue
1096 this thread with a signal? */
1097 gdb_assert (signo
== TARGET_SIGNAL_0
);
1101 /* Mark LWP as not stopped to prevent it from being continued by
1107 iterate_over_lwps (resume_callback
, NULL
);
1109 child_resume (ptid
, step
, signo
);
1110 if (debug_linux_nat
)
1111 fprintf_unfiltered (gdb_stdlog
,
1112 "LLR: %s %s, %s (resume event thread)\n",
1113 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1114 target_pid_to_str (ptid
),
1115 signo
? strsignal (signo
) : "0");
1118 /* Issue kill to specified lwp. */
1120 static int tkill_failed
;
1123 kill_lwp (int lwpid
, int signo
)
1127 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1128 fails, then we are not using nptl threads and we should be using kill. */
1130 #ifdef HAVE_TKILL_SYSCALL
1133 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1134 if (errno
!= ENOSYS
)
1141 return kill (lwpid
, signo
);
1144 /* Handle a GNU/Linux extended wait response. Most of the work we
1145 just pass off to linux_handle_extended_wait, but if it reports a
1146 clone event we need to add the new LWP to our list (and not report
1147 the trap to higher layers). This function returns non-zero if
1148 the event should be ignored and we should wait again. */
1151 linux_nat_handle_extended (struct lwp_info
*lp
, int status
)
1153 linux_handle_extended_wait (GET_LWP (lp
->ptid
), status
,
1156 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1157 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_SPURIOUS
)
1159 struct lwp_info
*new_lp
;
1160 new_lp
= add_lwp (BUILD_LWP (lp
->waitstatus
.value
.related_pid
,
1161 GET_PID (inferior_ptid
)));
1163 new_lp
->stopped
= 1;
1165 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1167 if (debug_linux_nat
)
1168 fprintf_unfiltered (gdb_stdlog
,
1169 "LLHE: Got clone event from LWP %ld, resuming\n",
1170 GET_LWP (lp
->ptid
));
1171 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1179 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1183 wait_lwp (struct lwp_info
*lp
)
1187 int thread_dead
= 0;
1189 gdb_assert (!lp
->stopped
);
1190 gdb_assert (lp
->status
== 0);
1192 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1193 if (pid
== -1 && errno
== ECHILD
)
1195 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1196 if (pid
== -1 && errno
== ECHILD
)
1198 /* The thread has previously exited. We need to delete it
1199 now because, for some vendor 2.4 kernels with NPTL
1200 support backported, there won't be an exit event unless
1201 it is the main thread. 2.6 kernels will report an exit
1202 event for each thread that exits, as expected. */
1204 if (debug_linux_nat
)
1205 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1206 target_pid_to_str (lp
->ptid
));
1212 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1214 if (debug_linux_nat
)
1216 fprintf_unfiltered (gdb_stdlog
,
1217 "WL: waitpid %s received %s\n",
1218 target_pid_to_str (lp
->ptid
),
1219 status_to_str (status
));
1223 /* Check if the thread has exited. */
1224 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1227 if (debug_linux_nat
)
1228 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1229 target_pid_to_str (lp
->ptid
));
1234 if (in_thread_list (lp
->ptid
))
1236 /* Core GDB cannot deal with us deleting the current thread. */
1237 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1238 delete_thread (lp
->ptid
);
1239 printf_unfiltered (_("[%s exited]\n"),
1240 target_pid_to_str (lp
->ptid
));
1243 delete_lwp (lp
->ptid
);
1247 gdb_assert (WIFSTOPPED (status
));
1249 /* Handle GNU/Linux's extended waitstatus for trace events. */
1250 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1252 if (debug_linux_nat
)
1253 fprintf_unfiltered (gdb_stdlog
,
1254 "WL: Handling extended status 0x%06x\n",
1256 if (linux_nat_handle_extended (lp
, status
))
1257 return wait_lwp (lp
);
1263 /* Send a SIGSTOP to LP. */
1266 stop_callback (struct lwp_info
*lp
, void *data
)
1268 if (!lp
->stopped
&& !lp
->signalled
)
1272 if (debug_linux_nat
)
1274 fprintf_unfiltered (gdb_stdlog
,
1275 "SC: kill %s **<SIGSTOP>**\n",
1276 target_pid_to_str (lp
->ptid
));
1279 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1280 if (debug_linux_nat
)
1282 fprintf_unfiltered (gdb_stdlog
,
1283 "SC: lwp kill %d %s\n",
1285 errno
? safe_strerror (errno
) : "ERRNO-OK");
1289 gdb_assert (lp
->status
== 0);
1295 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1296 a pointer to a set of signals to be flushed immediately. */
1299 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1301 sigset_t
*flush_mask
= data
;
1307 status
= wait_lwp (lp
);
1311 /* Ignore any signals in FLUSH_MASK. */
1312 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1321 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1322 if (debug_linux_nat
)
1323 fprintf_unfiltered (gdb_stdlog
,
1324 "PTRACE_CONT %s, 0, 0 (%s)\n",
1325 target_pid_to_str (lp
->ptid
),
1326 errno
? safe_strerror (errno
) : "OK");
1328 return stop_wait_callback (lp
, flush_mask
);
1331 if (WSTOPSIG (status
) != SIGSTOP
)
1333 if (WSTOPSIG (status
) == SIGTRAP
)
1335 /* If a LWP other than the LWP that we're reporting an
1336 event for has hit a GDB breakpoint (as opposed to
1337 some random trap signal), then just arrange for it to
1338 hit it again later. We don't keep the SIGTRAP status
1339 and don't forward the SIGTRAP signal to the LWP. We
1340 will handle the current event, eventually we will
1341 resume all LWPs, and this one will get its breakpoint
1344 If we do not do this, then we run the risk that the
1345 user will delete or disable the breakpoint, but the
1346 thread will have already tripped on it. */
1348 /* Now resume this LWP and get the SIGSTOP event. */
1350 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1351 if (debug_linux_nat
)
1353 fprintf_unfiltered (gdb_stdlog
,
1354 "PTRACE_CONT %s, 0, 0 (%s)\n",
1355 target_pid_to_str (lp
->ptid
),
1356 errno
? safe_strerror (errno
) : "OK");
1358 fprintf_unfiltered (gdb_stdlog
,
1359 "SWC: Candidate SIGTRAP event in %s\n",
1360 target_pid_to_str (lp
->ptid
));
1362 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1363 stop_wait_callback (lp
, data
);
1364 /* If there's another event, throw it back into the queue. */
1367 if (debug_linux_nat
)
1369 fprintf_unfiltered (gdb_stdlog
,
1370 "SWC: kill %s, %s\n",
1371 target_pid_to_str (lp
->ptid
),
1372 status_to_str ((int) status
));
1374 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1376 /* Save the sigtrap event. */
1377 lp
->status
= status
;
1382 /* The thread was stopped with a signal other than
1383 SIGSTOP, and didn't accidentally trip a breakpoint. */
1385 if (debug_linux_nat
)
1387 fprintf_unfiltered (gdb_stdlog
,
1388 "SWC: Pending event %s in %s\n",
1389 status_to_str ((int) status
),
1390 target_pid_to_str (lp
->ptid
));
1392 /* Now resume this LWP and get the SIGSTOP event. */
1394 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1395 if (debug_linux_nat
)
1396 fprintf_unfiltered (gdb_stdlog
,
1397 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1398 target_pid_to_str (lp
->ptid
),
1399 errno
? safe_strerror (errno
) : "OK");
1401 /* Hold this event/waitstatus while we check to see if
1402 there are any more (we still want to get that SIGSTOP). */
1403 stop_wait_callback (lp
, data
);
1404 /* If the lp->status field is still empty, use it to hold
1405 this event. If not, then this event must be returned
1406 to the event queue of the LWP. */
1407 if (lp
->status
== 0)
1408 lp
->status
= status
;
1411 if (debug_linux_nat
)
1413 fprintf_unfiltered (gdb_stdlog
,
1414 "SWC: kill %s, %s\n",
1415 target_pid_to_str (lp
->ptid
),
1416 status_to_str ((int) status
));
1418 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1425 /* We caught the SIGSTOP that we intended to catch, so
1426 there's no SIGSTOP pending. */
1435 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1436 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1439 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
1441 sigset_t blocked
, ignored
;
1444 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
1449 for (i
= 1; i
< NSIG
; i
++)
1450 if (sigismember (pending
, i
))
1451 if (!sigismember (flush_mask
, i
)
1452 || sigismember (&blocked
, i
)
1453 || sigismember (&ignored
, i
))
1454 sigdelset (pending
, i
);
1456 if (sigisemptyset (pending
))
1462 /* DATA is interpreted as a mask of signals to flush. If LP has
1463 signals pending, and they are all in the flush mask, then arrange
1464 to flush them. LP should be stopped, as should all other threads
1465 it might share a signal queue with. */
1468 flush_callback (struct lwp_info
*lp
, void *data
)
1470 sigset_t
*flush_mask
= data
;
1471 sigset_t pending
, intersection
, blocked
, ignored
;
1474 /* Normally, when an LWP exits, it is removed from the LWP list. The
1475 last LWP isn't removed till later, however. So if there is only
1476 one LWP on the list, make sure it's alive. */
1477 if (lwp_list
== lp
&& lp
->next
== NULL
)
1478 if (!linux_nat_thread_alive (lp
->ptid
))
1481 /* Just because the LWP is stopped doesn't mean that new signals
1482 can't arrive from outside, so this function must be careful of
1483 race conditions. However, because all threads are stopped, we
1484 can assume that the pending mask will not shrink unless we resume
1485 the LWP, and that it will then get another signal. We can't
1486 control which one, however. */
1490 if (debug_linux_nat
)
1491 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp
->status
);
1492 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
1496 while (linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
1501 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1502 if (debug_linux_nat
)
1503 fprintf_unfiltered (gdb_stderr
,
1504 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
1507 stop_wait_callback (lp
, flush_mask
);
1508 if (debug_linux_nat
)
1509 fprintf_unfiltered (gdb_stderr
,
1510 "FC: Wait finished; saved status is %d\n",
1517 /* Return non-zero if LP has a wait status pending. */
1520 status_callback (struct lwp_info
*lp
, void *data
)
1522 /* Only report a pending wait status if we pretend that this has
1523 indeed been resumed. */
1524 return (lp
->status
!= 0 && lp
->resumed
);
1527 /* Return non-zero if LP isn't stopped. */
1530 running_callback (struct lwp_info
*lp
, void *data
)
1532 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
1535 /* Count the LWP's that have had events. */
1538 count_events_callback (struct lwp_info
*lp
, void *data
)
1542 gdb_assert (count
!= NULL
);
1544 /* Count only LWPs that have a SIGTRAP event pending. */
1546 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1552 /* Select the LWP (if any) that is currently being single-stepped. */
1555 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
1557 if (lp
->step
&& lp
->status
!= 0)
1563 /* Select the Nth LWP that has had a SIGTRAP event. */
1566 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
1568 int *selector
= data
;
1570 gdb_assert (selector
!= NULL
);
1572 /* Select only LWPs that have a SIGTRAP event pending. */
1574 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1575 if ((*selector
)-- == 0)
1582 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
1584 struct lwp_info
*event_lp
= data
;
1586 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1590 /* If a LWP other than the LWP that we're reporting an event for has
1591 hit a GDB breakpoint (as opposed to some random trap signal),
1592 then just arrange for it to hit it again later. We don't keep
1593 the SIGTRAP status and don't forward the SIGTRAP signal to the
1594 LWP. We will handle the current event, eventually we will resume
1595 all LWPs, and this one will get its breakpoint trap again.
1597 If we do not do this, then we run the risk that the user will
1598 delete or disable the breakpoint, but the LWP will have already
1602 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
1603 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
1604 DECR_PC_AFTER_BREAK
))
1606 if (debug_linux_nat
)
1607 fprintf_unfiltered (gdb_stdlog
,
1608 "CBC: Push back breakpoint for %s\n",
1609 target_pid_to_str (lp
->ptid
));
1611 /* Back up the PC if necessary. */
1612 if (DECR_PC_AFTER_BREAK
)
1613 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
1615 /* Throw away the SIGTRAP. */
1622 /* Select one LWP out of those that have events pending. */
1625 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
1628 int random_selector
;
1629 struct lwp_info
*event_lp
;
1631 /* Record the wait status for the origional LWP. */
1632 (*orig_lp
)->status
= *status
;
1634 /* Give preference to any LWP that is being single-stepped. */
1635 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
1636 if (event_lp
!= NULL
)
1638 if (debug_linux_nat
)
1639 fprintf_unfiltered (gdb_stdlog
,
1640 "SEL: Select single-step %s\n",
1641 target_pid_to_str (event_lp
->ptid
));
1645 /* No single-stepping LWP. Select one at random, out of those
1646 which have had SIGTRAP events. */
1648 /* First see how many SIGTRAP events we have. */
1649 iterate_over_lwps (count_events_callback
, &num_events
);
1651 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1652 random_selector
= (int)
1653 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1655 if (debug_linux_nat
&& num_events
> 1)
1656 fprintf_unfiltered (gdb_stdlog
,
1657 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1658 num_events
, random_selector
);
1660 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
1664 if (event_lp
!= NULL
)
1666 /* Switch the event LWP. */
1667 *orig_lp
= event_lp
;
1668 *status
= event_lp
->status
;
1671 /* Flush the wait status for the event LWP. */
1672 (*orig_lp
)->status
= 0;
1675 /* Return non-zero if LP has been resumed. */
1678 resumed_callback (struct lwp_info
*lp
, void *data
)
1685 /* We need to override child_wait to support attaching to cloned
1686 processes, since a normal wait (as done by the default version)
1687 ignores those processes. */
1689 /* Wait for child PTID to do something. Return id of the child,
1690 minus_one_ptid in case of error; store status into *OURSTATUS. */
1693 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1699 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1703 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1704 attached process. */
1707 pid
= my_waitpid (GET_PID (ptid
), &status
, 0);
1708 if (pid
== -1 && errno
== ECHILD
)
1709 /* Try again with __WCLONE to check cloned processes. */
1710 pid
= my_waitpid (GET_PID (ptid
), &status
, __WCLONE
);
1712 if (debug_linux_nat
)
1714 fprintf_unfiltered (gdb_stdlog
,
1715 "CW: waitpid %ld received %s\n",
1716 (long) pid
, status_to_str (status
));
1721 /* Make sure we don't report an event for the exit of the
1722 original program, if we've detached from it. */
1723 if (pid
!= -1 && !WIFSTOPPED (status
) && pid
!= GET_PID (inferior_ptid
))
1729 /* Check for stop events reported by a process we didn't already
1730 know about - in this case, anything other than inferior_ptid.
1732 If we're expecting to receive stopped processes after fork,
1733 vfork, and clone events, then we'll just add the new one to
1734 our list and go back to waiting for the event to be reported
1735 - the stopped process might be returned from waitpid before
1736 or after the event is. If we want to handle debugging of
1737 CLONE_PTRACE processes we need to do more here, i.e. switch
1738 to multi-threaded mode. */
1739 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
1740 && pid
!= GET_PID (inferior_ptid
))
1742 linux_record_stopped_pid (pid
);
1747 /* Handle GNU/Linux's extended waitstatus for trace events. */
1748 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
1749 && status
>> 16 != 0)
1751 linux_handle_extended_wait (pid
, status
, ourstatus
);
1753 /* If we see a clone event, detach the child, and don't
1754 report the event. It would be nice to offer some way to
1755 switch into a non-thread-db based threaded mode at this
1757 if (ourstatus
->kind
== TARGET_WAITKIND_SPURIOUS
)
1759 ptrace (PTRACE_DETACH
, ourstatus
->value
.related_pid
, 0, 0);
1760 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1761 ptrace (PTRACE_CONT
, pid
, 0, 0);
1767 clear_sigio_trap ();
1768 clear_sigint_trap ();
1770 while (pid
== -1 && save_errno
== EINTR
);
1774 warning (_("Child process unexpectedly missing: %s"),
1775 safe_strerror (errno
));
1777 /* Claim it exited with unknown signal. */
1778 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1779 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1780 return minus_one_ptid
;
1783 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1784 store_waitstatus (ourstatus
, status
);
1786 return pid_to_ptid (pid
);
1791 /* Stop an active thread, verify it still exists, then resume it. */
1794 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
1796 struct lwp_info
*ptr
;
1798 if (!lp
->stopped
&& !lp
->signalled
)
1800 stop_callback (lp
, NULL
);
1801 stop_wait_callback (lp
, NULL
);
1802 /* Resume if the lwp still exists. */
1803 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
1806 resume_callback (lp
, NULL
);
1807 resume_set_callback (lp
, NULL
);
1814 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1816 struct lwp_info
*lp
= NULL
;
1819 pid_t pid
= PIDGET (ptid
);
1820 sigset_t flush_mask
;
1822 sigemptyset (&flush_mask
);
1824 /* Make sure SIGCHLD is blocked. */
1825 if (!sigismember (&blocked_mask
, SIGCHLD
))
1827 sigaddset (&blocked_mask
, SIGCHLD
);
1828 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1833 /* Make sure there is at least one LWP that has been resumed, at
1834 least if there are any LWPs at all. */
1835 gdb_assert (num_lwps
== 0 || iterate_over_lwps (resumed_callback
, NULL
));
1837 /* First check if there is a LWP with a wait status pending. */
1840 /* Any LWP that's been resumed will do. */
1841 lp
= iterate_over_lwps (status_callback
, NULL
);
1844 status
= lp
->status
;
1847 if (debug_linux_nat
&& status
)
1848 fprintf_unfiltered (gdb_stdlog
,
1849 "LLW: Using pending wait status %s for %s.\n",
1850 status_to_str (status
),
1851 target_pid_to_str (lp
->ptid
));
1854 /* But if we don't fine one, we'll have to wait, and check both
1855 cloned and uncloned processes. We start with the cloned
1857 options
= __WCLONE
| WNOHANG
;
1859 else if (is_lwp (ptid
))
1861 if (debug_linux_nat
)
1862 fprintf_unfiltered (gdb_stdlog
,
1863 "LLW: Waiting for specific LWP %s.\n",
1864 target_pid_to_str (ptid
));
1866 /* We have a specific LWP to check. */
1867 lp
= find_lwp_pid (ptid
);
1869 status
= lp
->status
;
1872 if (debug_linux_nat
&& status
)
1873 fprintf_unfiltered (gdb_stdlog
,
1874 "LLW: Using pending wait status %s for %s.\n",
1875 status_to_str (status
),
1876 target_pid_to_str (lp
->ptid
));
1878 /* If we have to wait, take into account whether PID is a cloned
1879 process or not. And we have to convert it to something that
1880 the layer beneath us can understand. */
1881 options
= lp
->cloned
? __WCLONE
: 0;
1882 pid
= GET_LWP (ptid
);
1885 if (status
&& lp
->signalled
)
1887 /* A pending SIGSTOP may interfere with the normal stream of
1888 events. In a typical case where interference is a problem,
1889 we have a SIGSTOP signal pending for LWP A while
1890 single-stepping it, encounter an event in LWP B, and take the
1891 pending SIGSTOP while trying to stop LWP A. After processing
1892 the event in LWP B, LWP A is continued, and we'll never see
1893 the SIGTRAP associated with the last time we were
1894 single-stepping LWP A. */
1896 /* Resume the thread. It should halt immediately returning the
1898 registers_changed ();
1899 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
1901 if (debug_linux_nat
)
1902 fprintf_unfiltered (gdb_stdlog
,
1903 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1904 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1905 target_pid_to_str (lp
->ptid
));
1907 gdb_assert (lp
->resumed
);
1909 /* This should catch the pending SIGSTOP. */
1910 stop_wait_callback (lp
, NULL
);
1913 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1914 attached process. */
1921 lwpid
= my_waitpid (pid
, &status
, options
);
1924 gdb_assert (pid
== -1 || lwpid
== pid
);
1926 if (debug_linux_nat
)
1928 fprintf_unfiltered (gdb_stdlog
,
1929 "LLW: waitpid %ld received %s\n",
1930 (long) lwpid
, status_to_str (status
));
1933 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
1935 /* Check for stop events reported by a process we didn't
1936 already know about - anything not already in our LWP
1939 If we're expecting to receive stopped processes after
1940 fork, vfork, and clone events, then we'll just add the
1941 new one to our list and go back to waiting for the event
1942 to be reported - the stopped process might be returned
1943 from waitpid before or after the event is. */
1944 if (WIFSTOPPED (status
) && !lp
)
1946 linux_record_stopped_pid (lwpid
);
1951 /* Make sure we don't report an event for the exit of an LWP not in
1952 our list, i.e. not part of the current process. This can happen
1953 if we detach from a program we original forked and then it
1955 if (!WIFSTOPPED (status
) && !lp
)
1961 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1962 CLONE_PTRACE processes which do not use the thread library -
1963 otherwise we wouldn't find the new LWP this way. That doesn't
1964 currently work, and the following code is currently unreachable
1965 due to the two blocks above. If it's fixed some day, this code
1966 should be broken out into a function so that we can also pick up
1967 LWPs from the new interface. */
1970 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
1971 if (options
& __WCLONE
)
1976 gdb_assert (WIFSTOPPED (status
)
1977 && WSTOPSIG (status
) == SIGSTOP
);
1980 if (!in_thread_list (inferior_ptid
))
1982 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
1983 GET_PID (inferior_ptid
));
1984 add_thread (inferior_ptid
);
1987 add_thread (lp
->ptid
);
1988 printf_unfiltered (_("[New %s]\n"),
1989 target_pid_to_str (lp
->ptid
));
1993 /* Handle GNU/Linux's extended waitstatus for trace events. */
1994 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1996 if (debug_linux_nat
)
1997 fprintf_unfiltered (gdb_stdlog
,
1998 "LLW: Handling extended status 0x%06x\n",
2000 if (linux_nat_handle_extended (lp
, status
))
2007 /* Check if the thread has exited. */
2008 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
2010 if (in_thread_list (lp
->ptid
))
2012 /* Core GDB cannot deal with us deleting the current
2014 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2015 delete_thread (lp
->ptid
);
2016 printf_unfiltered (_("[%s exited]\n"),
2017 target_pid_to_str (lp
->ptid
));
2020 /* If this is the main thread, we must stop all threads and
2021 verify if they are still alive. This is because in the nptl
2022 thread model, there is no signal issued for exiting LWPs
2023 other than the main thread. We only get the main thread
2024 exit signal once all child threads have already exited.
2025 If we stop all the threads and use the stop_wait_callback
2026 to check if they have exited we can determine whether this
2027 signal should be ignored or whether it means the end of the
2028 debugged application, regardless of which threading model
2030 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2033 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2036 if (debug_linux_nat
)
2037 fprintf_unfiltered (gdb_stdlog
,
2038 "LLW: %s exited.\n",
2039 target_pid_to_str (lp
->ptid
));
2041 delete_lwp (lp
->ptid
);
2043 /* If there is at least one more LWP, then the exit signal
2044 was not the end of the debugged application and should be
2048 /* Make sure there is at least one thread running. */
2049 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2051 /* Discard the event. */
2057 /* Check if the current LWP has previously exited. In the nptl
2058 thread model, LWPs other than the main thread do not issue
2059 signals when they exit so we must check whenever the thread
2060 has stopped. A similar check is made in stop_wait_callback(). */
2061 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2063 if (in_thread_list (lp
->ptid
))
2065 /* Core GDB cannot deal with us deleting the current
2067 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2068 delete_thread (lp
->ptid
);
2069 printf_unfiltered (_("[%s exited]\n"),
2070 target_pid_to_str (lp
->ptid
));
2072 if (debug_linux_nat
)
2073 fprintf_unfiltered (gdb_stdlog
,
2074 "LLW: %s exited.\n",
2075 target_pid_to_str (lp
->ptid
));
2077 delete_lwp (lp
->ptid
);
2079 /* Make sure there is at least one thread running. */
2080 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2082 /* Discard the event. */
2087 /* Make sure we don't report a SIGSTOP that we sent
2088 ourselves in an attempt to stop an LWP. */
2090 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2092 if (debug_linux_nat
)
2093 fprintf_unfiltered (gdb_stdlog
,
2094 "LLW: Delayed SIGSTOP caught for %s.\n",
2095 target_pid_to_str (lp
->ptid
));
2097 /* This is a delayed SIGSTOP. */
2100 registers_changed ();
2101 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
2103 if (debug_linux_nat
)
2104 fprintf_unfiltered (gdb_stdlog
,
2105 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2107 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2108 target_pid_to_str (lp
->ptid
));
2111 gdb_assert (lp
->resumed
);
2113 /* Discard the event. */
2123 /* Alternate between checking cloned and uncloned processes. */
2124 options
^= __WCLONE
;
2126 /* And suspend every time we have checked both. */
2127 if (options
& __WCLONE
)
2128 sigsuspend (&suspend_mask
);
2131 /* We shouldn't end up here unless we want to try again. */
2132 gdb_assert (status
== 0);
2135 clear_sigio_trap ();
2136 clear_sigint_trap ();
2140 /* Don't report signals that GDB isn't interested in, such as
2141 signals that are neither printed nor stopped upon. Stopping all
2142 threads can be a bit time-consuming so if we want decent
2143 performance with heavily multi-threaded programs, especially when
2144 they're using a high frequency timer, we'd better avoid it if we
2147 if (WIFSTOPPED (status
))
2149 int signo
= target_signal_from_host (WSTOPSIG (status
));
2151 if (signal_stop_state (signo
) == 0
2152 && signal_print_state (signo
) == 0
2153 && signal_pass_state (signo
) == 1)
2155 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2156 here? It is not clear we should. GDB may not expect
2157 other threads to run. On the other hand, not resuming
2158 newly attached threads may cause an unwanted delay in
2159 getting them running. */
2160 registers_changed ();
2161 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
, signo
);
2162 if (debug_linux_nat
)
2163 fprintf_unfiltered (gdb_stdlog
,
2164 "LLW: %s %s, %s (preempt 'handle')\n",
2166 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2167 target_pid_to_str (lp
->ptid
),
2168 signo
? strsignal (signo
) : "0");
2174 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2176 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2177 forwarded to the entire process group, that is, all LWP's
2178 will receive it. Since we only want to report it once,
2179 we try to flush it from all LWPs except this one. */
2180 sigaddset (&flush_mask
, SIGINT
);
2184 /* This LWP is stopped now. */
2187 if (debug_linux_nat
)
2188 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2189 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2191 /* Now stop all other LWP's ... */
2192 iterate_over_lwps (stop_callback
, NULL
);
2194 /* ... and wait until all of them have reported back that they're no
2196 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2197 iterate_over_lwps (flush_callback
, &flush_mask
);
2199 /* If we're not waiting for a specific LWP, choose an event LWP from
2200 among those that have had events. Giving equal priority to all
2201 LWPs that have had events helps prevent starvation. */
2203 select_event_lwp (&lp
, &status
);
2205 /* Now that we've selected our final event LWP, cancel any
2206 breakpoints in other LWPs that have hit a GDB breakpoint. See
2207 the comment in cancel_breakpoints_callback to find out why. */
2208 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2210 /* If we're not running in "threaded" mode, we'll report the bare
2213 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2215 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2216 if (debug_linux_nat
)
2217 fprintf_unfiltered (gdb_stdlog
,
2218 "LLW: trap_ptid is %s.\n",
2219 target_pid_to_str (trap_ptid
));
2222 trap_ptid
= null_ptid
;
2224 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2226 *ourstatus
= lp
->waitstatus
;
2227 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2230 store_waitstatus (ourstatus
, status
);
2232 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2236 kill_callback (struct lwp_info
*lp
, void *data
)
2239 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2240 if (debug_linux_nat
)
2241 fprintf_unfiltered (gdb_stdlog
,
2242 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2243 target_pid_to_str (lp
->ptid
),
2244 errno
? safe_strerror (errno
) : "OK");
2250 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2254 /* We must make sure that there are no pending events (delayed
2255 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2256 program doesn't interfere with any following debugging session. */
2258 /* For cloned processes we must check both with __WCLONE and
2259 without, since the exit status of a cloned process isn't reported
2265 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2266 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2268 fprintf_unfiltered (gdb_stdlog
,
2269 "KWC: wait %s received unknown.\n",
2270 target_pid_to_str (lp
->ptid
));
2273 while (pid
== GET_LWP (lp
->ptid
));
2275 gdb_assert (pid
== -1 && errno
== ECHILD
);
2280 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2281 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2283 fprintf_unfiltered (gdb_stdlog
,
2284 "KWC: wait %s received unk.\n",
2285 target_pid_to_str (lp
->ptid
));
2288 while (pid
== GET_LWP (lp
->ptid
));
2290 gdb_assert (pid
== -1 && errno
== ECHILD
);
2295 linux_nat_kill (void)
2297 /* Kill all LWP's ... */
2298 iterate_over_lwps (kill_callback
, NULL
);
2300 /* ... and wait until we've flushed all events. */
2301 iterate_over_lwps (kill_wait_callback
, NULL
);
2303 target_mourn_inferior ();
2307 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
2310 deprecated_child_ops
.to_create_inferior (exec_file
, allargs
, env
, from_tty
);
2314 linux_nat_mourn_inferior (void)
2316 trap_ptid
= null_ptid
;
2318 /* Destroy LWP info; it's no longer valid. */
2321 /* Restore the original signal mask. */
2322 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
2323 sigemptyset (&blocked_mask
);
2325 deprecated_child_ops
.to_mourn_inferior ();
2329 linux_nat_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
2330 struct mem_attrib
*attrib
, struct target_ops
*target
)
2332 struct cleanup
*old_chain
= save_inferior_ptid ();
2335 if (is_lwp (inferior_ptid
))
2336 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2338 xfer
= linux_proc_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2340 xfer
= child_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2342 do_cleanups (old_chain
);
2347 linux_nat_thread_alive (ptid_t ptid
)
2349 gdb_assert (is_lwp (ptid
));
2352 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2353 if (debug_linux_nat
)
2354 fprintf_unfiltered (gdb_stdlog
,
2355 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2356 target_pid_to_str (ptid
),
2357 errno
? safe_strerror (errno
) : "OK");
2365 linux_nat_pid_to_str (ptid_t ptid
)
2367 static char buf
[64];
2371 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2375 return normal_pid_to_str (ptid
);
2379 init_linux_nat_ops (void)
2382 linux_nat_ops
.to_open
= linux_nat_open
;
2384 linux_nat_ops
.to_shortname
= "lwp-layer";
2385 linux_nat_ops
.to_longname
= "lwp-layer";
2386 linux_nat_ops
.to_doc
= "Low level threads support (LWP layer)";
2387 linux_nat_ops
.to_attach
= linux_nat_attach
;
2388 linux_nat_ops
.to_detach
= linux_nat_detach
;
2389 linux_nat_ops
.to_resume
= linux_nat_resume
;
2390 linux_nat_ops
.to_wait
= linux_nat_wait
;
2391 /* fetch_inferior_registers and store_inferior_registers will
2392 honor the LWP id, so we can use them directly. */
2393 linux_nat_ops
.to_fetch_registers
= fetch_inferior_registers
;
2394 linux_nat_ops
.to_store_registers
= store_inferior_registers
;
2395 linux_nat_ops
.deprecated_xfer_memory
= linux_nat_xfer_memory
;
2396 linux_nat_ops
.to_kill
= linux_nat_kill
;
2397 linux_nat_ops
.to_create_inferior
= linux_nat_create_inferior
;
2398 linux_nat_ops
.to_mourn_inferior
= linux_nat_mourn_inferior
;
2399 linux_nat_ops
.to_thread_alive
= linux_nat_thread_alive
;
2400 linux_nat_ops
.to_pid_to_str
= linux_nat_pid_to_str
;
2401 linux_nat_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
2402 linux_nat_ops
.to_post_attach
= child_post_attach
;
2403 linux_nat_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
2404 linux_nat_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
2405 linux_nat_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
2407 linux_nat_ops
.to_stratum
= thread_stratum
;
2408 linux_nat_ops
.to_has_thread_control
= tc_schedlock
;
2409 linux_nat_ops
.to_magic
= OPS_MAGIC
;
2413 sigchld_handler (int signo
)
2415 /* Do nothing. The only reason for this handler is that it allows
2416 us to use sigsuspend in linux_nat_wait above to wait for the
2417 arrival of a SIGCHLD. */
2420 /* Accepts an integer PID; Returns a string representing a file that
2421 can be opened to get the symbols for the child process. */
2424 child_pid_to_exec_file (int pid
)
2426 char *name1
, *name2
;
2428 name1
= xmalloc (MAXPATHLEN
);
2429 name2
= xmalloc (MAXPATHLEN
);
2430 make_cleanup (xfree
, name1
);
2431 make_cleanup (xfree
, name2
);
2432 memset (name2
, 0, MAXPATHLEN
);
2434 sprintf (name1
, "/proc/%d/exe", pid
);
2435 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
2441 /* Service function for corefiles and info proc. */
2444 read_mapping (FILE *mapfile
,
2449 char *device
, long long *inode
, char *filename
)
2451 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
2452 addr
, endaddr
, permissions
, offset
, device
, inode
);
2454 if (ret
> 0 && ret
!= EOF
&& *inode
!= 0)
2456 /* Eat everything up to EOL for the filename. This will prevent
2457 weird filenames (such as one with embedded whitespace) from
2458 confusing this code. It also makes this code more robust in
2459 respect to annotations the kernel may add after the filename.
2461 Note the filename is used for informational purposes
2463 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
2467 filename
[0] = '\0'; /* no filename */
2468 fscanf (mapfile
, "\n");
2470 return (ret
!= 0 && ret
!= EOF
);
2473 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2474 regions in the inferior for a corefile. */
2477 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
2479 int, int, int, void *), void *obfd
)
2481 long long pid
= PIDGET (inferior_ptid
);
2482 char mapsfilename
[MAXPATHLEN
];
2484 long long addr
, endaddr
, size
, offset
, inode
;
2485 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2486 int read
, write
, exec
;
2489 /* Compose the filename for the /proc memory map, and open it. */
2490 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
2491 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
2492 error (_("Could not open %s."), mapsfilename
);
2495 fprintf_filtered (gdb_stdout
,
2496 "Reading memory regions from %s\n", mapsfilename
);
2498 /* Now iterate until end-of-file. */
2499 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
2500 &offset
, &device
[0], &inode
, &filename
[0]))
2502 size
= endaddr
- addr
;
2504 /* Get the segment's permissions. */
2505 read
= (strchr (permissions
, 'r') != 0);
2506 write
= (strchr (permissions
, 'w') != 0);
2507 exec
= (strchr (permissions
, 'x') != 0);
2511 fprintf_filtered (gdb_stdout
,
2512 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2513 size
, paddr_nz (addr
),
2515 write
? 'w' : ' ', exec
? 'x' : ' ');
2516 if (filename
&& filename
[0])
2517 fprintf_filtered (gdb_stdout
, " for %s", filename
);
2518 fprintf_filtered (gdb_stdout
, "\n");
2521 /* Invoke the callback function to create the corefile
2523 func (addr
, size
, read
, write
, exec
, obfd
);
2529 /* Records the thread's register state for the corefile note
2533 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
2534 char *note_data
, int *note_size
)
2536 gdb_gregset_t gregs
;
2537 gdb_fpregset_t fpregs
;
2538 #ifdef FILL_FPXREGSET
2539 gdb_fpxregset_t fpxregs
;
2541 unsigned long lwp
= ptid_get_lwp (ptid
);
2543 fill_gregset (&gregs
, -1);
2544 note_data
= (char *) elfcore_write_prstatus (obfd
,
2548 stop_signal
, &gregs
);
2550 fill_fpregset (&fpregs
, -1);
2551 note_data
= (char *) elfcore_write_prfpreg (obfd
,
2554 &fpregs
, sizeof (fpregs
));
2555 #ifdef FILL_FPXREGSET
2556 fill_fpxregset (&fpxregs
, -1);
2557 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
2560 &fpxregs
, sizeof (fpxregs
));
2565 struct linux_nat_corefile_thread_data
2573 /* Called by gdbthread.c once per thread. Records the thread's
2574 register state for the corefile note section. */
2577 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
2579 struct linux_nat_corefile_thread_data
*args
= data
;
2580 ptid_t saved_ptid
= inferior_ptid
;
2582 inferior_ptid
= ti
->ptid
;
2583 registers_changed ();
2584 target_fetch_registers (-1); /* FIXME should not be necessary;
2585 fill_gregset should do it automatically. */
2586 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
2591 inferior_ptid
= saved_ptid
;
2592 registers_changed ();
2593 target_fetch_registers (-1); /* FIXME should not be necessary;
2594 fill_gregset should do it automatically. */
2598 /* Records the register state for the corefile note section. */
2601 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
2602 char *note_data
, int *note_size
)
2604 registers_changed ();
2605 target_fetch_registers (-1); /* FIXME should not be necessary;
2606 fill_gregset should do it automatically. */
2607 return linux_nat_do_thread_registers (obfd
,
2608 ptid_build (ptid_get_pid (inferior_ptid
),
2609 ptid_get_pid (inferior_ptid
),
2611 note_data
, note_size
);
2615 /* Fills the "to_make_corefile_note" target vector. Builds the note
2616 section for a corefile, and returns it in a malloc buffer. */
2619 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
2621 struct linux_nat_corefile_thread_data thread_args
;
2622 struct cleanup
*old_chain
;
2623 char fname
[16] = { '\0' };
2624 char psargs
[80] = { '\0' };
2625 char *note_data
= NULL
;
2626 ptid_t current_ptid
= inferior_ptid
;
2630 if (get_exec_file (0))
2632 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
2633 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
2634 if (get_inferior_args ())
2636 strncat (psargs
, " ", sizeof (psargs
) - strlen (psargs
));
2637 strncat (psargs
, get_inferior_args (),
2638 sizeof (psargs
) - strlen (psargs
));
2640 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
2642 note_size
, fname
, psargs
);
2645 /* Dump information for threads. */
2646 thread_args
.obfd
= obfd
;
2647 thread_args
.note_data
= note_data
;
2648 thread_args
.note_size
= note_size
;
2649 thread_args
.num_notes
= 0;
2650 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
2651 if (thread_args
.num_notes
== 0)
2653 /* iterate_over_threads didn't come up with any threads; just
2654 use inferior_ptid. */
2655 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
2656 note_data
, note_size
);
2660 note_data
= thread_args
.note_data
;
2663 auxv_len
= target_auxv_read (¤t_target
, &auxv
);
2666 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
2667 "CORE", NT_AUXV
, auxv
, auxv_len
);
2671 make_cleanup (xfree
, note_data
);
2675 /* Implement the "info proc" command. */
2678 linux_nat_info_proc_cmd (char *args
, int from_tty
)
2680 long long pid
= PIDGET (inferior_ptid
);
2683 char buffer
[MAXPATHLEN
];
2684 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
2697 /* Break up 'args' into an argv array. */
2698 if ((argv
= buildargv (args
)) == NULL
)
2701 make_cleanup_freeargv (argv
);
2703 while (argv
!= NULL
&& *argv
!= NULL
)
2705 if (isdigit (argv
[0][0]))
2707 pid
= strtoul (argv
[0], NULL
, 10);
2709 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
2713 else if (strcmp (argv
[0], "status") == 0)
2717 else if (strcmp (argv
[0], "stat") == 0)
2721 else if (strcmp (argv
[0], "cmd") == 0)
2725 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
2729 else if (strcmp (argv
[0], "cwd") == 0)
2733 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
2739 /* [...] (future options here) */
2744 error (_("No current process: you must name one."));
2746 sprintf (fname1
, "/proc/%lld", pid
);
2747 if (stat (fname1
, &dummy
) != 0)
2748 error (_("No /proc directory: '%s'"), fname1
);
2750 printf_filtered (_("process %lld\n"), pid
);
2751 if (cmdline_f
|| all
)
2753 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
2754 if ((procfile
= fopen (fname1
, "r")) > 0)
2756 fgets (buffer
, sizeof (buffer
), procfile
);
2757 printf_filtered ("cmdline = '%s'\n", buffer
);
2761 warning (_("unable to open /proc file '%s'"), fname1
);
2765 sprintf (fname1
, "/proc/%lld/cwd", pid
);
2766 memset (fname2
, 0, sizeof (fname2
));
2767 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2768 printf_filtered ("cwd = '%s'\n", fname2
);
2770 warning (_("unable to read link '%s'"), fname1
);
2774 sprintf (fname1
, "/proc/%lld/exe", pid
);
2775 memset (fname2
, 0, sizeof (fname2
));
2776 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2777 printf_filtered ("exe = '%s'\n", fname2
);
2779 warning (_("unable to read link '%s'"), fname1
);
2781 if (mappings_f
|| all
)
2783 sprintf (fname1
, "/proc/%lld/maps", pid
);
2784 if ((procfile
= fopen (fname1
, "r")) > 0)
2786 long long addr
, endaddr
, size
, offset
, inode
;
2787 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2789 printf_filtered (_("Mapped address spaces:\n\n"));
2790 if (TARGET_ADDR_BIT
== 32)
2792 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2795 " Size", " Offset", "objfile");
2799 printf_filtered (" %18s %18s %10s %10s %7s\n",
2802 " Size", " Offset", "objfile");
2805 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
2806 &offset
, &device
[0], &inode
, &filename
[0]))
2808 size
= endaddr
- addr
;
2810 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2811 calls here (and possibly above) should be abstracted
2812 out into their own functions? Andrew suggests using
2813 a generic local_address_string instead to print out
2814 the addresses; that makes sense to me, too. */
2816 if (TARGET_ADDR_BIT
== 32)
2818 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2819 (unsigned long) addr
, /* FIXME: pr_addr */
2820 (unsigned long) endaddr
,
2822 (unsigned int) offset
,
2823 filename
[0] ? filename
: "");
2827 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2828 (unsigned long) addr
, /* FIXME: pr_addr */
2829 (unsigned long) endaddr
,
2831 (unsigned int) offset
,
2832 filename
[0] ? filename
: "");
2839 warning (_("unable to open /proc file '%s'"), fname1
);
2841 if (status_f
|| all
)
2843 sprintf (fname1
, "/proc/%lld/status", pid
);
2844 if ((procfile
= fopen (fname1
, "r")) > 0)
2846 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
2847 puts_filtered (buffer
);
2851 warning (_("unable to open /proc file '%s'"), fname1
);
2855 sprintf (fname1
, "/proc/%lld/stat", pid
);
2856 if ((procfile
= fopen (fname1
, "r")) > 0)
2861 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2862 printf_filtered (_("Process: %d\n"), itmp
);
2863 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
2864 printf_filtered (_("Exec file: %s\n"), buffer
);
2865 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
2866 printf_filtered (_("State: %c\n"), ctmp
);
2867 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2868 printf_filtered (_("Parent process: %d\n"), itmp
);
2869 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2870 printf_filtered (_("Process group: %d\n"), itmp
);
2871 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2872 printf_filtered (_("Session id: %d\n"), itmp
);
2873 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2874 printf_filtered (_("TTY: %d\n"), itmp
);
2875 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2876 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
2877 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2878 printf_filtered (_("Flags: 0x%x\n"), itmp
);
2879 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2880 printf_filtered (_("Minor faults (no memory page): %u\n"),
2881 (unsigned int) itmp
);
2882 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2883 printf_filtered (_("Minor faults, children: %u\n"),
2884 (unsigned int) itmp
);
2885 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2886 printf_filtered (_("Major faults (memory page faults): %u\n"),
2887 (unsigned int) itmp
);
2888 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2889 printf_filtered (_("Major faults, children: %u\n"),
2890 (unsigned int) itmp
);
2891 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2892 printf_filtered ("utime: %d\n", itmp
);
2893 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2894 printf_filtered ("stime: %d\n", itmp
);
2895 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2896 printf_filtered ("utime, children: %d\n", itmp
);
2897 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2898 printf_filtered ("stime, children: %d\n", itmp
);
2899 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2900 printf_filtered (_("jiffies remaining in current time slice: %d\n"),
2902 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2903 printf_filtered ("'nice' value: %d\n", itmp
);
2904 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2905 printf_filtered (_("jiffies until next timeout: %u\n"),
2906 (unsigned int) itmp
);
2907 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2908 printf_filtered ("jiffies until next SIGALRM: %u\n",
2909 (unsigned int) itmp
);
2910 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2911 printf_filtered (_("start time (jiffies since system boot): %d\n"),
2913 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2914 printf_filtered (_("Virtual memory size: %u\n"),
2915 (unsigned int) itmp
);
2916 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2917 printf_filtered (_("Resident set size: %u\n"), (unsigned int) itmp
);
2918 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2919 printf_filtered ("rlim: %u\n", (unsigned int) itmp
);
2920 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2921 printf_filtered (_("Start of text: 0x%x\n"), itmp
);
2922 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2923 printf_filtered (_("End of text: 0x%x\n"), itmp
);
2924 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2925 printf_filtered (_("Start of stack: 0x%x\n"), itmp
);
2926 #if 0 /* Don't know how architecture-dependent the rest is...
2927 Anyway the signal bitmap info is available from "status". */
2928 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2929 printf_filtered (_("Kernel stack pointer: 0x%x\n"), itmp
);
2930 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2931 printf_filtered (_("Kernel instr pointer: 0x%x\n"), itmp
);
2932 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2933 printf_filtered (_("Pending signals bitmap: 0x%x\n"), itmp
);
2934 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2935 printf_filtered (_("Blocked signals bitmap: 0x%x\n"), itmp
);
2936 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2937 printf_filtered (_("Ignored signals bitmap: 0x%x\n"), itmp
);
2938 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2939 printf_filtered (_("Catched signals bitmap: 0x%x\n"), itmp
);
2940 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2941 printf_filtered (_("wchan (system call): 0x%x\n"), itmp
);
2946 warning (_("unable to open /proc file '%s'"), fname1
);
2951 linux_proc_xfer_memory (CORE_ADDR addr
, char *myaddr
, int len
, int write
,
2952 struct mem_attrib
*attrib
, struct target_ops
*target
)
2960 /* Don't bother for one word. */
2961 if (len
< 3 * sizeof (long))
2964 /* We could keep this file open and cache it - possibly one per
2965 thread. That requires some juggling, but is even faster. */
2966 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
2967 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
2971 /* If pread64 is available, use it. It's faster if the kernel
2972 supports it (only one syscall), and it's 64-bit safe even on
2973 32-bit platforms (for instance, SPARC debugging a SPARC64
2976 if (pread64 (fd
, myaddr
, len
, addr
) != len
)
2978 if (lseek (fd
, addr
, SEEK_SET
) == -1 || read (fd
, myaddr
, len
) != len
)
2988 /* Parse LINE as a signal set and add its set bits to SIGS. */
2991 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
2993 int len
= strlen (line
) - 1;
2997 if (line
[len
] != '\n')
2998 error (_("Could not parse signal set: %s"), line
);
3006 if (*p
>= '0' && *p
<= '9')
3008 else if (*p
>= 'a' && *p
<= 'f')
3009 digit
= *p
- 'a' + 10;
3011 error (_("Could not parse signal set: %s"), line
);
3016 sigaddset (sigs
, signum
+ 1);
3018 sigaddset (sigs
, signum
+ 2);
3020 sigaddset (sigs
, signum
+ 3);
3022 sigaddset (sigs
, signum
+ 4);
3028 /* Find process PID's pending signals from /proc/pid/status and set
3032 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3035 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3038 sigemptyset (pending
);
3039 sigemptyset (blocked
);
3040 sigemptyset (ignored
);
3041 sprintf (fname
, "/proc/%d/status", pid
);
3042 procfile
= fopen (fname
, "r");
3043 if (procfile
== NULL
)
3044 error (_("Could not open %s"), fname
);
3046 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3048 /* Normal queued signals are on the SigPnd line in the status
3049 file. However, 2.6 kernels also have a "shared" pending
3050 queue for delivering signals to a thread group, so check for
3053 Unfortunately some Red Hat kernels include the shared pending
3054 queue but not the ShdPnd status field. */
3056 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3057 add_line_to_sigset (buffer
+ 8, pending
);
3058 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3059 add_line_to_sigset (buffer
+ 8, pending
);
3060 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3061 add_line_to_sigset (buffer
+ 8, blocked
);
3062 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3063 add_line_to_sigset (buffer
+ 8, ignored
);
3070 _initialize_linux_nat (void)
3072 struct sigaction action
;
3073 extern void thread_db_init (struct target_ops
*);
3075 deprecated_child_ops
.to_find_memory_regions
= linux_nat_find_memory_regions
;
3076 deprecated_child_ops
.to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3078 add_info ("proc", linux_nat_info_proc_cmd
, _("\
3079 Show /proc process information about any running process.\n\
3080 Specify any process id, or use the program being debugged by default.\n\
3081 Specify any of the following keywords for detailed info:\n\
3082 mappings -- list of mapped memory regions.\n\
3083 stat -- list a bunch of random process info.\n\
3084 status -- list a different bunch of random process info.\n\
3085 all -- list all available /proc info."));
3087 init_linux_nat_ops ();
3088 add_target (&linux_nat_ops
);
3089 thread_db_init (&linux_nat_ops
);
3091 /* Save the original signal mask. */
3092 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
3094 action
.sa_handler
= sigchld_handler
;
3095 sigemptyset (&action
.sa_mask
);
3096 action
.sa_flags
= SA_RESTART
;
3097 sigaction (SIGCHLD
, &action
, NULL
);
3099 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3100 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
3101 sigdelset (&suspend_mask
, SIGCHLD
);
3103 sigemptyset (&blocked_mask
);
3105 add_setshow_zinteger_cmd ("lin-lwp", no_class
, &debug_linux_nat
, _("\
3106 Set debugging of GNU/Linux lwp module."), _("\
3107 Show debugging of GNU/Linux lwp module."), _("\
3108 Enables printf debugging output."),
3110 show_debug_linux_nat
,
3111 &setdebuglist
, &showdebuglist
);
3115 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3116 the GNU/Linux Threads library and therefore doesn't really belong
3119 /* Read variable NAME in the target and return its value if found.
3120 Otherwise return zero. It is assumed that the type of the variable
3124 get_signo (const char *name
)
3126 struct minimal_symbol
*ms
;
3129 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
3133 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (char *) &signo
,
3134 sizeof (signo
)) != 0)
3140 /* Return the set of signals used by the threads library in *SET. */
3143 lin_thread_get_thread_signals (sigset_t
*set
)
3145 struct sigaction action
;
3146 int restart
, cancel
;
3150 restart
= get_signo ("__pthread_sig_restart");
3154 cancel
= get_signo ("__pthread_sig_cancel");
3158 sigaddset (set
, restart
);
3159 sigaddset (set
, cancel
);
3161 /* The GNU/Linux Threads library makes terminating threads send a
3162 special "cancel" signal instead of SIGCHLD. Make sure we catch
3163 those (to prevent them from terminating GDB itself, which is
3164 likely to be their default action) and treat them the same way as
3167 action
.sa_handler
= sigchld_handler
;
3168 sigemptyset (&action
.sa_mask
);
3169 action
.sa_flags
= SA_RESTART
;
3170 sigaction (cancel
, &action
, NULL
);
3172 /* We block the "cancel" signal throughout this code ... */
3173 sigaddset (&blocked_mask
, cancel
);
3174 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
3176 /* ... except during a sigsuspend. */
3177 sigdelset (&suspend_mask
, cancel
);