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-ptrace.h"
41 #include <sys/param.h> /* for MAXPATHLEN */
42 #include <sys/procfs.h> /* for elf_gregset etc. */
43 #include "elf-bfd.h" /* for elfcore_write_* */
44 #include "gregset.h" /* for gregset */
45 #include "gdbcore.h" /* for get_exec_file */
46 #include <ctype.h> /* for isdigit */
47 #include "gdbthread.h" /* for struct thread_info etc. */
48 #include "gdb_stat.h" /* for struct stat */
49 #include <fcntl.h> /* for O_RDONLY */
51 #include "event-loop.h"
52 #include "event-top.h"
54 #include <sys/types.h>
55 #include "gdb_dirent.h"
56 #include "xml-support.h"
60 #include "linux-osdata.h"
61 #include "linux-tdep.h"
64 #include "tracepoint.h"
67 #define SPUFS_MAGIC 0x23c9b64e
70 #ifdef HAVE_PERSONALITY
71 # include <sys/personality.h>
72 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
73 # define ADDR_NO_RANDOMIZE 0x0040000
75 #endif /* HAVE_PERSONALITY */
77 /* This comment documents high-level logic of this file.
79 Waiting for events in sync mode
80 ===============================
82 When waiting for an event in a specific thread, we just use waitpid, passing
83 the specific pid, and not passing WNOHANG.
85 When waiting for an event in all threads, waitpid is not quite good. Prior to
86 version 2.4, Linux can either wait for event in main thread, or in secondary
87 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
88 miss an event. The solution is to use non-blocking waitpid, together with
89 sigsuspend. First, we use non-blocking waitpid to get an event in the main
90 process, if any. Second, we use non-blocking waitpid with the __WCLONED
91 flag to check for events in cloned processes. If nothing is found, we use
92 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
93 happened to a child process -- and SIGCHLD will be delivered both for events
94 in main debugged process and in cloned processes. As soon as we know there's
95 an event, we get back to calling nonblocking waitpid with and without
98 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
99 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
100 blocked, the signal becomes pending and sigsuspend immediately
101 notices it and returns.
103 Waiting for events in async mode
104 ================================
106 In async mode, GDB should always be ready to handle both user input
107 and target events, so neither blocking waitpid nor sigsuspend are
108 viable options. Instead, we should asynchronously notify the GDB main
109 event loop whenever there's an unprocessed event from the target. We
110 detect asynchronous target events by handling SIGCHLD signals. To
111 notify the event loop about target events, the self-pipe trick is used
112 --- a pipe is registered as waitable event source in the event loop,
113 the event loop select/poll's on the read end of this pipe (as well on
114 other event sources, e.g., stdin), and the SIGCHLD handler writes a
115 byte to this pipe. This is more portable than relying on
116 pselect/ppoll, since on kernels that lack those syscalls, libc
117 emulates them with select/poll+sigprocmask, and that is racy
118 (a.k.a. plain broken).
120 Obviously, if we fail to notify the event loop if there's a target
121 event, it's bad. OTOH, if we notify the event loop when there's no
122 event from the target, linux_nat_wait will detect that there's no real
123 event to report, and return event of type TARGET_WAITKIND_IGNORE.
124 This is mostly harmless, but it will waste time and is better avoided.
126 The main design point is that every time GDB is outside linux-nat.c,
127 we have a SIGCHLD handler installed that is called when something
128 happens to the target and notifies the GDB event loop. Whenever GDB
129 core decides to handle the event, and calls into linux-nat.c, we
130 process things as in sync mode, except that the we never block in
133 While processing an event, we may end up momentarily blocked in
134 waitpid calls. Those waitpid calls, while blocking, are guarantied to
135 return quickly. E.g., in all-stop mode, before reporting to the core
136 that an LWP hit a breakpoint, all LWPs are stopped by sending them
137 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
138 Note that this is different from blocking indefinitely waiting for the
139 next event --- here, we're already handling an event.
144 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
145 signal is not entirely significant; we just need for a signal to be delivered,
146 so that we can intercept it. SIGSTOP's advantage is that it can not be
147 blocked. A disadvantage is that it is not a real-time signal, so it can only
148 be queued once; we do not keep track of other sources of SIGSTOP.
150 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
151 use them, because they have special behavior when the signal is generated -
152 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
153 kills the entire thread group.
155 A delivered SIGSTOP would stop the entire thread group, not just the thread we
156 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
157 cancel it (by PTRACE_CONT without passing SIGSTOP).
159 We could use a real-time signal instead. This would solve those problems; we
160 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
161 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
162 generates it, and there are races with trying to find a signal that is not
166 #define O_LARGEFILE 0
169 /* Unlike other extended result codes, WSTOPSIG (status) on
170 PTRACE_O_TRACESYSGOOD syscall events doesn't return SIGTRAP, but
171 instead SIGTRAP with bit 7 set. */
172 #define SYSCALL_SIGTRAP (SIGTRAP | 0x80)
174 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
175 the use of the multi-threaded target. */
176 static struct target_ops *linux_ops;
177 static struct target_ops linux_ops_saved;
179 /* The method to call, if any, when a new thread is attached. */
180 static void (*linux_nat_new_thread) (struct lwp_info *);
182 /* Hook to call prior to resuming a thread. */
183 static void (*linux_nat_prepare_to_resume) (struct lwp_info *);
185 /* The method to call, if any, when the siginfo object needs to be
186 converted between the layout returned by ptrace, and the layout in
187 the architecture of the inferior. */
188 static int (*linux_nat_siginfo_fixup) (struct siginfo *,
192 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
193 Called by our to_xfer_partial. */
194 static LONGEST (*super_xfer_partial) (struct target_ops *,
196 const char *, gdb_byte *,
200 static int debug_linux_nat;
202 show_debug_linux_nat (struct ui_file *file, int from_tty,
203 struct cmd_list_element *c, const char *value)
205 fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
209 struct simple_pid_list
213 struct simple_pid_list *next;
215 struct simple_pid_list *stopped_pids;
217 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
218 can not be used, 1 if it can. */
220 static int linux_supports_tracefork_flag = -1;
222 /* This variable is a tri-state flag: -1 for unknown, 0 if
223 PTRACE_O_TRACESYSGOOD can not be used, 1 if it can. */
225 static int linux_supports_tracesysgood_flag = -1;
227 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
228 PTRACE_O_TRACEVFORKDONE. */
230 static int linux_supports_tracevforkdone_flag = -1;
232 /* Stores the current used ptrace() options. */
233 static int current_ptrace_options = 0;
235 /* Async mode support. */
237 /* The read/write ends of the pipe registered as waitable file in the
239 static int linux_nat_event_pipe[2] = { -1, -1 };
241 /* Flush the event pipe. */
244 async_file_flush (void)
251 ret = read (linux_nat_event_pipe[0], &buf, 1);
253 while (ret >= 0 || (ret == -1 && errno == EINTR));
256 /* Put something (anything, doesn't matter what, or how much) in event
257 pipe, so that the select/poll in the event-loop realizes we have
258 something to process. */
261 async_file_mark (void)
265 /* It doesn't really matter what the pipe contains, as long we end
266 up with something in it. Might as well flush the previous
272 ret = write (linux_nat_event_pipe[1], "+", 1);
274 while (ret == -1 && errno == EINTR);
276 /* Ignore EAGAIN. If the pipe is full, the event loop will already
277 be awakened anyway. */
280 static void linux_nat_async (void (*callback)
281 (enum inferior_event_type event_type,
284 static int kill_lwp (int lwpid, int signo);
286 static int stop_callback (struct lwp_info *lp, void *data);
288 static void block_child_signals (sigset_t *prev_mask);
289 static void restore_child_signals_mask (sigset_t *prev_mask);
292 static struct lwp_info *add_lwp (ptid_t ptid);
293 static void purge_lwp_list (int pid);
294 static void delete_lwp (ptid_t ptid);
295 static struct lwp_info *find_lwp_pid (ptid_t ptid);
298 /* Trivial list manipulation functions to keep track of a list of
299 new stopped processes. */
301 add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
303 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
306 new_pid->status = status;
307 new_pid->next = *listp;
312 in_pid_list_p (struct simple_pid_list *list, int pid)
314 struct simple_pid_list *p;
316 for (p = list; p != NULL; p = p->next)
323 pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
325 struct simple_pid_list **p;
327 for (p = listp; *p != NULL; p = &(*p)->next)
328 if ((*p)->pid == pid)
330 struct simple_pid_list *next = (*p)->next;
332 *statusp = (*p)->status;
341 /* A helper function for linux_test_for_tracefork, called after fork (). */
344 linux_tracefork_child (void)
346 ptrace (PTRACE_TRACEME, 0, 0, 0);
347 kill (getpid (), SIGSTOP);
352 /* Wrapper function for waitpid which handles EINTR. */
355 my_waitpid (int pid, int *statusp, int flags)
361 ret = waitpid (pid, statusp, flags);
363 while (ret == -1 && errno == EINTR);
368 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
370 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
371 we know that the feature is not available. This may change the tracing
372 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
374 However, if it succeeds, we don't know for sure that the feature is
375 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
376 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
377 fork tracing, and let it fork. If the process exits, we assume that we
378 can't use TRACEFORK; if we get the fork notification, and we can extract
379 the new child's PID, then we assume that we can. */
382 linux_test_for_tracefork (int original_pid)
384 int child_pid, ret, status;
388 /* We don't want those ptrace calls to be interrupted. */
389 block_child_signals (&prev_mask);
391 linux_supports_tracefork_flag = 0;
392 linux_supports_tracevforkdone_flag = 0;
394 ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK);
397 restore_child_signals_mask (&prev_mask);
403 perror_with_name (("fork"));
406 linux_tracefork_child ();
408 ret = my_waitpid (child_pid, &status, 0);
410 perror_with_name (("waitpid"));
411 else if (ret != child_pid)
412 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret);
413 if (! WIFSTOPPED (status))
414 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."),
417 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
420 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
423 warning (_("linux_test_for_tracefork: failed to kill child"));
424 restore_child_signals_mask (&prev_mask);
428 ret = my_waitpid (child_pid, &status, 0);
429 if (ret != child_pid)
430 warning (_("linux_test_for_tracefork: failed "
431 "to wait for killed child"));
432 else if (!WIFSIGNALED (status))
433 warning (_("linux_test_for_tracefork: unexpected "
434 "wait status 0x%x from killed child"), status);
436 restore_child_signals_mask (&prev_mask);
440 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
441 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
442 PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
443 linux_supports_tracevforkdone_flag = (ret == 0);
445 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
447 warning (_("linux_test_for_tracefork: failed to resume child"));
449 ret = my_waitpid (child_pid, &status, 0);
451 if (ret == child_pid && WIFSTOPPED (status)
452 && status >> 16 == PTRACE_EVENT_FORK)
455 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
456 if (ret == 0 && second_pid != 0)
460 linux_supports_tracefork_flag = 1;
461 my_waitpid (second_pid, &second_status, 0);
462 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
464 warning (_("linux_test_for_tracefork: "
465 "failed to kill second child"));
466 my_waitpid (second_pid, &status, 0);
470 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
471 "(%d, status 0x%x)"), ret, status);
473 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
475 warning (_("linux_test_for_tracefork: failed to kill child"));
476 my_waitpid (child_pid, &status, 0);
478 restore_child_signals_mask (&prev_mask);
481 /* Determine if PTRACE_O_TRACESYSGOOD can be used to follow syscalls.
483 We try to enable syscall tracing on ORIGINAL_PID. If this fails,
484 we know that the feature is not available. This may change the tracing
485 options for ORIGINAL_PID, but we'll be setting them shortly anyway. */
488 linux_test_for_tracesysgood (int original_pid)
493 /* We don't want those ptrace calls to be interrupted. */
494 block_child_signals (&prev_mask);
496 linux_supports_tracesysgood_flag = 0;
498 ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACESYSGOOD);
502 linux_supports_tracesysgood_flag = 1;
504 restore_child_signals_mask (&prev_mask);
507 /* Determine wether we support PTRACE_O_TRACESYSGOOD option available.
508 This function also sets linux_supports_tracesysgood_flag. */
511 linux_supports_tracesysgood (int pid)
513 if (linux_supports_tracesysgood_flag == -1)
514 linux_test_for_tracesysgood (pid);
515 return linux_supports_tracesysgood_flag;
518 /* Return non-zero iff we have tracefork functionality available.
519 This function also sets linux_supports_tracefork_flag. */
522 linux_supports_tracefork (int pid)
524 if (linux_supports_tracefork_flag == -1)
525 linux_test_for_tracefork (pid);
526 return linux_supports_tracefork_flag;
530 linux_supports_tracevforkdone (int pid)
532 if (linux_supports_tracefork_flag == -1)
533 linux_test_for_tracefork (pid);
534 return linux_supports_tracevforkdone_flag;
538 linux_enable_tracesysgood (ptid_t ptid)
540 int pid = ptid_get_lwp (ptid);
543 pid = ptid_get_pid (ptid);
545 if (linux_supports_tracesysgood (pid) == 0)
548 current_ptrace_options |= PTRACE_O_TRACESYSGOOD;
550 ptrace (PTRACE_SETOPTIONS, pid, 0, current_ptrace_options);
555 linux_enable_event_reporting (ptid_t ptid)
557 int pid = ptid_get_lwp (ptid);
560 pid = ptid_get_pid (ptid);
562 if (! linux_supports_tracefork (pid))
565 current_ptrace_options |= PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK
566 | PTRACE_O_TRACEEXEC | PTRACE_O_TRACECLONE;
568 if (linux_supports_tracevforkdone (pid))
569 current_ptrace_options |= PTRACE_O_TRACEVFORKDONE;
571 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
572 read-only process state. */
574 ptrace (PTRACE_SETOPTIONS, pid, 0, current_ptrace_options);
578 linux_child_post_attach (int pid)
580 linux_enable_event_reporting (pid_to_ptid (pid));
581 linux_enable_tracesysgood (pid_to_ptid (pid));
585 linux_child_post_startup_inferior (ptid_t ptid)
587 linux_enable_event_reporting (ptid);
588 linux_enable_tracesysgood (ptid);
591 /* Return the number of known LWPs in the tgid given by PID. */
599 for (lp = lwp_list; lp; lp = lp->next)
600 if (ptid_get_pid (lp->ptid) == pid)
606 /* Call delete_lwp with prototype compatible for make_cleanup. */
609 delete_lwp_cleanup (void *lp_voidp)
611 struct lwp_info *lp = lp_voidp;
613 delete_lwp (lp->ptid);
617 linux_child_follow_fork (struct target_ops *ops, int follow_child)
621 int parent_pid, child_pid;
623 block_child_signals (&prev_mask);
625 has_vforked = (inferior_thread ()->pending_follow.kind
626 == TARGET_WAITKIND_VFORKED);
627 parent_pid = ptid_get_lwp (inferior_ptid);
629 parent_pid = ptid_get_pid (inferior_ptid);
630 child_pid = PIDGET (inferior_thread ()->pending_follow.value.related_pid);
633 linux_enable_event_reporting (pid_to_ptid (child_pid));
636 && !non_stop /* Non-stop always resumes both branches. */
637 && (!target_is_async_p () || sync_execution)
638 && !(follow_child || detach_fork || sched_multi))
640 /* The parent stays blocked inside the vfork syscall until the
641 child execs or exits. If we don't let the child run, then
642 the parent stays blocked. If we're telling the parent to run
643 in the foreground, the user will not be able to ctrl-c to get
644 back the terminal, effectively hanging the debug session. */
645 fprintf_filtered (gdb_stderr, _("\
646 Can not resume the parent process over vfork in the foreground while\n\
647 holding the child stopped. Try \"set detach-on-fork\" or \
648 \"set schedule-multiple\".\n"));
649 /* FIXME output string > 80 columns. */
655 struct lwp_info *child_lp = NULL;
657 /* We're already attached to the parent, by default. */
659 /* Detach new forked process? */
662 struct cleanup *old_chain;
664 /* Before detaching from the child, remove all breakpoints
665 from it. If we forked, then this has already been taken
666 care of by infrun.c. If we vforked however, any
667 breakpoint inserted in the parent is visible in the
668 child, even those added while stopped in a vfork
669 catchpoint. This will remove the breakpoints from the
670 parent also, but they'll be reinserted below. */
673 /* keep breakpoints list in sync. */
674 remove_breakpoints_pid (GET_PID (inferior_ptid));
677 if (info_verbose || debug_linux_nat)
679 target_terminal_ours ();
680 fprintf_filtered (gdb_stdlog,
681 "Detaching after fork from "
682 "child process %d.\n",
686 old_chain = save_inferior_ptid ();
687 inferior_ptid = ptid_build (child_pid, child_pid, 0);
689 child_lp = add_lwp (inferior_ptid);
690 child_lp->stopped = 1;
691 child_lp->last_resume_kind = resume_stop;
692 make_cleanup (delete_lwp_cleanup, child_lp);
694 /* CHILD_LP has new PID, therefore linux_nat_new_thread is not called for it.
695 See i386_inferior_data_get for the Linux kernel specifics.
696 Ensure linux_nat_prepare_to_resume will reset the hardware debug
697 registers. It is done by the linux_nat_new_thread call, which is
698 being skipped in add_lwp above for the first lwp of a pid. */
699 gdb_assert (num_lwps (GET_PID (child_lp->ptid)) == 1);
700 if (linux_nat_new_thread != NULL)
701 linux_nat_new_thread (child_lp);
703 if (linux_nat_prepare_to_resume != NULL)
704 linux_nat_prepare_to_resume (child_lp);
705 ptrace (PTRACE_DETACH, child_pid, 0, 0);
707 do_cleanups (old_chain);
711 struct inferior *parent_inf, *child_inf;
712 struct cleanup *old_chain;
714 /* Add process to GDB's tables. */
715 child_inf = add_inferior (child_pid);
717 parent_inf = current_inferior ();
718 child_inf->attach_flag = parent_inf->attach_flag;
719 copy_terminal_info (child_inf, parent_inf);
721 old_chain = save_inferior_ptid ();
722 save_current_program_space ();
724 inferior_ptid = ptid_build (child_pid, child_pid, 0);
725 add_thread (inferior_ptid);
726 child_lp = add_lwp (inferior_ptid);
727 child_lp->stopped = 1;
728 child_lp->last_resume_kind = resume_stop;
729 child_inf->symfile_flags = SYMFILE_NO_READ;
731 /* If this is a vfork child, then the address-space is
732 shared with the parent. */
735 child_inf->pspace = parent_inf->pspace;
736 child_inf->aspace = parent_inf->aspace;
738 /* The parent will be frozen until the child is done
739 with the shared region. Keep track of the
741 child_inf->vfork_parent = parent_inf;
742 child_inf->pending_detach = 0;
743 parent_inf->vfork_child = child_inf;
744 parent_inf->pending_detach = 0;
748 child_inf->aspace = new_address_space ();
749 child_inf->pspace = add_program_space (child_inf->aspace);
750 child_inf->removable = 1;
751 set_current_program_space (child_inf->pspace);
752 clone_program_space (child_inf->pspace, parent_inf->pspace);
754 /* Let the shared library layer (solib-svr4) learn about
755 this new process, relocate the cloned exec, pull in
756 shared libraries, and install the solib event
757 breakpoint. If a "cloned-VM" event was propagated
758 better throughout the core, this wouldn't be
760 solib_create_inferior_hook (0);
763 /* Let the thread_db layer learn about this new process. */
764 check_for_thread_db ();
766 do_cleanups (old_chain);
771 struct lwp_info *parent_lp;
772 struct inferior *parent_inf;
774 parent_inf = current_inferior ();
776 /* If we detached from the child, then we have to be careful
777 to not insert breakpoints in the parent until the child
778 is done with the shared memory region. However, if we're
779 staying attached to the child, then we can and should
780 insert breakpoints, so that we can debug it. A
781 subsequent child exec or exit is enough to know when does
782 the child stops using the parent's address space. */
783 parent_inf->waiting_for_vfork_done = detach_fork;
784 parent_inf->pspace->breakpoints_not_allowed = detach_fork;
786 parent_lp = find_lwp_pid (pid_to_ptid (parent_pid));
787 gdb_assert (linux_supports_tracefork_flag >= 0);
789 if (linux_supports_tracevforkdone (0))
792 fprintf_unfiltered (gdb_stdlog,
793 "LCFF: waiting for VFORK_DONE on %d\n",
795 parent_lp->stopped = 1;
797 /* We'll handle the VFORK_DONE event like any other
798 event, in target_wait. */
802 /* We can't insert breakpoints until the child has
803 finished with the shared memory region. We need to
804 wait until that happens. Ideal would be to just
806 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
807 - waitpid (parent_pid, &status, __WALL);
808 However, most architectures can't handle a syscall
809 being traced on the way out if it wasn't traced on
812 We might also think to loop, continuing the child
813 until it exits or gets a SIGTRAP. One problem is
814 that the child might call ptrace with PTRACE_TRACEME.
816 There's no simple and reliable way to figure out when
817 the vforked child will be done with its copy of the
818 shared memory. We could step it out of the syscall,
819 two instructions, let it go, and then single-step the
820 parent once. When we have hardware single-step, this
821 would work; with software single-step it could still
822 be made to work but we'd have to be able to insert
823 single-step breakpoints in the child, and we'd have
824 to insert -just- the single-step breakpoint in the
825 parent. Very awkward.
827 In the end, the best we can do is to make sure it
828 runs for a little while. Hopefully it will be out of
829 range of any breakpoints we reinsert. Usually this
830 is only the single-step breakpoint at vfork's return
834 fprintf_unfiltered (gdb_stdlog,
835 "LCFF: no VFORK_DONE "
836 "support, sleeping a bit\n");
840 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
841 and leave it pending. The next linux_nat_resume call
842 will notice a pending event, and bypasses actually
843 resuming the inferior. */
844 parent_lp->status = 0;
845 parent_lp->waitstatus.kind = TARGET_WAITKIND_VFORK_DONE;
846 parent_lp->stopped = 1;
848 /* If we're in async mode, need to tell the event loop
849 there's something here to process. */
850 if (target_can_async_p ())
857 struct inferior *parent_inf, *child_inf;
858 struct lwp_info *child_lp;
859 struct program_space *parent_pspace;
861 if (info_verbose || debug_linux_nat)
863 target_terminal_ours ();
865 fprintf_filtered (gdb_stdlog,
866 _("Attaching after process %d "
867 "vfork to child process %d.\n"),
868 parent_pid, child_pid);
870 fprintf_filtered (gdb_stdlog,
871 _("Attaching after process %d "
872 "fork to child process %d.\n"),
873 parent_pid, child_pid);
876 /* Add the new inferior first, so that the target_detach below
877 doesn't unpush the target. */
879 child_inf = add_inferior (child_pid);
881 parent_inf = current_inferior ();
882 child_inf->attach_flag = parent_inf->attach_flag;
883 copy_terminal_info (child_inf, parent_inf);
885 parent_pspace = parent_inf->pspace;
887 /* If we're vforking, we want to hold on to the parent until the
888 child exits or execs. At child exec or exit time we can
889 remove the old breakpoints from the parent and detach or
890 resume debugging it. Otherwise, detach the parent now; we'll
891 want to reuse it's program/address spaces, but we can't set
892 them to the child before removing breakpoints from the
893 parent, otherwise, the breakpoints module could decide to
894 remove breakpoints from the wrong process (since they'd be
895 assigned to the same address space). */
899 gdb_assert (child_inf->vfork_parent == NULL);
900 gdb_assert (parent_inf->vfork_child == NULL);
901 child_inf->vfork_parent = parent_inf;
902 child_inf->pending_detach = 0;
903 parent_inf->vfork_child = child_inf;
904 parent_inf->pending_detach = detach_fork;
905 parent_inf->waiting_for_vfork_done = 0;
907 else if (detach_fork)
908 target_detach (NULL, 0);
910 /* Note that the detach above makes PARENT_INF dangling. */
912 /* Add the child thread to the appropriate lists, and switch to
913 this new thread, before cloning the program space, and
914 informing the solib layer about this new process. */
916 inferior_ptid = ptid_build (child_pid, child_pid, 0);
917 add_thread (inferior_ptid);
918 child_lp = add_lwp (inferior_ptid);
919 child_lp->stopped = 1;
920 child_lp->last_resume_kind = resume_stop;
922 /* If this is a vfork child, then the address-space is shared
923 with the parent. If we detached from the parent, then we can
924 reuse the parent's program/address spaces. */
925 if (has_vforked || detach_fork)
927 child_inf->pspace = parent_pspace;
928 child_inf->aspace = child_inf->pspace->aspace;
932 child_inf->aspace = new_address_space ();
933 child_inf->pspace = add_program_space (child_inf->aspace);
934 child_inf->removable = 1;
935 child_inf->symfile_flags = SYMFILE_NO_READ;
936 set_current_program_space (child_inf->pspace);
937 clone_program_space (child_inf->pspace, parent_pspace);
939 /* Let the shared library layer (solib-svr4) learn about
940 this new process, relocate the cloned exec, pull in
941 shared libraries, and install the solib event breakpoint.
942 If a "cloned-VM" event was propagated better throughout
943 the core, this wouldn't be required. */
944 solib_create_inferior_hook (0);
947 /* Let the thread_db layer learn about this new process. */
948 check_for_thread_db ();
951 restore_child_signals_mask (&prev_mask);
957 linux_child_insert_fork_catchpoint (int pid)
959 return !linux_supports_tracefork (pid);
963 linux_child_remove_fork_catchpoint (int pid)
969 linux_child_insert_vfork_catchpoint (int pid)
971 return !linux_supports_tracefork (pid);
975 linux_child_remove_vfork_catchpoint (int pid)
981 linux_child_insert_exec_catchpoint (int pid)
983 return !linux_supports_tracefork (pid);
987 linux_child_remove_exec_catchpoint (int pid)
993 linux_child_set_syscall_catchpoint (int pid, int needed, int any_count,
994 int table_size, int *table)
996 if (!linux_supports_tracesysgood (pid))
999 /* On GNU/Linux, we ignore the arguments. It means that we only
1000 enable the syscall catchpoints, but do not disable them.
1002 Also, we do not use the `table' information because we do not
1003 filter system calls here. We let GDB do the logic for us. */
1007 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
1008 are processes sharing the same VM space. A multi-threaded process
1009 is basically a group of such processes. However, such a grouping
1010 is almost entirely a user-space issue; the kernel doesn't enforce
1011 such a grouping at all (this might change in the future). In
1012 general, we'll rely on the threads library (i.e. the GNU/Linux
1013 Threads library) to provide such a grouping.
1015 It is perfectly well possible to write a multi-threaded application
1016 without the assistance of a threads library, by using the clone
1017 system call directly. This module should be able to give some
1018 rudimentary support for debugging such applications if developers
1019 specify the CLONE_PTRACE flag in the clone system call, and are
1020 using the Linux kernel 2.4 or above.
1022 Note that there are some peculiarities in GNU/Linux that affect
1025 - In general one should specify the __WCLONE flag to waitpid in
1026 order to make it report events for any of the cloned processes
1027 (and leave it out for the initial process). However, if a cloned
1028 process has exited the exit status is only reported if the
1029 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
1030 we cannot use it since GDB must work on older systems too.
1032 - When a traced, cloned process exits and is waited for by the
1033 debugger, the kernel reassigns it to the original parent and
1034 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
1035 library doesn't notice this, which leads to the "zombie problem":
1036 When debugged a multi-threaded process that spawns a lot of
1037 threads will run out of processes, even if the threads exit,
1038 because the "zombies" stay around. */
1040 /* List of known LWPs. */
1041 struct lwp_info *lwp_list;
1044 /* Original signal mask. */
1045 static sigset_t normal_mask;
1047 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
1048 _initialize_linux_nat. */
1049 static sigset_t suspend_mask;
1051 /* Signals to block to make that sigsuspend work. */
1052 static sigset_t blocked_mask;
1054 /* SIGCHLD action. */
1055 struct sigaction sigchld_action;
1057 /* Block child signals (SIGCHLD and linux threads signals), and store
1058 the previous mask in PREV_MASK. */
1061 block_child_signals (sigset_t *prev_mask)
1063 /* Make sure SIGCHLD is blocked. */
1064 if (!sigismember (&blocked_mask, SIGCHLD))
1065 sigaddset (&blocked_mask, SIGCHLD);
1067 sigprocmask (SIG_BLOCK, &blocked_mask, prev_mask);
1070 /* Restore child signals mask, previously returned by
1071 block_child_signals. */
1074 restore_child_signals_mask (sigset_t *prev_mask)
1076 sigprocmask (SIG_SETMASK, prev_mask, NULL);
1079 /* Mask of signals to pass directly to the inferior. */
1080 static sigset_t pass_mask;
1082 /* Update signals to pass to the inferior. */
1084 linux_nat_pass_signals (int numsigs, unsigned char *pass_signals)
1088 sigemptyset (&pass_mask);
1090 for (signo = 1; signo < NSIG; signo++)
1092 int target_signo = target_signal_from_host (signo);
1093 if (target_signo < numsigs && pass_signals[target_signo])
1094 sigaddset (&pass_mask, signo);
1100 /* Prototypes for local functions. */
1101 static int stop_wait_callback (struct lwp_info *lp, void *data);
1102 static int linux_thread_alive (ptid_t ptid);
1103 static char *linux_child_pid_to_exec_file (int pid);
1106 /* Convert wait status STATUS to a string. Used for printing debug
1110 status_to_str (int status)
1112 static char buf[64];
1114 if (WIFSTOPPED (status))
1116 if (WSTOPSIG (status) == SYSCALL_SIGTRAP)
1117 snprintf (buf, sizeof (buf), "%s (stopped at syscall)",
1118 strsignal (SIGTRAP));
1120 snprintf (buf, sizeof (buf), "%s (stopped)",
1121 strsignal (WSTOPSIG (status)));
1123 else if (WIFSIGNALED (status))
1124 snprintf (buf, sizeof (buf), "%s (terminated)",
1125 strsignal (WTERMSIG (status)));
1127 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
1132 /* Destroy and free LP. */
1135 lwp_free (struct lwp_info *lp)
1137 xfree (lp->arch_private);
1141 /* Remove all LWPs belong to PID from the lwp list. */
1144 purge_lwp_list (int pid)
1146 struct lwp_info *lp, *lpprev, *lpnext;
1150 for (lp = lwp_list; lp; lp = lpnext)
1154 if (ptid_get_pid (lp->ptid) == pid)
1157 lwp_list = lp->next;
1159 lpprev->next = lp->next;
1168 /* Add the LWP specified by PID to the list. Return a pointer to the
1169 structure describing the new LWP. The LWP should already be stopped
1170 (with an exception for the very first LWP). */
1172 static struct lwp_info *
1173 add_lwp (ptid_t ptid)
1175 struct lwp_info *lp;
1177 gdb_assert (is_lwp (ptid));
1179 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
1181 memset (lp, 0, sizeof (struct lwp_info));
1183 lp->last_resume_kind = resume_continue;
1184 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1189 lp->next = lwp_list;
1192 /* Let the arch specific bits know about this new thread. Current
1193 clients of this callback take the opportunity to install
1194 watchpoints in the new thread. Don't do this for the first
1195 thread though. If we're spawning a child ("run"), the thread
1196 executes the shell wrapper first, and we shouldn't touch it until
1197 it execs the program we want to debug. For "attach", it'd be
1198 okay to call the callback, but it's not necessary, because
1199 watchpoints can't yet have been inserted into the inferior. */
1200 if (num_lwps (GET_PID (ptid)) > 1 && linux_nat_new_thread != NULL)
1201 linux_nat_new_thread (lp);
1206 /* Remove the LWP specified by PID from the list. */
1209 delete_lwp (ptid_t ptid)
1211 struct lwp_info *lp, *lpprev;
1215 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
1216 if (ptid_equal (lp->ptid, ptid))
1223 lpprev->next = lp->next;
1225 lwp_list = lp->next;
1230 /* Return a pointer to the structure describing the LWP corresponding
1231 to PID. If no corresponding LWP could be found, return NULL. */
1233 static struct lwp_info *
1234 find_lwp_pid (ptid_t ptid)
1236 struct lwp_info *lp;
1240 lwp = GET_LWP (ptid);
1242 lwp = GET_PID (ptid);
1244 for (lp = lwp_list; lp; lp = lp->next)
1245 if (lwp == GET_LWP (lp->ptid))
1251 /* Call CALLBACK with its second argument set to DATA for every LWP in
1252 the list. If CALLBACK returns 1 for a particular LWP, return a
1253 pointer to the structure describing that LWP immediately.
1254 Otherwise return NULL. */
1257 iterate_over_lwps (ptid_t filter,
1258 int (*callback) (struct lwp_info *, void *),
1261 struct lwp_info *lp, *lpnext;
1263 for (lp = lwp_list; lp; lp = lpnext)
1267 if (ptid_match (lp->ptid, filter))
1269 if ((*callback) (lp, data))
1277 /* Iterate like iterate_over_lwps does except when forking-off a child call
1278 CALLBACK with CALLBACK_DATA specifically only for that new child PID. */
1281 linux_nat_iterate_watchpoint_lwps
1282 (linux_nat_iterate_watchpoint_lwps_ftype callback, void *callback_data)
1284 int inferior_pid = ptid_get_pid (inferior_ptid);
1285 struct inferior *inf = current_inferior ();
1287 if (inf->pid == inferior_pid)
1289 /* Iterate all the threads of the current inferior. Without specifying
1290 INFERIOR_PID it would iterate all threads of all inferiors, which is
1291 inappropriate for watchpoints. */
1293 iterate_over_lwps (pid_to_ptid (inferior_pid), callback, callback_data);
1297 /* Detaching a new child PID temporarily present in INFERIOR_PID. */
1299 struct lwp_info *child_lp;
1300 struct cleanup *old_chain;
1301 pid_t child_pid = GET_PID (inferior_ptid);
1302 ptid_t child_ptid = ptid_build (child_pid, child_pid, 0);
1304 gdb_assert (!is_lwp (inferior_ptid));
1305 gdb_assert (find_lwp_pid (child_ptid) == NULL);
1306 child_lp = add_lwp (child_ptid);
1307 child_lp->stopped = 1;
1308 child_lp->last_resume_kind = resume_stop;
1309 old_chain = make_cleanup (delete_lwp_cleanup, child_lp);
1311 callback (child_lp, callback_data);
1313 do_cleanups (old_chain);
1317 /* Update our internal state when changing from one checkpoint to
1318 another indicated by NEW_PTID. We can only switch single-threaded
1319 applications, so we only create one new LWP, and the previous list
1323 linux_nat_switch_fork (ptid_t new_ptid)
1325 struct lwp_info *lp;
1327 purge_lwp_list (GET_PID (inferior_ptid));
1329 lp = add_lwp (new_ptid);
1332 /* This changes the thread's ptid while preserving the gdb thread
1333 num. Also changes the inferior pid, while preserving the
1335 thread_change_ptid (inferior_ptid, new_ptid);
1337 /* We've just told GDB core that the thread changed target id, but,
1338 in fact, it really is a different thread, with different register
1340 registers_changed ();
1343 /* Handle the exit of a single thread LP. */
1346 exit_lwp (struct lwp_info *lp)
1348 struct thread_info *th = find_thread_ptid (lp->ptid);
1352 if (print_thread_events)
1353 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid));
1355 delete_thread (lp->ptid);
1358 delete_lwp (lp->ptid);
1361 /* Wait for the LWP specified by LP, which we have just attached to.
1362 Returns a wait status for that LWP, to cache. */
1365 linux_nat_post_attach_wait (ptid_t ptid, int first, int *cloned,
1368 pid_t new_pid, pid = GET_LWP (ptid);
1371 if (linux_proc_pid_is_stopped (pid))
1373 if (debug_linux_nat)
1374 fprintf_unfiltered (gdb_stdlog,
1375 "LNPAW: Attaching to a stopped process\n");
1377 /* The process is definitely stopped. It is in a job control
1378 stop, unless the kernel predates the TASK_STOPPED /
1379 TASK_TRACED distinction, in which case it might be in a
1380 ptrace stop. Make sure it is in a ptrace stop; from there we
1381 can kill it, signal it, et cetera.
1383 First make sure there is a pending SIGSTOP. Since we are
1384 already attached, the process can not transition from stopped
1385 to running without a PTRACE_CONT; so we know this signal will
1386 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1387 probably already in the queue (unless this kernel is old
1388 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1389 is not an RT signal, it can only be queued once. */
1390 kill_lwp (pid, SIGSTOP);
1392 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1393 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1394 ptrace (PTRACE_CONT, pid, 0, 0);
1397 /* Make sure the initial process is stopped. The user-level threads
1398 layer might want to poke around in the inferior, and that won't
1399 work if things haven't stabilized yet. */
1400 new_pid = my_waitpid (pid, &status, 0);
1401 if (new_pid == -1 && errno == ECHILD)
1404 warning (_("%s is a cloned process"), target_pid_to_str (ptid));
1406 /* Try again with __WCLONE to check cloned processes. */
1407 new_pid = my_waitpid (pid, &status, __WCLONE);
1411 gdb_assert (pid == new_pid);
1413 if (!WIFSTOPPED (status))
1415 /* The pid we tried to attach has apparently just exited. */
1416 if (debug_linux_nat)
1417 fprintf_unfiltered (gdb_stdlog, "LNPAW: Failed to stop %d: %s",
1418 pid, status_to_str (status));
1422 if (WSTOPSIG (status) != SIGSTOP)
1425 if (debug_linux_nat)
1426 fprintf_unfiltered (gdb_stdlog,
1427 "LNPAW: Received %s after attaching\n",
1428 status_to_str (status));
1434 /* Attach to the LWP specified by PID. Return 0 if successful, -1 if
1435 the new LWP could not be attached, or 1 if we're already auto
1436 attached to this thread, but haven't processed the
1437 PTRACE_EVENT_CLONE event of its parent thread, so we just ignore
1438 its existance, without considering it an error. */
1441 lin_lwp_attach_lwp (ptid_t ptid)
1443 struct lwp_info *lp;
1447 gdb_assert (is_lwp (ptid));
1449 block_child_signals (&prev_mask);
1451 lp = find_lwp_pid (ptid);
1452 lwpid = GET_LWP (ptid);
1454 /* We assume that we're already attached to any LWP that has an id
1455 equal to the overall process id, and to any LWP that is already
1456 in our list of LWPs. If we're not seeing exit events from threads
1457 and we've had PID wraparound since we last tried to stop all threads,
1458 this assumption might be wrong; fortunately, this is very unlikely
1460 if (lwpid != GET_PID (ptid) && lp == NULL)
1462 int status, cloned = 0, signalled = 0;
1464 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0)
1466 if (linux_supports_tracefork_flag)
1468 /* If we haven't stopped all threads when we get here,
1469 we may have seen a thread listed in thread_db's list,
1470 but not processed the PTRACE_EVENT_CLONE yet. If
1471 that's the case, ignore this new thread, and let
1472 normal event handling discover it later. */
1473 if (in_pid_list_p (stopped_pids, lwpid))
1475 /* We've already seen this thread stop, but we
1476 haven't seen the PTRACE_EVENT_CLONE extended
1478 restore_child_signals_mask (&prev_mask);
1486 /* See if we've got a stop for this new child
1487 pending. If so, we're already attached. */
1488 new_pid = my_waitpid (lwpid, &status, WNOHANG);
1489 if (new_pid == -1 && errno == ECHILD)
1490 new_pid = my_waitpid (lwpid, &status, __WCLONE | WNOHANG);
1493 if (WIFSTOPPED (status))
1494 add_to_pid_list (&stopped_pids, lwpid, status);
1496 restore_child_signals_mask (&prev_mask);
1502 /* If we fail to attach to the thread, issue a warning,
1503 but continue. One way this can happen is if thread
1504 creation is interrupted; as of Linux kernel 2.6.19, a
1505 bug may place threads in the thread list and then fail
1507 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid),
1508 safe_strerror (errno));
1509 restore_child_signals_mask (&prev_mask);
1513 if (debug_linux_nat)
1514 fprintf_unfiltered (gdb_stdlog,
1515 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1516 target_pid_to_str (ptid));
1518 status = linux_nat_post_attach_wait (ptid, 0, &cloned, &signalled);
1519 if (!WIFSTOPPED (status))
1521 restore_child_signals_mask (&prev_mask);
1525 lp = add_lwp (ptid);
1527 lp->cloned = cloned;
1528 lp->signalled = signalled;
1529 if (WSTOPSIG (status) != SIGSTOP)
1532 lp->status = status;
1535 target_post_attach (GET_LWP (lp->ptid));
1537 if (debug_linux_nat)
1539 fprintf_unfiltered (gdb_stdlog,
1540 "LLAL: waitpid %s received %s\n",
1541 target_pid_to_str (ptid),
1542 status_to_str (status));
1547 /* We assume that the LWP representing the original process is
1548 already stopped. Mark it as stopped in the data structure
1549 that the GNU/linux ptrace layer uses to keep track of
1550 threads. Note that this won't have already been done since
1551 the main thread will have, we assume, been stopped by an
1552 attach from a different layer. */
1554 lp = add_lwp (ptid);
1558 lp->last_resume_kind = resume_stop;
1559 restore_child_signals_mask (&prev_mask);
1564 linux_nat_create_inferior (struct target_ops *ops,
1565 char *exec_file, char *allargs, char **env,
1568 #ifdef HAVE_PERSONALITY
1569 int personality_orig = 0, personality_set = 0;
1570 #endif /* HAVE_PERSONALITY */
1572 /* The fork_child mechanism is synchronous and calls target_wait, so
1573 we have to mask the async mode. */
1575 #ifdef HAVE_PERSONALITY
1576 if (disable_randomization)
1579 personality_orig = personality (0xffffffff);
1580 if (errno == 0 && !(personality_orig & ADDR_NO_RANDOMIZE))
1582 personality_set = 1;
1583 personality (personality_orig | ADDR_NO_RANDOMIZE);
1585 if (errno != 0 || (personality_set
1586 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE)))
1587 warning (_("Error disabling address space randomization: %s"),
1588 safe_strerror (errno));
1590 #endif /* HAVE_PERSONALITY */
1592 /* Make sure we report all signals during startup. */
1593 linux_nat_pass_signals (0, NULL);
1595 linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
1597 #ifdef HAVE_PERSONALITY
1598 if (personality_set)
1601 personality (personality_orig);
1603 warning (_("Error restoring address space randomization: %s"),
1604 safe_strerror (errno));
1606 #endif /* HAVE_PERSONALITY */
1610 linux_nat_attach (struct target_ops *ops, char *args, int from_tty)
1612 struct lwp_info *lp;
1616 /* Make sure we report all signals during attach. */
1617 linux_nat_pass_signals (0, NULL);
1619 linux_ops->to_attach (ops, args, from_tty);
1621 /* The ptrace base target adds the main thread with (pid,0,0)
1622 format. Decorate it with lwp info. */
1623 ptid = BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid));
1624 thread_change_ptid (inferior_ptid, ptid);
1626 /* Add the initial process as the first LWP to the list. */
1627 lp = add_lwp (ptid);
1629 status = linux_nat_post_attach_wait (lp->ptid, 1, &lp->cloned,
1631 if (!WIFSTOPPED (status))
1633 if (WIFEXITED (status))
1635 int exit_code = WEXITSTATUS (status);
1637 target_terminal_ours ();
1638 target_mourn_inferior ();
1640 error (_("Unable to attach: program exited normally."));
1642 error (_("Unable to attach: program exited with code %d."),
1645 else if (WIFSIGNALED (status))
1647 enum target_signal signo;
1649 target_terminal_ours ();
1650 target_mourn_inferior ();
1652 signo = target_signal_from_host (WTERMSIG (status));
1653 error (_("Unable to attach: program terminated with signal "
1655 target_signal_to_name (signo),
1656 target_signal_to_string (signo));
1659 internal_error (__FILE__, __LINE__,
1660 _("unexpected status %d for PID %ld"),
1661 status, (long) GET_LWP (ptid));
1666 /* Save the wait status to report later. */
1668 if (debug_linux_nat)
1669 fprintf_unfiltered (gdb_stdlog,
1670 "LNA: waitpid %ld, saving status %s\n",
1671 (long) GET_PID (lp->ptid), status_to_str (status));
1673 lp->status = status;
1675 if (target_can_async_p ())
1676 target_async (inferior_event_handler, 0);
1679 /* Get pending status of LP. */
1681 get_pending_status (struct lwp_info *lp, int *status)
1683 enum target_signal signo = TARGET_SIGNAL_0;
1685 /* If we paused threads momentarily, we may have stored pending
1686 events in lp->status or lp->waitstatus (see stop_wait_callback),
1687 and GDB core hasn't seen any signal for those threads.
1688 Otherwise, the last signal reported to the core is found in the
1689 thread object's stop_signal.
1691 There's a corner case that isn't handled here at present. Only
1692 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1693 stop_signal make sense as a real signal to pass to the inferior.
1694 Some catchpoint related events, like
1695 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1696 to TARGET_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1697 those traps are debug API (ptrace in our case) related and
1698 induced; the inferior wouldn't see them if it wasn't being
1699 traced. Hence, we should never pass them to the inferior, even
1700 when set to pass state. Since this corner case isn't handled by
1701 infrun.c when proceeding with a signal, for consistency, neither
1702 do we handle it here (or elsewhere in the file we check for
1703 signal pass state). Normally SIGTRAP isn't set to pass state, so
1704 this is really a corner case. */
1706 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
1707 signo = TARGET_SIGNAL_0; /* a pending ptrace event, not a real signal. */
1708 else if (lp->status)
1709 signo = target_signal_from_host (WSTOPSIG (lp->status));
1710 else if (non_stop && !is_executing (lp->ptid))
1712 struct thread_info *tp = find_thread_ptid (lp->ptid);
1714 signo = tp->suspend.stop_signal;
1718 struct target_waitstatus last;
1721 get_last_target_status (&last_ptid, &last);
1723 if (GET_LWP (lp->ptid) == GET_LWP (last_ptid))
1725 struct thread_info *tp = find_thread_ptid (lp->ptid);
1727 signo = tp->suspend.stop_signal;
1733 if (signo == TARGET_SIGNAL_0)
1735 if (debug_linux_nat)
1736 fprintf_unfiltered (gdb_stdlog,
1737 "GPT: lwp %s has no pending signal\n",
1738 target_pid_to_str (lp->ptid));
1740 else if (!signal_pass_state (signo))
1742 if (debug_linux_nat)
1743 fprintf_unfiltered (gdb_stdlog,
1744 "GPT: lwp %s had signal %s, "
1745 "but it is in no pass state\n",
1746 target_pid_to_str (lp->ptid),
1747 target_signal_to_string (signo));
1751 *status = W_STOPCODE (target_signal_to_host (signo));
1753 if (debug_linux_nat)
1754 fprintf_unfiltered (gdb_stdlog,
1755 "GPT: lwp %s has pending signal %s\n",
1756 target_pid_to_str (lp->ptid),
1757 target_signal_to_string (signo));
1764 detach_callback (struct lwp_info *lp, void *data)
1766 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1768 if (debug_linux_nat && lp->status)
1769 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
1770 strsignal (WSTOPSIG (lp->status)),
1771 target_pid_to_str (lp->ptid));
1773 /* If there is a pending SIGSTOP, get rid of it. */
1776 if (debug_linux_nat)
1777 fprintf_unfiltered (gdb_stdlog,
1778 "DC: Sending SIGCONT to %s\n",
1779 target_pid_to_str (lp->ptid));
1781 kill_lwp (GET_LWP (lp->ptid), SIGCONT);
1785 /* We don't actually detach from the LWP that has an id equal to the
1786 overall process id just yet. */
1787 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
1791 /* Pass on any pending signal for this LWP. */
1792 get_pending_status (lp, &status);
1794 if (linux_nat_prepare_to_resume != NULL)
1795 linux_nat_prepare_to_resume (lp);
1797 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
1798 WSTOPSIG (status)) < 0)
1799 error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
1800 safe_strerror (errno));
1802 if (debug_linux_nat)
1803 fprintf_unfiltered (gdb_stdlog,
1804 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1805 target_pid_to_str (lp->ptid),
1806 strsignal (WSTOPSIG (status)));
1808 delete_lwp (lp->ptid);
1815 linux_nat_detach (struct target_ops *ops, char *args, int from_tty)
1819 struct lwp_info *main_lwp;
1821 pid = GET_PID (inferior_ptid);
1823 if (target_can_async_p ())
1824 linux_nat_async (NULL, 0);
1826 /* Stop all threads before detaching. ptrace requires that the
1827 thread is stopped to sucessfully detach. */
1828 iterate_over_lwps (pid_to_ptid (pid), stop_callback, NULL);
1829 /* ... and wait until all of them have reported back that
1830 they're no longer running. */
1831 iterate_over_lwps (pid_to_ptid (pid), stop_wait_callback, NULL);
1833 iterate_over_lwps (pid_to_ptid (pid), detach_callback, NULL);
1835 /* Only the initial process should be left right now. */
1836 gdb_assert (num_lwps (GET_PID (inferior_ptid)) == 1);
1838 main_lwp = find_lwp_pid (pid_to_ptid (pid));
1840 /* Pass on any pending signal for the last LWP. */
1841 if ((args == NULL || *args == '\0')
1842 && get_pending_status (main_lwp, &status) != -1
1843 && WIFSTOPPED (status))
1845 /* Put the signal number in ARGS so that inf_ptrace_detach will
1846 pass it along with PTRACE_DETACH. */
1848 sprintf (args, "%d", (int) WSTOPSIG (status));
1849 if (debug_linux_nat)
1850 fprintf_unfiltered (gdb_stdlog,
1851 "LND: Sending signal %s to %s\n",
1853 target_pid_to_str (main_lwp->ptid));
1856 if (linux_nat_prepare_to_resume != NULL)
1857 linux_nat_prepare_to_resume (main_lwp);
1858 delete_lwp (main_lwp->ptid);
1860 if (forks_exist_p ())
1862 /* Multi-fork case. The current inferior_ptid is being detached
1863 from, but there are other viable forks to debug. Detach from
1864 the current fork, and context-switch to the first
1866 linux_fork_detach (args, from_tty);
1868 if (non_stop && target_can_async_p ())
1869 target_async (inferior_event_handler, 0);
1872 linux_ops->to_detach (ops, args, from_tty);
1878 resume_lwp (struct lwp_info *lp, int step)
1882 struct inferior *inf = find_inferior_pid (GET_PID (lp->ptid));
1884 if (inf->vfork_child != NULL)
1886 if (debug_linux_nat)
1887 fprintf_unfiltered (gdb_stdlog,
1888 "RC: Not resuming %s (vfork parent)\n",
1889 target_pid_to_str (lp->ptid));
1891 else if (lp->status == 0
1892 && lp->waitstatus.kind == TARGET_WAITKIND_IGNORE)
1894 if (debug_linux_nat)
1895 fprintf_unfiltered (gdb_stdlog,
1896 "RC: PTRACE_CONT %s, 0, 0 (resuming sibling)\n",
1897 target_pid_to_str (lp->ptid));
1899 if (linux_nat_prepare_to_resume != NULL)
1900 linux_nat_prepare_to_resume (lp);
1901 linux_ops->to_resume (linux_ops,
1902 pid_to_ptid (GET_LWP (lp->ptid)),
1903 step, TARGET_SIGNAL_0);
1906 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
1907 lp->stopped_by_watchpoint = 0;
1911 if (debug_linux_nat)
1912 fprintf_unfiltered (gdb_stdlog,
1913 "RC: Not resuming sibling %s (has pending)\n",
1914 target_pid_to_str (lp->ptid));
1919 if (debug_linux_nat)
1920 fprintf_unfiltered (gdb_stdlog,
1921 "RC: Not resuming sibling %s (not stopped)\n",
1922 target_pid_to_str (lp->ptid));
1927 resume_callback (struct lwp_info *lp, void *data)
1934 resume_clear_callback (struct lwp_info *lp, void *data)
1937 lp->last_resume_kind = resume_stop;
1942 resume_set_callback (struct lwp_info *lp, void *data)
1945 lp->last_resume_kind = resume_continue;
1950 linux_nat_resume (struct target_ops *ops,
1951 ptid_t ptid, int step, enum target_signal signo)
1954 struct lwp_info *lp;
1957 if (debug_linux_nat)
1958 fprintf_unfiltered (gdb_stdlog,
1959 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1960 step ? "step" : "resume",
1961 target_pid_to_str (ptid),
1962 (signo != TARGET_SIGNAL_0
1963 ? strsignal (target_signal_to_host (signo)) : "0"),
1964 target_pid_to_str (inferior_ptid));
1966 block_child_signals (&prev_mask);
1968 /* A specific PTID means `step only this process id'. */
1969 resume_many = (ptid_equal (minus_one_ptid, ptid)
1970 || ptid_is_pid (ptid));
1972 /* Mark the lwps we're resuming as resumed. */
1973 iterate_over_lwps (ptid, resume_set_callback, NULL);
1975 /* See if it's the current inferior that should be handled
1978 lp = find_lwp_pid (inferior_ptid);
1980 lp = find_lwp_pid (ptid);
1981 gdb_assert (lp != NULL);
1983 /* Remember if we're stepping. */
1985 lp->last_resume_kind = step ? resume_step : resume_continue;
1987 /* If we have a pending wait status for this thread, there is no
1988 point in resuming the process. But first make sure that
1989 linux_nat_wait won't preemptively handle the event - we
1990 should never take this short-circuit if we are going to
1991 leave LP running, since we have skipped resuming all the
1992 other threads. This bit of code needs to be synchronized
1993 with linux_nat_wait. */
1995 if (lp->status && WIFSTOPPED (lp->status))
1998 && WSTOPSIG (lp->status)
1999 && sigismember (&pass_mask, WSTOPSIG (lp->status)))
2001 if (debug_linux_nat)
2002 fprintf_unfiltered (gdb_stdlog,
2003 "LLR: Not short circuiting for ignored "
2004 "status 0x%x\n", lp->status);
2006 /* FIXME: What should we do if we are supposed to continue
2007 this thread with a signal? */
2008 gdb_assert (signo == TARGET_SIGNAL_0);
2009 signo = target_signal_from_host (WSTOPSIG (lp->status));
2014 if (lp->status || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2016 /* FIXME: What should we do if we are supposed to continue
2017 this thread with a signal? */
2018 gdb_assert (signo == TARGET_SIGNAL_0);
2020 if (debug_linux_nat)
2021 fprintf_unfiltered (gdb_stdlog,
2022 "LLR: Short circuiting for status 0x%x\n",
2025 restore_child_signals_mask (&prev_mask);
2026 if (target_can_async_p ())
2028 target_async (inferior_event_handler, 0);
2029 /* Tell the event loop we have something to process. */
2035 /* Mark LWP as not stopped to prevent it from being continued by
2040 iterate_over_lwps (ptid, resume_callback, NULL);
2042 /* Convert to something the lower layer understands. */
2043 ptid = pid_to_ptid (GET_LWP (lp->ptid));
2045 if (linux_nat_prepare_to_resume != NULL)
2046 linux_nat_prepare_to_resume (lp);
2047 linux_ops->to_resume (linux_ops, ptid, step, signo);
2048 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
2049 lp->stopped_by_watchpoint = 0;
2051 if (debug_linux_nat)
2052 fprintf_unfiltered (gdb_stdlog,
2053 "LLR: %s %s, %s (resume event thread)\n",
2054 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2055 target_pid_to_str (ptid),
2056 (signo != TARGET_SIGNAL_0
2057 ? strsignal (target_signal_to_host (signo)) : "0"));
2059 restore_child_signals_mask (&prev_mask);
2060 if (target_can_async_p ())
2061 target_async (inferior_event_handler, 0);
2064 /* Send a signal to an LWP. */
2067 kill_lwp (int lwpid, int signo)
2069 /* Use tkill, if possible, in case we are using nptl threads. If tkill
2070 fails, then we are not using nptl threads and we should be using kill. */
2072 #ifdef HAVE_TKILL_SYSCALL
2074 static int tkill_failed;
2081 ret = syscall (__NR_tkill, lwpid, signo);
2082 if (errno != ENOSYS)
2089 return kill (lwpid, signo);
2092 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
2093 event, check if the core is interested in it: if not, ignore the
2094 event, and keep waiting; otherwise, we need to toggle the LWP's
2095 syscall entry/exit status, since the ptrace event itself doesn't
2096 indicate it, and report the trap to higher layers. */
2099 linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
2101 struct target_waitstatus *ourstatus = &lp->waitstatus;
2102 struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
2103 int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, lp->ptid);
2107 /* If we're stopping threads, there's a SIGSTOP pending, which
2108 makes it so that the LWP reports an immediate syscall return,
2109 followed by the SIGSTOP. Skip seeing that "return" using
2110 PTRACE_CONT directly, and let stop_wait_callback collect the
2111 SIGSTOP. Later when the thread is resumed, a new syscall
2112 entry event. If we didn't do this (and returned 0), we'd
2113 leave a syscall entry pending, and our caller, by using
2114 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
2115 itself. Later, when the user re-resumes this LWP, we'd see
2116 another syscall entry event and we'd mistake it for a return.
2118 If stop_wait_callback didn't force the SIGSTOP out of the LWP
2119 (leaving immediately with LWP->signalled set, without issuing
2120 a PTRACE_CONT), it would still be problematic to leave this
2121 syscall enter pending, as later when the thread is resumed,
2122 it would then see the same syscall exit mentioned above,
2123 followed by the delayed SIGSTOP, while the syscall didn't
2124 actually get to execute. It seems it would be even more
2125 confusing to the user. */
2127 if (debug_linux_nat)
2128 fprintf_unfiltered (gdb_stdlog,
2129 "LHST: ignoring syscall %d "
2130 "for LWP %ld (stopping threads), "
2131 "resuming with PTRACE_CONT for SIGSTOP\n",
2133 GET_LWP (lp->ptid));
2135 lp->syscall_state = TARGET_WAITKIND_IGNORE;
2136 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2140 if (catch_syscall_enabled ())
2142 /* Always update the entry/return state, even if this particular
2143 syscall isn't interesting to the core now. In async mode,
2144 the user could install a new catchpoint for this syscall
2145 between syscall enter/return, and we'll need to know to
2146 report a syscall return if that happens. */
2147 lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
2148 ? TARGET_WAITKIND_SYSCALL_RETURN
2149 : TARGET_WAITKIND_SYSCALL_ENTRY);
2151 if (catching_syscall_number (syscall_number))
2153 /* Alright, an event to report. */
2154 ourstatus->kind = lp->syscall_state;
2155 ourstatus->value.syscall_number = syscall_number;
2157 if (debug_linux_nat)
2158 fprintf_unfiltered (gdb_stdlog,
2159 "LHST: stopping for %s of syscall %d"
2162 == TARGET_WAITKIND_SYSCALL_ENTRY
2163 ? "entry" : "return",
2165 GET_LWP (lp->ptid));
2169 if (debug_linux_nat)
2170 fprintf_unfiltered (gdb_stdlog,
2171 "LHST: ignoring %s of syscall %d "
2173 lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
2174 ? "entry" : "return",
2176 GET_LWP (lp->ptid));
2180 /* If we had been syscall tracing, and hence used PT_SYSCALL
2181 before on this LWP, it could happen that the user removes all
2182 syscall catchpoints before we get to process this event.
2183 There are two noteworthy issues here:
2185 - When stopped at a syscall entry event, resuming with
2186 PT_STEP still resumes executing the syscall and reports a
2189 - Only PT_SYSCALL catches syscall enters. If we last
2190 single-stepped this thread, then this event can't be a
2191 syscall enter. If we last single-stepped this thread, this
2192 has to be a syscall exit.
2194 The points above mean that the next resume, be it PT_STEP or
2195 PT_CONTINUE, can not trigger a syscall trace event. */
2196 if (debug_linux_nat)
2197 fprintf_unfiltered (gdb_stdlog,
2198 "LHST: caught syscall event "
2199 "with no syscall catchpoints."
2200 " %d for LWP %ld, ignoring\n",
2202 GET_LWP (lp->ptid));
2203 lp->syscall_state = TARGET_WAITKIND_IGNORE;
2206 /* The core isn't interested in this event. For efficiency, avoid
2207 stopping all threads only to have the core resume them all again.
2208 Since we're not stopping threads, if we're still syscall tracing
2209 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
2210 subsequent syscall. Simply resume using the inf-ptrace layer,
2211 which knows when to use PT_SYSCALL or PT_CONTINUE. */
2213 /* Note that gdbarch_get_syscall_number may access registers, hence
2215 registers_changed ();
2216 if (linux_nat_prepare_to_resume != NULL)
2217 linux_nat_prepare_to_resume (lp);
2218 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
2219 lp->step, TARGET_SIGNAL_0);
2223 /* Handle a GNU/Linux extended wait response. If we see a clone
2224 event, we need to add the new LWP to our list (and not report the
2225 trap to higher layers). This function returns non-zero if the
2226 event should be ignored and we should wait again. If STOPPING is
2227 true, the new LWP remains stopped, otherwise it is continued. */
2230 linux_handle_extended_wait (struct lwp_info *lp, int status,
2233 int pid = GET_LWP (lp->ptid);
2234 struct target_waitstatus *ourstatus = &lp->waitstatus;
2235 int event = status >> 16;
2237 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
2238 || event == PTRACE_EVENT_CLONE)
2240 unsigned long new_pid;
2243 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
2245 /* If we haven't already seen the new PID stop, wait for it now. */
2246 if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
2248 /* The new child has a pending SIGSTOP. We can't affect it until it
2249 hits the SIGSTOP, but we're already attached. */
2250 ret = my_waitpid (new_pid, &status,
2251 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
2253 perror_with_name (_("waiting for new child"));
2254 else if (ret != new_pid)
2255 internal_error (__FILE__, __LINE__,
2256 _("wait returned unexpected PID %d"), ret);
2257 else if (!WIFSTOPPED (status))
2258 internal_error (__FILE__, __LINE__,
2259 _("wait returned unexpected status 0x%x"), status);
2262 ourstatus->value.related_pid = ptid_build (new_pid, new_pid, 0);
2264 if (event == PTRACE_EVENT_FORK
2265 && linux_fork_checkpointing_p (GET_PID (lp->ptid)))
2267 /* Handle checkpointing by linux-fork.c here as a special
2268 case. We don't want the follow-fork-mode or 'catch fork'
2269 to interfere with this. */
2271 /* This won't actually modify the breakpoint list, but will
2272 physically remove the breakpoints from the child. */
2273 detach_breakpoints (new_pid);
2275 /* Retain child fork in ptrace (stopped) state. */
2276 if (!find_fork_pid (new_pid))
2279 /* Report as spurious, so that infrun doesn't want to follow
2280 this fork. We're actually doing an infcall in
2282 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
2283 linux_enable_event_reporting (pid_to_ptid (new_pid));
2285 /* Report the stop to the core. */
2289 if (event == PTRACE_EVENT_FORK)
2290 ourstatus->kind = TARGET_WAITKIND_FORKED;
2291 else if (event == PTRACE_EVENT_VFORK)
2292 ourstatus->kind = TARGET_WAITKIND_VFORKED;
2295 struct lwp_info *new_lp;
2297 ourstatus->kind = TARGET_WAITKIND_IGNORE;
2299 if (debug_linux_nat)
2300 fprintf_unfiltered (gdb_stdlog,
2301 "LHEW: Got clone event "
2302 "from LWP %d, new child is LWP %ld\n",
2305 new_lp = add_lwp (BUILD_LWP (new_pid, GET_PID (lp->ptid)));
2307 new_lp->stopped = 1;
2309 if (WSTOPSIG (status) != SIGSTOP)
2311 /* This can happen if someone starts sending signals to
2312 the new thread before it gets a chance to run, which
2313 have a lower number than SIGSTOP (e.g. SIGUSR1).
2314 This is an unlikely case, and harder to handle for
2315 fork / vfork than for clone, so we do not try - but
2316 we handle it for clone events here. We'll send
2317 the other signal on to the thread below. */
2319 new_lp->signalled = 1;
2323 struct thread_info *tp;
2325 /* When we stop for an event in some other thread, and
2326 pull the thread list just as this thread has cloned,
2327 we'll have seen the new thread in the thread_db list
2328 before handling the CLONE event (glibc's
2329 pthread_create adds the new thread to the thread list
2330 before clone'ing, and has the kernel fill in the
2331 thread's tid on the clone call with
2332 CLONE_PARENT_SETTID). If that happened, and the core
2333 had requested the new thread to stop, we'll have
2334 killed it with SIGSTOP. But since SIGSTOP is not an
2335 RT signal, it can only be queued once. We need to be
2336 careful to not resume the LWP if we wanted it to
2337 stop. In that case, we'll leave the SIGSTOP pending.
2338 It will later be reported as TARGET_SIGNAL_0. */
2339 tp = find_thread_ptid (new_lp->ptid);
2340 if (tp != NULL && tp->stop_requested)
2341 new_lp->last_resume_kind = resume_stop;
2348 /* Add the new thread to GDB's lists as soon as possible
2351 1) the frontend doesn't have to wait for a stop to
2354 2) we tag it with the correct running state. */
2356 /* If the thread_db layer is active, let it know about
2357 this new thread, and add it to GDB's list. */
2358 if (!thread_db_attach_lwp (new_lp->ptid))
2360 /* We're not using thread_db. Add it to GDB's
2362 target_post_attach (GET_LWP (new_lp->ptid));
2363 add_thread (new_lp->ptid);
2368 set_running (new_lp->ptid, 1);
2369 set_executing (new_lp->ptid, 1);
2370 /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
2372 new_lp->last_resume_kind = resume_continue;
2378 /* We created NEW_LP so it cannot yet contain STATUS. */
2379 gdb_assert (new_lp->status == 0);
2381 /* Save the wait status to report later. */
2382 if (debug_linux_nat)
2383 fprintf_unfiltered (gdb_stdlog,
2384 "LHEW: waitpid of new LWP %ld, "
2385 "saving status %s\n",
2386 (long) GET_LWP (new_lp->ptid),
2387 status_to_str (status));
2388 new_lp->status = status;
2391 /* Note the need to use the low target ops to resume, to
2392 handle resuming with PT_SYSCALL if we have syscall
2396 new_lp->resumed = 1;
2400 gdb_assert (new_lp->last_resume_kind == resume_continue);
2401 if (debug_linux_nat)
2402 fprintf_unfiltered (gdb_stdlog,
2403 "LHEW: resuming new LWP %ld\n",
2404 GET_LWP (new_lp->ptid));
2405 if (linux_nat_prepare_to_resume != NULL)
2406 linux_nat_prepare_to_resume (new_lp);
2407 linux_ops->to_resume (linux_ops, pid_to_ptid (new_pid),
2408 0, TARGET_SIGNAL_0);
2409 new_lp->stopped = 0;
2413 if (debug_linux_nat)
2414 fprintf_unfiltered (gdb_stdlog,
2415 "LHEW: resuming parent LWP %d\n", pid);
2416 if (linux_nat_prepare_to_resume != NULL)
2417 linux_nat_prepare_to_resume (lp);
2418 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
2419 0, TARGET_SIGNAL_0);
2427 if (event == PTRACE_EVENT_EXEC)
2429 if (debug_linux_nat)
2430 fprintf_unfiltered (gdb_stdlog,
2431 "LHEW: Got exec event from LWP %ld\n",
2432 GET_LWP (lp->ptid));
2434 ourstatus->kind = TARGET_WAITKIND_EXECD;
2435 ourstatus->value.execd_pathname
2436 = xstrdup (linux_child_pid_to_exec_file (pid));
2441 if (event == PTRACE_EVENT_VFORK_DONE)
2443 if (current_inferior ()->waiting_for_vfork_done)
2445 if (debug_linux_nat)
2446 fprintf_unfiltered (gdb_stdlog,
2447 "LHEW: Got expected PTRACE_EVENT_"
2448 "VFORK_DONE from LWP %ld: stopping\n",
2449 GET_LWP (lp->ptid));
2451 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
2455 if (debug_linux_nat)
2456 fprintf_unfiltered (gdb_stdlog,
2457 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2458 "from LWP %ld: resuming\n",
2459 GET_LWP (lp->ptid));
2460 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2464 internal_error (__FILE__, __LINE__,
2465 _("unknown ptrace event %d"), event);
2468 /* Return non-zero if LWP is a zombie. */
2471 linux_lwp_is_zombie (long lwp)
2473 char buffer[MAXPATHLEN];
2478 xsnprintf (buffer, sizeof (buffer), "/proc/%ld/status", lwp);
2479 procfile = fopen (buffer, "r");
2480 if (procfile == NULL)
2482 warning (_("unable to open /proc file '%s'"), buffer);
2487 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
2488 if (strncmp (buffer, "State:", 6) == 0)
2493 retval = (have_state
2494 && strcmp (buffer, "State:\tZ (zombie)\n") == 0);
2499 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2503 wait_lwp (struct lwp_info *lp)
2507 int thread_dead = 0;
2510 gdb_assert (!lp->stopped);
2511 gdb_assert (lp->status == 0);
2513 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2514 block_child_signals (&prev_mask);
2518 /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2519 was right and we should just call sigsuspend. */
2521 pid = my_waitpid (GET_LWP (lp->ptid), &status, WNOHANG);
2522 if (pid == -1 && errno == ECHILD)
2523 pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE | WNOHANG);
2524 if (pid == -1 && errno == ECHILD)
2526 /* The thread has previously exited. We need to delete it
2527 now because, for some vendor 2.4 kernels with NPTL
2528 support backported, there won't be an exit event unless
2529 it is the main thread. 2.6 kernels will report an exit
2530 event for each thread that exits, as expected. */
2532 if (debug_linux_nat)
2533 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
2534 target_pid_to_str (lp->ptid));
2539 /* Bugs 10970, 12702.
2540 Thread group leader may have exited in which case we'll lock up in
2541 waitpid if there are other threads, even if they are all zombies too.
2542 Basically, we're not supposed to use waitpid this way.
2543 __WCLONE is not applicable for the leader so we can't use that.
2544 LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2545 process; it gets ESRCH both for the zombie and for running processes.
2547 As a workaround, check if we're waiting for the thread group leader and
2548 if it's a zombie, and avoid calling waitpid if it is.
2550 This is racy, what if the tgl becomes a zombie right after we check?
2551 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2552 waiting waitpid but the linux_lwp_is_zombie is safe this way. */
2554 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid)
2555 && linux_lwp_is_zombie (GET_LWP (lp->ptid)))
2558 if (debug_linux_nat)
2559 fprintf_unfiltered (gdb_stdlog,
2560 "WL: Thread group leader %s vanished.\n",
2561 target_pid_to_str (lp->ptid));
2565 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2566 get invoked despite our caller had them intentionally blocked by
2567 block_child_signals. This is sensitive only to the loop of
2568 linux_nat_wait_1 and there if we get called my_waitpid gets called
2569 again before it gets to sigsuspend so we can safely let the handlers
2570 get executed here. */
2572 sigsuspend (&suspend_mask);
2575 restore_child_signals_mask (&prev_mask);
2579 gdb_assert (pid == GET_LWP (lp->ptid));
2581 if (debug_linux_nat)
2583 fprintf_unfiltered (gdb_stdlog,
2584 "WL: waitpid %s received %s\n",
2585 target_pid_to_str (lp->ptid),
2586 status_to_str (status));
2589 /* Check if the thread has exited. */
2590 if (WIFEXITED (status) || WIFSIGNALED (status))
2593 if (debug_linux_nat)
2594 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
2595 target_pid_to_str (lp->ptid));
2605 gdb_assert (WIFSTOPPED (status));
2607 /* Handle GNU/Linux's syscall SIGTRAPs. */
2608 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
2610 /* No longer need the sysgood bit. The ptrace event ends up
2611 recorded in lp->waitstatus if we care for it. We can carry
2612 on handling the event like a regular SIGTRAP from here
2614 status = W_STOPCODE (SIGTRAP);
2615 if (linux_handle_syscall_trap (lp, 1))
2616 return wait_lwp (lp);
2619 /* Handle GNU/Linux's extended waitstatus for trace events. */
2620 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
2622 if (debug_linux_nat)
2623 fprintf_unfiltered (gdb_stdlog,
2624 "WL: Handling extended status 0x%06x\n",
2626 if (linux_handle_extended_wait (lp, status, 1))
2627 return wait_lwp (lp);
2633 /* Save the most recent siginfo for LP. This is currently only called
2634 for SIGTRAP; some ports use the si_addr field for
2635 target_stopped_data_address. In the future, it may also be used to
2636 restore the siginfo of requeued signals. */
2639 save_siginfo (struct lwp_info *lp)
2642 ptrace (PTRACE_GETSIGINFO, GET_LWP (lp->ptid),
2643 (PTRACE_TYPE_ARG3) 0, &lp->siginfo);
2646 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
2649 /* Send a SIGSTOP to LP. */
2652 stop_callback (struct lwp_info *lp, void *data)
2654 if (!lp->stopped && !lp->signalled)
2658 if (debug_linux_nat)
2660 fprintf_unfiltered (gdb_stdlog,
2661 "SC: kill %s **<SIGSTOP>**\n",
2662 target_pid_to_str (lp->ptid));
2665 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
2666 if (debug_linux_nat)
2668 fprintf_unfiltered (gdb_stdlog,
2669 "SC: lwp kill %d %s\n",
2671 errno ? safe_strerror (errno) : "ERRNO-OK");
2675 gdb_assert (lp->status == 0);
2681 /* Request a stop on LWP. */
2684 linux_stop_lwp (struct lwp_info *lwp)
2686 stop_callback (lwp, NULL);
2689 /* Return non-zero if LWP PID has a pending SIGINT. */
2692 linux_nat_has_pending_sigint (int pid)
2694 sigset_t pending, blocked, ignored;
2696 linux_proc_pending_signals (pid, &pending, &blocked, &ignored);
2698 if (sigismember (&pending, SIGINT)
2699 && !sigismember (&ignored, SIGINT))
2705 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2708 set_ignore_sigint (struct lwp_info *lp, void *data)
2710 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2711 flag to consume the next one. */
2712 if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status)
2713 && WSTOPSIG (lp->status) == SIGINT)
2716 lp->ignore_sigint = 1;
2721 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2722 This function is called after we know the LWP has stopped; if the LWP
2723 stopped before the expected SIGINT was delivered, then it will never have
2724 arrived. Also, if the signal was delivered to a shared queue and consumed
2725 by a different thread, it will never be delivered to this LWP. */
2728 maybe_clear_ignore_sigint (struct lwp_info *lp)
2730 if (!lp->ignore_sigint)
2733 if (!linux_nat_has_pending_sigint (GET_LWP (lp->ptid)))
2735 if (debug_linux_nat)
2736 fprintf_unfiltered (gdb_stdlog,
2737 "MCIS: Clearing bogus flag for %s\n",
2738 target_pid_to_str (lp->ptid));
2739 lp->ignore_sigint = 0;
2743 /* Fetch the possible triggered data watchpoint info and store it in
2746 On some archs, like x86, that use debug registers to set
2747 watchpoints, it's possible that the way to know which watched
2748 address trapped, is to check the register that is used to select
2749 which address to watch. Problem is, between setting the watchpoint
2750 and reading back which data address trapped, the user may change
2751 the set of watchpoints, and, as a consequence, GDB changes the
2752 debug registers in the inferior. To avoid reading back a stale
2753 stopped-data-address when that happens, we cache in LP the fact
2754 that a watchpoint trapped, and the corresponding data address, as
2755 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2756 registers meanwhile, we have the cached data we can rely on. */
2759 save_sigtrap (struct lwp_info *lp)
2761 struct cleanup *old_chain;
2763 if (linux_ops->to_stopped_by_watchpoint == NULL)
2765 lp->stopped_by_watchpoint = 0;
2769 old_chain = save_inferior_ptid ();
2770 inferior_ptid = lp->ptid;
2772 lp->stopped_by_watchpoint = linux_ops->to_stopped_by_watchpoint ();
2774 if (lp->stopped_by_watchpoint)
2776 if (linux_ops->to_stopped_data_address != NULL)
2777 lp->stopped_data_address_p =
2778 linux_ops->to_stopped_data_address (¤t_target,
2779 &lp->stopped_data_address);
2781 lp->stopped_data_address_p = 0;
2784 do_cleanups (old_chain);
2787 /* See save_sigtrap. */
2790 linux_nat_stopped_by_watchpoint (void)
2792 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2794 gdb_assert (lp != NULL);
2796 return lp->stopped_by_watchpoint;
2800 linux_nat_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
2802 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2804 gdb_assert (lp != NULL);
2806 *addr_p = lp->stopped_data_address;
2808 return lp->stopped_data_address_p;
2811 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2814 sigtrap_is_event (int status)
2816 return WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP;
2819 /* SIGTRAP-like events recognizer. */
2821 static int (*linux_nat_status_is_event) (int status) = sigtrap_is_event;
2823 /* Check for SIGTRAP-like events in LP. */
2826 linux_nat_lp_status_is_event (struct lwp_info *lp)
2828 /* We check for lp->waitstatus in addition to lp->status, because we can
2829 have pending process exits recorded in lp->status
2830 and W_EXITCODE(0,0) == 0. We should probably have an additional
2831 lp->status_p flag. */
2833 return (lp->waitstatus.kind == TARGET_WAITKIND_IGNORE
2834 && linux_nat_status_is_event (lp->status));
2837 /* Set alternative SIGTRAP-like events recognizer. If
2838 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2842 linux_nat_set_status_is_event (struct target_ops *t,
2843 int (*status_is_event) (int status))
2845 linux_nat_status_is_event = status_is_event;
2848 /* Wait until LP is stopped. */
2851 stop_wait_callback (struct lwp_info *lp, void *data)
2853 struct inferior *inf = find_inferior_pid (GET_PID (lp->ptid));
2855 /* If this is a vfork parent, bail out, it is not going to report
2856 any SIGSTOP until the vfork is done with. */
2857 if (inf->vfork_child != NULL)
2864 status = wait_lwp (lp);
2868 if (lp->ignore_sigint && WIFSTOPPED (status)
2869 && WSTOPSIG (status) == SIGINT)
2871 lp->ignore_sigint = 0;
2874 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2875 if (debug_linux_nat)
2876 fprintf_unfiltered (gdb_stdlog,
2877 "PTRACE_CONT %s, 0, 0 (%s) "
2878 "(discarding SIGINT)\n",
2879 target_pid_to_str (lp->ptid),
2880 errno ? safe_strerror (errno) : "OK");
2882 return stop_wait_callback (lp, NULL);
2885 maybe_clear_ignore_sigint (lp);
2887 if (WSTOPSIG (status) != SIGSTOP)
2889 if (linux_nat_status_is_event (status))
2891 /* If a LWP other than the LWP that we're reporting an
2892 event for has hit a GDB breakpoint (as opposed to
2893 some random trap signal), then just arrange for it to
2894 hit it again later. We don't keep the SIGTRAP status
2895 and don't forward the SIGTRAP signal to the LWP. We
2896 will handle the current event, eventually we will
2897 resume all LWPs, and this one will get its breakpoint
2900 If we do not do this, then we run the risk that the
2901 user will delete or disable the breakpoint, but the
2902 thread will have already tripped on it. */
2904 /* Save the trap's siginfo in case we need it later. */
2909 /* Now resume this LWP and get the SIGSTOP event. */
2911 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2912 if (debug_linux_nat)
2914 fprintf_unfiltered (gdb_stdlog,
2915 "PTRACE_CONT %s, 0, 0 (%s)\n",
2916 target_pid_to_str (lp->ptid),
2917 errno ? safe_strerror (errno) : "OK");
2919 fprintf_unfiltered (gdb_stdlog,
2920 "SWC: Candidate SIGTRAP event in %s\n",
2921 target_pid_to_str (lp->ptid));
2923 /* Hold this event/waitstatus while we check to see if
2924 there are any more (we still want to get that SIGSTOP). */
2925 stop_wait_callback (lp, NULL);
2927 /* Hold the SIGTRAP for handling by linux_nat_wait. If
2928 there's another event, throw it back into the
2932 if (debug_linux_nat)
2933 fprintf_unfiltered (gdb_stdlog,
2934 "SWC: kill %s, %s\n",
2935 target_pid_to_str (lp->ptid),
2936 status_to_str ((int) status));
2937 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
2940 /* Save the sigtrap event. */
2941 lp->status = status;
2946 /* The thread was stopped with a signal other than
2947 SIGSTOP, and didn't accidentally trip a breakpoint. */
2949 if (debug_linux_nat)
2951 fprintf_unfiltered (gdb_stdlog,
2952 "SWC: Pending event %s in %s\n",
2953 status_to_str ((int) status),
2954 target_pid_to_str (lp->ptid));
2956 /* Now resume this LWP and get the SIGSTOP event. */
2958 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2959 if (debug_linux_nat)
2960 fprintf_unfiltered (gdb_stdlog,
2961 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2962 target_pid_to_str (lp->ptid),
2963 errno ? safe_strerror (errno) : "OK");
2965 /* Hold this event/waitstatus while we check to see if
2966 there are any more (we still want to get that SIGSTOP). */
2967 stop_wait_callback (lp, NULL);
2969 /* If the lp->status field is still empty, use it to
2970 hold this event. If not, then this event must be
2971 returned to the event queue of the LWP. */
2974 if (debug_linux_nat)
2976 fprintf_unfiltered (gdb_stdlog,
2977 "SWC: kill %s, %s\n",
2978 target_pid_to_str (lp->ptid),
2979 status_to_str ((int) status));
2981 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
2984 lp->status = status;
2990 /* We caught the SIGSTOP that we intended to catch, so
2991 there's no SIGSTOP pending. */
3000 /* Return non-zero if LP has a wait status pending. */
3003 status_callback (struct lwp_info *lp, void *data)
3005 /* Only report a pending wait status if we pretend that this has
3006 indeed been resumed. */
3010 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3012 /* A ptrace event, like PTRACE_FORK|VFORK|EXEC, syscall event,
3013 or a pending process exit. Note that `W_EXITCODE(0,0) ==
3014 0', so a clean process exit can not be stored pending in
3015 lp->status, it is indistinguishable from
3016 no-pending-status. */
3020 if (lp->status != 0)
3026 /* Return non-zero if LP isn't stopped. */
3029 running_callback (struct lwp_info *lp, void *data)
3031 return (!lp->stopped
3032 || ((lp->status != 0
3033 || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3037 /* Count the LWP's that have had events. */
3040 count_events_callback (struct lwp_info *lp, void *data)
3044 gdb_assert (count != NULL);
3046 /* Count only resumed LWPs that have a SIGTRAP event pending. */
3047 if (lp->resumed && linux_nat_lp_status_is_event (lp))
3053 /* Select the LWP (if any) that is currently being single-stepped. */
3056 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
3058 if (lp->last_resume_kind == resume_step
3065 /* Select the Nth LWP that has had a SIGTRAP event. */
3068 select_event_lwp_callback (struct lwp_info *lp, void *data)
3070 int *selector = data;
3072 gdb_assert (selector != NULL);
3074 /* Select only resumed LWPs that have a SIGTRAP event pending. */
3075 if (lp->resumed && linux_nat_lp_status_is_event (lp))
3076 if ((*selector)-- == 0)
3083 cancel_breakpoint (struct lwp_info *lp)
3085 /* Arrange for a breakpoint to be hit again later. We don't keep
3086 the SIGTRAP status and don't forward the SIGTRAP signal to the
3087 LWP. We will handle the current event, eventually we will resume
3088 this LWP, and this breakpoint will trap again.
3090 If we do not do this, then we run the risk that the user will
3091 delete or disable the breakpoint, but the LWP will have already
3094 struct regcache *regcache = get_thread_regcache (lp->ptid);
3095 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3098 pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
3099 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
3101 if (debug_linux_nat)
3102 fprintf_unfiltered (gdb_stdlog,
3103 "CB: Push back breakpoint for %s\n",
3104 target_pid_to_str (lp->ptid));
3106 /* Back up the PC if necessary. */
3107 if (gdbarch_decr_pc_after_break (gdbarch))
3108 regcache_write_pc (regcache, pc);
3116 cancel_breakpoints_callback (struct lwp_info *lp, void *data)
3118 struct lwp_info *event_lp = data;
3120 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
3124 /* If a LWP other than the LWP that we're reporting an event for has
3125 hit a GDB breakpoint (as opposed to some random trap signal),
3126 then just arrange for it to hit it again later. We don't keep
3127 the SIGTRAP status and don't forward the SIGTRAP signal to the
3128 LWP. We will handle the current event, eventually we will resume
3129 all LWPs, and this one will get its breakpoint trap again.
3131 If we do not do this, then we run the risk that the user will
3132 delete or disable the breakpoint, but the LWP will have already
3135 if (linux_nat_lp_status_is_event (lp)
3136 && cancel_breakpoint (lp))
3137 /* Throw away the SIGTRAP. */
3143 /* Select one LWP out of those that have events pending. */
3146 select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status)
3149 int random_selector;
3150 struct lwp_info *event_lp;
3152 /* Record the wait status for the original LWP. */
3153 (*orig_lp)->status = *status;
3155 /* Give preference to any LWP that is being single-stepped. */
3156 event_lp = iterate_over_lwps (filter,
3157 select_singlestep_lwp_callback, NULL);
3158 if (event_lp != NULL)
3160 if (debug_linux_nat)
3161 fprintf_unfiltered (gdb_stdlog,
3162 "SEL: Select single-step %s\n",
3163 target_pid_to_str (event_lp->ptid));
3167 /* No single-stepping LWP. Select one at random, out of those
3168 which have had SIGTRAP events. */
3170 /* First see how many SIGTRAP events we have. */
3171 iterate_over_lwps (filter, count_events_callback, &num_events);
3173 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
3174 random_selector = (int)
3175 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
3177 if (debug_linux_nat && num_events > 1)
3178 fprintf_unfiltered (gdb_stdlog,
3179 "SEL: Found %d SIGTRAP events, selecting #%d\n",
3180 num_events, random_selector);
3182 event_lp = iterate_over_lwps (filter,
3183 select_event_lwp_callback,
3187 if (event_lp != NULL)
3189 /* Switch the event LWP. */
3190 *orig_lp = event_lp;
3191 *status = event_lp->status;
3194 /* Flush the wait status for the event LWP. */
3195 (*orig_lp)->status = 0;
3198 /* Return non-zero if LP has been resumed. */
3201 resumed_callback (struct lwp_info *lp, void *data)
3206 /* Stop an active thread, verify it still exists, then resume it. If
3207 the thread ends up with a pending status, then it is not resumed,
3208 and *DATA (really a pointer to int), is set. */
3211 stop_and_resume_callback (struct lwp_info *lp, void *data)
3213 int *new_pending_p = data;
3217 ptid_t ptid = lp->ptid;
3219 stop_callback (lp, NULL);
3220 stop_wait_callback (lp, NULL);
3222 /* Resume if the lwp still exists, and the core wanted it
3224 lp = find_lwp_pid (ptid);
3227 if (lp->last_resume_kind == resume_stop
3230 /* The core wanted the LWP to stop. Even if it stopped
3231 cleanly (with SIGSTOP), leave the event pending. */
3232 if (debug_linux_nat)
3233 fprintf_unfiltered (gdb_stdlog,
3234 "SARC: core wanted LWP %ld stopped "
3235 "(leaving SIGSTOP pending)\n",
3236 GET_LWP (lp->ptid));
3237 lp->status = W_STOPCODE (SIGSTOP);
3240 if (lp->status == 0)
3242 if (debug_linux_nat)
3243 fprintf_unfiltered (gdb_stdlog,
3244 "SARC: re-resuming LWP %ld\n",
3245 GET_LWP (lp->ptid));
3246 resume_lwp (lp, lp->step);
3250 if (debug_linux_nat)
3251 fprintf_unfiltered (gdb_stdlog,
3252 "SARC: not re-resuming LWP %ld "
3254 GET_LWP (lp->ptid));
3263 /* Check if we should go on and pass this event to common code.
3264 Return the affected lwp if we are, or NULL otherwise. If we stop
3265 all lwps temporarily, we may end up with new pending events in some
3266 other lwp. In that case set *NEW_PENDING_P to true. */
3268 static struct lwp_info *
3269 linux_nat_filter_event (int lwpid, int status, int *new_pending_p)
3271 struct lwp_info *lp;
3275 lp = find_lwp_pid (pid_to_ptid (lwpid));
3277 /* Check for stop events reported by a process we didn't already
3278 know about - anything not already in our LWP list.
3280 If we're expecting to receive stopped processes after
3281 fork, vfork, and clone events, then we'll just add the
3282 new one to our list and go back to waiting for the event
3283 to be reported - the stopped process might be returned
3284 from waitpid before or after the event is.
3286 But note the case of a non-leader thread exec'ing after the
3287 leader having exited, and gone from our lists. The non-leader
3288 thread changes its tid to the tgid. */
3290 if (WIFSTOPPED (status) && lp == NULL
3291 && (WSTOPSIG (status) == SIGTRAP && status >> 16 == PTRACE_EVENT_EXEC))
3293 /* A multi-thread exec after we had seen the leader exiting. */
3294 if (debug_linux_nat)
3295 fprintf_unfiltered (gdb_stdlog,
3296 "LLW: Re-adding thread group leader LWP %d.\n",
3299 lp = add_lwp (BUILD_LWP (lwpid, lwpid));
3302 add_thread (lp->ptid);
3305 if (WIFSTOPPED (status) && !lp)
3307 add_to_pid_list (&stopped_pids, lwpid, status);
3311 /* Make sure we don't report an event for the exit of an LWP not in
3312 our list, i.e. not part of the current process. This can happen
3313 if we detach from a program we originally forked and then it
3315 if (!WIFSTOPPED (status) && !lp)
3318 /* Handle GNU/Linux's syscall SIGTRAPs. */
3319 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
3321 /* No longer need the sysgood bit. The ptrace event ends up
3322 recorded in lp->waitstatus if we care for it. We can carry
3323 on handling the event like a regular SIGTRAP from here
3325 status = W_STOPCODE (SIGTRAP);
3326 if (linux_handle_syscall_trap (lp, 0))
3330 /* Handle GNU/Linux's extended waitstatus for trace events. */
3331 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
3333 if (debug_linux_nat)
3334 fprintf_unfiltered (gdb_stdlog,
3335 "LLW: Handling extended status 0x%06x\n",
3337 if (linux_handle_extended_wait (lp, status, 0))
3341 if (linux_nat_status_is_event (status))
3343 /* Save the trap's siginfo in case we need it later. */
3349 /* Check if the thread has exited. */
3350 if ((WIFEXITED (status) || WIFSIGNALED (status))
3351 && num_lwps (GET_PID (lp->ptid)) > 1)
3353 /* If this is the main thread, we must stop all threads and verify
3354 if they are still alive. This is because in the nptl thread model
3355 on Linux 2.4, there is no signal issued for exiting LWPs
3356 other than the main thread. We only get the main thread exit
3357 signal once all child threads have already exited. If we
3358 stop all the threads and use the stop_wait_callback to check
3359 if they have exited we can determine whether this signal
3360 should be ignored or whether it means the end of the debugged
3361 application, regardless of which threading model is being
3363 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
3366 iterate_over_lwps (pid_to_ptid (GET_PID (lp->ptid)),
3367 stop_and_resume_callback, new_pending_p);
3370 if (debug_linux_nat)
3371 fprintf_unfiltered (gdb_stdlog,
3372 "LLW: %s exited.\n",
3373 target_pid_to_str (lp->ptid));
3375 if (num_lwps (GET_PID (lp->ptid)) > 1)
3377 /* If there is at least one more LWP, then the exit signal
3378 was not the end of the debugged application and should be
3385 /* Check if the current LWP has previously exited. In the nptl
3386 thread model, LWPs other than the main thread do not issue
3387 signals when they exit so we must check whenever the thread has
3388 stopped. A similar check is made in stop_wait_callback(). */
3389 if (num_lwps (GET_PID (lp->ptid)) > 1 && !linux_thread_alive (lp->ptid))
3391 ptid_t ptid = pid_to_ptid (GET_PID (lp->ptid));
3393 if (debug_linux_nat)
3394 fprintf_unfiltered (gdb_stdlog,
3395 "LLW: %s exited.\n",
3396 target_pid_to_str (lp->ptid));
3400 /* Make sure there is at least one thread running. */
3401 gdb_assert (iterate_over_lwps (ptid, running_callback, NULL));
3403 /* Discard the event. */
3407 /* Make sure we don't report a SIGSTOP that we sent ourselves in
3408 an attempt to stop an LWP. */
3410 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
3412 if (debug_linux_nat)
3413 fprintf_unfiltered (gdb_stdlog,
3414 "LLW: Delayed SIGSTOP caught for %s.\n",
3415 target_pid_to_str (lp->ptid));
3419 if (lp->last_resume_kind != resume_stop)
3421 /* This is a delayed SIGSTOP. */
3423 registers_changed ();
3425 if (linux_nat_prepare_to_resume != NULL)
3426 linux_nat_prepare_to_resume (lp);
3427 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
3428 lp->step, TARGET_SIGNAL_0);
3429 if (debug_linux_nat)
3430 fprintf_unfiltered (gdb_stdlog,
3431 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
3433 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3434 target_pid_to_str (lp->ptid));
3437 gdb_assert (lp->resumed);
3439 /* Discard the event. */
3444 /* Make sure we don't report a SIGINT that we have already displayed
3445 for another thread. */
3446 if (lp->ignore_sigint
3447 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT)
3449 if (debug_linux_nat)
3450 fprintf_unfiltered (gdb_stdlog,
3451 "LLW: Delayed SIGINT caught for %s.\n",
3452 target_pid_to_str (lp->ptid));
3454 /* This is a delayed SIGINT. */
3455 lp->ignore_sigint = 0;
3457 registers_changed ();
3458 if (linux_nat_prepare_to_resume != NULL)
3459 linux_nat_prepare_to_resume (lp);
3460 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
3461 lp->step, TARGET_SIGNAL_0);
3462 if (debug_linux_nat)
3463 fprintf_unfiltered (gdb_stdlog,
3464 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
3466 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3467 target_pid_to_str (lp->ptid));
3470 gdb_assert (lp->resumed);
3472 /* Discard the event. */
3476 /* An interesting event. */
3478 lp->status = status;
3482 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3483 their exits until all other threads in the group have exited. */
3486 check_zombie_leaders (void)
3488 struct inferior *inf;
3492 struct lwp_info *leader_lp;
3497 leader_lp = find_lwp_pid (pid_to_ptid (inf->pid));
3498 if (leader_lp != NULL
3499 /* Check if there are other threads in the group, as we may
3500 have raced with the inferior simply exiting. */
3501 && num_lwps (inf->pid) > 1
3502 && linux_lwp_is_zombie (inf->pid))
3504 if (debug_linux_nat)
3505 fprintf_unfiltered (gdb_stdlog,
3506 "CZL: Thread group leader %d zombie "
3507 "(it exited, or another thread execd).\n",
3510 /* A leader zombie can mean one of two things:
3512 - It exited, and there's an exit status pending
3513 available, or only the leader exited (not the whole
3514 program). In the latter case, we can't waitpid the
3515 leader's exit status until all other threads are gone.
3517 - There are 3 or more threads in the group, and a thread
3518 other than the leader exec'd. On an exec, the Linux
3519 kernel destroys all other threads (except the execing
3520 one) in the thread group, and resets the execing thread's
3521 tid to the tgid. No exit notification is sent for the
3522 execing thread -- from the ptracer's perspective, it
3523 appears as though the execing thread just vanishes.
3524 Until we reap all other threads except the leader and the
3525 execing thread, the leader will be zombie, and the
3526 execing thread will be in `D (disc sleep)'. As soon as
3527 all other threads are reaped, the execing thread changes
3528 it's tid to the tgid, and the previous (zombie) leader
3529 vanishes, giving place to the "new" leader. We could try
3530 distinguishing the exit and exec cases, by waiting once
3531 more, and seeing if something comes out, but it doesn't
3532 sound useful. The previous leader _does_ go away, and
3533 we'll re-add the new one once we see the exec event
3534 (which is just the same as what would happen if the
3535 previous leader did exit voluntarily before some other
3538 if (debug_linux_nat)
3539 fprintf_unfiltered (gdb_stdlog,
3540 "CZL: Thread group leader %d vanished.\n",
3542 exit_lwp (leader_lp);
3548 linux_nat_wait_1 (struct target_ops *ops,
3549 ptid_t ptid, struct target_waitstatus *ourstatus,
3552 static sigset_t prev_mask;
3553 enum resume_kind last_resume_kind;
3554 struct lwp_info *lp;
3557 if (debug_linux_nat)
3558 fprintf_unfiltered (gdb_stdlog, "LLW: enter\n");
3560 /* The first time we get here after starting a new inferior, we may
3561 not have added it to the LWP list yet - this is the earliest
3562 moment at which we know its PID. */
3563 if (ptid_is_pid (inferior_ptid))
3565 /* Upgrade the main thread's ptid. */
3566 thread_change_ptid (inferior_ptid,
3567 BUILD_LWP (GET_PID (inferior_ptid),
3568 GET_PID (inferior_ptid)));
3570 lp = add_lwp (inferior_ptid);
3574 /* Make sure SIGCHLD is blocked. */
3575 block_child_signals (&prev_mask);
3581 /* First check if there is a LWP with a wait status pending. */
3582 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
3584 /* Any LWP in the PTID group that's been resumed will do. */
3585 lp = iterate_over_lwps (ptid, status_callback, NULL);
3588 if (debug_linux_nat && lp->status)
3589 fprintf_unfiltered (gdb_stdlog,
3590 "LLW: Using pending wait status %s for %s.\n",
3591 status_to_str (lp->status),
3592 target_pid_to_str (lp->ptid));
3595 else if (is_lwp (ptid))
3597 if (debug_linux_nat)
3598 fprintf_unfiltered (gdb_stdlog,
3599 "LLW: Waiting for specific LWP %s.\n",
3600 target_pid_to_str (ptid));
3602 /* We have a specific LWP to check. */
3603 lp = find_lwp_pid (ptid);
3606 if (debug_linux_nat && lp->status)
3607 fprintf_unfiltered (gdb_stdlog,
3608 "LLW: Using pending wait status %s for %s.\n",
3609 status_to_str (lp->status),
3610 target_pid_to_str (lp->ptid));
3612 /* We check for lp->waitstatus in addition to lp->status,
3613 because we can have pending process exits recorded in
3614 lp->status and W_EXITCODE(0,0) == 0. We should probably have
3615 an additional lp->status_p flag. */
3616 if (lp->status == 0 && lp->waitstatus.kind == TARGET_WAITKIND_IGNORE)
3620 if (lp && lp->signalled && lp->last_resume_kind != resume_stop)
3622 /* A pending SIGSTOP may interfere with the normal stream of
3623 events. In a typical case where interference is a problem,
3624 we have a SIGSTOP signal pending for LWP A while
3625 single-stepping it, encounter an event in LWP B, and take the
3626 pending SIGSTOP while trying to stop LWP A. After processing
3627 the event in LWP B, LWP A is continued, and we'll never see
3628 the SIGTRAP associated with the last time we were
3629 single-stepping LWP A. */
3631 /* Resume the thread. It should halt immediately returning the
3633 registers_changed ();
3634 if (linux_nat_prepare_to_resume != NULL)
3635 linux_nat_prepare_to_resume (lp);
3636 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
3637 lp->step, TARGET_SIGNAL_0);
3638 if (debug_linux_nat)
3639 fprintf_unfiltered (gdb_stdlog,
3640 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
3641 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3642 target_pid_to_str (lp->ptid));
3644 gdb_assert (lp->resumed);
3646 /* Catch the pending SIGSTOP. */
3647 status = lp->status;
3650 stop_wait_callback (lp, NULL);
3652 /* If the lp->status field isn't empty, we caught another signal
3653 while flushing the SIGSTOP. Return it back to the event
3654 queue of the LWP, as we already have an event to handle. */
3657 if (debug_linux_nat)
3658 fprintf_unfiltered (gdb_stdlog,
3659 "LLW: kill %s, %s\n",
3660 target_pid_to_str (lp->ptid),
3661 status_to_str (lp->status));
3662 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
3665 lp->status = status;
3668 if (!target_can_async_p ())
3670 /* Causes SIGINT to be passed on to the attached process. */
3674 /* But if we don't find a pending event, we'll have to wait. */
3680 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3683 - If the thread group leader exits while other threads in the
3684 thread group still exist, waitpid(TGID, ...) hangs. That
3685 waitpid won't return an exit status until the other threads
3686 in the group are reapped.
3688 - When a non-leader thread execs, that thread just vanishes
3689 without reporting an exit (so we'd hang if we waited for it
3690 explicitly in that case). The exec event is reported to
3694 lwpid = my_waitpid (-1, &status, __WCLONE | WNOHANG);
3695 if (lwpid == 0 || (lwpid == -1 && errno == ECHILD))
3696 lwpid = my_waitpid (-1, &status, WNOHANG);
3698 if (debug_linux_nat)
3699 fprintf_unfiltered (gdb_stdlog,
3700 "LNW: waitpid(-1, ...) returned %d, %s\n",
3701 lwpid, errno ? safe_strerror (errno) : "ERRNO-OK");
3705 /* If this is true, then we paused LWPs momentarily, and may
3706 now have pending events to handle. */
3709 if (debug_linux_nat)
3711 fprintf_unfiltered (gdb_stdlog,
3712 "LLW: waitpid %ld received %s\n",
3713 (long) lwpid, status_to_str (status));
3716 lp = linux_nat_filter_event (lwpid, status, &new_pending);
3718 /* STATUS is now no longer valid, use LP->STATUS instead. */
3721 if (lp && !ptid_match (lp->ptid, ptid))
3723 gdb_assert (lp->resumed);
3725 if (debug_linux_nat)
3727 "LWP %ld got an event %06x, leaving pending.\n",
3728 ptid_get_lwp (lp->ptid), lp->status);
3730 if (WIFSTOPPED (lp->status))
3732 if (WSTOPSIG (lp->status) != SIGSTOP)
3734 /* Cancel breakpoint hits. The breakpoint may
3735 be removed before we fetch events from this
3736 process to report to the core. It is best
3737 not to assume the moribund breakpoints
3738 heuristic always handles these cases --- it
3739 could be too many events go through to the
3740 core before this one is handled. All-stop
3741 always cancels breakpoint hits in all
3744 && linux_nat_lp_status_is_event (lp)
3745 && cancel_breakpoint (lp))
3747 /* Throw away the SIGTRAP. */
3750 if (debug_linux_nat)
3752 "LLW: LWP %ld hit a breakpoint while"
3753 " waiting for another process;"
3755 ptid_get_lwp (lp->ptid));
3765 else if (WIFEXITED (lp->status) || WIFSIGNALED (lp->status))
3767 if (debug_linux_nat)
3769 "Process %ld exited while stopping LWPs\n",
3770 ptid_get_lwp (lp->ptid));
3772 /* This was the last lwp in the process. Since
3773 events are serialized to GDB core, and we can't
3774 report this one right now, but GDB core and the
3775 other target layers will want to be notified
3776 about the exit code/signal, leave the status
3777 pending for the next time we're able to report
3780 /* Prevent trying to stop this thread again. We'll
3781 never try to resume it because it has a pending
3785 /* Dead LWP's aren't expected to reported a pending
3789 /* Store the pending event in the waitstatus as
3790 well, because W_EXITCODE(0,0) == 0. */
3791 store_waitstatus (&lp->waitstatus, lp->status);
3800 /* Some LWP now has a pending event. Go all the way
3801 back to check it. */
3807 /* We got an event to report to the core. */
3811 /* Retry until nothing comes out of waitpid. A single
3812 SIGCHLD can indicate more than one child stopped. */
3816 /* Check for zombie thread group leaders. Those can't be reaped
3817 until all other threads in the thread group are. */
3818 check_zombie_leaders ();
3820 /* If there are no resumed children left, bail. We'd be stuck
3821 forever in the sigsuspend call below otherwise. */
3822 if (iterate_over_lwps (ptid, resumed_callback, NULL) == NULL)
3824 if (debug_linux_nat)
3825 fprintf_unfiltered (gdb_stdlog, "LLW: exit (no resumed LWP)\n");
3827 ourstatus->kind = TARGET_WAITKIND_NO_RESUMED;
3829 if (!target_can_async_p ())
3830 clear_sigint_trap ();
3832 restore_child_signals_mask (&prev_mask);
3833 return minus_one_ptid;
3836 /* No interesting event to report to the core. */
3838 if (target_options & TARGET_WNOHANG)
3840 if (debug_linux_nat)
3841 fprintf_unfiltered (gdb_stdlog, "LLW: exit (ignore)\n");
3843 ourstatus->kind = TARGET_WAITKIND_IGNORE;
3844 restore_child_signals_mask (&prev_mask);
3845 return minus_one_ptid;
3848 /* We shouldn't end up here unless we want to try again. */
3849 gdb_assert (lp == NULL);
3851 /* Block until we get an event reported with SIGCHLD. */
3852 sigsuspend (&suspend_mask);
3855 if (!target_can_async_p ())
3856 clear_sigint_trap ();
3860 status = lp->status;
3863 /* Don't report signals that GDB isn't interested in, such as
3864 signals that are neither printed nor stopped upon. Stopping all
3865 threads can be a bit time-consuming so if we want decent
3866 performance with heavily multi-threaded programs, especially when
3867 they're using a high frequency timer, we'd better avoid it if we
3870 if (WIFSTOPPED (status))
3872 enum target_signal signo = target_signal_from_host (WSTOPSIG (status));
3874 /* When using hardware single-step, we need to report every signal.
3875 Otherwise, signals in pass_mask may be short-circuited. */
3877 && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status)))
3879 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3880 here? It is not clear we should. GDB may not expect
3881 other threads to run. On the other hand, not resuming
3882 newly attached threads may cause an unwanted delay in
3883 getting them running. */
3884 registers_changed ();
3885 if (linux_nat_prepare_to_resume != NULL)
3886 linux_nat_prepare_to_resume (lp);
3887 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
3889 if (debug_linux_nat)
3890 fprintf_unfiltered (gdb_stdlog,
3891 "LLW: %s %s, %s (preempt 'handle')\n",
3893 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3894 target_pid_to_str (lp->ptid),
3895 (signo != TARGET_SIGNAL_0
3896 ? strsignal (target_signal_to_host (signo))
3904 /* Only do the below in all-stop, as we currently use SIGINT
3905 to implement target_stop (see linux_nat_stop) in
3907 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
3909 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3910 forwarded to the entire process group, that is, all LWPs
3911 will receive it - unless they're using CLONE_THREAD to
3912 share signals. Since we only want to report it once, we
3913 mark it as ignored for all LWPs except this one. */
3914 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid)),
3915 set_ignore_sigint, NULL);
3916 lp->ignore_sigint = 0;
3919 maybe_clear_ignore_sigint (lp);
3923 /* This LWP is stopped now. */
3926 if (debug_linux_nat)
3927 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
3928 status_to_str (status), target_pid_to_str (lp->ptid));
3932 /* Now stop all other LWP's ... */
3933 iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
3935 /* ... and wait until all of them have reported back that
3936 they're no longer running. */
3937 iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
3939 /* If we're not waiting for a specific LWP, choose an event LWP
3940 from among those that have had events. Giving equal priority
3941 to all LWPs that have had events helps prevent
3943 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
3944 select_event_lwp (ptid, &lp, &status);
3946 /* Now that we've selected our final event LWP, cancel any
3947 breakpoints in other LWPs that have hit a GDB breakpoint.
3948 See the comment in cancel_breakpoints_callback to find out
3950 iterate_over_lwps (minus_one_ptid, cancel_breakpoints_callback, lp);
3952 /* We'll need this to determine whether to report a SIGSTOP as
3953 TARGET_WAITKIND_0. Need to take a copy because
3954 resume_clear_callback clears it. */
3955 last_resume_kind = lp->last_resume_kind;
3957 /* In all-stop, from the core's perspective, all LWPs are now
3958 stopped until a new resume action is sent over. */
3959 iterate_over_lwps (minus_one_ptid, resume_clear_callback, NULL);
3964 last_resume_kind = lp->last_resume_kind;
3965 resume_clear_callback (lp, NULL);
3968 if (linux_nat_status_is_event (status))
3970 if (debug_linux_nat)
3971 fprintf_unfiltered (gdb_stdlog,
3972 "LLW: trap ptid is %s.\n",
3973 target_pid_to_str (lp->ptid));
3976 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3978 *ourstatus = lp->waitstatus;
3979 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
3982 store_waitstatus (ourstatus, status);
3984 if (debug_linux_nat)
3985 fprintf_unfiltered (gdb_stdlog, "LLW: exit\n");
3987 restore_child_signals_mask (&prev_mask);
3989 if (last_resume_kind == resume_stop
3990 && ourstatus->kind == TARGET_WAITKIND_STOPPED
3991 && WSTOPSIG (status) == SIGSTOP)
3993 /* A thread that has been requested to stop by GDB with
3994 target_stop, and it stopped cleanly, so report as SIG0. The
3995 use of SIGSTOP is an implementation detail. */
3996 ourstatus->value.sig = TARGET_SIGNAL_0;
3999 if (ourstatus->kind == TARGET_WAITKIND_EXITED
4000 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
4003 lp->core = linux_nat_core_of_thread_1 (lp->ptid);
4008 /* Resume LWPs that are currently stopped without any pending status
4009 to report, but are resumed from the core's perspective. */
4012 resume_stopped_resumed_lwps (struct lwp_info *lp, void *data)
4014 ptid_t *wait_ptid_p = data;
4019 && lp->waitstatus.kind == TARGET_WAITKIND_IGNORE)
4021 struct regcache *regcache = get_thread_regcache (lp->ptid);
4022 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4023 CORE_ADDR pc = regcache_read_pc (regcache);
4025 gdb_assert (is_executing (lp->ptid));
4027 /* Don't bother if there's a breakpoint at PC that we'd hit
4028 immediately, and we're not waiting for this LWP. */
4029 if (!ptid_match (lp->ptid, *wait_ptid_p))
4031 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
4035 if (debug_linux_nat)
4036 fprintf_unfiltered (gdb_stdlog,
4037 "RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
4038 target_pid_to_str (lp->ptid),
4039 paddress (gdbarch, pc),
4042 registers_changed ();
4043 if (linux_nat_prepare_to_resume != NULL)
4044 linux_nat_prepare_to_resume (lp);
4045 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
4046 lp->step, TARGET_SIGNAL_0);
4048 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
4049 lp->stopped_by_watchpoint = 0;
4056 linux_nat_wait (struct target_ops *ops,
4057 ptid_t ptid, struct target_waitstatus *ourstatus,
4062 if (debug_linux_nat)
4063 fprintf_unfiltered (gdb_stdlog,
4064 "linux_nat_wait: [%s]\n", target_pid_to_str (ptid));
4066 /* Flush the async file first. */
4067 if (target_can_async_p ())
4068 async_file_flush ();
4070 /* Resume LWPs that are currently stopped without any pending status
4071 to report, but are resumed from the core's perspective. LWPs get
4072 in this state if we find them stopping at a time we're not
4073 interested in reporting the event (target_wait on a
4074 specific_process, for example, see linux_nat_wait_1), and
4075 meanwhile the event became uninteresting. Don't bother resuming
4076 LWPs we're not going to wait for if they'd stop immediately. */
4078 iterate_over_lwps (minus_one_ptid, resume_stopped_resumed_lwps, &ptid);
4080 event_ptid = linux_nat_wait_1 (ops, ptid, ourstatus, target_options);
4082 /* If we requested any event, and something came out, assume there
4083 may be more. If we requested a specific lwp or process, also
4084 assume there may be more. */
4085 if (target_can_async_p ()
4086 && ((ourstatus->kind != TARGET_WAITKIND_IGNORE
4087 && ourstatus->kind != TARGET_WAITKIND_NO_RESUMED)
4088 || !ptid_equal (ptid, minus_one_ptid)))
4091 /* Get ready for the next event. */
4092 if (target_can_async_p ())
4093 target_async (inferior_event_handler, 0);
4099 kill_callback (struct lwp_info *lp, void *data)
4101 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
4104 kill (GET_LWP (lp->ptid), SIGKILL);
4105 if (debug_linux_nat)
4106 fprintf_unfiltered (gdb_stdlog,
4107 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
4108 target_pid_to_str (lp->ptid),
4109 errno ? safe_strerror (errno) : "OK");
4111 /* Some kernels ignore even SIGKILL for processes under ptrace. */
4114 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
4115 if (debug_linux_nat)
4116 fprintf_unfiltered (gdb_stdlog,
4117 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
4118 target_pid_to_str (lp->ptid),
4119 errno ? safe_strerror (errno) : "OK");
4125 kill_wait_callback (struct lwp_info *lp, void *data)
4129 /* We must make sure that there are no pending events (delayed
4130 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
4131 program doesn't interfere with any following debugging session. */
4133 /* For cloned processes we must check both with __WCLONE and
4134 without, since the exit status of a cloned process isn't reported
4140 pid = my_waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
4141 if (pid != (pid_t) -1)
4143 if (debug_linux_nat)
4144 fprintf_unfiltered (gdb_stdlog,
4145 "KWC: wait %s received unknown.\n",
4146 target_pid_to_str (lp->ptid));
4147 /* The Linux kernel sometimes fails to kill a thread
4148 completely after PTRACE_KILL; that goes from the stop
4149 point in do_fork out to the one in
4150 get_signal_to_deliever and waits again. So kill it
4152 kill_callback (lp, NULL);
4155 while (pid == GET_LWP (lp->ptid));
4157 gdb_assert (pid == -1 && errno == ECHILD);
4162 pid = my_waitpid (GET_LWP (lp->ptid), NULL, 0);
4163 if (pid != (pid_t) -1)
4165 if (debug_linux_nat)
4166 fprintf_unfiltered (gdb_stdlog,
4167 "KWC: wait %s received unk.\n",
4168 target_pid_to_str (lp->ptid));
4169 /* See the call to kill_callback above. */
4170 kill_callback (lp, NULL);
4173 while (pid == GET_LWP (lp->ptid));
4175 gdb_assert (pid == -1 && errno == ECHILD);
4180 linux_nat_kill (struct target_ops *ops)
4182 struct target_waitstatus last;
4186 /* If we're stopped while forking and we haven't followed yet,
4187 kill the other task. We need to do this first because the
4188 parent will be sleeping if this is a vfork. */
4190 get_last_target_status (&last_ptid, &last);
4192 if (last.kind == TARGET_WAITKIND_FORKED
4193 || last.kind == TARGET_WAITKIND_VFORKED)
4195 ptrace (PT_KILL, PIDGET (last.value.related_pid), 0, 0);
4199 if (forks_exist_p ())
4200 linux_fork_killall ();
4203 ptid_t ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
4205 /* Stop all threads before killing them, since ptrace requires
4206 that the thread is stopped to sucessfully PTRACE_KILL. */
4207 iterate_over_lwps (ptid, stop_callback, NULL);
4208 /* ... and wait until all of them have reported back that
4209 they're no longer running. */
4210 iterate_over_lwps (ptid, stop_wait_callback, NULL);
4212 /* Kill all LWP's ... */
4213 iterate_over_lwps (ptid, kill_callback, NULL);
4215 /* ... and wait until we've flushed all events. */
4216 iterate_over_lwps (ptid, kill_wait_callback, NULL);
4219 target_mourn_inferior ();
4223 linux_nat_mourn_inferior (struct target_ops *ops)
4225 purge_lwp_list (ptid_get_pid (inferior_ptid));
4227 if (! forks_exist_p ())
4228 /* Normal case, no other forks available. */
4229 linux_ops->to_mourn_inferior (ops);
4231 /* Multi-fork case. The current inferior_ptid has exited, but
4232 there are other viable forks to debug. Delete the exiting
4233 one and context-switch to the first available. */
4234 linux_fork_mourn_inferior ();
4237 /* Convert a native/host siginfo object, into/from the siginfo in the
4238 layout of the inferiors' architecture. */
4241 siginfo_fixup (struct siginfo *siginfo, gdb_byte *inf_siginfo, int direction)
4245 if (linux_nat_siginfo_fixup != NULL)
4246 done = linux_nat_siginfo_fixup (siginfo, inf_siginfo, direction);
4248 /* If there was no callback, or the callback didn't do anything,
4249 then just do a straight memcpy. */
4253 memcpy (siginfo, inf_siginfo, sizeof (struct siginfo));
4255 memcpy (inf_siginfo, siginfo, sizeof (struct siginfo));
4260 linux_xfer_siginfo (struct target_ops *ops, enum target_object object,
4261 const char *annex, gdb_byte *readbuf,
4262 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4265 struct siginfo siginfo;
4266 gdb_byte inf_siginfo[sizeof (struct siginfo)];
4268 gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO);
4269 gdb_assert (readbuf || writebuf);
4271 pid = GET_LWP (inferior_ptid);
4273 pid = GET_PID (inferior_ptid);
4275 if (offset > sizeof (siginfo))
4279 ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
4283 /* When GDB is built as a 64-bit application, ptrace writes into
4284 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
4285 inferior with a 64-bit GDB should look the same as debugging it
4286 with a 32-bit GDB, we need to convert it. GDB core always sees
4287 the converted layout, so any read/write will have to be done
4289 siginfo_fixup (&siginfo, inf_siginfo, 0);
4291 if (offset + len > sizeof (siginfo))
4292 len = sizeof (siginfo) - offset;
4294 if (readbuf != NULL)
4295 memcpy (readbuf, inf_siginfo + offset, len);
4298 memcpy (inf_siginfo + offset, writebuf, len);
4300 /* Convert back to ptrace layout before flushing it out. */
4301 siginfo_fixup (&siginfo, inf_siginfo, 1);
4304 ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
4313 linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
4314 const char *annex, gdb_byte *readbuf,
4315 const gdb_byte *writebuf,
4316 ULONGEST offset, LONGEST len)
4318 struct cleanup *old_chain;
4321 if (object == TARGET_OBJECT_SIGNAL_INFO)
4322 return linux_xfer_siginfo (ops, object, annex, readbuf, writebuf,
4325 /* The target is connected but no live inferior is selected. Pass
4326 this request down to a lower stratum (e.g., the executable
4328 if (object == TARGET_OBJECT_MEMORY && ptid_equal (inferior_ptid, null_ptid))
4331 old_chain = save_inferior_ptid ();
4333 if (is_lwp (inferior_ptid))
4334 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
4336 xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
4339 do_cleanups (old_chain);
4344 linux_thread_alive (ptid_t ptid)
4348 gdb_assert (is_lwp (ptid));
4350 /* Send signal 0 instead of anything ptrace, because ptracing a
4351 running thread errors out claiming that the thread doesn't
4353 err = kill_lwp (GET_LWP (ptid), 0);
4355 if (debug_linux_nat)
4356 fprintf_unfiltered (gdb_stdlog,
4357 "LLTA: KILL(SIG0) %s (%s)\n",
4358 target_pid_to_str (ptid),
4359 err ? safe_strerror (tmp_errno) : "OK");
4368 linux_nat_thread_alive (struct target_ops *ops, ptid_t ptid)
4370 return linux_thread_alive (ptid);
4374 linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
4376 static char buf[64];
4379 && (GET_PID (ptid) != GET_LWP (ptid)
4380 || num_lwps (GET_PID (ptid)) > 1))
4382 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
4386 return normal_pid_to_str (ptid);
4390 linux_nat_thread_name (struct thread_info *thr)
4392 int pid = ptid_get_pid (thr->ptid);
4393 long lwp = ptid_get_lwp (thr->ptid);
4394 #define FORMAT "/proc/%d/task/%ld/comm"
4395 char buf[sizeof (FORMAT) + 30];
4397 char *result = NULL;
4399 snprintf (buf, sizeof (buf), FORMAT, pid, lwp);
4400 comm_file = fopen (buf, "r");
4403 /* Not exported by the kernel, so we define it here. */
4405 static char line[COMM_LEN + 1];
4407 if (fgets (line, sizeof (line), comm_file))
4409 char *nl = strchr (line, '\n');
4426 /* Accepts an integer PID; Returns a string representing a file that
4427 can be opened to get the symbols for the child process. */
4430 linux_child_pid_to_exec_file (int pid)
4432 char *name1, *name2;
4434 name1 = xmalloc (MAXPATHLEN);
4435 name2 = xmalloc (MAXPATHLEN);
4436 make_cleanup (xfree, name1);
4437 make_cleanup (xfree, name2);
4438 memset (name2, 0, MAXPATHLEN);
4440 sprintf (name1, "/proc/%d/exe", pid);
4441 if (readlink (name1, name2, MAXPATHLEN) > 0)
4447 /* Records the thread's register state for the corefile note
4451 linux_nat_collect_thread_registers (const struct regcache *regcache,
4452 ptid_t ptid, bfd *obfd,
4453 char *note_data, int *note_size,
4454 enum target_signal stop_signal)
4456 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4457 const struct regset *regset;
4459 gdb_gregset_t gregs;
4460 gdb_fpregset_t fpregs;
4462 core_regset_p = gdbarch_regset_from_core_section_p (gdbarch);
4465 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg",
4467 != NULL && regset->collect_regset != NULL)
4468 regset->collect_regset (regset, regcache, -1, &gregs, sizeof (gregs));
4470 fill_gregset (regcache, &gregs, -1);
4472 note_data = (char *) elfcore_write_prstatus
4473 (obfd, note_data, note_size, ptid_get_lwp (ptid),
4474 target_signal_to_host (stop_signal), &gregs);
4477 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg2",
4479 != NULL && regset->collect_regset != NULL)
4480 regset->collect_regset (regset, regcache, -1, &fpregs, sizeof (fpregs));
4482 fill_fpregset (regcache, &fpregs, -1);
4484 note_data = (char *) elfcore_write_prfpreg (obfd, note_data, note_size,
4485 &fpregs, sizeof (fpregs));
4490 /* Fills the "to_make_corefile_note" target vector. Builds the note
4491 section for a corefile, and returns it in a malloc buffer. */
4494 linux_nat_make_corefile_notes (bfd *obfd, int *note_size)
4496 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
4497 converted to gdbarch_core_regset_sections, this function can go away. */
4498 return linux_make_corefile_notes (target_gdbarch, obfd, note_size,
4499 linux_nat_collect_thread_registers);
4502 /* Implement the to_xfer_partial interface for memory reads using the /proc
4503 filesystem. Because we can use a single read() call for /proc, this
4504 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4505 but it doesn't support writes. */
4508 linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
4509 const char *annex, gdb_byte *readbuf,
4510 const gdb_byte *writebuf,
4511 ULONGEST offset, LONGEST len)
4517 if (object != TARGET_OBJECT_MEMORY || !readbuf)
4520 /* Don't bother for one word. */
4521 if (len < 3 * sizeof (long))
4524 /* We could keep this file open and cache it - possibly one per
4525 thread. That requires some juggling, but is even faster. */
4526 sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid));
4527 fd = open (filename, O_RDONLY | O_LARGEFILE);
4531 /* If pread64 is available, use it. It's faster if the kernel
4532 supports it (only one syscall), and it's 64-bit safe even on
4533 32-bit platforms (for instance, SPARC debugging a SPARC64
4536 if (pread64 (fd, readbuf, len, offset) != len)
4538 if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
4549 /* Enumerate spufs IDs for process PID. */
4551 spu_enumerate_spu_ids (int pid, gdb_byte *buf, ULONGEST offset, LONGEST len)
4553 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
4555 LONGEST written = 0;
4558 struct dirent *entry;
4560 xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
4561 dir = opendir (path);
4566 while ((entry = readdir (dir)) != NULL)
4572 fd = atoi (entry->d_name);
4576 xsnprintf (path, sizeof path, "/proc/%d/fd/%d", pid, fd);
4577 if (stat (path, &st) != 0)
4579 if (!S_ISDIR (st.st_mode))
4582 if (statfs (path, &stfs) != 0)
4584 if (stfs.f_type != SPUFS_MAGIC)
4587 if (pos >= offset && pos + 4 <= offset + len)
4589 store_unsigned_integer (buf + pos - offset, 4, byte_order, fd);
4599 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4600 object type, using the /proc file system. */
4602 linux_proc_xfer_spu (struct target_ops *ops, enum target_object object,
4603 const char *annex, gdb_byte *readbuf,
4604 const gdb_byte *writebuf,
4605 ULONGEST offset, LONGEST len)
4610 int pid = PIDGET (inferior_ptid);
4617 return spu_enumerate_spu_ids (pid, readbuf, offset, len);
4620 xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
4621 fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
4626 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
4633 ret = write (fd, writebuf, (size_t) len);
4635 ret = read (fd, readbuf, (size_t) len);
4642 /* Parse LINE as a signal set and add its set bits to SIGS. */
4645 add_line_to_sigset (const char *line, sigset_t *sigs)
4647 int len = strlen (line) - 1;
4651 if (line[len] != '\n')
4652 error (_("Could not parse signal set: %s"), line);
4660 if (*p >= '0' && *p <= '9')
4662 else if (*p >= 'a' && *p <= 'f')
4663 digit = *p - 'a' + 10;
4665 error (_("Could not parse signal set: %s"), line);
4670 sigaddset (sigs, signum + 1);
4672 sigaddset (sigs, signum + 2);
4674 sigaddset (sigs, signum + 3);
4676 sigaddset (sigs, signum + 4);
4682 /* Find process PID's pending signals from /proc/pid/status and set
4686 linux_proc_pending_signals (int pid, sigset_t *pending,
4687 sigset_t *blocked, sigset_t *ignored)
4690 char buffer[MAXPATHLEN], fname[MAXPATHLEN];
4691 struct cleanup *cleanup;
4693 sigemptyset (pending);
4694 sigemptyset (blocked);
4695 sigemptyset (ignored);
4696 sprintf (fname, "/proc/%d/status", pid);
4697 procfile = fopen (fname, "r");
4698 if (procfile == NULL)
4699 error (_("Could not open %s"), fname);
4700 cleanup = make_cleanup_fclose (procfile);
4702 while (fgets (buffer, MAXPATHLEN, procfile) != NULL)
4704 /* Normal queued signals are on the SigPnd line in the status
4705 file. However, 2.6 kernels also have a "shared" pending
4706 queue for delivering signals to a thread group, so check for
4709 Unfortunately some Red Hat kernels include the shared pending
4710 queue but not the ShdPnd status field. */
4712 if (strncmp (buffer, "SigPnd:\t", 8) == 0)
4713 add_line_to_sigset (buffer + 8, pending);
4714 else if (strncmp (buffer, "ShdPnd:\t", 8) == 0)
4715 add_line_to_sigset (buffer + 8, pending);
4716 else if (strncmp (buffer, "SigBlk:\t", 8) == 0)
4717 add_line_to_sigset (buffer + 8, blocked);
4718 else if (strncmp (buffer, "SigIgn:\t", 8) == 0)
4719 add_line_to_sigset (buffer + 8, ignored);
4722 do_cleanups (cleanup);
4726 linux_nat_xfer_osdata (struct target_ops *ops, enum target_object object,
4727 const char *annex, gdb_byte *readbuf,
4728 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4730 gdb_assert (object == TARGET_OBJECT_OSDATA);
4732 return linux_common_xfer_osdata (annex, readbuf, offset, len);
4736 linux_xfer_partial (struct target_ops *ops, enum target_object object,
4737 const char *annex, gdb_byte *readbuf,
4738 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4742 if (object == TARGET_OBJECT_AUXV)
4743 return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
4746 if (object == TARGET_OBJECT_OSDATA)
4747 return linux_nat_xfer_osdata (ops, object, annex, readbuf, writebuf,
4750 if (object == TARGET_OBJECT_SPU)
4751 return linux_proc_xfer_spu (ops, object, annex, readbuf, writebuf,
4754 /* GDB calculates all the addresses in possibly larget width of the address.
4755 Address width needs to be masked before its final use - either by
4756 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4758 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4760 if (object == TARGET_OBJECT_MEMORY)
4762 int addr_bit = gdbarch_addr_bit (target_gdbarch);
4764 if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
4765 offset &= ((ULONGEST) 1 << addr_bit) - 1;
4768 xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
4773 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
4778 cleanup_target_stop (void *arg)
4780 ptid_t *ptid = (ptid_t *) arg;
4782 gdb_assert (arg != NULL);
4785 target_resume (*ptid, 0, TARGET_SIGNAL_0);
4788 static VEC(static_tracepoint_marker_p) *
4789 linux_child_static_tracepoint_markers_by_strid (const char *strid)
4791 char s[IPA_CMD_BUF_SIZE];
4792 struct cleanup *old_chain;
4793 int pid = ptid_get_pid (inferior_ptid);
4794 VEC(static_tracepoint_marker_p) *markers = NULL;
4795 struct static_tracepoint_marker *marker = NULL;
4797 ptid_t ptid = ptid_build (pid, 0, 0);
4802 memcpy (s, "qTfSTM", sizeof ("qTfSTM"));
4803 s[sizeof ("qTfSTM")] = 0;
4805 agent_run_command (pid, s);
4807 old_chain = make_cleanup (free_current_marker, &marker);
4808 make_cleanup (cleanup_target_stop, &ptid);
4813 marker = XCNEW (struct static_tracepoint_marker);
4817 parse_static_tracepoint_marker_definition (p, &p, marker);
4819 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
4821 VEC_safe_push (static_tracepoint_marker_p,
4827 release_static_tracepoint_marker (marker);
4828 memset (marker, 0, sizeof (*marker));
4831 while (*p++ == ','); /* comma-separated list */
4833 memcpy (s, "qTsSTM", sizeof ("qTsSTM"));
4834 s[sizeof ("qTsSTM")] = 0;
4835 agent_run_command (pid, s);
4839 do_cleanups (old_chain);
4844 /* Create a prototype generic GNU/Linux target. The client can override
4845 it with local methods. */
4848 linux_target_install_ops (struct target_ops *t)
4850 t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
4851 t->to_remove_fork_catchpoint = linux_child_remove_fork_catchpoint;
4852 t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
4853 t->to_remove_vfork_catchpoint = linux_child_remove_vfork_catchpoint;
4854 t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
4855 t->to_remove_exec_catchpoint = linux_child_remove_exec_catchpoint;
4856 t->to_set_syscall_catchpoint = linux_child_set_syscall_catchpoint;
4857 t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
4858 t->to_post_startup_inferior = linux_child_post_startup_inferior;
4859 t->to_post_attach = linux_child_post_attach;
4860 t->to_follow_fork = linux_child_follow_fork;
4861 t->to_make_corefile_notes = linux_nat_make_corefile_notes;
4863 super_xfer_partial = t->to_xfer_partial;
4864 t->to_xfer_partial = linux_xfer_partial;
4866 t->to_static_tracepoint_markers_by_strid
4867 = linux_child_static_tracepoint_markers_by_strid;
4873 struct target_ops *t;
4875 t = inf_ptrace_target ();
4876 linux_target_install_ops (t);
4882 linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
4884 struct target_ops *t;
4886 t = inf_ptrace_trad_target (register_u_offset);
4887 linux_target_install_ops (t);
4892 /* target_is_async_p implementation. */
4895 linux_nat_is_async_p (void)
4897 /* NOTE: palves 2008-03-21: We're only async when the user requests
4898 it explicitly with the "set target-async" command.
4899 Someday, linux will always be async. */
4900 return target_async_permitted;
4903 /* target_can_async_p implementation. */
4906 linux_nat_can_async_p (void)
4908 /* NOTE: palves 2008-03-21: We're only async when the user requests
4909 it explicitly with the "set target-async" command.
4910 Someday, linux will always be async. */
4911 return target_async_permitted;
4915 linux_nat_supports_non_stop (void)
4920 /* True if we want to support multi-process. To be removed when GDB
4921 supports multi-exec. */
4923 int linux_multi_process = 1;
4926 linux_nat_supports_multi_process (void)
4928 return linux_multi_process;
4932 linux_nat_supports_disable_randomization (void)
4934 #ifdef HAVE_PERSONALITY
4941 static int async_terminal_is_ours = 1;
4943 /* target_terminal_inferior implementation. */
4946 linux_nat_terminal_inferior (void)
4948 if (!target_is_async_p ())
4950 /* Async mode is disabled. */
4951 terminal_inferior ();
4955 terminal_inferior ();
4957 /* Calls to target_terminal_*() are meant to be idempotent. */
4958 if (!async_terminal_is_ours)
4961 delete_file_handler (input_fd);
4962 async_terminal_is_ours = 0;
4966 /* target_terminal_ours implementation. */
4969 linux_nat_terminal_ours (void)
4971 if (!target_is_async_p ())
4973 /* Async mode is disabled. */
4978 /* GDB should never give the terminal to the inferior if the
4979 inferior is running in the background (run&, continue&, etc.),
4980 but claiming it sure should. */
4983 if (async_terminal_is_ours)
4986 clear_sigint_trap ();
4987 add_file_handler (input_fd, stdin_event_handler, 0);
4988 async_terminal_is_ours = 1;
4991 static void (*async_client_callback) (enum inferior_event_type event_type,
4993 static void *async_client_context;
4995 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4996 so we notice when any child changes state, and notify the
4997 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4998 above to wait for the arrival of a SIGCHLD. */
5001 sigchld_handler (int signo)
5003 int old_errno = errno;
5005 if (debug_linux_nat)
5006 ui_file_write_async_safe (gdb_stdlog,
5007 "sigchld\n", sizeof ("sigchld\n") - 1);
5009 if (signo == SIGCHLD
5010 && linux_nat_event_pipe[0] != -1)
5011 async_file_mark (); /* Let the event loop know that there are
5012 events to handle. */
5017 /* Callback registered with the target events file descriptor. */
5020 handle_target_event (int error, gdb_client_data client_data)
5022 (*async_client_callback) (INF_REG_EVENT, async_client_context);
5025 /* Create/destroy the target events pipe. Returns previous state. */
5028 linux_async_pipe (int enable)
5030 int previous = (linux_nat_event_pipe[0] != -1);
5032 if (previous != enable)
5036 block_child_signals (&prev_mask);
5040 if (pipe (linux_nat_event_pipe) == -1)
5041 internal_error (__FILE__, __LINE__,
5042 "creating event pipe failed.");
5044 fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
5045 fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
5049 close (linux_nat_event_pipe[0]);
5050 close (linux_nat_event_pipe[1]);
5051 linux_nat_event_pipe[0] = -1;
5052 linux_nat_event_pipe[1] = -1;
5055 restore_child_signals_mask (&prev_mask);
5061 /* target_async implementation. */
5064 linux_nat_async (void (*callback) (enum inferior_event_type event_type,
5065 void *context), void *context)
5067 if (callback != NULL)
5069 async_client_callback = callback;
5070 async_client_context = context;
5071 if (!linux_async_pipe (1))
5073 add_file_handler (linux_nat_event_pipe[0],
5074 handle_target_event, NULL);
5075 /* There may be pending events to handle. Tell the event loop
5082 async_client_callback = callback;
5083 async_client_context = context;
5084 delete_file_handler (linux_nat_event_pipe[0]);
5085 linux_async_pipe (0);
5090 /* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
5094 linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
5098 ptid_t ptid = lwp->ptid;
5100 if (debug_linux_nat)
5101 fprintf_unfiltered (gdb_stdlog,
5102 "LNSL: running -> suspending %s\n",
5103 target_pid_to_str (lwp->ptid));
5106 if (lwp->last_resume_kind == resume_stop)
5108 if (debug_linux_nat)
5109 fprintf_unfiltered (gdb_stdlog,
5110 "linux-nat: already stopping LWP %ld at "
5112 ptid_get_lwp (lwp->ptid));
5116 stop_callback (lwp, NULL);
5117 lwp->last_resume_kind = resume_stop;
5121 /* Already known to be stopped; do nothing. */
5123 if (debug_linux_nat)
5125 if (find_thread_ptid (lwp->ptid)->stop_requested)
5126 fprintf_unfiltered (gdb_stdlog,
5127 "LNSL: already stopped/stop_requested %s\n",
5128 target_pid_to_str (lwp->ptid));
5130 fprintf_unfiltered (gdb_stdlog,
5131 "LNSL: already stopped/no "
5132 "stop_requested yet %s\n",
5133 target_pid_to_str (lwp->ptid));
5140 linux_nat_stop (ptid_t ptid)
5143 iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
5145 linux_ops->to_stop (ptid);
5149 linux_nat_close (int quitting)
5151 /* Unregister from the event loop. */
5152 if (linux_nat_is_async_p ())
5153 linux_nat_async (NULL, 0);
5155 if (linux_ops->to_close)
5156 linux_ops->to_close (quitting);
5159 /* When requests are passed down from the linux-nat layer to the
5160 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
5161 used. The address space pointer is stored in the inferior object,
5162 but the common code that is passed such ptid can't tell whether
5163 lwpid is a "main" process id or not (it assumes so). We reverse
5164 look up the "main" process id from the lwp here. */
5166 static struct address_space *
5167 linux_nat_thread_address_space (struct target_ops *t, ptid_t ptid)
5169 struct lwp_info *lwp;
5170 struct inferior *inf;
5173 pid = GET_LWP (ptid);
5174 if (GET_LWP (ptid) == 0)
5176 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
5178 lwp = find_lwp_pid (ptid);
5179 pid = GET_PID (lwp->ptid);
5183 /* A (pid,lwpid,0) ptid. */
5184 pid = GET_PID (ptid);
5187 inf = find_inferior_pid (pid);
5188 gdb_assert (inf != NULL);
5193 linux_nat_core_of_thread_1 (ptid_t ptid)
5195 struct cleanup *back_to;
5198 char *content = NULL;
5201 int content_read = 0;
5205 filename = xstrprintf ("/proc/%d/task/%ld/stat",
5206 GET_PID (ptid), GET_LWP (ptid));
5207 back_to = make_cleanup (xfree, filename);
5209 f = fopen (filename, "r");
5212 do_cleanups (back_to);
5216 make_cleanup_fclose (f);
5222 content = xrealloc (content, content_read + 1024);
5223 n = fread (content + content_read, 1, 1024, f);
5227 content[content_read] = '\0';
5232 make_cleanup (xfree, content);
5234 p = strchr (content, '(');
5238 p = strchr (p, ')');
5242 /* If the first field after program name has index 0, then core number is
5243 the field with index 36. There's no constant for that anywhere. */
5245 p = strtok_r (p, " ", &ts);
5246 for (i = 0; p != NULL && i != 36; ++i)
5247 p = strtok_r (NULL, " ", &ts);
5249 if (p == NULL || sscanf (p, "%d", &core) == 0)
5252 do_cleanups (back_to);
5257 /* Return the cached value of the processor core for thread PTID. */
5260 linux_nat_core_of_thread (struct target_ops *ops, ptid_t ptid)
5262 struct lwp_info *info = find_lwp_pid (ptid);
5270 linux_nat_add_target (struct target_ops *t)
5272 /* Save the provided single-threaded target. We save this in a separate
5273 variable because another target we've inherited from (e.g. inf-ptrace)
5274 may have saved a pointer to T; we want to use it for the final
5275 process stratum target. */
5276 linux_ops_saved = *t;
5277 linux_ops = &linux_ops_saved;
5279 /* Override some methods for multithreading. */
5280 t->to_create_inferior = linux_nat_create_inferior;
5281 t->to_attach = linux_nat_attach;
5282 t->to_detach = linux_nat_detach;
5283 t->to_resume = linux_nat_resume;
5284 t->to_wait = linux_nat_wait;
5285 t->to_pass_signals = linux_nat_pass_signals;
5286 t->to_xfer_partial = linux_nat_xfer_partial;
5287 t->to_kill = linux_nat_kill;
5288 t->to_mourn_inferior = linux_nat_mourn_inferior;
5289 t->to_thread_alive = linux_nat_thread_alive;
5290 t->to_pid_to_str = linux_nat_pid_to_str;
5291 t->to_thread_name = linux_nat_thread_name;
5292 t->to_has_thread_control = tc_schedlock;
5293 t->to_thread_address_space = linux_nat_thread_address_space;
5294 t->to_stopped_by_watchpoint = linux_nat_stopped_by_watchpoint;
5295 t->to_stopped_data_address = linux_nat_stopped_data_address;
5297 t->to_can_async_p = linux_nat_can_async_p;
5298 t->to_is_async_p = linux_nat_is_async_p;
5299 t->to_supports_non_stop = linux_nat_supports_non_stop;
5300 t->to_async = linux_nat_async;
5301 t->to_terminal_inferior = linux_nat_terminal_inferior;
5302 t->to_terminal_ours = linux_nat_terminal_ours;
5303 t->to_close = linux_nat_close;
5305 /* Methods for non-stop support. */
5306 t->to_stop = linux_nat_stop;
5308 t->to_supports_multi_process = linux_nat_supports_multi_process;
5310 t->to_supports_disable_randomization
5311 = linux_nat_supports_disable_randomization;
5313 t->to_core_of_thread = linux_nat_core_of_thread;
5315 /* We don't change the stratum; this target will sit at
5316 process_stratum and thread_db will set at thread_stratum. This
5317 is a little strange, since this is a multi-threaded-capable
5318 target, but we want to be on the stack below thread_db, and we
5319 also want to be used for single-threaded processes. */
5324 /* Register a method to call whenever a new thread is attached. */
5326 linux_nat_set_new_thread (struct target_ops *t,
5327 void (*new_thread) (struct lwp_info *))
5329 /* Save the pointer. We only support a single registered instance
5330 of the GNU/Linux native target, so we do not need to map this to
5332 linux_nat_new_thread = new_thread;
5335 /* Register a method that converts a siginfo object between the layout
5336 that ptrace returns, and the layout in the architecture of the
5339 linux_nat_set_siginfo_fixup (struct target_ops *t,
5340 int (*siginfo_fixup) (struct siginfo *,
5344 /* Save the pointer. */
5345 linux_nat_siginfo_fixup = siginfo_fixup;
5348 /* Register a method to call prior to resuming a thread. */
5351 linux_nat_set_prepare_to_resume (struct target_ops *t,
5352 void (*prepare_to_resume) (struct lwp_info *))
5354 /* Save the pointer. */
5355 linux_nat_prepare_to_resume = prepare_to_resume;
5358 /* Return the saved siginfo associated with PTID. */
5360 linux_nat_get_siginfo (ptid_t ptid)
5362 struct lwp_info *lp = find_lwp_pid (ptid);
5364 gdb_assert (lp != NULL);
5366 return &lp->siginfo;
5369 /* Provide a prototype to silence -Wmissing-prototypes. */
5370 extern initialize_file_ftype _initialize_linux_nat;
5373 _initialize_linux_nat (void)
5375 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance,
5376 &debug_linux_nat, _("\
5377 Set debugging of GNU/Linux lwp module."), _("\
5378 Show debugging of GNU/Linux lwp module."), _("\
5379 Enables printf debugging output."),
5381 show_debug_linux_nat,
5382 &setdebuglist, &showdebuglist);
5384 /* Save this mask as the default. */
5385 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
5387 /* Install a SIGCHLD handler. */
5388 sigchld_action.sa_handler = sigchld_handler;
5389 sigemptyset (&sigchld_action.sa_mask);
5390 sigchld_action.sa_flags = SA_RESTART;
5392 /* Make it the default. */
5393 sigaction (SIGCHLD, &sigchld_action, NULL);
5395 /* Make sure we don't block SIGCHLD during a sigsuspend. */
5396 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
5397 sigdelset (&suspend_mask, SIGCHLD);
5399 sigemptyset (&blocked_mask);
5403 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
5404 the GNU/Linux Threads library and therefore doesn't really belong
5407 /* Read variable NAME in the target and return its value if found.
5408 Otherwise return zero. It is assumed that the type of the variable
5412 get_signo (const char *name)
5414 struct minimal_symbol *ms;
5417 ms = lookup_minimal_symbol (name, NULL, NULL);
5421 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (gdb_byte *) &signo,
5422 sizeof (signo)) != 0)
5428 /* Return the set of signals used by the threads library in *SET. */
5431 lin_thread_get_thread_signals (sigset_t *set)
5433 struct sigaction action;
5434 int restart, cancel;
5436 sigemptyset (&blocked_mask);
5439 restart = get_signo ("__pthread_sig_restart");
5440 cancel = get_signo ("__pthread_sig_cancel");
5442 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5443 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5444 not provide any way for the debugger to query the signal numbers -
5445 fortunately they don't change! */
5448 restart = __SIGRTMIN;
5451 cancel = __SIGRTMIN + 1;
5453 sigaddset (set, restart);
5454 sigaddset (set, cancel);
5456 /* The GNU/Linux Threads library makes terminating threads send a
5457 special "cancel" signal instead of SIGCHLD. Make sure we catch
5458 those (to prevent them from terminating GDB itself, which is
5459 likely to be their default action) and treat them the same way as
5462 action.sa_handler = sigchld_handler;
5463 sigemptyset (&action.sa_mask);
5464 action.sa_flags = SA_RESTART;
5465 sigaction (cancel, &action, NULL);
5467 /* We block the "cancel" signal throughout this code ... */
5468 sigaddset (&blocked_mask, cancel);
5469 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
5471 /* ... except during a sigsuspend. */
5472 sigdelset (&suspend_mask, cancel);