1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "gdb_string.h"
27 #include "gdb_assert.h"
28 #ifdef HAVE_TKILL_SYSCALL
30 #include <sys/syscall.h>
32 #include <sys/ptrace.h>
33 #include "linux-nat.h"
34 #include "gdbthread.h"
37 #include <sys/param.h> /* for MAXPATHLEN */
38 #include <sys/procfs.h> /* for elf_gregset etc. */
39 #include "elf-bfd.h" /* for elfcore_write_* */
40 #include "gregset.h" /* for gregset */
41 #include "gdbcore.h" /* for get_exec_file */
42 #include <ctype.h> /* for isdigit */
43 #include "gdbthread.h" /* for struct thread_info etc. */
44 #include "gdb_stat.h" /* for struct stat */
45 #include <fcntl.h> /* for O_RDONLY */
51 /* If the system headers did not provide the constants, hard-code the normal
53 #ifndef PTRACE_EVENT_FORK
55 #define PTRACE_SETOPTIONS 0x4200
56 #define PTRACE_GETEVENTMSG 0x4201
58 /* options set using PTRACE_SETOPTIONS */
59 #define PTRACE_O_TRACESYSGOOD 0x00000001
60 #define PTRACE_O_TRACEFORK 0x00000002
61 #define PTRACE_O_TRACEVFORK 0x00000004
62 #define PTRACE_O_TRACECLONE 0x00000008
63 #define PTRACE_O_TRACEEXEC 0x00000010
64 #define PTRACE_O_TRACEVFORKDONE 0x00000020
65 #define PTRACE_O_TRACEEXIT 0x00000040
67 /* Wait extended result codes for the above trace options. */
68 #define PTRACE_EVENT_FORK 1
69 #define PTRACE_EVENT_VFORK 2
70 #define PTRACE_EVENT_CLONE 3
71 #define PTRACE_EVENT_EXEC 4
72 #define PTRACE_EVENT_VFORK_DONE 5
73 #define PTRACE_EVENT_EXIT 6
75 #endif /* PTRACE_EVENT_FORK */
77 /* We can't always assume that this flag is available, but all systems
78 with the ptrace event handlers also have __WALL, so it's safe to use
81 #define __WALL 0x40000000 /* Wait for any child. */
84 static int debug_linux_nat;
86 static int linux_parent_pid;
88 struct simple_pid_list
91 struct simple_pid_list *next;
93 struct simple_pid_list *stopped_pids;
95 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
96 can not be used, 1 if it can. */
98 static int linux_supports_tracefork_flag = -1;
100 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
101 PTRACE_O_TRACEVFORKDONE. */
103 static int linux_supports_tracevforkdone_flag = -1;
106 /* Trivial list manipulation functions to keep track of a list of
107 new stopped processes. */
109 add_to_pid_list (struct simple_pid_list **listp, int pid)
111 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
113 new_pid->next = *listp;
118 pull_pid_from_list (struct simple_pid_list **listp, int pid)
120 struct simple_pid_list **p;
122 for (p = listp; *p != NULL; p = &(*p)->next)
123 if ((*p)->pid == pid)
125 struct simple_pid_list *next = (*p)->next;
134 linux_record_stopped_pid (int pid)
136 add_to_pid_list (&stopped_pids, pid);
140 /* A helper function for linux_test_for_tracefork, called after fork (). */
143 linux_tracefork_child (void)
147 ptrace (PTRACE_TRACEME, 0, 0, 0);
148 kill (getpid (), SIGSTOP);
153 /* Wrapper function for waitpid which handles EINTR. */
156 my_waitpid (int pid, int *status, int flags)
161 ret = waitpid (pid, status, flags);
163 while (ret == -1 && errno == EINTR);
168 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
170 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
171 we know that the feature is not available. This may change the tracing
172 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
174 However, if it succeeds, we don't know for sure that the feature is
175 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
176 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
177 fork tracing, and let it fork. If the process exits, we assume that we
178 can't use TRACEFORK; if we get the fork notification, and we can extract
179 the new child's PID, then we assume that we can. */
182 linux_test_for_tracefork (int original_pid)
184 int child_pid, ret, status;
187 linux_supports_tracefork_flag = 0;
188 linux_supports_tracevforkdone_flag = 0;
190 ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK);
196 perror_with_name (("fork"));
199 linux_tracefork_child ();
201 ret = my_waitpid (child_pid, &status, 0);
203 perror_with_name (("waitpid"));
204 else if (ret != child_pid)
205 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret);
206 if (! WIFSTOPPED (status))
207 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status);
209 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
212 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
215 warning (_("linux_test_for_tracefork: failed to kill child"));
219 ret = my_waitpid (child_pid, &status, 0);
220 if (ret != child_pid)
221 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
222 else if (!WIFSIGNALED (status))
223 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
224 "killed child"), status);
229 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
230 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
231 PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
232 linux_supports_tracevforkdone_flag = (ret == 0);
234 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
236 warning (_("linux_test_for_tracefork: failed to resume child"));
238 ret = my_waitpid (child_pid, &status, 0);
240 if (ret == child_pid && WIFSTOPPED (status)
241 && status >> 16 == PTRACE_EVENT_FORK)
244 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
245 if (ret == 0 && second_pid != 0)
249 linux_supports_tracefork_flag = 1;
250 my_waitpid (second_pid, &second_status, 0);
251 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
253 warning (_("linux_test_for_tracefork: failed to kill second child"));
257 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
258 "(%d, status 0x%x)"), ret, status);
260 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
262 warning (_("linux_test_for_tracefork: failed to kill child"));
263 my_waitpid (child_pid, &status, 0);
266 /* Return non-zero iff we have tracefork functionality available.
267 This function also sets linux_supports_tracefork_flag. */
270 linux_supports_tracefork (int pid)
272 if (linux_supports_tracefork_flag == -1)
273 linux_test_for_tracefork (pid);
274 return linux_supports_tracefork_flag;
278 linux_supports_tracevforkdone (int pid)
280 if (linux_supports_tracefork_flag == -1)
281 linux_test_for_tracefork (pid);
282 return linux_supports_tracevforkdone_flag;
287 linux_enable_event_reporting (ptid_t ptid)
289 int pid = ptid_get_pid (ptid);
292 if (! linux_supports_tracefork (pid))
295 options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC
296 | PTRACE_O_TRACECLONE;
297 if (linux_supports_tracevforkdone (pid))
298 options |= PTRACE_O_TRACEVFORKDONE;
300 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
301 read-only process state. */
303 ptrace (PTRACE_SETOPTIONS, pid, 0, options);
307 child_post_attach (int pid)
309 linux_enable_event_reporting (pid_to_ptid (pid));
313 linux_child_post_startup_inferior (ptid_t ptid)
315 linux_enable_event_reporting (ptid);
318 #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
320 child_post_startup_inferior (ptid_t ptid)
322 linux_child_post_startup_inferior (ptid);
327 child_follow_fork (int follow_child)
330 struct target_waitstatus last_status;
332 int parent_pid, child_pid;
334 get_last_target_status (&last_ptid, &last_status);
335 has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
336 parent_pid = ptid_get_pid (last_ptid);
337 child_pid = last_status.value.related_pid;
341 /* We're already attached to the parent, by default. */
343 /* Before detaching from the child, remove all breakpoints from
344 it. (This won't actually modify the breakpoint list, but will
345 physically remove the breakpoints from the child.) */
346 /* If we vforked this will remove the breakpoints from the parent
347 also, but they'll be reinserted below. */
348 detach_breakpoints (child_pid);
350 fprintf_filtered (gdb_stdout,
351 "Detaching after fork from child process %d.\n",
354 ptrace (PTRACE_DETACH, child_pid, 0, 0);
358 gdb_assert (linux_supports_tracefork_flag >= 0);
359 if (linux_supports_tracevforkdone (0))
363 ptrace (PTRACE_CONT, parent_pid, 0, 0);
364 waitpid (parent_pid, &status, __WALL);
365 if ((status >> 16) != PTRACE_EVENT_VFORK_DONE)
366 warning (_("Unexpected waitpid result %06x when waiting for "
367 "vfork-done"), status);
371 /* We can't insert breakpoints until the child has
372 finished with the shared memory region. We need to
373 wait until that happens. Ideal would be to just
375 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
376 - waitpid (parent_pid, &status, __WALL);
377 However, most architectures can't handle a syscall
378 being traced on the way out if it wasn't traced on
381 We might also think to loop, continuing the child
382 until it exits or gets a SIGTRAP. One problem is
383 that the child might call ptrace with PTRACE_TRACEME.
385 There's no simple and reliable way to figure out when
386 the vforked child will be done with its copy of the
387 shared memory. We could step it out of the syscall,
388 two instructions, let it go, and then single-step the
389 parent once. When we have hardware single-step, this
390 would work; with software single-step it could still
391 be made to work but we'd have to be able to insert
392 single-step breakpoints in the child, and we'd have
393 to insert -just- the single-step breakpoint in the
394 parent. Very awkward.
396 In the end, the best we can do is to make sure it
397 runs for a little while. Hopefully it will be out of
398 range of any breakpoints we reinsert. Usually this
399 is only the single-step breakpoint at vfork's return
405 /* Since we vforked, breakpoints were removed in the parent
406 too. Put them back. */
407 reattach_breakpoints (parent_pid);
412 char child_pid_spelling[40];
414 /* Needed to keep the breakpoint lists in sync. */
416 detach_breakpoints (child_pid);
418 /* Before detaching from the parent, remove all breakpoints from it. */
419 remove_breakpoints ();
421 fprintf_filtered (gdb_stdout,
422 "Attaching after fork to child process %d.\n",
425 /* If we're vforking, we may want to hold on to the parent until
426 the child exits or execs. At exec time we can remove the old
427 breakpoints from the parent and detach it; at exit time we
428 could do the same (or even, sneakily, resume debugging it - the
429 child's exec has failed, or something similar).
431 This doesn't clean up "properly", because we can't call
432 target_detach, but that's OK; if the current target is "child",
433 then it doesn't need any further cleanups, and lin_lwp will
434 generally not encounter vfork (vfork is defined to fork
437 The holding part is very easy if we have VFORKDONE events;
438 but keeping track of both processes is beyond GDB at the
439 moment. So we don't expose the parent to the rest of GDB.
440 Instead we quietly hold onto it until such time as we can
444 linux_parent_pid = parent_pid;
446 target_detach (NULL, 0);
448 inferior_ptid = pid_to_ptid (child_pid);
449 push_target (&deprecated_child_ops);
451 /* Reset breakpoints in the child as appropriate. */
452 follow_inferior_reset_breakpoints ();
459 linux_handle_extended_wait (int pid, int status,
460 struct target_waitstatus *ourstatus)
462 int event = status >> 16;
464 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
465 || event == PTRACE_EVENT_CLONE)
467 unsigned long new_pid;
470 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
472 /* If we haven't already seen the new PID stop, wait for it now. */
473 if (! pull_pid_from_list (&stopped_pids, new_pid))
475 /* The new child has a pending SIGSTOP. We can't affect it until it
476 hits the SIGSTOP, but we're already attached. */
478 ret = waitpid (new_pid, &status,
479 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
480 } while (ret == -1 && errno == EINTR);
482 perror_with_name (_("waiting for new child"));
483 else if (ret != new_pid)
484 internal_error (__FILE__, __LINE__,
485 _("wait returned unexpected PID %d"), ret);
486 else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP)
487 internal_error (__FILE__, __LINE__,
488 _("wait returned unexpected status 0x%x"), status);
491 if (event == PTRACE_EVENT_FORK)
492 ourstatus->kind = TARGET_WAITKIND_FORKED;
493 else if (event == PTRACE_EVENT_VFORK)
494 ourstatus->kind = TARGET_WAITKIND_VFORKED;
496 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
498 ourstatus->value.related_pid = new_pid;
499 return inferior_ptid;
502 if (event == PTRACE_EVENT_EXEC)
504 ourstatus->kind = TARGET_WAITKIND_EXECD;
505 ourstatus->value.execd_pathname
506 = xstrdup (child_pid_to_exec_file (pid));
508 if (linux_parent_pid)
510 detach_breakpoints (linux_parent_pid);
511 ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0);
513 linux_parent_pid = 0;
516 return inferior_ptid;
519 internal_error (__FILE__, __LINE__,
520 _("unknown ptrace event %d"), event);
525 child_insert_fork_catchpoint (int pid)
527 if (! linux_supports_tracefork (pid))
528 error (_("Your system does not support fork catchpoints."));
532 child_insert_vfork_catchpoint (int pid)
534 if (!linux_supports_tracefork (pid))
535 error (_("Your system does not support vfork catchpoints."));
539 child_insert_exec_catchpoint (int pid)
541 if (!linux_supports_tracefork (pid))
542 error (_("Your system does not support exec catchpoints."));
549 int pid = PIDGET (inferior_ptid);
550 struct target_waitstatus last;
557 /* If we're stopped while forking and we haven't followed yet, kill the
558 other task. We need to do this first because the parent will be
559 sleeping if this is a vfork. */
561 get_last_target_status (&last_ptid, &last);
563 if (last.kind == TARGET_WAITKIND_FORKED
564 || last.kind == TARGET_WAITKIND_VFORKED)
566 ptrace (PT_KILL, last.value.related_pid, 0, 0);
570 /* Kill the current process. */
571 ptrace (PT_KILL, pid, 0, 0);
572 ret = wait (&status);
574 /* We might get a SIGCHLD instead of an exit status. This is
575 aggravated by the first kill above - a child has just died. */
577 while (ret == pid && WIFSTOPPED (status))
579 ptrace (PT_KILL, pid, 0, 0);
580 ret = wait (&status);
583 target_mourn_inferior ();
586 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
587 are processes sharing the same VM space. A multi-threaded process
588 is basically a group of such processes. However, such a grouping
589 is almost entirely a user-space issue; the kernel doesn't enforce
590 such a grouping at all (this might change in the future). In
591 general, we'll rely on the threads library (i.e. the GNU/Linux
592 Threads library) to provide such a grouping.
594 It is perfectly well possible to write a multi-threaded application
595 without the assistance of a threads library, by using the clone
596 system call directly. This module should be able to give some
597 rudimentary support for debugging such applications if developers
598 specify the CLONE_PTRACE flag in the clone system call, and are
599 using the Linux kernel 2.4 or above.
601 Note that there are some peculiarities in GNU/Linux that affect
604 - In general one should specify the __WCLONE flag to waitpid in
605 order to make it report events for any of the cloned processes
606 (and leave it out for the initial process). However, if a cloned
607 process has exited the exit status is only reported if the
608 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
609 we cannot use it since GDB must work on older systems too.
611 - When a traced, cloned process exits and is waited for by the
612 debugger, the kernel reassigns it to the original parent and
613 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
614 library doesn't notice this, which leads to the "zombie problem":
615 When debugged a multi-threaded process that spawns a lot of
616 threads will run out of processes, even if the threads exit,
617 because the "zombies" stay around. */
619 /* List of known LWPs. */
620 static struct lwp_info *lwp_list;
622 /* Number of LWPs in the list. */
625 /* Non-zero if we're running in "threaded" mode. */
629 #define GET_LWP(ptid) ptid_get_lwp (ptid)
630 #define GET_PID(ptid) ptid_get_pid (ptid)
631 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
632 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
634 /* If the last reported event was a SIGTRAP, this variable is set to
635 the process id of the LWP/thread that got it. */
639 /* This module's target-specific operations. */
640 static struct target_ops linux_nat_ops;
642 /* Since we cannot wait (in linux_nat_wait) for the initial process and
643 any cloned processes with a single call to waitpid, we have to use
644 the WNOHANG flag and call waitpid in a loop. To optimize
645 things a bit we use `sigsuspend' to wake us up when a process has
646 something to report (it will send us a SIGCHLD if it has). To make
647 this work we have to juggle with the signal mask. We save the
648 original signal mask such that we can restore it before creating a
649 new process in order to avoid blocking certain signals in the
650 inferior. We then block SIGCHLD during the waitpid/sigsuspend
653 /* Original signal mask. */
654 static sigset_t normal_mask;
656 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
657 _initialize_linux_nat. */
658 static sigset_t suspend_mask;
660 /* Signals to block to make that sigsuspend work. */
661 static sigset_t blocked_mask;
664 /* Prototypes for local functions. */
665 static int stop_wait_callback (struct lwp_info *lp, void *data);
666 static int linux_nat_thread_alive (ptid_t ptid);
668 /* Convert wait status STATUS to a string. Used for printing debug
672 status_to_str (int status)
676 if (WIFSTOPPED (status))
677 snprintf (buf, sizeof (buf), "%s (stopped)",
678 strsignal (WSTOPSIG (status)));
679 else if (WIFSIGNALED (status))
680 snprintf (buf, sizeof (buf), "%s (terminated)",
681 strsignal (WSTOPSIG (status)));
683 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
688 /* Initialize the list of LWPs. Note that this module, contrary to
689 what GDB's generic threads layer does for its thread list,
690 re-initializes the LWP lists whenever we mourn or detach (which
691 doesn't involve mourning) the inferior. */
696 struct lwp_info *lp, *lpnext;
698 for (lp = lwp_list; lp; lp = lpnext)
709 /* Add the LWP specified by PID to the list. If this causes the
710 number of LWPs to become larger than one, go into "threaded" mode.
711 Return a pointer to the structure describing the new LWP. */
713 static struct lwp_info *
714 add_lwp (ptid_t ptid)
718 gdb_assert (is_lwp (ptid));
720 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
722 memset (lp, 0, sizeof (struct lwp_info));
724 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
736 /* Remove the LWP specified by PID from the list. */
739 delete_lwp (ptid_t ptid)
741 struct lwp_info *lp, *lpprev;
745 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
746 if (ptid_equal (lp->ptid, ptid))
752 /* We don't go back to "non-threaded" mode if the number of threads
753 becomes less than two. */
757 lpprev->next = lp->next;
764 /* Return a pointer to the structure describing the LWP corresponding
765 to PID. If no corresponding LWP could be found, return NULL. */
767 static struct lwp_info *
768 find_lwp_pid (ptid_t ptid)
774 lwp = GET_LWP (ptid);
776 lwp = GET_PID (ptid);
778 for (lp = lwp_list; lp; lp = lp->next)
779 if (lwp == GET_LWP (lp->ptid))
785 /* Call CALLBACK with its second argument set to DATA for every LWP in
786 the list. If CALLBACK returns 1 for a particular LWP, return a
787 pointer to the structure describing that LWP immediately.
788 Otherwise return NULL. */
791 iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
793 struct lwp_info *lp, *lpnext;
795 for (lp = lwp_list; lp; lp = lpnext)
798 if ((*callback) (lp, data))
805 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
806 a message telling the user that a new LWP has been added to the
810 lin_lwp_attach_lwp (ptid_t ptid, int verbose)
812 struct lwp_info *lp, *found_lp;
814 gdb_assert (is_lwp (ptid));
816 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
817 to interrupt either the ptrace() or waitpid() calls below. */
818 if (!sigismember (&blocked_mask, SIGCHLD))
820 sigaddset (&blocked_mask, SIGCHLD);
821 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
825 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid));
827 found_lp = lp = find_lwp_pid (ptid);
831 /* We assume that we're already attached to any LWP that has an id
832 equal to the overall process id, and to any LWP that is already
833 in our list of LWPs. If we're not seeing exit events from threads
834 and we've had PID wraparound since we last tried to stop all threads,
835 this assumption might be wrong; fortunately, this is very unlikely
837 if (GET_LWP (ptid) != GET_PID (ptid) && found_lp == NULL)
842 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
843 error (_("Can't attach %s: %s"), target_pid_to_str (ptid),
844 safe_strerror (errno));
847 fprintf_unfiltered (gdb_stdlog,
848 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
849 target_pid_to_str (ptid));
851 pid = waitpid (GET_LWP (ptid), &status, 0);
852 if (pid == -1 && errno == ECHILD)
854 /* Try again with __WCLONE to check cloned processes. */
855 pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
859 gdb_assert (pid == GET_LWP (ptid)
860 && WIFSTOPPED (status) && WSTOPSIG (status));
862 child_post_attach (pid);
868 fprintf_unfiltered (gdb_stdlog,
869 "LLAL: waitpid %s received %s\n",
870 target_pid_to_str (ptid),
871 status_to_str (status));
876 /* We assume that the LWP representing the original process is
877 already stopped. Mark it as stopped in the data structure
878 that the linux ptrace layer uses to keep track of threads.
879 Note that this won't have already been done since the main
880 thread will have, we assume, been stopped by an attach from a
887 linux_nat_attach (char *args, int from_tty)
893 /* FIXME: We should probably accept a list of process id's, and
894 attach all of them. */
895 deprecated_child_ops.to_attach (args, from_tty);
897 /* Add the initial process as the first LWP to the list. */
898 lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
900 /* Make sure the initial process is stopped. The user-level threads
901 layer might want to poke around in the inferior, and that won't
902 work if things haven't stabilized yet. */
903 pid = waitpid (GET_PID (inferior_ptid), &status, 0);
904 if (pid == -1 && errno == ECHILD)
906 warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid));
908 /* Try again with __WCLONE to check cloned processes. */
909 pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
913 gdb_assert (pid == GET_PID (inferior_ptid)
914 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
918 /* Fake the SIGSTOP that core GDB expects. */
919 lp->status = W_STOPCODE (SIGSTOP);
923 fprintf_unfiltered (gdb_stdlog,
924 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid);
929 detach_callback (struct lwp_info *lp, void *data)
931 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
933 if (debug_linux_nat && lp->status)
934 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
935 strsignal (WSTOPSIG (lp->status)),
936 target_pid_to_str (lp->ptid));
938 while (lp->signalled && lp->stopped)
941 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
942 WSTOPSIG (lp->status)) < 0)
943 error (_("Can't continue %s: %s"), target_pid_to_str (lp->ptid),
944 safe_strerror (errno));
947 fprintf_unfiltered (gdb_stdlog,
948 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
949 target_pid_to_str (lp->ptid),
950 status_to_str (lp->status));
955 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
956 here. But since lp->signalled was cleared above,
957 stop_wait_callback didn't do anything; the process was left
958 running. Shouldn't we be waiting for it to stop?
959 I've removed the call, since stop_wait_callback now does do
960 something when called with lp->signalled == 0. */
962 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
965 /* We don't actually detach from the LWP that has an id equal to the
966 overall process id just yet. */
967 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
970 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
971 WSTOPSIG (lp->status)) < 0)
972 error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
973 safe_strerror (errno));
976 fprintf_unfiltered (gdb_stdlog,
977 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
978 target_pid_to_str (lp->ptid),
979 strsignal (WSTOPSIG (lp->status)));
981 delete_lwp (lp->ptid);
988 linux_nat_detach (char *args, int from_tty)
990 iterate_over_lwps (detach_callback, NULL);
992 /* Only the initial process should be left right now. */
993 gdb_assert (num_lwps == 1);
995 trap_ptid = null_ptid;
997 /* Destroy LWP info; it's no longer valid. */
1000 /* Restore the original signal mask. */
1001 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1002 sigemptyset (&blocked_mask);
1004 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
1005 deprecated_child_ops.to_detach (args, from_tty);
1011 resume_callback (struct lwp_info *lp, void *data)
1013 if (lp->stopped && lp->status == 0)
1015 struct thread_info *tp;
1017 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
1018 if (debug_linux_nat)
1019 fprintf_unfiltered (gdb_stdlog,
1020 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1021 target_pid_to_str (lp->ptid));
1030 resume_clear_callback (struct lwp_info *lp, void *data)
1037 resume_set_callback (struct lwp_info *lp, void *data)
1044 linux_nat_resume (ptid_t ptid, int step, enum target_signal signo)
1046 struct lwp_info *lp;
1049 /* A specific PTID means `step only this process id'. */
1050 resume_all = (PIDGET (ptid) == -1);
1053 iterate_over_lwps (resume_set_callback, NULL);
1055 iterate_over_lwps (resume_clear_callback, NULL);
1057 /* If PID is -1, it's the current inferior that should be
1058 handled specially. */
1059 if (PIDGET (ptid) == -1)
1060 ptid = inferior_ptid;
1062 lp = find_lwp_pid (ptid);
1065 ptid = pid_to_ptid (GET_LWP (lp->ptid));
1067 /* Remember if we're stepping. */
1070 /* Mark this LWP as resumed. */
1073 /* If we have a pending wait status for this thread, there is no
1074 point in resuming the process. */
1077 /* FIXME: What should we do if we are supposed to continue
1078 this thread with a signal? */
1079 gdb_assert (signo == TARGET_SIGNAL_0);
1083 /* Mark LWP as not stopped to prevent it from being continued by
1089 iterate_over_lwps (resume_callback, NULL);
1091 child_resume (ptid, step, signo);
1092 if (debug_linux_nat)
1093 fprintf_unfiltered (gdb_stdlog,
1094 "LLR: %s %s, %s (resume event thread)\n",
1095 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1096 target_pid_to_str (ptid),
1097 signo ? strsignal (signo) : "0");
1100 /* Issue kill to specified lwp. */
1102 static int tkill_failed;
1105 kill_lwp (int lwpid, int signo)
1109 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1110 fails, then we are not using nptl threads and we should be using kill. */
1112 #ifdef HAVE_TKILL_SYSCALL
1115 int ret = syscall (__NR_tkill, lwpid, signo);
1116 if (errno != ENOSYS)
1123 return kill (lwpid, signo);
1126 /* Handle a GNU/Linux extended wait response. Most of the work we
1127 just pass off to linux_handle_extended_wait, but if it reports a
1128 clone event we need to add the new LWP to our list (and not report
1129 the trap to higher layers). This function returns non-zero if
1130 the event should be ignored and we should wait again. */
1133 linux_nat_handle_extended (struct lwp_info *lp, int status)
1135 linux_handle_extended_wait (GET_LWP (lp->ptid), status,
1138 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1139 if (lp->waitstatus.kind == TARGET_WAITKIND_SPURIOUS)
1141 struct lwp_info *new_lp;
1142 new_lp = add_lwp (BUILD_LWP (lp->waitstatus.value.related_pid,
1143 GET_PID (inferior_ptid)));
1145 new_lp->stopped = 1;
1147 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1149 if (debug_linux_nat)
1150 fprintf_unfiltered (gdb_stdlog,
1151 "LLHE: Got clone event from LWP %ld, resuming\n",
1152 GET_LWP (lp->ptid));
1153 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1161 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1165 wait_lwp (struct lwp_info *lp)
1169 int thread_dead = 0;
1171 gdb_assert (!lp->stopped);
1172 gdb_assert (lp->status == 0);
1174 pid = waitpid (GET_LWP (lp->ptid), &status, 0);
1175 if (pid == -1 && errno == ECHILD)
1177 pid = waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
1178 if (pid == -1 && errno == ECHILD)
1180 /* The thread has previously exited. We need to delete it
1181 now because, for some vendor 2.4 kernels with NPTL
1182 support backported, there won't be an exit event unless
1183 it is the main thread. 2.6 kernels will report an exit
1184 event for each thread that exits, as expected. */
1186 if (debug_linux_nat)
1187 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
1188 target_pid_to_str (lp->ptid));
1194 gdb_assert (pid == GET_LWP (lp->ptid));
1196 if (debug_linux_nat)
1198 fprintf_unfiltered (gdb_stdlog,
1199 "WL: waitpid %s received %s\n",
1200 target_pid_to_str (lp->ptid),
1201 status_to_str (status));
1205 /* Check if the thread has exited. */
1206 if (WIFEXITED (status) || WIFSIGNALED (status))
1209 if (debug_linux_nat)
1210 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
1211 target_pid_to_str (lp->ptid));
1216 if (in_thread_list (lp->ptid))
1218 /* Core GDB cannot deal with us deleting the current thread. */
1219 if (!ptid_equal (lp->ptid, inferior_ptid))
1220 delete_thread (lp->ptid);
1221 printf_unfiltered (_("[%s exited]\n"),
1222 target_pid_to_str (lp->ptid));
1225 delete_lwp (lp->ptid);
1229 gdb_assert (WIFSTOPPED (status));
1231 /* Handle GNU/Linux's extended waitstatus for trace events. */
1232 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1234 if (debug_linux_nat)
1235 fprintf_unfiltered (gdb_stdlog,
1236 "WL: Handling extended status 0x%06x\n",
1238 if (linux_nat_handle_extended (lp, status))
1239 return wait_lwp (lp);
1245 /* Send a SIGSTOP to LP. */
1248 stop_callback (struct lwp_info *lp, void *data)
1250 if (!lp->stopped && !lp->signalled)
1254 if (debug_linux_nat)
1256 fprintf_unfiltered (gdb_stdlog,
1257 "SC: kill %s **<SIGSTOP>**\n",
1258 target_pid_to_str (lp->ptid));
1261 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
1262 if (debug_linux_nat)
1264 fprintf_unfiltered (gdb_stdlog,
1265 "SC: lwp kill %d %s\n",
1267 errno ? safe_strerror (errno) : "ERRNO-OK");
1271 gdb_assert (lp->status == 0);
1277 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1278 a pointer to a set of signals to be flushed immediately. */
1281 stop_wait_callback (struct lwp_info *lp, void *data)
1283 sigset_t *flush_mask = data;
1289 status = wait_lwp (lp);
1293 /* Ignore any signals in FLUSH_MASK. */
1294 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
1303 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1304 if (debug_linux_nat)
1305 fprintf_unfiltered (gdb_stdlog,
1306 "PTRACE_CONT %s, 0, 0 (%s)\n",
1307 target_pid_to_str (lp->ptid),
1308 errno ? safe_strerror (errno) : "OK");
1310 return stop_wait_callback (lp, flush_mask);
1313 if (WSTOPSIG (status) != SIGSTOP)
1315 if (WSTOPSIG (status) == SIGTRAP)
1317 /* If a LWP other than the LWP that we're reporting an
1318 event for has hit a GDB breakpoint (as opposed to
1319 some random trap signal), then just arrange for it to
1320 hit it again later. We don't keep the SIGTRAP status
1321 and don't forward the SIGTRAP signal to the LWP. We
1322 will handle the current event, eventually we will
1323 resume all LWPs, and this one will get its breakpoint
1326 If we do not do this, then we run the risk that the
1327 user will delete or disable the breakpoint, but the
1328 thread will have already tripped on it. */
1330 /* Now resume this LWP and get the SIGSTOP event. */
1332 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1333 if (debug_linux_nat)
1335 fprintf_unfiltered (gdb_stdlog,
1336 "PTRACE_CONT %s, 0, 0 (%s)\n",
1337 target_pid_to_str (lp->ptid),
1338 errno ? safe_strerror (errno) : "OK");
1340 fprintf_unfiltered (gdb_stdlog,
1341 "SWC: Candidate SIGTRAP event in %s\n",
1342 target_pid_to_str (lp->ptid));
1344 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1345 stop_wait_callback (lp, data);
1346 /* If there's another event, throw it back into the queue. */
1349 if (debug_linux_nat)
1351 fprintf_unfiltered (gdb_stdlog,
1352 "SWC: kill %s, %s\n",
1353 target_pid_to_str (lp->ptid),
1354 status_to_str ((int) status));
1356 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
1358 /* Save the sigtrap event. */
1359 lp->status = status;
1364 /* The thread was stopped with a signal other than
1365 SIGSTOP, and didn't accidentally trip a breakpoint. */
1367 if (debug_linux_nat)
1369 fprintf_unfiltered (gdb_stdlog,
1370 "SWC: Pending event %s in %s\n",
1371 status_to_str ((int) status),
1372 target_pid_to_str (lp->ptid));
1374 /* Now resume this LWP and get the SIGSTOP event. */
1376 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1377 if (debug_linux_nat)
1378 fprintf_unfiltered (gdb_stdlog,
1379 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1380 target_pid_to_str (lp->ptid),
1381 errno ? safe_strerror (errno) : "OK");
1383 /* Hold this event/waitstatus while we check to see if
1384 there are any more (we still want to get that SIGSTOP). */
1385 stop_wait_callback (lp, data);
1386 /* If the lp->status field is still empty, use it to hold
1387 this event. If not, then this event must be returned
1388 to the event queue of the LWP. */
1389 if (lp->status == 0)
1390 lp->status = status;
1393 if (debug_linux_nat)
1395 fprintf_unfiltered (gdb_stdlog,
1396 "SWC: kill %s, %s\n",
1397 target_pid_to_str (lp->ptid),
1398 status_to_str ((int) status));
1400 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
1407 /* We caught the SIGSTOP that we intended to catch, so
1408 there's no SIGSTOP pending. */
1417 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1418 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1421 linux_nat_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask)
1423 sigset_t blocked, ignored;
1426 linux_proc_pending_signals (pid, pending, &blocked, &ignored);
1431 for (i = 1; i < NSIG; i++)
1432 if (sigismember (pending, i))
1433 if (!sigismember (flush_mask, i)
1434 || sigismember (&blocked, i)
1435 || sigismember (&ignored, i))
1436 sigdelset (pending, i);
1438 if (sigisemptyset (pending))
1444 /* DATA is interpreted as a mask of signals to flush. If LP has
1445 signals pending, and they are all in the flush mask, then arrange
1446 to flush them. LP should be stopped, as should all other threads
1447 it might share a signal queue with. */
1450 flush_callback (struct lwp_info *lp, void *data)
1452 sigset_t *flush_mask = data;
1453 sigset_t pending, intersection, blocked, ignored;
1456 /* Normally, when an LWP exits, it is removed from the LWP list. The
1457 last LWP isn't removed till later, however. So if there is only
1458 one LWP on the list, make sure it's alive. */
1459 if (lwp_list == lp && lp->next == NULL)
1460 if (!linux_nat_thread_alive (lp->ptid))
1463 /* Just because the LWP is stopped doesn't mean that new signals
1464 can't arrive from outside, so this function must be careful of
1465 race conditions. However, because all threads are stopped, we
1466 can assume that the pending mask will not shrink unless we resume
1467 the LWP, and that it will then get another signal. We can't
1468 control which one, however. */
1472 if (debug_linux_nat)
1473 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp->status);
1474 if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status)))
1478 while (linux_nat_has_pending (GET_LWP (lp->ptid), &pending, flush_mask))
1483 ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1484 if (debug_linux_nat)
1485 fprintf_unfiltered (gdb_stderr,
1486 "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno);
1489 stop_wait_callback (lp, flush_mask);
1490 if (debug_linux_nat)
1491 fprintf_unfiltered (gdb_stderr,
1492 "FC: Wait finished; saved status is %d\n",
1499 /* Return non-zero if LP has a wait status pending. */
1502 status_callback (struct lwp_info *lp, void *data)
1504 /* Only report a pending wait status if we pretend that this has
1505 indeed been resumed. */
1506 return (lp->status != 0 && lp->resumed);
1509 /* Return non-zero if LP isn't stopped. */
1512 running_callback (struct lwp_info *lp, void *data)
1514 return (lp->stopped == 0 || (lp->status != 0 && lp->resumed));
1517 /* Count the LWP's that have had events. */
1520 count_events_callback (struct lwp_info *lp, void *data)
1524 gdb_assert (count != NULL);
1526 /* Count only LWPs that have a SIGTRAP event pending. */
1528 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1534 /* Select the LWP (if any) that is currently being single-stepped. */
1537 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
1539 if (lp->step && lp->status != 0)
1545 /* Select the Nth LWP that has had a SIGTRAP event. */
1548 select_event_lwp_callback (struct lwp_info *lp, void *data)
1550 int *selector = data;
1552 gdb_assert (selector != NULL);
1554 /* Select only LWPs that have a SIGTRAP event pending. */
1556 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1557 if ((*selector)-- == 0)
1564 cancel_breakpoints_callback (struct lwp_info *lp, void *data)
1566 struct lwp_info *event_lp = data;
1568 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1572 /* If a LWP other than the LWP that we're reporting an event for has
1573 hit a GDB breakpoint (as opposed to some random trap signal),
1574 then just arrange for it to hit it again later. We don't keep
1575 the SIGTRAP status and don't forward the SIGTRAP signal to the
1576 LWP. We will handle the current event, eventually we will resume
1577 all LWPs, and this one will get its breakpoint trap again.
1579 If we do not do this, then we run the risk that the user will
1580 delete or disable the breakpoint, but the LWP will have already
1584 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
1585 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
1586 DECR_PC_AFTER_BREAK))
1588 if (debug_linux_nat)
1589 fprintf_unfiltered (gdb_stdlog,
1590 "CBC: Push back breakpoint for %s\n",
1591 target_pid_to_str (lp->ptid));
1593 /* Back up the PC if necessary. */
1594 if (DECR_PC_AFTER_BREAK)
1595 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
1597 /* Throw away the SIGTRAP. */
1604 /* Select one LWP out of those that have events pending. */
1607 select_event_lwp (struct lwp_info **orig_lp, int *status)
1610 int random_selector;
1611 struct lwp_info *event_lp;
1613 /* Record the wait status for the origional LWP. */
1614 (*orig_lp)->status = *status;
1616 /* Give preference to any LWP that is being single-stepped. */
1617 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
1618 if (event_lp != NULL)
1620 if (debug_linux_nat)
1621 fprintf_unfiltered (gdb_stdlog,
1622 "SEL: Select single-step %s\n",
1623 target_pid_to_str (event_lp->ptid));
1627 /* No single-stepping LWP. Select one at random, out of those
1628 which have had SIGTRAP events. */
1630 /* First see how many SIGTRAP events we have. */
1631 iterate_over_lwps (count_events_callback, &num_events);
1633 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1634 random_selector = (int)
1635 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
1637 if (debug_linux_nat && num_events > 1)
1638 fprintf_unfiltered (gdb_stdlog,
1639 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1640 num_events, random_selector);
1642 event_lp = iterate_over_lwps (select_event_lwp_callback,
1646 if (event_lp != NULL)
1648 /* Switch the event LWP. */
1649 *orig_lp = event_lp;
1650 *status = event_lp->status;
1653 /* Flush the wait status for the event LWP. */
1654 (*orig_lp)->status = 0;
1657 /* Return non-zero if LP has been resumed. */
1660 resumed_callback (struct lwp_info *lp, void *data)
1667 /* We need to override child_wait to support attaching to cloned
1668 processes, since a normal wait (as done by the default version)
1669 ignores those processes. */
1671 /* Wait for child PTID to do something. Return id of the child,
1672 minus_one_ptid in case of error; store status into *OURSTATUS. */
1675 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1681 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1685 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1686 attached process. */
1689 pid = waitpid (GET_PID (ptid), &status, 0);
1690 if (pid == -1 && errno == ECHILD)
1691 /* Try again with __WCLONE to check cloned processes. */
1692 pid = waitpid (GET_PID (ptid), &status, __WCLONE);
1694 if (debug_linux_nat)
1696 fprintf_unfiltered (gdb_stdlog,
1697 "CW: waitpid %ld received %s\n",
1698 (long) pid, status_to_str (status));
1703 /* Make sure we don't report an event for the exit of the
1704 original program, if we've detached from it. */
1705 if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid))
1711 /* Check for stop events reported by a process we didn't already
1712 know about - in this case, anything other than inferior_ptid.
1714 If we're expecting to receive stopped processes after fork,
1715 vfork, and clone events, then we'll just add the new one to
1716 our list and go back to waiting for the event to be reported
1717 - the stopped process might be returned from waitpid before
1718 or after the event is. If we want to handle debugging of
1719 CLONE_PTRACE processes we need to do more here, i.e. switch
1720 to multi-threaded mode. */
1721 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP
1722 && pid != GET_PID (inferior_ptid))
1724 linux_record_stopped_pid (pid);
1729 /* Handle GNU/Linux's extended waitstatus for trace events. */
1730 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
1731 && status >> 16 != 0)
1733 linux_handle_extended_wait (pid, status, ourstatus);
1735 /* If we see a clone event, detach the child, and don't
1736 report the event. It would be nice to offer some way to
1737 switch into a non-thread-db based threaded mode at this
1739 if (ourstatus->kind == TARGET_WAITKIND_SPURIOUS)
1741 ptrace (PTRACE_DETACH, ourstatus->value.related_pid, 0, 0);
1742 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1743 ptrace (PTRACE_CONT, pid, 0, 0);
1749 clear_sigio_trap ();
1750 clear_sigint_trap ();
1752 while (pid == -1 && save_errno == EINTR);
1756 warning (_("Child process unexpectedly missing: %s"),
1757 safe_strerror (errno));
1759 /* Claim it exited with unknown signal. */
1760 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1761 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1762 return minus_one_ptid;
1765 if (ourstatus->kind == TARGET_WAITKIND_IGNORE)
1766 store_waitstatus (ourstatus, status);
1768 return pid_to_ptid (pid);
1773 /* Stop an active thread, verify it still exists, then resume it. */
1776 stop_and_resume_callback (struct lwp_info *lp, void *data)
1778 struct lwp_info *ptr;
1780 if (!lp->stopped && !lp->signalled)
1782 stop_callback (lp, NULL);
1783 stop_wait_callback (lp, NULL);
1784 /* Resume if the lwp still exists. */
1785 for (ptr = lwp_list; ptr; ptr = ptr->next)
1788 resume_callback (lp, NULL);
1789 resume_set_callback (lp, NULL);
1796 linux_nat_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1798 struct lwp_info *lp = NULL;
1801 pid_t pid = PIDGET (ptid);
1802 sigset_t flush_mask;
1804 sigemptyset (&flush_mask);
1806 /* Make sure SIGCHLD is blocked. */
1807 if (!sigismember (&blocked_mask, SIGCHLD))
1809 sigaddset (&blocked_mask, SIGCHLD);
1810 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1815 /* Make sure there is at least one LWP that has been resumed, at
1816 least if there are any LWPs at all. */
1817 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
1819 /* First check if there is a LWP with a wait status pending. */
1822 /* Any LWP that's been resumed will do. */
1823 lp = iterate_over_lwps (status_callback, NULL);
1826 status = lp->status;
1829 if (debug_linux_nat && status)
1830 fprintf_unfiltered (gdb_stdlog,
1831 "LLW: Using pending wait status %s for %s.\n",
1832 status_to_str (status),
1833 target_pid_to_str (lp->ptid));
1836 /* But if we don't fine one, we'll have to wait, and check both
1837 cloned and uncloned processes. We start with the cloned
1839 options = __WCLONE | WNOHANG;
1841 else if (is_lwp (ptid))
1843 if (debug_linux_nat)
1844 fprintf_unfiltered (gdb_stdlog,
1845 "LLW: Waiting for specific LWP %s.\n",
1846 target_pid_to_str (ptid));
1848 /* We have a specific LWP to check. */
1849 lp = find_lwp_pid (ptid);
1851 status = lp->status;
1854 if (debug_linux_nat && status)
1855 fprintf_unfiltered (gdb_stdlog,
1856 "LLW: Using pending wait status %s for %s.\n",
1857 status_to_str (status),
1858 target_pid_to_str (lp->ptid));
1860 /* If we have to wait, take into account whether PID is a cloned
1861 process or not. And we have to convert it to something that
1862 the layer beneath us can understand. */
1863 options = lp->cloned ? __WCLONE : 0;
1864 pid = GET_LWP (ptid);
1867 if (status && lp->signalled)
1869 /* A pending SIGSTOP may interfere with the normal stream of
1870 events. In a typical case where interference is a problem,
1871 we have a SIGSTOP signal pending for LWP A while
1872 single-stepping it, encounter an event in LWP B, and take the
1873 pending SIGSTOP while trying to stop LWP A. After processing
1874 the event in LWP B, LWP A is continued, and we'll never see
1875 the SIGTRAP associated with the last time we were
1876 single-stepping LWP A. */
1878 /* Resume the thread. It should halt immediately returning the
1880 registers_changed ();
1881 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1883 if (debug_linux_nat)
1884 fprintf_unfiltered (gdb_stdlog,
1885 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1886 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1887 target_pid_to_str (lp->ptid));
1889 gdb_assert (lp->resumed);
1891 /* This should catch the pending SIGSTOP. */
1892 stop_wait_callback (lp, NULL);
1895 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1896 attached process. */
1903 lwpid = waitpid (pid, &status, options);
1906 gdb_assert (pid == -1 || lwpid == pid);
1908 if (debug_linux_nat)
1910 fprintf_unfiltered (gdb_stdlog,
1911 "LLW: waitpid %ld received %s\n",
1912 (long) lwpid, status_to_str (status));
1915 lp = find_lwp_pid (pid_to_ptid (lwpid));
1917 /* Check for stop events reported by a process we didn't
1918 already know about - anything not already in our LWP
1921 If we're expecting to receive stopped processes after
1922 fork, vfork, and clone events, then we'll just add the
1923 new one to our list and go back to waiting for the event
1924 to be reported - the stopped process might be returned
1925 from waitpid before or after the event is. */
1926 if (WIFSTOPPED (status) && !lp)
1928 linux_record_stopped_pid (lwpid);
1933 /* Make sure we don't report an event for the exit of an LWP not in
1934 our list, i.e. not part of the current process. This can happen
1935 if we detach from a program we original forked and then it
1937 if (!WIFSTOPPED (status) && !lp)
1943 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1944 CLONE_PTRACE processes which do not use the thread library -
1945 otherwise we wouldn't find the new LWP this way. That doesn't
1946 currently work, and the following code is currently unreachable
1947 due to the two blocks above. If it's fixed some day, this code
1948 should be broken out into a function so that we can also pick up
1949 LWPs from the new interface. */
1952 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
1953 if (options & __WCLONE)
1958 gdb_assert (WIFSTOPPED (status)
1959 && WSTOPSIG (status) == SIGSTOP);
1962 if (!in_thread_list (inferior_ptid))
1964 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1965 GET_PID (inferior_ptid));
1966 add_thread (inferior_ptid);
1969 add_thread (lp->ptid);
1970 printf_unfiltered (_("[New %s]\n"),
1971 target_pid_to_str (lp->ptid));
1975 /* Handle GNU/Linux's extended waitstatus for trace events. */
1976 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1978 if (debug_linux_nat)
1979 fprintf_unfiltered (gdb_stdlog,
1980 "LLW: Handling extended status 0x%06x\n",
1982 if (linux_nat_handle_extended (lp, status))
1989 /* Check if the thread has exited. */
1990 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
1992 if (in_thread_list (lp->ptid))
1994 /* Core GDB cannot deal with us deleting the current
1996 if (!ptid_equal (lp->ptid, inferior_ptid))
1997 delete_thread (lp->ptid);
1998 printf_unfiltered (_("[%s exited]\n"),
1999 target_pid_to_str (lp->ptid));
2002 /* If this is the main thread, we must stop all threads and
2003 verify if they are still alive. This is because in the nptl
2004 thread model, there is no signal issued for exiting LWPs
2005 other than the main thread. We only get the main thread
2006 exit signal once all child threads have already exited.
2007 If we stop all the threads and use the stop_wait_callback
2008 to check if they have exited we can determine whether this
2009 signal should be ignored or whether it means the end of the
2010 debugged application, regardless of which threading model
2012 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
2015 iterate_over_lwps (stop_and_resume_callback, NULL);
2018 if (debug_linux_nat)
2019 fprintf_unfiltered (gdb_stdlog,
2020 "LLW: %s exited.\n",
2021 target_pid_to_str (lp->ptid));
2023 delete_lwp (lp->ptid);
2025 /* If there is at least one more LWP, then the exit signal
2026 was not the end of the debugged application and should be
2030 /* Make sure there is at least one thread running. */
2031 gdb_assert (iterate_over_lwps (running_callback, NULL));
2033 /* Discard the event. */
2039 /* Check if the current LWP has previously exited. In the nptl
2040 thread model, LWPs other than the main thread do not issue
2041 signals when they exit so we must check whenever the thread
2042 has stopped. A similar check is made in stop_wait_callback(). */
2043 if (num_lwps > 1 && !linux_nat_thread_alive (lp->ptid))
2045 if (in_thread_list (lp->ptid))
2047 /* Core GDB cannot deal with us deleting the current
2049 if (!ptid_equal (lp->ptid, inferior_ptid))
2050 delete_thread (lp->ptid);
2051 printf_unfiltered (_("[%s exited]\n"),
2052 target_pid_to_str (lp->ptid));
2054 if (debug_linux_nat)
2055 fprintf_unfiltered (gdb_stdlog,
2056 "LLW: %s exited.\n",
2057 target_pid_to_str (lp->ptid));
2059 delete_lwp (lp->ptid);
2061 /* Make sure there is at least one thread running. */
2062 gdb_assert (iterate_over_lwps (running_callback, NULL));
2064 /* Discard the event. */
2069 /* Make sure we don't report a SIGSTOP that we sent
2070 ourselves in an attempt to stop an LWP. */
2072 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
2074 if (debug_linux_nat)
2075 fprintf_unfiltered (gdb_stdlog,
2076 "LLW: Delayed SIGSTOP caught for %s.\n",
2077 target_pid_to_str (lp->ptid));
2079 /* This is a delayed SIGSTOP. */
2082 registers_changed ();
2083 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
2085 if (debug_linux_nat)
2086 fprintf_unfiltered (gdb_stdlog,
2087 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2089 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2090 target_pid_to_str (lp->ptid));
2093 gdb_assert (lp->resumed);
2095 /* Discard the event. */
2105 /* Alternate between checking cloned and uncloned processes. */
2106 options ^= __WCLONE;
2108 /* And suspend every time we have checked both. */
2109 if (options & __WCLONE)
2110 sigsuspend (&suspend_mask);
2113 /* We shouldn't end up here unless we want to try again. */
2114 gdb_assert (status == 0);
2117 clear_sigio_trap ();
2118 clear_sigint_trap ();
2122 /* Don't report signals that GDB isn't interested in, such as
2123 signals that are neither printed nor stopped upon. Stopping all
2124 threads can be a bit time-consuming so if we want decent
2125 performance with heavily multi-threaded programs, especially when
2126 they're using a high frequency timer, we'd better avoid it if we
2129 if (WIFSTOPPED (status))
2131 int signo = target_signal_from_host (WSTOPSIG (status));
2133 if (signal_stop_state (signo) == 0
2134 && signal_print_state (signo) == 0
2135 && signal_pass_state (signo) == 1)
2137 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2138 here? It is not clear we should. GDB may not expect
2139 other threads to run. On the other hand, not resuming
2140 newly attached threads may cause an unwanted delay in
2141 getting them running. */
2142 registers_changed ();
2143 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
2144 if (debug_linux_nat)
2145 fprintf_unfiltered (gdb_stdlog,
2146 "LLW: %s %s, %s (preempt 'handle')\n",
2148 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2149 target_pid_to_str (lp->ptid),
2150 signo ? strsignal (signo) : "0");
2156 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
2158 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2159 forwarded to the entire process group, that is, all LWP's
2160 will receive it. Since we only want to report it once,
2161 we try to flush it from all LWPs except this one. */
2162 sigaddset (&flush_mask, SIGINT);
2166 /* This LWP is stopped now. */
2169 if (debug_linux_nat)
2170 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
2171 status_to_str (status), target_pid_to_str (lp->ptid));
2173 /* Now stop all other LWP's ... */
2174 iterate_over_lwps (stop_callback, NULL);
2176 /* ... and wait until all of them have reported back that they're no
2178 iterate_over_lwps (stop_wait_callback, &flush_mask);
2179 iterate_over_lwps (flush_callback, &flush_mask);
2181 /* If we're not waiting for a specific LWP, choose an event LWP from
2182 among those that have had events. Giving equal priority to all
2183 LWPs that have had events helps prevent starvation. */
2185 select_event_lwp (&lp, &status);
2187 /* Now that we've selected our final event LWP, cancel any
2188 breakpoints in other LWPs that have hit a GDB breakpoint. See
2189 the comment in cancel_breakpoints_callback to find out why. */
2190 iterate_over_lwps (cancel_breakpoints_callback, lp);
2192 /* If we're not running in "threaded" mode, we'll report the bare
2195 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
2197 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
2198 if (debug_linux_nat)
2199 fprintf_unfiltered (gdb_stdlog,
2200 "LLW: trap_ptid is %s.\n",
2201 target_pid_to_str (trap_ptid));
2204 trap_ptid = null_ptid;
2206 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2208 *ourstatus = lp->waitstatus;
2209 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
2212 store_waitstatus (ourstatus, status);
2214 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
2218 kill_callback (struct lwp_info *lp, void *data)
2221 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
2222 if (debug_linux_nat)
2223 fprintf_unfiltered (gdb_stdlog,
2224 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2225 target_pid_to_str (lp->ptid),
2226 errno ? safe_strerror (errno) : "OK");
2232 kill_wait_callback (struct lwp_info *lp, void *data)
2236 /* We must make sure that there are no pending events (delayed
2237 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2238 program doesn't interfere with any following debugging session. */
2240 /* For cloned processes we must check both with __WCLONE and
2241 without, since the exit status of a cloned process isn't reported
2247 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
2248 if (pid != (pid_t) -1 && debug_linux_nat)
2250 fprintf_unfiltered (gdb_stdlog,
2251 "KWC: wait %s received unknown.\n",
2252 target_pid_to_str (lp->ptid));
2255 while (pid == GET_LWP (lp->ptid));
2257 gdb_assert (pid == -1 && errno == ECHILD);
2262 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
2263 if (pid != (pid_t) -1 && debug_linux_nat)
2265 fprintf_unfiltered (gdb_stdlog,
2266 "KWC: wait %s received unk.\n",
2267 target_pid_to_str (lp->ptid));
2270 while (pid == GET_LWP (lp->ptid));
2272 gdb_assert (pid == -1 && errno == ECHILD);
2277 linux_nat_kill (void)
2279 /* Kill all LWP's ... */
2280 iterate_over_lwps (kill_callback, NULL);
2282 /* ... and wait until we've flushed all events. */
2283 iterate_over_lwps (kill_wait_callback, NULL);
2285 target_mourn_inferior ();
2289 linux_nat_create_inferior (char *exec_file, char *allargs, char **env,
2292 deprecated_child_ops.to_create_inferior (exec_file, allargs, env, from_tty);
2296 linux_nat_mourn_inferior (void)
2298 trap_ptid = null_ptid;
2300 /* Destroy LWP info; it's no longer valid. */
2303 /* Restore the original signal mask. */
2304 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
2305 sigemptyset (&blocked_mask);
2307 deprecated_child_ops.to_mourn_inferior ();
2311 linux_nat_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
2312 struct mem_attrib *attrib, struct target_ops *target)
2314 struct cleanup *old_chain = save_inferior_ptid ();
2317 if (is_lwp (inferior_ptid))
2318 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
2320 xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target);
2322 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
2324 do_cleanups (old_chain);
2329 linux_nat_thread_alive (ptid_t ptid)
2331 gdb_assert (is_lwp (ptid));
2334 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
2335 if (debug_linux_nat)
2336 fprintf_unfiltered (gdb_stdlog,
2337 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2338 target_pid_to_str (ptid),
2339 errno ? safe_strerror (errno) : "OK");
2347 linux_nat_pid_to_str (ptid_t ptid)
2349 static char buf[64];
2353 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
2357 return normal_pid_to_str (ptid);
2361 init_linux_nat_ops (void)
2364 linux_nat_ops.to_open = linux_nat_open;
2366 linux_nat_ops.to_shortname = "lwp-layer";
2367 linux_nat_ops.to_longname = "lwp-layer";
2368 linux_nat_ops.to_doc = "Low level threads support (LWP layer)";
2369 linux_nat_ops.to_attach = linux_nat_attach;
2370 linux_nat_ops.to_detach = linux_nat_detach;
2371 linux_nat_ops.to_resume = linux_nat_resume;
2372 linux_nat_ops.to_wait = linux_nat_wait;
2373 /* fetch_inferior_registers and store_inferior_registers will
2374 honor the LWP id, so we can use them directly. */
2375 linux_nat_ops.to_fetch_registers = fetch_inferior_registers;
2376 linux_nat_ops.to_store_registers = store_inferior_registers;
2377 linux_nat_ops.deprecated_xfer_memory = linux_nat_xfer_memory;
2378 linux_nat_ops.to_kill = linux_nat_kill;
2379 linux_nat_ops.to_create_inferior = linux_nat_create_inferior;
2380 linux_nat_ops.to_mourn_inferior = linux_nat_mourn_inferior;
2381 linux_nat_ops.to_thread_alive = linux_nat_thread_alive;
2382 linux_nat_ops.to_pid_to_str = linux_nat_pid_to_str;
2383 linux_nat_ops.to_post_startup_inferior = child_post_startup_inferior;
2384 linux_nat_ops.to_post_attach = child_post_attach;
2385 linux_nat_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
2386 linux_nat_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
2387 linux_nat_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
2389 linux_nat_ops.to_stratum = thread_stratum;
2390 linux_nat_ops.to_has_thread_control = tc_schedlock;
2391 linux_nat_ops.to_magic = OPS_MAGIC;
2395 sigchld_handler (int signo)
2397 /* Do nothing. The only reason for this handler is that it allows
2398 us to use sigsuspend in linux_nat_wait above to wait for the
2399 arrival of a SIGCHLD. */
2402 /* Accepts an integer PID; Returns a string representing a file that
2403 can be opened to get the symbols for the child process. */
2406 child_pid_to_exec_file (int pid)
2408 char *name1, *name2;
2410 name1 = xmalloc (MAXPATHLEN);
2411 name2 = xmalloc (MAXPATHLEN);
2412 make_cleanup (xfree, name1);
2413 make_cleanup (xfree, name2);
2414 memset (name2, 0, MAXPATHLEN);
2416 sprintf (name1, "/proc/%d/exe", pid);
2417 if (readlink (name1, name2, MAXPATHLEN) > 0)
2423 /* Service function for corefiles and info proc. */
2426 read_mapping (FILE *mapfile,
2431 char *device, long long *inode, char *filename)
2433 int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx",
2434 addr, endaddr, permissions, offset, device, inode);
2436 if (ret > 0 && ret != EOF && *inode != 0)
2438 /* Eat everything up to EOL for the filename. This will prevent
2439 weird filenames (such as one with embedded whitespace) from
2440 confusing this code. It also makes this code more robust in
2441 respect to annotations the kernel may add after the filename.
2443 Note the filename is used for informational purposes
2445 ret += fscanf (mapfile, "%[^\n]\n", filename);
2449 filename[0] = '\0'; /* no filename */
2450 fscanf (mapfile, "\n");
2452 return (ret != 0 && ret != EOF);
2455 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2456 regions in the inferior for a corefile. */
2459 linux_nat_find_memory_regions (int (*func) (CORE_ADDR,
2461 int, int, int, void *), void *obfd)
2463 long long pid = PIDGET (inferior_ptid);
2464 char mapsfilename[MAXPATHLEN];
2466 long long addr, endaddr, size, offset, inode;
2467 char permissions[8], device[8], filename[MAXPATHLEN];
2468 int read, write, exec;
2471 /* Compose the filename for the /proc memory map, and open it. */
2472 sprintf (mapsfilename, "/proc/%lld/maps", pid);
2473 if ((mapsfile = fopen (mapsfilename, "r")) == NULL)
2474 error (_("Could not open %s."), mapsfilename);
2477 fprintf_filtered (gdb_stdout,
2478 "Reading memory regions from %s\n", mapsfilename);
2480 /* Now iterate until end-of-file. */
2481 while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0],
2482 &offset, &device[0], &inode, &filename[0]))
2484 size = endaddr - addr;
2486 /* Get the segment's permissions. */
2487 read = (strchr (permissions, 'r') != 0);
2488 write = (strchr (permissions, 'w') != 0);
2489 exec = (strchr (permissions, 'x') != 0);
2493 fprintf_filtered (gdb_stdout,
2494 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2495 size, paddr_nz (addr),
2497 write ? 'w' : ' ', exec ? 'x' : ' ');
2498 if (filename && filename[0])
2499 fprintf_filtered (gdb_stdout, " for %s", filename);
2500 fprintf_filtered (gdb_stdout, "\n");
2503 /* Invoke the callback function to create the corefile
2505 func (addr, size, read, write, exec, obfd);
2511 /* Records the thread's register state for the corefile note
2515 linux_nat_do_thread_registers (bfd *obfd, ptid_t ptid,
2516 char *note_data, int *note_size)
2518 gdb_gregset_t gregs;
2519 gdb_fpregset_t fpregs;
2520 #ifdef FILL_FPXREGSET
2521 gdb_fpxregset_t fpxregs;
2523 unsigned long lwp = ptid_get_lwp (ptid);
2525 fill_gregset (&gregs, -1);
2526 note_data = (char *) elfcore_write_prstatus (obfd,
2530 stop_signal, &gregs);
2532 fill_fpregset (&fpregs, -1);
2533 note_data = (char *) elfcore_write_prfpreg (obfd,
2536 &fpregs, sizeof (fpregs));
2537 #ifdef FILL_FPXREGSET
2538 fill_fpxregset (&fpxregs, -1);
2539 note_data = (char *) elfcore_write_prxfpreg (obfd,
2542 &fpxregs, sizeof (fpxregs));
2547 struct linux_nat_corefile_thread_data
2555 /* Called by gdbthread.c once per thread. Records the thread's
2556 register state for the corefile note section. */
2559 linux_nat_corefile_thread_callback (struct lwp_info *ti, void *data)
2561 struct linux_nat_corefile_thread_data *args = data;
2562 ptid_t saved_ptid = inferior_ptid;
2564 inferior_ptid = ti->ptid;
2565 registers_changed ();
2566 target_fetch_registers (-1); /* FIXME should not be necessary;
2567 fill_gregset should do it automatically. */
2568 args->note_data = linux_nat_do_thread_registers (args->obfd,
2573 inferior_ptid = saved_ptid;
2574 registers_changed ();
2575 target_fetch_registers (-1); /* FIXME should not be necessary;
2576 fill_gregset should do it automatically. */
2580 /* Records the register state for the corefile note section. */
2583 linux_nat_do_registers (bfd *obfd, ptid_t ptid,
2584 char *note_data, int *note_size)
2586 registers_changed ();
2587 target_fetch_registers (-1); /* FIXME should not be necessary;
2588 fill_gregset should do it automatically. */
2589 return linux_nat_do_thread_registers (obfd,
2590 ptid_build (ptid_get_pid (inferior_ptid),
2591 ptid_get_pid (inferior_ptid),
2593 note_data, note_size);
2597 /* Fills the "to_make_corefile_note" target vector. Builds the note
2598 section for a corefile, and returns it in a malloc buffer. */
2601 linux_nat_make_corefile_notes (bfd *obfd, int *note_size)
2603 struct linux_nat_corefile_thread_data thread_args;
2604 struct cleanup *old_chain;
2605 char fname[16] = { '\0' };
2606 char psargs[80] = { '\0' };
2607 char *note_data = NULL;
2608 ptid_t current_ptid = inferior_ptid;
2612 if (get_exec_file (0))
2614 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
2615 strncpy (psargs, get_exec_file (0), sizeof (psargs));
2616 if (get_inferior_args ())
2618 strncat (psargs, " ", sizeof (psargs) - strlen (psargs));
2619 strncat (psargs, get_inferior_args (),
2620 sizeof (psargs) - strlen (psargs));
2622 note_data = (char *) elfcore_write_prpsinfo (obfd,
2624 note_size, fname, psargs);
2627 /* Dump information for threads. */
2628 thread_args.obfd = obfd;
2629 thread_args.note_data = note_data;
2630 thread_args.note_size = note_size;
2631 thread_args.num_notes = 0;
2632 iterate_over_lwps (linux_nat_corefile_thread_callback, &thread_args);
2633 if (thread_args.num_notes == 0)
2635 /* iterate_over_threads didn't come up with any threads; just
2636 use inferior_ptid. */
2637 note_data = linux_nat_do_registers (obfd, inferior_ptid,
2638 note_data, note_size);
2642 note_data = thread_args.note_data;
2645 auxv_len = target_auxv_read (¤t_target, &auxv);
2648 note_data = elfcore_write_note (obfd, note_data, note_size,
2649 "CORE", NT_AUXV, auxv, auxv_len);
2653 make_cleanup (xfree, note_data);
2657 /* Implement the "info proc" command. */
2660 linux_nat_info_proc_cmd (char *args, int from_tty)
2662 long long pid = PIDGET (inferior_ptid);
2665 char buffer[MAXPATHLEN];
2666 char fname1[MAXPATHLEN], fname2[MAXPATHLEN];
2679 /* Break up 'args' into an argv array. */
2680 if ((argv = buildargv (args)) == NULL)
2683 make_cleanup_freeargv (argv);
2685 while (argv != NULL && *argv != NULL)
2687 if (isdigit (argv[0][0]))
2689 pid = strtoul (argv[0], NULL, 10);
2691 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
2695 else if (strcmp (argv[0], "status") == 0)
2699 else if (strcmp (argv[0], "stat") == 0)
2703 else if (strcmp (argv[0], "cmd") == 0)
2707 else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0)
2711 else if (strcmp (argv[0], "cwd") == 0)
2715 else if (strncmp (argv[0], "all", strlen (argv[0])) == 0)
2721 /* [...] (future options here) */
2726 error (_("No current process: you must name one."));
2728 sprintf (fname1, "/proc/%lld", pid);
2729 if (stat (fname1, &dummy) != 0)
2730 error (_("No /proc directory: '%s'"), fname1);
2732 printf_filtered (_("process %lld\n"), pid);
2733 if (cmdline_f || all)
2735 sprintf (fname1, "/proc/%lld/cmdline", pid);
2736 if ((procfile = fopen (fname1, "r")) > 0)
2738 fgets (buffer, sizeof (buffer), procfile);
2739 printf_filtered ("cmdline = '%s'\n", buffer);
2743 warning (_("unable to open /proc file '%s'"), fname1);
2747 sprintf (fname1, "/proc/%lld/cwd", pid);
2748 memset (fname2, 0, sizeof (fname2));
2749 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
2750 printf_filtered ("cwd = '%s'\n", fname2);
2752 warning (_("unable to read link '%s'"), fname1);
2756 sprintf (fname1, "/proc/%lld/exe", pid);
2757 memset (fname2, 0, sizeof (fname2));
2758 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
2759 printf_filtered ("exe = '%s'\n", fname2);
2761 warning (_("unable to read link '%s'"), fname1);
2763 if (mappings_f || all)
2765 sprintf (fname1, "/proc/%lld/maps", pid);
2766 if ((procfile = fopen (fname1, "r")) > 0)
2768 long long addr, endaddr, size, offset, inode;
2769 char permissions[8], device[8], filename[MAXPATHLEN];
2771 printf_filtered (_("Mapped address spaces:\n\n"));
2772 if (TARGET_ADDR_BIT == 32)
2774 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2777 " Size", " Offset", "objfile");
2781 printf_filtered (" %18s %18s %10s %10s %7s\n",
2784 " Size", " Offset", "objfile");
2787 while (read_mapping (procfile, &addr, &endaddr, &permissions[0],
2788 &offset, &device[0], &inode, &filename[0]))
2790 size = endaddr - addr;
2792 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2793 calls here (and possibly above) should be abstracted
2794 out into their own functions? Andrew suggests using
2795 a generic local_address_string instead to print out
2796 the addresses; that makes sense to me, too. */
2798 if (TARGET_ADDR_BIT == 32)
2800 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2801 (unsigned long) addr, /* FIXME: pr_addr */
2802 (unsigned long) endaddr,
2804 (unsigned int) offset,
2805 filename[0] ? filename : "");
2809 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2810 (unsigned long) addr, /* FIXME: pr_addr */
2811 (unsigned long) endaddr,
2813 (unsigned int) offset,
2814 filename[0] ? filename : "");
2821 warning (_("unable to open /proc file '%s'"), fname1);
2823 if (status_f || all)
2825 sprintf (fname1, "/proc/%lld/status", pid);
2826 if ((procfile = fopen (fname1, "r")) > 0)
2828 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
2829 puts_filtered (buffer);
2833 warning (_("unable to open /proc file '%s'"), fname1);
2837 sprintf (fname1, "/proc/%lld/stat", pid);
2838 if ((procfile = fopen (fname1, "r")) > 0)
2843 if (fscanf (procfile, "%d ", &itmp) > 0)
2844 printf_filtered (_("Process: %d\n"), itmp);
2845 if (fscanf (procfile, "%s ", &buffer[0]) > 0)
2846 printf_filtered (_("Exec file: %s\n"), buffer);
2847 if (fscanf (procfile, "%c ", &ctmp) > 0)
2848 printf_filtered (_("State: %c\n"), ctmp);
2849 if (fscanf (procfile, "%d ", &itmp) > 0)
2850 printf_filtered (_("Parent process: %d\n"), itmp);
2851 if (fscanf (procfile, "%d ", &itmp) > 0)
2852 printf_filtered (_("Process group: %d\n"), itmp);
2853 if (fscanf (procfile, "%d ", &itmp) > 0)
2854 printf_filtered (_("Session id: %d\n"), itmp);
2855 if (fscanf (procfile, "%d ", &itmp) > 0)
2856 printf_filtered (_("TTY: %d\n"), itmp);
2857 if (fscanf (procfile, "%d ", &itmp) > 0)
2858 printf_filtered (_("TTY owner process group: %d\n"), itmp);
2859 if (fscanf (procfile, "%u ", &itmp) > 0)
2860 printf_filtered (_("Flags: 0x%x\n"), itmp);
2861 if (fscanf (procfile, "%u ", &itmp) > 0)
2862 printf_filtered (_("Minor faults (no memory page): %u\n"),
2863 (unsigned int) itmp);
2864 if (fscanf (procfile, "%u ", &itmp) > 0)
2865 printf_filtered (_("Minor faults, children: %u\n"),
2866 (unsigned int) itmp);
2867 if (fscanf (procfile, "%u ", &itmp) > 0)
2868 printf_filtered (_("Major faults (memory page faults): %u\n"),
2869 (unsigned int) itmp);
2870 if (fscanf (procfile, "%u ", &itmp) > 0)
2871 printf_filtered (_("Major faults, children: %u\n"),
2872 (unsigned int) itmp);
2873 if (fscanf (procfile, "%d ", &itmp) > 0)
2874 printf_filtered ("utime: %d\n", itmp);
2875 if (fscanf (procfile, "%d ", &itmp) > 0)
2876 printf_filtered ("stime: %d\n", itmp);
2877 if (fscanf (procfile, "%d ", &itmp) > 0)
2878 printf_filtered ("utime, children: %d\n", itmp);
2879 if (fscanf (procfile, "%d ", &itmp) > 0)
2880 printf_filtered ("stime, children: %d\n", itmp);
2881 if (fscanf (procfile, "%d ", &itmp) > 0)
2882 printf_filtered (_("jiffies remaining in current time slice: %d\n"),
2884 if (fscanf (procfile, "%d ", &itmp) > 0)
2885 printf_filtered ("'nice' value: %d\n", itmp);
2886 if (fscanf (procfile, "%u ", &itmp) > 0)
2887 printf_filtered (_("jiffies until next timeout: %u\n"),
2888 (unsigned int) itmp);
2889 if (fscanf (procfile, "%u ", &itmp) > 0)
2890 printf_filtered ("jiffies until next SIGALRM: %u\n",
2891 (unsigned int) itmp);
2892 if (fscanf (procfile, "%d ", &itmp) > 0)
2893 printf_filtered (_("start time (jiffies since system boot): %d\n"),
2895 if (fscanf (procfile, "%u ", &itmp) > 0)
2896 printf_filtered (_("Virtual memory size: %u\n"),
2897 (unsigned int) itmp);
2898 if (fscanf (procfile, "%u ", &itmp) > 0)
2899 printf_filtered (_("Resident set size: %u\n"), (unsigned int) itmp);
2900 if (fscanf (procfile, "%u ", &itmp) > 0)
2901 printf_filtered ("rlim: %u\n", (unsigned int) itmp);
2902 if (fscanf (procfile, "%u ", &itmp) > 0)
2903 printf_filtered (_("Start of text: 0x%x\n"), itmp);
2904 if (fscanf (procfile, "%u ", &itmp) > 0)
2905 printf_filtered (_("End of text: 0x%x\n"), itmp);
2906 if (fscanf (procfile, "%u ", &itmp) > 0)
2907 printf_filtered (_("Start of stack: 0x%x\n"), itmp);
2908 #if 0 /* Don't know how architecture-dependent the rest is...
2909 Anyway the signal bitmap info is available from "status". */
2910 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
2911 printf_filtered (_("Kernel stack pointer: 0x%x\n"), itmp);
2912 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
2913 printf_filtered (_("Kernel instr pointer: 0x%x\n"), itmp);
2914 if (fscanf (procfile, "%d ", &itmp) > 0)
2915 printf_filtered (_("Pending signals bitmap: 0x%x\n"), itmp);
2916 if (fscanf (procfile, "%d ", &itmp) > 0)
2917 printf_filtered (_("Blocked signals bitmap: 0x%x\n"), itmp);
2918 if (fscanf (procfile, "%d ", &itmp) > 0)
2919 printf_filtered (_("Ignored signals bitmap: 0x%x\n"), itmp);
2920 if (fscanf (procfile, "%d ", &itmp) > 0)
2921 printf_filtered (_("Catched signals bitmap: 0x%x\n"), itmp);
2922 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
2923 printf_filtered (_("wchan (system call): 0x%x\n"), itmp);
2928 warning (_("unable to open /proc file '%s'"), fname1);
2933 linux_proc_xfer_memory (CORE_ADDR addr, char *myaddr, int len, int write,
2934 struct mem_attrib *attrib, struct target_ops *target)
2942 /* Don't bother for one word. */
2943 if (len < 3 * sizeof (long))
2946 /* We could keep this file open and cache it - possibly one per
2947 thread. That requires some juggling, but is even faster. */
2948 sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid));
2949 fd = open (filename, O_RDONLY | O_LARGEFILE);
2953 /* If pread64 is available, use it. It's faster if the kernel
2954 supports it (only one syscall), and it's 64-bit safe even on
2955 32-bit platforms (for instance, SPARC debugging a SPARC64
2958 if (pread64 (fd, myaddr, len, addr) != len)
2960 if (lseek (fd, addr, SEEK_SET) == -1 || read (fd, myaddr, len) != len)
2970 /* Parse LINE as a signal set and add its set bits to SIGS. */
2973 add_line_to_sigset (const char *line, sigset_t *sigs)
2975 int len = strlen (line) - 1;
2979 if (line[len] != '\n')
2980 error (_("Could not parse signal set: %s"), line);
2988 if (*p >= '0' && *p <= '9')
2990 else if (*p >= 'a' && *p <= 'f')
2991 digit = *p - 'a' + 10;
2993 error (_("Could not parse signal set: %s"), line);
2998 sigaddset (sigs, signum + 1);
3000 sigaddset (sigs, signum + 2);
3002 sigaddset (sigs, signum + 3);
3004 sigaddset (sigs, signum + 4);
3010 /* Find process PID's pending signals from /proc/pid/status and set
3014 linux_proc_pending_signals (int pid, sigset_t *pending, sigset_t *blocked, sigset_t *ignored)
3017 char buffer[MAXPATHLEN], fname[MAXPATHLEN];
3020 sigemptyset (pending);
3021 sigemptyset (blocked);
3022 sigemptyset (ignored);
3023 sprintf (fname, "/proc/%d/status", pid);
3024 procfile = fopen (fname, "r");
3025 if (procfile == NULL)
3026 error (_("Could not open %s"), fname);
3028 while (fgets (buffer, MAXPATHLEN, procfile) != NULL)
3030 /* Normal queued signals are on the SigPnd line in the status
3031 file. However, 2.6 kernels also have a "shared" pending
3032 queue for delivering signals to a thread group, so check for
3035 Unfortunately some Red Hat kernels include the shared pending
3036 queue but not the ShdPnd status field. */
3038 if (strncmp (buffer, "SigPnd:\t", 8) == 0)
3039 add_line_to_sigset (buffer + 8, pending);
3040 else if (strncmp (buffer, "ShdPnd:\t", 8) == 0)
3041 add_line_to_sigset (buffer + 8, pending);
3042 else if (strncmp (buffer, "SigBlk:\t", 8) == 0)
3043 add_line_to_sigset (buffer + 8, blocked);
3044 else if (strncmp (buffer, "SigIgn:\t", 8) == 0)
3045 add_line_to_sigset (buffer + 8, ignored);
3052 _initialize_linux_nat (void)
3054 struct sigaction action;
3055 extern void thread_db_init (struct target_ops *);
3057 deprecated_child_ops.to_find_memory_regions = linux_nat_find_memory_regions;
3058 deprecated_child_ops.to_make_corefile_notes = linux_nat_make_corefile_notes;
3060 add_info ("proc", linux_nat_info_proc_cmd, _("\
3061 Show /proc process information about any running process.\n\
3062 Specify any process id, or use the program being debugged by default.\n\
3063 Specify any of the following keywords for detailed info:\n\
3064 mappings -- list of mapped memory regions.\n\
3065 stat -- list a bunch of random process info.\n\
3066 status -- list a different bunch of random process info.\n\
3067 all -- list all available /proc info."));
3069 init_linux_nat_ops ();
3070 add_target (&linux_nat_ops);
3071 thread_db_init (&linux_nat_ops);
3073 /* Save the original signal mask. */
3074 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
3076 action.sa_handler = sigchld_handler;
3077 sigemptyset (&action.sa_mask);
3078 action.sa_flags = 0;
3079 sigaction (SIGCHLD, &action, NULL);
3081 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3082 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
3083 sigdelset (&suspend_mask, SIGCHLD);
3085 sigemptyset (&blocked_mask);
3087 add_setshow_zinteger_cmd ("lin-lwp", no_class, &debug_linux_nat, _("\
3088 Set debugging of GNU/Linux lwp module."), _("\
3089 Show debugging of GNU/Linux lwp module."), _("\
3090 Enables printf debugging output."),
3092 NULL, /* FIXME: i18n: */
3093 &setdebuglist, &showdebuglist);
3097 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3098 the GNU/Linux Threads library and therefore doesn't really belong
3101 /* Read variable NAME in the target and return its value if found.
3102 Otherwise return zero. It is assumed that the type of the variable
3106 get_signo (const char *name)
3108 struct minimal_symbol *ms;
3111 ms = lookup_minimal_symbol (name, NULL, NULL);
3115 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
3116 sizeof (signo)) != 0)
3122 /* Return the set of signals used by the threads library in *SET. */
3125 lin_thread_get_thread_signals (sigset_t *set)
3127 struct sigaction action;
3128 int restart, cancel;
3132 restart = get_signo ("__pthread_sig_restart");
3136 cancel = get_signo ("__pthread_sig_cancel");
3140 sigaddset (set, restart);
3141 sigaddset (set, cancel);
3143 /* The GNU/Linux Threads library makes terminating threads send a
3144 special "cancel" signal instead of SIGCHLD. Make sure we catch
3145 those (to prevent them from terminating GDB itself, which is
3146 likely to be their default action) and treat them the same way as
3149 action.sa_handler = sigchld_handler;
3150 sigemptyset (&action.sa_mask);
3151 action.sa_flags = 0;
3152 sigaction (cancel, &action, NULL);
3154 /* We block the "cancel" signal throughout this code ... */
3155 sigaddset (&blocked_mask, cancel);
3156 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
3158 /* ... except during a sigsuspend. */
3159 sigdelset (&suspend_mask, cancel);