1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_string.h"
26 #include "gdb_assert.h"
27 #ifdef HAVE_TKILL_SYSCALL
29 #include <sys/syscall.h>
31 #include <sys/ptrace.h>
32 #include "linux-nat.h"
33 #include "linux-fork.h"
34 #include "gdbthread.h"
38 #include "inf-ptrace.h"
40 #include <sys/param.h> /* for MAXPATHLEN */
41 #include <sys/procfs.h> /* for elf_gregset etc. */
42 #include "elf-bfd.h" /* for elfcore_write_* */
43 #include "gregset.h" /* for gregset */
44 #include "gdbcore.h" /* for get_exec_file */
45 #include <ctype.h> /* for isdigit */
46 #include "gdbthread.h" /* for struct thread_info etc. */
47 #include "gdb_stat.h" /* for struct stat */
48 #include <fcntl.h> /* for O_RDONLY */
54 /* If the system headers did not provide the constants, hard-code the normal
56 #ifndef PTRACE_EVENT_FORK
58 #define PTRACE_SETOPTIONS 0x4200
59 #define PTRACE_GETEVENTMSG 0x4201
61 /* options set using PTRACE_SETOPTIONS */
62 #define PTRACE_O_TRACESYSGOOD 0x00000001
63 #define PTRACE_O_TRACEFORK 0x00000002
64 #define PTRACE_O_TRACEVFORK 0x00000004
65 #define PTRACE_O_TRACECLONE 0x00000008
66 #define PTRACE_O_TRACEEXEC 0x00000010
67 #define PTRACE_O_TRACEVFORKDONE 0x00000020
68 #define PTRACE_O_TRACEEXIT 0x00000040
70 /* Wait extended result codes for the above trace options. */
71 #define PTRACE_EVENT_FORK 1
72 #define PTRACE_EVENT_VFORK 2
73 #define PTRACE_EVENT_CLONE 3
74 #define PTRACE_EVENT_EXEC 4
75 #define PTRACE_EVENT_VFORK_DONE 5
76 #define PTRACE_EVENT_EXIT 6
78 #endif /* PTRACE_EVENT_FORK */
80 /* We can't always assume that this flag is available, but all systems
81 with the ptrace event handlers also have __WALL, so it's safe to use
84 #define __WALL 0x40000000 /* Wait for any child. */
87 #ifndef PTRACE_GETSIGINFO
88 #define PTRACE_GETSIGINFO 0x4202
91 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
92 the use of the multi-threaded target. */
93 static struct target_ops *linux_ops;
94 static struct target_ops linux_ops_saved;
96 /* The method to call, if any, when a new thread is attached. */
97 static void (*linux_nat_new_thread) (ptid_t);
99 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
100 Called by our to_xfer_partial. */
101 static LONGEST (*super_xfer_partial) (struct target_ops *,
103 const char *, gdb_byte *,
107 static int debug_linux_nat;
109 show_debug_linux_nat (struct ui_file *file, int from_tty,
110 struct cmd_list_element *c, const char *value)
112 fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
116 static int linux_parent_pid;
118 struct simple_pid_list
122 struct simple_pid_list *next;
124 struct simple_pid_list *stopped_pids;
126 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
127 can not be used, 1 if it can. */
129 static int linux_supports_tracefork_flag = -1;
131 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
132 PTRACE_O_TRACEVFORKDONE. */
134 static int linux_supports_tracevforkdone_flag = -1;
137 /* Trivial list manipulation functions to keep track of a list of
138 new stopped processes. */
140 add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
142 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
144 new_pid->status = status;
145 new_pid->next = *listp;
150 pull_pid_from_list (struct simple_pid_list **listp, int pid, int *status)
152 struct simple_pid_list **p;
154 for (p = listp; *p != NULL; p = &(*p)->next)
155 if ((*p)->pid == pid)
157 struct simple_pid_list *next = (*p)->next;
158 *status = (*p)->status;
167 linux_record_stopped_pid (int pid, int status)
169 add_to_pid_list (&stopped_pids, pid, status);
173 /* A helper function for linux_test_for_tracefork, called after fork (). */
176 linux_tracefork_child (void)
180 ptrace (PTRACE_TRACEME, 0, 0, 0);
181 kill (getpid (), SIGSTOP);
186 /* Wrapper function for waitpid which handles EINTR. */
189 my_waitpid (int pid, int *status, int flags)
194 ret = waitpid (pid, status, flags);
196 while (ret == -1 && errno == EINTR);
201 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
203 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
204 we know that the feature is not available. This may change the tracing
205 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
207 However, if it succeeds, we don't know for sure that the feature is
208 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
209 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
210 fork tracing, and let it fork. If the process exits, we assume that we
211 can't use TRACEFORK; if we get the fork notification, and we can extract
212 the new child's PID, then we assume that we can. */
215 linux_test_for_tracefork (int original_pid)
217 int child_pid, ret, status;
220 linux_supports_tracefork_flag = 0;
221 linux_supports_tracevforkdone_flag = 0;
223 ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK);
229 perror_with_name (("fork"));
232 linux_tracefork_child ();
234 ret = my_waitpid (child_pid, &status, 0);
236 perror_with_name (("waitpid"));
237 else if (ret != child_pid)
238 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret);
239 if (! WIFSTOPPED (status))
240 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status);
242 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
245 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
248 warning (_("linux_test_for_tracefork: failed to kill child"));
252 ret = my_waitpid (child_pid, &status, 0);
253 if (ret != child_pid)
254 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
255 else if (!WIFSIGNALED (status))
256 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
257 "killed child"), status);
262 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
263 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
264 PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
265 linux_supports_tracevforkdone_flag = (ret == 0);
267 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
269 warning (_("linux_test_for_tracefork: failed to resume child"));
271 ret = my_waitpid (child_pid, &status, 0);
273 if (ret == child_pid && WIFSTOPPED (status)
274 && status >> 16 == PTRACE_EVENT_FORK)
277 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
278 if (ret == 0 && second_pid != 0)
282 linux_supports_tracefork_flag = 1;
283 my_waitpid (second_pid, &second_status, 0);
284 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
286 warning (_("linux_test_for_tracefork: failed to kill second child"));
287 my_waitpid (second_pid, &status, 0);
291 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
292 "(%d, status 0x%x)"), ret, status);
294 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
296 warning (_("linux_test_for_tracefork: failed to kill child"));
297 my_waitpid (child_pid, &status, 0);
300 /* Return non-zero iff we have tracefork functionality available.
301 This function also sets linux_supports_tracefork_flag. */
304 linux_supports_tracefork (int pid)
306 if (linux_supports_tracefork_flag == -1)
307 linux_test_for_tracefork (pid);
308 return linux_supports_tracefork_flag;
312 linux_supports_tracevforkdone (int pid)
314 if (linux_supports_tracefork_flag == -1)
315 linux_test_for_tracefork (pid);
316 return linux_supports_tracevforkdone_flag;
321 linux_enable_event_reporting (ptid_t ptid)
323 int pid = ptid_get_lwp (ptid);
327 pid = ptid_get_pid (ptid);
329 if (! linux_supports_tracefork (pid))
332 options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC
333 | PTRACE_O_TRACECLONE;
334 if (linux_supports_tracevforkdone (pid))
335 options |= PTRACE_O_TRACEVFORKDONE;
337 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
338 read-only process state. */
340 ptrace (PTRACE_SETOPTIONS, pid, 0, options);
344 linux_child_post_attach (int pid)
346 linux_enable_event_reporting (pid_to_ptid (pid));
347 check_for_thread_db ();
351 linux_child_post_startup_inferior (ptid_t ptid)
353 linux_enable_event_reporting (ptid);
354 check_for_thread_db ();
358 linux_child_follow_fork (struct target_ops *ops, int follow_child)
361 struct target_waitstatus last_status;
363 int parent_pid, child_pid;
365 get_last_target_status (&last_ptid, &last_status);
366 has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
367 parent_pid = ptid_get_lwp (last_ptid);
369 parent_pid = ptid_get_pid (last_ptid);
370 child_pid = last_status.value.related_pid;
374 /* We're already attached to the parent, by default. */
376 /* Before detaching from the child, remove all breakpoints from
377 it. (This won't actually modify the breakpoint list, but will
378 physically remove the breakpoints from the child.) */
379 /* If we vforked this will remove the breakpoints from the parent
380 also, but they'll be reinserted below. */
381 detach_breakpoints (child_pid);
383 /* Detach new forked process? */
388 target_terminal_ours ();
389 fprintf_filtered (gdb_stdlog,
390 "Detaching after fork from child process %d.\n",
394 ptrace (PTRACE_DETACH, child_pid, 0, 0);
398 struct fork_info *fp;
399 /* Retain child fork in ptrace (stopped) state. */
400 fp = find_fork_pid (child_pid);
402 fp = add_fork (child_pid);
403 fork_save_infrun_state (fp, 0);
408 gdb_assert (linux_supports_tracefork_flag >= 0);
409 if (linux_supports_tracevforkdone (0))
413 ptrace (PTRACE_CONT, parent_pid, 0, 0);
414 my_waitpid (parent_pid, &status, __WALL);
415 if ((status >> 16) != PTRACE_EVENT_VFORK_DONE)
416 warning (_("Unexpected waitpid result %06x when waiting for "
417 "vfork-done"), status);
421 /* We can't insert breakpoints until the child has
422 finished with the shared memory region. We need to
423 wait until that happens. Ideal would be to just
425 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
426 - waitpid (parent_pid, &status, __WALL);
427 However, most architectures can't handle a syscall
428 being traced on the way out if it wasn't traced on
431 We might also think to loop, continuing the child
432 until it exits or gets a SIGTRAP. One problem is
433 that the child might call ptrace with PTRACE_TRACEME.
435 There's no simple and reliable way to figure out when
436 the vforked child will be done with its copy of the
437 shared memory. We could step it out of the syscall,
438 two instructions, let it go, and then single-step the
439 parent once. When we have hardware single-step, this
440 would work; with software single-step it could still
441 be made to work but we'd have to be able to insert
442 single-step breakpoints in the child, and we'd have
443 to insert -just- the single-step breakpoint in the
444 parent. Very awkward.
446 In the end, the best we can do is to make sure it
447 runs for a little while. Hopefully it will be out of
448 range of any breakpoints we reinsert. Usually this
449 is only the single-step breakpoint at vfork's return
455 /* Since we vforked, breakpoints were removed in the parent
456 too. Put them back. */
457 reattach_breakpoints (parent_pid);
462 char child_pid_spelling[40];
464 /* Needed to keep the breakpoint lists in sync. */
466 detach_breakpoints (child_pid);
468 /* Before detaching from the parent, remove all breakpoints from it. */
469 remove_breakpoints ();
473 target_terminal_ours ();
474 fprintf_filtered (gdb_stdlog,
475 "Attaching after fork to child process %d.\n",
479 /* If we're vforking, we may want to hold on to the parent until
480 the child exits or execs. At exec time we can remove the old
481 breakpoints from the parent and detach it; at exit time we
482 could do the same (or even, sneakily, resume debugging it - the
483 child's exec has failed, or something similar).
485 This doesn't clean up "properly", because we can't call
486 target_detach, but that's OK; if the current target is "child",
487 then it doesn't need any further cleanups, and lin_lwp will
488 generally not encounter vfork (vfork is defined to fork
491 The holding part is very easy if we have VFORKDONE events;
492 but keeping track of both processes is beyond GDB at the
493 moment. So we don't expose the parent to the rest of GDB.
494 Instead we quietly hold onto it until such time as we can
498 linux_parent_pid = parent_pid;
499 else if (!detach_fork)
501 struct fork_info *fp;
502 /* Retain parent fork in ptrace (stopped) state. */
503 fp = find_fork_pid (parent_pid);
505 fp = add_fork (parent_pid);
506 fork_save_infrun_state (fp, 0);
510 target_detach (NULL, 0);
513 inferior_ptid = ptid_build (child_pid, child_pid, 0);
515 /* Reinstall ourselves, since we might have been removed in
516 target_detach (which does other necessary cleanup). */
519 linux_nat_switch_fork (inferior_ptid);
520 check_for_thread_db ();
522 /* Reset breakpoints in the child as appropriate. */
523 follow_inferior_reset_breakpoints ();
531 linux_child_insert_fork_catchpoint (int pid)
533 if (! linux_supports_tracefork (pid))
534 error (_("Your system does not support fork catchpoints."));
538 linux_child_insert_vfork_catchpoint (int pid)
540 if (!linux_supports_tracefork (pid))
541 error (_("Your system does not support vfork catchpoints."));
545 linux_child_insert_exec_catchpoint (int pid)
547 if (!linux_supports_tracefork (pid))
548 error (_("Your system does not support exec catchpoints."));
551 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
552 are processes sharing the same VM space. A multi-threaded process
553 is basically a group of such processes. However, such a grouping
554 is almost entirely a user-space issue; the kernel doesn't enforce
555 such a grouping at all (this might change in the future). In
556 general, we'll rely on the threads library (i.e. the GNU/Linux
557 Threads library) to provide such a grouping.
559 It is perfectly well possible to write a multi-threaded application
560 without the assistance of a threads library, by using the clone
561 system call directly. This module should be able to give some
562 rudimentary support for debugging such applications if developers
563 specify the CLONE_PTRACE flag in the clone system call, and are
564 using the Linux kernel 2.4 or above.
566 Note that there are some peculiarities in GNU/Linux that affect
569 - In general one should specify the __WCLONE flag to waitpid in
570 order to make it report events for any of the cloned processes
571 (and leave it out for the initial process). However, if a cloned
572 process has exited the exit status is only reported if the
573 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
574 we cannot use it since GDB must work on older systems too.
576 - When a traced, cloned process exits and is waited for by the
577 debugger, the kernel reassigns it to the original parent and
578 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
579 library doesn't notice this, which leads to the "zombie problem":
580 When debugged a multi-threaded process that spawns a lot of
581 threads will run out of processes, even if the threads exit,
582 because the "zombies" stay around. */
584 /* List of known LWPs. */
585 struct lwp_info *lwp_list;
587 /* Number of LWPs in the list. */
591 #define GET_LWP(ptid) ptid_get_lwp (ptid)
592 #define GET_PID(ptid) ptid_get_pid (ptid)
593 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
594 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
596 /* If the last reported event was a SIGTRAP, this variable is set to
597 the process id of the LWP/thread that got it. */
601 /* Since we cannot wait (in linux_nat_wait) for the initial process and
602 any cloned processes with a single call to waitpid, we have to use
603 the WNOHANG flag and call waitpid in a loop. To optimize
604 things a bit we use `sigsuspend' to wake us up when a process has
605 something to report (it will send us a SIGCHLD if it has). To make
606 this work we have to juggle with the signal mask. We save the
607 original signal mask such that we can restore it before creating a
608 new process in order to avoid blocking certain signals in the
609 inferior. We then block SIGCHLD during the waitpid/sigsuspend
612 /* Original signal mask. */
613 static sigset_t normal_mask;
615 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
616 _initialize_linux_nat. */
617 static sigset_t suspend_mask;
619 /* Signals to block to make that sigsuspend work. */
620 static sigset_t blocked_mask;
623 /* Prototypes for local functions. */
624 static int stop_wait_callback (struct lwp_info *lp, void *data);
625 static int linux_nat_thread_alive (ptid_t ptid);
626 static char *linux_child_pid_to_exec_file (int pid);
628 /* Convert wait status STATUS to a string. Used for printing debug
632 status_to_str (int status)
636 if (WIFSTOPPED (status))
637 snprintf (buf, sizeof (buf), "%s (stopped)",
638 strsignal (WSTOPSIG (status)));
639 else if (WIFSIGNALED (status))
640 snprintf (buf, sizeof (buf), "%s (terminated)",
641 strsignal (WSTOPSIG (status)));
643 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
648 /* Initialize the list of LWPs. Note that this module, contrary to
649 what GDB's generic threads layer does for its thread list,
650 re-initializes the LWP lists whenever we mourn or detach (which
651 doesn't involve mourning) the inferior. */
656 struct lwp_info *lp, *lpnext;
658 for (lp = lwp_list; lp; lp = lpnext)
668 /* Add the LWP specified by PID to the list. Return a pointer to the
669 structure describing the new LWP. The LWP should already be stopped
670 (with an exception for the very first LWP). */
672 static struct lwp_info *
673 add_lwp (ptid_t ptid)
677 gdb_assert (is_lwp (ptid));
679 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
681 memset (lp, 0, sizeof (struct lwp_info));
683 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
691 if (num_lwps > 1 && linux_nat_new_thread != NULL)
692 linux_nat_new_thread (ptid);
697 /* Remove the LWP specified by PID from the list. */
700 delete_lwp (ptid_t ptid)
702 struct lwp_info *lp, *lpprev;
706 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
707 if (ptid_equal (lp->ptid, ptid))
716 lpprev->next = lp->next;
723 /* Return a pointer to the structure describing the LWP corresponding
724 to PID. If no corresponding LWP could be found, return NULL. */
726 static struct lwp_info *
727 find_lwp_pid (ptid_t ptid)
733 lwp = GET_LWP (ptid);
735 lwp = GET_PID (ptid);
737 for (lp = lwp_list; lp; lp = lp->next)
738 if (lwp == GET_LWP (lp->ptid))
744 /* Call CALLBACK with its second argument set to DATA for every LWP in
745 the list. If CALLBACK returns 1 for a particular LWP, return a
746 pointer to the structure describing that LWP immediately.
747 Otherwise return NULL. */
750 iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
752 struct lwp_info *lp, *lpnext;
754 for (lp = lwp_list; lp; lp = lpnext)
757 if ((*callback) (lp, data))
764 /* Update our internal state when changing from one fork (checkpoint,
765 et cetera) to another indicated by NEW_PTID. We can only switch
766 single-threaded applications, so we only create one new LWP, and
767 the previous list is discarded. */
770 linux_nat_switch_fork (ptid_t new_ptid)
775 lp = add_lwp (new_ptid);
779 /* Record a PTID for later deletion. */
784 struct saved_ptids *next;
786 static struct saved_ptids *threads_to_delete;
789 record_dead_thread (ptid_t ptid)
791 struct saved_ptids *p = xmalloc (sizeof (struct saved_ptids));
793 p->next = threads_to_delete;
794 threads_to_delete = p;
797 /* Delete any dead threads which are not the current thread. */
802 struct saved_ptids **p = &threads_to_delete;
805 if (! ptid_equal ((*p)->ptid, inferior_ptid))
807 struct saved_ptids *tmp = *p;
808 delete_thread (tmp->ptid);
816 /* Callback for iterate_over_threads that finds a thread corresponding
820 find_thread_from_lwp (struct thread_info *thr, void *dummy)
822 ptid_t *ptid_p = dummy;
824 if (GET_LWP (thr->ptid) && GET_LWP (thr->ptid) == GET_LWP (*ptid_p))
830 /* Handle the exit of a single thread LP. */
833 exit_lwp (struct lwp_info *lp)
835 if (in_thread_list (lp->ptid))
837 /* Core GDB cannot deal with us deleting the current thread. */
838 if (!ptid_equal (lp->ptid, inferior_ptid))
839 delete_thread (lp->ptid);
841 record_dead_thread (lp->ptid);
842 printf_unfiltered (_("[%s exited]\n"),
843 target_pid_to_str (lp->ptid));
847 /* Even if LP->PTID is not in the global GDB thread list, the
848 LWP may be - with an additional thread ID. We don't need
849 to print anything in this case; thread_db is in use and
850 already took care of that. But it didn't delete the thread
851 in order to handle zombies correctly. */
853 struct thread_info *thr;
855 thr = iterate_over_threads (find_thread_from_lwp, &lp->ptid);
858 if (!ptid_equal (thr->ptid, inferior_ptid))
859 delete_thread (thr->ptid);
861 record_dead_thread (thr->ptid);
865 delete_lwp (lp->ptid);
868 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
869 a message telling the user that a new LWP has been added to the
870 process. Return 0 if successful or -1 if the new LWP could not
874 lin_lwp_attach_lwp (ptid_t ptid, int verbose)
878 gdb_assert (is_lwp (ptid));
880 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
881 to interrupt either the ptrace() or waitpid() calls below. */
882 if (!sigismember (&blocked_mask, SIGCHLD))
884 sigaddset (&blocked_mask, SIGCHLD);
885 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
888 lp = find_lwp_pid (ptid);
890 /* We assume that we're already attached to any LWP that has an id
891 equal to the overall process id, and to any LWP that is already
892 in our list of LWPs. If we're not seeing exit events from threads
893 and we've had PID wraparound since we last tried to stop all threads,
894 this assumption might be wrong; fortunately, this is very unlikely
896 if (GET_LWP (ptid) != GET_PID (ptid) && lp == NULL)
902 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
904 /* If we fail to attach to the thread, issue a warning,
905 but continue. One way this can happen is if thread
906 creation is interrupted; as of Linux kernel 2.6.19, a
907 bug may place threads in the thread list and then fail
909 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid),
910 safe_strerror (errno));
915 fprintf_unfiltered (gdb_stdlog,
916 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
917 target_pid_to_str (ptid));
919 pid = my_waitpid (GET_LWP (ptid), &status, 0);
920 if (pid == -1 && errno == ECHILD)
922 /* Try again with __WCLONE to check cloned processes. */
923 pid = my_waitpid (GET_LWP (ptid), &status, __WCLONE);
927 gdb_assert (pid == GET_LWP (ptid)
928 && WIFSTOPPED (status) && WSTOPSIG (status));
934 target_post_attach (pid);
940 fprintf_unfiltered (gdb_stdlog,
941 "LLAL: waitpid %s received %s\n",
942 target_pid_to_str (ptid),
943 status_to_str (status));
948 /* We assume that the LWP representing the original process is
949 already stopped. Mark it as stopped in the data structure
950 that the GNU/linux ptrace layer uses to keep track of
951 threads. Note that this won't have already been done since
952 the main thread will have, we assume, been stopped by an
953 attach from a different layer. */
960 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid));
966 linux_nat_attach (char *args, int from_tty)
973 /* FIXME: We should probably accept a list of process id's, and
974 attach all of them. */
975 linux_ops->to_attach (args, from_tty);
977 /* Make sure the initial process is stopped. The user-level threads
978 layer might want to poke around in the inferior, and that won't
979 work if things haven't stabilized yet. */
980 pid = my_waitpid (GET_PID (inferior_ptid), &status, 0);
981 if (pid == -1 && errno == ECHILD)
983 warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid));
985 /* Try again with __WCLONE to check cloned processes. */
986 pid = my_waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
990 gdb_assert (pid == GET_PID (inferior_ptid)
991 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
993 /* Add the initial process as the first LWP to the list. */
994 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid));
995 lp = add_lwp (inferior_ptid);
1000 /* Fake the SIGSTOP that core GDB expects. */
1001 lp->status = W_STOPCODE (SIGSTOP);
1003 if (debug_linux_nat)
1005 fprintf_unfiltered (gdb_stdlog,
1006 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid);
1011 detach_callback (struct lwp_info *lp, void *data)
1013 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1015 if (debug_linux_nat && lp->status)
1016 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
1017 strsignal (WSTOPSIG (lp->status)),
1018 target_pid_to_str (lp->ptid));
1020 while (lp->signalled && lp->stopped)
1023 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
1024 WSTOPSIG (lp->status)) < 0)
1025 error (_("Can't continue %s: %s"), target_pid_to_str (lp->ptid),
1026 safe_strerror (errno));
1028 if (debug_linux_nat)
1029 fprintf_unfiltered (gdb_stdlog,
1030 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
1031 target_pid_to_str (lp->ptid),
1032 status_to_str (lp->status));
1037 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
1038 here. But since lp->signalled was cleared above,
1039 stop_wait_callback didn't do anything; the process was left
1040 running. Shouldn't we be waiting for it to stop?
1041 I've removed the call, since stop_wait_callback now does do
1042 something when called with lp->signalled == 0. */
1044 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1047 /* We don't actually detach from the LWP that has an id equal to the
1048 overall process id just yet. */
1049 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
1052 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
1053 WSTOPSIG (lp->status)) < 0)
1054 error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
1055 safe_strerror (errno));
1057 if (debug_linux_nat)
1058 fprintf_unfiltered (gdb_stdlog,
1059 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1060 target_pid_to_str (lp->ptid),
1061 strsignal (WSTOPSIG (lp->status)));
1063 delete_lwp (lp->ptid);
1070 linux_nat_detach (char *args, int from_tty)
1072 iterate_over_lwps (detach_callback, NULL);
1074 /* Only the initial process should be left right now. */
1075 gdb_assert (num_lwps == 1);
1077 trap_ptid = null_ptid;
1079 /* Destroy LWP info; it's no longer valid. */
1082 /* Restore the original signal mask. */
1083 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1084 sigemptyset (&blocked_mask);
1086 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
1087 linux_ops->to_detach (args, from_tty);
1093 resume_callback (struct lwp_info *lp, void *data)
1095 if (lp->stopped && lp->status == 0)
1097 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
1098 0, TARGET_SIGNAL_0);
1099 if (debug_linux_nat)
1100 fprintf_unfiltered (gdb_stdlog,
1101 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1102 target_pid_to_str (lp->ptid));
1105 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
1112 resume_clear_callback (struct lwp_info *lp, void *data)
1119 resume_set_callback (struct lwp_info *lp, void *data)
1126 linux_nat_resume (ptid_t ptid, int step, enum target_signal signo)
1128 struct lwp_info *lp;
1131 if (debug_linux_nat)
1132 fprintf_unfiltered (gdb_stdlog,
1133 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1134 step ? "step" : "resume",
1135 target_pid_to_str (ptid),
1136 signo ? strsignal (signo) : "0",
1137 target_pid_to_str (inferior_ptid));
1141 /* A specific PTID means `step only this process id'. */
1142 resume_all = (PIDGET (ptid) == -1);
1145 iterate_over_lwps (resume_set_callback, NULL);
1147 iterate_over_lwps (resume_clear_callback, NULL);
1149 /* If PID is -1, it's the current inferior that should be
1150 handled specially. */
1151 if (PIDGET (ptid) == -1)
1152 ptid = inferior_ptid;
1154 lp = find_lwp_pid (ptid);
1155 gdb_assert (lp != NULL);
1157 ptid = pid_to_ptid (GET_LWP (lp->ptid));
1159 /* Remember if we're stepping. */
1162 /* Mark this LWP as resumed. */
1165 /* If we have a pending wait status for this thread, there is no
1166 point in resuming the process. But first make sure that
1167 linux_nat_wait won't preemptively handle the event - we
1168 should never take this short-circuit if we are going to
1169 leave LP running, since we have skipped resuming all the
1170 other threads. This bit of code needs to be synchronized
1171 with linux_nat_wait. */
1173 if (lp->status && WIFSTOPPED (lp->status))
1175 int saved_signo = target_signal_from_host (WSTOPSIG (lp->status));
1177 if (signal_stop_state (saved_signo) == 0
1178 && signal_print_state (saved_signo) == 0
1179 && signal_pass_state (saved_signo) == 1)
1181 if (debug_linux_nat)
1182 fprintf_unfiltered (gdb_stdlog,
1183 "LLR: Not short circuiting for ignored "
1184 "status 0x%x\n", lp->status);
1186 /* FIXME: What should we do if we are supposed to continue
1187 this thread with a signal? */
1188 gdb_assert (signo == TARGET_SIGNAL_0);
1189 signo = saved_signo;
1196 /* FIXME: What should we do if we are supposed to continue
1197 this thread with a signal? */
1198 gdb_assert (signo == TARGET_SIGNAL_0);
1200 if (debug_linux_nat)
1201 fprintf_unfiltered (gdb_stdlog,
1202 "LLR: Short circuiting for status 0x%x\n",
1208 /* Mark LWP as not stopped to prevent it from being continued by
1213 iterate_over_lwps (resume_callback, NULL);
1215 linux_ops->to_resume (ptid, step, signo);
1216 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
1218 if (debug_linux_nat)
1219 fprintf_unfiltered (gdb_stdlog,
1220 "LLR: %s %s, %s (resume event thread)\n",
1221 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1222 target_pid_to_str (ptid),
1223 signo ? strsignal (signo) : "0");
1226 /* Issue kill to specified lwp. */
1228 static int tkill_failed;
1231 kill_lwp (int lwpid, int signo)
1235 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1236 fails, then we are not using nptl threads and we should be using kill. */
1238 #ifdef HAVE_TKILL_SYSCALL
1241 int ret = syscall (__NR_tkill, lwpid, signo);
1242 if (errno != ENOSYS)
1249 return kill (lwpid, signo);
1252 /* Handle a GNU/Linux extended wait response. If we see a clone
1253 event, we need to add the new LWP to our list (and not report the
1254 trap to higher layers). This function returns non-zero if the
1255 event should be ignored and we should wait again. If STOPPING is
1256 true, the new LWP remains stopped, otherwise it is continued. */
1259 linux_handle_extended_wait (struct lwp_info *lp, int status,
1262 int pid = GET_LWP (lp->ptid);
1263 struct target_waitstatus *ourstatus = &lp->waitstatus;
1264 struct lwp_info *new_lp = NULL;
1265 int event = status >> 16;
1267 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
1268 || event == PTRACE_EVENT_CLONE)
1270 unsigned long new_pid;
1273 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
1275 /* If we haven't already seen the new PID stop, wait for it now. */
1276 if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
1278 /* The new child has a pending SIGSTOP. We can't affect it until it
1279 hits the SIGSTOP, but we're already attached. */
1280 ret = my_waitpid (new_pid, &status,
1281 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
1283 perror_with_name (_("waiting for new child"));
1284 else if (ret != new_pid)
1285 internal_error (__FILE__, __LINE__,
1286 _("wait returned unexpected PID %d"), ret);
1287 else if (!WIFSTOPPED (status))
1288 internal_error (__FILE__, __LINE__,
1289 _("wait returned unexpected status 0x%x"), status);
1292 ourstatus->value.related_pid = new_pid;
1294 if (event == PTRACE_EVENT_FORK)
1295 ourstatus->kind = TARGET_WAITKIND_FORKED;
1296 else if (event == PTRACE_EVENT_VFORK)
1297 ourstatus->kind = TARGET_WAITKIND_VFORKED;
1300 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1301 new_lp = add_lwp (BUILD_LWP (new_pid, GET_PID (inferior_ptid)));
1304 if (WSTOPSIG (status) != SIGSTOP)
1306 /* This can happen if someone starts sending signals to
1307 the new thread before it gets a chance to run, which
1308 have a lower number than SIGSTOP (e.g. SIGUSR1).
1309 This is an unlikely case, and harder to handle for
1310 fork / vfork than for clone, so we do not try - but
1311 we handle it for clone events here. We'll send
1312 the other signal on to the thread below. */
1314 new_lp->signalled = 1;
1320 new_lp->stopped = 1;
1323 new_lp->resumed = 1;
1324 ptrace (PTRACE_CONT, lp->waitstatus.value.related_pid, 0,
1325 status ? WSTOPSIG (status) : 0);
1328 if (debug_linux_nat)
1329 fprintf_unfiltered (gdb_stdlog,
1330 "LHEW: Got clone event from LWP %ld, resuming\n",
1331 GET_LWP (lp->ptid));
1332 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1340 if (event == PTRACE_EVENT_EXEC)
1342 ourstatus->kind = TARGET_WAITKIND_EXECD;
1343 ourstatus->value.execd_pathname
1344 = xstrdup (linux_child_pid_to_exec_file (pid));
1346 if (linux_parent_pid)
1348 detach_breakpoints (linux_parent_pid);
1349 ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0);
1351 linux_parent_pid = 0;
1357 internal_error (__FILE__, __LINE__,
1358 _("unknown ptrace event %d"), event);
1361 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1365 wait_lwp (struct lwp_info *lp)
1369 int thread_dead = 0;
1371 gdb_assert (!lp->stopped);
1372 gdb_assert (lp->status == 0);
1374 pid = my_waitpid (GET_LWP (lp->ptid), &status, 0);
1375 if (pid == -1 && errno == ECHILD)
1377 pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
1378 if (pid == -1 && errno == ECHILD)
1380 /* The thread has previously exited. We need to delete it
1381 now because, for some vendor 2.4 kernels with NPTL
1382 support backported, there won't be an exit event unless
1383 it is the main thread. 2.6 kernels will report an exit
1384 event for each thread that exits, as expected. */
1386 if (debug_linux_nat)
1387 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
1388 target_pid_to_str (lp->ptid));
1394 gdb_assert (pid == GET_LWP (lp->ptid));
1396 if (debug_linux_nat)
1398 fprintf_unfiltered (gdb_stdlog,
1399 "WL: waitpid %s received %s\n",
1400 target_pid_to_str (lp->ptid),
1401 status_to_str (status));
1405 /* Check if the thread has exited. */
1406 if (WIFEXITED (status) || WIFSIGNALED (status))
1409 if (debug_linux_nat)
1410 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
1411 target_pid_to_str (lp->ptid));
1420 gdb_assert (WIFSTOPPED (status));
1422 /* Handle GNU/Linux's extended waitstatus for trace events. */
1423 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1425 if (debug_linux_nat)
1426 fprintf_unfiltered (gdb_stdlog,
1427 "WL: Handling extended status 0x%06x\n",
1429 if (linux_handle_extended_wait (lp, status, 1))
1430 return wait_lwp (lp);
1436 /* Save the most recent siginfo for LP. This is currently only called
1437 for SIGTRAP; some ports use the si_addr field for
1438 target_stopped_data_address. In the future, it may also be used to
1439 restore the siginfo of requeued signals. */
1442 save_siginfo (struct lwp_info *lp)
1445 ptrace (PTRACE_GETSIGINFO, GET_LWP (lp->ptid),
1446 (PTRACE_TYPE_ARG3) 0, &lp->siginfo);
1449 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
1452 /* Send a SIGSTOP to LP. */
1455 stop_callback (struct lwp_info *lp, void *data)
1457 if (!lp->stopped && !lp->signalled)
1461 if (debug_linux_nat)
1463 fprintf_unfiltered (gdb_stdlog,
1464 "SC: kill %s **<SIGSTOP>**\n",
1465 target_pid_to_str (lp->ptid));
1468 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
1469 if (debug_linux_nat)
1471 fprintf_unfiltered (gdb_stdlog,
1472 "SC: lwp kill %d %s\n",
1474 errno ? safe_strerror (errno) : "ERRNO-OK");
1478 gdb_assert (lp->status == 0);
1484 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1485 a pointer to a set of signals to be flushed immediately. */
1488 stop_wait_callback (struct lwp_info *lp, void *data)
1490 sigset_t *flush_mask = data;
1496 status = wait_lwp (lp);
1500 /* Ignore any signals in FLUSH_MASK. */
1501 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
1510 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1511 if (debug_linux_nat)
1512 fprintf_unfiltered (gdb_stdlog,
1513 "PTRACE_CONT %s, 0, 0 (%s)\n",
1514 target_pid_to_str (lp->ptid),
1515 errno ? safe_strerror (errno) : "OK");
1517 return stop_wait_callback (lp, flush_mask);
1520 if (WSTOPSIG (status) != SIGSTOP)
1522 if (WSTOPSIG (status) == SIGTRAP)
1524 /* If a LWP other than the LWP that we're reporting an
1525 event for has hit a GDB breakpoint (as opposed to
1526 some random trap signal), then just arrange for it to
1527 hit it again later. We don't keep the SIGTRAP status
1528 and don't forward the SIGTRAP signal to the LWP. We
1529 will handle the current event, eventually we will
1530 resume all LWPs, and this one will get its breakpoint
1533 If we do not do this, then we run the risk that the
1534 user will delete or disable the breakpoint, but the
1535 thread will have already tripped on it. */
1537 /* Save the trap's siginfo in case we need it later. */
1540 /* Now resume this LWP and get the SIGSTOP event. */
1542 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1543 if (debug_linux_nat)
1545 fprintf_unfiltered (gdb_stdlog,
1546 "PTRACE_CONT %s, 0, 0 (%s)\n",
1547 target_pid_to_str (lp->ptid),
1548 errno ? safe_strerror (errno) : "OK");
1550 fprintf_unfiltered (gdb_stdlog,
1551 "SWC: Candidate SIGTRAP event in %s\n",
1552 target_pid_to_str (lp->ptid));
1554 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1555 stop_wait_callback (lp, data);
1556 /* If there's another event, throw it back into the queue. */
1559 if (debug_linux_nat)
1561 fprintf_unfiltered (gdb_stdlog,
1562 "SWC: kill %s, %s\n",
1563 target_pid_to_str (lp->ptid),
1564 status_to_str ((int) status));
1566 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
1568 /* Save the sigtrap event. */
1569 lp->status = status;
1574 /* The thread was stopped with a signal other than
1575 SIGSTOP, and didn't accidentally trip a breakpoint. */
1577 if (debug_linux_nat)
1579 fprintf_unfiltered (gdb_stdlog,
1580 "SWC: Pending event %s in %s\n",
1581 status_to_str ((int) status),
1582 target_pid_to_str (lp->ptid));
1584 /* Now resume this LWP and get the SIGSTOP event. */
1586 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1587 if (debug_linux_nat)
1588 fprintf_unfiltered (gdb_stdlog,
1589 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1590 target_pid_to_str (lp->ptid),
1591 errno ? safe_strerror (errno) : "OK");
1593 /* Hold this event/waitstatus while we check to see if
1594 there are any more (we still want to get that SIGSTOP). */
1595 stop_wait_callback (lp, data);
1596 /* If the lp->status field is still empty, use it to hold
1597 this event. If not, then this event must be returned
1598 to the event queue of the LWP. */
1599 if (lp->status == 0)
1600 lp->status = status;
1603 if (debug_linux_nat)
1605 fprintf_unfiltered (gdb_stdlog,
1606 "SWC: kill %s, %s\n",
1607 target_pid_to_str (lp->ptid),
1608 status_to_str ((int) status));
1610 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
1617 /* We caught the SIGSTOP that we intended to catch, so
1618 there's no SIGSTOP pending. */
1627 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1628 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1631 linux_nat_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask)
1633 sigset_t blocked, ignored;
1636 linux_proc_pending_signals (pid, pending, &blocked, &ignored);
1641 for (i = 1; i < NSIG; i++)
1642 if (sigismember (pending, i))
1643 if (!sigismember (flush_mask, i)
1644 || sigismember (&blocked, i)
1645 || sigismember (&ignored, i))
1646 sigdelset (pending, i);
1648 if (sigisemptyset (pending))
1654 /* DATA is interpreted as a mask of signals to flush. If LP has
1655 signals pending, and they are all in the flush mask, then arrange
1656 to flush them. LP should be stopped, as should all other threads
1657 it might share a signal queue with. */
1660 flush_callback (struct lwp_info *lp, void *data)
1662 sigset_t *flush_mask = data;
1663 sigset_t pending, intersection, blocked, ignored;
1666 /* Normally, when an LWP exits, it is removed from the LWP list. The
1667 last LWP isn't removed till later, however. So if there is only
1668 one LWP on the list, make sure it's alive. */
1669 if (lwp_list == lp && lp->next == NULL)
1670 if (!linux_nat_thread_alive (lp->ptid))
1673 /* Just because the LWP is stopped doesn't mean that new signals
1674 can't arrive from outside, so this function must be careful of
1675 race conditions. However, because all threads are stopped, we
1676 can assume that the pending mask will not shrink unless we resume
1677 the LWP, and that it will then get another signal. We can't
1678 control which one, however. */
1682 if (debug_linux_nat)
1683 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp->status);
1684 if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status)))
1688 /* While there is a pending signal we would like to flush, continue
1689 the inferior and collect another signal. But if there's already
1690 a saved status that we don't want to flush, we can't resume the
1691 inferior - if it stopped for some other reason we wouldn't have
1692 anywhere to save the new status. In that case, we must leave the
1693 signal unflushed (and possibly generate an extra SIGINT stop).
1694 That's much less bad than losing a signal. */
1695 while (lp->status == 0
1696 && linux_nat_has_pending (GET_LWP (lp->ptid), &pending, flush_mask))
1701 ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1702 if (debug_linux_nat)
1703 fprintf_unfiltered (gdb_stderr,
1704 "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno);
1707 stop_wait_callback (lp, flush_mask);
1708 if (debug_linux_nat)
1709 fprintf_unfiltered (gdb_stderr,
1710 "FC: Wait finished; saved status is %d\n",
1717 /* Return non-zero if LP has a wait status pending. */
1720 status_callback (struct lwp_info *lp, void *data)
1722 /* Only report a pending wait status if we pretend that this has
1723 indeed been resumed. */
1724 return (lp->status != 0 && lp->resumed);
1727 /* Return non-zero if LP isn't stopped. */
1730 running_callback (struct lwp_info *lp, void *data)
1732 return (lp->stopped == 0 || (lp->status != 0 && lp->resumed));
1735 /* Count the LWP's that have had events. */
1738 count_events_callback (struct lwp_info *lp, void *data)
1742 gdb_assert (count != NULL);
1744 /* Count only LWPs that have a SIGTRAP event pending. */
1746 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1752 /* Select the LWP (if any) that is currently being single-stepped. */
1755 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
1757 if (lp->step && lp->status != 0)
1763 /* Select the Nth LWP that has had a SIGTRAP event. */
1766 select_event_lwp_callback (struct lwp_info *lp, void *data)
1768 int *selector = data;
1770 gdb_assert (selector != NULL);
1772 /* Select only LWPs that have a SIGTRAP event pending. */
1774 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1775 if ((*selector)-- == 0)
1782 cancel_breakpoints_callback (struct lwp_info *lp, void *data)
1784 struct lwp_info *event_lp = data;
1786 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1790 /* If a LWP other than the LWP that we're reporting an event for has
1791 hit a GDB breakpoint (as opposed to some random trap signal),
1792 then just arrange for it to hit it again later. We don't keep
1793 the SIGTRAP status and don't forward the SIGTRAP signal to the
1794 LWP. We will handle the current event, eventually we will resume
1795 all LWPs, and this one will get its breakpoint trap again.
1797 If we do not do this, then we run the risk that the user will
1798 delete or disable the breakpoint, but the LWP will have already
1802 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
1803 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
1804 gdbarch_decr_pc_after_break
1807 if (debug_linux_nat)
1808 fprintf_unfiltered (gdb_stdlog,
1809 "CBC: Push back breakpoint for %s\n",
1810 target_pid_to_str (lp->ptid));
1812 /* Back up the PC if necessary. */
1813 if (gdbarch_decr_pc_after_break (current_gdbarch))
1814 write_pc_pid (read_pc_pid (lp->ptid) - gdbarch_decr_pc_after_break
1818 /* Throw away the SIGTRAP. */
1825 /* Select one LWP out of those that have events pending. */
1828 select_event_lwp (struct lwp_info **orig_lp, int *status)
1831 int random_selector;
1832 struct lwp_info *event_lp;
1834 /* Record the wait status for the original LWP. */
1835 (*orig_lp)->status = *status;
1837 /* Give preference to any LWP that is being single-stepped. */
1838 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
1839 if (event_lp != NULL)
1841 if (debug_linux_nat)
1842 fprintf_unfiltered (gdb_stdlog,
1843 "SEL: Select single-step %s\n",
1844 target_pid_to_str (event_lp->ptid));
1848 /* No single-stepping LWP. Select one at random, out of those
1849 which have had SIGTRAP events. */
1851 /* First see how many SIGTRAP events we have. */
1852 iterate_over_lwps (count_events_callback, &num_events);
1854 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1855 random_selector = (int)
1856 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
1858 if (debug_linux_nat && num_events > 1)
1859 fprintf_unfiltered (gdb_stdlog,
1860 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1861 num_events, random_selector);
1863 event_lp = iterate_over_lwps (select_event_lwp_callback,
1867 if (event_lp != NULL)
1869 /* Switch the event LWP. */
1870 *orig_lp = event_lp;
1871 *status = event_lp->status;
1874 /* Flush the wait status for the event LWP. */
1875 (*orig_lp)->status = 0;
1878 /* Return non-zero if LP has been resumed. */
1881 resumed_callback (struct lwp_info *lp, void *data)
1886 /* Stop an active thread, verify it still exists, then resume it. */
1889 stop_and_resume_callback (struct lwp_info *lp, void *data)
1891 struct lwp_info *ptr;
1893 if (!lp->stopped && !lp->signalled)
1895 stop_callback (lp, NULL);
1896 stop_wait_callback (lp, NULL);
1897 /* Resume if the lwp still exists. */
1898 for (ptr = lwp_list; ptr; ptr = ptr->next)
1901 resume_callback (lp, NULL);
1902 resume_set_callback (lp, NULL);
1909 linux_nat_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1911 struct lwp_info *lp = NULL;
1914 pid_t pid = PIDGET (ptid);
1915 sigset_t flush_mask;
1917 /* The first time we get here after starting a new inferior, we may
1918 not have added it to the LWP list yet - this is the earliest
1919 moment at which we know its PID. */
1922 gdb_assert (!is_lwp (inferior_ptid));
1924 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1925 GET_PID (inferior_ptid));
1926 lp = add_lwp (inferior_ptid);
1930 sigemptyset (&flush_mask);
1932 /* Make sure SIGCHLD is blocked. */
1933 if (!sigismember (&blocked_mask, SIGCHLD))
1935 sigaddset (&blocked_mask, SIGCHLD);
1936 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1941 /* Make sure there is at least one LWP that has been resumed. */
1942 gdb_assert (iterate_over_lwps (resumed_callback, NULL));
1944 /* First check if there is a LWP with a wait status pending. */
1947 /* Any LWP that's been resumed will do. */
1948 lp = iterate_over_lwps (status_callback, NULL);
1951 status = lp->status;
1954 if (debug_linux_nat && status)
1955 fprintf_unfiltered (gdb_stdlog,
1956 "LLW: Using pending wait status %s for %s.\n",
1957 status_to_str (status),
1958 target_pid_to_str (lp->ptid));
1961 /* But if we don't fine one, we'll have to wait, and check both
1962 cloned and uncloned processes. We start with the cloned
1964 options = __WCLONE | WNOHANG;
1966 else if (is_lwp (ptid))
1968 if (debug_linux_nat)
1969 fprintf_unfiltered (gdb_stdlog,
1970 "LLW: Waiting for specific LWP %s.\n",
1971 target_pid_to_str (ptid));
1973 /* We have a specific LWP to check. */
1974 lp = find_lwp_pid (ptid);
1976 status = lp->status;
1979 if (debug_linux_nat && status)
1980 fprintf_unfiltered (gdb_stdlog,
1981 "LLW: Using pending wait status %s for %s.\n",
1982 status_to_str (status),
1983 target_pid_to_str (lp->ptid));
1985 /* If we have to wait, take into account whether PID is a cloned
1986 process or not. And we have to convert it to something that
1987 the layer beneath us can understand. */
1988 options = lp->cloned ? __WCLONE : 0;
1989 pid = GET_LWP (ptid);
1992 if (status && lp->signalled)
1994 /* A pending SIGSTOP may interfere with the normal stream of
1995 events. In a typical case where interference is a problem,
1996 we have a SIGSTOP signal pending for LWP A while
1997 single-stepping it, encounter an event in LWP B, and take the
1998 pending SIGSTOP while trying to stop LWP A. After processing
1999 the event in LWP B, LWP A is continued, and we'll never see
2000 the SIGTRAP associated with the last time we were
2001 single-stepping LWP A. */
2003 /* Resume the thread. It should halt immediately returning the
2005 registers_changed ();
2006 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
2007 lp->step, TARGET_SIGNAL_0);
2008 if (debug_linux_nat)
2009 fprintf_unfiltered (gdb_stdlog,
2010 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2011 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2012 target_pid_to_str (lp->ptid));
2014 gdb_assert (lp->resumed);
2016 /* This should catch the pending SIGSTOP. */
2017 stop_wait_callback (lp, NULL);
2020 set_sigint_trap (); /* Causes SIGINT to be passed on to the
2021 attached process. */
2028 lwpid = my_waitpid (pid, &status, options);
2031 gdb_assert (pid == -1 || lwpid == pid);
2033 if (debug_linux_nat)
2035 fprintf_unfiltered (gdb_stdlog,
2036 "LLW: waitpid %ld received %s\n",
2037 (long) lwpid, status_to_str (status));
2040 lp = find_lwp_pid (pid_to_ptid (lwpid));
2042 /* Check for stop events reported by a process we didn't
2043 already know about - anything not already in our LWP
2046 If we're expecting to receive stopped processes after
2047 fork, vfork, and clone events, then we'll just add the
2048 new one to our list and go back to waiting for the event
2049 to be reported - the stopped process might be returned
2050 from waitpid before or after the event is. */
2051 if (WIFSTOPPED (status) && !lp)
2053 linux_record_stopped_pid (lwpid, status);
2058 /* Make sure we don't report an event for the exit of an LWP not in
2059 our list, i.e. not part of the current process. This can happen
2060 if we detach from a program we original forked and then it
2062 if (!WIFSTOPPED (status) && !lp)
2068 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2069 CLONE_PTRACE processes which do not use the thread library -
2070 otherwise we wouldn't find the new LWP this way. That doesn't
2071 currently work, and the following code is currently unreachable
2072 due to the two blocks above. If it's fixed some day, this code
2073 should be broken out into a function so that we can also pick up
2074 LWPs from the new interface. */
2077 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
2078 if (options & __WCLONE)
2081 gdb_assert (WIFSTOPPED (status)
2082 && WSTOPSIG (status) == SIGSTOP);
2085 if (!in_thread_list (inferior_ptid))
2087 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
2088 GET_PID (inferior_ptid));
2089 add_thread (inferior_ptid);
2092 add_thread (lp->ptid);
2093 printf_unfiltered (_("[New %s]\n"),
2094 target_pid_to_str (lp->ptid));
2097 /* Save the trap's siginfo in case we need it later. */
2098 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
2101 /* Handle GNU/Linux's extended waitstatus for trace events. */
2102 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
2104 if (debug_linux_nat)
2105 fprintf_unfiltered (gdb_stdlog,
2106 "LLW: Handling extended status 0x%06x\n",
2108 if (linux_handle_extended_wait (lp, status, 0))
2115 /* Check if the thread has exited. */
2116 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
2118 /* If this is the main thread, we must stop all threads and
2119 verify if they are still alive. This is because in the nptl
2120 thread model, there is no signal issued for exiting LWPs
2121 other than the main thread. We only get the main thread
2122 exit signal once all child threads have already exited.
2123 If we stop all the threads and use the stop_wait_callback
2124 to check if they have exited we can determine whether this
2125 signal should be ignored or whether it means the end of the
2126 debugged application, regardless of which threading model
2128 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
2131 iterate_over_lwps (stop_and_resume_callback, NULL);
2134 if (debug_linux_nat)
2135 fprintf_unfiltered (gdb_stdlog,
2136 "LLW: %s exited.\n",
2137 target_pid_to_str (lp->ptid));
2141 /* If there is at least one more LWP, then the exit signal
2142 was not the end of the debugged application and should be
2146 /* Make sure there is at least one thread running. */
2147 gdb_assert (iterate_over_lwps (running_callback, NULL));
2149 /* Discard the event. */
2155 /* Check if the current LWP has previously exited. In the nptl
2156 thread model, LWPs other than the main thread do not issue
2157 signals when they exit so we must check whenever the thread
2158 has stopped. A similar check is made in stop_wait_callback(). */
2159 if (num_lwps > 1 && !linux_nat_thread_alive (lp->ptid))
2161 if (debug_linux_nat)
2162 fprintf_unfiltered (gdb_stdlog,
2163 "LLW: %s exited.\n",
2164 target_pid_to_str (lp->ptid));
2168 /* Make sure there is at least one thread running. */
2169 gdb_assert (iterate_over_lwps (running_callback, NULL));
2171 /* Discard the event. */
2176 /* Make sure we don't report a SIGSTOP that we sent
2177 ourselves in an attempt to stop an LWP. */
2179 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
2181 if (debug_linux_nat)
2182 fprintf_unfiltered (gdb_stdlog,
2183 "LLW: Delayed SIGSTOP caught for %s.\n",
2184 target_pid_to_str (lp->ptid));
2186 /* This is a delayed SIGSTOP. */
2189 registers_changed ();
2190 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
2191 lp->step, TARGET_SIGNAL_0);
2192 if (debug_linux_nat)
2193 fprintf_unfiltered (gdb_stdlog,
2194 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2196 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2197 target_pid_to_str (lp->ptid));
2200 gdb_assert (lp->resumed);
2202 /* Discard the event. */
2212 /* Alternate between checking cloned and uncloned processes. */
2213 options ^= __WCLONE;
2215 /* And suspend every time we have checked both. */
2216 if (options & __WCLONE)
2217 sigsuspend (&suspend_mask);
2220 /* We shouldn't end up here unless we want to try again. */
2221 gdb_assert (status == 0);
2224 clear_sigio_trap ();
2225 clear_sigint_trap ();
2229 /* Don't report signals that GDB isn't interested in, such as
2230 signals that are neither printed nor stopped upon. Stopping all
2231 threads can be a bit time-consuming so if we want decent
2232 performance with heavily multi-threaded programs, especially when
2233 they're using a high frequency timer, we'd better avoid it if we
2236 if (WIFSTOPPED (status))
2238 int signo = target_signal_from_host (WSTOPSIG (status));
2240 /* If we get a signal while single-stepping, we may need special
2241 care, e.g. to skip the signal handler. Defer to common code. */
2243 && signal_stop_state (signo) == 0
2244 && signal_print_state (signo) == 0
2245 && signal_pass_state (signo) == 1)
2247 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2248 here? It is not clear we should. GDB may not expect
2249 other threads to run. On the other hand, not resuming
2250 newly attached threads may cause an unwanted delay in
2251 getting them running. */
2252 registers_changed ();
2253 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
2255 if (debug_linux_nat)
2256 fprintf_unfiltered (gdb_stdlog,
2257 "LLW: %s %s, %s (preempt 'handle')\n",
2259 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2260 target_pid_to_str (lp->ptid),
2261 signo ? strsignal (signo) : "0");
2267 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
2269 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2270 forwarded to the entire process group, that is, all LWP's
2271 will receive it. Since we only want to report it once,
2272 we try to flush it from all LWPs except this one. */
2273 sigaddset (&flush_mask, SIGINT);
2277 /* This LWP is stopped now. */
2280 if (debug_linux_nat)
2281 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
2282 status_to_str (status), target_pid_to_str (lp->ptid));
2284 /* Now stop all other LWP's ... */
2285 iterate_over_lwps (stop_callback, NULL);
2287 /* ... and wait until all of them have reported back that they're no
2289 iterate_over_lwps (stop_wait_callback, &flush_mask);
2290 iterate_over_lwps (flush_callback, &flush_mask);
2292 /* If we're not waiting for a specific LWP, choose an event LWP from
2293 among those that have had events. Giving equal priority to all
2294 LWPs that have had events helps prevent starvation. */
2296 select_event_lwp (&lp, &status);
2298 /* Now that we've selected our final event LWP, cancel any
2299 breakpoints in other LWPs that have hit a GDB breakpoint. See
2300 the comment in cancel_breakpoints_callback to find out why. */
2301 iterate_over_lwps (cancel_breakpoints_callback, lp);
2303 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
2305 trap_ptid = lp->ptid;
2306 if (debug_linux_nat)
2307 fprintf_unfiltered (gdb_stdlog,
2308 "LLW: trap_ptid is %s.\n",
2309 target_pid_to_str (trap_ptid));
2312 trap_ptid = null_ptid;
2314 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2316 *ourstatus = lp->waitstatus;
2317 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
2320 store_waitstatus (ourstatus, status);
2326 kill_callback (struct lwp_info *lp, void *data)
2329 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
2330 if (debug_linux_nat)
2331 fprintf_unfiltered (gdb_stdlog,
2332 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2333 target_pid_to_str (lp->ptid),
2334 errno ? safe_strerror (errno) : "OK");
2340 kill_wait_callback (struct lwp_info *lp, void *data)
2344 /* We must make sure that there are no pending events (delayed
2345 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2346 program doesn't interfere with any following debugging session. */
2348 /* For cloned processes we must check both with __WCLONE and
2349 without, since the exit status of a cloned process isn't reported
2355 pid = my_waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
2356 if (pid != (pid_t) -1 && debug_linux_nat)
2358 fprintf_unfiltered (gdb_stdlog,
2359 "KWC: wait %s received unknown.\n",
2360 target_pid_to_str (lp->ptid));
2363 while (pid == GET_LWP (lp->ptid));
2365 gdb_assert (pid == -1 && errno == ECHILD);
2370 pid = my_waitpid (GET_LWP (lp->ptid), NULL, 0);
2371 if (pid != (pid_t) -1 && debug_linux_nat)
2373 fprintf_unfiltered (gdb_stdlog,
2374 "KWC: wait %s received unk.\n",
2375 target_pid_to_str (lp->ptid));
2378 while (pid == GET_LWP (lp->ptid));
2380 gdb_assert (pid == -1 && errno == ECHILD);
2385 linux_nat_kill (void)
2387 struct target_waitstatus last;
2391 /* If we're stopped while forking and we haven't followed yet,
2392 kill the other task. We need to do this first because the
2393 parent will be sleeping if this is a vfork. */
2395 get_last_target_status (&last_ptid, &last);
2397 if (last.kind == TARGET_WAITKIND_FORKED
2398 || last.kind == TARGET_WAITKIND_VFORKED)
2400 ptrace (PT_KILL, last.value.related_pid, 0, 0);
2404 if (forks_exist_p ())
2405 linux_fork_killall ();
2408 /* Kill all LWP's ... */
2409 iterate_over_lwps (kill_callback, NULL);
2411 /* ... and wait until we've flushed all events. */
2412 iterate_over_lwps (kill_wait_callback, NULL);
2415 target_mourn_inferior ();
2419 linux_nat_mourn_inferior (void)
2421 trap_ptid = null_ptid;
2423 /* Destroy LWP info; it's no longer valid. */
2426 /* Restore the original signal mask. */
2427 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
2428 sigemptyset (&blocked_mask);
2430 if (! forks_exist_p ())
2431 /* Normal case, no other forks available. */
2432 linux_ops->to_mourn_inferior ();
2434 /* Multi-fork case. The current inferior_ptid has exited, but
2435 there are other viable forks to debug. Delete the exiting
2436 one and context-switch to the first available. */
2437 linux_fork_mourn_inferior ();
2441 linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
2442 const char *annex, gdb_byte *readbuf,
2443 const gdb_byte *writebuf,
2444 ULONGEST offset, LONGEST len)
2446 struct cleanup *old_chain = save_inferior_ptid ();
2449 if (is_lwp (inferior_ptid))
2450 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
2452 xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
2455 do_cleanups (old_chain);
2460 linux_nat_thread_alive (ptid_t ptid)
2462 gdb_assert (is_lwp (ptid));
2465 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
2466 if (debug_linux_nat)
2467 fprintf_unfiltered (gdb_stdlog,
2468 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2469 target_pid_to_str (ptid),
2470 errno ? safe_strerror (errno) : "OK");
2472 /* Not every Linux kernel implements PTRACE_PEEKUSER. But we can
2473 handle that case gracefully since ptrace will first do a lookup
2474 for the process based upon the passed-in pid. If that fails we
2475 will get either -ESRCH or -EPERM, otherwise the child exists and
2477 if (errno == ESRCH || errno == EPERM)
2484 linux_nat_pid_to_str (ptid_t ptid)
2486 static char buf[64];
2488 if (lwp_list && lwp_list->next && is_lwp (ptid))
2490 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
2494 return normal_pid_to_str (ptid);
2498 sigchld_handler (int signo)
2500 /* Do nothing. The only reason for this handler is that it allows
2501 us to use sigsuspend in linux_nat_wait above to wait for the
2502 arrival of a SIGCHLD. */
2505 /* Accepts an integer PID; Returns a string representing a file that
2506 can be opened to get the symbols for the child process. */
2509 linux_child_pid_to_exec_file (int pid)
2511 char *name1, *name2;
2513 name1 = xmalloc (MAXPATHLEN);
2514 name2 = xmalloc (MAXPATHLEN);
2515 make_cleanup (xfree, name1);
2516 make_cleanup (xfree, name2);
2517 memset (name2, 0, MAXPATHLEN);
2519 sprintf (name1, "/proc/%d/exe", pid);
2520 if (readlink (name1, name2, MAXPATHLEN) > 0)
2526 /* Service function for corefiles and info proc. */
2529 read_mapping (FILE *mapfile,
2534 char *device, long long *inode, char *filename)
2536 int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx",
2537 addr, endaddr, permissions, offset, device, inode);
2540 if (ret > 0 && ret != EOF)
2542 /* Eat everything up to EOL for the filename. This will prevent
2543 weird filenames (such as one with embedded whitespace) from
2544 confusing this code. It also makes this code more robust in
2545 respect to annotations the kernel may add after the filename.
2547 Note the filename is used for informational purposes
2549 ret += fscanf (mapfile, "%[^\n]\n", filename);
2552 return (ret != 0 && ret != EOF);
2555 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2556 regions in the inferior for a corefile. */
2559 linux_nat_find_memory_regions (int (*func) (CORE_ADDR,
2561 int, int, int, void *), void *obfd)
2563 long long pid = PIDGET (inferior_ptid);
2564 char mapsfilename[MAXPATHLEN];
2566 long long addr, endaddr, size, offset, inode;
2567 char permissions[8], device[8], filename[MAXPATHLEN];
2568 int read, write, exec;
2571 /* Compose the filename for the /proc memory map, and open it. */
2572 sprintf (mapsfilename, "/proc/%lld/maps", pid);
2573 if ((mapsfile = fopen (mapsfilename, "r")) == NULL)
2574 error (_("Could not open %s."), mapsfilename);
2577 fprintf_filtered (gdb_stdout,
2578 "Reading memory regions from %s\n", mapsfilename);
2580 /* Now iterate until end-of-file. */
2581 while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0],
2582 &offset, &device[0], &inode, &filename[0]))
2584 size = endaddr - addr;
2586 /* Get the segment's permissions. */
2587 read = (strchr (permissions, 'r') != 0);
2588 write = (strchr (permissions, 'w') != 0);
2589 exec = (strchr (permissions, 'x') != 0);
2593 fprintf_filtered (gdb_stdout,
2594 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2595 size, paddr_nz (addr),
2597 write ? 'w' : ' ', exec ? 'x' : ' ');
2599 fprintf_filtered (gdb_stdout, " for %s", filename);
2600 fprintf_filtered (gdb_stdout, "\n");
2603 /* Invoke the callback function to create the corefile
2605 func (addr, size, read, write, exec, obfd);
2611 /* Records the thread's register state for the corefile note
2615 linux_nat_do_thread_registers (bfd *obfd, ptid_t ptid,
2616 char *note_data, int *note_size)
2618 gdb_gregset_t gregs;
2619 gdb_fpregset_t fpregs;
2620 #ifdef FILL_FPXREGSET
2621 gdb_fpxregset_t fpxregs;
2623 unsigned long lwp = ptid_get_lwp (ptid);
2624 struct regcache *regcache = get_thread_regcache (ptid);
2625 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2626 const struct regset *regset;
2628 struct cleanup *old_chain;
2630 old_chain = save_inferior_ptid ();
2631 inferior_ptid = ptid;
2632 target_fetch_registers (regcache, -1);
2633 do_cleanups (old_chain);
2635 core_regset_p = gdbarch_regset_from_core_section_p (gdbarch);
2637 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg",
2638 sizeof (gregs))) != NULL
2639 && regset->collect_regset != NULL)
2640 regset->collect_regset (regset, regcache, -1,
2641 &gregs, sizeof (gregs));
2643 fill_gregset (regcache, &gregs, -1);
2645 note_data = (char *) elfcore_write_prstatus (obfd,
2649 stop_signal, &gregs);
2652 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg2",
2653 sizeof (fpregs))) != NULL
2654 && regset->collect_regset != NULL)
2655 regset->collect_regset (regset, regcache, -1,
2656 &fpregs, sizeof (fpregs));
2658 fill_fpregset (regcache, &fpregs, -1);
2660 note_data = (char *) elfcore_write_prfpreg (obfd,
2663 &fpregs, sizeof (fpregs));
2665 #ifdef FILL_FPXREGSET
2667 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg-xfp",
2668 sizeof (fpxregs))) != NULL
2669 && regset->collect_regset != NULL)
2670 regset->collect_regset (regset, regcache, -1,
2671 &fpxregs, sizeof (fpxregs));
2673 fill_fpxregset (regcache, &fpxregs, -1);
2675 note_data = (char *) elfcore_write_prxfpreg (obfd,
2678 &fpxregs, sizeof (fpxregs));
2683 struct linux_nat_corefile_thread_data
2691 /* Called by gdbthread.c once per thread. Records the thread's
2692 register state for the corefile note section. */
2695 linux_nat_corefile_thread_callback (struct lwp_info *ti, void *data)
2697 struct linux_nat_corefile_thread_data *args = data;
2699 args->note_data = linux_nat_do_thread_registers (args->obfd,
2708 /* Records the register state for the corefile note section. */
2711 linux_nat_do_registers (bfd *obfd, ptid_t ptid,
2712 char *note_data, int *note_size)
2714 return linux_nat_do_thread_registers (obfd,
2715 ptid_build (ptid_get_pid (inferior_ptid),
2716 ptid_get_pid (inferior_ptid),
2718 note_data, note_size);
2721 /* Fills the "to_make_corefile_note" target vector. Builds the note
2722 section for a corefile, and returns it in a malloc buffer. */
2725 linux_nat_make_corefile_notes (bfd *obfd, int *note_size)
2727 struct linux_nat_corefile_thread_data thread_args;
2728 struct cleanup *old_chain;
2729 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
2730 char fname[16] = { '\0' };
2731 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
2732 char psargs[80] = { '\0' };
2733 char *note_data = NULL;
2734 ptid_t current_ptid = inferior_ptid;
2738 if (get_exec_file (0))
2740 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
2741 strncpy (psargs, get_exec_file (0), sizeof (psargs));
2742 if (get_inferior_args ())
2745 char *psargs_end = psargs + sizeof (psargs);
2747 /* linux_elfcore_write_prpsinfo () handles zero unterminated
2749 string_end = memchr (psargs, 0, sizeof (psargs));
2750 if (string_end != NULL)
2752 *string_end++ = ' ';
2753 strncpy (string_end, get_inferior_args (),
2754 psargs_end - string_end);
2757 note_data = (char *) elfcore_write_prpsinfo (obfd,
2759 note_size, fname, psargs);
2762 /* Dump information for threads. */
2763 thread_args.obfd = obfd;
2764 thread_args.note_data = note_data;
2765 thread_args.note_size = note_size;
2766 thread_args.num_notes = 0;
2767 iterate_over_lwps (linux_nat_corefile_thread_callback, &thread_args);
2768 if (thread_args.num_notes == 0)
2770 /* iterate_over_threads didn't come up with any threads; just
2771 use inferior_ptid. */
2772 note_data = linux_nat_do_registers (obfd, inferior_ptid,
2773 note_data, note_size);
2777 note_data = thread_args.note_data;
2780 auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV,
2784 note_data = elfcore_write_note (obfd, note_data, note_size,
2785 "CORE", NT_AUXV, auxv, auxv_len);
2789 make_cleanup (xfree, note_data);
2793 /* Implement the "info proc" command. */
2796 linux_nat_info_proc_cmd (char *args, int from_tty)
2798 long long pid = PIDGET (inferior_ptid);
2801 char buffer[MAXPATHLEN];
2802 char fname1[MAXPATHLEN], fname2[MAXPATHLEN];
2815 /* Break up 'args' into an argv array. */
2816 if ((argv = buildargv (args)) == NULL)
2819 make_cleanup_freeargv (argv);
2821 while (argv != NULL && *argv != NULL)
2823 if (isdigit (argv[0][0]))
2825 pid = strtoul (argv[0], NULL, 10);
2827 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
2831 else if (strcmp (argv[0], "status") == 0)
2835 else if (strcmp (argv[0], "stat") == 0)
2839 else if (strcmp (argv[0], "cmd") == 0)
2843 else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0)
2847 else if (strcmp (argv[0], "cwd") == 0)
2851 else if (strncmp (argv[0], "all", strlen (argv[0])) == 0)
2857 /* [...] (future options here) */
2862 error (_("No current process: you must name one."));
2864 sprintf (fname1, "/proc/%lld", pid);
2865 if (stat (fname1, &dummy) != 0)
2866 error (_("No /proc directory: '%s'"), fname1);
2868 printf_filtered (_("process %lld\n"), pid);
2869 if (cmdline_f || all)
2871 sprintf (fname1, "/proc/%lld/cmdline", pid);
2872 if ((procfile = fopen (fname1, "r")) != NULL)
2874 fgets (buffer, sizeof (buffer), procfile);
2875 printf_filtered ("cmdline = '%s'\n", buffer);
2879 warning (_("unable to open /proc file '%s'"), fname1);
2883 sprintf (fname1, "/proc/%lld/cwd", pid);
2884 memset (fname2, 0, sizeof (fname2));
2885 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
2886 printf_filtered ("cwd = '%s'\n", fname2);
2888 warning (_("unable to read link '%s'"), fname1);
2892 sprintf (fname1, "/proc/%lld/exe", pid);
2893 memset (fname2, 0, sizeof (fname2));
2894 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
2895 printf_filtered ("exe = '%s'\n", fname2);
2897 warning (_("unable to read link '%s'"), fname1);
2899 if (mappings_f || all)
2901 sprintf (fname1, "/proc/%lld/maps", pid);
2902 if ((procfile = fopen (fname1, "r")) != NULL)
2904 long long addr, endaddr, size, offset, inode;
2905 char permissions[8], device[8], filename[MAXPATHLEN];
2907 printf_filtered (_("Mapped address spaces:\n\n"));
2908 if (gdbarch_addr_bit (current_gdbarch) == 32)
2910 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2913 " Size", " Offset", "objfile");
2917 printf_filtered (" %18s %18s %10s %10s %7s\n",
2920 " Size", " Offset", "objfile");
2923 while (read_mapping (procfile, &addr, &endaddr, &permissions[0],
2924 &offset, &device[0], &inode, &filename[0]))
2926 size = endaddr - addr;
2928 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2929 calls here (and possibly above) should be abstracted
2930 out into their own functions? Andrew suggests using
2931 a generic local_address_string instead to print out
2932 the addresses; that makes sense to me, too. */
2934 if (gdbarch_addr_bit (current_gdbarch) == 32)
2936 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2937 (unsigned long) addr, /* FIXME: pr_addr */
2938 (unsigned long) endaddr,
2940 (unsigned int) offset,
2941 filename[0] ? filename : "");
2945 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2946 (unsigned long) addr, /* FIXME: pr_addr */
2947 (unsigned long) endaddr,
2949 (unsigned int) offset,
2950 filename[0] ? filename : "");
2957 warning (_("unable to open /proc file '%s'"), fname1);
2959 if (status_f || all)
2961 sprintf (fname1, "/proc/%lld/status", pid);
2962 if ((procfile = fopen (fname1, "r")) != NULL)
2964 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
2965 puts_filtered (buffer);
2969 warning (_("unable to open /proc file '%s'"), fname1);
2973 sprintf (fname1, "/proc/%lld/stat", pid);
2974 if ((procfile = fopen (fname1, "r")) != NULL)
2980 if (fscanf (procfile, "%d ", &itmp) > 0)
2981 printf_filtered (_("Process: %d\n"), itmp);
2982 if (fscanf (procfile, "(%[^)]) ", &buffer[0]) > 0)
2983 printf_filtered (_("Exec file: %s\n"), buffer);
2984 if (fscanf (procfile, "%c ", &ctmp) > 0)
2985 printf_filtered (_("State: %c\n"), ctmp);
2986 if (fscanf (procfile, "%d ", &itmp) > 0)
2987 printf_filtered (_("Parent process: %d\n"), itmp);
2988 if (fscanf (procfile, "%d ", &itmp) > 0)
2989 printf_filtered (_("Process group: %d\n"), itmp);
2990 if (fscanf (procfile, "%d ", &itmp) > 0)
2991 printf_filtered (_("Session id: %d\n"), itmp);
2992 if (fscanf (procfile, "%d ", &itmp) > 0)
2993 printf_filtered (_("TTY: %d\n"), itmp);
2994 if (fscanf (procfile, "%d ", &itmp) > 0)
2995 printf_filtered (_("TTY owner process group: %d\n"), itmp);
2996 if (fscanf (procfile, "%lu ", <mp) > 0)
2997 printf_filtered (_("Flags: 0x%lx\n"), ltmp);
2998 if (fscanf (procfile, "%lu ", <mp) > 0)
2999 printf_filtered (_("Minor faults (no memory page): %lu\n"),
3000 (unsigned long) ltmp);
3001 if (fscanf (procfile, "%lu ", <mp) > 0)
3002 printf_filtered (_("Minor faults, children: %lu\n"),
3003 (unsigned long) ltmp);
3004 if (fscanf (procfile, "%lu ", <mp) > 0)
3005 printf_filtered (_("Major faults (memory page faults): %lu\n"),
3006 (unsigned long) ltmp);
3007 if (fscanf (procfile, "%lu ", <mp) > 0)
3008 printf_filtered (_("Major faults, children: %lu\n"),
3009 (unsigned long) ltmp);
3010 if (fscanf (procfile, "%ld ", <mp) > 0)
3011 printf_filtered (_("utime: %ld\n"), ltmp);
3012 if (fscanf (procfile, "%ld ", <mp) > 0)
3013 printf_filtered (_("stime: %ld\n"), ltmp);
3014 if (fscanf (procfile, "%ld ", <mp) > 0)
3015 printf_filtered (_("utime, children: %ld\n"), ltmp);
3016 if (fscanf (procfile, "%ld ", <mp) > 0)
3017 printf_filtered (_("stime, children: %ld\n"), ltmp);
3018 if (fscanf (procfile, "%ld ", <mp) > 0)
3019 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
3021 if (fscanf (procfile, "%ld ", <mp) > 0)
3022 printf_filtered (_("'nice' value: %ld\n"), ltmp);
3023 if (fscanf (procfile, "%lu ", <mp) > 0)
3024 printf_filtered (_("jiffies until next timeout: %lu\n"),
3025 (unsigned long) ltmp);
3026 if (fscanf (procfile, "%lu ", <mp) > 0)
3027 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
3028 (unsigned long) ltmp);
3029 if (fscanf (procfile, "%ld ", <mp) > 0)
3030 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
3032 if (fscanf (procfile, "%lu ", <mp) > 0)
3033 printf_filtered (_("Virtual memory size: %lu\n"),
3034 (unsigned long) ltmp);
3035 if (fscanf (procfile, "%lu ", <mp) > 0)
3036 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp);
3037 if (fscanf (procfile, "%lu ", <mp) > 0)
3038 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp);
3039 if (fscanf (procfile, "%lu ", <mp) > 0)
3040 printf_filtered (_("Start of text: 0x%lx\n"), ltmp);
3041 if (fscanf (procfile, "%lu ", <mp) > 0)
3042 printf_filtered (_("End of text: 0x%lx\n"), ltmp);
3043 if (fscanf (procfile, "%lu ", <mp) > 0)
3044 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp);
3045 #if 0 /* Don't know how architecture-dependent the rest is...
3046 Anyway the signal bitmap info is available from "status". */
3047 if (fscanf (procfile, "%lu ", <mp) > 0) /* FIXME arch? */
3048 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp);
3049 if (fscanf (procfile, "%lu ", <mp) > 0) /* FIXME arch? */
3050 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp);
3051 if (fscanf (procfile, "%ld ", <mp) > 0)
3052 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp);
3053 if (fscanf (procfile, "%ld ", <mp) > 0)
3054 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp);
3055 if (fscanf (procfile, "%ld ", <mp) > 0)
3056 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp);
3057 if (fscanf (procfile, "%ld ", <mp) > 0)
3058 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp);
3059 if (fscanf (procfile, "%lu ", <mp) > 0) /* FIXME arch? */
3060 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp);
3065 warning (_("unable to open /proc file '%s'"), fname1);
3069 /* Implement the to_xfer_partial interface for memory reads using the /proc
3070 filesystem. Because we can use a single read() call for /proc, this
3071 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3072 but it doesn't support writes. */
3075 linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
3076 const char *annex, gdb_byte *readbuf,
3077 const gdb_byte *writebuf,
3078 ULONGEST offset, LONGEST len)
3084 if (object != TARGET_OBJECT_MEMORY || !readbuf)
3087 /* Don't bother for one word. */
3088 if (len < 3 * sizeof (long))
3091 /* We could keep this file open and cache it - possibly one per
3092 thread. That requires some juggling, but is even faster. */
3093 sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid));
3094 fd = open (filename, O_RDONLY | O_LARGEFILE);
3098 /* If pread64 is available, use it. It's faster if the kernel
3099 supports it (only one syscall), and it's 64-bit safe even on
3100 32-bit platforms (for instance, SPARC debugging a SPARC64
3103 if (pread64 (fd, readbuf, len, offset) != len)
3105 if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
3115 /* Parse LINE as a signal set and add its set bits to SIGS. */
3118 add_line_to_sigset (const char *line, sigset_t *sigs)
3120 int len = strlen (line) - 1;
3124 if (line[len] != '\n')
3125 error (_("Could not parse signal set: %s"), line);
3133 if (*p >= '0' && *p <= '9')
3135 else if (*p >= 'a' && *p <= 'f')
3136 digit = *p - 'a' + 10;
3138 error (_("Could not parse signal set: %s"), line);
3143 sigaddset (sigs, signum + 1);
3145 sigaddset (sigs, signum + 2);
3147 sigaddset (sigs, signum + 3);
3149 sigaddset (sigs, signum + 4);
3155 /* Find process PID's pending signals from /proc/pid/status and set
3159 linux_proc_pending_signals (int pid, sigset_t *pending, sigset_t *blocked, sigset_t *ignored)
3162 char buffer[MAXPATHLEN], fname[MAXPATHLEN];
3165 sigemptyset (pending);
3166 sigemptyset (blocked);
3167 sigemptyset (ignored);
3168 sprintf (fname, "/proc/%d/status", pid);
3169 procfile = fopen (fname, "r");
3170 if (procfile == NULL)
3171 error (_("Could not open %s"), fname);
3173 while (fgets (buffer, MAXPATHLEN, procfile) != NULL)
3175 /* Normal queued signals are on the SigPnd line in the status
3176 file. However, 2.6 kernels also have a "shared" pending
3177 queue for delivering signals to a thread group, so check for
3180 Unfortunately some Red Hat kernels include the shared pending
3181 queue but not the ShdPnd status field. */
3183 if (strncmp (buffer, "SigPnd:\t", 8) == 0)
3184 add_line_to_sigset (buffer + 8, pending);
3185 else if (strncmp (buffer, "ShdPnd:\t", 8) == 0)
3186 add_line_to_sigset (buffer + 8, pending);
3187 else if (strncmp (buffer, "SigBlk:\t", 8) == 0)
3188 add_line_to_sigset (buffer + 8, blocked);
3189 else if (strncmp (buffer, "SigIgn:\t", 8) == 0)
3190 add_line_to_sigset (buffer + 8, ignored);
3197 linux_xfer_partial (struct target_ops *ops, enum target_object object,
3198 const char *annex, gdb_byte *readbuf,
3199 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
3203 if (object == TARGET_OBJECT_AUXV)
3204 return procfs_xfer_auxv (ops, object, annex, readbuf, writebuf,
3207 xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
3212 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
3216 /* Create a prototype generic GNU/Linux target. The client can override
3217 it with local methods. */
3220 linux_target_install_ops (struct target_ops *t)
3222 t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
3223 t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
3224 t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
3225 t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
3226 t->to_post_startup_inferior = linux_child_post_startup_inferior;
3227 t->to_post_attach = linux_child_post_attach;
3228 t->to_follow_fork = linux_child_follow_fork;
3229 t->to_find_memory_regions = linux_nat_find_memory_regions;
3230 t->to_make_corefile_notes = linux_nat_make_corefile_notes;
3232 super_xfer_partial = t->to_xfer_partial;
3233 t->to_xfer_partial = linux_xfer_partial;
3239 struct target_ops *t;
3241 t = inf_ptrace_target ();
3242 linux_target_install_ops (t);
3248 linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
3250 struct target_ops *t;
3252 t = inf_ptrace_trad_target (register_u_offset);
3253 linux_target_install_ops (t);
3259 linux_nat_add_target (struct target_ops *t)
3261 /* Save the provided single-threaded target. We save this in a separate
3262 variable because another target we've inherited from (e.g. inf-ptrace)
3263 may have saved a pointer to T; we want to use it for the final
3264 process stratum target. */
3265 linux_ops_saved = *t;
3266 linux_ops = &linux_ops_saved;
3268 /* Override some methods for multithreading. */
3269 t->to_attach = linux_nat_attach;
3270 t->to_detach = linux_nat_detach;
3271 t->to_resume = linux_nat_resume;
3272 t->to_wait = linux_nat_wait;
3273 t->to_xfer_partial = linux_nat_xfer_partial;
3274 t->to_kill = linux_nat_kill;
3275 t->to_mourn_inferior = linux_nat_mourn_inferior;
3276 t->to_thread_alive = linux_nat_thread_alive;
3277 t->to_pid_to_str = linux_nat_pid_to_str;
3278 t->to_has_thread_control = tc_schedlock;
3280 /* We don't change the stratum; this target will sit at
3281 process_stratum and thread_db will set at thread_stratum. This
3282 is a little strange, since this is a multi-threaded-capable
3283 target, but we want to be on the stack below thread_db, and we
3284 also want to be used for single-threaded processes. */
3288 /* TODO: Eliminate this and have libthread_db use
3289 find_target_beneath. */
3293 /* Register a method to call whenever a new thread is attached. */
3295 linux_nat_set_new_thread (struct target_ops *t, void (*new_thread) (ptid_t))
3297 /* Save the pointer. We only support a single registered instance
3298 of the GNU/Linux native target, so we do not need to map this to
3300 linux_nat_new_thread = new_thread;
3303 /* Return the saved siginfo associated with PTID. */
3305 linux_nat_get_siginfo (ptid_t ptid)
3307 struct lwp_info *lp = find_lwp_pid (ptid);
3309 gdb_assert (lp != NULL);
3311 return &lp->siginfo;
3315 _initialize_linux_nat (void)
3317 struct sigaction action;
3319 add_info ("proc", linux_nat_info_proc_cmd, _("\
3320 Show /proc process information about any running process.\n\
3321 Specify any process id, or use the program being debugged by default.\n\
3322 Specify any of the following keywords for detailed info:\n\
3323 mappings -- list of mapped memory regions.\n\
3324 stat -- list a bunch of random process info.\n\
3325 status -- list a different bunch of random process info.\n\
3326 all -- list all available /proc info."));
3328 /* Save the original signal mask. */
3329 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
3331 action.sa_handler = sigchld_handler;
3332 sigemptyset (&action.sa_mask);
3333 action.sa_flags = SA_RESTART;
3334 sigaction (SIGCHLD, &action, NULL);
3336 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3337 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
3338 sigdelset (&suspend_mask, SIGCHLD);
3340 sigemptyset (&blocked_mask);
3342 add_setshow_zinteger_cmd ("lin-lwp", no_class, &debug_linux_nat, _("\
3343 Set debugging of GNU/Linux lwp module."), _("\
3344 Show debugging of GNU/Linux lwp module."), _("\
3345 Enables printf debugging output."),
3347 show_debug_linux_nat,
3348 &setdebuglist, &showdebuglist);
3352 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3353 the GNU/Linux Threads library and therefore doesn't really belong
3356 /* Read variable NAME in the target and return its value if found.
3357 Otherwise return zero. It is assumed that the type of the variable
3361 get_signo (const char *name)
3363 struct minimal_symbol *ms;
3366 ms = lookup_minimal_symbol (name, NULL, NULL);
3370 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (gdb_byte *) &signo,
3371 sizeof (signo)) != 0)
3377 /* Return the set of signals used by the threads library in *SET. */
3380 lin_thread_get_thread_signals (sigset_t *set)
3382 struct sigaction action;
3383 int restart, cancel;
3387 restart = get_signo ("__pthread_sig_restart");
3388 cancel = get_signo ("__pthread_sig_cancel");
3390 /* LinuxThreads normally uses the first two RT signals, but in some legacy
3391 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
3392 not provide any way for the debugger to query the signal numbers -
3393 fortunately they don't change! */
3396 restart = __SIGRTMIN;
3399 cancel = __SIGRTMIN + 1;
3401 sigaddset (set, restart);
3402 sigaddset (set, cancel);
3404 /* The GNU/Linux Threads library makes terminating threads send a
3405 special "cancel" signal instead of SIGCHLD. Make sure we catch
3406 those (to prevent them from terminating GDB itself, which is
3407 likely to be their default action) and treat them the same way as
3410 action.sa_handler = sigchld_handler;
3411 sigemptyset (&action.sa_mask);
3412 action.sa_flags = SA_RESTART;
3413 sigaction (cancel, &action, NULL);
3415 /* We block the "cancel" signal throughout this code ... */
3416 sigaddset (&blocked_mask, cancel);
3417 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
3419 /* ... except during a sigsuspend. */
3420 sigdelset (&suspend_mask, cancel);