1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001-2015 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 3 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, see <http://www.gnu.org/licenses/>. */
24 #include "nat/linux-nat.h"
25 #include "nat/linux-waitpid.h"
27 #ifdef HAVE_TKILL_SYSCALL
29 #include <sys/syscall.h>
31 #include "nat/gdb_ptrace.h"
32 #include "linux-nat.h"
33 #include "nat/linux-ptrace.h"
34 #include "nat/linux-procfs.h"
35 #include "nat/linux-personality.h"
36 #include "linux-fork.h"
37 #include "gdbthread.h"
41 #include "inf-child.h"
42 #include "inf-ptrace.h"
44 #include <sys/procfs.h> /* for elf_gregset etc. */
45 #include "elf-bfd.h" /* for elfcore_write_* */
46 #include "gregset.h" /* for gregset */
47 #include "gdbcore.h" /* for get_exec_file */
48 #include <ctype.h> /* for isdigit */
49 #include <sys/stat.h> /* for struct stat */
50 #include <fcntl.h> /* for O_RDONLY */
52 #include "event-loop.h"
53 #include "event-top.h"
55 #include <sys/types.h>
57 #include "xml-support.h"
60 #include "nat/linux-osdata.h"
61 #include "linux-tdep.h"
64 #include "tracepoint.h"
66 #include "target-descriptions.h"
67 #include "filestuff.h"
69 #include "nat/linux-namespaces.h"
73 #define SPUFS_MAGIC 0x23c9b64e
76 /* This comment documents high-level logic of this file.
78 Waiting for events in sync mode
79 ===============================
81 When waiting for an event in a specific thread, we just use waitpid, passing
82 the specific pid, and not passing WNOHANG.
84 When waiting for an event in all threads, waitpid is not quite good. Prior to
85 version 2.4, Linux can either wait for event in main thread, or in secondary
86 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
87 miss an event. The solution is to use non-blocking waitpid, together with
88 sigsuspend. First, we use non-blocking waitpid to get an event in the main
89 process, if any. Second, we use non-blocking waitpid with the __WCLONED
90 flag to check for events in cloned processes. If nothing is found, we use
91 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
92 happened to a child process -- and SIGCHLD will be delivered both for events
93 in main debugged process and in cloned processes. As soon as we know there's
94 an event, we get back to calling nonblocking waitpid with and without
97 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
98 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
99 blocked, the signal becomes pending and sigsuspend immediately
100 notices it and returns.
102 Waiting for events in async mode
103 ================================
105 In async mode, GDB should always be ready to handle both user input
106 and target events, so neither blocking waitpid nor sigsuspend are
107 viable options. Instead, we should asynchronously notify the GDB main
108 event loop whenever there's an unprocessed event from the target. We
109 detect asynchronous target events by handling SIGCHLD signals. To
110 notify the event loop about target events, the self-pipe trick is used
111 --- a pipe is registered as waitable event source in the event loop,
112 the event loop select/poll's on the read end of this pipe (as well on
113 other event sources, e.g., stdin), and the SIGCHLD handler writes a
114 byte to this pipe. This is more portable than relying on
115 pselect/ppoll, since on kernels that lack those syscalls, libc
116 emulates them with select/poll+sigprocmask, and that is racy
117 (a.k.a. plain broken).
119 Obviously, if we fail to notify the event loop if there's a target
120 event, it's bad. OTOH, if we notify the event loop when there's no
121 event from the target, linux_nat_wait will detect that there's no real
122 event to report, and return event of type TARGET_WAITKIND_IGNORE.
123 This is mostly harmless, but it will waste time and is better avoided.
125 The main design point is that every time GDB is outside linux-nat.c,
126 we have a SIGCHLD handler installed that is called when something
127 happens to the target and notifies the GDB event loop. Whenever GDB
128 core decides to handle the event, and calls into linux-nat.c, we
129 process things as in sync mode, except that the we never block in
132 While processing an event, we may end up momentarily blocked in
133 waitpid calls. Those waitpid calls, while blocking, are guarantied to
134 return quickly. E.g., in all-stop mode, before reporting to the core
135 that an LWP hit a breakpoint, all LWPs are stopped by sending them
136 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
137 Note that this is different from blocking indefinitely waiting for the
138 next event --- here, we're already handling an event.
143 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
144 signal is not entirely significant; we just need for a signal to be delivered,
145 so that we can intercept it. SIGSTOP's advantage is that it can not be
146 blocked. A disadvantage is that it is not a real-time signal, so it can only
147 be queued once; we do not keep track of other sources of SIGSTOP.
149 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
150 use them, because they have special behavior when the signal is generated -
151 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
152 kills the entire thread group.
154 A delivered SIGSTOP would stop the entire thread group, not just the thread we
155 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
156 cancel it (by PTRACE_CONT without passing SIGSTOP).
158 We could use a real-time signal instead. This would solve those problems; we
159 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
160 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
161 generates it, and there are races with trying to find a signal that is not
165 #define O_LARGEFILE 0
168 /* Does the current host support PTRACE_GETREGSET? */
169 enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN;
171 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
172 the use of the multi-threaded target. */
173 static struct target_ops *linux_ops;
174 static struct target_ops linux_ops_saved;
176 /* The method to call, if any, when a new thread is attached. */
177 static void (*linux_nat_new_thread) (struct lwp_info *);
179 /* The method to call, if any, when a new fork is attached. */
180 static linux_nat_new_fork_ftype *linux_nat_new_fork;
182 /* The method to call, if any, when a process is no longer
184 static linux_nat_forget_process_ftype *linux_nat_forget_process_hook;
186 /* Hook to call prior to resuming a thread. */
187 static void (*linux_nat_prepare_to_resume) (struct lwp_info *);
189 /* The method to call, if any, when the siginfo object needs to be
190 converted between the layout returned by ptrace, and the layout in
191 the architecture of the inferior. */
192 static int (*linux_nat_siginfo_fixup) (siginfo_t *,
196 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
197 Called by our to_xfer_partial. */
198 static target_xfer_partial_ftype *super_xfer_partial;
200 /* The saved to_close method, inherited from inf-ptrace.c.
201 Called by our to_close. */
202 static void (*super_close) (struct target_ops *);
204 static unsigned int debug_linux_nat;
206 show_debug_linux_nat (struct ui_file *file, int from_tty,
207 struct cmd_list_element *c, const char *value)
209 fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
213 struct simple_pid_list
217 struct simple_pid_list *next;
219 struct simple_pid_list *stopped_pids;
221 /* Async mode support. */
223 /* The read/write ends of the pipe registered as waitable file in the
225 static int linux_nat_event_pipe[2] = { -1, -1 };
227 /* True if we're currently in async mode. */
228 #define linux_is_async_p() (linux_nat_event_pipe[0] != -1)
230 /* Flush the event pipe. */
233 async_file_flush (void)
240 ret = read (linux_nat_event_pipe[0], &buf, 1);
242 while (ret >= 0 || (ret == -1 && errno == EINTR));
245 /* Put something (anything, doesn't matter what, or how much) in event
246 pipe, so that the select/poll in the event-loop realizes we have
247 something to process. */
250 async_file_mark (void)
254 /* It doesn't really matter what the pipe contains, as long we end
255 up with something in it. Might as well flush the previous
261 ret = write (linux_nat_event_pipe[1], "+", 1);
263 while (ret == -1 && errno == EINTR);
265 /* Ignore EAGAIN. If the pipe is full, the event loop will already
266 be awakened anyway. */
269 static int kill_lwp (int lwpid, int signo);
271 static int stop_callback (struct lwp_info *lp, void *data);
272 static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
274 static void block_child_signals (sigset_t *prev_mask);
275 static void restore_child_signals_mask (sigset_t *prev_mask);
278 static struct lwp_info *add_lwp (ptid_t ptid);
279 static void purge_lwp_list (int pid);
280 static void delete_lwp (ptid_t ptid);
281 static struct lwp_info *find_lwp_pid (ptid_t ptid);
283 static int lwp_status_pending_p (struct lwp_info *lp);
285 static int check_stopped_by_breakpoint (struct lwp_info *lp);
286 static int sigtrap_is_event (int status);
287 static int (*linux_nat_status_is_event) (int status) = sigtrap_is_event;
292 /* See nat/linux-nat.h. */
295 ptid_of_lwp (struct lwp_info *lwp)
300 /* See nat/linux-nat.h. */
303 lwp_set_arch_private_info (struct lwp_info *lwp,
304 struct arch_lwp_info *info)
306 lwp->arch_private = info;
309 /* See nat/linux-nat.h. */
311 struct arch_lwp_info *
312 lwp_arch_private_info (struct lwp_info *lwp)
314 return lwp->arch_private;
317 /* See nat/linux-nat.h. */
320 lwp_is_stopped (struct lwp_info *lwp)
325 /* See nat/linux-nat.h. */
327 enum target_stop_reason
328 lwp_stop_reason (struct lwp_info *lwp)
330 return lwp->stop_reason;
334 /* Trivial list manipulation functions to keep track of a list of
335 new stopped processes. */
337 add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
339 struct simple_pid_list *new_pid = XNEW (struct simple_pid_list);
342 new_pid->status = status;
343 new_pid->next = *listp;
348 in_pid_list_p (struct simple_pid_list *list, int pid)
350 struct simple_pid_list *p;
352 for (p = list; p != NULL; p = p->next)
359 pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
361 struct simple_pid_list **p;
363 for (p = listp; *p != NULL; p = &(*p)->next)
364 if ((*p)->pid == pid)
366 struct simple_pid_list *next = (*p)->next;
368 *statusp = (*p)->status;
376 /* Return the ptrace options that we want to try to enable. */
379 linux_nat_ptrace_options (int attached)
384 options |= PTRACE_O_EXITKILL;
386 options |= (PTRACE_O_TRACESYSGOOD
387 | PTRACE_O_TRACEVFORKDONE
388 | PTRACE_O_TRACEVFORK
390 | PTRACE_O_TRACEEXEC);
395 /* Initialize ptrace warnings and check for supported ptrace
398 ATTACHED should be nonzero iff we attached to the inferior. */
401 linux_init_ptrace (pid_t pid, int attached)
403 int options = linux_nat_ptrace_options (attached);
405 linux_enable_event_reporting (pid, options);
406 linux_ptrace_init_warnings ();
410 linux_child_post_attach (struct target_ops *self, int pid)
412 linux_init_ptrace (pid, 1);
416 linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
418 linux_init_ptrace (ptid_get_pid (ptid), 0);
421 /* Return the number of known LWPs in the tgid given by PID. */
429 for (lp = lwp_list; lp; lp = lp->next)
430 if (ptid_get_pid (lp->ptid) == pid)
436 /* Call delete_lwp with prototype compatible for make_cleanup. */
439 delete_lwp_cleanup (void *lp_voidp)
441 struct lwp_info *lp = (struct lwp_info *) lp_voidp;
443 delete_lwp (lp->ptid);
446 /* Target hook for follow_fork. On entry inferior_ptid must be the
447 ptid of the followed inferior. At return, inferior_ptid will be
451 linux_child_follow_fork (struct target_ops *ops, int follow_child,
456 struct lwp_info *child_lp = NULL;
457 int status = W_STOPCODE (0);
458 struct cleanup *old_chain;
460 ptid_t parent_ptid, child_ptid;
461 int parent_pid, child_pid;
463 has_vforked = (inferior_thread ()->pending_follow.kind
464 == TARGET_WAITKIND_VFORKED);
465 parent_ptid = inferior_ptid;
466 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
467 parent_pid = ptid_get_lwp (parent_ptid);
468 child_pid = ptid_get_lwp (child_ptid);
470 /* We're already attached to the parent, by default. */
471 old_chain = save_inferior_ptid ();
472 inferior_ptid = child_ptid;
473 child_lp = add_lwp (inferior_ptid);
474 child_lp->stopped = 1;
475 child_lp->last_resume_kind = resume_stop;
477 /* Detach new forked process? */
480 make_cleanup (delete_lwp_cleanup, child_lp);
482 if (linux_nat_prepare_to_resume != NULL)
483 linux_nat_prepare_to_resume (child_lp);
485 /* When debugging an inferior in an architecture that supports
486 hardware single stepping on a kernel without commit
487 6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child
488 process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits
489 set if the parent process had them set.
490 To work around this, single step the child process
491 once before detaching to clear the flags. */
493 if (!gdbarch_software_single_step_p (target_thread_architecture
496 linux_disable_event_reporting (child_pid);
497 if (ptrace (PTRACE_SINGLESTEP, child_pid, 0, 0) < 0)
498 perror_with_name (_("Couldn't do single step"));
499 if (my_waitpid (child_pid, &status, 0) < 0)
500 perror_with_name (_("Couldn't wait vfork process"));
503 if (WIFSTOPPED (status))
507 signo = WSTOPSIG (status);
509 && !signal_pass_state (gdb_signal_from_host (signo)))
511 ptrace (PTRACE_DETACH, child_pid, 0, signo);
514 /* Resets value of inferior_ptid to parent ptid. */
515 do_cleanups (old_chain);
519 /* Let the thread_db layer learn about this new process. */
520 check_for_thread_db ();
523 do_cleanups (old_chain);
527 struct lwp_info *parent_lp;
529 parent_lp = find_lwp_pid (parent_ptid);
530 gdb_assert (linux_supports_tracefork () >= 0);
532 if (linux_supports_tracevforkdone ())
535 fprintf_unfiltered (gdb_stdlog,
536 "LCFF: waiting for VFORK_DONE on %d\n",
538 parent_lp->stopped = 1;
540 /* We'll handle the VFORK_DONE event like any other
541 event, in target_wait. */
545 /* We can't insert breakpoints until the child has
546 finished with the shared memory region. We need to
547 wait until that happens. Ideal would be to just
549 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
550 - waitpid (parent_pid, &status, __WALL);
551 However, most architectures can't handle a syscall
552 being traced on the way out if it wasn't traced on
555 We might also think to loop, continuing the child
556 until it exits or gets a SIGTRAP. One problem is
557 that the child might call ptrace with PTRACE_TRACEME.
559 There's no simple and reliable way to figure out when
560 the vforked child will be done with its copy of the
561 shared memory. We could step it out of the syscall,
562 two instructions, let it go, and then single-step the
563 parent once. When we have hardware single-step, this
564 would work; with software single-step it could still
565 be made to work but we'd have to be able to insert
566 single-step breakpoints in the child, and we'd have
567 to insert -just- the single-step breakpoint in the
568 parent. Very awkward.
570 In the end, the best we can do is to make sure it
571 runs for a little while. Hopefully it will be out of
572 range of any breakpoints we reinsert. Usually this
573 is only the single-step breakpoint at vfork's return
577 fprintf_unfiltered (gdb_stdlog,
578 "LCFF: no VFORK_DONE "
579 "support, sleeping a bit\n");
583 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
584 and leave it pending. The next linux_nat_resume call
585 will notice a pending event, and bypasses actually
586 resuming the inferior. */
587 parent_lp->status = 0;
588 parent_lp->waitstatus.kind = TARGET_WAITKIND_VFORK_DONE;
589 parent_lp->stopped = 1;
591 /* If we're in async mode, need to tell the event loop
592 there's something here to process. */
593 if (target_is_async_p ())
600 struct lwp_info *child_lp;
602 child_lp = add_lwp (inferior_ptid);
603 child_lp->stopped = 1;
604 child_lp->last_resume_kind = resume_stop;
606 /* Let the thread_db layer learn about this new process. */
607 check_for_thread_db ();
615 linux_child_insert_fork_catchpoint (struct target_ops *self, int pid)
617 return !linux_supports_tracefork ();
621 linux_child_remove_fork_catchpoint (struct target_ops *self, int pid)
627 linux_child_insert_vfork_catchpoint (struct target_ops *self, int pid)
629 return !linux_supports_tracefork ();
633 linux_child_remove_vfork_catchpoint (struct target_ops *self, int pid)
639 linux_child_insert_exec_catchpoint (struct target_ops *self, int pid)
641 return !linux_supports_tracefork ();
645 linux_child_remove_exec_catchpoint (struct target_ops *self, int pid)
651 linux_child_set_syscall_catchpoint (struct target_ops *self,
652 int pid, int needed, int any_count,
653 int table_size, int *table)
655 if (!linux_supports_tracesysgood ())
658 /* On GNU/Linux, we ignore the arguments. It means that we only
659 enable the syscall catchpoints, but do not disable them.
661 Also, we do not use the `table' information because we do not
662 filter system calls here. We let GDB do the logic for us. */
666 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
667 are processes sharing the same VM space. A multi-threaded process
668 is basically a group of such processes. However, such a grouping
669 is almost entirely a user-space issue; the kernel doesn't enforce
670 such a grouping at all (this might change in the future). In
671 general, we'll rely on the threads library (i.e. the GNU/Linux
672 Threads library) to provide such a grouping.
674 It is perfectly well possible to write a multi-threaded application
675 without the assistance of a threads library, by using the clone
676 system call directly. This module should be able to give some
677 rudimentary support for debugging such applications if developers
678 specify the CLONE_PTRACE flag in the clone system call, and are
679 using the Linux kernel 2.4 or above.
681 Note that there are some peculiarities in GNU/Linux that affect
684 - In general one should specify the __WCLONE flag to waitpid in
685 order to make it report events for any of the cloned processes
686 (and leave it out for the initial process). However, if a cloned
687 process has exited the exit status is only reported if the
688 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
689 we cannot use it since GDB must work on older systems too.
691 - When a traced, cloned process exits and is waited for by the
692 debugger, the kernel reassigns it to the original parent and
693 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
694 library doesn't notice this, which leads to the "zombie problem":
695 When debugged a multi-threaded process that spawns a lot of
696 threads will run out of processes, even if the threads exit,
697 because the "zombies" stay around. */
699 /* List of known LWPs. */
700 struct lwp_info *lwp_list;
703 /* Original signal mask. */
704 static sigset_t normal_mask;
706 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
707 _initialize_linux_nat. */
708 static sigset_t suspend_mask;
710 /* Signals to block to make that sigsuspend work. */
711 static sigset_t blocked_mask;
713 /* SIGCHLD action. */
714 struct sigaction sigchld_action;
716 /* Block child signals (SIGCHLD and linux threads signals), and store
717 the previous mask in PREV_MASK. */
720 block_child_signals (sigset_t *prev_mask)
722 /* Make sure SIGCHLD is blocked. */
723 if (!sigismember (&blocked_mask, SIGCHLD))
724 sigaddset (&blocked_mask, SIGCHLD);
726 sigprocmask (SIG_BLOCK, &blocked_mask, prev_mask);
729 /* Restore child signals mask, previously returned by
730 block_child_signals. */
733 restore_child_signals_mask (sigset_t *prev_mask)
735 sigprocmask (SIG_SETMASK, prev_mask, NULL);
738 /* Mask of signals to pass directly to the inferior. */
739 static sigset_t pass_mask;
741 /* Update signals to pass to the inferior. */
743 linux_nat_pass_signals (struct target_ops *self,
744 int numsigs, unsigned char *pass_signals)
748 sigemptyset (&pass_mask);
750 for (signo = 1; signo < NSIG; signo++)
752 int target_signo = gdb_signal_from_host (signo);
753 if (target_signo < numsigs && pass_signals[target_signo])
754 sigaddset (&pass_mask, signo);
760 /* Prototypes for local functions. */
761 static int stop_wait_callback (struct lwp_info *lp, void *data);
762 static int linux_thread_alive (ptid_t ptid);
763 static char *linux_child_pid_to_exec_file (struct target_ops *self, int pid);
764 static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
768 /* Destroy and free LP. */
771 lwp_free (struct lwp_info *lp)
773 xfree (lp->arch_private);
777 /* Remove all LWPs belong to PID from the lwp list. */
780 purge_lwp_list (int pid)
782 struct lwp_info *lp, *lpprev, *lpnext;
786 for (lp = lwp_list; lp; lp = lpnext)
790 if (ptid_get_pid (lp->ptid) == pid)
795 lpprev->next = lp->next;
804 /* Add the LWP specified by PTID to the list. PTID is the first LWP
805 in the process. Return a pointer to the structure describing the
808 This differs from add_lwp in that we don't let the arch specific
809 bits know about this new thread. Current clients of this callback
810 take the opportunity to install watchpoints in the new thread, and
811 we shouldn't do that for the first thread. If we're spawning a
812 child ("run"), the thread executes the shell wrapper first, and we
813 shouldn't touch it until it execs the program we want to debug.
814 For "attach", it'd be okay to call the callback, but it's not
815 necessary, because watchpoints can't yet have been inserted into
818 static struct lwp_info *
819 add_initial_lwp (ptid_t ptid)
823 gdb_assert (ptid_lwp_p (ptid));
825 lp = XNEW (struct lwp_info);
827 memset (lp, 0, sizeof (struct lwp_info));
829 lp->last_resume_kind = resume_continue;
830 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
841 /* Add the LWP specified by PID to the list. Return a pointer to the
842 structure describing the new LWP. The LWP should already be
845 static struct lwp_info *
846 add_lwp (ptid_t ptid)
850 lp = add_initial_lwp (ptid);
852 /* Let the arch specific bits know about this new thread. Current
853 clients of this callback take the opportunity to install
854 watchpoints in the new thread. We don't do this for the first
855 thread though. See add_initial_lwp. */
856 if (linux_nat_new_thread != NULL)
857 linux_nat_new_thread (lp);
862 /* Remove the LWP specified by PID from the list. */
865 delete_lwp (ptid_t ptid)
867 struct lwp_info *lp, *lpprev;
871 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
872 if (ptid_equal (lp->ptid, ptid))
879 lpprev->next = lp->next;
886 /* Return a pointer to the structure describing the LWP corresponding
887 to PID. If no corresponding LWP could be found, return NULL. */
889 static struct lwp_info *
890 find_lwp_pid (ptid_t ptid)
895 if (ptid_lwp_p (ptid))
896 lwp = ptid_get_lwp (ptid);
898 lwp = ptid_get_pid (ptid);
900 for (lp = lwp_list; lp; lp = lp->next)
901 if (lwp == ptid_get_lwp (lp->ptid))
907 /* See nat/linux-nat.h. */
910 iterate_over_lwps (ptid_t filter,
911 iterate_over_lwps_ftype callback,
914 struct lwp_info *lp, *lpnext;
916 for (lp = lwp_list; lp; lp = lpnext)
920 if (ptid_match (lp->ptid, filter))
922 if ((*callback) (lp, data) != 0)
930 /* Update our internal state when changing from one checkpoint to
931 another indicated by NEW_PTID. We can only switch single-threaded
932 applications, so we only create one new LWP, and the previous list
936 linux_nat_switch_fork (ptid_t new_ptid)
940 purge_lwp_list (ptid_get_pid (inferior_ptid));
942 lp = add_lwp (new_ptid);
945 /* This changes the thread's ptid while preserving the gdb thread
946 num. Also changes the inferior pid, while preserving the
948 thread_change_ptid (inferior_ptid, new_ptid);
950 /* We've just told GDB core that the thread changed target id, but,
951 in fact, it really is a different thread, with different register
953 registers_changed ();
956 /* Handle the exit of a single thread LP. */
959 exit_lwp (struct lwp_info *lp)
961 struct thread_info *th = find_thread_ptid (lp->ptid);
965 if (print_thread_events)
966 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid));
968 delete_thread (lp->ptid);
971 delete_lwp (lp->ptid);
974 /* Wait for the LWP specified by LP, which we have just attached to.
975 Returns a wait status for that LWP, to cache. */
978 linux_nat_post_attach_wait (ptid_t ptid, int first, int *cloned,
981 pid_t new_pid, pid = ptid_get_lwp (ptid);
984 if (linux_proc_pid_is_stopped (pid))
987 fprintf_unfiltered (gdb_stdlog,
988 "LNPAW: Attaching to a stopped process\n");
990 /* The process is definitely stopped. It is in a job control
991 stop, unless the kernel predates the TASK_STOPPED /
992 TASK_TRACED distinction, in which case it might be in a
993 ptrace stop. Make sure it is in a ptrace stop; from there we
994 can kill it, signal it, et cetera.
996 First make sure there is a pending SIGSTOP. Since we are
997 already attached, the process can not transition from stopped
998 to running without a PTRACE_CONT; so we know this signal will
999 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1000 probably already in the queue (unless this kernel is old
1001 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1002 is not an RT signal, it can only be queued once. */
1003 kill_lwp (pid, SIGSTOP);
1005 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1006 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1007 ptrace (PTRACE_CONT, pid, 0, 0);
1010 /* Make sure the initial process is stopped. The user-level threads
1011 layer might want to poke around in the inferior, and that won't
1012 work if things haven't stabilized yet. */
1013 new_pid = my_waitpid (pid, &status, 0);
1014 if (new_pid == -1 && errno == ECHILD)
1017 warning (_("%s is a cloned process"), target_pid_to_str (ptid));
1019 /* Try again with __WCLONE to check cloned processes. */
1020 new_pid = my_waitpid (pid, &status, __WCLONE);
1024 gdb_assert (pid == new_pid);
1026 if (!WIFSTOPPED (status))
1028 /* The pid we tried to attach has apparently just exited. */
1029 if (debug_linux_nat)
1030 fprintf_unfiltered (gdb_stdlog, "LNPAW: Failed to stop %d: %s",
1031 pid, status_to_str (status));
1035 if (WSTOPSIG (status) != SIGSTOP)
1038 if (debug_linux_nat)
1039 fprintf_unfiltered (gdb_stdlog,
1040 "LNPAW: Received %s after attaching\n",
1041 status_to_str (status));
1047 /* Attach to the LWP specified by PID. Return 0 if successful, -1 if
1048 the new LWP could not be attached, or 1 if we're already auto
1049 attached to this thread, but haven't processed the
1050 PTRACE_EVENT_CLONE event of its parent thread, so we just ignore
1051 its existance, without considering it an error. */
1054 lin_lwp_attach_lwp (ptid_t ptid)
1056 struct lwp_info *lp;
1059 gdb_assert (ptid_lwp_p (ptid));
1061 lp = find_lwp_pid (ptid);
1062 lwpid = ptid_get_lwp (ptid);
1064 /* We assume that we're already attached to any LWP that is already
1065 in our list of LWPs. If we're not seeing exit events from threads
1066 and we've had PID wraparound since we last tried to stop all threads,
1067 this assumption might be wrong; fortunately, this is very unlikely
1071 int status, cloned = 0, signalled = 0;
1073 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0)
1075 if (linux_supports_tracefork ())
1077 /* If we haven't stopped all threads when we get here,
1078 we may have seen a thread listed in thread_db's list,
1079 but not processed the PTRACE_EVENT_CLONE yet. If
1080 that's the case, ignore this new thread, and let
1081 normal event handling discover it later. */
1082 if (in_pid_list_p (stopped_pids, lwpid))
1084 /* We've already seen this thread stop, but we
1085 haven't seen the PTRACE_EVENT_CLONE extended
1087 if (debug_linux_nat)
1088 fprintf_unfiltered (gdb_stdlog,
1089 "LLAL: attach failed, but already seen "
1090 "this thread %s stop\n",
1091 target_pid_to_str (ptid));
1099 if (debug_linux_nat)
1100 fprintf_unfiltered (gdb_stdlog,
1101 "LLAL: attach failed, and haven't seen "
1102 "this thread %s stop yet\n",
1103 target_pid_to_str (ptid));
1105 /* We may or may not be attached to the LWP already.
1106 Try waitpid on it. If that errors, we're not
1107 attached to the LWP yet. Otherwise, we're
1108 already attached. */
1109 gdb_assert (lwpid > 0);
1110 new_pid = my_waitpid (lwpid, &status, WNOHANG);
1111 if (new_pid == -1 && errno == ECHILD)
1112 new_pid = my_waitpid (lwpid, &status, __WCLONE | WNOHANG);
1117 /* The child hasn't stopped for its initial
1118 SIGSTOP stop yet. */
1119 if (debug_linux_nat)
1120 fprintf_unfiltered (gdb_stdlog,
1121 "LLAL: child hasn't "
1124 else if (WIFSTOPPED (status))
1126 if (debug_linux_nat)
1127 fprintf_unfiltered (gdb_stdlog,
1128 "LLAL: adding to stopped_pids\n");
1129 add_to_pid_list (&stopped_pids, lwpid, status);
1136 /* If we fail to attach to the thread, issue a warning,
1137 but continue. One way this can happen is if thread
1138 creation is interrupted; as of Linux kernel 2.6.19, a
1139 bug may place threads in the thread list and then fail
1141 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid),
1142 safe_strerror (errno));
1146 if (debug_linux_nat)
1147 fprintf_unfiltered (gdb_stdlog,
1148 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1149 target_pid_to_str (ptid));
1151 status = linux_nat_post_attach_wait (ptid, 0, &cloned, &signalled);
1152 if (!WIFSTOPPED (status))
1155 lp = add_lwp (ptid);
1157 lp->last_resume_kind = resume_stop;
1158 lp->cloned = cloned;
1159 lp->signalled = signalled;
1160 if (WSTOPSIG (status) != SIGSTOP)
1163 lp->status = status;
1166 target_post_attach (ptid_get_lwp (lp->ptid));
1168 if (debug_linux_nat)
1170 fprintf_unfiltered (gdb_stdlog,
1171 "LLAL: waitpid %s received %s\n",
1172 target_pid_to_str (ptid),
1173 status_to_str (status));
1181 linux_nat_create_inferior (struct target_ops *ops,
1182 char *exec_file, char *allargs, char **env,
1185 struct cleanup *restore_personality
1186 = maybe_disable_address_space_randomization (disable_randomization);
1188 /* The fork_child mechanism is synchronous and calls target_wait, so
1189 we have to mask the async mode. */
1191 /* Make sure we report all signals during startup. */
1192 linux_nat_pass_signals (ops, 0, NULL);
1194 linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
1196 do_cleanups (restore_personality);
1199 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1200 already attached. Returns true if a new LWP is found, false
1204 attach_proc_task_lwp_callback (ptid_t ptid)
1206 struct lwp_info *lp;
1208 /* Ignore LWPs we're already attached to. */
1209 lp = find_lwp_pid (ptid);
1212 int lwpid = ptid_get_lwp (ptid);
1214 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0)
1218 /* Be quiet if we simply raced with the thread exiting.
1219 EPERM is returned if the thread's task still exists, and
1220 is marked as exited or zombie, as well as other
1221 conditions, so in that case, confirm the status in
1222 /proc/PID/status. */
1224 || (err == EPERM && linux_proc_pid_is_gone (lwpid)))
1226 if (debug_linux_nat)
1228 fprintf_unfiltered (gdb_stdlog,
1229 "Cannot attach to lwp %d: "
1230 "thread is gone (%d: %s)\n",
1231 lwpid, err, safe_strerror (err));
1236 warning (_("Cannot attach to lwp %d: %s"),
1238 linux_ptrace_attach_fail_reason_string (ptid,
1244 if (debug_linux_nat)
1245 fprintf_unfiltered (gdb_stdlog,
1246 "PTRACE_ATTACH %s, 0, 0 (OK)\n",
1247 target_pid_to_str (ptid));
1249 lp = add_lwp (ptid);
1252 /* The next time we wait for this LWP we'll see a SIGSTOP as
1253 PTRACE_ATTACH brings it to a halt. */
1256 /* We need to wait for a stop before being able to make the
1257 next ptrace call on this LWP. */
1258 lp->must_set_ptrace_flags = 1;
1267 linux_nat_attach (struct target_ops *ops, const char *args, int from_tty)
1269 struct lwp_info *lp;
1273 /* Make sure we report all signals during attach. */
1274 linux_nat_pass_signals (ops, 0, NULL);
1278 linux_ops->to_attach (ops, args, from_tty);
1280 CATCH (ex, RETURN_MASK_ERROR)
1282 pid_t pid = parse_pid_to_attach (args);
1283 struct buffer buffer;
1284 char *message, *buffer_s;
1286 message = xstrdup (ex.message);
1287 make_cleanup (xfree, message);
1289 buffer_init (&buffer);
1290 linux_ptrace_attach_fail_reason (pid, &buffer);
1292 buffer_grow_str0 (&buffer, "");
1293 buffer_s = buffer_finish (&buffer);
1294 make_cleanup (xfree, buffer_s);
1296 if (*buffer_s != '\0')
1297 throw_error (ex.error, "warning: %s\n%s", buffer_s, message);
1299 throw_error (ex.error, "%s", message);
1303 /* The ptrace base target adds the main thread with (pid,0,0)
1304 format. Decorate it with lwp info. */
1305 ptid = ptid_build (ptid_get_pid (inferior_ptid),
1306 ptid_get_pid (inferior_ptid),
1308 thread_change_ptid (inferior_ptid, ptid);
1310 /* Add the initial process as the first LWP to the list. */
1311 lp = add_initial_lwp (ptid);
1313 status = linux_nat_post_attach_wait (lp->ptid, 1, &lp->cloned,
1315 if (!WIFSTOPPED (status))
1317 if (WIFEXITED (status))
1319 int exit_code = WEXITSTATUS (status);
1321 target_terminal_ours ();
1322 target_mourn_inferior ();
1324 error (_("Unable to attach: program exited normally."));
1326 error (_("Unable to attach: program exited with code %d."),
1329 else if (WIFSIGNALED (status))
1331 enum gdb_signal signo;
1333 target_terminal_ours ();
1334 target_mourn_inferior ();
1336 signo = gdb_signal_from_host (WTERMSIG (status));
1337 error (_("Unable to attach: program terminated with signal "
1339 gdb_signal_to_name (signo),
1340 gdb_signal_to_string (signo));
1343 internal_error (__FILE__, __LINE__,
1344 _("unexpected status %d for PID %ld"),
1345 status, (long) ptid_get_lwp (ptid));
1350 /* Save the wait status to report later. */
1352 if (debug_linux_nat)
1353 fprintf_unfiltered (gdb_stdlog,
1354 "LNA: waitpid %ld, saving status %s\n",
1355 (long) ptid_get_pid (lp->ptid), status_to_str (status));
1357 lp->status = status;
1359 /* We must attach to every LWP. If /proc is mounted, use that to
1360 find them now. The inferior may be using raw clone instead of
1361 using pthreads. But even if it is using pthreads, thread_db
1362 walks structures in the inferior's address space to find the list
1363 of threads/LWPs, and those structures may well be corrupted.
1364 Note that once thread_db is loaded, we'll still use it to list
1365 threads and associate pthread info with each LWP. */
1366 linux_proc_attach_tgid_threads (ptid_get_pid (lp->ptid),
1367 attach_proc_task_lwp_callback);
1369 if (target_can_async_p ())
1373 /* Get pending status of LP. */
1375 get_pending_status (struct lwp_info *lp, int *status)
1377 enum gdb_signal signo = GDB_SIGNAL_0;
1379 /* If we paused threads momentarily, we may have stored pending
1380 events in lp->status or lp->waitstatus (see stop_wait_callback),
1381 and GDB core hasn't seen any signal for those threads.
1382 Otherwise, the last signal reported to the core is found in the
1383 thread object's stop_signal.
1385 There's a corner case that isn't handled here at present. Only
1386 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1387 stop_signal make sense as a real signal to pass to the inferior.
1388 Some catchpoint related events, like
1389 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1390 to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1391 those traps are debug API (ptrace in our case) related and
1392 induced; the inferior wouldn't see them if it wasn't being
1393 traced. Hence, we should never pass them to the inferior, even
1394 when set to pass state. Since this corner case isn't handled by
1395 infrun.c when proceeding with a signal, for consistency, neither
1396 do we handle it here (or elsewhere in the file we check for
1397 signal pass state). Normally SIGTRAP isn't set to pass state, so
1398 this is really a corner case. */
1400 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
1401 signo = GDB_SIGNAL_0; /* a pending ptrace event, not a real signal. */
1402 else if (lp->status)
1403 signo = gdb_signal_from_host (WSTOPSIG (lp->status));
1404 else if (target_is_non_stop_p () && !is_executing (lp->ptid))
1406 struct thread_info *tp = find_thread_ptid (lp->ptid);
1408 signo = tp->suspend.stop_signal;
1410 else if (!target_is_non_stop_p ())
1412 struct target_waitstatus last;
1415 get_last_target_status (&last_ptid, &last);
1417 if (ptid_get_lwp (lp->ptid) == ptid_get_lwp (last_ptid))
1419 struct thread_info *tp = find_thread_ptid (lp->ptid);
1421 signo = tp->suspend.stop_signal;
1427 if (signo == GDB_SIGNAL_0)
1429 if (debug_linux_nat)
1430 fprintf_unfiltered (gdb_stdlog,
1431 "GPT: lwp %s has no pending signal\n",
1432 target_pid_to_str (lp->ptid));
1434 else if (!signal_pass_state (signo))
1436 if (debug_linux_nat)
1437 fprintf_unfiltered (gdb_stdlog,
1438 "GPT: lwp %s had signal %s, "
1439 "but it is in no pass state\n",
1440 target_pid_to_str (lp->ptid),
1441 gdb_signal_to_string (signo));
1445 *status = W_STOPCODE (gdb_signal_to_host (signo));
1447 if (debug_linux_nat)
1448 fprintf_unfiltered (gdb_stdlog,
1449 "GPT: lwp %s has pending signal %s\n",
1450 target_pid_to_str (lp->ptid),
1451 gdb_signal_to_string (signo));
1458 detach_callback (struct lwp_info *lp, void *data)
1460 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1462 if (debug_linux_nat && lp->status)
1463 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
1464 strsignal (WSTOPSIG (lp->status)),
1465 target_pid_to_str (lp->ptid));
1467 /* If there is a pending SIGSTOP, get rid of it. */
1470 if (debug_linux_nat)
1471 fprintf_unfiltered (gdb_stdlog,
1472 "DC: Sending SIGCONT to %s\n",
1473 target_pid_to_str (lp->ptid));
1475 kill_lwp (ptid_get_lwp (lp->ptid), SIGCONT);
1479 /* We don't actually detach from the LWP that has an id equal to the
1480 overall process id just yet. */
1481 if (ptid_get_lwp (lp->ptid) != ptid_get_pid (lp->ptid))
1485 /* Pass on any pending signal for this LWP. */
1486 get_pending_status (lp, &status);
1488 if (linux_nat_prepare_to_resume != NULL)
1489 linux_nat_prepare_to_resume (lp);
1491 if (ptrace (PTRACE_DETACH, ptid_get_lwp (lp->ptid), 0,
1492 WSTOPSIG (status)) < 0)
1493 error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
1494 safe_strerror (errno));
1496 if (debug_linux_nat)
1497 fprintf_unfiltered (gdb_stdlog,
1498 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1499 target_pid_to_str (lp->ptid),
1500 strsignal (WSTOPSIG (status)));
1502 delete_lwp (lp->ptid);
1509 linux_nat_detach (struct target_ops *ops, const char *args, int from_tty)
1513 struct lwp_info *main_lwp;
1515 pid = ptid_get_pid (inferior_ptid);
1517 /* Don't unregister from the event loop, as there may be other
1518 inferiors running. */
1520 /* Stop all threads before detaching. ptrace requires that the
1521 thread is stopped to sucessfully detach. */
1522 iterate_over_lwps (pid_to_ptid (pid), stop_callback, NULL);
1523 /* ... and wait until all of them have reported back that
1524 they're no longer running. */
1525 iterate_over_lwps (pid_to_ptid (pid), stop_wait_callback, NULL);
1527 iterate_over_lwps (pid_to_ptid (pid), detach_callback, NULL);
1529 /* Only the initial process should be left right now. */
1530 gdb_assert (num_lwps (ptid_get_pid (inferior_ptid)) == 1);
1532 main_lwp = find_lwp_pid (pid_to_ptid (pid));
1534 /* Pass on any pending signal for the last LWP. */
1535 if ((args == NULL || *args == '\0')
1536 && get_pending_status (main_lwp, &status) != -1
1537 && WIFSTOPPED (status))
1541 /* Put the signal number in ARGS so that inf_ptrace_detach will
1542 pass it along with PTRACE_DETACH. */
1543 tem = (char *) alloca (8);
1544 xsnprintf (tem, 8, "%d", (int) WSTOPSIG (status));
1546 if (debug_linux_nat)
1547 fprintf_unfiltered (gdb_stdlog,
1548 "LND: Sending signal %s to %s\n",
1550 target_pid_to_str (main_lwp->ptid));
1553 if (linux_nat_prepare_to_resume != NULL)
1554 linux_nat_prepare_to_resume (main_lwp);
1555 delete_lwp (main_lwp->ptid);
1557 if (forks_exist_p ())
1559 /* Multi-fork case. The current inferior_ptid is being detached
1560 from, but there are other viable forks to debug. Detach from
1561 the current fork, and context-switch to the first
1563 linux_fork_detach (args, from_tty);
1566 linux_ops->to_detach (ops, args, from_tty);
1569 /* Resume execution of the inferior process. If STEP is nonzero,
1570 single-step it. If SIGNAL is nonzero, give it that signal. */
1573 linux_resume_one_lwp_throw (struct lwp_info *lp, int step,
1574 enum gdb_signal signo)
1578 /* stop_pc doubles as the PC the LWP had when it was last resumed.
1579 We only presently need that if the LWP is stepped though (to
1580 handle the case of stepping a breakpoint instruction). */
1583 struct regcache *regcache = get_thread_regcache (lp->ptid);
1585 lp->stop_pc = regcache_read_pc (regcache);
1590 if (linux_nat_prepare_to_resume != NULL)
1591 linux_nat_prepare_to_resume (lp);
1592 linux_ops->to_resume (linux_ops, lp->ptid, step, signo);
1594 /* Successfully resumed. Clear state that no longer makes sense,
1595 and mark the LWP as running. Must not do this before resuming
1596 otherwise if that fails other code will be confused. E.g., we'd
1597 later try to stop the LWP and hang forever waiting for a stop
1598 status. Note that we must not throw after this is cleared,
1599 otherwise handle_zombie_lwp_error would get confused. */
1601 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
1602 registers_changed_ptid (lp->ptid);
1605 /* Called when we try to resume a stopped LWP and that errors out. If
1606 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
1607 or about to become), discard the error, clear any pending status
1608 the LWP may have, and return true (we'll collect the exit status
1609 soon enough). Otherwise, return false. */
1612 check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
1614 /* If we get an error after resuming the LWP successfully, we'd
1615 confuse !T state for the LWP being gone. */
1616 gdb_assert (lp->stopped);
1618 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
1619 because even if ptrace failed with ESRCH, the tracee may be "not
1620 yet fully dead", but already refusing ptrace requests. In that
1621 case the tracee has 'R (Running)' state for a little bit
1622 (observed in Linux 3.18). See also the note on ESRCH in the
1623 ptrace(2) man page. Instead, check whether the LWP has any state
1624 other than ptrace-stopped. */
1626 /* Don't assume anything if /proc/PID/status can't be read. */
1627 if (linux_proc_pid_is_trace_stopped_nowarn (ptid_get_lwp (lp->ptid)) == 0)
1629 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
1631 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1637 /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
1638 disappears while we try to resume it. */
1641 linux_resume_one_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
1645 linux_resume_one_lwp_throw (lp, step, signo);
1647 CATCH (ex, RETURN_MASK_ERROR)
1649 if (!check_ptrace_stopped_lwp_gone (lp))
1650 throw_exception (ex);
1658 resume_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
1662 struct inferior *inf = find_inferior_ptid (lp->ptid);
1664 if (inf->vfork_child != NULL)
1666 if (debug_linux_nat)
1667 fprintf_unfiltered (gdb_stdlog,
1668 "RC: Not resuming %s (vfork parent)\n",
1669 target_pid_to_str (lp->ptid));
1671 else if (!lwp_status_pending_p (lp))
1673 if (debug_linux_nat)
1674 fprintf_unfiltered (gdb_stdlog,
1675 "RC: Resuming sibling %s, %s, %s\n",
1676 target_pid_to_str (lp->ptid),
1677 (signo != GDB_SIGNAL_0
1678 ? strsignal (gdb_signal_to_host (signo))
1680 step ? "step" : "resume");
1682 linux_resume_one_lwp (lp, step, signo);
1686 if (debug_linux_nat)
1687 fprintf_unfiltered (gdb_stdlog,
1688 "RC: Not resuming sibling %s (has pending)\n",
1689 target_pid_to_str (lp->ptid));
1694 if (debug_linux_nat)
1695 fprintf_unfiltered (gdb_stdlog,
1696 "RC: Not resuming sibling %s (not stopped)\n",
1697 target_pid_to_str (lp->ptid));
1701 /* Callback for iterate_over_lwps. If LWP is EXCEPT, do nothing.
1702 Resume LWP with the last stop signal, if it is in pass state. */
1705 linux_nat_resume_callback (struct lwp_info *lp, void *except)
1707 enum gdb_signal signo = GDB_SIGNAL_0;
1714 struct thread_info *thread;
1716 thread = find_thread_ptid (lp->ptid);
1719 signo = thread->suspend.stop_signal;
1720 thread->suspend.stop_signal = GDB_SIGNAL_0;
1724 resume_lwp (lp, 0, signo);
1729 resume_clear_callback (struct lwp_info *lp, void *data)
1732 lp->last_resume_kind = resume_stop;
1737 resume_set_callback (struct lwp_info *lp, void *data)
1740 lp->last_resume_kind = resume_continue;
1745 linux_nat_resume (struct target_ops *ops,
1746 ptid_t ptid, int step, enum gdb_signal signo)
1748 struct lwp_info *lp;
1751 if (debug_linux_nat)
1752 fprintf_unfiltered (gdb_stdlog,
1753 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1754 step ? "step" : "resume",
1755 target_pid_to_str (ptid),
1756 (signo != GDB_SIGNAL_0
1757 ? strsignal (gdb_signal_to_host (signo)) : "0"),
1758 target_pid_to_str (inferior_ptid));
1760 /* A specific PTID means `step only this process id'. */
1761 resume_many = (ptid_equal (minus_one_ptid, ptid)
1762 || ptid_is_pid (ptid));
1764 /* Mark the lwps we're resuming as resumed. */
1765 iterate_over_lwps (ptid, resume_set_callback, NULL);
1767 /* See if it's the current inferior that should be handled
1770 lp = find_lwp_pid (inferior_ptid);
1772 lp = find_lwp_pid (ptid);
1773 gdb_assert (lp != NULL);
1775 /* Remember if we're stepping. */
1776 lp->last_resume_kind = step ? resume_step : resume_continue;
1778 /* If we have a pending wait status for this thread, there is no
1779 point in resuming the process. But first make sure that
1780 linux_nat_wait won't preemptively handle the event - we
1781 should never take this short-circuit if we are going to
1782 leave LP running, since we have skipped resuming all the
1783 other threads. This bit of code needs to be synchronized
1784 with linux_nat_wait. */
1786 if (lp->status && WIFSTOPPED (lp->status))
1789 && WSTOPSIG (lp->status)
1790 && sigismember (&pass_mask, WSTOPSIG (lp->status)))
1792 if (debug_linux_nat)
1793 fprintf_unfiltered (gdb_stdlog,
1794 "LLR: Not short circuiting for ignored "
1795 "status 0x%x\n", lp->status);
1797 /* FIXME: What should we do if we are supposed to continue
1798 this thread with a signal? */
1799 gdb_assert (signo == GDB_SIGNAL_0);
1800 signo = gdb_signal_from_host (WSTOPSIG (lp->status));
1805 if (lwp_status_pending_p (lp))
1807 /* FIXME: What should we do if we are supposed to continue
1808 this thread with a signal? */
1809 gdb_assert (signo == GDB_SIGNAL_0);
1811 if (debug_linux_nat)
1812 fprintf_unfiltered (gdb_stdlog,
1813 "LLR: Short circuiting for status 0x%x\n",
1816 if (target_can_async_p ())
1819 /* Tell the event loop we have something to process. */
1826 iterate_over_lwps (ptid, linux_nat_resume_callback, lp);
1828 if (debug_linux_nat)
1829 fprintf_unfiltered (gdb_stdlog,
1830 "LLR: %s %s, %s (resume event thread)\n",
1831 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1832 target_pid_to_str (lp->ptid),
1833 (signo != GDB_SIGNAL_0
1834 ? strsignal (gdb_signal_to_host (signo)) : "0"));
1836 linux_resume_one_lwp (lp, step, signo);
1838 if (target_can_async_p ())
1842 /* Send a signal to an LWP. */
1845 kill_lwp (int lwpid, int signo)
1847 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1848 fails, then we are not using nptl threads and we should be using kill. */
1850 #ifdef HAVE_TKILL_SYSCALL
1852 static int tkill_failed;
1859 ret = syscall (__NR_tkill, lwpid, signo);
1860 if (errno != ENOSYS)
1867 return kill (lwpid, signo);
1870 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
1871 event, check if the core is interested in it: if not, ignore the
1872 event, and keep waiting; otherwise, we need to toggle the LWP's
1873 syscall entry/exit status, since the ptrace event itself doesn't
1874 indicate it, and report the trap to higher layers. */
1877 linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
1879 struct target_waitstatus *ourstatus = &lp->waitstatus;
1880 struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
1881 int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, lp->ptid);
1885 /* If we're stopping threads, there's a SIGSTOP pending, which
1886 makes it so that the LWP reports an immediate syscall return,
1887 followed by the SIGSTOP. Skip seeing that "return" using
1888 PTRACE_CONT directly, and let stop_wait_callback collect the
1889 SIGSTOP. Later when the thread is resumed, a new syscall
1890 entry event. If we didn't do this (and returned 0), we'd
1891 leave a syscall entry pending, and our caller, by using
1892 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
1893 itself. Later, when the user re-resumes this LWP, we'd see
1894 another syscall entry event and we'd mistake it for a return.
1896 If stop_wait_callback didn't force the SIGSTOP out of the LWP
1897 (leaving immediately with LWP->signalled set, without issuing
1898 a PTRACE_CONT), it would still be problematic to leave this
1899 syscall enter pending, as later when the thread is resumed,
1900 it would then see the same syscall exit mentioned above,
1901 followed by the delayed SIGSTOP, while the syscall didn't
1902 actually get to execute. It seems it would be even more
1903 confusing to the user. */
1905 if (debug_linux_nat)
1906 fprintf_unfiltered (gdb_stdlog,
1907 "LHST: ignoring syscall %d "
1908 "for LWP %ld (stopping threads), "
1909 "resuming with PTRACE_CONT for SIGSTOP\n",
1911 ptid_get_lwp (lp->ptid));
1913 lp->syscall_state = TARGET_WAITKIND_IGNORE;
1914 ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
1919 if (catch_syscall_enabled ())
1921 /* Always update the entry/return state, even if this particular
1922 syscall isn't interesting to the core now. In async mode,
1923 the user could install a new catchpoint for this syscall
1924 between syscall enter/return, and we'll need to know to
1925 report a syscall return if that happens. */
1926 lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
1927 ? TARGET_WAITKIND_SYSCALL_RETURN
1928 : TARGET_WAITKIND_SYSCALL_ENTRY);
1930 if (catching_syscall_number (syscall_number))
1932 /* Alright, an event to report. */
1933 ourstatus->kind = lp->syscall_state;
1934 ourstatus->value.syscall_number = syscall_number;
1936 if (debug_linux_nat)
1937 fprintf_unfiltered (gdb_stdlog,
1938 "LHST: stopping for %s of syscall %d"
1941 == TARGET_WAITKIND_SYSCALL_ENTRY
1942 ? "entry" : "return",
1944 ptid_get_lwp (lp->ptid));
1948 if (debug_linux_nat)
1949 fprintf_unfiltered (gdb_stdlog,
1950 "LHST: ignoring %s of syscall %d "
1952 lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
1953 ? "entry" : "return",
1955 ptid_get_lwp (lp->ptid));
1959 /* If we had been syscall tracing, and hence used PT_SYSCALL
1960 before on this LWP, it could happen that the user removes all
1961 syscall catchpoints before we get to process this event.
1962 There are two noteworthy issues here:
1964 - When stopped at a syscall entry event, resuming with
1965 PT_STEP still resumes executing the syscall and reports a
1968 - Only PT_SYSCALL catches syscall enters. If we last
1969 single-stepped this thread, then this event can't be a
1970 syscall enter. If we last single-stepped this thread, this
1971 has to be a syscall exit.
1973 The points above mean that the next resume, be it PT_STEP or
1974 PT_CONTINUE, can not trigger a syscall trace event. */
1975 if (debug_linux_nat)
1976 fprintf_unfiltered (gdb_stdlog,
1977 "LHST: caught syscall event "
1978 "with no syscall catchpoints."
1979 " %d for LWP %ld, ignoring\n",
1981 ptid_get_lwp (lp->ptid));
1982 lp->syscall_state = TARGET_WAITKIND_IGNORE;
1985 /* The core isn't interested in this event. For efficiency, avoid
1986 stopping all threads only to have the core resume them all again.
1987 Since we're not stopping threads, if we're still syscall tracing
1988 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
1989 subsequent syscall. Simply resume using the inf-ptrace layer,
1990 which knows when to use PT_SYSCALL or PT_CONTINUE. */
1992 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
1996 /* Handle a GNU/Linux extended wait response. If we see a clone
1997 event, we need to add the new LWP to our list (and not report the
1998 trap to higher layers). This function returns non-zero if the
1999 event should be ignored and we should wait again. If STOPPING is
2000 true, the new LWP remains stopped, otherwise it is continued. */
2003 linux_handle_extended_wait (struct lwp_info *lp, int status)
2005 int pid = ptid_get_lwp (lp->ptid);
2006 struct target_waitstatus *ourstatus = &lp->waitstatus;
2007 int event = linux_ptrace_get_extended_event (status);
2009 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
2010 || event == PTRACE_EVENT_CLONE)
2012 unsigned long new_pid;
2015 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
2017 /* If we haven't already seen the new PID stop, wait for it now. */
2018 if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
2020 /* The new child has a pending SIGSTOP. We can't affect it until it
2021 hits the SIGSTOP, but we're already attached. */
2022 ret = my_waitpid (new_pid, &status,
2023 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
2025 perror_with_name (_("waiting for new child"));
2026 else if (ret != new_pid)
2027 internal_error (__FILE__, __LINE__,
2028 _("wait returned unexpected PID %d"), ret);
2029 else if (!WIFSTOPPED (status))
2030 internal_error (__FILE__, __LINE__,
2031 _("wait returned unexpected status 0x%x"), status);
2034 ourstatus->value.related_pid = ptid_build (new_pid, new_pid, 0);
2036 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
2038 /* The arch-specific native code may need to know about new
2039 forks even if those end up never mapped to an
2041 if (linux_nat_new_fork != NULL)
2042 linux_nat_new_fork (lp, new_pid);
2045 if (event == PTRACE_EVENT_FORK
2046 && linux_fork_checkpointing_p (ptid_get_pid (lp->ptid)))
2048 /* Handle checkpointing by linux-fork.c here as a special
2049 case. We don't want the follow-fork-mode or 'catch fork'
2050 to interfere with this. */
2052 /* This won't actually modify the breakpoint list, but will
2053 physically remove the breakpoints from the child. */
2054 detach_breakpoints (ptid_build (new_pid, new_pid, 0));
2056 /* Retain child fork in ptrace (stopped) state. */
2057 if (!find_fork_pid (new_pid))
2060 /* Report as spurious, so that infrun doesn't want to follow
2061 this fork. We're actually doing an infcall in
2063 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
2065 /* Report the stop to the core. */
2069 if (event == PTRACE_EVENT_FORK)
2070 ourstatus->kind = TARGET_WAITKIND_FORKED;
2071 else if (event == PTRACE_EVENT_VFORK)
2072 ourstatus->kind = TARGET_WAITKIND_VFORKED;
2073 else if (event == PTRACE_EVENT_CLONE)
2075 struct lwp_info *new_lp;
2077 ourstatus->kind = TARGET_WAITKIND_IGNORE;
2079 if (debug_linux_nat)
2080 fprintf_unfiltered (gdb_stdlog,
2081 "LHEW: Got clone event "
2082 "from LWP %d, new child is LWP %ld\n",
2085 new_lp = add_lwp (ptid_build (ptid_get_pid (lp->ptid), new_pid, 0));
2087 new_lp->stopped = 1;
2088 new_lp->resumed = 1;
2090 /* If the thread_db layer is active, let it record the user
2091 level thread id and status, and add the thread to GDB's
2093 if (!thread_db_notice_clone (lp->ptid, new_lp->ptid))
2095 /* The process is not using thread_db. Add the LWP to
2097 target_post_attach (ptid_get_lwp (new_lp->ptid));
2098 add_thread (new_lp->ptid);
2101 /* Even if we're stopping the thread for some reason
2102 internal to this module, from the perspective of infrun
2103 and the user/frontend, this new thread is running until
2104 it next reports a stop. */
2105 set_running (new_lp->ptid, 1);
2106 set_executing (new_lp->ptid, 1);
2108 if (WSTOPSIG (status) != SIGSTOP)
2110 /* This can happen if someone starts sending signals to
2111 the new thread before it gets a chance to run, which
2112 have a lower number than SIGSTOP (e.g. SIGUSR1).
2113 This is an unlikely case, and harder to handle for
2114 fork / vfork than for clone, so we do not try - but
2115 we handle it for clone events here. */
2117 new_lp->signalled = 1;
2119 /* We created NEW_LP so it cannot yet contain STATUS. */
2120 gdb_assert (new_lp->status == 0);
2122 /* Save the wait status to report later. */
2123 if (debug_linux_nat)
2124 fprintf_unfiltered (gdb_stdlog,
2125 "LHEW: waitpid of new LWP %ld, "
2126 "saving status %s\n",
2127 (long) ptid_get_lwp (new_lp->ptid),
2128 status_to_str (status));
2129 new_lp->status = status;
2138 if (event == PTRACE_EVENT_EXEC)
2140 if (debug_linux_nat)
2141 fprintf_unfiltered (gdb_stdlog,
2142 "LHEW: Got exec event from LWP %ld\n",
2143 ptid_get_lwp (lp->ptid));
2145 ourstatus->kind = TARGET_WAITKIND_EXECD;
2146 ourstatus->value.execd_pathname
2147 = xstrdup (linux_child_pid_to_exec_file (NULL, pid));
2149 /* The thread that execed must have been resumed, but, when a
2150 thread execs, it changes its tid to the tgid, and the old
2151 tgid thread might have not been resumed. */
2156 if (event == PTRACE_EVENT_VFORK_DONE)
2158 if (current_inferior ()->waiting_for_vfork_done)
2160 if (debug_linux_nat)
2161 fprintf_unfiltered (gdb_stdlog,
2162 "LHEW: Got expected PTRACE_EVENT_"
2163 "VFORK_DONE from LWP %ld: stopping\n",
2164 ptid_get_lwp (lp->ptid));
2166 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
2170 if (debug_linux_nat)
2171 fprintf_unfiltered (gdb_stdlog,
2172 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2173 "from LWP %ld: ignoring\n",
2174 ptid_get_lwp (lp->ptid));
2178 internal_error (__FILE__, __LINE__,
2179 _("unknown ptrace event %d"), event);
2182 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2186 wait_lwp (struct lwp_info *lp)
2190 int thread_dead = 0;
2193 gdb_assert (!lp->stopped);
2194 gdb_assert (lp->status == 0);
2196 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2197 block_child_signals (&prev_mask);
2201 /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2202 was right and we should just call sigsuspend. */
2204 pid = my_waitpid (ptid_get_lwp (lp->ptid), &status, WNOHANG);
2205 if (pid == -1 && errno == ECHILD)
2206 pid = my_waitpid (ptid_get_lwp (lp->ptid), &status, __WCLONE | WNOHANG);
2207 if (pid == -1 && errno == ECHILD)
2209 /* The thread has previously exited. We need to delete it
2210 now because, for some vendor 2.4 kernels with NPTL
2211 support backported, there won't be an exit event unless
2212 it is the main thread. 2.6 kernels will report an exit
2213 event for each thread that exits, as expected. */
2215 if (debug_linux_nat)
2216 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
2217 target_pid_to_str (lp->ptid));
2222 /* Bugs 10970, 12702.
2223 Thread group leader may have exited in which case we'll lock up in
2224 waitpid if there are other threads, even if they are all zombies too.
2225 Basically, we're not supposed to use waitpid this way.
2226 __WCLONE is not applicable for the leader so we can't use that.
2227 LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2228 process; it gets ESRCH both for the zombie and for running processes.
2230 As a workaround, check if we're waiting for the thread group leader and
2231 if it's a zombie, and avoid calling waitpid if it is.
2233 This is racy, what if the tgl becomes a zombie right after we check?
2234 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2235 waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
2237 if (ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid)
2238 && linux_proc_pid_is_zombie (ptid_get_lwp (lp->ptid)))
2241 if (debug_linux_nat)
2242 fprintf_unfiltered (gdb_stdlog,
2243 "WL: Thread group leader %s vanished.\n",
2244 target_pid_to_str (lp->ptid));
2248 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2249 get invoked despite our caller had them intentionally blocked by
2250 block_child_signals. This is sensitive only to the loop of
2251 linux_nat_wait_1 and there if we get called my_waitpid gets called
2252 again before it gets to sigsuspend so we can safely let the handlers
2253 get executed here. */
2255 if (debug_linux_nat)
2256 fprintf_unfiltered (gdb_stdlog, "WL: about to sigsuspend\n");
2257 sigsuspend (&suspend_mask);
2260 restore_child_signals_mask (&prev_mask);
2264 gdb_assert (pid == ptid_get_lwp (lp->ptid));
2266 if (debug_linux_nat)
2268 fprintf_unfiltered (gdb_stdlog,
2269 "WL: waitpid %s received %s\n",
2270 target_pid_to_str (lp->ptid),
2271 status_to_str (status));
2274 /* Check if the thread has exited. */
2275 if (WIFEXITED (status) || WIFSIGNALED (status))
2277 if (ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid))
2279 if (debug_linux_nat)
2280 fprintf_unfiltered (gdb_stdlog, "WL: Process %d exited.\n",
2281 ptid_get_pid (lp->ptid));
2283 /* This is the leader exiting, it means the whole
2284 process is gone. Store the status to report to the
2285 core. Store it in lp->waitstatus, because lp->status
2286 would be ambiguous (W_EXITCODE(0,0) == 0). */
2287 store_waitstatus (&lp->waitstatus, status);
2292 if (debug_linux_nat)
2293 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
2294 target_pid_to_str (lp->ptid));
2304 gdb_assert (WIFSTOPPED (status));
2307 if (lp->must_set_ptrace_flags)
2309 struct inferior *inf = find_inferior_pid (ptid_get_pid (lp->ptid));
2310 int options = linux_nat_ptrace_options (inf->attach_flag);
2312 linux_enable_event_reporting (ptid_get_lwp (lp->ptid), options);
2313 lp->must_set_ptrace_flags = 0;
2316 /* Handle GNU/Linux's syscall SIGTRAPs. */
2317 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
2319 /* No longer need the sysgood bit. The ptrace event ends up
2320 recorded in lp->waitstatus if we care for it. We can carry
2321 on handling the event like a regular SIGTRAP from here
2323 status = W_STOPCODE (SIGTRAP);
2324 if (linux_handle_syscall_trap (lp, 1))
2325 return wait_lwp (lp);
2328 /* Handle GNU/Linux's extended waitstatus for trace events. */
2329 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
2330 && linux_is_extended_waitstatus (status))
2332 if (debug_linux_nat)
2333 fprintf_unfiltered (gdb_stdlog,
2334 "WL: Handling extended status 0x%06x\n",
2336 linux_handle_extended_wait (lp, status);
2343 /* Send a SIGSTOP to LP. */
2346 stop_callback (struct lwp_info *lp, void *data)
2348 if (!lp->stopped && !lp->signalled)
2352 if (debug_linux_nat)
2354 fprintf_unfiltered (gdb_stdlog,
2355 "SC: kill %s **<SIGSTOP>**\n",
2356 target_pid_to_str (lp->ptid));
2359 ret = kill_lwp (ptid_get_lwp (lp->ptid), SIGSTOP);
2360 if (debug_linux_nat)
2362 fprintf_unfiltered (gdb_stdlog,
2363 "SC: lwp kill %d %s\n",
2365 errno ? safe_strerror (errno) : "ERRNO-OK");
2369 gdb_assert (lp->status == 0);
2375 /* Request a stop on LWP. */
2378 linux_stop_lwp (struct lwp_info *lwp)
2380 stop_callback (lwp, NULL);
2383 /* See linux-nat.h */
2386 linux_stop_and_wait_all_lwps (void)
2388 /* Stop all LWP's ... */
2389 iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
2391 /* ... and wait until all of them have reported back that
2392 they're no longer running. */
2393 iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
2396 /* See linux-nat.h */
2399 linux_unstop_all_lwps (void)
2401 iterate_over_lwps (minus_one_ptid,
2402 resume_stopped_resumed_lwps, &minus_one_ptid);
2405 /* Return non-zero if LWP PID has a pending SIGINT. */
2408 linux_nat_has_pending_sigint (int pid)
2410 sigset_t pending, blocked, ignored;
2412 linux_proc_pending_signals (pid, &pending, &blocked, &ignored);
2414 if (sigismember (&pending, SIGINT)
2415 && !sigismember (&ignored, SIGINT))
2421 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2424 set_ignore_sigint (struct lwp_info *lp, void *data)
2426 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2427 flag to consume the next one. */
2428 if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status)
2429 && WSTOPSIG (lp->status) == SIGINT)
2432 lp->ignore_sigint = 1;
2437 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2438 This function is called after we know the LWP has stopped; if the LWP
2439 stopped before the expected SIGINT was delivered, then it will never have
2440 arrived. Also, if the signal was delivered to a shared queue and consumed
2441 by a different thread, it will never be delivered to this LWP. */
2444 maybe_clear_ignore_sigint (struct lwp_info *lp)
2446 if (!lp->ignore_sigint)
2449 if (!linux_nat_has_pending_sigint (ptid_get_lwp (lp->ptid)))
2451 if (debug_linux_nat)
2452 fprintf_unfiltered (gdb_stdlog,
2453 "MCIS: Clearing bogus flag for %s\n",
2454 target_pid_to_str (lp->ptid));
2455 lp->ignore_sigint = 0;
2459 /* Fetch the possible triggered data watchpoint info and store it in
2462 On some archs, like x86, that use debug registers to set
2463 watchpoints, it's possible that the way to know which watched
2464 address trapped, is to check the register that is used to select
2465 which address to watch. Problem is, between setting the watchpoint
2466 and reading back which data address trapped, the user may change
2467 the set of watchpoints, and, as a consequence, GDB changes the
2468 debug registers in the inferior. To avoid reading back a stale
2469 stopped-data-address when that happens, we cache in LP the fact
2470 that a watchpoint trapped, and the corresponding data address, as
2471 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2472 registers meanwhile, we have the cached data we can rely on. */
2475 check_stopped_by_watchpoint (struct lwp_info *lp)
2477 struct cleanup *old_chain;
2479 if (linux_ops->to_stopped_by_watchpoint == NULL)
2482 old_chain = save_inferior_ptid ();
2483 inferior_ptid = lp->ptid;
2485 if (linux_ops->to_stopped_by_watchpoint (linux_ops))
2487 lp->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
2489 if (linux_ops->to_stopped_data_address != NULL)
2490 lp->stopped_data_address_p =
2491 linux_ops->to_stopped_data_address (¤t_target,
2492 &lp->stopped_data_address);
2494 lp->stopped_data_address_p = 0;
2497 do_cleanups (old_chain);
2499 return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
2502 /* Called when the LWP stopped for a trap that could be explained by a
2503 watchpoint or a breakpoint. */
2506 save_sigtrap (struct lwp_info *lp)
2508 gdb_assert (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON);
2509 gdb_assert (lp->status != 0);
2511 /* Check first if this was a SW/HW breakpoint before checking
2512 watchpoints, because at least s390 can't tell the data address of
2513 hardware watchpoint hits, and the kernel returns
2514 stopped-by-watchpoint as long as there's a watchpoint set. */
2515 if (linux_nat_status_is_event (lp->status))
2516 check_stopped_by_breakpoint (lp);
2518 /* Note that TRAP_HWBKPT can indicate either a hardware breakpoint
2519 or hardware watchpoint. Check which is which if we got
2520 TARGET_STOPPED_BY_HW_BREAKPOINT. */
2521 if (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON
2522 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
2523 check_stopped_by_watchpoint (lp);
2526 /* Returns true if the LWP had stopped for a watchpoint. */
2529 linux_nat_stopped_by_watchpoint (struct target_ops *ops)
2531 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2533 gdb_assert (lp != NULL);
2535 return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
2539 linux_nat_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
2541 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2543 gdb_assert (lp != NULL);
2545 *addr_p = lp->stopped_data_address;
2547 return lp->stopped_data_address_p;
2550 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2553 sigtrap_is_event (int status)
2555 return WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP;
2558 /* Set alternative SIGTRAP-like events recognizer. If
2559 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2563 linux_nat_set_status_is_event (struct target_ops *t,
2564 int (*status_is_event) (int status))
2566 linux_nat_status_is_event = status_is_event;
2569 /* Wait until LP is stopped. */
2572 stop_wait_callback (struct lwp_info *lp, void *data)
2574 struct inferior *inf = find_inferior_ptid (lp->ptid);
2576 /* If this is a vfork parent, bail out, it is not going to report
2577 any SIGSTOP until the vfork is done with. */
2578 if (inf->vfork_child != NULL)
2585 status = wait_lwp (lp);
2589 if (lp->ignore_sigint && WIFSTOPPED (status)
2590 && WSTOPSIG (status) == SIGINT)
2592 lp->ignore_sigint = 0;
2595 ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
2597 if (debug_linux_nat)
2598 fprintf_unfiltered (gdb_stdlog,
2599 "PTRACE_CONT %s, 0, 0 (%s) "
2600 "(discarding SIGINT)\n",
2601 target_pid_to_str (lp->ptid),
2602 errno ? safe_strerror (errno) : "OK");
2604 return stop_wait_callback (lp, NULL);
2607 maybe_clear_ignore_sigint (lp);
2609 if (WSTOPSIG (status) != SIGSTOP)
2611 /* The thread was stopped with a signal other than SIGSTOP. */
2613 if (debug_linux_nat)
2614 fprintf_unfiltered (gdb_stdlog,
2615 "SWC: Pending event %s in %s\n",
2616 status_to_str ((int) status),
2617 target_pid_to_str (lp->ptid));
2619 /* Save the sigtrap event. */
2620 lp->status = status;
2621 gdb_assert (lp->signalled);
2626 /* We caught the SIGSTOP that we intended to catch, so
2627 there's no SIGSTOP pending. */
2629 if (debug_linux_nat)
2630 fprintf_unfiltered (gdb_stdlog,
2631 "SWC: Expected SIGSTOP caught for %s.\n",
2632 target_pid_to_str (lp->ptid));
2634 /* Reset SIGNALLED only after the stop_wait_callback call
2635 above as it does gdb_assert on SIGNALLED. */
2643 /* Return non-zero if LP has a wait status pending. Discard the
2644 pending event and resume the LWP if the event that originally
2645 caused the stop became uninteresting. */
2648 status_callback (struct lwp_info *lp, void *data)
2650 /* Only report a pending wait status if we pretend that this has
2651 indeed been resumed. */
2655 if (!lwp_status_pending_p (lp))
2658 if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
2659 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
2661 struct regcache *regcache = get_thread_regcache (lp->ptid);
2662 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2666 pc = regcache_read_pc (regcache);
2668 if (pc != lp->stop_pc)
2670 if (debug_linux_nat)
2671 fprintf_unfiltered (gdb_stdlog,
2672 "SC: PC of %s changed. was=%s, now=%s\n",
2673 target_pid_to_str (lp->ptid),
2674 paddress (target_gdbarch (), lp->stop_pc),
2675 paddress (target_gdbarch (), pc));
2679 #if !USE_SIGTRAP_SIGINFO
2680 else if (!breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
2682 if (debug_linux_nat)
2683 fprintf_unfiltered (gdb_stdlog,
2684 "SC: previous breakpoint of %s, at %s gone\n",
2685 target_pid_to_str (lp->ptid),
2686 paddress (target_gdbarch (), lp->stop_pc));
2694 if (debug_linux_nat)
2695 fprintf_unfiltered (gdb_stdlog,
2696 "SC: pending event of %s cancelled.\n",
2697 target_pid_to_str (lp->ptid));
2700 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
2708 /* Return non-zero if LP isn't stopped. */
2711 running_callback (struct lwp_info *lp, void *data)
2713 return (!lp->stopped
2714 || (lwp_status_pending_p (lp) && lp->resumed));
2717 /* Count the LWP's that have had events. */
2720 count_events_callback (struct lwp_info *lp, void *data)
2722 int *count = (int *) data;
2724 gdb_assert (count != NULL);
2726 /* Select only resumed LWPs that have an event pending. */
2727 if (lp->resumed && lwp_status_pending_p (lp))
2733 /* Select the LWP (if any) that is currently being single-stepped. */
2736 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
2738 if (lp->last_resume_kind == resume_step
2745 /* Returns true if LP has a status pending. */
2748 lwp_status_pending_p (struct lwp_info *lp)
2750 /* We check for lp->waitstatus in addition to lp->status, because we
2751 can have pending process exits recorded in lp->status and
2752 W_EXITCODE(0,0) happens to be 0. */
2753 return lp->status != 0 || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE;
2756 /* Select the Nth LWP that has had an event. */
2759 select_event_lwp_callback (struct lwp_info *lp, void *data)
2761 int *selector = (int *) data;
2763 gdb_assert (selector != NULL);
2765 /* Select only resumed LWPs that have an event pending. */
2766 if (lp->resumed && lwp_status_pending_p (lp))
2767 if ((*selector)-- == 0)
2773 /* Called when the LWP got a signal/trap that could be explained by a
2774 software or hardware breakpoint. */
2777 check_stopped_by_breakpoint (struct lwp_info *lp)
2779 /* Arrange for a breakpoint to be hit again later. We don't keep
2780 the SIGTRAP status and don't forward the SIGTRAP signal to the
2781 LWP. We will handle the current event, eventually we will resume
2782 this LWP, and this breakpoint will trap again.
2784 If we do not do this, then we run the risk that the user will
2785 delete or disable the breakpoint, but the LWP will have already
2788 struct regcache *regcache = get_thread_regcache (lp->ptid);
2789 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2792 #if USE_SIGTRAP_SIGINFO
2796 pc = regcache_read_pc (regcache);
2797 sw_bp_pc = pc - gdbarch_decr_pc_after_break (gdbarch);
2799 #if USE_SIGTRAP_SIGINFO
2800 if (linux_nat_get_siginfo (lp->ptid, &siginfo))
2802 if (siginfo.si_signo == SIGTRAP)
2804 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code))
2806 if (debug_linux_nat)
2807 fprintf_unfiltered (gdb_stdlog,
2808 "CSBB: %s stopped by software "
2810 target_pid_to_str (lp->ptid));
2812 /* Back up the PC if necessary. */
2814 regcache_write_pc (regcache, sw_bp_pc);
2816 lp->stop_pc = sw_bp_pc;
2817 lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
2820 else if (siginfo.si_code == TRAP_HWBKPT)
2822 if (debug_linux_nat)
2823 fprintf_unfiltered (gdb_stdlog,
2824 "CSBB: %s stopped by hardware "
2825 "breakpoint/watchpoint\n",
2826 target_pid_to_str (lp->ptid));
2829 lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
2832 else if (siginfo.si_code == TRAP_TRACE)
2834 if (debug_linux_nat)
2835 fprintf_unfiltered (gdb_stdlog,
2836 "CSBB: %s stopped by trace\n",
2837 target_pid_to_str (lp->ptid));
2842 if ((!lp->step || lp->stop_pc == sw_bp_pc)
2843 && software_breakpoint_inserted_here_p (get_regcache_aspace (regcache),
2846 /* The LWP was either continued, or stepped a software
2847 breakpoint instruction. */
2848 if (debug_linux_nat)
2849 fprintf_unfiltered (gdb_stdlog,
2850 "CSBB: %s stopped by software breakpoint\n",
2851 target_pid_to_str (lp->ptid));
2853 /* Back up the PC if necessary. */
2855 regcache_write_pc (regcache, sw_bp_pc);
2857 lp->stop_pc = sw_bp_pc;
2858 lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
2862 if (hardware_breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
2864 if (debug_linux_nat)
2865 fprintf_unfiltered (gdb_stdlog,
2866 "CSBB: stopped by hardware breakpoint %s\n",
2867 target_pid_to_str (lp->ptid));
2870 lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
2879 /* Returns true if the LWP had stopped for a software breakpoint. */
2882 linux_nat_stopped_by_sw_breakpoint (struct target_ops *ops)
2884 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2886 gdb_assert (lp != NULL);
2888 return lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
2891 /* Implement the supports_stopped_by_sw_breakpoint method. */
2894 linux_nat_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
2896 return USE_SIGTRAP_SIGINFO;
2899 /* Returns true if the LWP had stopped for a hardware
2900 breakpoint/watchpoint. */
2903 linux_nat_stopped_by_hw_breakpoint (struct target_ops *ops)
2905 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2907 gdb_assert (lp != NULL);
2909 return lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
2912 /* Implement the supports_stopped_by_hw_breakpoint method. */
2915 linux_nat_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
2917 return USE_SIGTRAP_SIGINFO;
2920 /* Select one LWP out of those that have events pending. */
2923 select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status)
2926 int random_selector;
2927 struct lwp_info *event_lp = NULL;
2929 /* Record the wait status for the original LWP. */
2930 (*orig_lp)->status = *status;
2932 /* In all-stop, give preference to the LWP that is being
2933 single-stepped. There will be at most one, and it will be the
2934 LWP that the core is most interested in. If we didn't do this,
2935 then we'd have to handle pending step SIGTRAPs somehow in case
2936 the core later continues the previously-stepped thread, as
2937 otherwise we'd report the pending SIGTRAP then, and the core, not
2938 having stepped the thread, wouldn't understand what the trap was
2939 for, and therefore would report it to the user as a random
2941 if (!target_is_non_stop_p ())
2943 event_lp = iterate_over_lwps (filter,
2944 select_singlestep_lwp_callback, NULL);
2945 if (event_lp != NULL)
2947 if (debug_linux_nat)
2948 fprintf_unfiltered (gdb_stdlog,
2949 "SEL: Select single-step %s\n",
2950 target_pid_to_str (event_lp->ptid));
2954 if (event_lp == NULL)
2956 /* Pick one at random, out of those which have had events. */
2958 /* First see how many events we have. */
2959 iterate_over_lwps (filter, count_events_callback, &num_events);
2960 gdb_assert (num_events > 0);
2962 /* Now randomly pick a LWP out of those that have had
2964 random_selector = (int)
2965 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
2967 if (debug_linux_nat && num_events > 1)
2968 fprintf_unfiltered (gdb_stdlog,
2969 "SEL: Found %d events, selecting #%d\n",
2970 num_events, random_selector);
2972 event_lp = iterate_over_lwps (filter,
2973 select_event_lwp_callback,
2977 if (event_lp != NULL)
2979 /* Switch the event LWP. */
2980 *orig_lp = event_lp;
2981 *status = event_lp->status;
2984 /* Flush the wait status for the event LWP. */
2985 (*orig_lp)->status = 0;
2988 /* Return non-zero if LP has been resumed. */
2991 resumed_callback (struct lwp_info *lp, void *data)
2996 /* Stop an active thread, verify it still exists, then resume it. If
2997 the thread ends up with a pending status, then it is not resumed,
2998 and *DATA (really a pointer to int), is set. */
3001 stop_and_resume_callback (struct lwp_info *lp, void *data)
3005 ptid_t ptid = lp->ptid;
3007 stop_callback (lp, NULL);
3008 stop_wait_callback (lp, NULL);
3010 /* Resume if the lwp still exists, and the core wanted it
3012 lp = find_lwp_pid (ptid);
3015 if (lp->last_resume_kind == resume_stop
3016 && !lwp_status_pending_p (lp))
3018 /* The core wanted the LWP to stop. Even if it stopped
3019 cleanly (with SIGSTOP), leave the event pending. */
3020 if (debug_linux_nat)
3021 fprintf_unfiltered (gdb_stdlog,
3022 "SARC: core wanted LWP %ld stopped "
3023 "(leaving SIGSTOP pending)\n",
3024 ptid_get_lwp (lp->ptid));
3025 lp->status = W_STOPCODE (SIGSTOP);
3028 if (!lwp_status_pending_p (lp))
3030 if (debug_linux_nat)
3031 fprintf_unfiltered (gdb_stdlog,
3032 "SARC: re-resuming LWP %ld\n",
3033 ptid_get_lwp (lp->ptid));
3034 resume_lwp (lp, lp->step, GDB_SIGNAL_0);
3038 if (debug_linux_nat)
3039 fprintf_unfiltered (gdb_stdlog,
3040 "SARC: not re-resuming LWP %ld "
3042 ptid_get_lwp (lp->ptid));
3049 /* Check if we should go on and pass this event to common code.
3050 Return the affected lwp if we are, or NULL otherwise. */
3052 static struct lwp_info *
3053 linux_nat_filter_event (int lwpid, int status)
3055 struct lwp_info *lp;
3056 int event = linux_ptrace_get_extended_event (status);
3058 lp = find_lwp_pid (pid_to_ptid (lwpid));
3060 /* Check for stop events reported by a process we didn't already
3061 know about - anything not already in our LWP list.
3063 If we're expecting to receive stopped processes after
3064 fork, vfork, and clone events, then we'll just add the
3065 new one to our list and go back to waiting for the event
3066 to be reported - the stopped process might be returned
3067 from waitpid before or after the event is.
3069 But note the case of a non-leader thread exec'ing after the
3070 leader having exited, and gone from our lists. The non-leader
3071 thread changes its tid to the tgid. */
3073 if (WIFSTOPPED (status) && lp == NULL
3074 && (WSTOPSIG (status) == SIGTRAP && event == PTRACE_EVENT_EXEC))
3076 /* A multi-thread exec after we had seen the leader exiting. */
3077 if (debug_linux_nat)
3078 fprintf_unfiltered (gdb_stdlog,
3079 "LLW: Re-adding thread group leader LWP %d.\n",
3082 lp = add_lwp (ptid_build (lwpid, lwpid, 0));
3085 add_thread (lp->ptid);
3088 if (WIFSTOPPED (status) && !lp)
3090 if (debug_linux_nat)
3091 fprintf_unfiltered (gdb_stdlog,
3092 "LHEW: saving LWP %ld status %s in stopped_pids list\n",
3093 (long) lwpid, status_to_str (status));
3094 add_to_pid_list (&stopped_pids, lwpid, status);
3098 /* Make sure we don't report an event for the exit of an LWP not in
3099 our list, i.e. not part of the current process. This can happen
3100 if we detach from a program we originally forked and then it
3102 if (!WIFSTOPPED (status) && !lp)
3105 /* This LWP is stopped now. (And if dead, this prevents it from
3106 ever being continued.) */
3109 if (WIFSTOPPED (status) && lp->must_set_ptrace_flags)
3111 struct inferior *inf = find_inferior_pid (ptid_get_pid (lp->ptid));
3112 int options = linux_nat_ptrace_options (inf->attach_flag);
3114 linux_enable_event_reporting (ptid_get_lwp (lp->ptid), options);
3115 lp->must_set_ptrace_flags = 0;
3118 /* Handle GNU/Linux's syscall SIGTRAPs. */
3119 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
3121 /* No longer need the sysgood bit. The ptrace event ends up
3122 recorded in lp->waitstatus if we care for it. We can carry
3123 on handling the event like a regular SIGTRAP from here
3125 status = W_STOPCODE (SIGTRAP);
3126 if (linux_handle_syscall_trap (lp, 0))
3130 /* Handle GNU/Linux's extended waitstatus for trace events. */
3131 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
3132 && linux_is_extended_waitstatus (status))
3134 if (debug_linux_nat)
3135 fprintf_unfiltered (gdb_stdlog,
3136 "LLW: Handling extended status 0x%06x\n",
3138 if (linux_handle_extended_wait (lp, status))
3142 /* Check if the thread has exited. */
3143 if (WIFEXITED (status) || WIFSIGNALED (status))
3145 if (num_lwps (ptid_get_pid (lp->ptid)) > 1)
3147 /* If this is the main thread, we must stop all threads and
3148 verify if they are still alive. This is because in the
3149 nptl thread model on Linux 2.4, there is no signal issued
3150 for exiting LWPs other than the main thread. We only get
3151 the main thread exit signal once all child threads have
3152 already exited. If we stop all the threads and use the
3153 stop_wait_callback to check if they have exited we can
3154 determine whether this signal should be ignored or
3155 whether it means the end of the debugged application,
3156 regardless of which threading model is being used. */
3157 if (ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid))
3159 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp->ptid)),
3160 stop_and_resume_callback, NULL);
3163 if (debug_linux_nat)
3164 fprintf_unfiltered (gdb_stdlog,
3165 "LLW: %s exited.\n",
3166 target_pid_to_str (lp->ptid));
3168 if (num_lwps (ptid_get_pid (lp->ptid)) > 1)
3170 /* If there is at least one more LWP, then the exit signal
3171 was not the end of the debugged application and should be
3178 /* Note that even if the leader was ptrace-stopped, it can still
3179 exit, if e.g., some other thread brings down the whole
3180 process (calls `exit'). So don't assert that the lwp is
3182 if (debug_linux_nat)
3183 fprintf_unfiltered (gdb_stdlog,
3184 "Process %ld exited (resumed=%d)\n",
3185 ptid_get_lwp (lp->ptid), lp->resumed);
3187 /* This was the last lwp in the process. Since events are
3188 serialized to GDB core, we may not be able report this one
3189 right now, but GDB core and the other target layers will want
3190 to be notified about the exit code/signal, leave the status
3191 pending for the next time we're able to report it. */
3193 /* Dead LWP's aren't expected to reported a pending sigstop. */
3196 /* Store the pending event in the waitstatus, because
3197 W_EXITCODE(0,0) == 0. */
3198 store_waitstatus (&lp->waitstatus, status);
3202 /* Check if the current LWP has previously exited. In the nptl
3203 thread model, LWPs other than the main thread do not issue
3204 signals when they exit so we must check whenever the thread has
3205 stopped. A similar check is made in stop_wait_callback(). */
3206 if (num_lwps (ptid_get_pid (lp->ptid)) > 1 && !linux_thread_alive (lp->ptid))
3208 ptid_t ptid = pid_to_ptid (ptid_get_pid (lp->ptid));
3210 if (debug_linux_nat)
3211 fprintf_unfiltered (gdb_stdlog,
3212 "LLW: %s exited.\n",
3213 target_pid_to_str (lp->ptid));
3217 /* Make sure there is at least one thread running. */
3218 gdb_assert (iterate_over_lwps (ptid, running_callback, NULL));
3220 /* Discard the event. */
3224 /* Make sure we don't report a SIGSTOP that we sent ourselves in
3225 an attempt to stop an LWP. */
3227 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
3231 if (lp->last_resume_kind == resume_stop)
3233 if (debug_linux_nat)
3234 fprintf_unfiltered (gdb_stdlog,
3235 "LLW: resume_stop SIGSTOP caught for %s.\n",
3236 target_pid_to_str (lp->ptid));
3240 /* This is a delayed SIGSTOP. Filter out the event. */
3242 if (debug_linux_nat)
3243 fprintf_unfiltered (gdb_stdlog,
3244 "LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
3246 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3247 target_pid_to_str (lp->ptid));
3249 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
3250 gdb_assert (lp->resumed);
3255 /* Make sure we don't report a SIGINT that we have already displayed
3256 for another thread. */
3257 if (lp->ignore_sigint
3258 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT)
3260 if (debug_linux_nat)
3261 fprintf_unfiltered (gdb_stdlog,
3262 "LLW: Delayed SIGINT caught for %s.\n",
3263 target_pid_to_str (lp->ptid));
3265 /* This is a delayed SIGINT. */
3266 lp->ignore_sigint = 0;
3268 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
3269 if (debug_linux_nat)
3270 fprintf_unfiltered (gdb_stdlog,
3271 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
3273 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3274 target_pid_to_str (lp->ptid));
3275 gdb_assert (lp->resumed);
3277 /* Discard the event. */
3281 /* Don't report signals that GDB isn't interested in, such as
3282 signals that are neither printed nor stopped upon. Stopping all
3283 threads can be a bit time-consuming so if we want decent
3284 performance with heavily multi-threaded programs, especially when
3285 they're using a high frequency timer, we'd better avoid it if we
3287 if (WIFSTOPPED (status))
3289 enum gdb_signal signo = gdb_signal_from_host (WSTOPSIG (status));
3291 if (!target_is_non_stop_p ())
3293 /* Only do the below in all-stop, as we currently use SIGSTOP
3294 to implement target_stop (see linux_nat_stop) in
3296 if (signo == GDB_SIGNAL_INT && signal_pass_state (signo) == 0)
3298 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3299 forwarded to the entire process group, that is, all LWPs
3300 will receive it - unless they're using CLONE_THREAD to
3301 share signals. Since we only want to report it once, we
3302 mark it as ignored for all LWPs except this one. */
3303 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp->ptid)),
3304 set_ignore_sigint, NULL);
3305 lp->ignore_sigint = 0;
3308 maybe_clear_ignore_sigint (lp);
3311 /* When using hardware single-step, we need to report every signal.
3312 Otherwise, signals in pass_mask may be short-circuited
3313 except signals that might be caused by a breakpoint. */
3315 && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status))
3316 && !linux_wstatus_maybe_breakpoint (status))
3318 linux_resume_one_lwp (lp, lp->step, signo);
3319 if (debug_linux_nat)
3320 fprintf_unfiltered (gdb_stdlog,
3321 "LLW: %s %s, %s (preempt 'handle')\n",
3323 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3324 target_pid_to_str (lp->ptid),
3325 (signo != GDB_SIGNAL_0
3326 ? strsignal (gdb_signal_to_host (signo))
3332 /* An interesting event. */
3334 lp->status = status;
3339 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3340 their exits until all other threads in the group have exited. */
3343 check_zombie_leaders (void)
3345 struct inferior *inf;
3349 struct lwp_info *leader_lp;
3354 leader_lp = find_lwp_pid (pid_to_ptid (inf->pid));
3355 if (leader_lp != NULL
3356 /* Check if there are other threads in the group, as we may
3357 have raced with the inferior simply exiting. */
3358 && num_lwps (inf->pid) > 1
3359 && linux_proc_pid_is_zombie (inf->pid))
3361 if (debug_linux_nat)
3362 fprintf_unfiltered (gdb_stdlog,
3363 "CZL: Thread group leader %d zombie "
3364 "(it exited, or another thread execd).\n",
3367 /* A leader zombie can mean one of two things:
3369 - It exited, and there's an exit status pending
3370 available, or only the leader exited (not the whole
3371 program). In the latter case, we can't waitpid the
3372 leader's exit status until all other threads are gone.
3374 - There are 3 or more threads in the group, and a thread
3375 other than the leader exec'd. On an exec, the Linux
3376 kernel destroys all other threads (except the execing
3377 one) in the thread group, and resets the execing thread's
3378 tid to the tgid. No exit notification is sent for the
3379 execing thread -- from the ptracer's perspective, it
3380 appears as though the execing thread just vanishes.
3381 Until we reap all other threads except the leader and the
3382 execing thread, the leader will be zombie, and the
3383 execing thread will be in `D (disc sleep)'. As soon as
3384 all other threads are reaped, the execing thread changes
3385 it's tid to the tgid, and the previous (zombie) leader
3386 vanishes, giving place to the "new" leader. We could try
3387 distinguishing the exit and exec cases, by waiting once
3388 more, and seeing if something comes out, but it doesn't
3389 sound useful. The previous leader _does_ go away, and
3390 we'll re-add the new one once we see the exec event
3391 (which is just the same as what would happen if the
3392 previous leader did exit voluntarily before some other
3395 if (debug_linux_nat)
3396 fprintf_unfiltered (gdb_stdlog,
3397 "CZL: Thread group leader %d vanished.\n",
3399 exit_lwp (leader_lp);
3405 linux_nat_wait_1 (struct target_ops *ops,
3406 ptid_t ptid, struct target_waitstatus *ourstatus,
3410 enum resume_kind last_resume_kind;
3411 struct lwp_info *lp;
3414 if (debug_linux_nat)
3415 fprintf_unfiltered (gdb_stdlog, "LLW: enter\n");
3417 /* The first time we get here after starting a new inferior, we may
3418 not have added it to the LWP list yet - this is the earliest
3419 moment at which we know its PID. */
3420 if (ptid_is_pid (inferior_ptid))
3422 /* Upgrade the main thread's ptid. */
3423 thread_change_ptid (inferior_ptid,
3424 ptid_build (ptid_get_pid (inferior_ptid),
3425 ptid_get_pid (inferior_ptid), 0));
3427 lp = add_initial_lwp (inferior_ptid);
3431 /* Make sure SIGCHLD is blocked until the sigsuspend below. */
3432 block_child_signals (&prev_mask);
3434 /* First check if there is a LWP with a wait status pending. */
3435 lp = iterate_over_lwps (ptid, status_callback, NULL);
3438 if (debug_linux_nat)
3439 fprintf_unfiltered (gdb_stdlog,
3440 "LLW: Using pending wait status %s for %s.\n",
3441 status_to_str (lp->status),
3442 target_pid_to_str (lp->ptid));
3445 /* But if we don't find a pending event, we'll have to wait. Always
3446 pull all events out of the kernel. We'll randomly select an
3447 event LWP out of all that have events, to prevent starvation. */
3453 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3456 - If the thread group leader exits while other threads in the
3457 thread group still exist, waitpid(TGID, ...) hangs. That
3458 waitpid won't return an exit status until the other threads
3459 in the group are reapped.
3461 - When a non-leader thread execs, that thread just vanishes
3462 without reporting an exit (so we'd hang if we waited for it
3463 explicitly in that case). The exec event is reported to
3467 lwpid = my_waitpid (-1, &status, __WCLONE | WNOHANG);
3468 if (lwpid == 0 || (lwpid == -1 && errno == ECHILD))
3469 lwpid = my_waitpid (-1, &status, WNOHANG);
3471 if (debug_linux_nat)
3472 fprintf_unfiltered (gdb_stdlog,
3473 "LNW: waitpid(-1, ...) returned %d, %s\n",
3474 lwpid, errno ? safe_strerror (errno) : "ERRNO-OK");
3478 if (debug_linux_nat)
3480 fprintf_unfiltered (gdb_stdlog,
3481 "LLW: waitpid %ld received %s\n",
3482 (long) lwpid, status_to_str (status));
3485 linux_nat_filter_event (lwpid, status);
3486 /* Retry until nothing comes out of waitpid. A single
3487 SIGCHLD can indicate more than one child stopped. */
3491 /* Now that we've pulled all events out of the kernel, resume
3492 LWPs that don't have an interesting event to report. */
3493 iterate_over_lwps (minus_one_ptid,
3494 resume_stopped_resumed_lwps, &minus_one_ptid);
3496 /* ... and find an LWP with a status to report to the core, if
3498 lp = iterate_over_lwps (ptid, status_callback, NULL);
3502 /* Check for zombie thread group leaders. Those can't be reaped
3503 until all other threads in the thread group are. */
3504 check_zombie_leaders ();
3506 /* If there are no resumed children left, bail. We'd be stuck
3507 forever in the sigsuspend call below otherwise. */
3508 if (iterate_over_lwps (ptid, resumed_callback, NULL) == NULL)
3510 if (debug_linux_nat)
3511 fprintf_unfiltered (gdb_stdlog, "LLW: exit (no resumed LWP)\n");
3513 ourstatus->kind = TARGET_WAITKIND_NO_RESUMED;
3515 restore_child_signals_mask (&prev_mask);
3516 return minus_one_ptid;
3519 /* No interesting event to report to the core. */
3521 if (target_options & TARGET_WNOHANG)
3523 if (debug_linux_nat)
3524 fprintf_unfiltered (gdb_stdlog, "LLW: exit (ignore)\n");
3526 ourstatus->kind = TARGET_WAITKIND_IGNORE;
3527 restore_child_signals_mask (&prev_mask);
3528 return minus_one_ptid;
3531 /* We shouldn't end up here unless we want to try again. */
3532 gdb_assert (lp == NULL);
3534 /* Block until we get an event reported with SIGCHLD. */
3535 if (debug_linux_nat)
3536 fprintf_unfiltered (gdb_stdlog, "LNW: about to sigsuspend\n");
3537 sigsuspend (&suspend_mask);
3542 status = lp->status;
3545 if (!target_is_non_stop_p ())
3547 /* Now stop all other LWP's ... */
3548 iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
3550 /* ... and wait until all of them have reported back that
3551 they're no longer running. */
3552 iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
3555 /* If we're not waiting for a specific LWP, choose an event LWP from
3556 among those that have had events. Giving equal priority to all
3557 LWPs that have had events helps prevent starvation. */
3558 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
3559 select_event_lwp (ptid, &lp, &status);
3561 gdb_assert (lp != NULL);
3563 /* Now that we've selected our final event LWP, un-adjust its PC if
3564 it was a software breakpoint, and we can't reliably support the
3565 "stopped by software breakpoint" stop reason. */
3566 if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
3567 && !USE_SIGTRAP_SIGINFO)
3569 struct regcache *regcache = get_thread_regcache (lp->ptid);
3570 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3571 int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
3577 pc = regcache_read_pc (regcache);
3578 regcache_write_pc (regcache, pc + decr_pc);
3582 /* We'll need this to determine whether to report a SIGSTOP as
3583 GDB_SIGNAL_0. Need to take a copy because resume_clear_callback
3585 last_resume_kind = lp->last_resume_kind;
3587 if (!target_is_non_stop_p ())
3589 /* In all-stop, from the core's perspective, all LWPs are now
3590 stopped until a new resume action is sent over. */
3591 iterate_over_lwps (minus_one_ptid, resume_clear_callback, NULL);
3595 resume_clear_callback (lp, NULL);
3598 if (linux_nat_status_is_event (status))
3600 if (debug_linux_nat)
3601 fprintf_unfiltered (gdb_stdlog,
3602 "LLW: trap ptid is %s.\n",
3603 target_pid_to_str (lp->ptid));
3606 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3608 *ourstatus = lp->waitstatus;
3609 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
3612 store_waitstatus (ourstatus, status);
3614 if (debug_linux_nat)
3615 fprintf_unfiltered (gdb_stdlog, "LLW: exit\n");
3617 restore_child_signals_mask (&prev_mask);
3619 if (last_resume_kind == resume_stop
3620 && ourstatus->kind == TARGET_WAITKIND_STOPPED
3621 && WSTOPSIG (status) == SIGSTOP)
3623 /* A thread that has been requested to stop by GDB with
3624 target_stop, and it stopped cleanly, so report as SIG0. The
3625 use of SIGSTOP is an implementation detail. */
3626 ourstatus->value.sig = GDB_SIGNAL_0;
3629 if (ourstatus->kind == TARGET_WAITKIND_EXITED
3630 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
3633 lp->core = linux_common_core_of_thread (lp->ptid);
3638 /* Resume LWPs that are currently stopped without any pending status
3639 to report, but are resumed from the core's perspective. */
3642 resume_stopped_resumed_lwps (struct lwp_info *lp, void *data)
3644 ptid_t *wait_ptid_p = (ptid_t *) data;
3648 if (debug_linux_nat)
3649 fprintf_unfiltered (gdb_stdlog,
3650 "RSRL: NOT resuming LWP %s, not stopped\n",
3651 target_pid_to_str (lp->ptid));
3653 else if (!lp->resumed)
3655 if (debug_linux_nat)
3656 fprintf_unfiltered (gdb_stdlog,
3657 "RSRL: NOT resuming LWP %s, not resumed\n",
3658 target_pid_to_str (lp->ptid));
3660 else if (lwp_status_pending_p (lp))
3662 if (debug_linux_nat)
3663 fprintf_unfiltered (gdb_stdlog,
3664 "RSRL: NOT resuming LWP %s, has pending status\n",
3665 target_pid_to_str (lp->ptid));
3669 struct regcache *regcache = get_thread_regcache (lp->ptid);
3670 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3674 CORE_ADDR pc = regcache_read_pc (regcache);
3675 int leave_stopped = 0;
3677 /* Don't bother if there's a breakpoint at PC that we'd hit
3678 immediately, and we're not waiting for this LWP. */
3679 if (!ptid_match (lp->ptid, *wait_ptid_p))
3681 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
3687 if (debug_linux_nat)
3688 fprintf_unfiltered (gdb_stdlog,
3689 "RSRL: resuming stopped-resumed LWP %s at "
3691 target_pid_to_str (lp->ptid),
3692 paddress (gdbarch, pc),
3695 linux_resume_one_lwp_throw (lp, lp->step, GDB_SIGNAL_0);
3698 CATCH (ex, RETURN_MASK_ERROR)
3700 if (!check_ptrace_stopped_lwp_gone (lp))
3701 throw_exception (ex);
3710 linux_nat_wait (struct target_ops *ops,
3711 ptid_t ptid, struct target_waitstatus *ourstatus,
3716 if (debug_linux_nat)
3718 char *options_string;
3720 options_string = target_options_to_string (target_options);
3721 fprintf_unfiltered (gdb_stdlog,
3722 "linux_nat_wait: [%s], [%s]\n",
3723 target_pid_to_str (ptid),
3725 xfree (options_string);
3728 /* Flush the async file first. */
3729 if (target_is_async_p ())
3730 async_file_flush ();
3732 /* Resume LWPs that are currently stopped without any pending status
3733 to report, but are resumed from the core's perspective. LWPs get
3734 in this state if we find them stopping at a time we're not
3735 interested in reporting the event (target_wait on a
3736 specific_process, for example, see linux_nat_wait_1), and
3737 meanwhile the event became uninteresting. Don't bother resuming
3738 LWPs we're not going to wait for if they'd stop immediately. */
3739 if (target_is_non_stop_p ())
3740 iterate_over_lwps (minus_one_ptid, resume_stopped_resumed_lwps, &ptid);
3742 event_ptid = linux_nat_wait_1 (ops, ptid, ourstatus, target_options);
3744 /* If we requested any event, and something came out, assume there
3745 may be more. If we requested a specific lwp or process, also
3746 assume there may be more. */
3747 if (target_is_async_p ()
3748 && ((ourstatus->kind != TARGET_WAITKIND_IGNORE
3749 && ourstatus->kind != TARGET_WAITKIND_NO_RESUMED)
3750 || !ptid_equal (ptid, minus_one_ptid)))
3757 kill_callback (struct lwp_info *lp, void *data)
3759 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
3762 kill_lwp (ptid_get_lwp (lp->ptid), SIGKILL);
3763 if (debug_linux_nat)
3765 int save_errno = errno;
3767 fprintf_unfiltered (gdb_stdlog,
3768 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
3769 target_pid_to_str (lp->ptid),
3770 save_errno ? safe_strerror (save_errno) : "OK");
3773 /* Some kernels ignore even SIGKILL for processes under ptrace. */
3776 ptrace (PTRACE_KILL, ptid_get_lwp (lp->ptid), 0, 0);
3777 if (debug_linux_nat)
3779 int save_errno = errno;
3781 fprintf_unfiltered (gdb_stdlog,
3782 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3783 target_pid_to_str (lp->ptid),
3784 save_errno ? safe_strerror (save_errno) : "OK");
3791 kill_wait_callback (struct lwp_info *lp, void *data)
3795 /* We must make sure that there are no pending events (delayed
3796 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3797 program doesn't interfere with any following debugging session. */
3799 /* For cloned processes we must check both with __WCLONE and
3800 without, since the exit status of a cloned process isn't reported
3806 pid = my_waitpid (ptid_get_lwp (lp->ptid), NULL, __WCLONE);
3807 if (pid != (pid_t) -1)
3809 if (debug_linux_nat)
3810 fprintf_unfiltered (gdb_stdlog,
3811 "KWC: wait %s received unknown.\n",
3812 target_pid_to_str (lp->ptid));
3813 /* The Linux kernel sometimes fails to kill a thread
3814 completely after PTRACE_KILL; that goes from the stop
3815 point in do_fork out to the one in
3816 get_signal_to_deliever and waits again. So kill it
3818 kill_callback (lp, NULL);
3821 while (pid == ptid_get_lwp (lp->ptid));
3823 gdb_assert (pid == -1 && errno == ECHILD);
3828 pid = my_waitpid (ptid_get_lwp (lp->ptid), NULL, 0);
3829 if (pid != (pid_t) -1)
3831 if (debug_linux_nat)
3832 fprintf_unfiltered (gdb_stdlog,
3833 "KWC: wait %s received unk.\n",
3834 target_pid_to_str (lp->ptid));
3835 /* See the call to kill_callback above. */
3836 kill_callback (lp, NULL);
3839 while (pid == ptid_get_lwp (lp->ptid));
3841 gdb_assert (pid == -1 && errno == ECHILD);
3846 linux_nat_kill (struct target_ops *ops)
3848 struct target_waitstatus last;
3852 /* If we're stopped while forking and we haven't followed yet,
3853 kill the other task. We need to do this first because the
3854 parent will be sleeping if this is a vfork. */
3856 get_last_target_status (&last_ptid, &last);
3858 if (last.kind == TARGET_WAITKIND_FORKED
3859 || last.kind == TARGET_WAITKIND_VFORKED)
3861 ptrace (PT_KILL, ptid_get_pid (last.value.related_pid), 0, 0);
3864 /* Let the arch-specific native code know this process is
3866 linux_nat_forget_process (ptid_get_pid (last.value.related_pid));
3869 if (forks_exist_p ())
3870 linux_fork_killall ();
3873 ptid_t ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
3875 /* Stop all threads before killing them, since ptrace requires
3876 that the thread is stopped to sucessfully PTRACE_KILL. */
3877 iterate_over_lwps (ptid, stop_callback, NULL);
3878 /* ... and wait until all of them have reported back that
3879 they're no longer running. */
3880 iterate_over_lwps (ptid, stop_wait_callback, NULL);
3882 /* Kill all LWP's ... */
3883 iterate_over_lwps (ptid, kill_callback, NULL);
3885 /* ... and wait until we've flushed all events. */
3886 iterate_over_lwps (ptid, kill_wait_callback, NULL);
3889 target_mourn_inferior ();
3893 linux_nat_mourn_inferior (struct target_ops *ops)
3895 int pid = ptid_get_pid (inferior_ptid);
3897 purge_lwp_list (pid);
3899 if (! forks_exist_p ())
3900 /* Normal case, no other forks available. */
3901 linux_ops->to_mourn_inferior (ops);
3903 /* Multi-fork case. The current inferior_ptid has exited, but
3904 there are other viable forks to debug. Delete the exiting
3905 one and context-switch to the first available. */
3906 linux_fork_mourn_inferior ();
3908 /* Let the arch-specific native code know this process is gone. */
3909 linux_nat_forget_process (pid);
3912 /* Convert a native/host siginfo object, into/from the siginfo in the
3913 layout of the inferiors' architecture. */
3916 siginfo_fixup (siginfo_t *siginfo, gdb_byte *inf_siginfo, int direction)
3920 if (linux_nat_siginfo_fixup != NULL)
3921 done = linux_nat_siginfo_fixup (siginfo, inf_siginfo, direction);
3923 /* If there was no callback, or the callback didn't do anything,
3924 then just do a straight memcpy. */
3928 memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
3930 memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
3934 static enum target_xfer_status
3935 linux_xfer_siginfo (struct target_ops *ops, enum target_object object,
3936 const char *annex, gdb_byte *readbuf,
3937 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
3938 ULONGEST *xfered_len)
3942 gdb_byte inf_siginfo[sizeof (siginfo_t)];
3944 gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO);
3945 gdb_assert (readbuf || writebuf);
3947 pid = ptid_get_lwp (inferior_ptid);
3949 pid = ptid_get_pid (inferior_ptid);
3951 if (offset > sizeof (siginfo))
3952 return TARGET_XFER_E_IO;
3955 ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
3957 return TARGET_XFER_E_IO;
3959 /* When GDB is built as a 64-bit application, ptrace writes into
3960 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3961 inferior with a 64-bit GDB should look the same as debugging it
3962 with a 32-bit GDB, we need to convert it. GDB core always sees
3963 the converted layout, so any read/write will have to be done
3965 siginfo_fixup (&siginfo, inf_siginfo, 0);
3967 if (offset + len > sizeof (siginfo))
3968 len = sizeof (siginfo) - offset;
3970 if (readbuf != NULL)
3971 memcpy (readbuf, inf_siginfo + offset, len);
3974 memcpy (inf_siginfo + offset, writebuf, len);
3976 /* Convert back to ptrace layout before flushing it out. */
3977 siginfo_fixup (&siginfo, inf_siginfo, 1);
3980 ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
3982 return TARGET_XFER_E_IO;
3986 return TARGET_XFER_OK;
3989 static enum target_xfer_status
3990 linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
3991 const char *annex, gdb_byte *readbuf,
3992 const gdb_byte *writebuf,
3993 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
3995 struct cleanup *old_chain;
3996 enum target_xfer_status xfer;
3998 if (object == TARGET_OBJECT_SIGNAL_INFO)
3999 return linux_xfer_siginfo (ops, object, annex, readbuf, writebuf,
4000 offset, len, xfered_len);
4002 /* The target is connected but no live inferior is selected. Pass
4003 this request down to a lower stratum (e.g., the executable
4005 if (object == TARGET_OBJECT_MEMORY && ptid_equal (inferior_ptid, null_ptid))
4006 return TARGET_XFER_EOF;
4008 old_chain = save_inferior_ptid ();
4010 if (ptid_lwp_p (inferior_ptid))
4011 inferior_ptid = pid_to_ptid (ptid_get_lwp (inferior_ptid));
4013 xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
4014 offset, len, xfered_len);
4016 do_cleanups (old_chain);
4021 linux_thread_alive (ptid_t ptid)
4025 gdb_assert (ptid_lwp_p (ptid));
4027 /* Send signal 0 instead of anything ptrace, because ptracing a
4028 running thread errors out claiming that the thread doesn't
4030 err = kill_lwp (ptid_get_lwp (ptid), 0);
4032 if (debug_linux_nat)
4033 fprintf_unfiltered (gdb_stdlog,
4034 "LLTA: KILL(SIG0) %s (%s)\n",
4035 target_pid_to_str (ptid),
4036 err ? safe_strerror (tmp_errno) : "OK");
4045 linux_nat_thread_alive (struct target_ops *ops, ptid_t ptid)
4047 return linux_thread_alive (ptid);
4050 /* Implement the to_update_thread_list target method for this
4054 linux_nat_update_thread_list (struct target_ops *ops)
4056 if (linux_supports_traceclone ())
4058 /* With support for clone events, we add/delete threads from the
4059 list as clone/exit events are processed, so just try deleting
4060 exited threads still in the thread list. */
4061 delete_exited_threads ();
4068 linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
4070 static char buf[64];
4072 if (ptid_lwp_p (ptid)
4073 && (ptid_get_pid (ptid) != ptid_get_lwp (ptid)
4074 || num_lwps (ptid_get_pid (ptid)) > 1))
4076 snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
4080 return normal_pid_to_str (ptid);
4084 linux_nat_thread_name (struct target_ops *self, struct thread_info *thr)
4086 int pid = ptid_get_pid (thr->ptid);
4087 long lwp = ptid_get_lwp (thr->ptid);
4088 #define FORMAT "/proc/%d/task/%ld/comm"
4089 char buf[sizeof (FORMAT) + 30];
4091 char *result = NULL;
4093 snprintf (buf, sizeof (buf), FORMAT, pid, lwp);
4094 comm_file = gdb_fopen_cloexec (buf, "r");
4097 /* Not exported by the kernel, so we define it here. */
4099 static char line[COMM_LEN + 1];
4101 if (fgets (line, sizeof (line), comm_file))
4103 char *nl = strchr (line, '\n');
4120 /* Accepts an integer PID; Returns a string representing a file that
4121 can be opened to get the symbols for the child process. */
4124 linux_child_pid_to_exec_file (struct target_ops *self, int pid)
4126 return linux_proc_pid_to_exec_file (pid);
4129 /* Implement the to_xfer_partial interface for memory reads using the /proc
4130 filesystem. Because we can use a single read() call for /proc, this
4131 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4132 but it doesn't support writes. */
4134 static enum target_xfer_status
4135 linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
4136 const char *annex, gdb_byte *readbuf,
4137 const gdb_byte *writebuf,
4138 ULONGEST offset, LONGEST len, ULONGEST *xfered_len)
4144 if (object != TARGET_OBJECT_MEMORY || !readbuf)
4145 return TARGET_XFER_EOF;
4147 /* Don't bother for one word. */
4148 if (len < 3 * sizeof (long))
4149 return TARGET_XFER_EOF;
4151 /* We could keep this file open and cache it - possibly one per
4152 thread. That requires some juggling, but is even faster. */
4153 xsnprintf (filename, sizeof filename, "/proc/%d/mem",
4154 ptid_get_pid (inferior_ptid));
4155 fd = gdb_open_cloexec (filename, O_RDONLY | O_LARGEFILE, 0);
4157 return TARGET_XFER_EOF;
4159 /* If pread64 is available, use it. It's faster if the kernel
4160 supports it (only one syscall), and it's 64-bit safe even on
4161 32-bit platforms (for instance, SPARC debugging a SPARC64
4164 if (pread64 (fd, readbuf, len, offset) != len)
4166 if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
4175 return TARGET_XFER_EOF;
4179 return TARGET_XFER_OK;
4184 /* Enumerate spufs IDs for process PID. */
4186 spu_enumerate_spu_ids (int pid, gdb_byte *buf, ULONGEST offset, ULONGEST len)
4188 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
4190 LONGEST written = 0;
4193 struct dirent *entry;
4195 xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
4196 dir = opendir (path);
4201 while ((entry = readdir (dir)) != NULL)
4207 fd = atoi (entry->d_name);
4211 xsnprintf (path, sizeof path, "/proc/%d/fd/%d", pid, fd);
4212 if (stat (path, &st) != 0)
4214 if (!S_ISDIR (st.st_mode))
4217 if (statfs (path, &stfs) != 0)
4219 if (stfs.f_type != SPUFS_MAGIC)
4222 if (pos >= offset && pos + 4 <= offset + len)
4224 store_unsigned_integer (buf + pos - offset, 4, byte_order, fd);
4234 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4235 object type, using the /proc file system. */
4237 static enum target_xfer_status
4238 linux_proc_xfer_spu (struct target_ops *ops, enum target_object object,
4239 const char *annex, gdb_byte *readbuf,
4240 const gdb_byte *writebuf,
4241 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
4246 int pid = ptid_get_pid (inferior_ptid);
4251 return TARGET_XFER_E_IO;
4254 LONGEST l = spu_enumerate_spu_ids (pid, readbuf, offset, len);
4257 return TARGET_XFER_E_IO;
4259 return TARGET_XFER_EOF;
4262 *xfered_len = (ULONGEST) l;
4263 return TARGET_XFER_OK;
4268 xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
4269 fd = gdb_open_cloexec (buf, writebuf? O_WRONLY : O_RDONLY, 0);
4271 return TARGET_XFER_E_IO;
4274 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
4277 return TARGET_XFER_EOF;
4281 ret = write (fd, writebuf, (size_t) len);
4283 ret = read (fd, readbuf, (size_t) len);
4288 return TARGET_XFER_E_IO;
4290 return TARGET_XFER_EOF;
4293 *xfered_len = (ULONGEST) ret;
4294 return TARGET_XFER_OK;
4299 /* Parse LINE as a signal set and add its set bits to SIGS. */
4302 add_line_to_sigset (const char *line, sigset_t *sigs)
4304 int len = strlen (line) - 1;
4308 if (line[len] != '\n')
4309 error (_("Could not parse signal set: %s"), line);
4317 if (*p >= '0' && *p <= '9')
4319 else if (*p >= 'a' && *p <= 'f')
4320 digit = *p - 'a' + 10;
4322 error (_("Could not parse signal set: %s"), line);
4327 sigaddset (sigs, signum + 1);
4329 sigaddset (sigs, signum + 2);
4331 sigaddset (sigs, signum + 3);
4333 sigaddset (sigs, signum + 4);
4339 /* Find process PID's pending signals from /proc/pid/status and set
4343 linux_proc_pending_signals (int pid, sigset_t *pending,
4344 sigset_t *blocked, sigset_t *ignored)
4347 char buffer[PATH_MAX], fname[PATH_MAX];
4348 struct cleanup *cleanup;
4350 sigemptyset (pending);
4351 sigemptyset (blocked);
4352 sigemptyset (ignored);
4353 xsnprintf (fname, sizeof fname, "/proc/%d/status", pid);
4354 procfile = gdb_fopen_cloexec (fname, "r");
4355 if (procfile == NULL)
4356 error (_("Could not open %s"), fname);
4357 cleanup = make_cleanup_fclose (procfile);
4359 while (fgets (buffer, PATH_MAX, procfile) != NULL)
4361 /* Normal queued signals are on the SigPnd line in the status
4362 file. However, 2.6 kernels also have a "shared" pending
4363 queue for delivering signals to a thread group, so check for
4366 Unfortunately some Red Hat kernels include the shared pending
4367 queue but not the ShdPnd status field. */
4369 if (startswith (buffer, "SigPnd:\t"))
4370 add_line_to_sigset (buffer + 8, pending);
4371 else if (startswith (buffer, "ShdPnd:\t"))
4372 add_line_to_sigset (buffer + 8, pending);
4373 else if (startswith (buffer, "SigBlk:\t"))
4374 add_line_to_sigset (buffer + 8, blocked);
4375 else if (startswith (buffer, "SigIgn:\t"))
4376 add_line_to_sigset (buffer + 8, ignored);
4379 do_cleanups (cleanup);
4382 static enum target_xfer_status
4383 linux_nat_xfer_osdata (struct target_ops *ops, enum target_object object,
4384 const char *annex, gdb_byte *readbuf,
4385 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
4386 ULONGEST *xfered_len)
4388 gdb_assert (object == TARGET_OBJECT_OSDATA);
4390 *xfered_len = linux_common_xfer_osdata (annex, readbuf, offset, len);
4391 if (*xfered_len == 0)
4392 return TARGET_XFER_EOF;
4394 return TARGET_XFER_OK;
4397 static enum target_xfer_status
4398 linux_xfer_partial (struct target_ops *ops, enum target_object object,
4399 const char *annex, gdb_byte *readbuf,
4400 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
4401 ULONGEST *xfered_len)
4403 enum target_xfer_status xfer;
4405 if (object == TARGET_OBJECT_AUXV)
4406 return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
4407 offset, len, xfered_len);
4409 if (object == TARGET_OBJECT_OSDATA)
4410 return linux_nat_xfer_osdata (ops, object, annex, readbuf, writebuf,
4411 offset, len, xfered_len);
4413 if (object == TARGET_OBJECT_SPU)
4414 return linux_proc_xfer_spu (ops, object, annex, readbuf, writebuf,
4415 offset, len, xfered_len);
4417 /* GDB calculates all the addresses in possibly larget width of the address.
4418 Address width needs to be masked before its final use - either by
4419 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4421 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4423 if (object == TARGET_OBJECT_MEMORY)
4425 int addr_bit = gdbarch_addr_bit (target_gdbarch ());
4427 if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
4428 offset &= ((ULONGEST) 1 << addr_bit) - 1;
4431 xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
4432 offset, len, xfered_len);
4433 if (xfer != TARGET_XFER_EOF)
4436 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
4437 offset, len, xfered_len);
4441 cleanup_target_stop (void *arg)
4443 ptid_t *ptid = (ptid_t *) arg;
4445 gdb_assert (arg != NULL);
4448 target_resume (*ptid, 0, GDB_SIGNAL_0);
4451 static VEC(static_tracepoint_marker_p) *
4452 linux_child_static_tracepoint_markers_by_strid (struct target_ops *self,
4455 char s[IPA_CMD_BUF_SIZE];
4456 struct cleanup *old_chain;
4457 int pid = ptid_get_pid (inferior_ptid);
4458 VEC(static_tracepoint_marker_p) *markers = NULL;
4459 struct static_tracepoint_marker *marker = NULL;
4461 ptid_t ptid = ptid_build (pid, 0, 0);
4466 memcpy (s, "qTfSTM", sizeof ("qTfSTM"));
4467 s[sizeof ("qTfSTM")] = 0;
4469 agent_run_command (pid, s, strlen (s) + 1);
4471 old_chain = make_cleanup (free_current_marker, &marker);
4472 make_cleanup (cleanup_target_stop, &ptid);
4477 marker = XCNEW (struct static_tracepoint_marker);
4481 parse_static_tracepoint_marker_definition (p, &p, marker);
4483 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
4485 VEC_safe_push (static_tracepoint_marker_p,
4491 release_static_tracepoint_marker (marker);
4492 memset (marker, 0, sizeof (*marker));
4495 while (*p++ == ','); /* comma-separated list */
4497 memcpy (s, "qTsSTM", sizeof ("qTsSTM"));
4498 s[sizeof ("qTsSTM")] = 0;
4499 agent_run_command (pid, s, strlen (s) + 1);
4503 do_cleanups (old_chain);
4508 /* Create a prototype generic GNU/Linux target. The client can override
4509 it with local methods. */
4512 linux_target_install_ops (struct target_ops *t)
4514 t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
4515 t->to_remove_fork_catchpoint = linux_child_remove_fork_catchpoint;
4516 t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
4517 t->to_remove_vfork_catchpoint = linux_child_remove_vfork_catchpoint;
4518 t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
4519 t->to_remove_exec_catchpoint = linux_child_remove_exec_catchpoint;
4520 t->to_set_syscall_catchpoint = linux_child_set_syscall_catchpoint;
4521 t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
4522 t->to_post_startup_inferior = linux_child_post_startup_inferior;
4523 t->to_post_attach = linux_child_post_attach;
4524 t->to_follow_fork = linux_child_follow_fork;
4526 super_xfer_partial = t->to_xfer_partial;
4527 t->to_xfer_partial = linux_xfer_partial;
4529 t->to_static_tracepoint_markers_by_strid
4530 = linux_child_static_tracepoint_markers_by_strid;
4536 struct target_ops *t;
4538 t = inf_ptrace_target ();
4539 linux_target_install_ops (t);
4545 linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
4547 struct target_ops *t;
4549 t = inf_ptrace_trad_target (register_u_offset);
4550 linux_target_install_ops (t);
4555 /* target_is_async_p implementation. */
4558 linux_nat_is_async_p (struct target_ops *ops)
4560 return linux_is_async_p ();
4563 /* target_can_async_p implementation. */
4566 linux_nat_can_async_p (struct target_ops *ops)
4568 /* NOTE: palves 2008-03-21: We're only async when the user requests
4569 it explicitly with the "set target-async" command.
4570 Someday, linux will always be async. */
4571 return target_async_permitted;
4575 linux_nat_supports_non_stop (struct target_ops *self)
4580 /* to_always_non_stop_p implementation. */
4583 linux_nat_always_non_stop_p (struct target_ops *self)
4588 /* True if we want to support multi-process. To be removed when GDB
4589 supports multi-exec. */
4591 int linux_multi_process = 1;
4594 linux_nat_supports_multi_process (struct target_ops *self)
4596 return linux_multi_process;
4600 linux_nat_supports_disable_randomization (struct target_ops *self)
4602 #ifdef HAVE_PERSONALITY
4609 static int async_terminal_is_ours = 1;
4611 /* target_terminal_inferior implementation.
4613 This is a wrapper around child_terminal_inferior to add async support. */
4616 linux_nat_terminal_inferior (struct target_ops *self)
4618 child_terminal_inferior (self);
4620 /* Calls to target_terminal_*() are meant to be idempotent. */
4621 if (!async_terminal_is_ours)
4624 delete_file_handler (input_fd);
4625 async_terminal_is_ours = 0;
4629 /* target_terminal_ours implementation.
4631 This is a wrapper around child_terminal_ours to add async support (and
4632 implement the target_terminal_ours vs target_terminal_ours_for_output
4633 distinction). child_terminal_ours is currently no different than
4634 child_terminal_ours_for_output.
4635 We leave target_terminal_ours_for_output alone, leaving it to
4636 child_terminal_ours_for_output. */
4639 linux_nat_terminal_ours (struct target_ops *self)
4641 /* GDB should never give the terminal to the inferior if the
4642 inferior is running in the background (run&, continue&, etc.),
4643 but claiming it sure should. */
4644 child_terminal_ours (self);
4646 if (async_terminal_is_ours)
4649 clear_sigint_trap ();
4650 add_file_handler (input_fd, stdin_event_handler, 0);
4651 async_terminal_is_ours = 1;
4654 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4655 so we notice when any child changes state, and notify the
4656 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4657 above to wait for the arrival of a SIGCHLD. */
4660 sigchld_handler (int signo)
4662 int old_errno = errno;
4664 if (debug_linux_nat)
4665 ui_file_write_async_safe (gdb_stdlog,
4666 "sigchld\n", sizeof ("sigchld\n") - 1);
4668 if (signo == SIGCHLD
4669 && linux_nat_event_pipe[0] != -1)
4670 async_file_mark (); /* Let the event loop know that there are
4671 events to handle. */
4676 /* Callback registered with the target events file descriptor. */
4679 handle_target_event (int error, gdb_client_data client_data)
4681 inferior_event_handler (INF_REG_EVENT, NULL);
4684 /* Create/destroy the target events pipe. Returns previous state. */
4687 linux_async_pipe (int enable)
4689 int previous = linux_is_async_p ();
4691 if (previous != enable)
4695 /* Block child signals while we create/destroy the pipe, as
4696 their handler writes to it. */
4697 block_child_signals (&prev_mask);
4701 if (gdb_pipe_cloexec (linux_nat_event_pipe) == -1)
4702 internal_error (__FILE__, __LINE__,
4703 "creating event pipe failed.");
4705 fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
4706 fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
4710 close (linux_nat_event_pipe[0]);
4711 close (linux_nat_event_pipe[1]);
4712 linux_nat_event_pipe[0] = -1;
4713 linux_nat_event_pipe[1] = -1;
4716 restore_child_signals_mask (&prev_mask);
4722 /* target_async implementation. */
4725 linux_nat_async (struct target_ops *ops, int enable)
4729 if (!linux_async_pipe (1))
4731 add_file_handler (linux_nat_event_pipe[0],
4732 handle_target_event, NULL);
4733 /* There may be pending events to handle. Tell the event loop
4740 delete_file_handler (linux_nat_event_pipe[0]);
4741 linux_async_pipe (0);
4746 /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
4750 linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
4754 if (debug_linux_nat)
4755 fprintf_unfiltered (gdb_stdlog,
4756 "LNSL: running -> suspending %s\n",
4757 target_pid_to_str (lwp->ptid));
4760 if (lwp->last_resume_kind == resume_stop)
4762 if (debug_linux_nat)
4763 fprintf_unfiltered (gdb_stdlog,
4764 "linux-nat: already stopping LWP %ld at "
4766 ptid_get_lwp (lwp->ptid));
4770 stop_callback (lwp, NULL);
4771 lwp->last_resume_kind = resume_stop;
4775 /* Already known to be stopped; do nothing. */
4777 if (debug_linux_nat)
4779 if (find_thread_ptid (lwp->ptid)->stop_requested)
4780 fprintf_unfiltered (gdb_stdlog,
4781 "LNSL: already stopped/stop_requested %s\n",
4782 target_pid_to_str (lwp->ptid));
4784 fprintf_unfiltered (gdb_stdlog,
4785 "LNSL: already stopped/no "
4786 "stop_requested yet %s\n",
4787 target_pid_to_str (lwp->ptid));
4794 linux_nat_stop (struct target_ops *self, ptid_t ptid)
4796 iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
4800 linux_nat_interrupt (struct target_ops *self, ptid_t ptid)
4803 iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
4805 linux_ops->to_interrupt (linux_ops, ptid);
4809 linux_nat_close (struct target_ops *self)
4811 /* Unregister from the event loop. */
4812 if (linux_nat_is_async_p (self))
4813 linux_nat_async (self, 0);
4815 if (linux_ops->to_close)
4816 linux_ops->to_close (linux_ops);
4821 /* When requests are passed down from the linux-nat layer to the
4822 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
4823 used. The address space pointer is stored in the inferior object,
4824 but the common code that is passed such ptid can't tell whether
4825 lwpid is a "main" process id or not (it assumes so). We reverse
4826 look up the "main" process id from the lwp here. */
4828 static struct address_space *
4829 linux_nat_thread_address_space (struct target_ops *t, ptid_t ptid)
4831 struct lwp_info *lwp;
4832 struct inferior *inf;
4835 if (ptid_get_lwp (ptid) == 0)
4837 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
4839 lwp = find_lwp_pid (ptid);
4840 pid = ptid_get_pid (lwp->ptid);
4844 /* A (pid,lwpid,0) ptid. */
4845 pid = ptid_get_pid (ptid);
4848 inf = find_inferior_pid (pid);
4849 gdb_assert (inf != NULL);
4853 /* Return the cached value of the processor core for thread PTID. */
4856 linux_nat_core_of_thread (struct target_ops *ops, ptid_t ptid)
4858 struct lwp_info *info = find_lwp_pid (ptid);
4865 /* Implementation of to_filesystem_is_local. */
4868 linux_nat_filesystem_is_local (struct target_ops *ops)
4870 struct inferior *inf = current_inferior ();
4872 if (inf->fake_pid_p || inf->pid == 0)
4875 return linux_ns_same (inf->pid, LINUX_NS_MNT);
4878 /* Convert the INF argument passed to a to_fileio_* method
4879 to a process ID suitable for passing to its corresponding
4880 linux_mntns_* function. If INF is non-NULL then the
4881 caller is requesting the filesystem seen by INF. If INF
4882 is NULL then the caller is requesting the filesystem seen
4883 by the GDB. We fall back to GDB's filesystem in the case
4884 that INF is non-NULL but its PID is unknown. */
4887 linux_nat_fileio_pid_of (struct inferior *inf)
4889 if (inf == NULL || inf->fake_pid_p || inf->pid == 0)
4895 /* Implementation of to_fileio_open. */
4898 linux_nat_fileio_open (struct target_ops *self,
4899 struct inferior *inf, const char *filename,
4900 int flags, int mode, int warn_if_slow,
4907 if (fileio_to_host_openflags (flags, &nat_flags) == -1
4908 || fileio_to_host_mode (mode, &nat_mode) == -1)
4910 *target_errno = FILEIO_EINVAL;
4914 fd = linux_mntns_open_cloexec (linux_nat_fileio_pid_of (inf),
4915 filename, nat_flags, nat_mode);
4917 *target_errno = host_to_fileio_error (errno);
4922 /* Implementation of to_fileio_readlink. */
4925 linux_nat_fileio_readlink (struct target_ops *self,
4926 struct inferior *inf, const char *filename,
4933 len = linux_mntns_readlink (linux_nat_fileio_pid_of (inf),
4934 filename, buf, sizeof (buf));
4937 *target_errno = host_to_fileio_error (errno);
4941 ret = (char *) xmalloc (len + 1);
4942 memcpy (ret, buf, len);
4947 /* Implementation of to_fileio_unlink. */
4950 linux_nat_fileio_unlink (struct target_ops *self,
4951 struct inferior *inf, const char *filename,
4956 ret = linux_mntns_unlink (linux_nat_fileio_pid_of (inf),
4959 *target_errno = host_to_fileio_error (errno);
4965 linux_nat_add_target (struct target_ops *t)
4967 /* Save the provided single-threaded target. We save this in a separate
4968 variable because another target we've inherited from (e.g. inf-ptrace)
4969 may have saved a pointer to T; we want to use it for the final
4970 process stratum target. */
4971 linux_ops_saved = *t;
4972 linux_ops = &linux_ops_saved;
4974 /* Override some methods for multithreading. */
4975 t->to_create_inferior = linux_nat_create_inferior;
4976 t->to_attach = linux_nat_attach;
4977 t->to_detach = linux_nat_detach;
4978 t->to_resume = linux_nat_resume;
4979 t->to_wait = linux_nat_wait;
4980 t->to_pass_signals = linux_nat_pass_signals;
4981 t->to_xfer_partial = linux_nat_xfer_partial;
4982 t->to_kill = linux_nat_kill;
4983 t->to_mourn_inferior = linux_nat_mourn_inferior;
4984 t->to_thread_alive = linux_nat_thread_alive;
4985 t->to_update_thread_list = linux_nat_update_thread_list;
4986 t->to_pid_to_str = linux_nat_pid_to_str;
4987 t->to_thread_name = linux_nat_thread_name;
4988 t->to_has_thread_control = tc_schedlock;
4989 t->to_thread_address_space = linux_nat_thread_address_space;
4990 t->to_stopped_by_watchpoint = linux_nat_stopped_by_watchpoint;
4991 t->to_stopped_data_address = linux_nat_stopped_data_address;
4992 t->to_stopped_by_sw_breakpoint = linux_nat_stopped_by_sw_breakpoint;
4993 t->to_supports_stopped_by_sw_breakpoint = linux_nat_supports_stopped_by_sw_breakpoint;
4994 t->to_stopped_by_hw_breakpoint = linux_nat_stopped_by_hw_breakpoint;
4995 t->to_supports_stopped_by_hw_breakpoint = linux_nat_supports_stopped_by_hw_breakpoint;
4997 t->to_can_async_p = linux_nat_can_async_p;
4998 t->to_is_async_p = linux_nat_is_async_p;
4999 t->to_supports_non_stop = linux_nat_supports_non_stop;
5000 t->to_always_non_stop_p = linux_nat_always_non_stop_p;
5001 t->to_async = linux_nat_async;
5002 t->to_terminal_inferior = linux_nat_terminal_inferior;
5003 t->to_terminal_ours = linux_nat_terminal_ours;
5005 super_close = t->to_close;
5006 t->to_close = linux_nat_close;
5008 t->to_stop = linux_nat_stop;
5009 t->to_interrupt = linux_nat_interrupt;
5011 t->to_supports_multi_process = linux_nat_supports_multi_process;
5013 t->to_supports_disable_randomization
5014 = linux_nat_supports_disable_randomization;
5016 t->to_core_of_thread = linux_nat_core_of_thread;
5018 t->to_filesystem_is_local = linux_nat_filesystem_is_local;
5019 t->to_fileio_open = linux_nat_fileio_open;
5020 t->to_fileio_readlink = linux_nat_fileio_readlink;
5021 t->to_fileio_unlink = linux_nat_fileio_unlink;
5023 /* We don't change the stratum; this target will sit at
5024 process_stratum and thread_db will set at thread_stratum. This
5025 is a little strange, since this is a multi-threaded-capable
5026 target, but we want to be on the stack below thread_db, and we
5027 also want to be used for single-threaded processes. */
5032 /* Register a method to call whenever a new thread is attached. */
5034 linux_nat_set_new_thread (struct target_ops *t,
5035 void (*new_thread) (struct lwp_info *))
5037 /* Save the pointer. We only support a single registered instance
5038 of the GNU/Linux native target, so we do not need to map this to
5040 linux_nat_new_thread = new_thread;
5043 /* See declaration in linux-nat.h. */
5046 linux_nat_set_new_fork (struct target_ops *t,
5047 linux_nat_new_fork_ftype *new_fork)
5049 /* Save the pointer. */
5050 linux_nat_new_fork = new_fork;
5053 /* See declaration in linux-nat.h. */
5056 linux_nat_set_forget_process (struct target_ops *t,
5057 linux_nat_forget_process_ftype *fn)
5059 /* Save the pointer. */
5060 linux_nat_forget_process_hook = fn;
5063 /* See declaration in linux-nat.h. */
5066 linux_nat_forget_process (pid_t pid)
5068 if (linux_nat_forget_process_hook != NULL)
5069 linux_nat_forget_process_hook (pid);
5072 /* Register a method that converts a siginfo object between the layout
5073 that ptrace returns, and the layout in the architecture of the
5076 linux_nat_set_siginfo_fixup (struct target_ops *t,
5077 int (*siginfo_fixup) (siginfo_t *,
5081 /* Save the pointer. */
5082 linux_nat_siginfo_fixup = siginfo_fixup;
5085 /* Register a method to call prior to resuming a thread. */
5088 linux_nat_set_prepare_to_resume (struct target_ops *t,
5089 void (*prepare_to_resume) (struct lwp_info *))
5091 /* Save the pointer. */
5092 linux_nat_prepare_to_resume = prepare_to_resume;
5095 /* See linux-nat.h. */
5098 linux_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo)
5102 pid = ptid_get_lwp (ptid);
5104 pid = ptid_get_pid (ptid);
5107 ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, siginfo);
5110 memset (siginfo, 0, sizeof (*siginfo));
5116 /* See nat/linux-nat.h. */
5119 current_lwp_ptid (void)
5121 gdb_assert (ptid_lwp_p (inferior_ptid));
5122 return inferior_ptid;
5125 /* Provide a prototype to silence -Wmissing-prototypes. */
5126 extern initialize_file_ftype _initialize_linux_nat;
5129 _initialize_linux_nat (void)
5131 add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance,
5132 &debug_linux_nat, _("\
5133 Set debugging of GNU/Linux lwp module."), _("\
5134 Show debugging of GNU/Linux lwp module."), _("\
5135 Enables printf debugging output."),
5137 show_debug_linux_nat,
5138 &setdebuglist, &showdebuglist);
5140 add_setshow_boolean_cmd ("linux-namespaces", class_maintenance,
5141 &debug_linux_namespaces, _("\
5142 Set debugging of GNU/Linux namespaces module."), _("\
5143 Show debugging of GNU/Linux namespaces module."), _("\
5144 Enables printf debugging output."),
5147 &setdebuglist, &showdebuglist);
5149 /* Save this mask as the default. */
5150 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
5152 /* Install a SIGCHLD handler. */
5153 sigchld_action.sa_handler = sigchld_handler;
5154 sigemptyset (&sigchld_action.sa_mask);
5155 sigchld_action.sa_flags = SA_RESTART;
5157 /* Make it the default. */
5158 sigaction (SIGCHLD, &sigchld_action, NULL);
5160 /* Make sure we don't block SIGCHLD during a sigsuspend. */
5161 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
5162 sigdelset (&suspend_mask, SIGCHLD);
5164 sigemptyset (&blocked_mask);
5168 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
5169 the GNU/Linux Threads library and therefore doesn't really belong
5172 /* Read variable NAME in the target and return its value if found.
5173 Otherwise return zero. It is assumed that the type of the variable
5177 get_signo (const char *name)
5179 struct bound_minimal_symbol ms;
5182 ms = lookup_minimal_symbol (name, NULL, NULL);
5183 if (ms.minsym == NULL)
5186 if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms), (gdb_byte *) &signo,
5187 sizeof (signo)) != 0)
5193 /* Return the set of signals used by the threads library in *SET. */
5196 lin_thread_get_thread_signals (sigset_t *set)
5198 struct sigaction action;
5199 int restart, cancel;
5201 sigemptyset (&blocked_mask);
5204 restart = get_signo ("__pthread_sig_restart");
5205 cancel = get_signo ("__pthread_sig_cancel");
5207 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5208 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5209 not provide any way for the debugger to query the signal numbers -
5210 fortunately they don't change! */
5213 restart = __SIGRTMIN;
5216 cancel = __SIGRTMIN + 1;
5218 sigaddset (set, restart);
5219 sigaddset (set, cancel);
5221 /* The GNU/Linux Threads library makes terminating threads send a
5222 special "cancel" signal instead of SIGCHLD. Make sure we catch
5223 those (to prevent them from terminating GDB itself, which is
5224 likely to be their default action) and treat them the same way as
5227 action.sa_handler = sigchld_handler;
5228 sigemptyset (&action.sa_mask);
5229 action.sa_flags = SA_RESTART;
5230 sigaction (cancel, &action, NULL);
5232 /* We block the "cancel" signal throughout this code ... */
5233 sigaddset (&blocked_mask, cancel);
5234 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
5236 /* ... except during a sigsuspend. */
5237 sigdelset (&suspend_mask, cancel);