1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "nat/linux-nat.h"
25 #include "nat/linux-waitpid.h"
28 #include <sys/syscall.h>
29 #include "nat/gdb_ptrace.h"
30 #include "linux-nat.h"
31 #include "nat/linux-ptrace.h"
32 #include "nat/linux-procfs.h"
33 #include "nat/linux-personality.h"
34 #include "linux-fork.h"
35 #include "gdbthread.h"
39 #include "inf-child.h"
40 #include "inf-ptrace.h"
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 <sys/stat.h> /* for struct stat */
48 #include <fcntl.h> /* for O_RDONLY */
50 #include "event-loop.h"
51 #include "event-top.h"
53 #include <sys/types.h>
55 #include "xml-support.h"
58 #include "nat/linux-osdata.h"
59 #include "linux-tdep.h"
62 #include "tracepoint.h"
64 #include "target-descriptions.h"
65 #include "filestuff.h"
67 #include "nat/linux-namespaces.h"
71 #define SPUFS_MAGIC 0x23c9b64e
74 /* This comment documents high-level logic of this file.
76 Waiting for events in sync mode
77 ===============================
79 When waiting for an event in a specific thread, we just use waitpid,
80 passing the specific pid, and not passing WNOHANG.
82 When waiting for an event in all threads, waitpid is not quite good:
84 - If the thread group leader exits while other threads in the thread
85 group still exist, waitpid(TGID, ...) hangs. That waitpid won't
86 return an exit status until the other threads in the group are
89 - When a non-leader thread execs, that thread just vanishes without
90 reporting an exit (so we'd hang if we waited for it explicitly in
91 that case). The exec event is instead reported to the TGID pid.
93 The solution is to always use -1 and WNOHANG, together with
96 First, we use non-blocking waitpid to check for events. If nothing is
97 found, we use sigsuspend to wait for SIGCHLD. When SIGCHLD arrives,
98 it means something happened to a child process. As soon as we know
99 there's an event, we get back to calling nonblocking waitpid.
101 Note that SIGCHLD should be blocked between waitpid and sigsuspend
102 calls, so that we don't miss a signal. If SIGCHLD arrives in between,
103 when it's blocked, the signal becomes pending and sigsuspend
104 immediately notices it and returns.
106 Waiting for events in async mode (TARGET_WNOHANG)
107 =================================================
109 In async mode, GDB should always be ready to handle both user input
110 and target events, so neither blocking waitpid nor sigsuspend are
111 viable options. Instead, we should asynchronously notify the GDB main
112 event loop whenever there's an unprocessed event from the target. We
113 detect asynchronous target events by handling SIGCHLD signals. To
114 notify the event loop about target events, the self-pipe trick is used
115 --- a pipe is registered as waitable event source in the event loop,
116 the event loop select/poll's on the read end of this pipe (as well on
117 other event sources, e.g., stdin), and the SIGCHLD handler writes a
118 byte to this pipe. This is more portable than relying on
119 pselect/ppoll, since on kernels that lack those syscalls, libc
120 emulates them with select/poll+sigprocmask, and that is racy
121 (a.k.a. plain broken).
123 Obviously, if we fail to notify the event loop if there's a target
124 event, it's bad. OTOH, if we notify the event loop when there's no
125 event from the target, linux_nat_wait will detect that there's no real
126 event to report, and return event of type TARGET_WAITKIND_IGNORE.
127 This is mostly harmless, but it will waste time and is better avoided.
129 The main design point is that every time GDB is outside linux-nat.c,
130 we have a SIGCHLD handler installed that is called when something
131 happens to the target and notifies the GDB event loop. Whenever GDB
132 core decides to handle the event, and calls into linux-nat.c, we
133 process things as in sync mode, except that the we never block in
136 While processing an event, we may end up momentarily blocked in
137 waitpid calls. Those waitpid calls, while blocking, are guarantied to
138 return quickly. E.g., in all-stop mode, before reporting to the core
139 that an LWP hit a breakpoint, all LWPs are stopped by sending them
140 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
141 Note that this is different from blocking indefinitely waiting for the
142 next event --- here, we're already handling an event.
147 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
148 signal is not entirely significant; we just need for a signal to be delivered,
149 so that we can intercept it. SIGSTOP's advantage is that it can not be
150 blocked. A disadvantage is that it is not a real-time signal, so it can only
151 be queued once; we do not keep track of other sources of SIGSTOP.
153 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
154 use them, because they have special behavior when the signal is generated -
155 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
156 kills the entire thread group.
158 A delivered SIGSTOP would stop the entire thread group, not just the thread we
159 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
160 cancel it (by PTRACE_CONT without passing SIGSTOP).
162 We could use a real-time signal instead. This would solve those problems; we
163 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
164 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
165 generates it, and there are races with trying to find a signal that is not
171 The case of a thread group (process) with 3 or more threads, and a
172 thread other than the leader execs is worth detailing:
174 On an exec, the Linux kernel destroys all threads except the execing
175 one in the thread group, and resets the execing thread's tid to the
176 tgid. No exit notification is sent for the execing thread -- from the
177 ptracer's perspective, it appears as though the execing thread just
178 vanishes. Until we reap all other threads except the leader and the
179 execing thread, the leader will be zombie, and the execing thread will
180 be in `D (disc sleep)' state. As soon as all other threads are
181 reaped, the execing thread changes its tid to the tgid, and the
182 previous (zombie) leader vanishes, giving place to the "new"
186 #define O_LARGEFILE 0
189 /* Does the current host support PTRACE_GETREGSET? */
190 enum tribool have_ptrace_getregset
= TRIBOOL_UNKNOWN
;
192 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
193 the use of the multi-threaded target. */
194 static struct target_ops
*linux_ops
;
195 static struct target_ops linux_ops_saved
;
197 /* The method to call, if any, when a new thread is attached. */
198 static void (*linux_nat_new_thread
) (struct lwp_info
*);
200 /* The method to call, if any, when a new fork is attached. */
201 static linux_nat_new_fork_ftype
*linux_nat_new_fork
;
203 /* The method to call, if any, when a process is no longer
205 static linux_nat_forget_process_ftype
*linux_nat_forget_process_hook
;
207 /* Hook to call prior to resuming a thread. */
208 static void (*linux_nat_prepare_to_resume
) (struct lwp_info
*);
210 /* The method to call, if any, when the siginfo object needs to be
211 converted between the layout returned by ptrace, and the layout in
212 the architecture of the inferior. */
213 static int (*linux_nat_siginfo_fixup
) (siginfo_t
*,
217 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
218 Called by our to_xfer_partial. */
219 static target_xfer_partial_ftype
*super_xfer_partial
;
221 /* The saved to_close method, inherited from inf-ptrace.c.
222 Called by our to_close. */
223 static void (*super_close
) (struct target_ops
*);
225 static unsigned int debug_linux_nat
;
227 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
228 struct cmd_list_element
*c
, const char *value
)
230 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
234 struct simple_pid_list
238 struct simple_pid_list
*next
;
240 struct simple_pid_list
*stopped_pids
;
242 /* Async mode support. */
244 /* The read/write ends of the pipe registered as waitable file in the
246 static int linux_nat_event_pipe
[2] = { -1, -1 };
248 /* True if we're currently in async mode. */
249 #define linux_is_async_p() (linux_nat_event_pipe[0] != -1)
251 /* Flush the event pipe. */
254 async_file_flush (void)
261 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
263 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
266 /* Put something (anything, doesn't matter what, or how much) in event
267 pipe, so that the select/poll in the event-loop realizes we have
268 something to process. */
271 async_file_mark (void)
275 /* It doesn't really matter what the pipe contains, as long we end
276 up with something in it. Might as well flush the previous
282 ret
= write (linux_nat_event_pipe
[1], "+", 1);
284 while (ret
== -1 && errno
== EINTR
);
286 /* Ignore EAGAIN. If the pipe is full, the event loop will already
287 be awakened anyway. */
290 static int kill_lwp (int lwpid
, int signo
);
292 static int stop_callback (struct lwp_info
*lp
, void *data
);
293 static int resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
);
295 static void block_child_signals (sigset_t
*prev_mask
);
296 static void restore_child_signals_mask (sigset_t
*prev_mask
);
299 static struct lwp_info
*add_lwp (ptid_t ptid
);
300 static void purge_lwp_list (int pid
);
301 static void delete_lwp (ptid_t ptid
);
302 static struct lwp_info
*find_lwp_pid (ptid_t ptid
);
304 static int lwp_status_pending_p (struct lwp_info
*lp
);
306 static int check_stopped_by_breakpoint (struct lwp_info
*lp
);
307 static int sigtrap_is_event (int status
);
308 static int (*linux_nat_status_is_event
) (int status
) = sigtrap_is_event
;
313 /* See nat/linux-nat.h. */
316 ptid_of_lwp (struct lwp_info
*lwp
)
321 /* See nat/linux-nat.h. */
324 lwp_set_arch_private_info (struct lwp_info
*lwp
,
325 struct arch_lwp_info
*info
)
327 lwp
->arch_private
= info
;
330 /* See nat/linux-nat.h. */
332 struct arch_lwp_info
*
333 lwp_arch_private_info (struct lwp_info
*lwp
)
335 return lwp
->arch_private
;
338 /* See nat/linux-nat.h. */
341 lwp_is_stopped (struct lwp_info
*lwp
)
346 /* See nat/linux-nat.h. */
348 enum target_stop_reason
349 lwp_stop_reason (struct lwp_info
*lwp
)
351 return lwp
->stop_reason
;
355 /* Trivial list manipulation functions to keep track of a list of
356 new stopped processes. */
358 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
360 struct simple_pid_list
*new_pid
= XNEW (struct simple_pid_list
);
363 new_pid
->status
= status
;
364 new_pid
->next
= *listp
;
369 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
371 struct simple_pid_list
**p
;
373 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
374 if ((*p
)->pid
== pid
)
376 struct simple_pid_list
*next
= (*p
)->next
;
378 *statusp
= (*p
)->status
;
386 /* Return the ptrace options that we want to try to enable. */
389 linux_nat_ptrace_options (int attached
)
394 options
|= PTRACE_O_EXITKILL
;
396 options
|= (PTRACE_O_TRACESYSGOOD
397 | PTRACE_O_TRACEVFORKDONE
398 | PTRACE_O_TRACEVFORK
400 | PTRACE_O_TRACEEXEC
);
405 /* Initialize ptrace warnings and check for supported ptrace
408 ATTACHED should be nonzero iff we attached to the inferior. */
411 linux_init_ptrace (pid_t pid
, int attached
)
413 int options
= linux_nat_ptrace_options (attached
);
415 linux_enable_event_reporting (pid
, options
);
416 linux_ptrace_init_warnings ();
420 linux_child_post_attach (struct target_ops
*self
, int pid
)
422 linux_init_ptrace (pid
, 1);
426 linux_child_post_startup_inferior (struct target_ops
*self
, ptid_t ptid
)
428 linux_init_ptrace (ptid_get_pid (ptid
), 0);
431 /* Return the number of known LWPs in the tgid given by PID. */
439 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
440 if (ptid_get_pid (lp
->ptid
) == pid
)
446 /* Call delete_lwp with prototype compatible for make_cleanup. */
449 delete_lwp_cleanup (void *lp_voidp
)
451 struct lwp_info
*lp
= (struct lwp_info
*) lp_voidp
;
453 delete_lwp (lp
->ptid
);
456 /* Target hook for follow_fork. On entry inferior_ptid must be the
457 ptid of the followed inferior. At return, inferior_ptid will be
461 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
,
466 struct lwp_info
*child_lp
= NULL
;
467 int status
= W_STOPCODE (0);
468 struct cleanup
*old_chain
;
470 ptid_t parent_ptid
, child_ptid
;
471 int parent_pid
, child_pid
;
473 has_vforked
= (inferior_thread ()->pending_follow
.kind
474 == TARGET_WAITKIND_VFORKED
);
475 parent_ptid
= inferior_ptid
;
476 child_ptid
= inferior_thread ()->pending_follow
.value
.related_pid
;
477 parent_pid
= ptid_get_lwp (parent_ptid
);
478 child_pid
= ptid_get_lwp (child_ptid
);
480 /* We're already attached to the parent, by default. */
481 old_chain
= save_inferior_ptid ();
482 inferior_ptid
= child_ptid
;
483 child_lp
= add_lwp (inferior_ptid
);
484 child_lp
->stopped
= 1;
485 child_lp
->last_resume_kind
= resume_stop
;
487 /* Detach new forked process? */
490 make_cleanup (delete_lwp_cleanup
, child_lp
);
492 if (linux_nat_prepare_to_resume
!= NULL
)
493 linux_nat_prepare_to_resume (child_lp
);
495 /* When debugging an inferior in an architecture that supports
496 hardware single stepping on a kernel without commit
497 6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child
498 process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits
499 set if the parent process had them set.
500 To work around this, single step the child process
501 once before detaching to clear the flags. */
503 if (!gdbarch_software_single_step_p (target_thread_architecture
506 linux_disable_event_reporting (child_pid
);
507 if (ptrace (PTRACE_SINGLESTEP
, child_pid
, 0, 0) < 0)
508 perror_with_name (_("Couldn't do single step"));
509 if (my_waitpid (child_pid
, &status
, 0) < 0)
510 perror_with_name (_("Couldn't wait vfork process"));
513 if (WIFSTOPPED (status
))
517 signo
= WSTOPSIG (status
);
519 && !signal_pass_state (gdb_signal_from_host (signo
)))
521 ptrace (PTRACE_DETACH
, child_pid
, 0, signo
);
524 /* Resets value of inferior_ptid to parent ptid. */
525 do_cleanups (old_chain
);
529 /* Let the thread_db layer learn about this new process. */
530 check_for_thread_db ();
533 do_cleanups (old_chain
);
537 struct lwp_info
*parent_lp
;
539 parent_lp
= find_lwp_pid (parent_ptid
);
540 gdb_assert (linux_supports_tracefork () >= 0);
542 if (linux_supports_tracevforkdone ())
545 fprintf_unfiltered (gdb_stdlog
,
546 "LCFF: waiting for VFORK_DONE on %d\n",
548 parent_lp
->stopped
= 1;
550 /* We'll handle the VFORK_DONE event like any other
551 event, in target_wait. */
555 /* We can't insert breakpoints until the child has
556 finished with the shared memory region. We need to
557 wait until that happens. Ideal would be to just
559 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
560 - waitpid (parent_pid, &status, __WALL);
561 However, most architectures can't handle a syscall
562 being traced on the way out if it wasn't traced on
565 We might also think to loop, continuing the child
566 until it exits or gets a SIGTRAP. One problem is
567 that the child might call ptrace with PTRACE_TRACEME.
569 There's no simple and reliable way to figure out when
570 the vforked child will be done with its copy of the
571 shared memory. We could step it out of the syscall,
572 two instructions, let it go, and then single-step the
573 parent once. When we have hardware single-step, this
574 would work; with software single-step it could still
575 be made to work but we'd have to be able to insert
576 single-step breakpoints in the child, and we'd have
577 to insert -just- the single-step breakpoint in the
578 parent. Very awkward.
580 In the end, the best we can do is to make sure it
581 runs for a little while. Hopefully it will be out of
582 range of any breakpoints we reinsert. Usually this
583 is only the single-step breakpoint at vfork's return
587 fprintf_unfiltered (gdb_stdlog
,
588 "LCFF: no VFORK_DONE "
589 "support, sleeping a bit\n");
593 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
594 and leave it pending. The next linux_nat_resume call
595 will notice a pending event, and bypasses actually
596 resuming the inferior. */
597 parent_lp
->status
= 0;
598 parent_lp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
599 parent_lp
->stopped
= 1;
601 /* If we're in async mode, need to tell the event loop
602 there's something here to process. */
603 if (target_is_async_p ())
610 struct lwp_info
*child_lp
;
612 child_lp
= add_lwp (inferior_ptid
);
613 child_lp
->stopped
= 1;
614 child_lp
->last_resume_kind
= resume_stop
;
616 /* Let the thread_db layer learn about this new process. */
617 check_for_thread_db ();
625 linux_child_insert_fork_catchpoint (struct target_ops
*self
, int pid
)
627 return !linux_supports_tracefork ();
631 linux_child_remove_fork_catchpoint (struct target_ops
*self
, int pid
)
637 linux_child_insert_vfork_catchpoint (struct target_ops
*self
, int pid
)
639 return !linux_supports_tracefork ();
643 linux_child_remove_vfork_catchpoint (struct target_ops
*self
, int pid
)
649 linux_child_insert_exec_catchpoint (struct target_ops
*self
, int pid
)
651 return !linux_supports_tracefork ();
655 linux_child_remove_exec_catchpoint (struct target_ops
*self
, int pid
)
661 linux_child_set_syscall_catchpoint (struct target_ops
*self
,
662 int pid
, int needed
, int any_count
,
663 int table_size
, int *table
)
665 if (!linux_supports_tracesysgood ())
668 /* On GNU/Linux, we ignore the arguments. It means that we only
669 enable the syscall catchpoints, but do not disable them.
671 Also, we do not use the `table' information because we do not
672 filter system calls here. We let GDB do the logic for us. */
676 /* List of known LWPs. */
677 struct lwp_info
*lwp_list
;
680 /* Original signal mask. */
681 static sigset_t normal_mask
;
683 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
684 _initialize_linux_nat. */
685 static sigset_t suspend_mask
;
687 /* Signals to block to make that sigsuspend work. */
688 static sigset_t blocked_mask
;
690 /* SIGCHLD action. */
691 struct sigaction sigchld_action
;
693 /* Block child signals (SIGCHLD and linux threads signals), and store
694 the previous mask in PREV_MASK. */
697 block_child_signals (sigset_t
*prev_mask
)
699 /* Make sure SIGCHLD is blocked. */
700 if (!sigismember (&blocked_mask
, SIGCHLD
))
701 sigaddset (&blocked_mask
, SIGCHLD
);
703 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
706 /* Restore child signals mask, previously returned by
707 block_child_signals. */
710 restore_child_signals_mask (sigset_t
*prev_mask
)
712 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
715 /* Mask of signals to pass directly to the inferior. */
716 static sigset_t pass_mask
;
718 /* Update signals to pass to the inferior. */
720 linux_nat_pass_signals (struct target_ops
*self
,
721 int numsigs
, unsigned char *pass_signals
)
725 sigemptyset (&pass_mask
);
727 for (signo
= 1; signo
< NSIG
; signo
++)
729 int target_signo
= gdb_signal_from_host (signo
);
730 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
731 sigaddset (&pass_mask
, signo
);
737 /* Prototypes for local functions. */
738 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
739 static char *linux_child_pid_to_exec_file (struct target_ops
*self
, int pid
);
740 static int resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
);
744 /* Destroy and free LP. */
747 lwp_free (struct lwp_info
*lp
)
749 xfree (lp
->arch_private
);
753 /* Remove all LWPs belong to PID from the lwp list. */
756 purge_lwp_list (int pid
)
758 struct lwp_info
*lp
, *lpprev
, *lpnext
;
762 for (lp
= lwp_list
; lp
; lp
= lpnext
)
766 if (ptid_get_pid (lp
->ptid
) == pid
)
771 lpprev
->next
= lp
->next
;
780 /* Add the LWP specified by PTID to the list. PTID is the first LWP
781 in the process. Return a pointer to the structure describing the
784 This differs from add_lwp in that we don't let the arch specific
785 bits know about this new thread. Current clients of this callback
786 take the opportunity to install watchpoints in the new thread, and
787 we shouldn't do that for the first thread. If we're spawning a
788 child ("run"), the thread executes the shell wrapper first, and we
789 shouldn't touch it until it execs the program we want to debug.
790 For "attach", it'd be okay to call the callback, but it's not
791 necessary, because watchpoints can't yet have been inserted into
794 static struct lwp_info
*
795 add_initial_lwp (ptid_t ptid
)
799 gdb_assert (ptid_lwp_p (ptid
));
801 lp
= XNEW (struct lwp_info
);
803 memset (lp
, 0, sizeof (struct lwp_info
));
805 lp
->last_resume_kind
= resume_continue
;
806 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
817 /* Add the LWP specified by PID to the list. Return a pointer to the
818 structure describing the new LWP. The LWP should already be
821 static struct lwp_info
*
822 add_lwp (ptid_t ptid
)
826 lp
= add_initial_lwp (ptid
);
828 /* Let the arch specific bits know about this new thread. Current
829 clients of this callback take the opportunity to install
830 watchpoints in the new thread. We don't do this for the first
831 thread though. See add_initial_lwp. */
832 if (linux_nat_new_thread
!= NULL
)
833 linux_nat_new_thread (lp
);
838 /* Remove the LWP specified by PID from the list. */
841 delete_lwp (ptid_t ptid
)
843 struct lwp_info
*lp
, *lpprev
;
847 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
848 if (ptid_equal (lp
->ptid
, ptid
))
855 lpprev
->next
= lp
->next
;
862 /* Return a pointer to the structure describing the LWP corresponding
863 to PID. If no corresponding LWP could be found, return NULL. */
865 static struct lwp_info
*
866 find_lwp_pid (ptid_t ptid
)
871 if (ptid_lwp_p (ptid
))
872 lwp
= ptid_get_lwp (ptid
);
874 lwp
= ptid_get_pid (ptid
);
876 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
877 if (lwp
== ptid_get_lwp (lp
->ptid
))
883 /* See nat/linux-nat.h. */
886 iterate_over_lwps (ptid_t filter
,
887 iterate_over_lwps_ftype callback
,
890 struct lwp_info
*lp
, *lpnext
;
892 for (lp
= lwp_list
; lp
; lp
= lpnext
)
896 if (ptid_match (lp
->ptid
, filter
))
898 if ((*callback
) (lp
, data
) != 0)
906 /* Update our internal state when changing from one checkpoint to
907 another indicated by NEW_PTID. We can only switch single-threaded
908 applications, so we only create one new LWP, and the previous list
912 linux_nat_switch_fork (ptid_t new_ptid
)
916 purge_lwp_list (ptid_get_pid (inferior_ptid
));
918 lp
= add_lwp (new_ptid
);
921 /* This changes the thread's ptid while preserving the gdb thread
922 num. Also changes the inferior pid, while preserving the
924 thread_change_ptid (inferior_ptid
, new_ptid
);
926 /* We've just told GDB core that the thread changed target id, but,
927 in fact, it really is a different thread, with different register
929 registers_changed ();
932 /* Handle the exit of a single thread LP. */
935 exit_lwp (struct lwp_info
*lp
)
937 struct thread_info
*th
= find_thread_ptid (lp
->ptid
);
941 if (print_thread_events
)
942 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
944 delete_thread (lp
->ptid
);
947 delete_lwp (lp
->ptid
);
950 /* Wait for the LWP specified by LP, which we have just attached to.
951 Returns a wait status for that LWP, to cache. */
954 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *signalled
)
956 pid_t new_pid
, pid
= ptid_get_lwp (ptid
);
959 if (linux_proc_pid_is_stopped (pid
))
962 fprintf_unfiltered (gdb_stdlog
,
963 "LNPAW: Attaching to a stopped process\n");
965 /* The process is definitely stopped. It is in a job control
966 stop, unless the kernel predates the TASK_STOPPED /
967 TASK_TRACED distinction, in which case it might be in a
968 ptrace stop. Make sure it is in a ptrace stop; from there we
969 can kill it, signal it, et cetera.
971 First make sure there is a pending SIGSTOP. Since we are
972 already attached, the process can not transition from stopped
973 to running without a PTRACE_CONT; so we know this signal will
974 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
975 probably already in the queue (unless this kernel is old
976 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
977 is not an RT signal, it can only be queued once. */
978 kill_lwp (pid
, SIGSTOP
);
980 /* Finally, resume the stopped process. This will deliver the SIGSTOP
981 (or a higher priority signal, just like normal PTRACE_ATTACH). */
982 ptrace (PTRACE_CONT
, pid
, 0, 0);
985 /* Make sure the initial process is stopped. The user-level threads
986 layer might want to poke around in the inferior, and that won't
987 work if things haven't stabilized yet. */
988 new_pid
= my_waitpid (pid
, &status
, __WALL
);
989 gdb_assert (pid
== new_pid
);
991 if (!WIFSTOPPED (status
))
993 /* The pid we tried to attach has apparently just exited. */
995 fprintf_unfiltered (gdb_stdlog
, "LNPAW: Failed to stop %d: %s",
996 pid
, status_to_str (status
));
1000 if (WSTOPSIG (status
) != SIGSTOP
)
1003 if (debug_linux_nat
)
1004 fprintf_unfiltered (gdb_stdlog
,
1005 "LNPAW: Received %s after attaching\n",
1006 status_to_str (status
));
1013 linux_nat_create_inferior (struct target_ops
*ops
,
1014 char *exec_file
, char *allargs
, char **env
,
1017 struct cleanup
*restore_personality
1018 = maybe_disable_address_space_randomization (disable_randomization
);
1020 /* The fork_child mechanism is synchronous and calls target_wait, so
1021 we have to mask the async mode. */
1023 /* Make sure we report all signals during startup. */
1024 linux_nat_pass_signals (ops
, 0, NULL
);
1026 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1028 do_cleanups (restore_personality
);
1031 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1032 already attached. Returns true if a new LWP is found, false
1036 attach_proc_task_lwp_callback (ptid_t ptid
)
1038 struct lwp_info
*lp
;
1040 /* Ignore LWPs we're already attached to. */
1041 lp
= find_lwp_pid (ptid
);
1044 int lwpid
= ptid_get_lwp (ptid
);
1046 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) < 0)
1050 /* Be quiet if we simply raced with the thread exiting.
1051 EPERM is returned if the thread's task still exists, and
1052 is marked as exited or zombie, as well as other
1053 conditions, so in that case, confirm the status in
1054 /proc/PID/status. */
1056 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
1058 if (debug_linux_nat
)
1060 fprintf_unfiltered (gdb_stdlog
,
1061 "Cannot attach to lwp %d: "
1062 "thread is gone (%d: %s)\n",
1063 lwpid
, err
, safe_strerror (err
));
1068 warning (_("Cannot attach to lwp %d: %s"),
1070 linux_ptrace_attach_fail_reason_string (ptid
,
1076 if (debug_linux_nat
)
1077 fprintf_unfiltered (gdb_stdlog
,
1078 "PTRACE_ATTACH %s, 0, 0 (OK)\n",
1079 target_pid_to_str (ptid
));
1081 lp
= add_lwp (ptid
);
1083 /* The next time we wait for this LWP we'll see a SIGSTOP as
1084 PTRACE_ATTACH brings it to a halt. */
1087 /* We need to wait for a stop before being able to make the
1088 next ptrace call on this LWP. */
1089 lp
->must_set_ptrace_flags
= 1;
1098 linux_nat_attach (struct target_ops
*ops
, const char *args
, int from_tty
)
1100 struct lwp_info
*lp
;
1104 /* Make sure we report all signals during attach. */
1105 linux_nat_pass_signals (ops
, 0, NULL
);
1109 linux_ops
->to_attach (ops
, args
, from_tty
);
1111 CATCH (ex
, RETURN_MASK_ERROR
)
1113 pid_t pid
= parse_pid_to_attach (args
);
1114 struct buffer buffer
;
1115 char *message
, *buffer_s
;
1117 message
= xstrdup (ex
.message
);
1118 make_cleanup (xfree
, message
);
1120 buffer_init (&buffer
);
1121 linux_ptrace_attach_fail_reason (pid
, &buffer
);
1123 buffer_grow_str0 (&buffer
, "");
1124 buffer_s
= buffer_finish (&buffer
);
1125 make_cleanup (xfree
, buffer_s
);
1127 if (*buffer_s
!= '\0')
1128 throw_error (ex
.error
, "warning: %s\n%s", buffer_s
, message
);
1130 throw_error (ex
.error
, "%s", message
);
1134 /* The ptrace base target adds the main thread with (pid,0,0)
1135 format. Decorate it with lwp info. */
1136 ptid
= ptid_build (ptid_get_pid (inferior_ptid
),
1137 ptid_get_pid (inferior_ptid
),
1139 thread_change_ptid (inferior_ptid
, ptid
);
1141 /* Add the initial process as the first LWP to the list. */
1142 lp
= add_initial_lwp (ptid
);
1144 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->signalled
);
1145 if (!WIFSTOPPED (status
))
1147 if (WIFEXITED (status
))
1149 int exit_code
= WEXITSTATUS (status
);
1151 target_terminal_ours ();
1152 target_mourn_inferior ();
1154 error (_("Unable to attach: program exited normally."));
1156 error (_("Unable to attach: program exited with code %d."),
1159 else if (WIFSIGNALED (status
))
1161 enum gdb_signal signo
;
1163 target_terminal_ours ();
1164 target_mourn_inferior ();
1166 signo
= gdb_signal_from_host (WTERMSIG (status
));
1167 error (_("Unable to attach: program terminated with signal "
1169 gdb_signal_to_name (signo
),
1170 gdb_signal_to_string (signo
));
1173 internal_error (__FILE__
, __LINE__
,
1174 _("unexpected status %d for PID %ld"),
1175 status
, (long) ptid_get_lwp (ptid
));
1180 /* Save the wait status to report later. */
1182 if (debug_linux_nat
)
1183 fprintf_unfiltered (gdb_stdlog
,
1184 "LNA: waitpid %ld, saving status %s\n",
1185 (long) ptid_get_pid (lp
->ptid
), status_to_str (status
));
1187 lp
->status
= status
;
1189 /* We must attach to every LWP. If /proc is mounted, use that to
1190 find them now. The inferior may be using raw clone instead of
1191 using pthreads. But even if it is using pthreads, thread_db
1192 walks structures in the inferior's address space to find the list
1193 of threads/LWPs, and those structures may well be corrupted.
1194 Note that once thread_db is loaded, we'll still use it to list
1195 threads and associate pthread info with each LWP. */
1196 linux_proc_attach_tgid_threads (ptid_get_pid (lp
->ptid
),
1197 attach_proc_task_lwp_callback
);
1199 if (target_can_async_p ())
1203 /* Get pending status of LP. */
1205 get_pending_status (struct lwp_info
*lp
, int *status
)
1207 enum gdb_signal signo
= GDB_SIGNAL_0
;
1209 /* If we paused threads momentarily, we may have stored pending
1210 events in lp->status or lp->waitstatus (see stop_wait_callback),
1211 and GDB core hasn't seen any signal for those threads.
1212 Otherwise, the last signal reported to the core is found in the
1213 thread object's stop_signal.
1215 There's a corner case that isn't handled here at present. Only
1216 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1217 stop_signal make sense as a real signal to pass to the inferior.
1218 Some catchpoint related events, like
1219 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1220 to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1221 those traps are debug API (ptrace in our case) related and
1222 induced; the inferior wouldn't see them if it wasn't being
1223 traced. Hence, we should never pass them to the inferior, even
1224 when set to pass state. Since this corner case isn't handled by
1225 infrun.c when proceeding with a signal, for consistency, neither
1226 do we handle it here (or elsewhere in the file we check for
1227 signal pass state). Normally SIGTRAP isn't set to pass state, so
1228 this is really a corner case. */
1230 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
1231 signo
= GDB_SIGNAL_0
; /* a pending ptrace event, not a real signal. */
1232 else if (lp
->status
)
1233 signo
= gdb_signal_from_host (WSTOPSIG (lp
->status
));
1234 else if (target_is_non_stop_p () && !is_executing (lp
->ptid
))
1236 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1238 signo
= tp
->suspend
.stop_signal
;
1240 else if (!target_is_non_stop_p ())
1242 struct target_waitstatus last
;
1245 get_last_target_status (&last_ptid
, &last
);
1247 if (ptid_get_lwp (lp
->ptid
) == ptid_get_lwp (last_ptid
))
1249 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1251 signo
= tp
->suspend
.stop_signal
;
1257 if (signo
== GDB_SIGNAL_0
)
1259 if (debug_linux_nat
)
1260 fprintf_unfiltered (gdb_stdlog
,
1261 "GPT: lwp %s has no pending signal\n",
1262 target_pid_to_str (lp
->ptid
));
1264 else if (!signal_pass_state (signo
))
1266 if (debug_linux_nat
)
1267 fprintf_unfiltered (gdb_stdlog
,
1268 "GPT: lwp %s had signal %s, "
1269 "but it is in no pass state\n",
1270 target_pid_to_str (lp
->ptid
),
1271 gdb_signal_to_string (signo
));
1275 *status
= W_STOPCODE (gdb_signal_to_host (signo
));
1277 if (debug_linux_nat
)
1278 fprintf_unfiltered (gdb_stdlog
,
1279 "GPT: lwp %s has pending signal %s\n",
1280 target_pid_to_str (lp
->ptid
),
1281 gdb_signal_to_string (signo
));
1288 detach_callback (struct lwp_info
*lp
, void *data
)
1290 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1292 if (debug_linux_nat
&& lp
->status
)
1293 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1294 strsignal (WSTOPSIG (lp
->status
)),
1295 target_pid_to_str (lp
->ptid
));
1297 /* If there is a pending SIGSTOP, get rid of it. */
1300 if (debug_linux_nat
)
1301 fprintf_unfiltered (gdb_stdlog
,
1302 "DC: Sending SIGCONT to %s\n",
1303 target_pid_to_str (lp
->ptid
));
1305 kill_lwp (ptid_get_lwp (lp
->ptid
), SIGCONT
);
1309 /* We don't actually detach from the LWP that has an id equal to the
1310 overall process id just yet. */
1311 if (ptid_get_lwp (lp
->ptid
) != ptid_get_pid (lp
->ptid
))
1315 /* Pass on any pending signal for this LWP. */
1316 get_pending_status (lp
, &status
);
1318 if (linux_nat_prepare_to_resume
!= NULL
)
1319 linux_nat_prepare_to_resume (lp
);
1321 if (ptrace (PTRACE_DETACH
, ptid_get_lwp (lp
->ptid
), 0,
1322 WSTOPSIG (status
)) < 0)
1323 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1324 safe_strerror (errno
));
1326 if (debug_linux_nat
)
1327 fprintf_unfiltered (gdb_stdlog
,
1328 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1329 target_pid_to_str (lp
->ptid
),
1330 strsignal (WSTOPSIG (status
)));
1332 delete_lwp (lp
->ptid
);
1339 linux_nat_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
1343 struct lwp_info
*main_lwp
;
1345 pid
= ptid_get_pid (inferior_ptid
);
1347 /* Don't unregister from the event loop, as there may be other
1348 inferiors running. */
1350 /* Stop all threads before detaching. ptrace requires that the
1351 thread is stopped to sucessfully detach. */
1352 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1353 /* ... and wait until all of them have reported back that
1354 they're no longer running. */
1355 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1357 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1359 /* Only the initial process should be left right now. */
1360 gdb_assert (num_lwps (ptid_get_pid (inferior_ptid
)) == 1);
1362 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1364 /* Pass on any pending signal for the last LWP. */
1365 if ((args
== NULL
|| *args
== '\0')
1366 && get_pending_status (main_lwp
, &status
) != -1
1367 && WIFSTOPPED (status
))
1371 /* Put the signal number in ARGS so that inf_ptrace_detach will
1372 pass it along with PTRACE_DETACH. */
1373 tem
= (char *) alloca (8);
1374 xsnprintf (tem
, 8, "%d", (int) WSTOPSIG (status
));
1376 if (debug_linux_nat
)
1377 fprintf_unfiltered (gdb_stdlog
,
1378 "LND: Sending signal %s to %s\n",
1380 target_pid_to_str (main_lwp
->ptid
));
1383 if (linux_nat_prepare_to_resume
!= NULL
)
1384 linux_nat_prepare_to_resume (main_lwp
);
1385 delete_lwp (main_lwp
->ptid
);
1387 if (forks_exist_p ())
1389 /* Multi-fork case. The current inferior_ptid is being detached
1390 from, but there are other viable forks to debug. Detach from
1391 the current fork, and context-switch to the first
1393 linux_fork_detach (args
, from_tty
);
1396 linux_ops
->to_detach (ops
, args
, from_tty
);
1399 /* Resume execution of the inferior process. If STEP is nonzero,
1400 single-step it. If SIGNAL is nonzero, give it that signal. */
1403 linux_resume_one_lwp_throw (struct lwp_info
*lp
, int step
,
1404 enum gdb_signal signo
)
1408 /* stop_pc doubles as the PC the LWP had when it was last resumed.
1409 We only presently need that if the LWP is stepped though (to
1410 handle the case of stepping a breakpoint instruction). */
1413 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
1415 lp
->stop_pc
= regcache_read_pc (regcache
);
1420 if (linux_nat_prepare_to_resume
!= NULL
)
1421 linux_nat_prepare_to_resume (lp
);
1422 linux_ops
->to_resume (linux_ops
, lp
->ptid
, step
, signo
);
1424 /* Successfully resumed. Clear state that no longer makes sense,
1425 and mark the LWP as running. Must not do this before resuming
1426 otherwise if that fails other code will be confused. E.g., we'd
1427 later try to stop the LWP and hang forever waiting for a stop
1428 status. Note that we must not throw after this is cleared,
1429 otherwise handle_zombie_lwp_error would get confused. */
1431 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1432 registers_changed_ptid (lp
->ptid
);
1435 /* Called when we try to resume a stopped LWP and that errors out. If
1436 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
1437 or about to become), discard the error, clear any pending status
1438 the LWP may have, and return true (we'll collect the exit status
1439 soon enough). Otherwise, return false. */
1442 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
1444 /* If we get an error after resuming the LWP successfully, we'd
1445 confuse !T state for the LWP being gone. */
1446 gdb_assert (lp
->stopped
);
1448 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
1449 because even if ptrace failed with ESRCH, the tracee may be "not
1450 yet fully dead", but already refusing ptrace requests. In that
1451 case the tracee has 'R (Running)' state for a little bit
1452 (observed in Linux 3.18). See also the note on ESRCH in the
1453 ptrace(2) man page. Instead, check whether the LWP has any state
1454 other than ptrace-stopped. */
1456 /* Don't assume anything if /proc/PID/status can't be read. */
1457 if (linux_proc_pid_is_trace_stopped_nowarn (ptid_get_lwp (lp
->ptid
)) == 0)
1459 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1461 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1467 /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
1468 disappears while we try to resume it. */
1471 linux_resume_one_lwp (struct lwp_info
*lp
, int step
, enum gdb_signal signo
)
1475 linux_resume_one_lwp_throw (lp
, step
, signo
);
1477 CATCH (ex
, RETURN_MASK_ERROR
)
1479 if (!check_ptrace_stopped_lwp_gone (lp
))
1480 throw_exception (ex
);
1488 resume_lwp (struct lwp_info
*lp
, int step
, enum gdb_signal signo
)
1492 struct inferior
*inf
= find_inferior_ptid (lp
->ptid
);
1494 if (inf
->vfork_child
!= NULL
)
1496 if (debug_linux_nat
)
1497 fprintf_unfiltered (gdb_stdlog
,
1498 "RC: Not resuming %s (vfork parent)\n",
1499 target_pid_to_str (lp
->ptid
));
1501 else if (!lwp_status_pending_p (lp
))
1503 if (debug_linux_nat
)
1504 fprintf_unfiltered (gdb_stdlog
,
1505 "RC: Resuming sibling %s, %s, %s\n",
1506 target_pid_to_str (lp
->ptid
),
1507 (signo
!= GDB_SIGNAL_0
1508 ? strsignal (gdb_signal_to_host (signo
))
1510 step
? "step" : "resume");
1512 linux_resume_one_lwp (lp
, step
, signo
);
1516 if (debug_linux_nat
)
1517 fprintf_unfiltered (gdb_stdlog
,
1518 "RC: Not resuming sibling %s (has pending)\n",
1519 target_pid_to_str (lp
->ptid
));
1524 if (debug_linux_nat
)
1525 fprintf_unfiltered (gdb_stdlog
,
1526 "RC: Not resuming sibling %s (not stopped)\n",
1527 target_pid_to_str (lp
->ptid
));
1531 /* Callback for iterate_over_lwps. If LWP is EXCEPT, do nothing.
1532 Resume LWP with the last stop signal, if it is in pass state. */
1535 linux_nat_resume_callback (struct lwp_info
*lp
, void *except
)
1537 enum gdb_signal signo
= GDB_SIGNAL_0
;
1544 struct thread_info
*thread
;
1546 thread
= find_thread_ptid (lp
->ptid
);
1549 signo
= thread
->suspend
.stop_signal
;
1550 thread
->suspend
.stop_signal
= GDB_SIGNAL_0
;
1554 resume_lwp (lp
, 0, signo
);
1559 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1562 lp
->last_resume_kind
= resume_stop
;
1567 resume_set_callback (struct lwp_info
*lp
, void *data
)
1570 lp
->last_resume_kind
= resume_continue
;
1575 linux_nat_resume (struct target_ops
*ops
,
1576 ptid_t ptid
, int step
, enum gdb_signal signo
)
1578 struct lwp_info
*lp
;
1581 if (debug_linux_nat
)
1582 fprintf_unfiltered (gdb_stdlog
,
1583 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1584 step
? "step" : "resume",
1585 target_pid_to_str (ptid
),
1586 (signo
!= GDB_SIGNAL_0
1587 ? strsignal (gdb_signal_to_host (signo
)) : "0"),
1588 target_pid_to_str (inferior_ptid
));
1590 /* A specific PTID means `step only this process id'. */
1591 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
1592 || ptid_is_pid (ptid
));
1594 /* Mark the lwps we're resuming as resumed. */
1595 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
1597 /* See if it's the current inferior that should be handled
1600 lp
= find_lwp_pid (inferior_ptid
);
1602 lp
= find_lwp_pid (ptid
);
1603 gdb_assert (lp
!= NULL
);
1605 /* Remember if we're stepping. */
1606 lp
->last_resume_kind
= step
? resume_step
: resume_continue
;
1608 /* If we have a pending wait status for this thread, there is no
1609 point in resuming the process. But first make sure that
1610 linux_nat_wait won't preemptively handle the event - we
1611 should never take this short-circuit if we are going to
1612 leave LP running, since we have skipped resuming all the
1613 other threads. This bit of code needs to be synchronized
1614 with linux_nat_wait. */
1616 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1619 && WSTOPSIG (lp
->status
)
1620 && sigismember (&pass_mask
, WSTOPSIG (lp
->status
)))
1622 if (debug_linux_nat
)
1623 fprintf_unfiltered (gdb_stdlog
,
1624 "LLR: Not short circuiting for ignored "
1625 "status 0x%x\n", lp
->status
);
1627 /* FIXME: What should we do if we are supposed to continue
1628 this thread with a signal? */
1629 gdb_assert (signo
== GDB_SIGNAL_0
);
1630 signo
= gdb_signal_from_host (WSTOPSIG (lp
->status
));
1635 if (lwp_status_pending_p (lp
))
1637 /* FIXME: What should we do if we are supposed to continue
1638 this thread with a signal? */
1639 gdb_assert (signo
== GDB_SIGNAL_0
);
1641 if (debug_linux_nat
)
1642 fprintf_unfiltered (gdb_stdlog
,
1643 "LLR: Short circuiting for status 0x%x\n",
1646 if (target_can_async_p ())
1649 /* Tell the event loop we have something to process. */
1656 iterate_over_lwps (ptid
, linux_nat_resume_callback
, lp
);
1658 if (debug_linux_nat
)
1659 fprintf_unfiltered (gdb_stdlog
,
1660 "LLR: %s %s, %s (resume event thread)\n",
1661 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1662 target_pid_to_str (lp
->ptid
),
1663 (signo
!= GDB_SIGNAL_0
1664 ? strsignal (gdb_signal_to_host (signo
)) : "0"));
1666 linux_resume_one_lwp (lp
, step
, signo
);
1668 if (target_can_async_p ())
1672 /* Send a signal to an LWP. */
1675 kill_lwp (int lwpid
, int signo
)
1680 ret
= syscall (__NR_tkill
, lwpid
, signo
);
1681 if (errno
== ENOSYS
)
1683 /* If tkill fails, then we are not using nptl threads, a
1684 configuration we no longer support. */
1685 perror_with_name (("tkill"));
1690 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
1691 event, check if the core is interested in it: if not, ignore the
1692 event, and keep waiting; otherwise, we need to toggle the LWP's
1693 syscall entry/exit status, since the ptrace event itself doesn't
1694 indicate it, and report the trap to higher layers. */
1697 linux_handle_syscall_trap (struct lwp_info
*lp
, int stopping
)
1699 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1700 struct gdbarch
*gdbarch
= target_thread_architecture (lp
->ptid
);
1701 int syscall_number
= (int) gdbarch_get_syscall_number (gdbarch
, lp
->ptid
);
1705 /* If we're stopping threads, there's a SIGSTOP pending, which
1706 makes it so that the LWP reports an immediate syscall return,
1707 followed by the SIGSTOP. Skip seeing that "return" using
1708 PTRACE_CONT directly, and let stop_wait_callback collect the
1709 SIGSTOP. Later when the thread is resumed, a new syscall
1710 entry event. If we didn't do this (and returned 0), we'd
1711 leave a syscall entry pending, and our caller, by using
1712 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
1713 itself. Later, when the user re-resumes this LWP, we'd see
1714 another syscall entry event and we'd mistake it for a return.
1716 If stop_wait_callback didn't force the SIGSTOP out of the LWP
1717 (leaving immediately with LWP->signalled set, without issuing
1718 a PTRACE_CONT), it would still be problematic to leave this
1719 syscall enter pending, as later when the thread is resumed,
1720 it would then see the same syscall exit mentioned above,
1721 followed by the delayed SIGSTOP, while the syscall didn't
1722 actually get to execute. It seems it would be even more
1723 confusing to the user. */
1725 if (debug_linux_nat
)
1726 fprintf_unfiltered (gdb_stdlog
,
1727 "LHST: ignoring syscall %d "
1728 "for LWP %ld (stopping threads), "
1729 "resuming with PTRACE_CONT for SIGSTOP\n",
1731 ptid_get_lwp (lp
->ptid
));
1733 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
1734 ptrace (PTRACE_CONT
, ptid_get_lwp (lp
->ptid
), 0, 0);
1739 /* Always update the entry/return state, even if this particular
1740 syscall isn't interesting to the core now. In async mode,
1741 the user could install a new catchpoint for this syscall
1742 between syscall enter/return, and we'll need to know to
1743 report a syscall return if that happens. */
1744 lp
->syscall_state
= (lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1745 ? TARGET_WAITKIND_SYSCALL_RETURN
1746 : TARGET_WAITKIND_SYSCALL_ENTRY
);
1748 if (catch_syscall_enabled ())
1750 if (catching_syscall_number (syscall_number
))
1752 /* Alright, an event to report. */
1753 ourstatus
->kind
= lp
->syscall_state
;
1754 ourstatus
->value
.syscall_number
= syscall_number
;
1756 if (debug_linux_nat
)
1757 fprintf_unfiltered (gdb_stdlog
,
1758 "LHST: stopping for %s of syscall %d"
1761 == TARGET_WAITKIND_SYSCALL_ENTRY
1762 ? "entry" : "return",
1764 ptid_get_lwp (lp
->ptid
));
1768 if (debug_linux_nat
)
1769 fprintf_unfiltered (gdb_stdlog
,
1770 "LHST: ignoring %s of syscall %d "
1772 lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1773 ? "entry" : "return",
1775 ptid_get_lwp (lp
->ptid
));
1779 /* If we had been syscall tracing, and hence used PT_SYSCALL
1780 before on this LWP, it could happen that the user removes all
1781 syscall catchpoints before we get to process this event.
1782 There are two noteworthy issues here:
1784 - When stopped at a syscall entry event, resuming with
1785 PT_STEP still resumes executing the syscall and reports a
1788 - Only PT_SYSCALL catches syscall enters. If we last
1789 single-stepped this thread, then this event can't be a
1790 syscall enter. If we last single-stepped this thread, this
1791 has to be a syscall exit.
1793 The points above mean that the next resume, be it PT_STEP or
1794 PT_CONTINUE, can not trigger a syscall trace event. */
1795 if (debug_linux_nat
)
1796 fprintf_unfiltered (gdb_stdlog
,
1797 "LHST: caught syscall event "
1798 "with no syscall catchpoints."
1799 " %d for LWP %ld, ignoring\n",
1801 ptid_get_lwp (lp
->ptid
));
1802 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
1805 /* The core isn't interested in this event. For efficiency, avoid
1806 stopping all threads only to have the core resume them all again.
1807 Since we're not stopping threads, if we're still syscall tracing
1808 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
1809 subsequent syscall. Simply resume using the inf-ptrace layer,
1810 which knows when to use PT_SYSCALL or PT_CONTINUE. */
1812 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
1816 /* Handle a GNU/Linux extended wait response. If we see a clone
1817 event, we need to add the new LWP to our list (and not report the
1818 trap to higher layers). This function returns non-zero if the
1819 event should be ignored and we should wait again. If STOPPING is
1820 true, the new LWP remains stopped, otherwise it is continued. */
1823 linux_handle_extended_wait (struct lwp_info
*lp
, int status
)
1825 int pid
= ptid_get_lwp (lp
->ptid
);
1826 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1827 int event
= linux_ptrace_get_extended_event (status
);
1829 /* All extended events we currently use are mid-syscall. Only
1830 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
1831 you have to be using PTRACE_SEIZE to get that. */
1832 lp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
1834 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1835 || event
== PTRACE_EVENT_CLONE
)
1837 unsigned long new_pid
;
1840 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1842 /* If we haven't already seen the new PID stop, wait for it now. */
1843 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1845 /* The new child has a pending SIGSTOP. We can't affect it until it
1846 hits the SIGSTOP, but we're already attached. */
1847 ret
= my_waitpid (new_pid
, &status
, __WALL
);
1849 perror_with_name (_("waiting for new child"));
1850 else if (ret
!= new_pid
)
1851 internal_error (__FILE__
, __LINE__
,
1852 _("wait returned unexpected PID %d"), ret
);
1853 else if (!WIFSTOPPED (status
))
1854 internal_error (__FILE__
, __LINE__
,
1855 _("wait returned unexpected status 0x%x"), status
);
1858 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
1860 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
1862 /* The arch-specific native code may need to know about new
1863 forks even if those end up never mapped to an
1865 if (linux_nat_new_fork
!= NULL
)
1866 linux_nat_new_fork (lp
, new_pid
);
1869 if (event
== PTRACE_EVENT_FORK
1870 && linux_fork_checkpointing_p (ptid_get_pid (lp
->ptid
)))
1872 /* Handle checkpointing by linux-fork.c here as a special
1873 case. We don't want the follow-fork-mode or 'catch fork'
1874 to interfere with this. */
1876 /* This won't actually modify the breakpoint list, but will
1877 physically remove the breakpoints from the child. */
1878 detach_breakpoints (ptid_build (new_pid
, new_pid
, 0));
1880 /* Retain child fork in ptrace (stopped) state. */
1881 if (!find_fork_pid (new_pid
))
1884 /* Report as spurious, so that infrun doesn't want to follow
1885 this fork. We're actually doing an infcall in
1887 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1889 /* Report the stop to the core. */
1893 if (event
== PTRACE_EVENT_FORK
)
1894 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1895 else if (event
== PTRACE_EVENT_VFORK
)
1896 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1897 else if (event
== PTRACE_EVENT_CLONE
)
1899 struct lwp_info
*new_lp
;
1901 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1903 if (debug_linux_nat
)
1904 fprintf_unfiltered (gdb_stdlog
,
1905 "LHEW: Got clone event "
1906 "from LWP %d, new child is LWP %ld\n",
1909 new_lp
= add_lwp (ptid_build (ptid_get_pid (lp
->ptid
), new_pid
, 0));
1910 new_lp
->stopped
= 1;
1911 new_lp
->resumed
= 1;
1913 /* If the thread_db layer is active, let it record the user
1914 level thread id and status, and add the thread to GDB's
1916 if (!thread_db_notice_clone (lp
->ptid
, new_lp
->ptid
))
1918 /* The process is not using thread_db. Add the LWP to
1920 target_post_attach (ptid_get_lwp (new_lp
->ptid
));
1921 add_thread (new_lp
->ptid
);
1924 /* Even if we're stopping the thread for some reason
1925 internal to this module, from the perspective of infrun
1926 and the user/frontend, this new thread is running until
1927 it next reports a stop. */
1928 set_running (new_lp
->ptid
, 1);
1929 set_executing (new_lp
->ptid
, 1);
1931 if (WSTOPSIG (status
) != SIGSTOP
)
1933 /* This can happen if someone starts sending signals to
1934 the new thread before it gets a chance to run, which
1935 have a lower number than SIGSTOP (e.g. SIGUSR1).
1936 This is an unlikely case, and harder to handle for
1937 fork / vfork than for clone, so we do not try - but
1938 we handle it for clone events here. */
1940 new_lp
->signalled
= 1;
1942 /* We created NEW_LP so it cannot yet contain STATUS. */
1943 gdb_assert (new_lp
->status
== 0);
1945 /* Save the wait status to report later. */
1946 if (debug_linux_nat
)
1947 fprintf_unfiltered (gdb_stdlog
,
1948 "LHEW: waitpid of new LWP %ld, "
1949 "saving status %s\n",
1950 (long) ptid_get_lwp (new_lp
->ptid
),
1951 status_to_str (status
));
1952 new_lp
->status
= status
;
1961 if (event
== PTRACE_EVENT_EXEC
)
1963 if (debug_linux_nat
)
1964 fprintf_unfiltered (gdb_stdlog
,
1965 "LHEW: Got exec event from LWP %ld\n",
1966 ptid_get_lwp (lp
->ptid
));
1968 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1969 ourstatus
->value
.execd_pathname
1970 = xstrdup (linux_child_pid_to_exec_file (NULL
, pid
));
1972 /* The thread that execed must have been resumed, but, when a
1973 thread execs, it changes its tid to the tgid, and the old
1974 tgid thread might have not been resumed. */
1979 if (event
== PTRACE_EVENT_VFORK_DONE
)
1981 if (current_inferior ()->waiting_for_vfork_done
)
1983 if (debug_linux_nat
)
1984 fprintf_unfiltered (gdb_stdlog
,
1985 "LHEW: Got expected PTRACE_EVENT_"
1986 "VFORK_DONE from LWP %ld: stopping\n",
1987 ptid_get_lwp (lp
->ptid
));
1989 ourstatus
->kind
= TARGET_WAITKIND_VFORK_DONE
;
1993 if (debug_linux_nat
)
1994 fprintf_unfiltered (gdb_stdlog
,
1995 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
1996 "from LWP %ld: ignoring\n",
1997 ptid_get_lwp (lp
->ptid
));
2001 internal_error (__FILE__
, __LINE__
,
2002 _("unknown ptrace event %d"), event
);
2005 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2009 wait_lwp (struct lwp_info
*lp
)
2013 int thread_dead
= 0;
2016 gdb_assert (!lp
->stopped
);
2017 gdb_assert (lp
->status
== 0);
2019 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2020 block_child_signals (&prev_mask
);
2024 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), &status
, __WALL
| WNOHANG
);
2025 if (pid
== -1 && errno
== ECHILD
)
2027 /* The thread has previously exited. We need to delete it
2028 now because if this was a non-leader thread execing, we
2029 won't get an exit event. See comments on exec events at
2030 the top of the file. */
2032 if (debug_linux_nat
)
2033 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2034 target_pid_to_str (lp
->ptid
));
2039 /* Bugs 10970, 12702.
2040 Thread group leader may have exited in which case we'll lock up in
2041 waitpid if there are other threads, even if they are all zombies too.
2042 Basically, we're not supposed to use waitpid this way.
2043 tkill(pid,0) cannot be used here as it gets ESRCH for both
2044 for zombie and running processes.
2046 As a workaround, check if we're waiting for the thread group leader and
2047 if it's a zombie, and avoid calling waitpid if it is.
2049 This is racy, what if the tgl becomes a zombie right after we check?
2050 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2051 waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
2053 if (ptid_get_pid (lp
->ptid
) == ptid_get_lwp (lp
->ptid
)
2054 && linux_proc_pid_is_zombie (ptid_get_lwp (lp
->ptid
)))
2057 if (debug_linux_nat
)
2058 fprintf_unfiltered (gdb_stdlog
,
2059 "WL: Thread group leader %s vanished.\n",
2060 target_pid_to_str (lp
->ptid
));
2064 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2065 get invoked despite our caller had them intentionally blocked by
2066 block_child_signals. This is sensitive only to the loop of
2067 linux_nat_wait_1 and there if we get called my_waitpid gets called
2068 again before it gets to sigsuspend so we can safely let the handlers
2069 get executed here. */
2071 if (debug_linux_nat
)
2072 fprintf_unfiltered (gdb_stdlog
, "WL: about to sigsuspend\n");
2073 sigsuspend (&suspend_mask
);
2076 restore_child_signals_mask (&prev_mask
);
2080 gdb_assert (pid
== ptid_get_lwp (lp
->ptid
));
2082 if (debug_linux_nat
)
2084 fprintf_unfiltered (gdb_stdlog
,
2085 "WL: waitpid %s received %s\n",
2086 target_pid_to_str (lp
->ptid
),
2087 status_to_str (status
));
2090 /* Check if the thread has exited. */
2091 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2093 if (ptid_get_pid (lp
->ptid
) == ptid_get_lwp (lp
->ptid
))
2095 if (debug_linux_nat
)
2096 fprintf_unfiltered (gdb_stdlog
, "WL: Process %d exited.\n",
2097 ptid_get_pid (lp
->ptid
));
2099 /* This is the leader exiting, it means the whole
2100 process is gone. Store the status to report to the
2101 core. Store it in lp->waitstatus, because lp->status
2102 would be ambiguous (W_EXITCODE(0,0) == 0). */
2103 store_waitstatus (&lp
->waitstatus
, status
);
2108 if (debug_linux_nat
)
2109 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2110 target_pid_to_str (lp
->ptid
));
2120 gdb_assert (WIFSTOPPED (status
));
2123 if (lp
->must_set_ptrace_flags
)
2125 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
2126 int options
= linux_nat_ptrace_options (inf
->attach_flag
);
2128 linux_enable_event_reporting (ptid_get_lwp (lp
->ptid
), options
);
2129 lp
->must_set_ptrace_flags
= 0;
2132 /* Handle GNU/Linux's syscall SIGTRAPs. */
2133 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2135 /* No longer need the sysgood bit. The ptrace event ends up
2136 recorded in lp->waitstatus if we care for it. We can carry
2137 on handling the event like a regular SIGTRAP from here
2139 status
= W_STOPCODE (SIGTRAP
);
2140 if (linux_handle_syscall_trap (lp
, 1))
2141 return wait_lwp (lp
);
2145 /* Almost all other ptrace-stops are known to be outside of system
2146 calls, with further exceptions in linux_handle_extended_wait. */
2147 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2150 /* Handle GNU/Linux's extended waitstatus for trace events. */
2151 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
2152 && linux_is_extended_waitstatus (status
))
2154 if (debug_linux_nat
)
2155 fprintf_unfiltered (gdb_stdlog
,
2156 "WL: Handling extended status 0x%06x\n",
2158 linux_handle_extended_wait (lp
, status
);
2165 /* Send a SIGSTOP to LP. */
2168 stop_callback (struct lwp_info
*lp
, void *data
)
2170 if (!lp
->stopped
&& !lp
->signalled
)
2174 if (debug_linux_nat
)
2176 fprintf_unfiltered (gdb_stdlog
,
2177 "SC: kill %s **<SIGSTOP>**\n",
2178 target_pid_to_str (lp
->ptid
));
2181 ret
= kill_lwp (ptid_get_lwp (lp
->ptid
), SIGSTOP
);
2182 if (debug_linux_nat
)
2184 fprintf_unfiltered (gdb_stdlog
,
2185 "SC: lwp kill %d %s\n",
2187 errno
? safe_strerror (errno
) : "ERRNO-OK");
2191 gdb_assert (lp
->status
== 0);
2197 /* Request a stop on LWP. */
2200 linux_stop_lwp (struct lwp_info
*lwp
)
2202 stop_callback (lwp
, NULL
);
2205 /* See linux-nat.h */
2208 linux_stop_and_wait_all_lwps (void)
2210 /* Stop all LWP's ... */
2211 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
2213 /* ... and wait until all of them have reported back that
2214 they're no longer running. */
2215 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
2218 /* See linux-nat.h */
2221 linux_unstop_all_lwps (void)
2223 iterate_over_lwps (minus_one_ptid
,
2224 resume_stopped_resumed_lwps
, &minus_one_ptid
);
2227 /* Return non-zero if LWP PID has a pending SIGINT. */
2230 linux_nat_has_pending_sigint (int pid
)
2232 sigset_t pending
, blocked
, ignored
;
2234 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2236 if (sigismember (&pending
, SIGINT
)
2237 && !sigismember (&ignored
, SIGINT
))
2243 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2246 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2248 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2249 flag to consume the next one. */
2250 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2251 && WSTOPSIG (lp
->status
) == SIGINT
)
2254 lp
->ignore_sigint
= 1;
2259 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2260 This function is called after we know the LWP has stopped; if the LWP
2261 stopped before the expected SIGINT was delivered, then it will never have
2262 arrived. Also, if the signal was delivered to a shared queue and consumed
2263 by a different thread, it will never be delivered to this LWP. */
2266 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2268 if (!lp
->ignore_sigint
)
2271 if (!linux_nat_has_pending_sigint (ptid_get_lwp (lp
->ptid
)))
2273 if (debug_linux_nat
)
2274 fprintf_unfiltered (gdb_stdlog
,
2275 "MCIS: Clearing bogus flag for %s\n",
2276 target_pid_to_str (lp
->ptid
));
2277 lp
->ignore_sigint
= 0;
2281 /* Fetch the possible triggered data watchpoint info and store it in
2284 On some archs, like x86, that use debug registers to set
2285 watchpoints, it's possible that the way to know which watched
2286 address trapped, is to check the register that is used to select
2287 which address to watch. Problem is, between setting the watchpoint
2288 and reading back which data address trapped, the user may change
2289 the set of watchpoints, and, as a consequence, GDB changes the
2290 debug registers in the inferior. To avoid reading back a stale
2291 stopped-data-address when that happens, we cache in LP the fact
2292 that a watchpoint trapped, and the corresponding data address, as
2293 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2294 registers meanwhile, we have the cached data we can rely on. */
2297 check_stopped_by_watchpoint (struct lwp_info
*lp
)
2299 struct cleanup
*old_chain
;
2301 if (linux_ops
->to_stopped_by_watchpoint
== NULL
)
2304 old_chain
= save_inferior_ptid ();
2305 inferior_ptid
= lp
->ptid
;
2307 if (linux_ops
->to_stopped_by_watchpoint (linux_ops
))
2309 lp
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2311 if (linux_ops
->to_stopped_data_address
!= NULL
)
2312 lp
->stopped_data_address_p
=
2313 linux_ops
->to_stopped_data_address (¤t_target
,
2314 &lp
->stopped_data_address
);
2316 lp
->stopped_data_address_p
= 0;
2319 do_cleanups (old_chain
);
2321 return lp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2324 /* Called when the LWP stopped for a trap that could be explained by a
2325 watchpoint or a breakpoint. */
2328 save_sigtrap (struct lwp_info
*lp
)
2330 gdb_assert (lp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
);
2331 gdb_assert (lp
->status
!= 0);
2333 /* Check first if this was a SW/HW breakpoint before checking
2334 watchpoints, because at least s390 can't tell the data address of
2335 hardware watchpoint hits, and the kernel returns
2336 stopped-by-watchpoint as long as there's a watchpoint set. */
2337 if (linux_nat_status_is_event (lp
->status
))
2338 check_stopped_by_breakpoint (lp
);
2340 /* Note that TRAP_HWBKPT can indicate either a hardware breakpoint
2341 or hardware watchpoint. Check which is which if we got
2342 TARGET_STOPPED_BY_HW_BREAKPOINT. */
2343 if (lp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
2344 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
2345 check_stopped_by_watchpoint (lp
);
2348 /* Returns true if the LWP had stopped for a watchpoint. */
2351 linux_nat_stopped_by_watchpoint (struct target_ops
*ops
)
2353 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2355 gdb_assert (lp
!= NULL
);
2357 return lp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2361 linux_nat_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
2363 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2365 gdb_assert (lp
!= NULL
);
2367 *addr_p
= lp
->stopped_data_address
;
2369 return lp
->stopped_data_address_p
;
2372 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2375 sigtrap_is_event (int status
)
2377 return WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
;
2380 /* Set alternative SIGTRAP-like events recognizer. If
2381 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2385 linux_nat_set_status_is_event (struct target_ops
*t
,
2386 int (*status_is_event
) (int status
))
2388 linux_nat_status_is_event
= status_is_event
;
2391 /* Wait until LP is stopped. */
2394 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2396 struct inferior
*inf
= find_inferior_ptid (lp
->ptid
);
2398 /* If this is a vfork parent, bail out, it is not going to report
2399 any SIGSTOP until the vfork is done with. */
2400 if (inf
->vfork_child
!= NULL
)
2407 status
= wait_lwp (lp
);
2411 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2412 && WSTOPSIG (status
) == SIGINT
)
2414 lp
->ignore_sigint
= 0;
2417 ptrace (PTRACE_CONT
, ptid_get_lwp (lp
->ptid
), 0, 0);
2419 if (debug_linux_nat
)
2420 fprintf_unfiltered (gdb_stdlog
,
2421 "PTRACE_CONT %s, 0, 0 (%s) "
2422 "(discarding SIGINT)\n",
2423 target_pid_to_str (lp
->ptid
),
2424 errno
? safe_strerror (errno
) : "OK");
2426 return stop_wait_callback (lp
, NULL
);
2429 maybe_clear_ignore_sigint (lp
);
2431 if (WSTOPSIG (status
) != SIGSTOP
)
2433 /* The thread was stopped with a signal other than SIGSTOP. */
2435 if (debug_linux_nat
)
2436 fprintf_unfiltered (gdb_stdlog
,
2437 "SWC: Pending event %s in %s\n",
2438 status_to_str ((int) status
),
2439 target_pid_to_str (lp
->ptid
));
2441 /* Save the sigtrap event. */
2442 lp
->status
= status
;
2443 gdb_assert (lp
->signalled
);
2448 /* We caught the SIGSTOP that we intended to catch, so
2449 there's no SIGSTOP pending. */
2451 if (debug_linux_nat
)
2452 fprintf_unfiltered (gdb_stdlog
,
2453 "SWC: Expected SIGSTOP caught for %s.\n",
2454 target_pid_to_str (lp
->ptid
));
2456 /* Reset SIGNALLED only after the stop_wait_callback call
2457 above as it does gdb_assert on SIGNALLED. */
2465 /* Return non-zero if LP has a wait status pending. Discard the
2466 pending event and resume the LWP if the event that originally
2467 caused the stop became uninteresting. */
2470 status_callback (struct lwp_info
*lp
, void *data
)
2472 /* Only report a pending wait status if we pretend that this has
2473 indeed been resumed. */
2477 if (!lwp_status_pending_p (lp
))
2480 if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
2481 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
2483 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2484 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2488 pc
= regcache_read_pc (regcache
);
2490 if (pc
!= lp
->stop_pc
)
2492 if (debug_linux_nat
)
2493 fprintf_unfiltered (gdb_stdlog
,
2494 "SC: PC of %s changed. was=%s, now=%s\n",
2495 target_pid_to_str (lp
->ptid
),
2496 paddress (target_gdbarch (), lp
->stop_pc
),
2497 paddress (target_gdbarch (), pc
));
2501 #if !USE_SIGTRAP_SIGINFO
2502 else if (!breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
2504 if (debug_linux_nat
)
2505 fprintf_unfiltered (gdb_stdlog
,
2506 "SC: previous breakpoint of %s, at %s gone\n",
2507 target_pid_to_str (lp
->ptid
),
2508 paddress (target_gdbarch (), lp
->stop_pc
));
2516 if (debug_linux_nat
)
2517 fprintf_unfiltered (gdb_stdlog
,
2518 "SC: pending event of %s cancelled.\n",
2519 target_pid_to_str (lp
->ptid
));
2522 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
2530 /* Count the LWP's that have had events. */
2533 count_events_callback (struct lwp_info
*lp
, void *data
)
2535 int *count
= (int *) data
;
2537 gdb_assert (count
!= NULL
);
2539 /* Select only resumed LWPs that have an event pending. */
2540 if (lp
->resumed
&& lwp_status_pending_p (lp
))
2546 /* Select the LWP (if any) that is currently being single-stepped. */
2549 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2551 if (lp
->last_resume_kind
== resume_step
2558 /* Returns true if LP has a status pending. */
2561 lwp_status_pending_p (struct lwp_info
*lp
)
2563 /* We check for lp->waitstatus in addition to lp->status, because we
2564 can have pending process exits recorded in lp->status and
2565 W_EXITCODE(0,0) happens to be 0. */
2566 return lp
->status
!= 0 || lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
;
2569 /* Select the Nth LWP that has had an event. */
2572 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2574 int *selector
= (int *) data
;
2576 gdb_assert (selector
!= NULL
);
2578 /* Select only resumed LWPs that have an event pending. */
2579 if (lp
->resumed
&& lwp_status_pending_p (lp
))
2580 if ((*selector
)-- == 0)
2586 /* Called when the LWP got a signal/trap that could be explained by a
2587 software or hardware breakpoint. */
2590 check_stopped_by_breakpoint (struct lwp_info
*lp
)
2592 /* Arrange for a breakpoint to be hit again later. We don't keep
2593 the SIGTRAP status and don't forward the SIGTRAP signal to the
2594 LWP. We will handle the current event, eventually we will resume
2595 this LWP, and this breakpoint will trap again.
2597 If we do not do this, then we run the risk that the user will
2598 delete or disable the breakpoint, but the LWP will have already
2601 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2602 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2605 #if USE_SIGTRAP_SIGINFO
2609 pc
= regcache_read_pc (regcache
);
2610 sw_bp_pc
= pc
- gdbarch_decr_pc_after_break (gdbarch
);
2612 #if USE_SIGTRAP_SIGINFO
2613 if (linux_nat_get_siginfo (lp
->ptid
, &siginfo
))
2615 if (siginfo
.si_signo
== SIGTRAP
)
2617 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
))
2619 if (debug_linux_nat
)
2620 fprintf_unfiltered (gdb_stdlog
,
2621 "CSBB: %s stopped by software "
2623 target_pid_to_str (lp
->ptid
));
2625 /* Back up the PC if necessary. */
2627 regcache_write_pc (regcache
, sw_bp_pc
);
2629 lp
->stop_pc
= sw_bp_pc
;
2630 lp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
2633 else if (siginfo
.si_code
== TRAP_HWBKPT
)
2635 if (debug_linux_nat
)
2636 fprintf_unfiltered (gdb_stdlog
,
2637 "CSBB: %s stopped by hardware "
2638 "breakpoint/watchpoint\n",
2639 target_pid_to_str (lp
->ptid
));
2642 lp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
2645 else if (siginfo
.si_code
== TRAP_TRACE
)
2647 if (debug_linux_nat
)
2648 fprintf_unfiltered (gdb_stdlog
,
2649 "CSBB: %s stopped by trace\n",
2650 target_pid_to_str (lp
->ptid
));
2655 if ((!lp
->step
|| lp
->stop_pc
== sw_bp_pc
)
2656 && software_breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
2659 /* The LWP was either continued, or stepped a software
2660 breakpoint instruction. */
2661 if (debug_linux_nat
)
2662 fprintf_unfiltered (gdb_stdlog
,
2663 "CSBB: %s stopped by software breakpoint\n",
2664 target_pid_to_str (lp
->ptid
));
2666 /* Back up the PC if necessary. */
2668 regcache_write_pc (regcache
, sw_bp_pc
);
2670 lp
->stop_pc
= sw_bp_pc
;
2671 lp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
2675 if (hardware_breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
2677 if (debug_linux_nat
)
2678 fprintf_unfiltered (gdb_stdlog
,
2679 "CSBB: stopped by hardware breakpoint %s\n",
2680 target_pid_to_str (lp
->ptid
));
2683 lp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
2692 /* Returns true if the LWP had stopped for a software breakpoint. */
2695 linux_nat_stopped_by_sw_breakpoint (struct target_ops
*ops
)
2697 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2699 gdb_assert (lp
!= NULL
);
2701 return lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
;
2704 /* Implement the supports_stopped_by_sw_breakpoint method. */
2707 linux_nat_supports_stopped_by_sw_breakpoint (struct target_ops
*ops
)
2709 return USE_SIGTRAP_SIGINFO
;
2712 /* Returns true if the LWP had stopped for a hardware
2713 breakpoint/watchpoint. */
2716 linux_nat_stopped_by_hw_breakpoint (struct target_ops
*ops
)
2718 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2720 gdb_assert (lp
!= NULL
);
2722 return lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
;
2725 /* Implement the supports_stopped_by_hw_breakpoint method. */
2728 linux_nat_supports_stopped_by_hw_breakpoint (struct target_ops
*ops
)
2730 return USE_SIGTRAP_SIGINFO
;
2733 /* Select one LWP out of those that have events pending. */
2736 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
2739 int random_selector
;
2740 struct lwp_info
*event_lp
= NULL
;
2742 /* Record the wait status for the original LWP. */
2743 (*orig_lp
)->status
= *status
;
2745 /* In all-stop, give preference to the LWP that is being
2746 single-stepped. There will be at most one, and it will be the
2747 LWP that the core is most interested in. If we didn't do this,
2748 then we'd have to handle pending step SIGTRAPs somehow in case
2749 the core later continues the previously-stepped thread, as
2750 otherwise we'd report the pending SIGTRAP then, and the core, not
2751 having stepped the thread, wouldn't understand what the trap was
2752 for, and therefore would report it to the user as a random
2754 if (!target_is_non_stop_p ())
2756 event_lp
= iterate_over_lwps (filter
,
2757 select_singlestep_lwp_callback
, NULL
);
2758 if (event_lp
!= NULL
)
2760 if (debug_linux_nat
)
2761 fprintf_unfiltered (gdb_stdlog
,
2762 "SEL: Select single-step %s\n",
2763 target_pid_to_str (event_lp
->ptid
));
2767 if (event_lp
== NULL
)
2769 /* Pick one at random, out of those which have had events. */
2771 /* First see how many events we have. */
2772 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
2773 gdb_assert (num_events
> 0);
2775 /* Now randomly pick a LWP out of those that have had
2777 random_selector
= (int)
2778 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2780 if (debug_linux_nat
&& num_events
> 1)
2781 fprintf_unfiltered (gdb_stdlog
,
2782 "SEL: Found %d events, selecting #%d\n",
2783 num_events
, random_selector
);
2785 event_lp
= iterate_over_lwps (filter
,
2786 select_event_lwp_callback
,
2790 if (event_lp
!= NULL
)
2792 /* Switch the event LWP. */
2793 *orig_lp
= event_lp
;
2794 *status
= event_lp
->status
;
2797 /* Flush the wait status for the event LWP. */
2798 (*orig_lp
)->status
= 0;
2801 /* Return non-zero if LP has been resumed. */
2804 resumed_callback (struct lwp_info
*lp
, void *data
)
2809 /* Check if we should go on and pass this event to common code.
2810 Return the affected lwp if we are, or NULL otherwise. */
2812 static struct lwp_info
*
2813 linux_nat_filter_event (int lwpid
, int status
)
2815 struct lwp_info
*lp
;
2816 int event
= linux_ptrace_get_extended_event (status
);
2818 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2820 /* Check for stop events reported by a process we didn't already
2821 know about - anything not already in our LWP list.
2823 If we're expecting to receive stopped processes after
2824 fork, vfork, and clone events, then we'll just add the
2825 new one to our list and go back to waiting for the event
2826 to be reported - the stopped process might be returned
2827 from waitpid before or after the event is.
2829 But note the case of a non-leader thread exec'ing after the
2830 leader having exited, and gone from our lists. The non-leader
2831 thread changes its tid to the tgid. */
2833 if (WIFSTOPPED (status
) && lp
== NULL
2834 && (WSTOPSIG (status
) == SIGTRAP
&& event
== PTRACE_EVENT_EXEC
))
2836 /* A multi-thread exec after we had seen the leader exiting. */
2837 if (debug_linux_nat
)
2838 fprintf_unfiltered (gdb_stdlog
,
2839 "LLW: Re-adding thread group leader LWP %d.\n",
2842 lp
= add_lwp (ptid_build (lwpid
, lwpid
, 0));
2845 add_thread (lp
->ptid
);
2848 if (WIFSTOPPED (status
) && !lp
)
2850 if (debug_linux_nat
)
2851 fprintf_unfiltered (gdb_stdlog
,
2852 "LHEW: saving LWP %ld status %s in stopped_pids list\n",
2853 (long) lwpid
, status_to_str (status
));
2854 add_to_pid_list (&stopped_pids
, lwpid
, status
);
2858 /* Make sure we don't report an event for the exit of an LWP not in
2859 our list, i.e. not part of the current process. This can happen
2860 if we detach from a program we originally forked and then it
2862 if (!WIFSTOPPED (status
) && !lp
)
2865 /* This LWP is stopped now. (And if dead, this prevents it from
2866 ever being continued.) */
2869 if (WIFSTOPPED (status
) && lp
->must_set_ptrace_flags
)
2871 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
2872 int options
= linux_nat_ptrace_options (inf
->attach_flag
);
2874 linux_enable_event_reporting (ptid_get_lwp (lp
->ptid
), options
);
2875 lp
->must_set_ptrace_flags
= 0;
2878 /* Handle GNU/Linux's syscall SIGTRAPs. */
2879 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2881 /* No longer need the sysgood bit. The ptrace event ends up
2882 recorded in lp->waitstatus if we care for it. We can carry
2883 on handling the event like a regular SIGTRAP from here
2885 status
= W_STOPCODE (SIGTRAP
);
2886 if (linux_handle_syscall_trap (lp
, 0))
2891 /* Almost all other ptrace-stops are known to be outside of system
2892 calls, with further exceptions in linux_handle_extended_wait. */
2893 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2896 /* Handle GNU/Linux's extended waitstatus for trace events. */
2897 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
2898 && linux_is_extended_waitstatus (status
))
2900 if (debug_linux_nat
)
2901 fprintf_unfiltered (gdb_stdlog
,
2902 "LLW: Handling extended status 0x%06x\n",
2904 if (linux_handle_extended_wait (lp
, status
))
2908 /* Check if the thread has exited. */
2909 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2911 if (num_lwps (ptid_get_pid (lp
->ptid
)) > 1)
2913 if (debug_linux_nat
)
2914 fprintf_unfiltered (gdb_stdlog
,
2915 "LLW: %s exited.\n",
2916 target_pid_to_str (lp
->ptid
));
2918 /* If there is at least one more LWP, then the exit signal
2919 was not the end of the debugged application and should be
2925 /* Note that even if the leader was ptrace-stopped, it can still
2926 exit, if e.g., some other thread brings down the whole
2927 process (calls `exit'). So don't assert that the lwp is
2929 if (debug_linux_nat
)
2930 fprintf_unfiltered (gdb_stdlog
,
2931 "Process %ld exited (resumed=%d)\n",
2932 ptid_get_lwp (lp
->ptid
), lp
->resumed
);
2934 /* This was the last lwp in the process. Since events are
2935 serialized to GDB core, we may not be able report this one
2936 right now, but GDB core and the other target layers will want
2937 to be notified about the exit code/signal, leave the status
2938 pending for the next time we're able to report it. */
2940 /* Dead LWP's aren't expected to reported a pending sigstop. */
2943 /* Store the pending event in the waitstatus, because
2944 W_EXITCODE(0,0) == 0. */
2945 store_waitstatus (&lp
->waitstatus
, status
);
2949 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2950 an attempt to stop an LWP. */
2952 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2956 if (lp
->last_resume_kind
== resume_stop
)
2958 if (debug_linux_nat
)
2959 fprintf_unfiltered (gdb_stdlog
,
2960 "LLW: resume_stop SIGSTOP caught for %s.\n",
2961 target_pid_to_str (lp
->ptid
));
2965 /* This is a delayed SIGSTOP. Filter out the event. */
2967 if (debug_linux_nat
)
2968 fprintf_unfiltered (gdb_stdlog
,
2969 "LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
2971 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2972 target_pid_to_str (lp
->ptid
));
2974 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
2975 gdb_assert (lp
->resumed
);
2980 /* Make sure we don't report a SIGINT that we have already displayed
2981 for another thread. */
2982 if (lp
->ignore_sigint
2983 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
2985 if (debug_linux_nat
)
2986 fprintf_unfiltered (gdb_stdlog
,
2987 "LLW: Delayed SIGINT caught for %s.\n",
2988 target_pid_to_str (lp
->ptid
));
2990 /* This is a delayed SIGINT. */
2991 lp
->ignore_sigint
= 0;
2993 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
2994 if (debug_linux_nat
)
2995 fprintf_unfiltered (gdb_stdlog
,
2996 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2998 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2999 target_pid_to_str (lp
->ptid
));
3000 gdb_assert (lp
->resumed
);
3002 /* Discard the event. */
3006 /* Don't report signals that GDB isn't interested in, such as
3007 signals that are neither printed nor stopped upon. Stopping all
3008 threads can be a bit time-consuming so if we want decent
3009 performance with heavily multi-threaded programs, especially when
3010 they're using a high frequency timer, we'd better avoid it if we
3012 if (WIFSTOPPED (status
))
3014 enum gdb_signal signo
= gdb_signal_from_host (WSTOPSIG (status
));
3016 if (!target_is_non_stop_p ())
3018 /* Only do the below in all-stop, as we currently use SIGSTOP
3019 to implement target_stop (see linux_nat_stop) in
3021 if (signo
== GDB_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3023 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3024 forwarded to the entire process group, that is, all LWPs
3025 will receive it - unless they're using CLONE_THREAD to
3026 share signals. Since we only want to report it once, we
3027 mark it as ignored for all LWPs except this one. */
3028 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp
->ptid
)),
3029 set_ignore_sigint
, NULL
);
3030 lp
->ignore_sigint
= 0;
3033 maybe_clear_ignore_sigint (lp
);
3036 /* When using hardware single-step, we need to report every signal.
3037 Otherwise, signals in pass_mask may be short-circuited
3038 except signals that might be caused by a breakpoint. */
3040 && WSTOPSIG (status
) && sigismember (&pass_mask
, WSTOPSIG (status
))
3041 && !linux_wstatus_maybe_breakpoint (status
))
3043 linux_resume_one_lwp (lp
, lp
->step
, signo
);
3044 if (debug_linux_nat
)
3045 fprintf_unfiltered (gdb_stdlog
,
3046 "LLW: %s %s, %s (preempt 'handle')\n",
3048 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3049 target_pid_to_str (lp
->ptid
),
3050 (signo
!= GDB_SIGNAL_0
3051 ? strsignal (gdb_signal_to_host (signo
))
3057 /* An interesting event. */
3059 lp
->status
= status
;
3064 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3065 their exits until all other threads in the group have exited. */
3068 check_zombie_leaders (void)
3070 struct inferior
*inf
;
3074 struct lwp_info
*leader_lp
;
3079 leader_lp
= find_lwp_pid (pid_to_ptid (inf
->pid
));
3080 if (leader_lp
!= NULL
3081 /* Check if there are other threads in the group, as we may
3082 have raced with the inferior simply exiting. */
3083 && num_lwps (inf
->pid
) > 1
3084 && linux_proc_pid_is_zombie (inf
->pid
))
3086 if (debug_linux_nat
)
3087 fprintf_unfiltered (gdb_stdlog
,
3088 "CZL: Thread group leader %d zombie "
3089 "(it exited, or another thread execd).\n",
3092 /* A leader zombie can mean one of two things:
3094 - It exited, and there's an exit status pending
3095 available, or only the leader exited (not the whole
3096 program). In the latter case, we can't waitpid the
3097 leader's exit status until all other threads are gone.
3099 - There are 3 or more threads in the group, and a thread
3100 other than the leader exec'd. See comments on exec
3101 events at the top of the file. We could try
3102 distinguishing the exit and exec cases, by waiting once
3103 more, and seeing if something comes out, but it doesn't
3104 sound useful. The previous leader _does_ go away, and
3105 we'll re-add the new one once we see the exec event
3106 (which is just the same as what would happen if the
3107 previous leader did exit voluntarily before some other
3110 if (debug_linux_nat
)
3111 fprintf_unfiltered (gdb_stdlog
,
3112 "CZL: Thread group leader %d vanished.\n",
3114 exit_lwp (leader_lp
);
3120 linux_nat_wait_1 (struct target_ops
*ops
,
3121 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3125 enum resume_kind last_resume_kind
;
3126 struct lwp_info
*lp
;
3129 if (debug_linux_nat
)
3130 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
3132 /* The first time we get here after starting a new inferior, we may
3133 not have added it to the LWP list yet - this is the earliest
3134 moment at which we know its PID. */
3135 if (ptid_is_pid (inferior_ptid
))
3137 /* Upgrade the main thread's ptid. */
3138 thread_change_ptid (inferior_ptid
,
3139 ptid_build (ptid_get_pid (inferior_ptid
),
3140 ptid_get_pid (inferior_ptid
), 0));
3142 lp
= add_initial_lwp (inferior_ptid
);
3146 /* Make sure SIGCHLD is blocked until the sigsuspend below. */
3147 block_child_signals (&prev_mask
);
3149 /* First check if there is a LWP with a wait status pending. */
3150 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3153 if (debug_linux_nat
)
3154 fprintf_unfiltered (gdb_stdlog
,
3155 "LLW: Using pending wait status %s for %s.\n",
3156 status_to_str (lp
->status
),
3157 target_pid_to_str (lp
->ptid
));
3160 /* But if we don't find a pending event, we'll have to wait. Always
3161 pull all events out of the kernel. We'll randomly select an
3162 event LWP out of all that have events, to prevent starvation. */
3168 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3171 - If the thread group leader exits while other threads in the
3172 thread group still exist, waitpid(TGID, ...) hangs. That
3173 waitpid won't return an exit status until the other threads
3174 in the group are reapped.
3176 - When a non-leader thread execs, that thread just vanishes
3177 without reporting an exit (so we'd hang if we waited for it
3178 explicitly in that case). The exec event is reported to
3182 lwpid
= my_waitpid (-1, &status
, __WALL
| WNOHANG
);
3184 if (debug_linux_nat
)
3185 fprintf_unfiltered (gdb_stdlog
,
3186 "LNW: waitpid(-1, ...) returned %d, %s\n",
3187 lwpid
, errno
? safe_strerror (errno
) : "ERRNO-OK");
3191 if (debug_linux_nat
)
3193 fprintf_unfiltered (gdb_stdlog
,
3194 "LLW: waitpid %ld received %s\n",
3195 (long) lwpid
, status_to_str (status
));
3198 linux_nat_filter_event (lwpid
, status
);
3199 /* Retry until nothing comes out of waitpid. A single
3200 SIGCHLD can indicate more than one child stopped. */
3204 /* Now that we've pulled all events out of the kernel, resume
3205 LWPs that don't have an interesting event to report. */
3206 iterate_over_lwps (minus_one_ptid
,
3207 resume_stopped_resumed_lwps
, &minus_one_ptid
);
3209 /* ... and find an LWP with a status to report to the core, if
3211 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3215 /* Check for zombie thread group leaders. Those can't be reaped
3216 until all other threads in the thread group are. */
3217 check_zombie_leaders ();
3219 /* If there are no resumed children left, bail. We'd be stuck
3220 forever in the sigsuspend call below otherwise. */
3221 if (iterate_over_lwps (ptid
, resumed_callback
, NULL
) == NULL
)
3223 if (debug_linux_nat
)
3224 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (no resumed LWP)\n");
3226 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3228 restore_child_signals_mask (&prev_mask
);
3229 return minus_one_ptid
;
3232 /* No interesting event to report to the core. */
3234 if (target_options
& TARGET_WNOHANG
)
3236 if (debug_linux_nat
)
3237 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
3239 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3240 restore_child_signals_mask (&prev_mask
);
3241 return minus_one_ptid
;
3244 /* We shouldn't end up here unless we want to try again. */
3245 gdb_assert (lp
== NULL
);
3247 /* Block until we get an event reported with SIGCHLD. */
3248 if (debug_linux_nat
)
3249 fprintf_unfiltered (gdb_stdlog
, "LNW: about to sigsuspend\n");
3250 sigsuspend (&suspend_mask
);
3255 status
= lp
->status
;
3258 if (!target_is_non_stop_p ())
3260 /* Now stop all other LWP's ... */
3261 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3263 /* ... and wait until all of them have reported back that
3264 they're no longer running. */
3265 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3268 /* If we're not waiting for a specific LWP, choose an event LWP from
3269 among those that have had events. Giving equal priority to all
3270 LWPs that have had events helps prevent starvation. */
3271 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3272 select_event_lwp (ptid
, &lp
, &status
);
3274 gdb_assert (lp
!= NULL
);
3276 /* Now that we've selected our final event LWP, un-adjust its PC if
3277 it was a software breakpoint, and we can't reliably support the
3278 "stopped by software breakpoint" stop reason. */
3279 if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3280 && !USE_SIGTRAP_SIGINFO
)
3282 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3283 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3284 int decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
3290 pc
= regcache_read_pc (regcache
);
3291 regcache_write_pc (regcache
, pc
+ decr_pc
);
3295 /* We'll need this to determine whether to report a SIGSTOP as
3296 GDB_SIGNAL_0. Need to take a copy because resume_clear_callback
3298 last_resume_kind
= lp
->last_resume_kind
;
3300 if (!target_is_non_stop_p ())
3302 /* In all-stop, from the core's perspective, all LWPs are now
3303 stopped until a new resume action is sent over. */
3304 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
3308 resume_clear_callback (lp
, NULL
);
3311 if (linux_nat_status_is_event (status
))
3313 if (debug_linux_nat
)
3314 fprintf_unfiltered (gdb_stdlog
,
3315 "LLW: trap ptid is %s.\n",
3316 target_pid_to_str (lp
->ptid
));
3319 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3321 *ourstatus
= lp
->waitstatus
;
3322 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3325 store_waitstatus (ourstatus
, status
);
3327 if (debug_linux_nat
)
3328 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3330 restore_child_signals_mask (&prev_mask
);
3332 if (last_resume_kind
== resume_stop
3333 && ourstatus
->kind
== TARGET_WAITKIND_STOPPED
3334 && WSTOPSIG (status
) == SIGSTOP
)
3336 /* A thread that has been requested to stop by GDB with
3337 target_stop, and it stopped cleanly, so report as SIG0. The
3338 use of SIGSTOP is an implementation detail. */
3339 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3342 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
3343 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
3346 lp
->core
= linux_common_core_of_thread (lp
->ptid
);
3351 /* Resume LWPs that are currently stopped without any pending status
3352 to report, but are resumed from the core's perspective. */
3355 resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
)
3357 ptid_t
*wait_ptid_p
= (ptid_t
*) data
;
3361 if (debug_linux_nat
)
3362 fprintf_unfiltered (gdb_stdlog
,
3363 "RSRL: NOT resuming LWP %s, not stopped\n",
3364 target_pid_to_str (lp
->ptid
));
3366 else if (!lp
->resumed
)
3368 if (debug_linux_nat
)
3369 fprintf_unfiltered (gdb_stdlog
,
3370 "RSRL: NOT resuming LWP %s, not resumed\n",
3371 target_pid_to_str (lp
->ptid
));
3373 else if (lwp_status_pending_p (lp
))
3375 if (debug_linux_nat
)
3376 fprintf_unfiltered (gdb_stdlog
,
3377 "RSRL: NOT resuming LWP %s, has pending status\n",
3378 target_pid_to_str (lp
->ptid
));
3382 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3383 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3387 CORE_ADDR pc
= regcache_read_pc (regcache
);
3388 int leave_stopped
= 0;
3390 /* Don't bother if there's a breakpoint at PC that we'd hit
3391 immediately, and we're not waiting for this LWP. */
3392 if (!ptid_match (lp
->ptid
, *wait_ptid_p
))
3394 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
3400 if (debug_linux_nat
)
3401 fprintf_unfiltered (gdb_stdlog
,
3402 "RSRL: resuming stopped-resumed LWP %s at "
3404 target_pid_to_str (lp
->ptid
),
3405 paddress (gdbarch
, pc
),
3408 linux_resume_one_lwp_throw (lp
, lp
->step
, GDB_SIGNAL_0
);
3411 CATCH (ex
, RETURN_MASK_ERROR
)
3413 if (!check_ptrace_stopped_lwp_gone (lp
))
3414 throw_exception (ex
);
3423 linux_nat_wait (struct target_ops
*ops
,
3424 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3429 if (debug_linux_nat
)
3431 char *options_string
;
3433 options_string
= target_options_to_string (target_options
);
3434 fprintf_unfiltered (gdb_stdlog
,
3435 "linux_nat_wait: [%s], [%s]\n",
3436 target_pid_to_str (ptid
),
3438 xfree (options_string
);
3441 /* Flush the async file first. */
3442 if (target_is_async_p ())
3443 async_file_flush ();
3445 /* Resume LWPs that are currently stopped without any pending status
3446 to report, but are resumed from the core's perspective. LWPs get
3447 in this state if we find them stopping at a time we're not
3448 interested in reporting the event (target_wait on a
3449 specific_process, for example, see linux_nat_wait_1), and
3450 meanwhile the event became uninteresting. Don't bother resuming
3451 LWPs we're not going to wait for if they'd stop immediately. */
3452 if (target_is_non_stop_p ())
3453 iterate_over_lwps (minus_one_ptid
, resume_stopped_resumed_lwps
, &ptid
);
3455 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
, target_options
);
3457 /* If we requested any event, and something came out, assume there
3458 may be more. If we requested a specific lwp or process, also
3459 assume there may be more. */
3460 if (target_is_async_p ()
3461 && ((ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
3462 && ourstatus
->kind
!= TARGET_WAITKIND_NO_RESUMED
)
3463 || !ptid_equal (ptid
, minus_one_ptid
)))
3472 kill_one_lwp (pid_t pid
)
3474 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
3477 kill_lwp (pid
, SIGKILL
);
3478 if (debug_linux_nat
)
3480 int save_errno
= errno
;
3482 fprintf_unfiltered (gdb_stdlog
,
3483 "KC: kill (SIGKILL) %ld, 0, 0 (%s)\n", (long) pid
,
3484 save_errno
? safe_strerror (save_errno
) : "OK");
3487 /* Some kernels ignore even SIGKILL for processes under ptrace. */
3490 ptrace (PTRACE_KILL
, pid
, 0, 0);
3491 if (debug_linux_nat
)
3493 int save_errno
= errno
;
3495 fprintf_unfiltered (gdb_stdlog
,
3496 "KC: PTRACE_KILL %ld, 0, 0 (%s)\n", (long) pid
,
3497 save_errno
? safe_strerror (save_errno
) : "OK");
3501 /* Wait for an LWP to die. */
3504 kill_wait_one_lwp (pid_t pid
)
3508 /* We must make sure that there are no pending events (delayed
3509 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3510 program doesn't interfere with any following debugging session. */
3514 res
= my_waitpid (pid
, NULL
, __WALL
);
3515 if (res
!= (pid_t
) -1)
3517 if (debug_linux_nat
)
3518 fprintf_unfiltered (gdb_stdlog
,
3519 "KWC: wait %ld received unknown.\n",
3521 /* The Linux kernel sometimes fails to kill a thread
3522 completely after PTRACE_KILL; that goes from the stop
3523 point in do_fork out to the one in get_signal_to_deliver
3524 and waits again. So kill it again. */
3530 gdb_assert (res
== -1 && errno
== ECHILD
);
3533 /* Callback for iterate_over_lwps. */
3536 kill_callback (struct lwp_info
*lp
, void *data
)
3538 kill_one_lwp (ptid_get_lwp (lp
->ptid
));
3542 /* Callback for iterate_over_lwps. */
3545 kill_wait_callback (struct lwp_info
*lp
, void *data
)
3547 kill_wait_one_lwp (ptid_get_lwp (lp
->ptid
));
3551 /* Kill the fork children of any threads of inferior INF that are
3552 stopped at a fork event. */
3555 kill_unfollowed_fork_children (struct inferior
*inf
)
3557 struct thread_info
*thread
;
3559 ALL_NON_EXITED_THREADS (thread
)
3560 if (thread
->inf
== inf
)
3562 struct target_waitstatus
*ws
= &thread
->pending_follow
;
3564 if (ws
->kind
== TARGET_WAITKIND_FORKED
3565 || ws
->kind
== TARGET_WAITKIND_VFORKED
)
3567 ptid_t child_ptid
= ws
->value
.related_pid
;
3568 int child_pid
= ptid_get_pid (child_ptid
);
3569 int child_lwp
= ptid_get_lwp (child_ptid
);
3572 kill_one_lwp (child_lwp
);
3573 kill_wait_one_lwp (child_lwp
);
3575 /* Let the arch-specific native code know this process is
3577 linux_nat_forget_process (child_pid
);
3583 linux_nat_kill (struct target_ops
*ops
)
3585 struct target_waitstatus last
;
3587 /* If we're stopped while forking and we haven't followed yet,
3588 kill the other task. We need to do this first because the
3589 parent will be sleeping if this is a vfork. */
3590 kill_unfollowed_fork_children (current_inferior ());
3592 if (forks_exist_p ())
3593 linux_fork_killall ();
3596 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3598 /* Stop all threads before killing them, since ptrace requires
3599 that the thread is stopped to sucessfully PTRACE_KILL. */
3600 iterate_over_lwps (ptid
, stop_callback
, NULL
);
3601 /* ... and wait until all of them have reported back that
3602 they're no longer running. */
3603 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
3605 /* Kill all LWP's ... */
3606 iterate_over_lwps (ptid
, kill_callback
, NULL
);
3608 /* ... and wait until we've flushed all events. */
3609 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
3612 target_mourn_inferior ();
3616 linux_nat_mourn_inferior (struct target_ops
*ops
)
3618 int pid
= ptid_get_pid (inferior_ptid
);
3620 purge_lwp_list (pid
);
3622 if (! forks_exist_p ())
3623 /* Normal case, no other forks available. */
3624 linux_ops
->to_mourn_inferior (ops
);
3626 /* Multi-fork case. The current inferior_ptid has exited, but
3627 there are other viable forks to debug. Delete the exiting
3628 one and context-switch to the first available. */
3629 linux_fork_mourn_inferior ();
3631 /* Let the arch-specific native code know this process is gone. */
3632 linux_nat_forget_process (pid
);
3635 /* Convert a native/host siginfo object, into/from the siginfo in the
3636 layout of the inferiors' architecture. */
3639 siginfo_fixup (siginfo_t
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
3643 if (linux_nat_siginfo_fixup
!= NULL
)
3644 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
3646 /* If there was no callback, or the callback didn't do anything,
3647 then just do a straight memcpy. */
3651 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
3653 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
3657 static enum target_xfer_status
3658 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
3659 const char *annex
, gdb_byte
*readbuf
,
3660 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
3661 ULONGEST
*xfered_len
)
3665 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
3667 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
3668 gdb_assert (readbuf
|| writebuf
);
3670 pid
= ptid_get_lwp (inferior_ptid
);
3672 pid
= ptid_get_pid (inferior_ptid
);
3674 if (offset
> sizeof (siginfo
))
3675 return TARGET_XFER_E_IO
;
3678 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3680 return TARGET_XFER_E_IO
;
3682 /* When GDB is built as a 64-bit application, ptrace writes into
3683 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3684 inferior with a 64-bit GDB should look the same as debugging it
3685 with a 32-bit GDB, we need to convert it. GDB core always sees
3686 the converted layout, so any read/write will have to be done
3688 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
3690 if (offset
+ len
> sizeof (siginfo
))
3691 len
= sizeof (siginfo
) - offset
;
3693 if (readbuf
!= NULL
)
3694 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
3697 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
3699 /* Convert back to ptrace layout before flushing it out. */
3700 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
3703 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3705 return TARGET_XFER_E_IO
;
3709 return TARGET_XFER_OK
;
3712 static enum target_xfer_status
3713 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3714 const char *annex
, gdb_byte
*readbuf
,
3715 const gdb_byte
*writebuf
,
3716 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
3718 struct cleanup
*old_chain
;
3719 enum target_xfer_status xfer
;
3721 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
3722 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
3723 offset
, len
, xfered_len
);
3725 /* The target is connected but no live inferior is selected. Pass
3726 this request down to a lower stratum (e.g., the executable
3728 if (object
== TARGET_OBJECT_MEMORY
&& ptid_equal (inferior_ptid
, null_ptid
))
3729 return TARGET_XFER_EOF
;
3731 old_chain
= save_inferior_ptid ();
3733 if (ptid_lwp_p (inferior_ptid
))
3734 inferior_ptid
= pid_to_ptid (ptid_get_lwp (inferior_ptid
));
3736 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3737 offset
, len
, xfered_len
);
3739 do_cleanups (old_chain
);
3744 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
3746 /* As long as a PTID is in lwp list, consider it alive. */
3747 return find_lwp_pid (ptid
) != NULL
;
3750 /* Implement the to_update_thread_list target method for this
3754 linux_nat_update_thread_list (struct target_ops
*ops
)
3756 struct lwp_info
*lwp
;
3758 /* We add/delete threads from the list as clone/exit events are
3759 processed, so just try deleting exited threads still in the
3761 delete_exited_threads ();
3763 /* Update the processor core that each lwp/thread was last seen
3766 lwp
->core
= linux_common_core_of_thread (lwp
->ptid
);
3770 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3772 static char buf
[64];
3774 if (ptid_lwp_p (ptid
)
3775 && (ptid_get_pid (ptid
) != ptid_get_lwp (ptid
)
3776 || num_lwps (ptid_get_pid (ptid
)) > 1))
3778 snprintf (buf
, sizeof (buf
), "LWP %ld", ptid_get_lwp (ptid
));
3782 return normal_pid_to_str (ptid
);
3786 linux_nat_thread_name (struct target_ops
*self
, struct thread_info
*thr
)
3788 return linux_proc_tid_get_name (thr
->ptid
);
3791 /* Accepts an integer PID; Returns a string representing a file that
3792 can be opened to get the symbols for the child process. */
3795 linux_child_pid_to_exec_file (struct target_ops
*self
, int pid
)
3797 return linux_proc_pid_to_exec_file (pid
);
3800 /* Implement the to_xfer_partial interface for memory reads using the /proc
3801 filesystem. Because we can use a single read() call for /proc, this
3802 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3803 but it doesn't support writes. */
3805 static enum target_xfer_status
3806 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3807 const char *annex
, gdb_byte
*readbuf
,
3808 const gdb_byte
*writebuf
,
3809 ULONGEST offset
, LONGEST len
, ULONGEST
*xfered_len
)
3815 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
3816 return TARGET_XFER_EOF
;
3818 /* Don't bother for one word. */
3819 if (len
< 3 * sizeof (long))
3820 return TARGET_XFER_EOF
;
3822 /* We could keep this file open and cache it - possibly one per
3823 thread. That requires some juggling, but is even faster. */
3824 xsnprintf (filename
, sizeof filename
, "/proc/%d/mem",
3825 ptid_get_pid (inferior_ptid
));
3826 fd
= gdb_open_cloexec (filename
, O_RDONLY
| O_LARGEFILE
, 0);
3828 return TARGET_XFER_EOF
;
3830 /* If pread64 is available, use it. It's faster if the kernel
3831 supports it (only one syscall), and it's 64-bit safe even on
3832 32-bit platforms (for instance, SPARC debugging a SPARC64
3835 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
3837 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
3846 return TARGET_XFER_EOF
;
3850 return TARGET_XFER_OK
;
3855 /* Enumerate spufs IDs for process PID. */
3857 spu_enumerate_spu_ids (int pid
, gdb_byte
*buf
, ULONGEST offset
, ULONGEST len
)
3859 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
3861 LONGEST written
= 0;
3864 struct dirent
*entry
;
3866 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
3867 dir
= opendir (path
);
3872 while ((entry
= readdir (dir
)) != NULL
)
3878 fd
= atoi (entry
->d_name
);
3882 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
3883 if (stat (path
, &st
) != 0)
3885 if (!S_ISDIR (st
.st_mode
))
3888 if (statfs (path
, &stfs
) != 0)
3890 if (stfs
.f_type
!= SPUFS_MAGIC
)
3893 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
3895 store_unsigned_integer (buf
+ pos
- offset
, 4, byte_order
, fd
);
3905 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
3906 object type, using the /proc file system. */
3908 static enum target_xfer_status
3909 linux_proc_xfer_spu (struct target_ops
*ops
, enum target_object object
,
3910 const char *annex
, gdb_byte
*readbuf
,
3911 const gdb_byte
*writebuf
,
3912 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
3917 int pid
= ptid_get_pid (inferior_ptid
);
3922 return TARGET_XFER_E_IO
;
3925 LONGEST l
= spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
3928 return TARGET_XFER_E_IO
;
3930 return TARGET_XFER_EOF
;
3933 *xfered_len
= (ULONGEST
) l
;
3934 return TARGET_XFER_OK
;
3939 xsnprintf (buf
, sizeof buf
, "/proc/%d/fd/%s", pid
, annex
);
3940 fd
= gdb_open_cloexec (buf
, writebuf
? O_WRONLY
: O_RDONLY
, 0);
3942 return TARGET_XFER_E_IO
;
3945 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
3948 return TARGET_XFER_EOF
;
3952 ret
= write (fd
, writebuf
, (size_t) len
);
3954 ret
= read (fd
, readbuf
, (size_t) len
);
3959 return TARGET_XFER_E_IO
;
3961 return TARGET_XFER_EOF
;
3964 *xfered_len
= (ULONGEST
) ret
;
3965 return TARGET_XFER_OK
;
3970 /* Parse LINE as a signal set and add its set bits to SIGS. */
3973 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
3975 int len
= strlen (line
) - 1;
3979 if (line
[len
] != '\n')
3980 error (_("Could not parse signal set: %s"), line
);
3988 if (*p
>= '0' && *p
<= '9')
3990 else if (*p
>= 'a' && *p
<= 'f')
3991 digit
= *p
- 'a' + 10;
3993 error (_("Could not parse signal set: %s"), line
);
3998 sigaddset (sigs
, signum
+ 1);
4000 sigaddset (sigs
, signum
+ 2);
4002 sigaddset (sigs
, signum
+ 3);
4004 sigaddset (sigs
, signum
+ 4);
4010 /* Find process PID's pending signals from /proc/pid/status and set
4014 linux_proc_pending_signals (int pid
, sigset_t
*pending
,
4015 sigset_t
*blocked
, sigset_t
*ignored
)
4018 char buffer
[PATH_MAX
], fname
[PATH_MAX
];
4019 struct cleanup
*cleanup
;
4021 sigemptyset (pending
);
4022 sigemptyset (blocked
);
4023 sigemptyset (ignored
);
4024 xsnprintf (fname
, sizeof fname
, "/proc/%d/status", pid
);
4025 procfile
= gdb_fopen_cloexec (fname
, "r");
4026 if (procfile
== NULL
)
4027 error (_("Could not open %s"), fname
);
4028 cleanup
= make_cleanup_fclose (procfile
);
4030 while (fgets (buffer
, PATH_MAX
, procfile
) != NULL
)
4032 /* Normal queued signals are on the SigPnd line in the status
4033 file. However, 2.6 kernels also have a "shared" pending
4034 queue for delivering signals to a thread group, so check for
4037 Unfortunately some Red Hat kernels include the shared pending
4038 queue but not the ShdPnd status field. */
4040 if (startswith (buffer
, "SigPnd:\t"))
4041 add_line_to_sigset (buffer
+ 8, pending
);
4042 else if (startswith (buffer
, "ShdPnd:\t"))
4043 add_line_to_sigset (buffer
+ 8, pending
);
4044 else if (startswith (buffer
, "SigBlk:\t"))
4045 add_line_to_sigset (buffer
+ 8, blocked
);
4046 else if (startswith (buffer
, "SigIgn:\t"))
4047 add_line_to_sigset (buffer
+ 8, ignored
);
4050 do_cleanups (cleanup
);
4053 static enum target_xfer_status
4054 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4055 const char *annex
, gdb_byte
*readbuf
,
4056 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
4057 ULONGEST
*xfered_len
)
4059 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4061 *xfered_len
= linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4062 if (*xfered_len
== 0)
4063 return TARGET_XFER_EOF
;
4065 return TARGET_XFER_OK
;
4068 static enum target_xfer_status
4069 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4070 const char *annex
, gdb_byte
*readbuf
,
4071 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
4072 ULONGEST
*xfered_len
)
4074 enum target_xfer_status xfer
;
4076 if (object
== TARGET_OBJECT_AUXV
)
4077 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4078 offset
, len
, xfered_len
);
4080 if (object
== TARGET_OBJECT_OSDATA
)
4081 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4082 offset
, len
, xfered_len
);
4084 if (object
== TARGET_OBJECT_SPU
)
4085 return linux_proc_xfer_spu (ops
, object
, annex
, readbuf
, writebuf
,
4086 offset
, len
, xfered_len
);
4088 /* GDB calculates all the addresses in possibly larget width of the address.
4089 Address width needs to be masked before its final use - either by
4090 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4092 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4094 if (object
== TARGET_OBJECT_MEMORY
)
4096 int addr_bit
= gdbarch_addr_bit (target_gdbarch ());
4098 if (addr_bit
< (sizeof (ULONGEST
) * HOST_CHAR_BIT
))
4099 offset
&= ((ULONGEST
) 1 << addr_bit
) - 1;
4102 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4103 offset
, len
, xfered_len
);
4104 if (xfer
!= TARGET_XFER_EOF
)
4107 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4108 offset
, len
, xfered_len
);
4112 cleanup_target_stop (void *arg
)
4114 ptid_t
*ptid
= (ptid_t
*) arg
;
4116 gdb_assert (arg
!= NULL
);
4119 target_resume (*ptid
, 0, GDB_SIGNAL_0
);
4122 static VEC(static_tracepoint_marker_p
) *
4123 linux_child_static_tracepoint_markers_by_strid (struct target_ops
*self
,
4126 char s
[IPA_CMD_BUF_SIZE
];
4127 struct cleanup
*old_chain
;
4128 int pid
= ptid_get_pid (inferior_ptid
);
4129 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
4130 struct static_tracepoint_marker
*marker
= NULL
;
4132 ptid_t ptid
= ptid_build (pid
, 0, 0);
4137 memcpy (s
, "qTfSTM", sizeof ("qTfSTM"));
4138 s
[sizeof ("qTfSTM")] = 0;
4140 agent_run_command (pid
, s
, strlen (s
) + 1);
4142 old_chain
= make_cleanup (free_current_marker
, &marker
);
4143 make_cleanup (cleanup_target_stop
, &ptid
);
4148 marker
= XCNEW (struct static_tracepoint_marker
);
4152 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
4154 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
4156 VEC_safe_push (static_tracepoint_marker_p
,
4162 release_static_tracepoint_marker (marker
);
4163 memset (marker
, 0, sizeof (*marker
));
4166 while (*p
++ == ','); /* comma-separated list */
4168 memcpy (s
, "qTsSTM", sizeof ("qTsSTM"));
4169 s
[sizeof ("qTsSTM")] = 0;
4170 agent_run_command (pid
, s
, strlen (s
) + 1);
4174 do_cleanups (old_chain
);
4179 /* Create a prototype generic GNU/Linux target. The client can override
4180 it with local methods. */
4183 linux_target_install_ops (struct target_ops
*t
)
4185 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4186 t
->to_remove_fork_catchpoint
= linux_child_remove_fork_catchpoint
;
4187 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4188 t
->to_remove_vfork_catchpoint
= linux_child_remove_vfork_catchpoint
;
4189 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4190 t
->to_remove_exec_catchpoint
= linux_child_remove_exec_catchpoint
;
4191 t
->to_set_syscall_catchpoint
= linux_child_set_syscall_catchpoint
;
4192 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4193 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4194 t
->to_post_attach
= linux_child_post_attach
;
4195 t
->to_follow_fork
= linux_child_follow_fork
;
4197 super_xfer_partial
= t
->to_xfer_partial
;
4198 t
->to_xfer_partial
= linux_xfer_partial
;
4200 t
->to_static_tracepoint_markers_by_strid
4201 = linux_child_static_tracepoint_markers_by_strid
;
4207 struct target_ops
*t
;
4209 t
= inf_ptrace_target ();
4210 linux_target_install_ops (t
);
4216 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4218 struct target_ops
*t
;
4220 t
= inf_ptrace_trad_target (register_u_offset
);
4221 linux_target_install_ops (t
);
4226 /* target_is_async_p implementation. */
4229 linux_nat_is_async_p (struct target_ops
*ops
)
4231 return linux_is_async_p ();
4234 /* target_can_async_p implementation. */
4237 linux_nat_can_async_p (struct target_ops
*ops
)
4239 /* NOTE: palves 2008-03-21: We're only async when the user requests
4240 it explicitly with the "set target-async" command.
4241 Someday, linux will always be async. */
4242 return target_async_permitted
;
4246 linux_nat_supports_non_stop (struct target_ops
*self
)
4251 /* to_always_non_stop_p implementation. */
4254 linux_nat_always_non_stop_p (struct target_ops
*self
)
4259 /* True if we want to support multi-process. To be removed when GDB
4260 supports multi-exec. */
4262 int linux_multi_process
= 1;
4265 linux_nat_supports_multi_process (struct target_ops
*self
)
4267 return linux_multi_process
;
4271 linux_nat_supports_disable_randomization (struct target_ops
*self
)
4273 #ifdef HAVE_PERSONALITY
4280 static int async_terminal_is_ours
= 1;
4282 /* target_terminal_inferior implementation.
4284 This is a wrapper around child_terminal_inferior to add async support. */
4287 linux_nat_terminal_inferior (struct target_ops
*self
)
4289 child_terminal_inferior (self
);
4291 /* Calls to target_terminal_*() are meant to be idempotent. */
4292 if (!async_terminal_is_ours
)
4295 delete_file_handler (input_fd
);
4296 async_terminal_is_ours
= 0;
4300 /* target_terminal_ours implementation.
4302 This is a wrapper around child_terminal_ours to add async support (and
4303 implement the target_terminal_ours vs target_terminal_ours_for_output
4304 distinction). child_terminal_ours is currently no different than
4305 child_terminal_ours_for_output.
4306 We leave target_terminal_ours_for_output alone, leaving it to
4307 child_terminal_ours_for_output. */
4310 linux_nat_terminal_ours (struct target_ops
*self
)
4312 /* GDB should never give the terminal to the inferior if the
4313 inferior is running in the background (run&, continue&, etc.),
4314 but claiming it sure should. */
4315 child_terminal_ours (self
);
4317 if (async_terminal_is_ours
)
4320 clear_sigint_trap ();
4321 add_file_handler (input_fd
, stdin_event_handler
, 0);
4322 async_terminal_is_ours
= 1;
4325 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4326 so we notice when any child changes state, and notify the
4327 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4328 above to wait for the arrival of a SIGCHLD. */
4331 sigchld_handler (int signo
)
4333 int old_errno
= errno
;
4335 if (debug_linux_nat
)
4336 ui_file_write_async_safe (gdb_stdlog
,
4337 "sigchld\n", sizeof ("sigchld\n") - 1);
4339 if (signo
== SIGCHLD
4340 && linux_nat_event_pipe
[0] != -1)
4341 async_file_mark (); /* Let the event loop know that there are
4342 events to handle. */
4347 /* Callback registered with the target events file descriptor. */
4350 handle_target_event (int error
, gdb_client_data client_data
)
4352 inferior_event_handler (INF_REG_EVENT
, NULL
);
4355 /* Create/destroy the target events pipe. Returns previous state. */
4358 linux_async_pipe (int enable
)
4360 int previous
= linux_is_async_p ();
4362 if (previous
!= enable
)
4366 /* Block child signals while we create/destroy the pipe, as
4367 their handler writes to it. */
4368 block_child_signals (&prev_mask
);
4372 if (gdb_pipe_cloexec (linux_nat_event_pipe
) == -1)
4373 internal_error (__FILE__
, __LINE__
,
4374 "creating event pipe failed.");
4376 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4377 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4381 close (linux_nat_event_pipe
[0]);
4382 close (linux_nat_event_pipe
[1]);
4383 linux_nat_event_pipe
[0] = -1;
4384 linux_nat_event_pipe
[1] = -1;
4387 restore_child_signals_mask (&prev_mask
);
4393 /* target_async implementation. */
4396 linux_nat_async (struct target_ops
*ops
, int enable
)
4400 if (!linux_async_pipe (1))
4402 add_file_handler (linux_nat_event_pipe
[0],
4403 handle_target_event
, NULL
);
4404 /* There may be pending events to handle. Tell the event loop
4411 delete_file_handler (linux_nat_event_pipe
[0]);
4412 linux_async_pipe (0);
4417 /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
4421 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4425 if (debug_linux_nat
)
4426 fprintf_unfiltered (gdb_stdlog
,
4427 "LNSL: running -> suspending %s\n",
4428 target_pid_to_str (lwp
->ptid
));
4431 if (lwp
->last_resume_kind
== resume_stop
)
4433 if (debug_linux_nat
)
4434 fprintf_unfiltered (gdb_stdlog
,
4435 "linux-nat: already stopping LWP %ld at "
4437 ptid_get_lwp (lwp
->ptid
));
4441 stop_callback (lwp
, NULL
);
4442 lwp
->last_resume_kind
= resume_stop
;
4446 /* Already known to be stopped; do nothing. */
4448 if (debug_linux_nat
)
4450 if (find_thread_ptid (lwp
->ptid
)->stop_requested
)
4451 fprintf_unfiltered (gdb_stdlog
,
4452 "LNSL: already stopped/stop_requested %s\n",
4453 target_pid_to_str (lwp
->ptid
));
4455 fprintf_unfiltered (gdb_stdlog
,
4456 "LNSL: already stopped/no "
4457 "stop_requested yet %s\n",
4458 target_pid_to_str (lwp
->ptid
));
4465 linux_nat_stop (struct target_ops
*self
, ptid_t ptid
)
4467 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
4471 linux_nat_interrupt (struct target_ops
*self
, ptid_t ptid
)
4474 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
4476 linux_ops
->to_interrupt (linux_ops
, ptid
);
4480 linux_nat_close (struct target_ops
*self
)
4482 /* Unregister from the event loop. */
4483 if (linux_nat_is_async_p (self
))
4484 linux_nat_async (self
, 0);
4486 if (linux_ops
->to_close
)
4487 linux_ops
->to_close (linux_ops
);
4492 /* When requests are passed down from the linux-nat layer to the
4493 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
4494 used. The address space pointer is stored in the inferior object,
4495 but the common code that is passed such ptid can't tell whether
4496 lwpid is a "main" process id or not (it assumes so). We reverse
4497 look up the "main" process id from the lwp here. */
4499 static struct address_space
*
4500 linux_nat_thread_address_space (struct target_ops
*t
, ptid_t ptid
)
4502 struct lwp_info
*lwp
;
4503 struct inferior
*inf
;
4506 if (ptid_get_lwp (ptid
) == 0)
4508 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
4510 lwp
= find_lwp_pid (ptid
);
4511 pid
= ptid_get_pid (lwp
->ptid
);
4515 /* A (pid,lwpid,0) ptid. */
4516 pid
= ptid_get_pid (ptid
);
4519 inf
= find_inferior_pid (pid
);
4520 gdb_assert (inf
!= NULL
);
4524 /* Return the cached value of the processor core for thread PTID. */
4527 linux_nat_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
4529 struct lwp_info
*info
= find_lwp_pid (ptid
);
4536 /* Implementation of to_filesystem_is_local. */
4539 linux_nat_filesystem_is_local (struct target_ops
*ops
)
4541 struct inferior
*inf
= current_inferior ();
4543 if (inf
->fake_pid_p
|| inf
->pid
== 0)
4546 return linux_ns_same (inf
->pid
, LINUX_NS_MNT
);
4549 /* Convert the INF argument passed to a to_fileio_* method
4550 to a process ID suitable for passing to its corresponding
4551 linux_mntns_* function. If INF is non-NULL then the
4552 caller is requesting the filesystem seen by INF. If INF
4553 is NULL then the caller is requesting the filesystem seen
4554 by the GDB. We fall back to GDB's filesystem in the case
4555 that INF is non-NULL but its PID is unknown. */
4558 linux_nat_fileio_pid_of (struct inferior
*inf
)
4560 if (inf
== NULL
|| inf
->fake_pid_p
|| inf
->pid
== 0)
4566 /* Implementation of to_fileio_open. */
4569 linux_nat_fileio_open (struct target_ops
*self
,
4570 struct inferior
*inf
, const char *filename
,
4571 int flags
, int mode
, int warn_if_slow
,
4578 if (fileio_to_host_openflags (flags
, &nat_flags
) == -1
4579 || fileio_to_host_mode (mode
, &nat_mode
) == -1)
4581 *target_errno
= FILEIO_EINVAL
;
4585 fd
= linux_mntns_open_cloexec (linux_nat_fileio_pid_of (inf
),
4586 filename
, nat_flags
, nat_mode
);
4588 *target_errno
= host_to_fileio_error (errno
);
4593 /* Implementation of to_fileio_readlink. */
4596 linux_nat_fileio_readlink (struct target_ops
*self
,
4597 struct inferior
*inf
, const char *filename
,
4604 len
= linux_mntns_readlink (linux_nat_fileio_pid_of (inf
),
4605 filename
, buf
, sizeof (buf
));
4608 *target_errno
= host_to_fileio_error (errno
);
4612 ret
= (char *) xmalloc (len
+ 1);
4613 memcpy (ret
, buf
, len
);
4618 /* Implementation of to_fileio_unlink. */
4621 linux_nat_fileio_unlink (struct target_ops
*self
,
4622 struct inferior
*inf
, const char *filename
,
4627 ret
= linux_mntns_unlink (linux_nat_fileio_pid_of (inf
),
4630 *target_errno
= host_to_fileio_error (errno
);
4636 linux_nat_add_target (struct target_ops
*t
)
4638 /* Save the provided single-threaded target. We save this in a separate
4639 variable because another target we've inherited from (e.g. inf-ptrace)
4640 may have saved a pointer to T; we want to use it for the final
4641 process stratum target. */
4642 linux_ops_saved
= *t
;
4643 linux_ops
= &linux_ops_saved
;
4645 /* Override some methods for multithreading. */
4646 t
->to_create_inferior
= linux_nat_create_inferior
;
4647 t
->to_attach
= linux_nat_attach
;
4648 t
->to_detach
= linux_nat_detach
;
4649 t
->to_resume
= linux_nat_resume
;
4650 t
->to_wait
= linux_nat_wait
;
4651 t
->to_pass_signals
= linux_nat_pass_signals
;
4652 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4653 t
->to_kill
= linux_nat_kill
;
4654 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4655 t
->to_thread_alive
= linux_nat_thread_alive
;
4656 t
->to_update_thread_list
= linux_nat_update_thread_list
;
4657 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4658 t
->to_thread_name
= linux_nat_thread_name
;
4659 t
->to_has_thread_control
= tc_schedlock
;
4660 t
->to_thread_address_space
= linux_nat_thread_address_space
;
4661 t
->to_stopped_by_watchpoint
= linux_nat_stopped_by_watchpoint
;
4662 t
->to_stopped_data_address
= linux_nat_stopped_data_address
;
4663 t
->to_stopped_by_sw_breakpoint
= linux_nat_stopped_by_sw_breakpoint
;
4664 t
->to_supports_stopped_by_sw_breakpoint
= linux_nat_supports_stopped_by_sw_breakpoint
;
4665 t
->to_stopped_by_hw_breakpoint
= linux_nat_stopped_by_hw_breakpoint
;
4666 t
->to_supports_stopped_by_hw_breakpoint
= linux_nat_supports_stopped_by_hw_breakpoint
;
4668 t
->to_can_async_p
= linux_nat_can_async_p
;
4669 t
->to_is_async_p
= linux_nat_is_async_p
;
4670 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
4671 t
->to_always_non_stop_p
= linux_nat_always_non_stop_p
;
4672 t
->to_async
= linux_nat_async
;
4673 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4674 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4676 super_close
= t
->to_close
;
4677 t
->to_close
= linux_nat_close
;
4679 t
->to_stop
= linux_nat_stop
;
4680 t
->to_interrupt
= linux_nat_interrupt
;
4682 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
4684 t
->to_supports_disable_randomization
4685 = linux_nat_supports_disable_randomization
;
4687 t
->to_core_of_thread
= linux_nat_core_of_thread
;
4689 t
->to_filesystem_is_local
= linux_nat_filesystem_is_local
;
4690 t
->to_fileio_open
= linux_nat_fileio_open
;
4691 t
->to_fileio_readlink
= linux_nat_fileio_readlink
;
4692 t
->to_fileio_unlink
= linux_nat_fileio_unlink
;
4694 /* We don't change the stratum; this target will sit at
4695 process_stratum and thread_db will set at thread_stratum. This
4696 is a little strange, since this is a multi-threaded-capable
4697 target, but we want to be on the stack below thread_db, and we
4698 also want to be used for single-threaded processes. */
4703 /* Register a method to call whenever a new thread is attached. */
4705 linux_nat_set_new_thread (struct target_ops
*t
,
4706 void (*new_thread
) (struct lwp_info
*))
4708 /* Save the pointer. We only support a single registered instance
4709 of the GNU/Linux native target, so we do not need to map this to
4711 linux_nat_new_thread
= new_thread
;
4714 /* See declaration in linux-nat.h. */
4717 linux_nat_set_new_fork (struct target_ops
*t
,
4718 linux_nat_new_fork_ftype
*new_fork
)
4720 /* Save the pointer. */
4721 linux_nat_new_fork
= new_fork
;
4724 /* See declaration in linux-nat.h. */
4727 linux_nat_set_forget_process (struct target_ops
*t
,
4728 linux_nat_forget_process_ftype
*fn
)
4730 /* Save the pointer. */
4731 linux_nat_forget_process_hook
= fn
;
4734 /* See declaration in linux-nat.h. */
4737 linux_nat_forget_process (pid_t pid
)
4739 if (linux_nat_forget_process_hook
!= NULL
)
4740 linux_nat_forget_process_hook (pid
);
4743 /* Register a method that converts a siginfo object between the layout
4744 that ptrace returns, and the layout in the architecture of the
4747 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
4748 int (*siginfo_fixup
) (siginfo_t
*,
4752 /* Save the pointer. */
4753 linux_nat_siginfo_fixup
= siginfo_fixup
;
4756 /* Register a method to call prior to resuming a thread. */
4759 linux_nat_set_prepare_to_resume (struct target_ops
*t
,
4760 void (*prepare_to_resume
) (struct lwp_info
*))
4762 /* Save the pointer. */
4763 linux_nat_prepare_to_resume
= prepare_to_resume
;
4766 /* See linux-nat.h. */
4769 linux_nat_get_siginfo (ptid_t ptid
, siginfo_t
*siginfo
)
4773 pid
= ptid_get_lwp (ptid
);
4775 pid
= ptid_get_pid (ptid
);
4778 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, siginfo
);
4781 memset (siginfo
, 0, sizeof (*siginfo
));
4787 /* See nat/linux-nat.h. */
4790 current_lwp_ptid (void)
4792 gdb_assert (ptid_lwp_p (inferior_ptid
));
4793 return inferior_ptid
;
4796 /* Provide a prototype to silence -Wmissing-prototypes. */
4797 extern initialize_file_ftype _initialize_linux_nat
;
4800 _initialize_linux_nat (void)
4802 add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance
,
4803 &debug_linux_nat
, _("\
4804 Set debugging of GNU/Linux lwp module."), _("\
4805 Show debugging of GNU/Linux lwp module."), _("\
4806 Enables printf debugging output."),
4808 show_debug_linux_nat
,
4809 &setdebuglist
, &showdebuglist
);
4811 add_setshow_boolean_cmd ("linux-namespaces", class_maintenance
,
4812 &debug_linux_namespaces
, _("\
4813 Set debugging of GNU/Linux namespaces module."), _("\
4814 Show debugging of GNU/Linux namespaces module."), _("\
4815 Enables printf debugging output."),
4818 &setdebuglist
, &showdebuglist
);
4820 /* Save this mask as the default. */
4821 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4823 /* Install a SIGCHLD handler. */
4824 sigchld_action
.sa_handler
= sigchld_handler
;
4825 sigemptyset (&sigchld_action
.sa_mask
);
4826 sigchld_action
.sa_flags
= SA_RESTART
;
4828 /* Make it the default. */
4829 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
4831 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4832 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
4833 sigdelset (&suspend_mask
, SIGCHLD
);
4835 sigemptyset (&blocked_mask
);
4839 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4840 the GNU/Linux Threads library and therefore doesn't really belong
4843 /* Return the set of signals used by the threads library in *SET. */
4846 lin_thread_get_thread_signals (sigset_t
*set
)
4850 /* NPTL reserves the first two RT signals, but does not provide any
4851 way for the debugger to query the signal numbers - fortunately
4852 they don't change. */
4853 sigaddset (set
, __SIGRTMIN
);
4854 sigaddset (set
, __SIGRTMIN
+ 1);