1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001-2012 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/>. */
23 #include "gdb_string.h"
25 #include "gdb_assert.h"
26 #ifdef HAVE_TKILL_SYSCALL
28 #include <sys/syscall.h>
30 #include <sys/ptrace.h>
31 #include "linux-nat.h"
32 #include "linux-ptrace.h"
33 #include "linux-procfs.h"
34 #include "linux-fork.h"
35 #include "gdbthread.h"
39 #include "inf-child.h"
40 #include "inf-ptrace.h"
42 #include <sys/param.h> /* for MAXPATHLEN */
43 #include <sys/procfs.h> /* for elf_gregset etc. */
44 #include "elf-bfd.h" /* for elfcore_write_* */
45 #include "gregset.h" /* for gregset */
46 #include "gdbcore.h" /* for get_exec_file */
47 #include <ctype.h> /* for isdigit */
48 #include "gdbthread.h" /* for struct thread_info etc. */
49 #include "gdb_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>
56 #include "gdb_dirent.h"
57 #include "xml-support.h"
61 #include "linux-osdata.h"
62 #include "linux-tdep.h"
65 #include "tracepoint.h"
66 #include "exceptions.h"
67 #include "linux-ptrace.h"
71 #define SPUFS_MAGIC 0x23c9b64e
74 #ifdef HAVE_PERSONALITY
75 # include <sys/personality.h>
76 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
77 # define ADDR_NO_RANDOMIZE 0x0040000
79 #endif /* HAVE_PERSONALITY */
81 /* This comment documents high-level logic of this file.
83 Waiting for events in sync mode
84 ===============================
86 When waiting for an event in a specific thread, we just use waitpid, passing
87 the specific pid, and not passing WNOHANG.
89 When waiting for an event in all threads, waitpid is not quite good. Prior to
90 version 2.4, Linux can either wait for event in main thread, or in secondary
91 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
92 miss an event. The solution is to use non-blocking waitpid, together with
93 sigsuspend. First, we use non-blocking waitpid to get an event in the main
94 process, if any. Second, we use non-blocking waitpid with the __WCLONED
95 flag to check for events in cloned processes. If nothing is found, we use
96 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
97 happened to a child process -- and SIGCHLD will be delivered both for events
98 in main debugged process and in cloned processes. As soon as we know there's
99 an event, we get back to calling nonblocking waitpid with and without
102 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
103 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
104 blocked, the signal becomes pending and sigsuspend immediately
105 notices it and returns.
107 Waiting for events in async mode
108 ================================
110 In async mode, GDB should always be ready to handle both user input
111 and target events, so neither blocking waitpid nor sigsuspend are
112 viable options. Instead, we should asynchronously notify the GDB main
113 event loop whenever there's an unprocessed event from the target. We
114 detect asynchronous target events by handling SIGCHLD signals. To
115 notify the event loop about target events, the self-pipe trick is used
116 --- a pipe is registered as waitable event source in the event loop,
117 the event loop select/poll's on the read end of this pipe (as well on
118 other event sources, e.g., stdin), and the SIGCHLD handler writes a
119 byte to this pipe. This is more portable than relying on
120 pselect/ppoll, since on kernels that lack those syscalls, libc
121 emulates them with select/poll+sigprocmask, and that is racy
122 (a.k.a. plain broken).
124 Obviously, if we fail to notify the event loop if there's a target
125 event, it's bad. OTOH, if we notify the event loop when there's no
126 event from the target, linux_nat_wait will detect that there's no real
127 event to report, and return event of type TARGET_WAITKIND_IGNORE.
128 This is mostly harmless, but it will waste time and is better avoided.
130 The main design point is that every time GDB is outside linux-nat.c,
131 we have a SIGCHLD handler installed that is called when something
132 happens to the target and notifies the GDB event loop. Whenever GDB
133 core decides to handle the event, and calls into linux-nat.c, we
134 process things as in sync mode, except that the we never block in
137 While processing an event, we may end up momentarily blocked in
138 waitpid calls. Those waitpid calls, while blocking, are guarantied to
139 return quickly. E.g., in all-stop mode, before reporting to the core
140 that an LWP hit a breakpoint, all LWPs are stopped by sending them
141 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
142 Note that this is different from blocking indefinitely waiting for the
143 next event --- here, we're already handling an event.
148 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
149 signal is not entirely significant; we just need for a signal to be delivered,
150 so that we can intercept it. SIGSTOP's advantage is that it can not be
151 blocked. A disadvantage is that it is not a real-time signal, so it can only
152 be queued once; we do not keep track of other sources of SIGSTOP.
154 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
155 use them, because they have special behavior when the signal is generated -
156 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
157 kills the entire thread group.
159 A delivered SIGSTOP would stop the entire thread group, not just the thread we
160 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
161 cancel it (by PTRACE_CONT without passing SIGSTOP).
163 We could use a real-time signal instead. This would solve those problems; we
164 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
165 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
166 generates it, and there are races with trying to find a signal that is not
170 #define O_LARGEFILE 0
173 /* Unlike other extended result codes, WSTOPSIG (status) on
174 PTRACE_O_TRACESYSGOOD syscall events doesn't return SIGTRAP, but
175 instead SIGTRAP with bit 7 set. */
176 #define SYSCALL_SIGTRAP (SIGTRAP | 0x80)
178 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
179 the use of the multi-threaded target. */
180 static struct target_ops *linux_ops;
181 static struct target_ops linux_ops_saved;
183 /* The method to call, if any, when a new thread is attached. */
184 static void (*linux_nat_new_thread) (struct lwp_info *);
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 LONGEST (*super_xfer_partial) (struct target_ops *,
200 const char *, gdb_byte *,
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 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
222 can not be used, 1 if it can. */
224 static int linux_supports_tracefork_flag = -1;
226 /* This variable is a tri-state flag: -1 for unknown, 0 if
227 PTRACE_O_TRACESYSGOOD can not be used, 1 if it can. */
229 static int linux_supports_tracesysgood_flag = -1;
231 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
232 PTRACE_O_TRACEVFORKDONE. */
234 static int linux_supports_tracevforkdone_flag = -1;
236 /* Stores the current used ptrace() options. */
237 static int current_ptrace_options = 0;
239 /* Async mode support. */
241 /* The read/write ends of the pipe registered as waitable file in the
243 static int linux_nat_event_pipe[2] = { -1, -1 };
245 /* Flush the event pipe. */
248 async_file_flush (void)
255 ret = read (linux_nat_event_pipe[0], &buf, 1);
257 while (ret >= 0 || (ret == -1 && errno == EINTR));
260 /* Put something (anything, doesn't matter what, or how much) in event
261 pipe, so that the select/poll in the event-loop realizes we have
262 something to process. */
265 async_file_mark (void)
269 /* It doesn't really matter what the pipe contains, as long we end
270 up with something in it. Might as well flush the previous
276 ret = write (linux_nat_event_pipe[1], "+", 1);
278 while (ret == -1 && errno == EINTR);
280 /* Ignore EAGAIN. If the pipe is full, the event loop will already
281 be awakened anyway. */
284 static void linux_nat_async (void (*callback)
285 (enum inferior_event_type event_type,
288 static int kill_lwp (int lwpid, int signo);
290 static int stop_callback (struct lwp_info *lp, void *data);
292 static void block_child_signals (sigset_t *prev_mask);
293 static void restore_child_signals_mask (sigset_t *prev_mask);
296 static struct lwp_info *add_lwp (ptid_t ptid);
297 static void purge_lwp_list (int pid);
298 static void delete_lwp (ptid_t ptid);
299 static struct lwp_info *find_lwp_pid (ptid_t ptid);
302 /* Trivial list manipulation functions to keep track of a list of
303 new stopped processes. */
305 add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
307 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
310 new_pid->status = status;
311 new_pid->next = *listp;
316 in_pid_list_p (struct simple_pid_list *list, int pid)
318 struct simple_pid_list *p;
320 for (p = list; p != NULL; p = p->next)
327 pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
329 struct simple_pid_list **p;
331 for (p = listp; *p != NULL; p = &(*p)->next)
332 if ((*p)->pid == pid)
334 struct simple_pid_list *next = (*p)->next;
336 *statusp = (*p)->status;
345 /* A helper function for linux_test_for_tracefork, called after fork (). */
348 linux_tracefork_child (void)
350 ptrace (PTRACE_TRACEME, 0, 0, 0);
351 kill (getpid (), SIGSTOP);
356 /* Wrapper function for waitpid which handles EINTR. */
359 my_waitpid (int pid, int *statusp, int flags)
365 ret = waitpid (pid, statusp, flags);
367 while (ret == -1 && errno == EINTR);
372 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
374 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
375 we know that the feature is not available. This may change the tracing
376 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
378 However, if it succeeds, we don't know for sure that the feature is
379 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
380 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
381 fork tracing, and let it fork. If the process exits, we assume that we
382 can't use TRACEFORK; if we get the fork notification, and we can extract
383 the new child's PID, then we assume that we can. */
386 linux_test_for_tracefork (int original_pid)
388 int child_pid, ret, status;
392 /* We don't want those ptrace calls to be interrupted. */
393 block_child_signals (&prev_mask);
395 linux_supports_tracefork_flag = 0;
396 linux_supports_tracevforkdone_flag = 0;
398 ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK);
401 restore_child_signals_mask (&prev_mask);
407 perror_with_name (("fork"));
410 linux_tracefork_child ();
412 ret = my_waitpid (child_pid, &status, 0);
414 perror_with_name (("waitpid"));
415 else if (ret != child_pid)
416 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret);
417 if (! WIFSTOPPED (status))
418 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."),
421 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
424 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
427 warning (_("linux_test_for_tracefork: failed to kill child"));
428 restore_child_signals_mask (&prev_mask);
432 ret = my_waitpid (child_pid, &status, 0);
433 if (ret != child_pid)
434 warning (_("linux_test_for_tracefork: failed "
435 "to wait for killed child"));
436 else if (!WIFSIGNALED (status))
437 warning (_("linux_test_for_tracefork: unexpected "
438 "wait status 0x%x from killed child"), status);
440 restore_child_signals_mask (&prev_mask);
444 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
445 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
446 PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
447 linux_supports_tracevforkdone_flag = (ret == 0);
449 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
451 warning (_("linux_test_for_tracefork: failed to resume child"));
453 ret = my_waitpid (child_pid, &status, 0);
455 if (ret == child_pid && WIFSTOPPED (status)
456 && status >> 16 == PTRACE_EVENT_FORK)
459 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
460 if (ret == 0 && second_pid != 0)
464 linux_supports_tracefork_flag = 1;
465 my_waitpid (second_pid, &second_status, 0);
466 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
468 warning (_("linux_test_for_tracefork: "
469 "failed to kill second child"));
470 my_waitpid (second_pid, &status, 0);
474 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
475 "(%d, status 0x%x)"), ret, status);
477 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
479 warning (_("linux_test_for_tracefork: failed to kill child"));
480 my_waitpid (child_pid, &status, 0);
482 restore_child_signals_mask (&prev_mask);
485 /* Determine if PTRACE_O_TRACESYSGOOD can be used to follow syscalls.
487 We try to enable syscall tracing on ORIGINAL_PID. If this fails,
488 we know that the feature is not available. This may change the tracing
489 options for ORIGINAL_PID, but we'll be setting them shortly anyway. */
492 linux_test_for_tracesysgood (int original_pid)
497 /* We don't want those ptrace calls to be interrupted. */
498 block_child_signals (&prev_mask);
500 linux_supports_tracesysgood_flag = 0;
502 ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACESYSGOOD);
506 linux_supports_tracesysgood_flag = 1;
508 restore_child_signals_mask (&prev_mask);
511 /* Determine wether we support PTRACE_O_TRACESYSGOOD option available.
512 This function also sets linux_supports_tracesysgood_flag. */
515 linux_supports_tracesysgood (int pid)
517 if (linux_supports_tracesysgood_flag == -1)
518 linux_test_for_tracesysgood (pid);
519 return linux_supports_tracesysgood_flag;
522 /* Return non-zero iff we have tracefork functionality available.
523 This function also sets linux_supports_tracefork_flag. */
526 linux_supports_tracefork (int pid)
528 if (linux_supports_tracefork_flag == -1)
529 linux_test_for_tracefork (pid);
530 return linux_supports_tracefork_flag;
534 linux_supports_tracevforkdone (int pid)
536 if (linux_supports_tracefork_flag == -1)
537 linux_test_for_tracefork (pid);
538 return linux_supports_tracevforkdone_flag;
542 linux_enable_tracesysgood (ptid_t ptid)
544 int pid = ptid_get_lwp (ptid);
547 pid = ptid_get_pid (ptid);
549 if (linux_supports_tracesysgood (pid) == 0)
552 current_ptrace_options |= PTRACE_O_TRACESYSGOOD;
554 ptrace (PTRACE_SETOPTIONS, pid, 0, current_ptrace_options);
559 linux_enable_event_reporting (ptid_t ptid)
561 int pid = ptid_get_lwp (ptid);
564 pid = ptid_get_pid (ptid);
566 if (! linux_supports_tracefork (pid))
569 current_ptrace_options |= PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK
570 | PTRACE_O_TRACEEXEC | PTRACE_O_TRACECLONE;
572 if (linux_supports_tracevforkdone (pid))
573 current_ptrace_options |= PTRACE_O_TRACEVFORKDONE;
575 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
576 read-only process state. */
578 ptrace (PTRACE_SETOPTIONS, pid, 0, current_ptrace_options);
582 linux_child_post_attach (int pid)
584 linux_enable_event_reporting (pid_to_ptid (pid));
585 linux_enable_tracesysgood (pid_to_ptid (pid));
586 linux_ptrace_init_warnings ();
590 linux_child_post_startup_inferior (ptid_t ptid)
592 linux_enable_event_reporting (ptid);
593 linux_enable_tracesysgood (ptid);
594 linux_ptrace_init_warnings ();
597 /* Return the number of known LWPs in the tgid given by PID. */
605 for (lp = lwp_list; lp; lp = lp->next)
606 if (ptid_get_pid (lp->ptid) == pid)
612 /* Call delete_lwp with prototype compatible for make_cleanup. */
615 delete_lwp_cleanup (void *lp_voidp)
617 struct lwp_info *lp = lp_voidp;
619 delete_lwp (lp->ptid);
623 linux_child_follow_fork (struct target_ops *ops, int follow_child)
627 int parent_pid, child_pid;
629 block_child_signals (&prev_mask);
631 has_vforked = (inferior_thread ()->pending_follow.kind
632 == TARGET_WAITKIND_VFORKED);
633 parent_pid = ptid_get_lwp (inferior_ptid);
635 parent_pid = ptid_get_pid (inferior_ptid);
636 child_pid = PIDGET (inferior_thread ()->pending_follow.value.related_pid);
639 linux_enable_event_reporting (pid_to_ptid (child_pid));
642 && !non_stop /* Non-stop always resumes both branches. */
643 && (!target_is_async_p () || sync_execution)
644 && !(follow_child || detach_fork || sched_multi))
646 /* The parent stays blocked inside the vfork syscall until the
647 child execs or exits. If we don't let the child run, then
648 the parent stays blocked. If we're telling the parent to run
649 in the foreground, the user will not be able to ctrl-c to get
650 back the terminal, effectively hanging the debug session. */
651 fprintf_filtered (gdb_stderr, _("\
652 Can not resume the parent process over vfork in the foreground while\n\
653 holding the child stopped. Try \"set detach-on-fork\" or \
654 \"set schedule-multiple\".\n"));
655 /* FIXME output string > 80 columns. */
661 struct lwp_info *child_lp = NULL;
663 /* We're already attached to the parent, by default. */
665 /* Detach new forked process? */
668 struct cleanup *old_chain;
670 /* Before detaching from the child, remove all breakpoints
671 from it. If we forked, then this has already been taken
672 care of by infrun.c. If we vforked however, any
673 breakpoint inserted in the parent is visible in the
674 child, even those added while stopped in a vfork
675 catchpoint. This will remove the breakpoints from the
676 parent also, but they'll be reinserted below. */
679 /* keep breakpoints list in sync. */
680 remove_breakpoints_pid (GET_PID (inferior_ptid));
683 if (info_verbose || debug_linux_nat)
685 target_terminal_ours ();
686 fprintf_filtered (gdb_stdlog,
687 "Detaching after fork from "
688 "child process %d.\n",
692 old_chain = save_inferior_ptid ();
693 inferior_ptid = ptid_build (child_pid, child_pid, 0);
695 child_lp = add_lwp (inferior_ptid);
696 child_lp->stopped = 1;
697 child_lp->last_resume_kind = resume_stop;
698 make_cleanup (delete_lwp_cleanup, child_lp);
700 /* CHILD_LP has new PID, therefore linux_nat_new_thread is not called for it.
701 See i386_inferior_data_get for the Linux kernel specifics.
702 Ensure linux_nat_prepare_to_resume will reset the hardware debug
703 registers. It is done by the linux_nat_new_thread call, which is
704 being skipped in add_lwp above for the first lwp of a pid. */
705 gdb_assert (num_lwps (GET_PID (child_lp->ptid)) == 1);
706 if (linux_nat_new_thread != NULL)
707 linux_nat_new_thread (child_lp);
709 if (linux_nat_prepare_to_resume != NULL)
710 linux_nat_prepare_to_resume (child_lp);
711 ptrace (PTRACE_DETACH, child_pid, 0, 0);
713 do_cleanups (old_chain);
717 struct inferior *parent_inf, *child_inf;
718 struct cleanup *old_chain;
720 /* Add process to GDB's tables. */
721 child_inf = add_inferior (child_pid);
723 parent_inf = current_inferior ();
724 child_inf->attach_flag = parent_inf->attach_flag;
725 copy_terminal_info (child_inf, parent_inf);
727 old_chain = save_inferior_ptid ();
728 save_current_program_space ();
730 inferior_ptid = ptid_build (child_pid, child_pid, 0);
731 add_thread (inferior_ptid);
732 child_lp = add_lwp (inferior_ptid);
733 child_lp->stopped = 1;
734 child_lp->last_resume_kind = resume_stop;
735 child_inf->symfile_flags = SYMFILE_NO_READ;
737 /* If this is a vfork child, then the address-space is
738 shared with the parent. */
741 child_inf->pspace = parent_inf->pspace;
742 child_inf->aspace = parent_inf->aspace;
744 /* The parent will be frozen until the child is done
745 with the shared region. Keep track of the
747 child_inf->vfork_parent = parent_inf;
748 child_inf->pending_detach = 0;
749 parent_inf->vfork_child = child_inf;
750 parent_inf->pending_detach = 0;
754 child_inf->aspace = new_address_space ();
755 child_inf->pspace = add_program_space (child_inf->aspace);
756 child_inf->removable = 1;
757 set_current_program_space (child_inf->pspace);
758 clone_program_space (child_inf->pspace, parent_inf->pspace);
760 /* Let the shared library layer (solib-svr4) learn about
761 this new process, relocate the cloned exec, pull in
762 shared libraries, and install the solib event
763 breakpoint. If a "cloned-VM" event was propagated
764 better throughout the core, this wouldn't be
766 solib_create_inferior_hook (0);
769 /* Let the thread_db layer learn about this new process. */
770 check_for_thread_db ();
772 do_cleanups (old_chain);
777 struct lwp_info *parent_lp;
778 struct inferior *parent_inf;
780 parent_inf = current_inferior ();
782 /* If we detached from the child, then we have to be careful
783 to not insert breakpoints in the parent until the child
784 is done with the shared memory region. However, if we're
785 staying attached to the child, then we can and should
786 insert breakpoints, so that we can debug it. A
787 subsequent child exec or exit is enough to know when does
788 the child stops using the parent's address space. */
789 parent_inf->waiting_for_vfork_done = detach_fork;
790 parent_inf->pspace->breakpoints_not_allowed = detach_fork;
792 parent_lp = find_lwp_pid (pid_to_ptid (parent_pid));
793 gdb_assert (linux_supports_tracefork_flag >= 0);
795 if (linux_supports_tracevforkdone (0))
798 fprintf_unfiltered (gdb_stdlog,
799 "LCFF: waiting for VFORK_DONE on %d\n",
801 parent_lp->stopped = 1;
803 /* We'll handle the VFORK_DONE event like any other
804 event, in target_wait. */
808 /* We can't insert breakpoints until the child has
809 finished with the shared memory region. We need to
810 wait until that happens. Ideal would be to just
812 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
813 - waitpid (parent_pid, &status, __WALL);
814 However, most architectures can't handle a syscall
815 being traced on the way out if it wasn't traced on
818 We might also think to loop, continuing the child
819 until it exits or gets a SIGTRAP. One problem is
820 that the child might call ptrace with PTRACE_TRACEME.
822 There's no simple and reliable way to figure out when
823 the vforked child will be done with its copy of the
824 shared memory. We could step it out of the syscall,
825 two instructions, let it go, and then single-step the
826 parent once. When we have hardware single-step, this
827 would work; with software single-step it could still
828 be made to work but we'd have to be able to insert
829 single-step breakpoints in the child, and we'd have
830 to insert -just- the single-step breakpoint in the
831 parent. Very awkward.
833 In the end, the best we can do is to make sure it
834 runs for a little while. Hopefully it will be out of
835 range of any breakpoints we reinsert. Usually this
836 is only the single-step breakpoint at vfork's return
840 fprintf_unfiltered (gdb_stdlog,
841 "LCFF: no VFORK_DONE "
842 "support, sleeping a bit\n");
846 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
847 and leave it pending. The next linux_nat_resume call
848 will notice a pending event, and bypasses actually
849 resuming the inferior. */
850 parent_lp->status = 0;
851 parent_lp->waitstatus.kind = TARGET_WAITKIND_VFORK_DONE;
852 parent_lp->stopped = 1;
854 /* If we're in async mode, need to tell the event loop
855 there's something here to process. */
856 if (target_can_async_p ())
863 struct inferior *parent_inf, *child_inf;
864 struct lwp_info *child_lp;
865 struct program_space *parent_pspace;
867 if (info_verbose || debug_linux_nat)
869 target_terminal_ours ();
871 fprintf_filtered (gdb_stdlog,
872 _("Attaching after process %d "
873 "vfork to child process %d.\n"),
874 parent_pid, child_pid);
876 fprintf_filtered (gdb_stdlog,
877 _("Attaching after process %d "
878 "fork to child process %d.\n"),
879 parent_pid, child_pid);
882 /* Add the new inferior first, so that the target_detach below
883 doesn't unpush the target. */
885 child_inf = add_inferior (child_pid);
887 parent_inf = current_inferior ();
888 child_inf->attach_flag = parent_inf->attach_flag;
889 copy_terminal_info (child_inf, parent_inf);
891 parent_pspace = parent_inf->pspace;
893 /* If we're vforking, we want to hold on to the parent until the
894 child exits or execs. At child exec or exit time we can
895 remove the old breakpoints from the parent and detach or
896 resume debugging it. Otherwise, detach the parent now; we'll
897 want to reuse it's program/address spaces, but we can't set
898 them to the child before removing breakpoints from the
899 parent, otherwise, the breakpoints module could decide to
900 remove breakpoints from the wrong process (since they'd be
901 assigned to the same address space). */
905 gdb_assert (child_inf->vfork_parent == NULL);
906 gdb_assert (parent_inf->vfork_child == NULL);
907 child_inf->vfork_parent = parent_inf;
908 child_inf->pending_detach = 0;
909 parent_inf->vfork_child = child_inf;
910 parent_inf->pending_detach = detach_fork;
911 parent_inf->waiting_for_vfork_done = 0;
913 else if (detach_fork)
914 target_detach (NULL, 0);
916 /* Note that the detach above makes PARENT_INF dangling. */
918 /* Add the child thread to the appropriate lists, and switch to
919 this new thread, before cloning the program space, and
920 informing the solib layer about this new process. */
922 inferior_ptid = ptid_build (child_pid, child_pid, 0);
923 add_thread (inferior_ptid);
924 child_lp = add_lwp (inferior_ptid);
925 child_lp->stopped = 1;
926 child_lp->last_resume_kind = resume_stop;
928 /* If this is a vfork child, then the address-space is shared
929 with the parent. If we detached from the parent, then we can
930 reuse the parent's program/address spaces. */
931 if (has_vforked || detach_fork)
933 child_inf->pspace = parent_pspace;
934 child_inf->aspace = child_inf->pspace->aspace;
938 child_inf->aspace = new_address_space ();
939 child_inf->pspace = add_program_space (child_inf->aspace);
940 child_inf->removable = 1;
941 child_inf->symfile_flags = SYMFILE_NO_READ;
942 set_current_program_space (child_inf->pspace);
943 clone_program_space (child_inf->pspace, parent_pspace);
945 /* Let the shared library layer (solib-svr4) learn about
946 this new process, relocate the cloned exec, pull in
947 shared libraries, and install the solib event breakpoint.
948 If a "cloned-VM" event was propagated better throughout
949 the core, this wouldn't be required. */
950 solib_create_inferior_hook (0);
953 /* Let the thread_db layer learn about this new process. */
954 check_for_thread_db ();
957 restore_child_signals_mask (&prev_mask);
963 linux_child_insert_fork_catchpoint (int pid)
965 return !linux_supports_tracefork (pid);
969 linux_child_remove_fork_catchpoint (int pid)
975 linux_child_insert_vfork_catchpoint (int pid)
977 return !linux_supports_tracefork (pid);
981 linux_child_remove_vfork_catchpoint (int pid)
987 linux_child_insert_exec_catchpoint (int pid)
989 return !linux_supports_tracefork (pid);
993 linux_child_remove_exec_catchpoint (int pid)
999 linux_child_set_syscall_catchpoint (int pid, int needed, int any_count,
1000 int table_size, int *table)
1002 if (!linux_supports_tracesysgood (pid))
1005 /* On GNU/Linux, we ignore the arguments. It means that we only
1006 enable the syscall catchpoints, but do not disable them.
1008 Also, we do not use the `table' information because we do not
1009 filter system calls here. We let GDB do the logic for us. */
1013 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
1014 are processes sharing the same VM space. A multi-threaded process
1015 is basically a group of such processes. However, such a grouping
1016 is almost entirely a user-space issue; the kernel doesn't enforce
1017 such a grouping at all (this might change in the future). In
1018 general, we'll rely on the threads library (i.e. the GNU/Linux
1019 Threads library) to provide such a grouping.
1021 It is perfectly well possible to write a multi-threaded application
1022 without the assistance of a threads library, by using the clone
1023 system call directly. This module should be able to give some
1024 rudimentary support for debugging such applications if developers
1025 specify the CLONE_PTRACE flag in the clone system call, and are
1026 using the Linux kernel 2.4 or above.
1028 Note that there are some peculiarities in GNU/Linux that affect
1031 - In general one should specify the __WCLONE flag to waitpid in
1032 order to make it report events for any of the cloned processes
1033 (and leave it out for the initial process). However, if a cloned
1034 process has exited the exit status is only reported if the
1035 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
1036 we cannot use it since GDB must work on older systems too.
1038 - When a traced, cloned process exits and is waited for by the
1039 debugger, the kernel reassigns it to the original parent and
1040 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
1041 library doesn't notice this, which leads to the "zombie problem":
1042 When debugged a multi-threaded process that spawns a lot of
1043 threads will run out of processes, even if the threads exit,
1044 because the "zombies" stay around. */
1046 /* List of known LWPs. */
1047 struct lwp_info *lwp_list;
1050 /* Original signal mask. */
1051 static sigset_t normal_mask;
1053 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
1054 _initialize_linux_nat. */
1055 static sigset_t suspend_mask;
1057 /* Signals to block to make that sigsuspend work. */
1058 static sigset_t blocked_mask;
1060 /* SIGCHLD action. */
1061 struct sigaction sigchld_action;
1063 /* Block child signals (SIGCHLD and linux threads signals), and store
1064 the previous mask in PREV_MASK. */
1067 block_child_signals (sigset_t *prev_mask)
1069 /* Make sure SIGCHLD is blocked. */
1070 if (!sigismember (&blocked_mask, SIGCHLD))
1071 sigaddset (&blocked_mask, SIGCHLD);
1073 sigprocmask (SIG_BLOCK, &blocked_mask, prev_mask);
1076 /* Restore child signals mask, previously returned by
1077 block_child_signals. */
1080 restore_child_signals_mask (sigset_t *prev_mask)
1082 sigprocmask (SIG_SETMASK, prev_mask, NULL);
1085 /* Mask of signals to pass directly to the inferior. */
1086 static sigset_t pass_mask;
1088 /* Update signals to pass to the inferior. */
1090 linux_nat_pass_signals (int numsigs, unsigned char *pass_signals)
1094 sigemptyset (&pass_mask);
1096 for (signo = 1; signo < NSIG; signo++)
1098 int target_signo = gdb_signal_from_host (signo);
1099 if (target_signo < numsigs && pass_signals[target_signo])
1100 sigaddset (&pass_mask, signo);
1106 /* Prototypes for local functions. */
1107 static int stop_wait_callback (struct lwp_info *lp, void *data);
1108 static int linux_thread_alive (ptid_t ptid);
1109 static char *linux_child_pid_to_exec_file (int pid);
1112 /* Convert wait status STATUS to a string. Used for printing debug
1116 status_to_str (int status)
1118 static char buf[64];
1120 if (WIFSTOPPED (status))
1122 if (WSTOPSIG (status) == SYSCALL_SIGTRAP)
1123 snprintf (buf, sizeof (buf), "%s (stopped at syscall)",
1124 strsignal (SIGTRAP));
1126 snprintf (buf, sizeof (buf), "%s (stopped)",
1127 strsignal (WSTOPSIG (status)));
1129 else if (WIFSIGNALED (status))
1130 snprintf (buf, sizeof (buf), "%s (terminated)",
1131 strsignal (WTERMSIG (status)));
1133 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
1138 /* Destroy and free LP. */
1141 lwp_free (struct lwp_info *lp)
1143 xfree (lp->arch_private);
1147 /* Remove all LWPs belong to PID from the lwp list. */
1150 purge_lwp_list (int pid)
1152 struct lwp_info *lp, *lpprev, *lpnext;
1156 for (lp = lwp_list; lp; lp = lpnext)
1160 if (ptid_get_pid (lp->ptid) == pid)
1163 lwp_list = lp->next;
1165 lpprev->next = lp->next;
1174 /* Add the LWP specified by PID to the list. Return a pointer to the
1175 structure describing the new LWP. The LWP should already be stopped
1176 (with an exception for the very first LWP). */
1178 static struct lwp_info *
1179 add_lwp (ptid_t ptid)
1181 struct lwp_info *lp;
1183 gdb_assert (is_lwp (ptid));
1185 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
1187 memset (lp, 0, sizeof (struct lwp_info));
1189 lp->last_resume_kind = resume_continue;
1190 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1195 lp->next = lwp_list;
1198 /* Let the arch specific bits know about this new thread. Current
1199 clients of this callback take the opportunity to install
1200 watchpoints in the new thread. Don't do this for the first
1201 thread though. If we're spawning a child ("run"), the thread
1202 executes the shell wrapper first, and we shouldn't touch it until
1203 it execs the program we want to debug. For "attach", it'd be
1204 okay to call the callback, but it's not necessary, because
1205 watchpoints can't yet have been inserted into the inferior. */
1206 if (num_lwps (GET_PID (ptid)) > 1 && linux_nat_new_thread != NULL)
1207 linux_nat_new_thread (lp);
1212 /* Remove the LWP specified by PID from the list. */
1215 delete_lwp (ptid_t ptid)
1217 struct lwp_info *lp, *lpprev;
1221 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
1222 if (ptid_equal (lp->ptid, ptid))
1229 lpprev->next = lp->next;
1231 lwp_list = lp->next;
1236 /* Return a pointer to the structure describing the LWP corresponding
1237 to PID. If no corresponding LWP could be found, return NULL. */
1239 static struct lwp_info *
1240 find_lwp_pid (ptid_t ptid)
1242 struct lwp_info *lp;
1246 lwp = GET_LWP (ptid);
1248 lwp = GET_PID (ptid);
1250 for (lp = lwp_list; lp; lp = lp->next)
1251 if (lwp == GET_LWP (lp->ptid))
1257 /* Call CALLBACK with its second argument set to DATA for every LWP in
1258 the list. If CALLBACK returns 1 for a particular LWP, return a
1259 pointer to the structure describing that LWP immediately.
1260 Otherwise return NULL. */
1263 iterate_over_lwps (ptid_t filter,
1264 int (*callback) (struct lwp_info *, void *),
1267 struct lwp_info *lp, *lpnext;
1269 for (lp = lwp_list; lp; lp = lpnext)
1273 if (ptid_match (lp->ptid, filter))
1275 if ((*callback) (lp, data))
1283 /* Iterate like iterate_over_lwps does except when forking-off a child call
1284 CALLBACK with CALLBACK_DATA specifically only for that new child PID. */
1287 linux_nat_iterate_watchpoint_lwps
1288 (linux_nat_iterate_watchpoint_lwps_ftype callback, void *callback_data)
1290 int inferior_pid = ptid_get_pid (inferior_ptid);
1291 struct inferior *inf = current_inferior ();
1293 if (inf->pid == inferior_pid)
1295 /* Iterate all the threads of the current inferior. Without specifying
1296 INFERIOR_PID it would iterate all threads of all inferiors, which is
1297 inappropriate for watchpoints. */
1299 iterate_over_lwps (pid_to_ptid (inferior_pid), callback, callback_data);
1303 /* Detaching a new child PID temporarily present in INFERIOR_PID. */
1305 struct lwp_info *child_lp;
1306 struct cleanup *old_chain;
1307 pid_t child_pid = GET_PID (inferior_ptid);
1308 ptid_t child_ptid = ptid_build (child_pid, child_pid, 0);
1310 gdb_assert (!is_lwp (inferior_ptid));
1311 gdb_assert (find_lwp_pid (child_ptid) == NULL);
1312 child_lp = add_lwp (child_ptid);
1313 child_lp->stopped = 1;
1314 child_lp->last_resume_kind = resume_stop;
1315 old_chain = make_cleanup (delete_lwp_cleanup, child_lp);
1317 callback (child_lp, callback_data);
1319 do_cleanups (old_chain);
1323 /* Update our internal state when changing from one checkpoint to
1324 another indicated by NEW_PTID. We can only switch single-threaded
1325 applications, so we only create one new LWP, and the previous list
1329 linux_nat_switch_fork (ptid_t new_ptid)
1331 struct lwp_info *lp;
1333 purge_lwp_list (GET_PID (inferior_ptid));
1335 lp = add_lwp (new_ptid);
1338 /* This changes the thread's ptid while preserving the gdb thread
1339 num. Also changes the inferior pid, while preserving the
1341 thread_change_ptid (inferior_ptid, new_ptid);
1343 /* We've just told GDB core that the thread changed target id, but,
1344 in fact, it really is a different thread, with different register
1346 registers_changed ();
1349 /* Handle the exit of a single thread LP. */
1352 exit_lwp (struct lwp_info *lp)
1354 struct thread_info *th = find_thread_ptid (lp->ptid);
1358 if (print_thread_events)
1359 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid));
1361 delete_thread (lp->ptid);
1364 delete_lwp (lp->ptid);
1367 /* Wait for the LWP specified by LP, which we have just attached to.
1368 Returns a wait status for that LWP, to cache. */
1371 linux_nat_post_attach_wait (ptid_t ptid, int first, int *cloned,
1374 pid_t new_pid, pid = GET_LWP (ptid);
1377 if (linux_proc_pid_is_stopped (pid))
1379 if (debug_linux_nat)
1380 fprintf_unfiltered (gdb_stdlog,
1381 "LNPAW: Attaching to a stopped process\n");
1383 /* The process is definitely stopped. It is in a job control
1384 stop, unless the kernel predates the TASK_STOPPED /
1385 TASK_TRACED distinction, in which case it might be in a
1386 ptrace stop. Make sure it is in a ptrace stop; from there we
1387 can kill it, signal it, et cetera.
1389 First make sure there is a pending SIGSTOP. Since we are
1390 already attached, the process can not transition from stopped
1391 to running without a PTRACE_CONT; so we know this signal will
1392 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1393 probably already in the queue (unless this kernel is old
1394 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1395 is not an RT signal, it can only be queued once. */
1396 kill_lwp (pid, SIGSTOP);
1398 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1399 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1400 ptrace (PTRACE_CONT, pid, 0, 0);
1403 /* Make sure the initial process is stopped. The user-level threads
1404 layer might want to poke around in the inferior, and that won't
1405 work if things haven't stabilized yet. */
1406 new_pid = my_waitpid (pid, &status, 0);
1407 if (new_pid == -1 && errno == ECHILD)
1410 warning (_("%s is a cloned process"), target_pid_to_str (ptid));
1412 /* Try again with __WCLONE to check cloned processes. */
1413 new_pid = my_waitpid (pid, &status, __WCLONE);
1417 gdb_assert (pid == new_pid);
1419 if (!WIFSTOPPED (status))
1421 /* The pid we tried to attach has apparently just exited. */
1422 if (debug_linux_nat)
1423 fprintf_unfiltered (gdb_stdlog, "LNPAW: Failed to stop %d: %s",
1424 pid, status_to_str (status));
1428 if (WSTOPSIG (status) != SIGSTOP)
1431 if (debug_linux_nat)
1432 fprintf_unfiltered (gdb_stdlog,
1433 "LNPAW: Received %s after attaching\n",
1434 status_to_str (status));
1440 /* Attach to the LWP specified by PID. Return 0 if successful, -1 if
1441 the new LWP could not be attached, or 1 if we're already auto
1442 attached to this thread, but haven't processed the
1443 PTRACE_EVENT_CLONE event of its parent thread, so we just ignore
1444 its existance, without considering it an error. */
1447 lin_lwp_attach_lwp (ptid_t ptid)
1449 struct lwp_info *lp;
1453 gdb_assert (is_lwp (ptid));
1455 block_child_signals (&prev_mask);
1457 lp = find_lwp_pid (ptid);
1458 lwpid = GET_LWP (ptid);
1460 /* We assume that we're already attached to any LWP that has an id
1461 equal to the overall process id, and to any LWP that is already
1462 in our list of LWPs. If we're not seeing exit events from threads
1463 and we've had PID wraparound since we last tried to stop all threads,
1464 this assumption might be wrong; fortunately, this is very unlikely
1466 if (lwpid != GET_PID (ptid) && lp == NULL)
1468 int status, cloned = 0, signalled = 0;
1470 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0)
1472 if (linux_supports_tracefork_flag)
1474 /* If we haven't stopped all threads when we get here,
1475 we may have seen a thread listed in thread_db's list,
1476 but not processed the PTRACE_EVENT_CLONE yet. If
1477 that's the case, ignore this new thread, and let
1478 normal event handling discover it later. */
1479 if (in_pid_list_p (stopped_pids, lwpid))
1481 /* We've already seen this thread stop, but we
1482 haven't seen the PTRACE_EVENT_CLONE extended
1484 restore_child_signals_mask (&prev_mask);
1492 /* See if we've got a stop for this new child
1493 pending. If so, we're already attached. */
1494 new_pid = my_waitpid (lwpid, &status, WNOHANG);
1495 if (new_pid == -1 && errno == ECHILD)
1496 new_pid = my_waitpid (lwpid, &status, __WCLONE | WNOHANG);
1499 if (WIFSTOPPED (status))
1500 add_to_pid_list (&stopped_pids, lwpid, status);
1502 restore_child_signals_mask (&prev_mask);
1508 /* If we fail to attach to the thread, issue a warning,
1509 but continue. One way this can happen is if thread
1510 creation is interrupted; as of Linux kernel 2.6.19, a
1511 bug may place threads in the thread list and then fail
1513 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid),
1514 safe_strerror (errno));
1515 restore_child_signals_mask (&prev_mask);
1519 if (debug_linux_nat)
1520 fprintf_unfiltered (gdb_stdlog,
1521 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1522 target_pid_to_str (ptid));
1524 status = linux_nat_post_attach_wait (ptid, 0, &cloned, &signalled);
1525 if (!WIFSTOPPED (status))
1527 restore_child_signals_mask (&prev_mask);
1531 lp = add_lwp (ptid);
1533 lp->cloned = cloned;
1534 lp->signalled = signalled;
1535 if (WSTOPSIG (status) != SIGSTOP)
1538 lp->status = status;
1541 target_post_attach (GET_LWP (lp->ptid));
1543 if (debug_linux_nat)
1545 fprintf_unfiltered (gdb_stdlog,
1546 "LLAL: waitpid %s received %s\n",
1547 target_pid_to_str (ptid),
1548 status_to_str (status));
1553 /* We assume that the LWP representing the original process is
1554 already stopped. Mark it as stopped in the data structure
1555 that the GNU/linux ptrace layer uses to keep track of
1556 threads. Note that this won't have already been done since
1557 the main thread will have, we assume, been stopped by an
1558 attach from a different layer. */
1560 lp = add_lwp (ptid);
1564 lp->last_resume_kind = resume_stop;
1565 restore_child_signals_mask (&prev_mask);
1570 linux_nat_create_inferior (struct target_ops *ops,
1571 char *exec_file, char *allargs, char **env,
1574 #ifdef HAVE_PERSONALITY
1575 int personality_orig = 0, personality_set = 0;
1576 #endif /* HAVE_PERSONALITY */
1578 /* The fork_child mechanism is synchronous and calls target_wait, so
1579 we have to mask the async mode. */
1581 #ifdef HAVE_PERSONALITY
1582 if (disable_randomization)
1585 personality_orig = personality (0xffffffff);
1586 if (errno == 0 && !(personality_orig & ADDR_NO_RANDOMIZE))
1588 personality_set = 1;
1589 personality (personality_orig | ADDR_NO_RANDOMIZE);
1591 if (errno != 0 || (personality_set
1592 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE)))
1593 warning (_("Error disabling address space randomization: %s"),
1594 safe_strerror (errno));
1596 #endif /* HAVE_PERSONALITY */
1598 /* Make sure we report all signals during startup. */
1599 linux_nat_pass_signals (0, NULL);
1601 linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
1603 #ifdef HAVE_PERSONALITY
1604 if (personality_set)
1607 personality (personality_orig);
1609 warning (_("Error restoring address space randomization: %s"),
1610 safe_strerror (errno));
1612 #endif /* HAVE_PERSONALITY */
1616 linux_nat_attach (struct target_ops *ops, char *args, int from_tty)
1618 struct lwp_info *lp;
1621 volatile struct gdb_exception ex;
1623 /* Make sure we report all signals during attach. */
1624 linux_nat_pass_signals (0, NULL);
1626 TRY_CATCH (ex, RETURN_MASK_ERROR)
1628 linux_ops->to_attach (ops, args, from_tty);
1632 pid_t pid = parse_pid_to_attach (args);
1633 struct buffer buffer;
1634 char *message, *buffer_s;
1636 message = xstrdup (ex.message);
1637 make_cleanup (xfree, message);
1639 buffer_init (&buffer);
1640 linux_ptrace_attach_warnings (pid, &buffer);
1642 buffer_grow_str0 (&buffer, "");
1643 buffer_s = buffer_finish (&buffer);
1644 make_cleanup (xfree, buffer_s);
1646 throw_error (ex.error, "%s%s", buffer_s, message);
1649 /* The ptrace base target adds the main thread with (pid,0,0)
1650 format. Decorate it with lwp info. */
1651 ptid = BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid));
1652 thread_change_ptid (inferior_ptid, ptid);
1654 /* Add the initial process as the first LWP to the list. */
1655 lp = add_lwp (ptid);
1657 status = linux_nat_post_attach_wait (lp->ptid, 1, &lp->cloned,
1659 if (!WIFSTOPPED (status))
1661 if (WIFEXITED (status))
1663 int exit_code = WEXITSTATUS (status);
1665 target_terminal_ours ();
1666 target_mourn_inferior ();
1668 error (_("Unable to attach: program exited normally."));
1670 error (_("Unable to attach: program exited with code %d."),
1673 else if (WIFSIGNALED (status))
1675 enum gdb_signal signo;
1677 target_terminal_ours ();
1678 target_mourn_inferior ();
1680 signo = gdb_signal_from_host (WTERMSIG (status));
1681 error (_("Unable to attach: program terminated with signal "
1683 gdb_signal_to_name (signo),
1684 gdb_signal_to_string (signo));
1687 internal_error (__FILE__, __LINE__,
1688 _("unexpected status %d for PID %ld"),
1689 status, (long) GET_LWP (ptid));
1694 /* Save the wait status to report later. */
1696 if (debug_linux_nat)
1697 fprintf_unfiltered (gdb_stdlog,
1698 "LNA: waitpid %ld, saving status %s\n",
1699 (long) GET_PID (lp->ptid), status_to_str (status));
1701 lp->status = status;
1703 if (target_can_async_p ())
1704 target_async (inferior_event_handler, 0);
1707 /* Get pending status of LP. */
1709 get_pending_status (struct lwp_info *lp, int *status)
1711 enum gdb_signal signo = GDB_SIGNAL_0;
1713 /* If we paused threads momentarily, we may have stored pending
1714 events in lp->status or lp->waitstatus (see stop_wait_callback),
1715 and GDB core hasn't seen any signal for those threads.
1716 Otherwise, the last signal reported to the core is found in the
1717 thread object's stop_signal.
1719 There's a corner case that isn't handled here at present. Only
1720 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1721 stop_signal make sense as a real signal to pass to the inferior.
1722 Some catchpoint related events, like
1723 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1724 to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1725 those traps are debug API (ptrace in our case) related and
1726 induced; the inferior wouldn't see them if it wasn't being
1727 traced. Hence, we should never pass them to the inferior, even
1728 when set to pass state. Since this corner case isn't handled by
1729 infrun.c when proceeding with a signal, for consistency, neither
1730 do we handle it here (or elsewhere in the file we check for
1731 signal pass state). Normally SIGTRAP isn't set to pass state, so
1732 this is really a corner case. */
1734 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
1735 signo = GDB_SIGNAL_0; /* a pending ptrace event, not a real signal. */
1736 else if (lp->status)
1737 signo = gdb_signal_from_host (WSTOPSIG (lp->status));
1738 else if (non_stop && !is_executing (lp->ptid))
1740 struct thread_info *tp = find_thread_ptid (lp->ptid);
1742 signo = tp->suspend.stop_signal;
1746 struct target_waitstatus last;
1749 get_last_target_status (&last_ptid, &last);
1751 if (GET_LWP (lp->ptid) == GET_LWP (last_ptid))
1753 struct thread_info *tp = find_thread_ptid (lp->ptid);
1755 signo = tp->suspend.stop_signal;
1761 if (signo == GDB_SIGNAL_0)
1763 if (debug_linux_nat)
1764 fprintf_unfiltered (gdb_stdlog,
1765 "GPT: lwp %s has no pending signal\n",
1766 target_pid_to_str (lp->ptid));
1768 else if (!signal_pass_state (signo))
1770 if (debug_linux_nat)
1771 fprintf_unfiltered (gdb_stdlog,
1772 "GPT: lwp %s had signal %s, "
1773 "but it is in no pass state\n",
1774 target_pid_to_str (lp->ptid),
1775 gdb_signal_to_string (signo));
1779 *status = W_STOPCODE (gdb_signal_to_host (signo));
1781 if (debug_linux_nat)
1782 fprintf_unfiltered (gdb_stdlog,
1783 "GPT: lwp %s has pending signal %s\n",
1784 target_pid_to_str (lp->ptid),
1785 gdb_signal_to_string (signo));
1792 detach_callback (struct lwp_info *lp, void *data)
1794 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1796 if (debug_linux_nat && lp->status)
1797 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
1798 strsignal (WSTOPSIG (lp->status)),
1799 target_pid_to_str (lp->ptid));
1801 /* If there is a pending SIGSTOP, get rid of it. */
1804 if (debug_linux_nat)
1805 fprintf_unfiltered (gdb_stdlog,
1806 "DC: Sending SIGCONT to %s\n",
1807 target_pid_to_str (lp->ptid));
1809 kill_lwp (GET_LWP (lp->ptid), SIGCONT);
1813 /* We don't actually detach from the LWP that has an id equal to the
1814 overall process id just yet. */
1815 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
1819 /* Pass on any pending signal for this LWP. */
1820 get_pending_status (lp, &status);
1822 if (linux_nat_prepare_to_resume != NULL)
1823 linux_nat_prepare_to_resume (lp);
1825 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
1826 WSTOPSIG (status)) < 0)
1827 error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
1828 safe_strerror (errno));
1830 if (debug_linux_nat)
1831 fprintf_unfiltered (gdb_stdlog,
1832 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1833 target_pid_to_str (lp->ptid),
1834 strsignal (WSTOPSIG (status)));
1836 delete_lwp (lp->ptid);
1843 linux_nat_detach (struct target_ops *ops, char *args, int from_tty)
1847 struct lwp_info *main_lwp;
1849 pid = GET_PID (inferior_ptid);
1851 /* Don't unregister from the event loop, as there may be other
1852 inferiors running. */
1854 /* Stop all threads before detaching. ptrace requires that the
1855 thread is stopped to sucessfully detach. */
1856 iterate_over_lwps (pid_to_ptid (pid), stop_callback, NULL);
1857 /* ... and wait until all of them have reported back that
1858 they're no longer running. */
1859 iterate_over_lwps (pid_to_ptid (pid), stop_wait_callback, NULL);
1861 iterate_over_lwps (pid_to_ptid (pid), detach_callback, NULL);
1863 /* Only the initial process should be left right now. */
1864 gdb_assert (num_lwps (GET_PID (inferior_ptid)) == 1);
1866 main_lwp = find_lwp_pid (pid_to_ptid (pid));
1868 /* Pass on any pending signal for the last LWP. */
1869 if ((args == NULL || *args == '\0')
1870 && get_pending_status (main_lwp, &status) != -1
1871 && WIFSTOPPED (status))
1873 /* Put the signal number in ARGS so that inf_ptrace_detach will
1874 pass it along with PTRACE_DETACH. */
1876 sprintf (args, "%d", (int) WSTOPSIG (status));
1877 if (debug_linux_nat)
1878 fprintf_unfiltered (gdb_stdlog,
1879 "LND: Sending signal %s to %s\n",
1881 target_pid_to_str (main_lwp->ptid));
1884 if (linux_nat_prepare_to_resume != NULL)
1885 linux_nat_prepare_to_resume (main_lwp);
1886 delete_lwp (main_lwp->ptid);
1888 if (forks_exist_p ())
1890 /* Multi-fork case. The current inferior_ptid is being detached
1891 from, but there are other viable forks to debug. Detach from
1892 the current fork, and context-switch to the first
1894 linux_fork_detach (args, from_tty);
1897 linux_ops->to_detach (ops, args, from_tty);
1903 resume_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
1907 struct inferior *inf = find_inferior_pid (GET_PID (lp->ptid));
1909 if (inf->vfork_child != NULL)
1911 if (debug_linux_nat)
1912 fprintf_unfiltered (gdb_stdlog,
1913 "RC: Not resuming %s (vfork parent)\n",
1914 target_pid_to_str (lp->ptid));
1916 else if (lp->status == 0
1917 && lp->waitstatus.kind == TARGET_WAITKIND_IGNORE)
1919 if (debug_linux_nat)
1920 fprintf_unfiltered (gdb_stdlog,
1921 "RC: Resuming sibling %s, %s, %s\n",
1922 target_pid_to_str (lp->ptid),
1923 (signo != GDB_SIGNAL_0
1924 ? strsignal (gdb_signal_to_host (signo))
1926 step ? "step" : "resume");
1928 if (linux_nat_prepare_to_resume != NULL)
1929 linux_nat_prepare_to_resume (lp);
1930 linux_ops->to_resume (linux_ops,
1931 pid_to_ptid (GET_LWP (lp->ptid)),
1935 lp->stopped_by_watchpoint = 0;
1939 if (debug_linux_nat)
1940 fprintf_unfiltered (gdb_stdlog,
1941 "RC: Not resuming sibling %s (has pending)\n",
1942 target_pid_to_str (lp->ptid));
1947 if (debug_linux_nat)
1948 fprintf_unfiltered (gdb_stdlog,
1949 "RC: Not resuming sibling %s (not stopped)\n",
1950 target_pid_to_str (lp->ptid));
1954 /* Resume LWP, with the last stop signal, if it is in pass state. */
1957 linux_nat_resume_callback (struct lwp_info *lp, void *data)
1959 enum gdb_signal signo = GDB_SIGNAL_0;
1963 struct thread_info *thread;
1965 thread = find_thread_ptid (lp->ptid);
1968 if (signal_pass_state (thread->suspend.stop_signal))
1969 signo = thread->suspend.stop_signal;
1970 thread->suspend.stop_signal = GDB_SIGNAL_0;
1974 resume_lwp (lp, 0, signo);
1979 resume_clear_callback (struct lwp_info *lp, void *data)
1982 lp->last_resume_kind = resume_stop;
1987 resume_set_callback (struct lwp_info *lp, void *data)
1990 lp->last_resume_kind = resume_continue;
1995 linux_nat_resume (struct target_ops *ops,
1996 ptid_t ptid, int step, enum gdb_signal signo)
1999 struct lwp_info *lp;
2002 if (debug_linux_nat)
2003 fprintf_unfiltered (gdb_stdlog,
2004 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
2005 step ? "step" : "resume",
2006 target_pid_to_str (ptid),
2007 (signo != GDB_SIGNAL_0
2008 ? strsignal (gdb_signal_to_host (signo)) : "0"),
2009 target_pid_to_str (inferior_ptid));
2011 block_child_signals (&prev_mask);
2013 /* A specific PTID means `step only this process id'. */
2014 resume_many = (ptid_equal (minus_one_ptid, ptid)
2015 || ptid_is_pid (ptid));
2017 /* Mark the lwps we're resuming as resumed. */
2018 iterate_over_lwps (ptid, resume_set_callback, NULL);
2020 /* See if it's the current inferior that should be handled
2023 lp = find_lwp_pid (inferior_ptid);
2025 lp = find_lwp_pid (ptid);
2026 gdb_assert (lp != NULL);
2028 /* Remember if we're stepping. */
2030 lp->last_resume_kind = step ? resume_step : resume_continue;
2032 /* If we have a pending wait status for this thread, there is no
2033 point in resuming the process. But first make sure that
2034 linux_nat_wait won't preemptively handle the event - we
2035 should never take this short-circuit if we are going to
2036 leave LP running, since we have skipped resuming all the
2037 other threads. This bit of code needs to be synchronized
2038 with linux_nat_wait. */
2040 if (lp->status && WIFSTOPPED (lp->status))
2043 && WSTOPSIG (lp->status)
2044 && sigismember (&pass_mask, WSTOPSIG (lp->status)))
2046 if (debug_linux_nat)
2047 fprintf_unfiltered (gdb_stdlog,
2048 "LLR: Not short circuiting for ignored "
2049 "status 0x%x\n", lp->status);
2051 /* FIXME: What should we do if we are supposed to continue
2052 this thread with a signal? */
2053 gdb_assert (signo == GDB_SIGNAL_0);
2054 signo = gdb_signal_from_host (WSTOPSIG (lp->status));
2059 if (lp->status || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2061 /* FIXME: What should we do if we are supposed to continue
2062 this thread with a signal? */
2063 gdb_assert (signo == GDB_SIGNAL_0);
2065 if (debug_linux_nat)
2066 fprintf_unfiltered (gdb_stdlog,
2067 "LLR: Short circuiting for status 0x%x\n",
2070 restore_child_signals_mask (&prev_mask);
2071 if (target_can_async_p ())
2073 target_async (inferior_event_handler, 0);
2074 /* Tell the event loop we have something to process. */
2080 /* Mark LWP as not stopped to prevent it from being continued by
2081 linux_nat_resume_callback. */
2085 iterate_over_lwps (ptid, linux_nat_resume_callback, NULL);
2087 /* Convert to something the lower layer understands. */
2088 ptid = pid_to_ptid (GET_LWP (lp->ptid));
2090 if (linux_nat_prepare_to_resume != NULL)
2091 linux_nat_prepare_to_resume (lp);
2092 linux_ops->to_resume (linux_ops, ptid, step, signo);
2093 lp->stopped_by_watchpoint = 0;
2095 if (debug_linux_nat)
2096 fprintf_unfiltered (gdb_stdlog,
2097 "LLR: %s %s, %s (resume event thread)\n",
2098 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2099 target_pid_to_str (ptid),
2100 (signo != GDB_SIGNAL_0
2101 ? strsignal (gdb_signal_to_host (signo)) : "0"));
2103 restore_child_signals_mask (&prev_mask);
2104 if (target_can_async_p ())
2105 target_async (inferior_event_handler, 0);
2108 /* Send a signal to an LWP. */
2111 kill_lwp (int lwpid, int signo)
2113 /* Use tkill, if possible, in case we are using nptl threads. If tkill
2114 fails, then we are not using nptl threads and we should be using kill. */
2116 #ifdef HAVE_TKILL_SYSCALL
2118 static int tkill_failed;
2125 ret = syscall (__NR_tkill, lwpid, signo);
2126 if (errno != ENOSYS)
2133 return kill (lwpid, signo);
2136 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
2137 event, check if the core is interested in it: if not, ignore the
2138 event, and keep waiting; otherwise, we need to toggle the LWP's
2139 syscall entry/exit status, since the ptrace event itself doesn't
2140 indicate it, and report the trap to higher layers. */
2143 linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
2145 struct target_waitstatus *ourstatus = &lp->waitstatus;
2146 struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
2147 int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, lp->ptid);
2151 /* If we're stopping threads, there's a SIGSTOP pending, which
2152 makes it so that the LWP reports an immediate syscall return,
2153 followed by the SIGSTOP. Skip seeing that "return" using
2154 PTRACE_CONT directly, and let stop_wait_callback collect the
2155 SIGSTOP. Later when the thread is resumed, a new syscall
2156 entry event. If we didn't do this (and returned 0), we'd
2157 leave a syscall entry pending, and our caller, by using
2158 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
2159 itself. Later, when the user re-resumes this LWP, we'd see
2160 another syscall entry event and we'd mistake it for a return.
2162 If stop_wait_callback didn't force the SIGSTOP out of the LWP
2163 (leaving immediately with LWP->signalled set, without issuing
2164 a PTRACE_CONT), it would still be problematic to leave this
2165 syscall enter pending, as later when the thread is resumed,
2166 it would then see the same syscall exit mentioned above,
2167 followed by the delayed SIGSTOP, while the syscall didn't
2168 actually get to execute. It seems it would be even more
2169 confusing to the user. */
2171 if (debug_linux_nat)
2172 fprintf_unfiltered (gdb_stdlog,
2173 "LHST: ignoring syscall %d "
2174 "for LWP %ld (stopping threads), "
2175 "resuming with PTRACE_CONT for SIGSTOP\n",
2177 GET_LWP (lp->ptid));
2179 lp->syscall_state = TARGET_WAITKIND_IGNORE;
2180 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2184 if (catch_syscall_enabled ())
2186 /* Always update the entry/return state, even if this particular
2187 syscall isn't interesting to the core now. In async mode,
2188 the user could install a new catchpoint for this syscall
2189 between syscall enter/return, and we'll need to know to
2190 report a syscall return if that happens. */
2191 lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
2192 ? TARGET_WAITKIND_SYSCALL_RETURN
2193 : TARGET_WAITKIND_SYSCALL_ENTRY);
2195 if (catching_syscall_number (syscall_number))
2197 /* Alright, an event to report. */
2198 ourstatus->kind = lp->syscall_state;
2199 ourstatus->value.syscall_number = syscall_number;
2201 if (debug_linux_nat)
2202 fprintf_unfiltered (gdb_stdlog,
2203 "LHST: stopping for %s of syscall %d"
2206 == TARGET_WAITKIND_SYSCALL_ENTRY
2207 ? "entry" : "return",
2209 GET_LWP (lp->ptid));
2213 if (debug_linux_nat)
2214 fprintf_unfiltered (gdb_stdlog,
2215 "LHST: ignoring %s of syscall %d "
2217 lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
2218 ? "entry" : "return",
2220 GET_LWP (lp->ptid));
2224 /* If we had been syscall tracing, and hence used PT_SYSCALL
2225 before on this LWP, it could happen that the user removes all
2226 syscall catchpoints before we get to process this event.
2227 There are two noteworthy issues here:
2229 - When stopped at a syscall entry event, resuming with
2230 PT_STEP still resumes executing the syscall and reports a
2233 - Only PT_SYSCALL catches syscall enters. If we last
2234 single-stepped this thread, then this event can't be a
2235 syscall enter. If we last single-stepped this thread, this
2236 has to be a syscall exit.
2238 The points above mean that the next resume, be it PT_STEP or
2239 PT_CONTINUE, can not trigger a syscall trace event. */
2240 if (debug_linux_nat)
2241 fprintf_unfiltered (gdb_stdlog,
2242 "LHST: caught syscall event "
2243 "with no syscall catchpoints."
2244 " %d for LWP %ld, ignoring\n",
2246 GET_LWP (lp->ptid));
2247 lp->syscall_state = TARGET_WAITKIND_IGNORE;
2250 /* The core isn't interested in this event. For efficiency, avoid
2251 stopping all threads only to have the core resume them all again.
2252 Since we're not stopping threads, if we're still syscall tracing
2253 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
2254 subsequent syscall. Simply resume using the inf-ptrace layer,
2255 which knows when to use PT_SYSCALL or PT_CONTINUE. */
2257 /* Note that gdbarch_get_syscall_number may access registers, hence
2259 registers_changed ();
2260 if (linux_nat_prepare_to_resume != NULL)
2261 linux_nat_prepare_to_resume (lp);
2262 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
2263 lp->step, GDB_SIGNAL_0);
2267 /* Handle a GNU/Linux extended wait response. If we see a clone
2268 event, we need to add the new LWP to our list (and not report the
2269 trap to higher layers). This function returns non-zero if the
2270 event should be ignored and we should wait again. If STOPPING is
2271 true, the new LWP remains stopped, otherwise it is continued. */
2274 linux_handle_extended_wait (struct lwp_info *lp, int status,
2277 int pid = GET_LWP (lp->ptid);
2278 struct target_waitstatus *ourstatus = &lp->waitstatus;
2279 int event = status >> 16;
2281 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
2282 || event == PTRACE_EVENT_CLONE)
2284 unsigned long new_pid;
2287 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
2289 /* If we haven't already seen the new PID stop, wait for it now. */
2290 if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
2292 /* The new child has a pending SIGSTOP. We can't affect it until it
2293 hits the SIGSTOP, but we're already attached. */
2294 ret = my_waitpid (new_pid, &status,
2295 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
2297 perror_with_name (_("waiting for new child"));
2298 else if (ret != new_pid)
2299 internal_error (__FILE__, __LINE__,
2300 _("wait returned unexpected PID %d"), ret);
2301 else if (!WIFSTOPPED (status))
2302 internal_error (__FILE__, __LINE__,
2303 _("wait returned unexpected status 0x%x"), status);
2306 ourstatus->value.related_pid = ptid_build (new_pid, new_pid, 0);
2308 if (event == PTRACE_EVENT_FORK
2309 && linux_fork_checkpointing_p (GET_PID (lp->ptid)))
2311 /* Handle checkpointing by linux-fork.c here as a special
2312 case. We don't want the follow-fork-mode or 'catch fork'
2313 to interfere with this. */
2315 /* This won't actually modify the breakpoint list, but will
2316 physically remove the breakpoints from the child. */
2317 detach_breakpoints (new_pid);
2319 /* Retain child fork in ptrace (stopped) state. */
2320 if (!find_fork_pid (new_pid))
2323 /* Report as spurious, so that infrun doesn't want to follow
2324 this fork. We're actually doing an infcall in
2326 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
2327 linux_enable_event_reporting (pid_to_ptid (new_pid));
2329 /* Report the stop to the core. */
2333 if (event == PTRACE_EVENT_FORK)
2334 ourstatus->kind = TARGET_WAITKIND_FORKED;
2335 else if (event == PTRACE_EVENT_VFORK)
2336 ourstatus->kind = TARGET_WAITKIND_VFORKED;
2339 struct lwp_info *new_lp;
2341 ourstatus->kind = TARGET_WAITKIND_IGNORE;
2343 if (debug_linux_nat)
2344 fprintf_unfiltered (gdb_stdlog,
2345 "LHEW: Got clone event "
2346 "from LWP %d, new child is LWP %ld\n",
2349 new_lp = add_lwp (BUILD_LWP (new_pid, GET_PID (lp->ptid)));
2351 new_lp->stopped = 1;
2353 if (WSTOPSIG (status) != SIGSTOP)
2355 /* This can happen if someone starts sending signals to
2356 the new thread before it gets a chance to run, which
2357 have a lower number than SIGSTOP (e.g. SIGUSR1).
2358 This is an unlikely case, and harder to handle for
2359 fork / vfork than for clone, so we do not try - but
2360 we handle it for clone events here. We'll send
2361 the other signal on to the thread below. */
2363 new_lp->signalled = 1;
2367 struct thread_info *tp;
2369 /* When we stop for an event in some other thread, and
2370 pull the thread list just as this thread has cloned,
2371 we'll have seen the new thread in the thread_db list
2372 before handling the CLONE event (glibc's
2373 pthread_create adds the new thread to the thread list
2374 before clone'ing, and has the kernel fill in the
2375 thread's tid on the clone call with
2376 CLONE_PARENT_SETTID). If that happened, and the core
2377 had requested the new thread to stop, we'll have
2378 killed it with SIGSTOP. But since SIGSTOP is not an
2379 RT signal, it can only be queued once. We need to be
2380 careful to not resume the LWP if we wanted it to
2381 stop. In that case, we'll leave the SIGSTOP pending.
2382 It will later be reported as GDB_SIGNAL_0. */
2383 tp = find_thread_ptid (new_lp->ptid);
2384 if (tp != NULL && tp->stop_requested)
2385 new_lp->last_resume_kind = resume_stop;
2392 /* Add the new thread to GDB's lists as soon as possible
2395 1) the frontend doesn't have to wait for a stop to
2398 2) we tag it with the correct running state. */
2400 /* If the thread_db layer is active, let it know about
2401 this new thread, and add it to GDB's list. */
2402 if (!thread_db_attach_lwp (new_lp->ptid))
2404 /* We're not using thread_db. Add it to GDB's
2406 target_post_attach (GET_LWP (new_lp->ptid));
2407 add_thread (new_lp->ptid);
2412 set_running (new_lp->ptid, 1);
2413 set_executing (new_lp->ptid, 1);
2414 /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
2416 new_lp->last_resume_kind = resume_continue;
2422 /* We created NEW_LP so it cannot yet contain STATUS. */
2423 gdb_assert (new_lp->status == 0);
2425 /* Save the wait status to report later. */
2426 if (debug_linux_nat)
2427 fprintf_unfiltered (gdb_stdlog,
2428 "LHEW: waitpid of new LWP %ld, "
2429 "saving status %s\n",
2430 (long) GET_LWP (new_lp->ptid),
2431 status_to_str (status));
2432 new_lp->status = status;
2435 /* Note the need to use the low target ops to resume, to
2436 handle resuming with PT_SYSCALL if we have syscall
2440 new_lp->resumed = 1;
2444 gdb_assert (new_lp->last_resume_kind == resume_continue);
2445 if (debug_linux_nat)
2446 fprintf_unfiltered (gdb_stdlog,
2447 "LHEW: resuming new LWP %ld\n",
2448 GET_LWP (new_lp->ptid));
2449 if (linux_nat_prepare_to_resume != NULL)
2450 linux_nat_prepare_to_resume (new_lp);
2451 linux_ops->to_resume (linux_ops, pid_to_ptid (new_pid),
2453 new_lp->stopped = 0;
2457 if (debug_linux_nat)
2458 fprintf_unfiltered (gdb_stdlog,
2459 "LHEW: resuming parent LWP %d\n", pid);
2460 if (linux_nat_prepare_to_resume != NULL)
2461 linux_nat_prepare_to_resume (lp);
2462 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
2471 if (event == PTRACE_EVENT_EXEC)
2473 if (debug_linux_nat)
2474 fprintf_unfiltered (gdb_stdlog,
2475 "LHEW: Got exec event from LWP %ld\n",
2476 GET_LWP (lp->ptid));
2478 ourstatus->kind = TARGET_WAITKIND_EXECD;
2479 ourstatus->value.execd_pathname
2480 = xstrdup (linux_child_pid_to_exec_file (pid));
2485 if (event == PTRACE_EVENT_VFORK_DONE)
2487 if (current_inferior ()->waiting_for_vfork_done)
2489 if (debug_linux_nat)
2490 fprintf_unfiltered (gdb_stdlog,
2491 "LHEW: Got expected PTRACE_EVENT_"
2492 "VFORK_DONE from LWP %ld: stopping\n",
2493 GET_LWP (lp->ptid));
2495 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
2499 if (debug_linux_nat)
2500 fprintf_unfiltered (gdb_stdlog,
2501 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2502 "from LWP %ld: resuming\n",
2503 GET_LWP (lp->ptid));
2504 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2508 internal_error (__FILE__, __LINE__,
2509 _("unknown ptrace event %d"), event);
2512 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2516 wait_lwp (struct lwp_info *lp)
2520 int thread_dead = 0;
2523 gdb_assert (!lp->stopped);
2524 gdb_assert (lp->status == 0);
2526 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2527 block_child_signals (&prev_mask);
2531 /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2532 was right and we should just call sigsuspend. */
2534 pid = my_waitpid (GET_LWP (lp->ptid), &status, WNOHANG);
2535 if (pid == -1 && errno == ECHILD)
2536 pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE | WNOHANG);
2537 if (pid == -1 && errno == ECHILD)
2539 /* The thread has previously exited. We need to delete it
2540 now because, for some vendor 2.4 kernels with NPTL
2541 support backported, there won't be an exit event unless
2542 it is the main thread. 2.6 kernels will report an exit
2543 event for each thread that exits, as expected. */
2545 if (debug_linux_nat)
2546 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
2547 target_pid_to_str (lp->ptid));
2552 /* Bugs 10970, 12702.
2553 Thread group leader may have exited in which case we'll lock up in
2554 waitpid if there are other threads, even if they are all zombies too.
2555 Basically, we're not supposed to use waitpid this way.
2556 __WCLONE is not applicable for the leader so we can't use that.
2557 LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2558 process; it gets ESRCH both for the zombie and for running processes.
2560 As a workaround, check if we're waiting for the thread group leader and
2561 if it's a zombie, and avoid calling waitpid if it is.
2563 This is racy, what if the tgl becomes a zombie right after we check?
2564 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2565 waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
2567 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid)
2568 && linux_proc_pid_is_zombie (GET_LWP (lp->ptid)))
2571 if (debug_linux_nat)
2572 fprintf_unfiltered (gdb_stdlog,
2573 "WL: Thread group leader %s vanished.\n",
2574 target_pid_to_str (lp->ptid));
2578 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2579 get invoked despite our caller had them intentionally blocked by
2580 block_child_signals. This is sensitive only to the loop of
2581 linux_nat_wait_1 and there if we get called my_waitpid gets called
2582 again before it gets to sigsuspend so we can safely let the handlers
2583 get executed here. */
2585 sigsuspend (&suspend_mask);
2588 restore_child_signals_mask (&prev_mask);
2592 gdb_assert (pid == GET_LWP (lp->ptid));
2594 if (debug_linux_nat)
2596 fprintf_unfiltered (gdb_stdlog,
2597 "WL: waitpid %s received %s\n",
2598 target_pid_to_str (lp->ptid),
2599 status_to_str (status));
2602 /* Check if the thread has exited. */
2603 if (WIFEXITED (status) || WIFSIGNALED (status))
2606 if (debug_linux_nat)
2607 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
2608 target_pid_to_str (lp->ptid));
2618 gdb_assert (WIFSTOPPED (status));
2620 /* Handle GNU/Linux's syscall SIGTRAPs. */
2621 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
2623 /* No longer need the sysgood bit. The ptrace event ends up
2624 recorded in lp->waitstatus if we care for it. We can carry
2625 on handling the event like a regular SIGTRAP from here
2627 status = W_STOPCODE (SIGTRAP);
2628 if (linux_handle_syscall_trap (lp, 1))
2629 return wait_lwp (lp);
2632 /* Handle GNU/Linux's extended waitstatus for trace events. */
2633 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
2635 if (debug_linux_nat)
2636 fprintf_unfiltered (gdb_stdlog,
2637 "WL: Handling extended status 0x%06x\n",
2639 if (linux_handle_extended_wait (lp, status, 1))
2640 return wait_lwp (lp);
2646 /* Send a SIGSTOP to LP. */
2649 stop_callback (struct lwp_info *lp, void *data)
2651 if (!lp->stopped && !lp->signalled)
2655 if (debug_linux_nat)
2657 fprintf_unfiltered (gdb_stdlog,
2658 "SC: kill %s **<SIGSTOP>**\n",
2659 target_pid_to_str (lp->ptid));
2662 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
2663 if (debug_linux_nat)
2665 fprintf_unfiltered (gdb_stdlog,
2666 "SC: lwp kill %d %s\n",
2668 errno ? safe_strerror (errno) : "ERRNO-OK");
2672 gdb_assert (lp->status == 0);
2678 /* Request a stop on LWP. */
2681 linux_stop_lwp (struct lwp_info *lwp)
2683 stop_callback (lwp, NULL);
2686 /* Return non-zero if LWP PID has a pending SIGINT. */
2689 linux_nat_has_pending_sigint (int pid)
2691 sigset_t pending, blocked, ignored;
2693 linux_proc_pending_signals (pid, &pending, &blocked, &ignored);
2695 if (sigismember (&pending, SIGINT)
2696 && !sigismember (&ignored, SIGINT))
2702 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2705 set_ignore_sigint (struct lwp_info *lp, void *data)
2707 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2708 flag to consume the next one. */
2709 if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status)
2710 && WSTOPSIG (lp->status) == SIGINT)
2713 lp->ignore_sigint = 1;
2718 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2719 This function is called after we know the LWP has stopped; if the LWP
2720 stopped before the expected SIGINT was delivered, then it will never have
2721 arrived. Also, if the signal was delivered to a shared queue and consumed
2722 by a different thread, it will never be delivered to this LWP. */
2725 maybe_clear_ignore_sigint (struct lwp_info *lp)
2727 if (!lp->ignore_sigint)
2730 if (!linux_nat_has_pending_sigint (GET_LWP (lp->ptid)))
2732 if (debug_linux_nat)
2733 fprintf_unfiltered (gdb_stdlog,
2734 "MCIS: Clearing bogus flag for %s\n",
2735 target_pid_to_str (lp->ptid));
2736 lp->ignore_sigint = 0;
2740 /* Fetch the possible triggered data watchpoint info and store it in
2743 On some archs, like x86, that use debug registers to set
2744 watchpoints, it's possible that the way to know which watched
2745 address trapped, is to check the register that is used to select
2746 which address to watch. Problem is, between setting the watchpoint
2747 and reading back which data address trapped, the user may change
2748 the set of watchpoints, and, as a consequence, GDB changes the
2749 debug registers in the inferior. To avoid reading back a stale
2750 stopped-data-address when that happens, we cache in LP the fact
2751 that a watchpoint trapped, and the corresponding data address, as
2752 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2753 registers meanwhile, we have the cached data we can rely on. */
2756 save_sigtrap (struct lwp_info *lp)
2758 struct cleanup *old_chain;
2760 if (linux_ops->to_stopped_by_watchpoint == NULL)
2762 lp->stopped_by_watchpoint = 0;
2766 old_chain = save_inferior_ptid ();
2767 inferior_ptid = lp->ptid;
2769 lp->stopped_by_watchpoint = linux_ops->to_stopped_by_watchpoint ();
2771 if (lp->stopped_by_watchpoint)
2773 if (linux_ops->to_stopped_data_address != NULL)
2774 lp->stopped_data_address_p =
2775 linux_ops->to_stopped_data_address (¤t_target,
2776 &lp->stopped_data_address);
2778 lp->stopped_data_address_p = 0;
2781 do_cleanups (old_chain);
2784 /* See save_sigtrap. */
2787 linux_nat_stopped_by_watchpoint (void)
2789 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2791 gdb_assert (lp != NULL);
2793 return lp->stopped_by_watchpoint;
2797 linux_nat_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
2799 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2801 gdb_assert (lp != NULL);
2803 *addr_p = lp->stopped_data_address;
2805 return lp->stopped_data_address_p;
2808 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2811 sigtrap_is_event (int status)
2813 return WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP;
2816 /* SIGTRAP-like events recognizer. */
2818 static int (*linux_nat_status_is_event) (int status) = sigtrap_is_event;
2820 /* Check for SIGTRAP-like events in LP. */
2823 linux_nat_lp_status_is_event (struct lwp_info *lp)
2825 /* We check for lp->waitstatus in addition to lp->status, because we can
2826 have pending process exits recorded in lp->status
2827 and W_EXITCODE(0,0) == 0. We should probably have an additional
2828 lp->status_p flag. */
2830 return (lp->waitstatus.kind == TARGET_WAITKIND_IGNORE
2831 && linux_nat_status_is_event (lp->status));
2834 /* Set alternative SIGTRAP-like events recognizer. If
2835 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2839 linux_nat_set_status_is_event (struct target_ops *t,
2840 int (*status_is_event) (int status))
2842 linux_nat_status_is_event = status_is_event;
2845 /* Wait until LP is stopped. */
2848 stop_wait_callback (struct lwp_info *lp, void *data)
2850 struct inferior *inf = find_inferior_pid (GET_PID (lp->ptid));
2852 /* If this is a vfork parent, bail out, it is not going to report
2853 any SIGSTOP until the vfork is done with. */
2854 if (inf->vfork_child != NULL)
2861 status = wait_lwp (lp);
2865 if (lp->ignore_sigint && WIFSTOPPED (status)
2866 && WSTOPSIG (status) == SIGINT)
2868 lp->ignore_sigint = 0;
2871 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2872 if (debug_linux_nat)
2873 fprintf_unfiltered (gdb_stdlog,
2874 "PTRACE_CONT %s, 0, 0 (%s) "
2875 "(discarding SIGINT)\n",
2876 target_pid_to_str (lp->ptid),
2877 errno ? safe_strerror (errno) : "OK");
2879 return stop_wait_callback (lp, NULL);
2882 maybe_clear_ignore_sigint (lp);
2884 if (WSTOPSIG (status) != SIGSTOP)
2886 /* The thread was stopped with a signal other than SIGSTOP. */
2890 if (debug_linux_nat)
2891 fprintf_unfiltered (gdb_stdlog,
2892 "SWC: Pending event %s in %s\n",
2893 status_to_str ((int) status),
2894 target_pid_to_str (lp->ptid));
2896 /* Save the sigtrap event. */
2897 lp->status = status;
2898 gdb_assert (!lp->stopped);
2899 gdb_assert (lp->signalled);
2904 /* We caught the SIGSTOP that we intended to catch, so
2905 there's no SIGSTOP pending. */
2907 if (debug_linux_nat)
2908 fprintf_unfiltered (gdb_stdlog,
2909 "SWC: Delayed SIGSTOP caught for %s.\n",
2910 target_pid_to_str (lp->ptid));
2914 /* Reset SIGNALLED only after the stop_wait_callback call
2915 above as it does gdb_assert on SIGNALLED. */
2923 /* Return non-zero if LP has a wait status pending. */
2926 status_callback (struct lwp_info *lp, void *data)
2928 /* Only report a pending wait status if we pretend that this has
2929 indeed been resumed. */
2933 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2935 /* A ptrace event, like PTRACE_FORK|VFORK|EXEC, syscall event,
2936 or a pending process exit. Note that `W_EXITCODE(0,0) ==
2937 0', so a clean process exit can not be stored pending in
2938 lp->status, it is indistinguishable from
2939 no-pending-status. */
2943 if (lp->status != 0)
2949 /* Return non-zero if LP isn't stopped. */
2952 running_callback (struct lwp_info *lp, void *data)
2954 return (!lp->stopped
2955 || ((lp->status != 0
2956 || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2960 /* Count the LWP's that have had events. */
2963 count_events_callback (struct lwp_info *lp, void *data)
2967 gdb_assert (count != NULL);
2969 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2970 if (lp->resumed && linux_nat_lp_status_is_event (lp))
2976 /* Select the LWP (if any) that is currently being single-stepped. */
2979 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
2981 if (lp->last_resume_kind == resume_step
2988 /* Select the Nth LWP that has had a SIGTRAP event. */
2991 select_event_lwp_callback (struct lwp_info *lp, void *data)
2993 int *selector = data;
2995 gdb_assert (selector != NULL);
2997 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2998 if (lp->resumed && linux_nat_lp_status_is_event (lp))
2999 if ((*selector)-- == 0)
3006 cancel_breakpoint (struct lwp_info *lp)
3008 /* Arrange for a breakpoint to be hit again later. We don't keep
3009 the SIGTRAP status and don't forward the SIGTRAP signal to the
3010 LWP. We will handle the current event, eventually we will resume
3011 this LWP, and this breakpoint will trap again.
3013 If we do not do this, then we run the risk that the user will
3014 delete or disable the breakpoint, but the LWP will have already
3017 struct regcache *regcache = get_thread_regcache (lp->ptid);
3018 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3021 pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
3022 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
3024 if (debug_linux_nat)
3025 fprintf_unfiltered (gdb_stdlog,
3026 "CB: Push back breakpoint for %s\n",
3027 target_pid_to_str (lp->ptid));
3029 /* Back up the PC if necessary. */
3030 if (gdbarch_decr_pc_after_break (gdbarch))
3031 regcache_write_pc (regcache, pc);
3039 cancel_breakpoints_callback (struct lwp_info *lp, void *data)
3041 struct lwp_info *event_lp = data;
3043 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
3047 /* If a LWP other than the LWP that we're reporting an event for has
3048 hit a GDB breakpoint (as opposed to some random trap signal),
3049 then just arrange for it to hit it again later. We don't keep
3050 the SIGTRAP status and don't forward the SIGTRAP signal to the
3051 LWP. We will handle the current event, eventually we will resume
3052 all LWPs, and this one will get its breakpoint trap again.
3054 If we do not do this, then we run the risk that the user will
3055 delete or disable the breakpoint, but the LWP will have already
3058 if (linux_nat_lp_status_is_event (lp)
3059 && cancel_breakpoint (lp))
3060 /* Throw away the SIGTRAP. */
3066 /* Select one LWP out of those that have events pending. */
3069 select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status)
3072 int random_selector;
3073 struct lwp_info *event_lp;
3075 /* Record the wait status for the original LWP. */
3076 (*orig_lp)->status = *status;
3078 /* Give preference to any LWP that is being single-stepped. */
3079 event_lp = iterate_over_lwps (filter,
3080 select_singlestep_lwp_callback, NULL);
3081 if (event_lp != NULL)
3083 if (debug_linux_nat)
3084 fprintf_unfiltered (gdb_stdlog,
3085 "SEL: Select single-step %s\n",
3086 target_pid_to_str (event_lp->ptid));
3090 /* No single-stepping LWP. Select one at random, out of those
3091 which have had SIGTRAP events. */
3093 /* First see how many SIGTRAP events we have. */
3094 iterate_over_lwps (filter, count_events_callback, &num_events);
3096 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
3097 random_selector = (int)
3098 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
3100 if (debug_linux_nat && num_events > 1)
3101 fprintf_unfiltered (gdb_stdlog,
3102 "SEL: Found %d SIGTRAP events, selecting #%d\n",
3103 num_events, random_selector);
3105 event_lp = iterate_over_lwps (filter,
3106 select_event_lwp_callback,
3110 if (event_lp != NULL)
3112 /* Switch the event LWP. */
3113 *orig_lp = event_lp;
3114 *status = event_lp->status;
3117 /* Flush the wait status for the event LWP. */
3118 (*orig_lp)->status = 0;
3121 /* Return non-zero if LP has been resumed. */
3124 resumed_callback (struct lwp_info *lp, void *data)
3129 /* Stop an active thread, verify it still exists, then resume it. If
3130 the thread ends up with a pending status, then it is not resumed,
3131 and *DATA (really a pointer to int), is set. */
3134 stop_and_resume_callback (struct lwp_info *lp, void *data)
3136 int *new_pending_p = data;
3140 ptid_t ptid = lp->ptid;
3142 stop_callback (lp, NULL);
3143 stop_wait_callback (lp, NULL);
3145 /* Resume if the lwp still exists, and the core wanted it
3147 lp = find_lwp_pid (ptid);
3150 if (lp->last_resume_kind == resume_stop
3153 /* The core wanted the LWP to stop. Even if it stopped
3154 cleanly (with SIGSTOP), leave the event pending. */
3155 if (debug_linux_nat)
3156 fprintf_unfiltered (gdb_stdlog,
3157 "SARC: core wanted LWP %ld stopped "
3158 "(leaving SIGSTOP pending)\n",
3159 GET_LWP (lp->ptid));
3160 lp->status = W_STOPCODE (SIGSTOP);
3163 if (lp->status == 0)
3165 if (debug_linux_nat)
3166 fprintf_unfiltered (gdb_stdlog,
3167 "SARC: re-resuming LWP %ld\n",
3168 GET_LWP (lp->ptid));
3169 resume_lwp (lp, lp->step, GDB_SIGNAL_0);
3173 if (debug_linux_nat)
3174 fprintf_unfiltered (gdb_stdlog,
3175 "SARC: not re-resuming LWP %ld "
3177 GET_LWP (lp->ptid));
3186 /* Check if we should go on and pass this event to common code.
3187 Return the affected lwp if we are, or NULL otherwise. If we stop
3188 all lwps temporarily, we may end up with new pending events in some
3189 other lwp. In that case set *NEW_PENDING_P to true. */
3191 static struct lwp_info *
3192 linux_nat_filter_event (int lwpid, int status, int *new_pending_p)
3194 struct lwp_info *lp;
3198 lp = find_lwp_pid (pid_to_ptid (lwpid));
3200 /* Check for stop events reported by a process we didn't already
3201 know about - anything not already in our LWP list.
3203 If we're expecting to receive stopped processes after
3204 fork, vfork, and clone events, then we'll just add the
3205 new one to our list and go back to waiting for the event
3206 to be reported - the stopped process might be returned
3207 from waitpid before or after the event is.
3209 But note the case of a non-leader thread exec'ing after the
3210 leader having exited, and gone from our lists. The non-leader
3211 thread changes its tid to the tgid. */
3213 if (WIFSTOPPED (status) && lp == NULL
3214 && (WSTOPSIG (status) == SIGTRAP && status >> 16 == PTRACE_EVENT_EXEC))
3216 /* A multi-thread exec after we had seen the leader exiting. */
3217 if (debug_linux_nat)
3218 fprintf_unfiltered (gdb_stdlog,
3219 "LLW: Re-adding thread group leader LWP %d.\n",
3222 lp = add_lwp (BUILD_LWP (lwpid, lwpid));
3225 add_thread (lp->ptid);
3228 if (WIFSTOPPED (status) && !lp)
3230 add_to_pid_list (&stopped_pids, lwpid, status);
3234 /* Make sure we don't report an event for the exit of an LWP not in
3235 our list, i.e. not part of the current process. This can happen
3236 if we detach from a program we originally forked and then it
3238 if (!WIFSTOPPED (status) && !lp)
3241 /* Handle GNU/Linux's syscall SIGTRAPs. */
3242 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
3244 /* No longer need the sysgood bit. The ptrace event ends up
3245 recorded in lp->waitstatus if we care for it. We can carry
3246 on handling the event like a regular SIGTRAP from here
3248 status = W_STOPCODE (SIGTRAP);
3249 if (linux_handle_syscall_trap (lp, 0))
3253 /* Handle GNU/Linux's extended waitstatus for trace events. */
3254 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
3256 if (debug_linux_nat)
3257 fprintf_unfiltered (gdb_stdlog,
3258 "LLW: Handling extended status 0x%06x\n",
3260 if (linux_handle_extended_wait (lp, status, 0))
3264 if (linux_nat_status_is_event (status))
3267 /* Check if the thread has exited. */
3268 if ((WIFEXITED (status) || WIFSIGNALED (status))
3269 && num_lwps (GET_PID (lp->ptid)) > 1)
3271 /* If this is the main thread, we must stop all threads and verify
3272 if they are still alive. This is because in the nptl thread model
3273 on Linux 2.4, there is no signal issued for exiting LWPs
3274 other than the main thread. We only get the main thread exit
3275 signal once all child threads have already exited. If we
3276 stop all the threads and use the stop_wait_callback to check
3277 if they have exited we can determine whether this signal
3278 should be ignored or whether it means the end of the debugged
3279 application, regardless of which threading model is being
3281 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
3284 iterate_over_lwps (pid_to_ptid (GET_PID (lp->ptid)),
3285 stop_and_resume_callback, new_pending_p);
3288 if (debug_linux_nat)
3289 fprintf_unfiltered (gdb_stdlog,
3290 "LLW: %s exited.\n",
3291 target_pid_to_str (lp->ptid));
3293 if (num_lwps (GET_PID (lp->ptid)) > 1)
3295 /* If there is at least one more LWP, then the exit signal
3296 was not the end of the debugged application and should be
3303 /* Check if the current LWP has previously exited. In the nptl
3304 thread model, LWPs other than the main thread do not issue
3305 signals when they exit so we must check whenever the thread has
3306 stopped. A similar check is made in stop_wait_callback(). */
3307 if (num_lwps (GET_PID (lp->ptid)) > 1 && !linux_thread_alive (lp->ptid))
3309 ptid_t ptid = pid_to_ptid (GET_PID (lp->ptid));
3311 if (debug_linux_nat)
3312 fprintf_unfiltered (gdb_stdlog,
3313 "LLW: %s exited.\n",
3314 target_pid_to_str (lp->ptid));
3318 /* Make sure there is at least one thread running. */
3319 gdb_assert (iterate_over_lwps (ptid, running_callback, NULL));
3321 /* Discard the event. */
3325 /* Make sure we don't report a SIGSTOP that we sent ourselves in
3326 an attempt to stop an LWP. */
3328 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
3330 if (debug_linux_nat)
3331 fprintf_unfiltered (gdb_stdlog,
3332 "LLW: Delayed SIGSTOP caught for %s.\n",
3333 target_pid_to_str (lp->ptid));
3337 if (lp->last_resume_kind != resume_stop)
3339 /* This is a delayed SIGSTOP. */
3341 registers_changed ();
3343 if (linux_nat_prepare_to_resume != NULL)
3344 linux_nat_prepare_to_resume (lp);
3345 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
3346 lp->step, GDB_SIGNAL_0);
3347 if (debug_linux_nat)
3348 fprintf_unfiltered (gdb_stdlog,
3349 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
3351 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3352 target_pid_to_str (lp->ptid));
3355 gdb_assert (lp->resumed);
3357 /* Discard the event. */
3362 /* Make sure we don't report a SIGINT that we have already displayed
3363 for another thread. */
3364 if (lp->ignore_sigint
3365 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT)
3367 if (debug_linux_nat)
3368 fprintf_unfiltered (gdb_stdlog,
3369 "LLW: Delayed SIGINT caught for %s.\n",
3370 target_pid_to_str (lp->ptid));
3372 /* This is a delayed SIGINT. */
3373 lp->ignore_sigint = 0;
3375 registers_changed ();
3376 if (linux_nat_prepare_to_resume != NULL)
3377 linux_nat_prepare_to_resume (lp);
3378 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
3379 lp->step, GDB_SIGNAL_0);
3380 if (debug_linux_nat)
3381 fprintf_unfiltered (gdb_stdlog,
3382 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
3384 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3385 target_pid_to_str (lp->ptid));
3388 gdb_assert (lp->resumed);
3390 /* Discard the event. */
3394 /* An interesting event. */
3396 lp->status = status;
3400 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3401 their exits until all other threads in the group have exited. */
3404 check_zombie_leaders (void)
3406 struct inferior *inf;
3410 struct lwp_info *leader_lp;
3415 leader_lp = find_lwp_pid (pid_to_ptid (inf->pid));
3416 if (leader_lp != NULL
3417 /* Check if there are other threads in the group, as we may
3418 have raced with the inferior simply exiting. */
3419 && num_lwps (inf->pid) > 1
3420 && linux_proc_pid_is_zombie (inf->pid))
3422 if (debug_linux_nat)
3423 fprintf_unfiltered (gdb_stdlog,
3424 "CZL: Thread group leader %d zombie "
3425 "(it exited, or another thread execd).\n",
3428 /* A leader zombie can mean one of two things:
3430 - It exited, and there's an exit status pending
3431 available, or only the leader exited (not the whole
3432 program). In the latter case, we can't waitpid the
3433 leader's exit status until all other threads are gone.
3435 - There are 3 or more threads in the group, and a thread
3436 other than the leader exec'd. On an exec, the Linux
3437 kernel destroys all other threads (except the execing
3438 one) in the thread group, and resets the execing thread's
3439 tid to the tgid. No exit notification is sent for the
3440 execing thread -- from the ptracer's perspective, it
3441 appears as though the execing thread just vanishes.
3442 Until we reap all other threads except the leader and the
3443 execing thread, the leader will be zombie, and the
3444 execing thread will be in `D (disc sleep)'. As soon as
3445 all other threads are reaped, the execing thread changes
3446 it's tid to the tgid, and the previous (zombie) leader
3447 vanishes, giving place to the "new" leader. We could try
3448 distinguishing the exit and exec cases, by waiting once
3449 more, and seeing if something comes out, but it doesn't
3450 sound useful. The previous leader _does_ go away, and
3451 we'll re-add the new one once we see the exec event
3452 (which is just the same as what would happen if the
3453 previous leader did exit voluntarily before some other
3456 if (debug_linux_nat)
3457 fprintf_unfiltered (gdb_stdlog,
3458 "CZL: Thread group leader %d vanished.\n",
3460 exit_lwp (leader_lp);
3466 linux_nat_wait_1 (struct target_ops *ops,
3467 ptid_t ptid, struct target_waitstatus *ourstatus,
3470 static sigset_t prev_mask;
3471 enum resume_kind last_resume_kind;
3472 struct lwp_info *lp;
3475 if (debug_linux_nat)
3476 fprintf_unfiltered (gdb_stdlog, "LLW: enter\n");
3478 /* The first time we get here after starting a new inferior, we may
3479 not have added it to the LWP list yet - this is the earliest
3480 moment at which we know its PID. */
3481 if (ptid_is_pid (inferior_ptid))
3483 /* Upgrade the main thread's ptid. */
3484 thread_change_ptid (inferior_ptid,
3485 BUILD_LWP (GET_PID (inferior_ptid),
3486 GET_PID (inferior_ptid)));
3488 lp = add_lwp (inferior_ptid);
3492 /* Make sure SIGCHLD is blocked. */
3493 block_child_signals (&prev_mask);
3499 /* First check if there is a LWP with a wait status pending. */
3500 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
3502 /* Any LWP in the PTID group that's been resumed will do. */
3503 lp = iterate_over_lwps (ptid, status_callback, NULL);
3506 if (debug_linux_nat && lp->status)
3507 fprintf_unfiltered (gdb_stdlog,
3508 "LLW: Using pending wait status %s for %s.\n",
3509 status_to_str (lp->status),
3510 target_pid_to_str (lp->ptid));
3513 else if (is_lwp (ptid))
3515 if (debug_linux_nat)
3516 fprintf_unfiltered (gdb_stdlog,
3517 "LLW: Waiting for specific LWP %s.\n",
3518 target_pid_to_str (ptid));
3520 /* We have a specific LWP to check. */
3521 lp = find_lwp_pid (ptid);
3524 if (debug_linux_nat && lp->status)
3525 fprintf_unfiltered (gdb_stdlog,
3526 "LLW: Using pending wait status %s for %s.\n",
3527 status_to_str (lp->status),
3528 target_pid_to_str (lp->ptid));
3530 /* We check for lp->waitstatus in addition to lp->status,
3531 because we can have pending process exits recorded in
3532 lp->status and W_EXITCODE(0,0) == 0. We should probably have
3533 an additional lp->status_p flag. */
3534 if (lp->status == 0 && lp->waitstatus.kind == TARGET_WAITKIND_IGNORE)
3538 if (!target_can_async_p ())
3540 /* Causes SIGINT to be passed on to the attached process. */
3544 /* But if we don't find a pending event, we'll have to wait. */
3550 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3553 - If the thread group leader exits while other threads in the
3554 thread group still exist, waitpid(TGID, ...) hangs. That
3555 waitpid won't return an exit status until the other threads
3556 in the group are reapped.
3558 - When a non-leader thread execs, that thread just vanishes
3559 without reporting an exit (so we'd hang if we waited for it
3560 explicitly in that case). The exec event is reported to
3564 lwpid = my_waitpid (-1, &status, __WCLONE | WNOHANG);
3565 if (lwpid == 0 || (lwpid == -1 && errno == ECHILD))
3566 lwpid = my_waitpid (-1, &status, WNOHANG);
3568 if (debug_linux_nat)
3569 fprintf_unfiltered (gdb_stdlog,
3570 "LNW: waitpid(-1, ...) returned %d, %s\n",
3571 lwpid, errno ? safe_strerror (errno) : "ERRNO-OK");
3575 /* If this is true, then we paused LWPs momentarily, and may
3576 now have pending events to handle. */
3579 if (debug_linux_nat)
3581 fprintf_unfiltered (gdb_stdlog,
3582 "LLW: waitpid %ld received %s\n",
3583 (long) lwpid, status_to_str (status));
3586 lp = linux_nat_filter_event (lwpid, status, &new_pending);
3588 /* STATUS is now no longer valid, use LP->STATUS instead. */
3591 if (lp && !ptid_match (lp->ptid, ptid))
3593 gdb_assert (lp->resumed);
3595 if (debug_linux_nat)
3597 "LWP %ld got an event %06x, leaving pending.\n",
3598 ptid_get_lwp (lp->ptid), lp->status);
3600 if (WIFSTOPPED (lp->status))
3602 if (WSTOPSIG (lp->status) != SIGSTOP)
3604 /* Cancel breakpoint hits. The breakpoint may
3605 be removed before we fetch events from this
3606 process to report to the core. It is best
3607 not to assume the moribund breakpoints
3608 heuristic always handles these cases --- it
3609 could be too many events go through to the
3610 core before this one is handled. All-stop
3611 always cancels breakpoint hits in all
3614 && linux_nat_lp_status_is_event (lp)
3615 && cancel_breakpoint (lp))
3617 /* Throw away the SIGTRAP. */
3620 if (debug_linux_nat)
3622 "LLW: LWP %ld hit a breakpoint while"
3623 " waiting for another process;"
3625 ptid_get_lwp (lp->ptid));
3635 else if (WIFEXITED (lp->status) || WIFSIGNALED (lp->status))
3637 if (debug_linux_nat)
3639 "Process %ld exited while stopping LWPs\n",
3640 ptid_get_lwp (lp->ptid));
3642 /* This was the last lwp in the process. Since
3643 events are serialized to GDB core, and we can't
3644 report this one right now, but GDB core and the
3645 other target layers will want to be notified
3646 about the exit code/signal, leave the status
3647 pending for the next time we're able to report
3650 /* Prevent trying to stop this thread again. We'll
3651 never try to resume it because it has a pending
3655 /* Dead LWP's aren't expected to reported a pending
3659 /* Store the pending event in the waitstatus as
3660 well, because W_EXITCODE(0,0) == 0. */
3661 store_waitstatus (&lp->waitstatus, lp->status);
3670 /* Some LWP now has a pending event. Go all the way
3671 back to check it. */
3677 /* We got an event to report to the core. */
3681 /* Retry until nothing comes out of waitpid. A single
3682 SIGCHLD can indicate more than one child stopped. */
3686 /* Check for zombie thread group leaders. Those can't be reaped
3687 until all other threads in the thread group are. */
3688 check_zombie_leaders ();
3690 /* If there are no resumed children left, bail. We'd be stuck
3691 forever in the sigsuspend call below otherwise. */
3692 if (iterate_over_lwps (ptid, resumed_callback, NULL) == NULL)
3694 if (debug_linux_nat)
3695 fprintf_unfiltered (gdb_stdlog, "LLW: exit (no resumed LWP)\n");
3697 ourstatus->kind = TARGET_WAITKIND_NO_RESUMED;
3699 if (!target_can_async_p ())
3700 clear_sigint_trap ();
3702 restore_child_signals_mask (&prev_mask);
3703 return minus_one_ptid;
3706 /* No interesting event to report to the core. */
3708 if (target_options & TARGET_WNOHANG)
3710 if (debug_linux_nat)
3711 fprintf_unfiltered (gdb_stdlog, "LLW: exit (ignore)\n");
3713 ourstatus->kind = TARGET_WAITKIND_IGNORE;
3714 restore_child_signals_mask (&prev_mask);
3715 return minus_one_ptid;
3718 /* We shouldn't end up here unless we want to try again. */
3719 gdb_assert (lp == NULL);
3721 /* Block until we get an event reported with SIGCHLD. */
3722 sigsuspend (&suspend_mask);
3725 if (!target_can_async_p ())
3726 clear_sigint_trap ();
3730 status = lp->status;
3733 /* Don't report signals that GDB isn't interested in, such as
3734 signals that are neither printed nor stopped upon. Stopping all
3735 threads can be a bit time-consuming so if we want decent
3736 performance with heavily multi-threaded programs, especially when
3737 they're using a high frequency timer, we'd better avoid it if we
3740 if (WIFSTOPPED (status))
3742 enum gdb_signal signo = gdb_signal_from_host (WSTOPSIG (status));
3744 /* When using hardware single-step, we need to report every signal.
3745 Otherwise, signals in pass_mask may be short-circuited. */
3747 && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status)))
3749 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3750 here? It is not clear we should. GDB may not expect
3751 other threads to run. On the other hand, not resuming
3752 newly attached threads may cause an unwanted delay in
3753 getting them running. */
3754 registers_changed ();
3755 if (linux_nat_prepare_to_resume != NULL)
3756 linux_nat_prepare_to_resume (lp);
3757 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
3759 if (debug_linux_nat)
3760 fprintf_unfiltered (gdb_stdlog,
3761 "LLW: %s %s, %s (preempt 'handle')\n",
3763 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3764 target_pid_to_str (lp->ptid),
3765 (signo != GDB_SIGNAL_0
3766 ? strsignal (gdb_signal_to_host (signo))
3774 /* Only do the below in all-stop, as we currently use SIGINT
3775 to implement target_stop (see linux_nat_stop) in
3777 if (signo == GDB_SIGNAL_INT && signal_pass_state (signo) == 0)
3779 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3780 forwarded to the entire process group, that is, all LWPs
3781 will receive it - unless they're using CLONE_THREAD to
3782 share signals. Since we only want to report it once, we
3783 mark it as ignored for all LWPs except this one. */
3784 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid)),
3785 set_ignore_sigint, NULL);
3786 lp->ignore_sigint = 0;
3789 maybe_clear_ignore_sigint (lp);
3793 /* This LWP is stopped now. */
3796 if (debug_linux_nat)
3797 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
3798 status_to_str (status), target_pid_to_str (lp->ptid));
3802 /* Now stop all other LWP's ... */
3803 iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
3805 /* ... and wait until all of them have reported back that
3806 they're no longer running. */
3807 iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
3809 /* If we're not waiting for a specific LWP, choose an event LWP
3810 from among those that have had events. Giving equal priority
3811 to all LWPs that have had events helps prevent
3813 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
3814 select_event_lwp (ptid, &lp, &status);
3816 /* Now that we've selected our final event LWP, cancel any
3817 breakpoints in other LWPs that have hit a GDB breakpoint.
3818 See the comment in cancel_breakpoints_callback to find out
3820 iterate_over_lwps (minus_one_ptid, cancel_breakpoints_callback, lp);
3822 /* We'll need this to determine whether to report a SIGSTOP as
3823 TARGET_WAITKIND_0. Need to take a copy because
3824 resume_clear_callback clears it. */
3825 last_resume_kind = lp->last_resume_kind;
3827 /* In all-stop, from the core's perspective, all LWPs are now
3828 stopped until a new resume action is sent over. */
3829 iterate_over_lwps (minus_one_ptid, resume_clear_callback, NULL);
3834 last_resume_kind = lp->last_resume_kind;
3835 resume_clear_callback (lp, NULL);
3838 if (linux_nat_status_is_event (status))
3840 if (debug_linux_nat)
3841 fprintf_unfiltered (gdb_stdlog,
3842 "LLW: trap ptid is %s.\n",
3843 target_pid_to_str (lp->ptid));
3846 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3848 *ourstatus = lp->waitstatus;
3849 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
3852 store_waitstatus (ourstatus, status);
3854 if (debug_linux_nat)
3855 fprintf_unfiltered (gdb_stdlog, "LLW: exit\n");
3857 restore_child_signals_mask (&prev_mask);
3859 if (last_resume_kind == resume_stop
3860 && ourstatus->kind == TARGET_WAITKIND_STOPPED
3861 && WSTOPSIG (status) == SIGSTOP)
3863 /* A thread that has been requested to stop by GDB with
3864 target_stop, and it stopped cleanly, so report as SIG0. The
3865 use of SIGSTOP is an implementation detail. */
3866 ourstatus->value.sig = GDB_SIGNAL_0;
3869 if (ourstatus->kind == TARGET_WAITKIND_EXITED
3870 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
3873 lp->core = linux_common_core_of_thread (lp->ptid);
3878 /* Resume LWPs that are currently stopped without any pending status
3879 to report, but are resumed from the core's perspective. */
3882 resume_stopped_resumed_lwps (struct lwp_info *lp, void *data)
3884 ptid_t *wait_ptid_p = data;
3889 && lp->waitstatus.kind == TARGET_WAITKIND_IGNORE)
3891 struct regcache *regcache = get_thread_regcache (lp->ptid);
3892 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3893 CORE_ADDR pc = regcache_read_pc (regcache);
3895 gdb_assert (is_executing (lp->ptid));
3897 /* Don't bother if there's a breakpoint at PC that we'd hit
3898 immediately, and we're not waiting for this LWP. */
3899 if (!ptid_match (lp->ptid, *wait_ptid_p))
3901 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
3905 if (debug_linux_nat)
3906 fprintf_unfiltered (gdb_stdlog,
3907 "RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
3908 target_pid_to_str (lp->ptid),
3909 paddress (gdbarch, pc),
3912 registers_changed ();
3913 if (linux_nat_prepare_to_resume != NULL)
3914 linux_nat_prepare_to_resume (lp);
3915 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
3916 lp->step, GDB_SIGNAL_0);
3918 lp->stopped_by_watchpoint = 0;
3925 linux_nat_wait (struct target_ops *ops,
3926 ptid_t ptid, struct target_waitstatus *ourstatus,
3931 if (debug_linux_nat)
3933 char *options_string;
3935 options_string = target_options_to_string (target_options);
3936 fprintf_unfiltered (gdb_stdlog,
3937 "linux_nat_wait: [%s], [%s]\n",
3938 target_pid_to_str (ptid),
3940 xfree (options_string);
3943 /* Flush the async file first. */
3944 if (target_can_async_p ())
3945 async_file_flush ();
3947 /* Resume LWPs that are currently stopped without any pending status
3948 to report, but are resumed from the core's perspective. LWPs get
3949 in this state if we find them stopping at a time we're not
3950 interested in reporting the event (target_wait on a
3951 specific_process, for example, see linux_nat_wait_1), and
3952 meanwhile the event became uninteresting. Don't bother resuming
3953 LWPs we're not going to wait for if they'd stop immediately. */
3955 iterate_over_lwps (minus_one_ptid, resume_stopped_resumed_lwps, &ptid);
3957 event_ptid = linux_nat_wait_1 (ops, ptid, ourstatus, target_options);
3959 /* If we requested any event, and something came out, assume there
3960 may be more. If we requested a specific lwp or process, also
3961 assume there may be more. */
3962 if (target_can_async_p ()
3963 && ((ourstatus->kind != TARGET_WAITKIND_IGNORE
3964 && ourstatus->kind != TARGET_WAITKIND_NO_RESUMED)
3965 || !ptid_equal (ptid, minus_one_ptid)))
3968 /* Get ready for the next event. */
3969 if (target_can_async_p ())
3970 target_async (inferior_event_handler, 0);
3976 kill_callback (struct lwp_info *lp, void *data)
3978 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
3981 kill (GET_LWP (lp->ptid), SIGKILL);
3982 if (debug_linux_nat)
3983 fprintf_unfiltered (gdb_stdlog,
3984 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
3985 target_pid_to_str (lp->ptid),
3986 errno ? safe_strerror (errno) : "OK");
3988 /* Some kernels ignore even SIGKILL for processes under ptrace. */
3991 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
3992 if (debug_linux_nat)
3993 fprintf_unfiltered (gdb_stdlog,
3994 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3995 target_pid_to_str (lp->ptid),
3996 errno ? safe_strerror (errno) : "OK");
4002 kill_wait_callback (struct lwp_info *lp, void *data)
4006 /* We must make sure that there are no pending events (delayed
4007 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
4008 program doesn't interfere with any following debugging session. */
4010 /* For cloned processes we must check both with __WCLONE and
4011 without, since the exit status of a cloned process isn't reported
4017 pid = my_waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
4018 if (pid != (pid_t) -1)
4020 if (debug_linux_nat)
4021 fprintf_unfiltered (gdb_stdlog,
4022 "KWC: wait %s received unknown.\n",
4023 target_pid_to_str (lp->ptid));
4024 /* The Linux kernel sometimes fails to kill a thread
4025 completely after PTRACE_KILL; that goes from the stop
4026 point in do_fork out to the one in
4027 get_signal_to_deliever and waits again. So kill it
4029 kill_callback (lp, NULL);
4032 while (pid == GET_LWP (lp->ptid));
4034 gdb_assert (pid == -1 && errno == ECHILD);
4039 pid = my_waitpid (GET_LWP (lp->ptid), NULL, 0);
4040 if (pid != (pid_t) -1)
4042 if (debug_linux_nat)
4043 fprintf_unfiltered (gdb_stdlog,
4044 "KWC: wait %s received unk.\n",
4045 target_pid_to_str (lp->ptid));
4046 /* See the call to kill_callback above. */
4047 kill_callback (lp, NULL);
4050 while (pid == GET_LWP (lp->ptid));
4052 gdb_assert (pid == -1 && errno == ECHILD);
4057 linux_nat_kill (struct target_ops *ops)
4059 struct target_waitstatus last;
4063 /* If we're stopped while forking and we haven't followed yet,
4064 kill the other task. We need to do this first because the
4065 parent will be sleeping if this is a vfork. */
4067 get_last_target_status (&last_ptid, &last);
4069 if (last.kind == TARGET_WAITKIND_FORKED
4070 || last.kind == TARGET_WAITKIND_VFORKED)
4072 ptrace (PT_KILL, PIDGET (last.value.related_pid), 0, 0);
4076 if (forks_exist_p ())
4077 linux_fork_killall ();
4080 ptid_t ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
4082 /* Stop all threads before killing them, since ptrace requires
4083 that the thread is stopped to sucessfully PTRACE_KILL. */
4084 iterate_over_lwps (ptid, stop_callback, NULL);
4085 /* ... and wait until all of them have reported back that
4086 they're no longer running. */
4087 iterate_over_lwps (ptid, stop_wait_callback, NULL);
4089 /* Kill all LWP's ... */
4090 iterate_over_lwps (ptid, kill_callback, NULL);
4092 /* ... and wait until we've flushed all events. */
4093 iterate_over_lwps (ptid, kill_wait_callback, NULL);
4096 target_mourn_inferior ();
4100 linux_nat_mourn_inferior (struct target_ops *ops)
4102 purge_lwp_list (ptid_get_pid (inferior_ptid));
4104 if (! forks_exist_p ())
4105 /* Normal case, no other forks available. */
4106 linux_ops->to_mourn_inferior (ops);
4108 /* Multi-fork case. The current inferior_ptid has exited, but
4109 there are other viable forks to debug. Delete the exiting
4110 one and context-switch to the first available. */
4111 linux_fork_mourn_inferior ();
4114 /* Convert a native/host siginfo object, into/from the siginfo in the
4115 layout of the inferiors' architecture. */
4118 siginfo_fixup (siginfo_t *siginfo, gdb_byte *inf_siginfo, int direction)
4122 if (linux_nat_siginfo_fixup != NULL)
4123 done = linux_nat_siginfo_fixup (siginfo, inf_siginfo, direction);
4125 /* If there was no callback, or the callback didn't do anything,
4126 then just do a straight memcpy. */
4130 memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
4132 memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
4137 linux_xfer_siginfo (struct target_ops *ops, enum target_object object,
4138 const char *annex, gdb_byte *readbuf,
4139 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4143 gdb_byte inf_siginfo[sizeof (siginfo_t)];
4145 gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO);
4146 gdb_assert (readbuf || writebuf);
4148 pid = GET_LWP (inferior_ptid);
4150 pid = GET_PID (inferior_ptid);
4152 if (offset > sizeof (siginfo))
4156 ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
4160 /* When GDB is built as a 64-bit application, ptrace writes into
4161 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
4162 inferior with a 64-bit GDB should look the same as debugging it
4163 with a 32-bit GDB, we need to convert it. GDB core always sees
4164 the converted layout, so any read/write will have to be done
4166 siginfo_fixup (&siginfo, inf_siginfo, 0);
4168 if (offset + len > sizeof (siginfo))
4169 len = sizeof (siginfo) - offset;
4171 if (readbuf != NULL)
4172 memcpy (readbuf, inf_siginfo + offset, len);
4175 memcpy (inf_siginfo + offset, writebuf, len);
4177 /* Convert back to ptrace layout before flushing it out. */
4178 siginfo_fixup (&siginfo, inf_siginfo, 1);
4181 ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
4190 linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
4191 const char *annex, gdb_byte *readbuf,
4192 const gdb_byte *writebuf,
4193 ULONGEST offset, LONGEST len)
4195 struct cleanup *old_chain;
4198 if (object == TARGET_OBJECT_SIGNAL_INFO)
4199 return linux_xfer_siginfo (ops, object, annex, readbuf, writebuf,
4202 /* The target is connected but no live inferior is selected. Pass
4203 this request down to a lower stratum (e.g., the executable
4205 if (object == TARGET_OBJECT_MEMORY && ptid_equal (inferior_ptid, null_ptid))
4208 old_chain = save_inferior_ptid ();
4210 if (is_lwp (inferior_ptid))
4211 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
4213 xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
4216 do_cleanups (old_chain);
4221 linux_thread_alive (ptid_t ptid)
4225 gdb_assert (is_lwp (ptid));
4227 /* Send signal 0 instead of anything ptrace, because ptracing a
4228 running thread errors out claiming that the thread doesn't
4230 err = kill_lwp (GET_LWP (ptid), 0);
4232 if (debug_linux_nat)
4233 fprintf_unfiltered (gdb_stdlog,
4234 "LLTA: KILL(SIG0) %s (%s)\n",
4235 target_pid_to_str (ptid),
4236 err ? safe_strerror (tmp_errno) : "OK");
4245 linux_nat_thread_alive (struct target_ops *ops, ptid_t ptid)
4247 return linux_thread_alive (ptid);
4251 linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
4253 static char buf[64];
4256 && (GET_PID (ptid) != GET_LWP (ptid)
4257 || num_lwps (GET_PID (ptid)) > 1))
4259 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
4263 return normal_pid_to_str (ptid);
4267 linux_nat_thread_name (struct thread_info *thr)
4269 int pid = ptid_get_pid (thr->ptid);
4270 long lwp = ptid_get_lwp (thr->ptid);
4271 #define FORMAT "/proc/%d/task/%ld/comm"
4272 char buf[sizeof (FORMAT) + 30];
4274 char *result = NULL;
4276 snprintf (buf, sizeof (buf), FORMAT, pid, lwp);
4277 comm_file = fopen (buf, "r");
4280 /* Not exported by the kernel, so we define it here. */
4282 static char line[COMM_LEN + 1];
4284 if (fgets (line, sizeof (line), comm_file))
4286 char *nl = strchr (line, '\n');
4303 /* Accepts an integer PID; Returns a string representing a file that
4304 can be opened to get the symbols for the child process. */
4307 linux_child_pid_to_exec_file (int pid)
4309 char *name1, *name2;
4311 name1 = xmalloc (MAXPATHLEN);
4312 name2 = xmalloc (MAXPATHLEN);
4313 make_cleanup (xfree, name1);
4314 make_cleanup (xfree, name2);
4315 memset (name2, 0, MAXPATHLEN);
4317 sprintf (name1, "/proc/%d/exe", pid);
4318 if (readlink (name1, name2, MAXPATHLEN) > 0)
4324 /* Records the thread's register state for the corefile note
4328 linux_nat_collect_thread_registers (const struct regcache *regcache,
4329 ptid_t ptid, bfd *obfd,
4330 char *note_data, int *note_size,
4331 enum gdb_signal stop_signal)
4333 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4334 const struct regset *regset;
4336 gdb_gregset_t gregs;
4337 gdb_fpregset_t fpregs;
4339 core_regset_p = gdbarch_regset_from_core_section_p (gdbarch);
4342 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg",
4344 != NULL && regset->collect_regset != NULL)
4345 regset->collect_regset (regset, regcache, -1, &gregs, sizeof (gregs));
4347 fill_gregset (regcache, &gregs, -1);
4349 note_data = (char *) elfcore_write_prstatus
4350 (obfd, note_data, note_size, ptid_get_lwp (ptid),
4351 gdb_signal_to_host (stop_signal), &gregs);
4354 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg2",
4356 != NULL && regset->collect_regset != NULL)
4357 regset->collect_regset (regset, regcache, -1, &fpregs, sizeof (fpregs));
4359 fill_fpregset (regcache, &fpregs, -1);
4361 note_data = (char *) elfcore_write_prfpreg (obfd, note_data, note_size,
4362 &fpregs, sizeof (fpregs));
4367 /* Fills the "to_make_corefile_note" target vector. Builds the note
4368 section for a corefile, and returns it in a malloc buffer. */
4371 linux_nat_make_corefile_notes (bfd *obfd, int *note_size)
4373 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
4374 converted to gdbarch_core_regset_sections, this function can go away. */
4375 return linux_make_corefile_notes (target_gdbarch, obfd, note_size,
4376 linux_nat_collect_thread_registers);
4379 /* Implement the to_xfer_partial interface for memory reads using the /proc
4380 filesystem. Because we can use a single read() call for /proc, this
4381 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4382 but it doesn't support writes. */
4385 linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
4386 const char *annex, gdb_byte *readbuf,
4387 const gdb_byte *writebuf,
4388 ULONGEST offset, LONGEST len)
4394 if (object != TARGET_OBJECT_MEMORY || !readbuf)
4397 /* Don't bother for one word. */
4398 if (len < 3 * sizeof (long))
4401 /* We could keep this file open and cache it - possibly one per
4402 thread. That requires some juggling, but is even faster. */
4403 sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid));
4404 fd = open (filename, O_RDONLY | O_LARGEFILE);
4408 /* If pread64 is available, use it. It's faster if the kernel
4409 supports it (only one syscall), and it's 64-bit safe even on
4410 32-bit platforms (for instance, SPARC debugging a SPARC64
4413 if (pread64 (fd, readbuf, len, offset) != len)
4415 if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
4426 /* Enumerate spufs IDs for process PID. */
4428 spu_enumerate_spu_ids (int pid, gdb_byte *buf, ULONGEST offset, LONGEST len)
4430 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
4432 LONGEST written = 0;
4435 struct dirent *entry;
4437 xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
4438 dir = opendir (path);
4443 while ((entry = readdir (dir)) != NULL)
4449 fd = atoi (entry->d_name);
4453 xsnprintf (path, sizeof path, "/proc/%d/fd/%d", pid, fd);
4454 if (stat (path, &st) != 0)
4456 if (!S_ISDIR (st.st_mode))
4459 if (statfs (path, &stfs) != 0)
4461 if (stfs.f_type != SPUFS_MAGIC)
4464 if (pos >= offset && pos + 4 <= offset + len)
4466 store_unsigned_integer (buf + pos - offset, 4, byte_order, fd);
4476 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4477 object type, using the /proc file system. */
4479 linux_proc_xfer_spu (struct target_ops *ops, enum target_object object,
4480 const char *annex, gdb_byte *readbuf,
4481 const gdb_byte *writebuf,
4482 ULONGEST offset, LONGEST len)
4487 int pid = PIDGET (inferior_ptid);
4494 return spu_enumerate_spu_ids (pid, readbuf, offset, len);
4497 xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
4498 fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
4503 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
4510 ret = write (fd, writebuf, (size_t) len);
4512 ret = read (fd, readbuf, (size_t) len);
4519 /* Parse LINE as a signal set and add its set bits to SIGS. */
4522 add_line_to_sigset (const char *line, sigset_t *sigs)
4524 int len = strlen (line) - 1;
4528 if (line[len] != '\n')
4529 error (_("Could not parse signal set: %s"), line);
4537 if (*p >= '0' && *p <= '9')
4539 else if (*p >= 'a' && *p <= 'f')
4540 digit = *p - 'a' + 10;
4542 error (_("Could not parse signal set: %s"), line);
4547 sigaddset (sigs, signum + 1);
4549 sigaddset (sigs, signum + 2);
4551 sigaddset (sigs, signum + 3);
4553 sigaddset (sigs, signum + 4);
4559 /* Find process PID's pending signals from /proc/pid/status and set
4563 linux_proc_pending_signals (int pid, sigset_t *pending,
4564 sigset_t *blocked, sigset_t *ignored)
4567 char buffer[MAXPATHLEN], fname[MAXPATHLEN];
4568 struct cleanup *cleanup;
4570 sigemptyset (pending);
4571 sigemptyset (blocked);
4572 sigemptyset (ignored);
4573 sprintf (fname, "/proc/%d/status", pid);
4574 procfile = fopen (fname, "r");
4575 if (procfile == NULL)
4576 error (_("Could not open %s"), fname);
4577 cleanup = make_cleanup_fclose (procfile);
4579 while (fgets (buffer, MAXPATHLEN, procfile) != NULL)
4581 /* Normal queued signals are on the SigPnd line in the status
4582 file. However, 2.6 kernels also have a "shared" pending
4583 queue for delivering signals to a thread group, so check for
4586 Unfortunately some Red Hat kernels include the shared pending
4587 queue but not the ShdPnd status field. */
4589 if (strncmp (buffer, "SigPnd:\t", 8) == 0)
4590 add_line_to_sigset (buffer + 8, pending);
4591 else if (strncmp (buffer, "ShdPnd:\t", 8) == 0)
4592 add_line_to_sigset (buffer + 8, pending);
4593 else if (strncmp (buffer, "SigBlk:\t", 8) == 0)
4594 add_line_to_sigset (buffer + 8, blocked);
4595 else if (strncmp (buffer, "SigIgn:\t", 8) == 0)
4596 add_line_to_sigset (buffer + 8, ignored);
4599 do_cleanups (cleanup);
4603 linux_nat_xfer_osdata (struct target_ops *ops, enum target_object object,
4604 const char *annex, gdb_byte *readbuf,
4605 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4607 gdb_assert (object == TARGET_OBJECT_OSDATA);
4609 return linux_common_xfer_osdata (annex, readbuf, offset, len);
4613 linux_xfer_partial (struct target_ops *ops, enum target_object object,
4614 const char *annex, gdb_byte *readbuf,
4615 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4619 if (object == TARGET_OBJECT_AUXV)
4620 return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
4623 if (object == TARGET_OBJECT_OSDATA)
4624 return linux_nat_xfer_osdata (ops, object, annex, readbuf, writebuf,
4627 if (object == TARGET_OBJECT_SPU)
4628 return linux_proc_xfer_spu (ops, object, annex, readbuf, writebuf,
4631 /* GDB calculates all the addresses in possibly larget width of the address.
4632 Address width needs to be masked before its final use - either by
4633 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4635 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4637 if (object == TARGET_OBJECT_MEMORY)
4639 int addr_bit = gdbarch_addr_bit (target_gdbarch);
4641 if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
4642 offset &= ((ULONGEST) 1 << addr_bit) - 1;
4645 xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
4650 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
4655 cleanup_target_stop (void *arg)
4657 ptid_t *ptid = (ptid_t *) arg;
4659 gdb_assert (arg != NULL);
4662 target_resume (*ptid, 0, GDB_SIGNAL_0);
4665 static VEC(static_tracepoint_marker_p) *
4666 linux_child_static_tracepoint_markers_by_strid (const char *strid)
4668 char s[IPA_CMD_BUF_SIZE];
4669 struct cleanup *old_chain;
4670 int pid = ptid_get_pid (inferior_ptid);
4671 VEC(static_tracepoint_marker_p) *markers = NULL;
4672 struct static_tracepoint_marker *marker = NULL;
4674 ptid_t ptid = ptid_build (pid, 0, 0);
4679 memcpy (s, "qTfSTM", sizeof ("qTfSTM"));
4680 s[sizeof ("qTfSTM")] = 0;
4682 agent_run_command (pid, s, strlen (s) + 1);
4684 old_chain = make_cleanup (free_current_marker, &marker);
4685 make_cleanup (cleanup_target_stop, &ptid);
4690 marker = XCNEW (struct static_tracepoint_marker);
4694 parse_static_tracepoint_marker_definition (p, &p, marker);
4696 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
4698 VEC_safe_push (static_tracepoint_marker_p,
4704 release_static_tracepoint_marker (marker);
4705 memset (marker, 0, sizeof (*marker));
4708 while (*p++ == ','); /* comma-separated list */
4710 memcpy (s, "qTsSTM", sizeof ("qTsSTM"));
4711 s[sizeof ("qTsSTM")] = 0;
4712 agent_run_command (pid, s, strlen (s) + 1);
4716 do_cleanups (old_chain);
4721 /* Create a prototype generic GNU/Linux target. The client can override
4722 it with local methods. */
4725 linux_target_install_ops (struct target_ops *t)
4727 t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
4728 t->to_remove_fork_catchpoint = linux_child_remove_fork_catchpoint;
4729 t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
4730 t->to_remove_vfork_catchpoint = linux_child_remove_vfork_catchpoint;
4731 t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
4732 t->to_remove_exec_catchpoint = linux_child_remove_exec_catchpoint;
4733 t->to_set_syscall_catchpoint = linux_child_set_syscall_catchpoint;
4734 t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
4735 t->to_post_startup_inferior = linux_child_post_startup_inferior;
4736 t->to_post_attach = linux_child_post_attach;
4737 t->to_follow_fork = linux_child_follow_fork;
4738 t->to_make_corefile_notes = linux_nat_make_corefile_notes;
4740 super_xfer_partial = t->to_xfer_partial;
4741 t->to_xfer_partial = linux_xfer_partial;
4743 t->to_static_tracepoint_markers_by_strid
4744 = linux_child_static_tracepoint_markers_by_strid;
4750 struct target_ops *t;
4752 t = inf_ptrace_target ();
4753 linux_target_install_ops (t);
4759 linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
4761 struct target_ops *t;
4763 t = inf_ptrace_trad_target (register_u_offset);
4764 linux_target_install_ops (t);
4769 /* target_is_async_p implementation. */
4772 linux_nat_is_async_p (void)
4774 /* NOTE: palves 2008-03-21: We're only async when the user requests
4775 it explicitly with the "set target-async" command.
4776 Someday, linux will always be async. */
4777 return target_async_permitted;
4780 /* target_can_async_p implementation. */
4783 linux_nat_can_async_p (void)
4785 /* NOTE: palves 2008-03-21: We're only async when the user requests
4786 it explicitly with the "set target-async" command.
4787 Someday, linux will always be async. */
4788 return target_async_permitted;
4792 linux_nat_supports_non_stop (void)
4797 /* True if we want to support multi-process. To be removed when GDB
4798 supports multi-exec. */
4800 int linux_multi_process = 1;
4803 linux_nat_supports_multi_process (void)
4805 return linux_multi_process;
4809 linux_nat_supports_disable_randomization (void)
4811 #ifdef HAVE_PERSONALITY
4818 static int async_terminal_is_ours = 1;
4820 /* target_terminal_inferior implementation. */
4823 linux_nat_terminal_inferior (void)
4825 if (!target_is_async_p ())
4827 /* Async mode is disabled. */
4828 terminal_inferior ();
4832 terminal_inferior ();
4834 /* Calls to target_terminal_*() are meant to be idempotent. */
4835 if (!async_terminal_is_ours)
4838 delete_file_handler (input_fd);
4839 async_terminal_is_ours = 0;
4843 /* target_terminal_ours implementation. */
4846 linux_nat_terminal_ours (void)
4848 if (!target_is_async_p ())
4850 /* Async mode is disabled. */
4855 /* GDB should never give the terminal to the inferior if the
4856 inferior is running in the background (run&, continue&, etc.),
4857 but claiming it sure should. */
4860 if (async_terminal_is_ours)
4863 clear_sigint_trap ();
4864 add_file_handler (input_fd, stdin_event_handler, 0);
4865 async_terminal_is_ours = 1;
4868 static void (*async_client_callback) (enum inferior_event_type event_type,
4870 static void *async_client_context;
4872 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4873 so we notice when any child changes state, and notify the
4874 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4875 above to wait for the arrival of a SIGCHLD. */
4878 sigchld_handler (int signo)
4880 int old_errno = errno;
4882 if (debug_linux_nat)
4883 ui_file_write_async_safe (gdb_stdlog,
4884 "sigchld\n", sizeof ("sigchld\n") - 1);
4886 if (signo == SIGCHLD
4887 && linux_nat_event_pipe[0] != -1)
4888 async_file_mark (); /* Let the event loop know that there are
4889 events to handle. */
4894 /* Callback registered with the target events file descriptor. */
4897 handle_target_event (int error, gdb_client_data client_data)
4899 (*async_client_callback) (INF_REG_EVENT, async_client_context);
4902 /* Create/destroy the target events pipe. Returns previous state. */
4905 linux_async_pipe (int enable)
4907 int previous = (linux_nat_event_pipe[0] != -1);
4909 if (previous != enable)
4913 block_child_signals (&prev_mask);
4917 if (pipe (linux_nat_event_pipe) == -1)
4918 internal_error (__FILE__, __LINE__,
4919 "creating event pipe failed.");
4921 fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
4922 fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
4926 close (linux_nat_event_pipe[0]);
4927 close (linux_nat_event_pipe[1]);
4928 linux_nat_event_pipe[0] = -1;
4929 linux_nat_event_pipe[1] = -1;
4932 restore_child_signals_mask (&prev_mask);
4938 /* target_async implementation. */
4941 linux_nat_async (void (*callback) (enum inferior_event_type event_type,
4942 void *context), void *context)
4944 if (callback != NULL)
4946 async_client_callback = callback;
4947 async_client_context = context;
4948 if (!linux_async_pipe (1))
4950 add_file_handler (linux_nat_event_pipe[0],
4951 handle_target_event, NULL);
4952 /* There may be pending events to handle. Tell the event loop
4959 async_client_callback = callback;
4960 async_client_context = context;
4961 delete_file_handler (linux_nat_event_pipe[0]);
4962 linux_async_pipe (0);
4967 /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
4971 linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
4975 ptid_t ptid = lwp->ptid;
4977 if (debug_linux_nat)
4978 fprintf_unfiltered (gdb_stdlog,
4979 "LNSL: running -> suspending %s\n",
4980 target_pid_to_str (lwp->ptid));
4983 if (lwp->last_resume_kind == resume_stop)
4985 if (debug_linux_nat)
4986 fprintf_unfiltered (gdb_stdlog,
4987 "linux-nat: already stopping LWP %ld at "
4989 ptid_get_lwp (lwp->ptid));
4993 stop_callback (lwp, NULL);
4994 lwp->last_resume_kind = resume_stop;
4998 /* Already known to be stopped; do nothing. */
5000 if (debug_linux_nat)
5002 if (find_thread_ptid (lwp->ptid)->stop_requested)
5003 fprintf_unfiltered (gdb_stdlog,
5004 "LNSL: already stopped/stop_requested %s\n",
5005 target_pid_to_str (lwp->ptid));
5007 fprintf_unfiltered (gdb_stdlog,
5008 "LNSL: already stopped/no "
5009 "stop_requested yet %s\n",
5010 target_pid_to_str (lwp->ptid));
5017 linux_nat_stop (ptid_t ptid)
5020 iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
5022 linux_ops->to_stop (ptid);
5026 linux_nat_close (int quitting)
5028 /* Unregister from the event loop. */
5029 if (linux_nat_is_async_p ())
5030 linux_nat_async (NULL, 0);
5032 if (linux_ops->to_close)
5033 linux_ops->to_close (quitting);
5036 /* When requests are passed down from the linux-nat layer to the
5037 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
5038 used. The address space pointer is stored in the inferior object,
5039 but the common code that is passed such ptid can't tell whether
5040 lwpid is a "main" process id or not (it assumes so). We reverse
5041 look up the "main" process id from the lwp here. */
5043 static struct address_space *
5044 linux_nat_thread_address_space (struct target_ops *t, ptid_t ptid)
5046 struct lwp_info *lwp;
5047 struct inferior *inf;
5050 pid = GET_LWP (ptid);
5051 if (GET_LWP (ptid) == 0)
5053 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
5055 lwp = find_lwp_pid (ptid);
5056 pid = GET_PID (lwp->ptid);
5060 /* A (pid,lwpid,0) ptid. */
5061 pid = GET_PID (ptid);
5064 inf = find_inferior_pid (pid);
5065 gdb_assert (inf != NULL);
5069 /* Return the cached value of the processor core for thread PTID. */
5072 linux_nat_core_of_thread (struct target_ops *ops, ptid_t ptid)
5074 struct lwp_info *info = find_lwp_pid (ptid);
5082 linux_nat_add_target (struct target_ops *t)
5084 /* Save the provided single-threaded target. We save this in a separate
5085 variable because another target we've inherited from (e.g. inf-ptrace)
5086 may have saved a pointer to T; we want to use it for the final
5087 process stratum target. */
5088 linux_ops_saved = *t;
5089 linux_ops = &linux_ops_saved;
5091 /* Override some methods for multithreading. */
5092 t->to_create_inferior = linux_nat_create_inferior;
5093 t->to_attach = linux_nat_attach;
5094 t->to_detach = linux_nat_detach;
5095 t->to_resume = linux_nat_resume;
5096 t->to_wait = linux_nat_wait;
5097 t->to_pass_signals = linux_nat_pass_signals;
5098 t->to_xfer_partial = linux_nat_xfer_partial;
5099 t->to_kill = linux_nat_kill;
5100 t->to_mourn_inferior = linux_nat_mourn_inferior;
5101 t->to_thread_alive = linux_nat_thread_alive;
5102 t->to_pid_to_str = linux_nat_pid_to_str;
5103 t->to_thread_name = linux_nat_thread_name;
5104 t->to_has_thread_control = tc_schedlock;
5105 t->to_thread_address_space = linux_nat_thread_address_space;
5106 t->to_stopped_by_watchpoint = linux_nat_stopped_by_watchpoint;
5107 t->to_stopped_data_address = linux_nat_stopped_data_address;
5109 t->to_can_async_p = linux_nat_can_async_p;
5110 t->to_is_async_p = linux_nat_is_async_p;
5111 t->to_supports_non_stop = linux_nat_supports_non_stop;
5112 t->to_async = linux_nat_async;
5113 t->to_terminal_inferior = linux_nat_terminal_inferior;
5114 t->to_terminal_ours = linux_nat_terminal_ours;
5115 t->to_close = linux_nat_close;
5117 /* Methods for non-stop support. */
5118 t->to_stop = linux_nat_stop;
5120 t->to_supports_multi_process = linux_nat_supports_multi_process;
5122 t->to_supports_disable_randomization
5123 = linux_nat_supports_disable_randomization;
5125 t->to_core_of_thread = linux_nat_core_of_thread;
5127 /* We don't change the stratum; this target will sit at
5128 process_stratum and thread_db will set at thread_stratum. This
5129 is a little strange, since this is a multi-threaded-capable
5130 target, but we want to be on the stack below thread_db, and we
5131 also want to be used for single-threaded processes. */
5136 /* Register a method to call whenever a new thread is attached. */
5138 linux_nat_set_new_thread (struct target_ops *t,
5139 void (*new_thread) (struct lwp_info *))
5141 /* Save the pointer. We only support a single registered instance
5142 of the GNU/Linux native target, so we do not need to map this to
5144 linux_nat_new_thread = new_thread;
5147 /* Register a method that converts a siginfo object between the layout
5148 that ptrace returns, and the layout in the architecture of the
5151 linux_nat_set_siginfo_fixup (struct target_ops *t,
5152 int (*siginfo_fixup) (siginfo_t *,
5156 /* Save the pointer. */
5157 linux_nat_siginfo_fixup = siginfo_fixup;
5160 /* Register a method to call prior to resuming a thread. */
5163 linux_nat_set_prepare_to_resume (struct target_ops *t,
5164 void (*prepare_to_resume) (struct lwp_info *))
5166 /* Save the pointer. */
5167 linux_nat_prepare_to_resume = prepare_to_resume;
5170 /* See linux-nat.h. */
5173 linux_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo)
5177 pid = GET_LWP (ptid);
5179 pid = GET_PID (ptid);
5182 ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, siginfo);
5185 memset (siginfo, 0, sizeof (*siginfo));
5191 /* Provide a prototype to silence -Wmissing-prototypes. */
5192 extern initialize_file_ftype _initialize_linux_nat;
5195 _initialize_linux_nat (void)
5197 add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance,
5198 &debug_linux_nat, _("\
5199 Set debugging of GNU/Linux lwp module."), _("\
5200 Show debugging of GNU/Linux lwp module."), _("\
5201 Enables printf debugging output."),
5203 show_debug_linux_nat,
5204 &setdebuglist, &showdebuglist);
5206 /* Save this mask as the default. */
5207 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
5209 /* Install a SIGCHLD handler. */
5210 sigchld_action.sa_handler = sigchld_handler;
5211 sigemptyset (&sigchld_action.sa_mask);
5212 sigchld_action.sa_flags = SA_RESTART;
5214 /* Make it the default. */
5215 sigaction (SIGCHLD, &sigchld_action, NULL);
5217 /* Make sure we don't block SIGCHLD during a sigsuspend. */
5218 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
5219 sigdelset (&suspend_mask, SIGCHLD);
5221 sigemptyset (&blocked_mask);
5225 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
5226 the GNU/Linux Threads library and therefore doesn't really belong
5229 /* Read variable NAME in the target and return its value if found.
5230 Otherwise return zero. It is assumed that the type of the variable
5234 get_signo (const char *name)
5236 struct minimal_symbol *ms;
5239 ms = lookup_minimal_symbol (name, NULL, NULL);
5243 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (gdb_byte *) &signo,
5244 sizeof (signo)) != 0)
5250 /* Return the set of signals used by the threads library in *SET. */
5253 lin_thread_get_thread_signals (sigset_t *set)
5255 struct sigaction action;
5256 int restart, cancel;
5258 sigemptyset (&blocked_mask);
5261 restart = get_signo ("__pthread_sig_restart");
5262 cancel = get_signo ("__pthread_sig_cancel");
5264 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5265 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5266 not provide any way for the debugger to query the signal numbers -
5267 fortunately they don't change! */
5270 restart = __SIGRTMIN;
5273 cancel = __SIGRTMIN + 1;
5275 sigaddset (set, restart);
5276 sigaddset (set, cancel);
5278 /* The GNU/Linux Threads library makes terminating threads send a
5279 special "cancel" signal instead of SIGCHLD. Make sure we catch
5280 those (to prevent them from terminating GDB itself, which is
5281 likely to be their default action) and treat them the same way as
5284 action.sa_handler = sigchld_handler;
5285 sigemptyset (&action.sa_mask);
5286 action.sa_flags = SA_RESTART;
5287 sigaction (cancel, &action, NULL);
5289 /* We block the "cancel" signal throughout this code ... */
5290 sigaddset (&blocked_mask, cancel);
5291 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
5293 /* ... except during a sigsuspend. */
5294 sigdelset (&suspend_mask, cancel);