1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "nat/linux-nat.h"
25 #include "nat/linux-waitpid.h"
27 #ifdef HAVE_TKILL_SYSCALL
29 #include <sys/syscall.h>
31 #include "nat/gdb_ptrace.h"
32 #include "linux-nat.h"
33 #include "nat/linux-ptrace.h"
34 #include "nat/linux-procfs.h"
35 #include "nat/linux-personality.h"
36 #include "linux-fork.h"
37 #include "gdbthread.h"
41 #include "inf-child.h"
42 #include "inf-ptrace.h"
44 #include <sys/procfs.h> /* for elf_gregset etc. */
45 #include "elf-bfd.h" /* for elfcore_write_* */
46 #include "gregset.h" /* for gregset */
47 #include "gdbcore.h" /* for get_exec_file */
48 #include <ctype.h> /* for isdigit */
49 #include <sys/stat.h> /* for struct stat */
50 #include <fcntl.h> /* for O_RDONLY */
52 #include "event-loop.h"
53 #include "event-top.h"
55 #include <sys/types.h>
57 #include "xml-support.h"
60 #include "nat/linux-osdata.h"
61 #include "linux-tdep.h"
64 #include "tracepoint.h"
66 #include "target-descriptions.h"
67 #include "filestuff.h"
69 #include "nat/linux-namespaces.h"
73 #define SPUFS_MAGIC 0x23c9b64e
76 /* This comment documents high-level logic of this file.
78 Waiting for events in sync mode
79 ===============================
81 When waiting for an event in a specific thread, we just use waitpid, passing
82 the specific pid, and not passing WNOHANG.
84 When waiting for an event in all threads, waitpid is not quite good. Prior to
85 version 2.4, Linux can either wait for event in main thread, or in secondary
86 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
87 miss an event. The solution is to use non-blocking waitpid, together with
88 sigsuspend. First, we use non-blocking waitpid to get an event in the main
89 process, if any. Second, we use non-blocking waitpid with the __WCLONED
90 flag to check for events in cloned processes. If nothing is found, we use
91 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
92 happened to a child process -- and SIGCHLD will be delivered both for events
93 in main debugged process and in cloned processes. As soon as we know there's
94 an event, we get back to calling nonblocking waitpid with and without
97 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
98 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
99 blocked, the signal becomes pending and sigsuspend immediately
100 notices it and returns.
102 Waiting for events in async mode
103 ================================
105 In async mode, GDB should always be ready to handle both user input
106 and target events, so neither blocking waitpid nor sigsuspend are
107 viable options. Instead, we should asynchronously notify the GDB main
108 event loop whenever there's an unprocessed event from the target. We
109 detect asynchronous target events by handling SIGCHLD signals. To
110 notify the event loop about target events, the self-pipe trick is used
111 --- a pipe is registered as waitable event source in the event loop,
112 the event loop select/poll's on the read end of this pipe (as well on
113 other event sources, e.g., stdin), and the SIGCHLD handler writes a
114 byte to this pipe. This is more portable than relying on
115 pselect/ppoll, since on kernels that lack those syscalls, libc
116 emulates them with select/poll+sigprocmask, and that is racy
117 (a.k.a. plain broken).
119 Obviously, if we fail to notify the event loop if there's a target
120 event, it's bad. OTOH, if we notify the event loop when there's no
121 event from the target, linux_nat_wait will detect that there's no real
122 event to report, and return event of type TARGET_WAITKIND_IGNORE.
123 This is mostly harmless, but it will waste time and is better avoided.
125 The main design point is that every time GDB is outside linux-nat.c,
126 we have a SIGCHLD handler installed that is called when something
127 happens to the target and notifies the GDB event loop. Whenever GDB
128 core decides to handle the event, and calls into linux-nat.c, we
129 process things as in sync mode, except that the we never block in
132 While processing an event, we may end up momentarily blocked in
133 waitpid calls. Those waitpid calls, while blocking, are guarantied to
134 return quickly. E.g., in all-stop mode, before reporting to the core
135 that an LWP hit a breakpoint, all LWPs are stopped by sending them
136 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
137 Note that this is different from blocking indefinitely waiting for the
138 next event --- here, we're already handling an event.
143 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
144 signal is not entirely significant; we just need for a signal to be delivered,
145 so that we can intercept it. SIGSTOP's advantage is that it can not be
146 blocked. A disadvantage is that it is not a real-time signal, so it can only
147 be queued once; we do not keep track of other sources of SIGSTOP.
149 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
150 use them, because they have special behavior when the signal is generated -
151 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
152 kills the entire thread group.
154 A delivered SIGSTOP would stop the entire thread group, not just the thread we
155 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
156 cancel it (by PTRACE_CONT without passing SIGSTOP).
158 We could use a real-time signal instead. This would solve those problems; we
159 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
160 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
161 generates it, and there are races with trying to find a signal that is not
165 #define O_LARGEFILE 0
168 /* Does the current host support PTRACE_GETREGSET? */
169 enum tribool have_ptrace_getregset
= TRIBOOL_UNKNOWN
;
171 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
172 the use of the multi-threaded target. */
173 static struct target_ops
*linux_ops
;
174 static struct target_ops linux_ops_saved
;
176 /* The method to call, if any, when a new thread is attached. */
177 static void (*linux_nat_new_thread
) (struct lwp_info
*);
179 /* The method to call, if any, when a new fork is attached. */
180 static linux_nat_new_fork_ftype
*linux_nat_new_fork
;
182 /* The method to call, if any, when a process is no longer
184 static linux_nat_forget_process_ftype
*linux_nat_forget_process_hook
;
186 /* Hook to call prior to resuming a thread. */
187 static void (*linux_nat_prepare_to_resume
) (struct lwp_info
*);
189 /* The method to call, if any, when the siginfo object needs to be
190 converted between the layout returned by ptrace, and the layout in
191 the architecture of the inferior. */
192 static int (*linux_nat_siginfo_fixup
) (siginfo_t
*,
196 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
197 Called by our to_xfer_partial. */
198 static target_xfer_partial_ftype
*super_xfer_partial
;
200 /* The saved to_close method, inherited from inf-ptrace.c.
201 Called by our to_close. */
202 static void (*super_close
) (struct target_ops
*);
204 static unsigned int debug_linux_nat
;
206 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
207 struct cmd_list_element
*c
, const char *value
)
209 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
213 struct simple_pid_list
217 struct simple_pid_list
*next
;
219 struct simple_pid_list
*stopped_pids
;
221 /* Async mode support. */
223 /* The read/write ends of the pipe registered as waitable file in the
225 static int linux_nat_event_pipe
[2] = { -1, -1 };
227 /* True if we're currently in async mode. */
228 #define linux_is_async_p() (linux_nat_event_pipe[0] != -1)
230 /* Flush the event pipe. */
233 async_file_flush (void)
240 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
242 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
245 /* Put something (anything, doesn't matter what, or how much) in event
246 pipe, so that the select/poll in the event-loop realizes we have
247 something to process. */
250 async_file_mark (void)
254 /* It doesn't really matter what the pipe contains, as long we end
255 up with something in it. Might as well flush the previous
261 ret
= write (linux_nat_event_pipe
[1], "+", 1);
263 while (ret
== -1 && errno
== EINTR
);
265 /* Ignore EAGAIN. If the pipe is full, the event loop will already
266 be awakened anyway. */
269 static int kill_lwp (int lwpid
, int signo
);
271 static int stop_callback (struct lwp_info
*lp
, void *data
);
272 static int resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
);
274 static void block_child_signals (sigset_t
*prev_mask
);
275 static void restore_child_signals_mask (sigset_t
*prev_mask
);
278 static struct lwp_info
*add_lwp (ptid_t ptid
);
279 static void purge_lwp_list (int pid
);
280 static void delete_lwp (ptid_t ptid
);
281 static struct lwp_info
*find_lwp_pid (ptid_t ptid
);
283 static int lwp_status_pending_p (struct lwp_info
*lp
);
285 static int check_stopped_by_breakpoint (struct lwp_info
*lp
);
286 static int sigtrap_is_event (int status
);
287 static int (*linux_nat_status_is_event
) (int status
) = sigtrap_is_event
;
292 /* See nat/linux-nat.h. */
295 ptid_of_lwp (struct lwp_info
*lwp
)
300 /* See nat/linux-nat.h. */
303 lwp_set_arch_private_info (struct lwp_info
*lwp
,
304 struct arch_lwp_info
*info
)
306 lwp
->arch_private
= info
;
309 /* See nat/linux-nat.h. */
311 struct arch_lwp_info
*
312 lwp_arch_private_info (struct lwp_info
*lwp
)
314 return lwp
->arch_private
;
317 /* See nat/linux-nat.h. */
320 lwp_is_stopped (struct lwp_info
*lwp
)
325 /* See nat/linux-nat.h. */
327 enum target_stop_reason
328 lwp_stop_reason (struct lwp_info
*lwp
)
330 return lwp
->stop_reason
;
334 /* Trivial list manipulation functions to keep track of a list of
335 new stopped processes. */
337 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
339 struct simple_pid_list
*new_pid
= XNEW (struct simple_pid_list
);
342 new_pid
->status
= status
;
343 new_pid
->next
= *listp
;
348 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
350 struct simple_pid_list
**p
;
352 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
353 if ((*p
)->pid
== pid
)
355 struct simple_pid_list
*next
= (*p
)->next
;
357 *statusp
= (*p
)->status
;
365 /* Return the ptrace options that we want to try to enable. */
368 linux_nat_ptrace_options (int attached
)
373 options
|= PTRACE_O_EXITKILL
;
375 options
|= (PTRACE_O_TRACESYSGOOD
376 | PTRACE_O_TRACEVFORKDONE
377 | PTRACE_O_TRACEVFORK
379 | PTRACE_O_TRACEEXEC
);
384 /* Initialize ptrace warnings and check for supported ptrace
387 ATTACHED should be nonzero iff we attached to the inferior. */
390 linux_init_ptrace (pid_t pid
, int attached
)
392 int options
= linux_nat_ptrace_options (attached
);
394 linux_enable_event_reporting (pid
, options
);
395 linux_ptrace_init_warnings ();
399 linux_child_post_attach (struct target_ops
*self
, int pid
)
401 linux_init_ptrace (pid
, 1);
405 linux_child_post_startup_inferior (struct target_ops
*self
, ptid_t ptid
)
407 linux_init_ptrace (ptid_get_pid (ptid
), 0);
410 /* Return the number of known LWPs in the tgid given by PID. */
418 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
419 if (ptid_get_pid (lp
->ptid
) == pid
)
425 /* Call delete_lwp with prototype compatible for make_cleanup. */
428 delete_lwp_cleanup (void *lp_voidp
)
430 struct lwp_info
*lp
= (struct lwp_info
*) lp_voidp
;
432 delete_lwp (lp
->ptid
);
435 /* Target hook for follow_fork. On entry inferior_ptid must be the
436 ptid of the followed inferior. At return, inferior_ptid will be
440 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
,
445 struct lwp_info
*child_lp
= NULL
;
446 int status
= W_STOPCODE (0);
447 struct cleanup
*old_chain
;
449 ptid_t parent_ptid
, child_ptid
;
450 int parent_pid
, child_pid
;
452 has_vforked
= (inferior_thread ()->pending_follow
.kind
453 == TARGET_WAITKIND_VFORKED
);
454 parent_ptid
= inferior_ptid
;
455 child_ptid
= inferior_thread ()->pending_follow
.value
.related_pid
;
456 parent_pid
= ptid_get_lwp (parent_ptid
);
457 child_pid
= ptid_get_lwp (child_ptid
);
459 /* We're already attached to the parent, by default. */
460 old_chain
= save_inferior_ptid ();
461 inferior_ptid
= child_ptid
;
462 child_lp
= add_lwp (inferior_ptid
);
463 child_lp
->stopped
= 1;
464 child_lp
->last_resume_kind
= resume_stop
;
466 /* Detach new forked process? */
469 make_cleanup (delete_lwp_cleanup
, child_lp
);
471 if (linux_nat_prepare_to_resume
!= NULL
)
472 linux_nat_prepare_to_resume (child_lp
);
474 /* When debugging an inferior in an architecture that supports
475 hardware single stepping on a kernel without commit
476 6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child
477 process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits
478 set if the parent process had them set.
479 To work around this, single step the child process
480 once before detaching to clear the flags. */
482 if (!gdbarch_software_single_step_p (target_thread_architecture
485 linux_disable_event_reporting (child_pid
);
486 if (ptrace (PTRACE_SINGLESTEP
, child_pid
, 0, 0) < 0)
487 perror_with_name (_("Couldn't do single step"));
488 if (my_waitpid (child_pid
, &status
, 0) < 0)
489 perror_with_name (_("Couldn't wait vfork process"));
492 if (WIFSTOPPED (status
))
496 signo
= WSTOPSIG (status
);
498 && !signal_pass_state (gdb_signal_from_host (signo
)))
500 ptrace (PTRACE_DETACH
, child_pid
, 0, signo
);
503 /* Resets value of inferior_ptid to parent ptid. */
504 do_cleanups (old_chain
);
508 /* Let the thread_db layer learn about this new process. */
509 check_for_thread_db ();
512 do_cleanups (old_chain
);
516 struct lwp_info
*parent_lp
;
518 parent_lp
= find_lwp_pid (parent_ptid
);
519 gdb_assert (linux_supports_tracefork () >= 0);
521 if (linux_supports_tracevforkdone ())
524 fprintf_unfiltered (gdb_stdlog
,
525 "LCFF: waiting for VFORK_DONE on %d\n",
527 parent_lp
->stopped
= 1;
529 /* We'll handle the VFORK_DONE event like any other
530 event, in target_wait. */
534 /* We can't insert breakpoints until the child has
535 finished with the shared memory region. We need to
536 wait until that happens. Ideal would be to just
538 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
539 - waitpid (parent_pid, &status, __WALL);
540 However, most architectures can't handle a syscall
541 being traced on the way out if it wasn't traced on
544 We might also think to loop, continuing the child
545 until it exits or gets a SIGTRAP. One problem is
546 that the child might call ptrace with PTRACE_TRACEME.
548 There's no simple and reliable way to figure out when
549 the vforked child will be done with its copy of the
550 shared memory. We could step it out of the syscall,
551 two instructions, let it go, and then single-step the
552 parent once. When we have hardware single-step, this
553 would work; with software single-step it could still
554 be made to work but we'd have to be able to insert
555 single-step breakpoints in the child, and we'd have
556 to insert -just- the single-step breakpoint in the
557 parent. Very awkward.
559 In the end, the best we can do is to make sure it
560 runs for a little while. Hopefully it will be out of
561 range of any breakpoints we reinsert. Usually this
562 is only the single-step breakpoint at vfork's return
566 fprintf_unfiltered (gdb_stdlog
,
567 "LCFF: no VFORK_DONE "
568 "support, sleeping a bit\n");
572 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
573 and leave it pending. The next linux_nat_resume call
574 will notice a pending event, and bypasses actually
575 resuming the inferior. */
576 parent_lp
->status
= 0;
577 parent_lp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
578 parent_lp
->stopped
= 1;
580 /* If we're in async mode, need to tell the event loop
581 there's something here to process. */
582 if (target_is_async_p ())
589 struct lwp_info
*child_lp
;
591 child_lp
= add_lwp (inferior_ptid
);
592 child_lp
->stopped
= 1;
593 child_lp
->last_resume_kind
= resume_stop
;
595 /* Let the thread_db layer learn about this new process. */
596 check_for_thread_db ();
604 linux_child_insert_fork_catchpoint (struct target_ops
*self
, int pid
)
606 return !linux_supports_tracefork ();
610 linux_child_remove_fork_catchpoint (struct target_ops
*self
, int pid
)
616 linux_child_insert_vfork_catchpoint (struct target_ops
*self
, int pid
)
618 return !linux_supports_tracefork ();
622 linux_child_remove_vfork_catchpoint (struct target_ops
*self
, int pid
)
628 linux_child_insert_exec_catchpoint (struct target_ops
*self
, int pid
)
630 return !linux_supports_tracefork ();
634 linux_child_remove_exec_catchpoint (struct target_ops
*self
, int pid
)
640 linux_child_set_syscall_catchpoint (struct target_ops
*self
,
641 int pid
, int needed
, int any_count
,
642 int table_size
, int *table
)
644 if (!linux_supports_tracesysgood ())
647 /* On GNU/Linux, we ignore the arguments. It means that we only
648 enable the syscall catchpoints, but do not disable them.
650 Also, we do not use the `table' information because we do not
651 filter system calls here. We let GDB do the logic for us. */
655 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
656 are processes sharing the same VM space. A multi-threaded process
657 is basically a group of such processes. However, such a grouping
658 is almost entirely a user-space issue; the kernel doesn't enforce
659 such a grouping at all (this might change in the future). In
660 general, we'll rely on the threads library (i.e. the GNU/Linux
661 Threads library) to provide such a grouping.
663 It is perfectly well possible to write a multi-threaded application
664 without the assistance of a threads library, by using the clone
665 system call directly. This module should be able to give some
666 rudimentary support for debugging such applications if developers
667 specify the CLONE_PTRACE flag in the clone system call, and are
668 using the Linux kernel 2.4 or above.
670 Note that there are some peculiarities in GNU/Linux that affect
673 - In general one should specify the __WCLONE flag to waitpid in
674 order to make it report events for any of the cloned processes
675 (and leave it out for the initial process). However, if a cloned
676 process has exited the exit status is only reported if the
677 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
678 we cannot use it since GDB must work on older systems too.
680 - When a traced, cloned process exits and is waited for by the
681 debugger, the kernel reassigns it to the original parent and
682 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
683 library doesn't notice this, which leads to the "zombie problem":
684 When debugged a multi-threaded process that spawns a lot of
685 threads will run out of processes, even if the threads exit,
686 because the "zombies" stay around. */
688 /* List of known LWPs. */
689 struct lwp_info
*lwp_list
;
692 /* Original signal mask. */
693 static sigset_t normal_mask
;
695 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
696 _initialize_linux_nat. */
697 static sigset_t suspend_mask
;
699 /* Signals to block to make that sigsuspend work. */
700 static sigset_t blocked_mask
;
702 /* SIGCHLD action. */
703 struct sigaction sigchld_action
;
705 /* Block child signals (SIGCHLD and linux threads signals), and store
706 the previous mask in PREV_MASK. */
709 block_child_signals (sigset_t
*prev_mask
)
711 /* Make sure SIGCHLD is blocked. */
712 if (!sigismember (&blocked_mask
, SIGCHLD
))
713 sigaddset (&blocked_mask
, SIGCHLD
);
715 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
718 /* Restore child signals mask, previously returned by
719 block_child_signals. */
722 restore_child_signals_mask (sigset_t
*prev_mask
)
724 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
727 /* Mask of signals to pass directly to the inferior. */
728 static sigset_t pass_mask
;
730 /* Update signals to pass to the inferior. */
732 linux_nat_pass_signals (struct target_ops
*self
,
733 int numsigs
, unsigned char *pass_signals
)
737 sigemptyset (&pass_mask
);
739 for (signo
= 1; signo
< NSIG
; signo
++)
741 int target_signo
= gdb_signal_from_host (signo
);
742 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
743 sigaddset (&pass_mask
, signo
);
749 /* Prototypes for local functions. */
750 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
751 static int linux_thread_alive (ptid_t ptid
);
752 static char *linux_child_pid_to_exec_file (struct target_ops
*self
, int pid
);
753 static int resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
);
757 /* Destroy and free LP. */
760 lwp_free (struct lwp_info
*lp
)
762 xfree (lp
->arch_private
);
766 /* Remove all LWPs belong to PID from the lwp list. */
769 purge_lwp_list (int pid
)
771 struct lwp_info
*lp
, *lpprev
, *lpnext
;
775 for (lp
= lwp_list
; lp
; lp
= lpnext
)
779 if (ptid_get_pid (lp
->ptid
) == pid
)
784 lpprev
->next
= lp
->next
;
793 /* Add the LWP specified by PTID to the list. PTID is the first LWP
794 in the process. Return a pointer to the structure describing the
797 This differs from add_lwp in that we don't let the arch specific
798 bits know about this new thread. Current clients of this callback
799 take the opportunity to install watchpoints in the new thread, and
800 we shouldn't do that for the first thread. If we're spawning a
801 child ("run"), the thread executes the shell wrapper first, and we
802 shouldn't touch it until it execs the program we want to debug.
803 For "attach", it'd be okay to call the callback, but it's not
804 necessary, because watchpoints can't yet have been inserted into
807 static struct lwp_info
*
808 add_initial_lwp (ptid_t ptid
)
812 gdb_assert (ptid_lwp_p (ptid
));
814 lp
= XNEW (struct lwp_info
);
816 memset (lp
, 0, sizeof (struct lwp_info
));
818 lp
->last_resume_kind
= resume_continue
;
819 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
830 /* Add the LWP specified by PID to the list. Return a pointer to the
831 structure describing the new LWP. The LWP should already be
834 static struct lwp_info
*
835 add_lwp (ptid_t ptid
)
839 lp
= add_initial_lwp (ptid
);
841 /* Let the arch specific bits know about this new thread. Current
842 clients of this callback take the opportunity to install
843 watchpoints in the new thread. We don't do this for the first
844 thread though. See add_initial_lwp. */
845 if (linux_nat_new_thread
!= NULL
)
846 linux_nat_new_thread (lp
);
851 /* Remove the LWP specified by PID from the list. */
854 delete_lwp (ptid_t ptid
)
856 struct lwp_info
*lp
, *lpprev
;
860 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
861 if (ptid_equal (lp
->ptid
, ptid
))
868 lpprev
->next
= lp
->next
;
875 /* Return a pointer to the structure describing the LWP corresponding
876 to PID. If no corresponding LWP could be found, return NULL. */
878 static struct lwp_info
*
879 find_lwp_pid (ptid_t ptid
)
884 if (ptid_lwp_p (ptid
))
885 lwp
= ptid_get_lwp (ptid
);
887 lwp
= ptid_get_pid (ptid
);
889 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
890 if (lwp
== ptid_get_lwp (lp
->ptid
))
896 /* See nat/linux-nat.h. */
899 iterate_over_lwps (ptid_t filter
,
900 iterate_over_lwps_ftype callback
,
903 struct lwp_info
*lp
, *lpnext
;
905 for (lp
= lwp_list
; lp
; lp
= lpnext
)
909 if (ptid_match (lp
->ptid
, filter
))
911 if ((*callback
) (lp
, data
) != 0)
919 /* Update our internal state when changing from one checkpoint to
920 another indicated by NEW_PTID. We can only switch single-threaded
921 applications, so we only create one new LWP, and the previous list
925 linux_nat_switch_fork (ptid_t new_ptid
)
929 purge_lwp_list (ptid_get_pid (inferior_ptid
));
931 lp
= add_lwp (new_ptid
);
934 /* This changes the thread's ptid while preserving the gdb thread
935 num. Also changes the inferior pid, while preserving the
937 thread_change_ptid (inferior_ptid
, new_ptid
);
939 /* We've just told GDB core that the thread changed target id, but,
940 in fact, it really is a different thread, with different register
942 registers_changed ();
945 /* Handle the exit of a single thread LP. */
948 exit_lwp (struct lwp_info
*lp
)
950 struct thread_info
*th
= find_thread_ptid (lp
->ptid
);
954 if (print_thread_events
)
955 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
957 delete_thread (lp
->ptid
);
960 delete_lwp (lp
->ptid
);
963 /* Wait for the LWP specified by LP, which we have just attached to.
964 Returns a wait status for that LWP, to cache. */
967 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
970 pid_t new_pid
, pid
= ptid_get_lwp (ptid
);
973 if (linux_proc_pid_is_stopped (pid
))
976 fprintf_unfiltered (gdb_stdlog
,
977 "LNPAW: Attaching to a stopped process\n");
979 /* The process is definitely stopped. It is in a job control
980 stop, unless the kernel predates the TASK_STOPPED /
981 TASK_TRACED distinction, in which case it might be in a
982 ptrace stop. Make sure it is in a ptrace stop; from there we
983 can kill it, signal it, et cetera.
985 First make sure there is a pending SIGSTOP. Since we are
986 already attached, the process can not transition from stopped
987 to running without a PTRACE_CONT; so we know this signal will
988 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
989 probably already in the queue (unless this kernel is old
990 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
991 is not an RT signal, it can only be queued once. */
992 kill_lwp (pid
, SIGSTOP
);
994 /* Finally, resume the stopped process. This will deliver the SIGSTOP
995 (or a higher priority signal, just like normal PTRACE_ATTACH). */
996 ptrace (PTRACE_CONT
, pid
, 0, 0);
999 /* Make sure the initial process is stopped. The user-level threads
1000 layer might want to poke around in the inferior, and that won't
1001 work if things haven't stabilized yet. */
1002 new_pid
= my_waitpid (pid
, &status
, 0);
1003 if (new_pid
== -1 && errno
== ECHILD
)
1006 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1008 /* Try again with __WCLONE to check cloned processes. */
1009 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1013 gdb_assert (pid
== new_pid
);
1015 if (!WIFSTOPPED (status
))
1017 /* The pid we tried to attach has apparently just exited. */
1018 if (debug_linux_nat
)
1019 fprintf_unfiltered (gdb_stdlog
, "LNPAW: Failed to stop %d: %s",
1020 pid
, status_to_str (status
));
1024 if (WSTOPSIG (status
) != SIGSTOP
)
1027 if (debug_linux_nat
)
1028 fprintf_unfiltered (gdb_stdlog
,
1029 "LNPAW: Received %s after attaching\n",
1030 status_to_str (status
));
1037 linux_nat_create_inferior (struct target_ops
*ops
,
1038 char *exec_file
, char *allargs
, char **env
,
1041 struct cleanup
*restore_personality
1042 = maybe_disable_address_space_randomization (disable_randomization
);
1044 /* The fork_child mechanism is synchronous and calls target_wait, so
1045 we have to mask the async mode. */
1047 /* Make sure we report all signals during startup. */
1048 linux_nat_pass_signals (ops
, 0, NULL
);
1050 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1052 do_cleanups (restore_personality
);
1055 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1056 already attached. Returns true if a new LWP is found, false
1060 attach_proc_task_lwp_callback (ptid_t ptid
)
1062 struct lwp_info
*lp
;
1064 /* Ignore LWPs we're already attached to. */
1065 lp
= find_lwp_pid (ptid
);
1068 int lwpid
= ptid_get_lwp (ptid
);
1070 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) < 0)
1074 /* Be quiet if we simply raced with the thread exiting.
1075 EPERM is returned if the thread's task still exists, and
1076 is marked as exited or zombie, as well as other
1077 conditions, so in that case, confirm the status in
1078 /proc/PID/status. */
1080 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
1082 if (debug_linux_nat
)
1084 fprintf_unfiltered (gdb_stdlog
,
1085 "Cannot attach to lwp %d: "
1086 "thread is gone (%d: %s)\n",
1087 lwpid
, err
, safe_strerror (err
));
1092 warning (_("Cannot attach to lwp %d: %s"),
1094 linux_ptrace_attach_fail_reason_string (ptid
,
1100 if (debug_linux_nat
)
1101 fprintf_unfiltered (gdb_stdlog
,
1102 "PTRACE_ATTACH %s, 0, 0 (OK)\n",
1103 target_pid_to_str (ptid
));
1105 lp
= add_lwp (ptid
);
1108 /* The next time we wait for this LWP we'll see a SIGSTOP as
1109 PTRACE_ATTACH brings it to a halt. */
1112 /* We need to wait for a stop before being able to make the
1113 next ptrace call on this LWP. */
1114 lp
->must_set_ptrace_flags
= 1;
1123 linux_nat_attach (struct target_ops
*ops
, const char *args
, int from_tty
)
1125 struct lwp_info
*lp
;
1129 /* Make sure we report all signals during attach. */
1130 linux_nat_pass_signals (ops
, 0, NULL
);
1134 linux_ops
->to_attach (ops
, args
, from_tty
);
1136 CATCH (ex
, RETURN_MASK_ERROR
)
1138 pid_t pid
= parse_pid_to_attach (args
);
1139 struct buffer buffer
;
1140 char *message
, *buffer_s
;
1142 message
= xstrdup (ex
.message
);
1143 make_cleanup (xfree
, message
);
1145 buffer_init (&buffer
);
1146 linux_ptrace_attach_fail_reason (pid
, &buffer
);
1148 buffer_grow_str0 (&buffer
, "");
1149 buffer_s
= buffer_finish (&buffer
);
1150 make_cleanup (xfree
, buffer_s
);
1152 if (*buffer_s
!= '\0')
1153 throw_error (ex
.error
, "warning: %s\n%s", buffer_s
, message
);
1155 throw_error (ex
.error
, "%s", message
);
1159 /* The ptrace base target adds the main thread with (pid,0,0)
1160 format. Decorate it with lwp info. */
1161 ptid
= ptid_build (ptid_get_pid (inferior_ptid
),
1162 ptid_get_pid (inferior_ptid
),
1164 thread_change_ptid (inferior_ptid
, ptid
);
1166 /* Add the initial process as the first LWP to the list. */
1167 lp
= add_initial_lwp (ptid
);
1169 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1171 if (!WIFSTOPPED (status
))
1173 if (WIFEXITED (status
))
1175 int exit_code
= WEXITSTATUS (status
);
1177 target_terminal_ours ();
1178 target_mourn_inferior ();
1180 error (_("Unable to attach: program exited normally."));
1182 error (_("Unable to attach: program exited with code %d."),
1185 else if (WIFSIGNALED (status
))
1187 enum gdb_signal signo
;
1189 target_terminal_ours ();
1190 target_mourn_inferior ();
1192 signo
= gdb_signal_from_host (WTERMSIG (status
));
1193 error (_("Unable to attach: program terminated with signal "
1195 gdb_signal_to_name (signo
),
1196 gdb_signal_to_string (signo
));
1199 internal_error (__FILE__
, __LINE__
,
1200 _("unexpected status %d for PID %ld"),
1201 status
, (long) ptid_get_lwp (ptid
));
1206 /* Save the wait status to report later. */
1208 if (debug_linux_nat
)
1209 fprintf_unfiltered (gdb_stdlog
,
1210 "LNA: waitpid %ld, saving status %s\n",
1211 (long) ptid_get_pid (lp
->ptid
), status_to_str (status
));
1213 lp
->status
= status
;
1215 /* We must attach to every LWP. If /proc is mounted, use that to
1216 find them now. The inferior may be using raw clone instead of
1217 using pthreads. But even if it is using pthreads, thread_db
1218 walks structures in the inferior's address space to find the list
1219 of threads/LWPs, and those structures may well be corrupted.
1220 Note that once thread_db is loaded, we'll still use it to list
1221 threads and associate pthread info with each LWP. */
1222 linux_proc_attach_tgid_threads (ptid_get_pid (lp
->ptid
),
1223 attach_proc_task_lwp_callback
);
1225 if (target_can_async_p ())
1229 /* Get pending status of LP. */
1231 get_pending_status (struct lwp_info
*lp
, int *status
)
1233 enum gdb_signal signo
= GDB_SIGNAL_0
;
1235 /* If we paused threads momentarily, we may have stored pending
1236 events in lp->status or lp->waitstatus (see stop_wait_callback),
1237 and GDB core hasn't seen any signal for those threads.
1238 Otherwise, the last signal reported to the core is found in the
1239 thread object's stop_signal.
1241 There's a corner case that isn't handled here at present. Only
1242 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1243 stop_signal make sense as a real signal to pass to the inferior.
1244 Some catchpoint related events, like
1245 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1246 to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1247 those traps are debug API (ptrace in our case) related and
1248 induced; the inferior wouldn't see them if it wasn't being
1249 traced. Hence, we should never pass them to the inferior, even
1250 when set to pass state. Since this corner case isn't handled by
1251 infrun.c when proceeding with a signal, for consistency, neither
1252 do we handle it here (or elsewhere in the file we check for
1253 signal pass state). Normally SIGTRAP isn't set to pass state, so
1254 this is really a corner case. */
1256 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
1257 signo
= GDB_SIGNAL_0
; /* a pending ptrace event, not a real signal. */
1258 else if (lp
->status
)
1259 signo
= gdb_signal_from_host (WSTOPSIG (lp
->status
));
1260 else if (target_is_non_stop_p () && !is_executing (lp
->ptid
))
1262 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1264 signo
= tp
->suspend
.stop_signal
;
1266 else if (!target_is_non_stop_p ())
1268 struct target_waitstatus last
;
1271 get_last_target_status (&last_ptid
, &last
);
1273 if (ptid_get_lwp (lp
->ptid
) == ptid_get_lwp (last_ptid
))
1275 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1277 signo
= tp
->suspend
.stop_signal
;
1283 if (signo
== GDB_SIGNAL_0
)
1285 if (debug_linux_nat
)
1286 fprintf_unfiltered (gdb_stdlog
,
1287 "GPT: lwp %s has no pending signal\n",
1288 target_pid_to_str (lp
->ptid
));
1290 else if (!signal_pass_state (signo
))
1292 if (debug_linux_nat
)
1293 fprintf_unfiltered (gdb_stdlog
,
1294 "GPT: lwp %s had signal %s, "
1295 "but it is in no pass state\n",
1296 target_pid_to_str (lp
->ptid
),
1297 gdb_signal_to_string (signo
));
1301 *status
= W_STOPCODE (gdb_signal_to_host (signo
));
1303 if (debug_linux_nat
)
1304 fprintf_unfiltered (gdb_stdlog
,
1305 "GPT: lwp %s has pending signal %s\n",
1306 target_pid_to_str (lp
->ptid
),
1307 gdb_signal_to_string (signo
));
1314 detach_callback (struct lwp_info
*lp
, void *data
)
1316 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1318 if (debug_linux_nat
&& lp
->status
)
1319 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1320 strsignal (WSTOPSIG (lp
->status
)),
1321 target_pid_to_str (lp
->ptid
));
1323 /* If there is a pending SIGSTOP, get rid of it. */
1326 if (debug_linux_nat
)
1327 fprintf_unfiltered (gdb_stdlog
,
1328 "DC: Sending SIGCONT to %s\n",
1329 target_pid_to_str (lp
->ptid
));
1331 kill_lwp (ptid_get_lwp (lp
->ptid
), SIGCONT
);
1335 /* We don't actually detach from the LWP that has an id equal to the
1336 overall process id just yet. */
1337 if (ptid_get_lwp (lp
->ptid
) != ptid_get_pid (lp
->ptid
))
1341 /* Pass on any pending signal for this LWP. */
1342 get_pending_status (lp
, &status
);
1344 if (linux_nat_prepare_to_resume
!= NULL
)
1345 linux_nat_prepare_to_resume (lp
);
1347 if (ptrace (PTRACE_DETACH
, ptid_get_lwp (lp
->ptid
), 0,
1348 WSTOPSIG (status
)) < 0)
1349 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1350 safe_strerror (errno
));
1352 if (debug_linux_nat
)
1353 fprintf_unfiltered (gdb_stdlog
,
1354 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1355 target_pid_to_str (lp
->ptid
),
1356 strsignal (WSTOPSIG (status
)));
1358 delete_lwp (lp
->ptid
);
1365 linux_nat_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
1369 struct lwp_info
*main_lwp
;
1371 pid
= ptid_get_pid (inferior_ptid
);
1373 /* Don't unregister from the event loop, as there may be other
1374 inferiors running. */
1376 /* Stop all threads before detaching. ptrace requires that the
1377 thread is stopped to sucessfully detach. */
1378 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1379 /* ... and wait until all of them have reported back that
1380 they're no longer running. */
1381 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1383 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1385 /* Only the initial process should be left right now. */
1386 gdb_assert (num_lwps (ptid_get_pid (inferior_ptid
)) == 1);
1388 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1390 /* Pass on any pending signal for the last LWP. */
1391 if ((args
== NULL
|| *args
== '\0')
1392 && get_pending_status (main_lwp
, &status
) != -1
1393 && WIFSTOPPED (status
))
1397 /* Put the signal number in ARGS so that inf_ptrace_detach will
1398 pass it along with PTRACE_DETACH. */
1399 tem
= (char *) alloca (8);
1400 xsnprintf (tem
, 8, "%d", (int) WSTOPSIG (status
));
1402 if (debug_linux_nat
)
1403 fprintf_unfiltered (gdb_stdlog
,
1404 "LND: Sending signal %s to %s\n",
1406 target_pid_to_str (main_lwp
->ptid
));
1409 if (linux_nat_prepare_to_resume
!= NULL
)
1410 linux_nat_prepare_to_resume (main_lwp
);
1411 delete_lwp (main_lwp
->ptid
);
1413 if (forks_exist_p ())
1415 /* Multi-fork case. The current inferior_ptid is being detached
1416 from, but there are other viable forks to debug. Detach from
1417 the current fork, and context-switch to the first
1419 linux_fork_detach (args
, from_tty
);
1422 linux_ops
->to_detach (ops
, args
, from_tty
);
1425 /* Resume execution of the inferior process. If STEP is nonzero,
1426 single-step it. If SIGNAL is nonzero, give it that signal. */
1429 linux_resume_one_lwp_throw (struct lwp_info
*lp
, int step
,
1430 enum gdb_signal signo
)
1434 /* stop_pc doubles as the PC the LWP had when it was last resumed.
1435 We only presently need that if the LWP is stepped though (to
1436 handle the case of stepping a breakpoint instruction). */
1439 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
1441 lp
->stop_pc
= regcache_read_pc (regcache
);
1446 if (linux_nat_prepare_to_resume
!= NULL
)
1447 linux_nat_prepare_to_resume (lp
);
1448 linux_ops
->to_resume (linux_ops
, lp
->ptid
, step
, signo
);
1450 /* Successfully resumed. Clear state that no longer makes sense,
1451 and mark the LWP as running. Must not do this before resuming
1452 otherwise if that fails other code will be confused. E.g., we'd
1453 later try to stop the LWP and hang forever waiting for a stop
1454 status. Note that we must not throw after this is cleared,
1455 otherwise handle_zombie_lwp_error would get confused. */
1457 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1458 registers_changed_ptid (lp
->ptid
);
1461 /* Called when we try to resume a stopped LWP and that errors out. If
1462 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
1463 or about to become), discard the error, clear any pending status
1464 the LWP may have, and return true (we'll collect the exit status
1465 soon enough). Otherwise, return false. */
1468 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
1470 /* If we get an error after resuming the LWP successfully, we'd
1471 confuse !T state for the LWP being gone. */
1472 gdb_assert (lp
->stopped
);
1474 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
1475 because even if ptrace failed with ESRCH, the tracee may be "not
1476 yet fully dead", but already refusing ptrace requests. In that
1477 case the tracee has 'R (Running)' state for a little bit
1478 (observed in Linux 3.18). See also the note on ESRCH in the
1479 ptrace(2) man page. Instead, check whether the LWP has any state
1480 other than ptrace-stopped. */
1482 /* Don't assume anything if /proc/PID/status can't be read. */
1483 if (linux_proc_pid_is_trace_stopped_nowarn (ptid_get_lwp (lp
->ptid
)) == 0)
1485 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1487 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1493 /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
1494 disappears while we try to resume it. */
1497 linux_resume_one_lwp (struct lwp_info
*lp
, int step
, enum gdb_signal signo
)
1501 linux_resume_one_lwp_throw (lp
, step
, signo
);
1503 CATCH (ex
, RETURN_MASK_ERROR
)
1505 if (!check_ptrace_stopped_lwp_gone (lp
))
1506 throw_exception (ex
);
1514 resume_lwp (struct lwp_info
*lp
, int step
, enum gdb_signal signo
)
1518 struct inferior
*inf
= find_inferior_ptid (lp
->ptid
);
1520 if (inf
->vfork_child
!= NULL
)
1522 if (debug_linux_nat
)
1523 fprintf_unfiltered (gdb_stdlog
,
1524 "RC: Not resuming %s (vfork parent)\n",
1525 target_pid_to_str (lp
->ptid
));
1527 else if (!lwp_status_pending_p (lp
))
1529 if (debug_linux_nat
)
1530 fprintf_unfiltered (gdb_stdlog
,
1531 "RC: Resuming sibling %s, %s, %s\n",
1532 target_pid_to_str (lp
->ptid
),
1533 (signo
!= GDB_SIGNAL_0
1534 ? strsignal (gdb_signal_to_host (signo
))
1536 step
? "step" : "resume");
1538 linux_resume_one_lwp (lp
, step
, signo
);
1542 if (debug_linux_nat
)
1543 fprintf_unfiltered (gdb_stdlog
,
1544 "RC: Not resuming sibling %s (has pending)\n",
1545 target_pid_to_str (lp
->ptid
));
1550 if (debug_linux_nat
)
1551 fprintf_unfiltered (gdb_stdlog
,
1552 "RC: Not resuming sibling %s (not stopped)\n",
1553 target_pid_to_str (lp
->ptid
));
1557 /* Callback for iterate_over_lwps. If LWP is EXCEPT, do nothing.
1558 Resume LWP with the last stop signal, if it is in pass state. */
1561 linux_nat_resume_callback (struct lwp_info
*lp
, void *except
)
1563 enum gdb_signal signo
= GDB_SIGNAL_0
;
1570 struct thread_info
*thread
;
1572 thread
= find_thread_ptid (lp
->ptid
);
1575 signo
= thread
->suspend
.stop_signal
;
1576 thread
->suspend
.stop_signal
= GDB_SIGNAL_0
;
1580 resume_lwp (lp
, 0, signo
);
1585 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1588 lp
->last_resume_kind
= resume_stop
;
1593 resume_set_callback (struct lwp_info
*lp
, void *data
)
1596 lp
->last_resume_kind
= resume_continue
;
1601 linux_nat_resume (struct target_ops
*ops
,
1602 ptid_t ptid
, int step
, enum gdb_signal signo
)
1604 struct lwp_info
*lp
;
1607 if (debug_linux_nat
)
1608 fprintf_unfiltered (gdb_stdlog
,
1609 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1610 step
? "step" : "resume",
1611 target_pid_to_str (ptid
),
1612 (signo
!= GDB_SIGNAL_0
1613 ? strsignal (gdb_signal_to_host (signo
)) : "0"),
1614 target_pid_to_str (inferior_ptid
));
1616 /* A specific PTID means `step only this process id'. */
1617 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
1618 || ptid_is_pid (ptid
));
1620 /* Mark the lwps we're resuming as resumed. */
1621 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
1623 /* See if it's the current inferior that should be handled
1626 lp
= find_lwp_pid (inferior_ptid
);
1628 lp
= find_lwp_pid (ptid
);
1629 gdb_assert (lp
!= NULL
);
1631 /* Remember if we're stepping. */
1632 lp
->last_resume_kind
= step
? resume_step
: resume_continue
;
1634 /* If we have a pending wait status for this thread, there is no
1635 point in resuming the process. But first make sure that
1636 linux_nat_wait won't preemptively handle the event - we
1637 should never take this short-circuit if we are going to
1638 leave LP running, since we have skipped resuming all the
1639 other threads. This bit of code needs to be synchronized
1640 with linux_nat_wait. */
1642 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1645 && WSTOPSIG (lp
->status
)
1646 && sigismember (&pass_mask
, WSTOPSIG (lp
->status
)))
1648 if (debug_linux_nat
)
1649 fprintf_unfiltered (gdb_stdlog
,
1650 "LLR: Not short circuiting for ignored "
1651 "status 0x%x\n", lp
->status
);
1653 /* FIXME: What should we do if we are supposed to continue
1654 this thread with a signal? */
1655 gdb_assert (signo
== GDB_SIGNAL_0
);
1656 signo
= gdb_signal_from_host (WSTOPSIG (lp
->status
));
1661 if (lwp_status_pending_p (lp
))
1663 /* FIXME: What should we do if we are supposed to continue
1664 this thread with a signal? */
1665 gdb_assert (signo
== GDB_SIGNAL_0
);
1667 if (debug_linux_nat
)
1668 fprintf_unfiltered (gdb_stdlog
,
1669 "LLR: Short circuiting for status 0x%x\n",
1672 if (target_can_async_p ())
1675 /* Tell the event loop we have something to process. */
1682 iterate_over_lwps (ptid
, linux_nat_resume_callback
, lp
);
1684 if (debug_linux_nat
)
1685 fprintf_unfiltered (gdb_stdlog
,
1686 "LLR: %s %s, %s (resume event thread)\n",
1687 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1688 target_pid_to_str (lp
->ptid
),
1689 (signo
!= GDB_SIGNAL_0
1690 ? strsignal (gdb_signal_to_host (signo
)) : "0"));
1692 linux_resume_one_lwp (lp
, step
, signo
);
1694 if (target_can_async_p ())
1698 /* Send a signal to an LWP. */
1701 kill_lwp (int lwpid
, int signo
)
1703 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1704 fails, then we are not using nptl threads and we should be using kill. */
1706 #ifdef HAVE_TKILL_SYSCALL
1708 static int tkill_failed
;
1715 ret
= syscall (__NR_tkill
, lwpid
, signo
);
1716 if (errno
!= ENOSYS
)
1723 return kill (lwpid
, signo
);
1726 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
1727 event, check if the core is interested in it: if not, ignore the
1728 event, and keep waiting; otherwise, we need to toggle the LWP's
1729 syscall entry/exit status, since the ptrace event itself doesn't
1730 indicate it, and report the trap to higher layers. */
1733 linux_handle_syscall_trap (struct lwp_info
*lp
, int stopping
)
1735 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1736 struct gdbarch
*gdbarch
= target_thread_architecture (lp
->ptid
);
1737 int syscall_number
= (int) gdbarch_get_syscall_number (gdbarch
, lp
->ptid
);
1741 /* If we're stopping threads, there's a SIGSTOP pending, which
1742 makes it so that the LWP reports an immediate syscall return,
1743 followed by the SIGSTOP. Skip seeing that "return" using
1744 PTRACE_CONT directly, and let stop_wait_callback collect the
1745 SIGSTOP. Later when the thread is resumed, a new syscall
1746 entry event. If we didn't do this (and returned 0), we'd
1747 leave a syscall entry pending, and our caller, by using
1748 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
1749 itself. Later, when the user re-resumes this LWP, we'd see
1750 another syscall entry event and we'd mistake it for a return.
1752 If stop_wait_callback didn't force the SIGSTOP out of the LWP
1753 (leaving immediately with LWP->signalled set, without issuing
1754 a PTRACE_CONT), it would still be problematic to leave this
1755 syscall enter pending, as later when the thread is resumed,
1756 it would then see the same syscall exit mentioned above,
1757 followed by the delayed SIGSTOP, while the syscall didn't
1758 actually get to execute. It seems it would be even more
1759 confusing to the user. */
1761 if (debug_linux_nat
)
1762 fprintf_unfiltered (gdb_stdlog
,
1763 "LHST: ignoring syscall %d "
1764 "for LWP %ld (stopping threads), "
1765 "resuming with PTRACE_CONT for SIGSTOP\n",
1767 ptid_get_lwp (lp
->ptid
));
1769 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
1770 ptrace (PTRACE_CONT
, ptid_get_lwp (lp
->ptid
), 0, 0);
1775 /* Always update the entry/return state, even if this particular
1776 syscall isn't interesting to the core now. In async mode,
1777 the user could install a new catchpoint for this syscall
1778 between syscall enter/return, and we'll need to know to
1779 report a syscall return if that happens. */
1780 lp
->syscall_state
= (lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1781 ? TARGET_WAITKIND_SYSCALL_RETURN
1782 : TARGET_WAITKIND_SYSCALL_ENTRY
);
1784 if (catch_syscall_enabled ())
1786 if (catching_syscall_number (syscall_number
))
1788 /* Alright, an event to report. */
1789 ourstatus
->kind
= lp
->syscall_state
;
1790 ourstatus
->value
.syscall_number
= syscall_number
;
1792 if (debug_linux_nat
)
1793 fprintf_unfiltered (gdb_stdlog
,
1794 "LHST: stopping for %s of syscall %d"
1797 == TARGET_WAITKIND_SYSCALL_ENTRY
1798 ? "entry" : "return",
1800 ptid_get_lwp (lp
->ptid
));
1804 if (debug_linux_nat
)
1805 fprintf_unfiltered (gdb_stdlog
,
1806 "LHST: ignoring %s of syscall %d "
1808 lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1809 ? "entry" : "return",
1811 ptid_get_lwp (lp
->ptid
));
1815 /* If we had been syscall tracing, and hence used PT_SYSCALL
1816 before on this LWP, it could happen that the user removes all
1817 syscall catchpoints before we get to process this event.
1818 There are two noteworthy issues here:
1820 - When stopped at a syscall entry event, resuming with
1821 PT_STEP still resumes executing the syscall and reports a
1824 - Only PT_SYSCALL catches syscall enters. If we last
1825 single-stepped this thread, then this event can't be a
1826 syscall enter. If we last single-stepped this thread, this
1827 has to be a syscall exit.
1829 The points above mean that the next resume, be it PT_STEP or
1830 PT_CONTINUE, can not trigger a syscall trace event. */
1831 if (debug_linux_nat
)
1832 fprintf_unfiltered (gdb_stdlog
,
1833 "LHST: caught syscall event "
1834 "with no syscall catchpoints."
1835 " %d for LWP %ld, ignoring\n",
1837 ptid_get_lwp (lp
->ptid
));
1838 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
1841 /* The core isn't interested in this event. For efficiency, avoid
1842 stopping all threads only to have the core resume them all again.
1843 Since we're not stopping threads, if we're still syscall tracing
1844 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
1845 subsequent syscall. Simply resume using the inf-ptrace layer,
1846 which knows when to use PT_SYSCALL or PT_CONTINUE. */
1848 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
1852 /* Handle a GNU/Linux extended wait response. If we see a clone
1853 event, we need to add the new LWP to our list (and not report the
1854 trap to higher layers). This function returns non-zero if the
1855 event should be ignored and we should wait again. If STOPPING is
1856 true, the new LWP remains stopped, otherwise it is continued. */
1859 linux_handle_extended_wait (struct lwp_info
*lp
, int status
)
1861 int pid
= ptid_get_lwp (lp
->ptid
);
1862 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1863 int event
= linux_ptrace_get_extended_event (status
);
1865 /* All extended events we currently use are mid-syscall. Only
1866 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
1867 you have to be using PTRACE_SEIZE to get that. */
1868 lp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
1870 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1871 || event
== PTRACE_EVENT_CLONE
)
1873 unsigned long new_pid
;
1876 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1878 /* If we haven't already seen the new PID stop, wait for it now. */
1879 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1881 /* The new child has a pending SIGSTOP. We can't affect it until it
1882 hits the SIGSTOP, but we're already attached. */
1883 ret
= my_waitpid (new_pid
, &status
,
1884 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
1886 perror_with_name (_("waiting for new child"));
1887 else if (ret
!= new_pid
)
1888 internal_error (__FILE__
, __LINE__
,
1889 _("wait returned unexpected PID %d"), ret
);
1890 else if (!WIFSTOPPED (status
))
1891 internal_error (__FILE__
, __LINE__
,
1892 _("wait returned unexpected status 0x%x"), status
);
1895 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
1897 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
1899 /* The arch-specific native code may need to know about new
1900 forks even if those end up never mapped to an
1902 if (linux_nat_new_fork
!= NULL
)
1903 linux_nat_new_fork (lp
, new_pid
);
1906 if (event
== PTRACE_EVENT_FORK
1907 && linux_fork_checkpointing_p (ptid_get_pid (lp
->ptid
)))
1909 /* Handle checkpointing by linux-fork.c here as a special
1910 case. We don't want the follow-fork-mode or 'catch fork'
1911 to interfere with this. */
1913 /* This won't actually modify the breakpoint list, but will
1914 physically remove the breakpoints from the child. */
1915 detach_breakpoints (ptid_build (new_pid
, new_pid
, 0));
1917 /* Retain child fork in ptrace (stopped) state. */
1918 if (!find_fork_pid (new_pid
))
1921 /* Report as spurious, so that infrun doesn't want to follow
1922 this fork. We're actually doing an infcall in
1924 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1926 /* Report the stop to the core. */
1930 if (event
== PTRACE_EVENT_FORK
)
1931 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1932 else if (event
== PTRACE_EVENT_VFORK
)
1933 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1934 else if (event
== PTRACE_EVENT_CLONE
)
1936 struct lwp_info
*new_lp
;
1938 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1940 if (debug_linux_nat
)
1941 fprintf_unfiltered (gdb_stdlog
,
1942 "LHEW: Got clone event "
1943 "from LWP %d, new child is LWP %ld\n",
1946 new_lp
= add_lwp (ptid_build (ptid_get_pid (lp
->ptid
), new_pid
, 0));
1948 new_lp
->stopped
= 1;
1949 new_lp
->resumed
= 1;
1951 /* If the thread_db layer is active, let it record the user
1952 level thread id and status, and add the thread to GDB's
1954 if (!thread_db_notice_clone (lp
->ptid
, new_lp
->ptid
))
1956 /* The process is not using thread_db. Add the LWP to
1958 target_post_attach (ptid_get_lwp (new_lp
->ptid
));
1959 add_thread (new_lp
->ptid
);
1962 /* Even if we're stopping the thread for some reason
1963 internal to this module, from the perspective of infrun
1964 and the user/frontend, this new thread is running until
1965 it next reports a stop. */
1966 set_running (new_lp
->ptid
, 1);
1967 set_executing (new_lp
->ptid
, 1);
1969 if (WSTOPSIG (status
) != SIGSTOP
)
1971 /* This can happen if someone starts sending signals to
1972 the new thread before it gets a chance to run, which
1973 have a lower number than SIGSTOP (e.g. SIGUSR1).
1974 This is an unlikely case, and harder to handle for
1975 fork / vfork than for clone, so we do not try - but
1976 we handle it for clone events here. */
1978 new_lp
->signalled
= 1;
1980 /* We created NEW_LP so it cannot yet contain STATUS. */
1981 gdb_assert (new_lp
->status
== 0);
1983 /* Save the wait status to report later. */
1984 if (debug_linux_nat
)
1985 fprintf_unfiltered (gdb_stdlog
,
1986 "LHEW: waitpid of new LWP %ld, "
1987 "saving status %s\n",
1988 (long) ptid_get_lwp (new_lp
->ptid
),
1989 status_to_str (status
));
1990 new_lp
->status
= status
;
1999 if (event
== PTRACE_EVENT_EXEC
)
2001 if (debug_linux_nat
)
2002 fprintf_unfiltered (gdb_stdlog
,
2003 "LHEW: Got exec event from LWP %ld\n",
2004 ptid_get_lwp (lp
->ptid
));
2006 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
2007 ourstatus
->value
.execd_pathname
2008 = xstrdup (linux_child_pid_to_exec_file (NULL
, pid
));
2010 /* The thread that execed must have been resumed, but, when a
2011 thread execs, it changes its tid to the tgid, and the old
2012 tgid thread might have not been resumed. */
2017 if (event
== PTRACE_EVENT_VFORK_DONE
)
2019 if (current_inferior ()->waiting_for_vfork_done
)
2021 if (debug_linux_nat
)
2022 fprintf_unfiltered (gdb_stdlog
,
2023 "LHEW: Got expected PTRACE_EVENT_"
2024 "VFORK_DONE from LWP %ld: stopping\n",
2025 ptid_get_lwp (lp
->ptid
));
2027 ourstatus
->kind
= TARGET_WAITKIND_VFORK_DONE
;
2031 if (debug_linux_nat
)
2032 fprintf_unfiltered (gdb_stdlog
,
2033 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2034 "from LWP %ld: ignoring\n",
2035 ptid_get_lwp (lp
->ptid
));
2039 internal_error (__FILE__
, __LINE__
,
2040 _("unknown ptrace event %d"), event
);
2043 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2047 wait_lwp (struct lwp_info
*lp
)
2051 int thread_dead
= 0;
2054 gdb_assert (!lp
->stopped
);
2055 gdb_assert (lp
->status
== 0);
2057 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2058 block_child_signals (&prev_mask
);
2062 /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2063 was right and we should just call sigsuspend. */
2065 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), &status
, WNOHANG
);
2066 if (pid
== -1 && errno
== ECHILD
)
2067 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), &status
, __WCLONE
| WNOHANG
);
2068 if (pid
== -1 && errno
== ECHILD
)
2070 /* The thread has previously exited. We need to delete it
2071 now because, for some vendor 2.4 kernels with NPTL
2072 support backported, there won't be an exit event unless
2073 it is the main thread. 2.6 kernels will report an exit
2074 event for each thread that exits, as expected. */
2076 if (debug_linux_nat
)
2077 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2078 target_pid_to_str (lp
->ptid
));
2083 /* Bugs 10970, 12702.
2084 Thread group leader may have exited in which case we'll lock up in
2085 waitpid if there are other threads, even if they are all zombies too.
2086 Basically, we're not supposed to use waitpid this way.
2087 __WCLONE is not applicable for the leader so we can't use that.
2088 LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2089 process; it gets ESRCH both for the zombie and for running processes.
2091 As a workaround, check if we're waiting for the thread group leader and
2092 if it's a zombie, and avoid calling waitpid if it is.
2094 This is racy, what if the tgl becomes a zombie right after we check?
2095 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2096 waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
2098 if (ptid_get_pid (lp
->ptid
) == ptid_get_lwp (lp
->ptid
)
2099 && linux_proc_pid_is_zombie (ptid_get_lwp (lp
->ptid
)))
2102 if (debug_linux_nat
)
2103 fprintf_unfiltered (gdb_stdlog
,
2104 "WL: Thread group leader %s vanished.\n",
2105 target_pid_to_str (lp
->ptid
));
2109 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2110 get invoked despite our caller had them intentionally blocked by
2111 block_child_signals. This is sensitive only to the loop of
2112 linux_nat_wait_1 and there if we get called my_waitpid gets called
2113 again before it gets to sigsuspend so we can safely let the handlers
2114 get executed here. */
2116 if (debug_linux_nat
)
2117 fprintf_unfiltered (gdb_stdlog
, "WL: about to sigsuspend\n");
2118 sigsuspend (&suspend_mask
);
2121 restore_child_signals_mask (&prev_mask
);
2125 gdb_assert (pid
== ptid_get_lwp (lp
->ptid
));
2127 if (debug_linux_nat
)
2129 fprintf_unfiltered (gdb_stdlog
,
2130 "WL: waitpid %s received %s\n",
2131 target_pid_to_str (lp
->ptid
),
2132 status_to_str (status
));
2135 /* Check if the thread has exited. */
2136 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2138 if (ptid_get_pid (lp
->ptid
) == ptid_get_lwp (lp
->ptid
))
2140 if (debug_linux_nat
)
2141 fprintf_unfiltered (gdb_stdlog
, "WL: Process %d exited.\n",
2142 ptid_get_pid (lp
->ptid
));
2144 /* This is the leader exiting, it means the whole
2145 process is gone. Store the status to report to the
2146 core. Store it in lp->waitstatus, because lp->status
2147 would be ambiguous (W_EXITCODE(0,0) == 0). */
2148 store_waitstatus (&lp
->waitstatus
, status
);
2153 if (debug_linux_nat
)
2154 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2155 target_pid_to_str (lp
->ptid
));
2165 gdb_assert (WIFSTOPPED (status
));
2168 if (lp
->must_set_ptrace_flags
)
2170 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
2171 int options
= linux_nat_ptrace_options (inf
->attach_flag
);
2173 linux_enable_event_reporting (ptid_get_lwp (lp
->ptid
), options
);
2174 lp
->must_set_ptrace_flags
= 0;
2177 /* Handle GNU/Linux's syscall SIGTRAPs. */
2178 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2180 /* No longer need the sysgood bit. The ptrace event ends up
2181 recorded in lp->waitstatus if we care for it. We can carry
2182 on handling the event like a regular SIGTRAP from here
2184 status
= W_STOPCODE (SIGTRAP
);
2185 if (linux_handle_syscall_trap (lp
, 1))
2186 return wait_lwp (lp
);
2190 /* Almost all other ptrace-stops are known to be outside of system
2191 calls, with further exceptions in linux_handle_extended_wait. */
2192 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2195 /* Handle GNU/Linux's extended waitstatus for trace events. */
2196 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
2197 && linux_is_extended_waitstatus (status
))
2199 if (debug_linux_nat
)
2200 fprintf_unfiltered (gdb_stdlog
,
2201 "WL: Handling extended status 0x%06x\n",
2203 linux_handle_extended_wait (lp
, status
);
2210 /* Send a SIGSTOP to LP. */
2213 stop_callback (struct lwp_info
*lp
, void *data
)
2215 if (!lp
->stopped
&& !lp
->signalled
)
2219 if (debug_linux_nat
)
2221 fprintf_unfiltered (gdb_stdlog
,
2222 "SC: kill %s **<SIGSTOP>**\n",
2223 target_pid_to_str (lp
->ptid
));
2226 ret
= kill_lwp (ptid_get_lwp (lp
->ptid
), SIGSTOP
);
2227 if (debug_linux_nat
)
2229 fprintf_unfiltered (gdb_stdlog
,
2230 "SC: lwp kill %d %s\n",
2232 errno
? safe_strerror (errno
) : "ERRNO-OK");
2236 gdb_assert (lp
->status
== 0);
2242 /* Request a stop on LWP. */
2245 linux_stop_lwp (struct lwp_info
*lwp
)
2247 stop_callback (lwp
, NULL
);
2250 /* See linux-nat.h */
2253 linux_stop_and_wait_all_lwps (void)
2255 /* Stop all LWP's ... */
2256 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
2258 /* ... and wait until all of them have reported back that
2259 they're no longer running. */
2260 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
2263 /* See linux-nat.h */
2266 linux_unstop_all_lwps (void)
2268 iterate_over_lwps (minus_one_ptid
,
2269 resume_stopped_resumed_lwps
, &minus_one_ptid
);
2272 /* Return non-zero if LWP PID has a pending SIGINT. */
2275 linux_nat_has_pending_sigint (int pid
)
2277 sigset_t pending
, blocked
, ignored
;
2279 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2281 if (sigismember (&pending
, SIGINT
)
2282 && !sigismember (&ignored
, SIGINT
))
2288 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2291 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2293 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2294 flag to consume the next one. */
2295 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2296 && WSTOPSIG (lp
->status
) == SIGINT
)
2299 lp
->ignore_sigint
= 1;
2304 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2305 This function is called after we know the LWP has stopped; if the LWP
2306 stopped before the expected SIGINT was delivered, then it will never have
2307 arrived. Also, if the signal was delivered to a shared queue and consumed
2308 by a different thread, it will never be delivered to this LWP. */
2311 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2313 if (!lp
->ignore_sigint
)
2316 if (!linux_nat_has_pending_sigint (ptid_get_lwp (lp
->ptid
)))
2318 if (debug_linux_nat
)
2319 fprintf_unfiltered (gdb_stdlog
,
2320 "MCIS: Clearing bogus flag for %s\n",
2321 target_pid_to_str (lp
->ptid
));
2322 lp
->ignore_sigint
= 0;
2326 /* Fetch the possible triggered data watchpoint info and store it in
2329 On some archs, like x86, that use debug registers to set
2330 watchpoints, it's possible that the way to know which watched
2331 address trapped, is to check the register that is used to select
2332 which address to watch. Problem is, between setting the watchpoint
2333 and reading back which data address trapped, the user may change
2334 the set of watchpoints, and, as a consequence, GDB changes the
2335 debug registers in the inferior. To avoid reading back a stale
2336 stopped-data-address when that happens, we cache in LP the fact
2337 that a watchpoint trapped, and the corresponding data address, as
2338 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2339 registers meanwhile, we have the cached data we can rely on. */
2342 check_stopped_by_watchpoint (struct lwp_info
*lp
)
2344 struct cleanup
*old_chain
;
2346 if (linux_ops
->to_stopped_by_watchpoint
== NULL
)
2349 old_chain
= save_inferior_ptid ();
2350 inferior_ptid
= lp
->ptid
;
2352 if (linux_ops
->to_stopped_by_watchpoint (linux_ops
))
2354 lp
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2356 if (linux_ops
->to_stopped_data_address
!= NULL
)
2357 lp
->stopped_data_address_p
=
2358 linux_ops
->to_stopped_data_address (¤t_target
,
2359 &lp
->stopped_data_address
);
2361 lp
->stopped_data_address_p
= 0;
2364 do_cleanups (old_chain
);
2366 return lp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2369 /* Called when the LWP stopped for a trap that could be explained by a
2370 watchpoint or a breakpoint. */
2373 save_sigtrap (struct lwp_info
*lp
)
2375 gdb_assert (lp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
);
2376 gdb_assert (lp
->status
!= 0);
2378 /* Check first if this was a SW/HW breakpoint before checking
2379 watchpoints, because at least s390 can't tell the data address of
2380 hardware watchpoint hits, and the kernel returns
2381 stopped-by-watchpoint as long as there's a watchpoint set. */
2382 if (linux_nat_status_is_event (lp
->status
))
2383 check_stopped_by_breakpoint (lp
);
2385 /* Note that TRAP_HWBKPT can indicate either a hardware breakpoint
2386 or hardware watchpoint. Check which is which if we got
2387 TARGET_STOPPED_BY_HW_BREAKPOINT. */
2388 if (lp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
2389 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
2390 check_stopped_by_watchpoint (lp
);
2393 /* Returns true if the LWP had stopped for a watchpoint. */
2396 linux_nat_stopped_by_watchpoint (struct target_ops
*ops
)
2398 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2400 gdb_assert (lp
!= NULL
);
2402 return lp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2406 linux_nat_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
2408 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2410 gdb_assert (lp
!= NULL
);
2412 *addr_p
= lp
->stopped_data_address
;
2414 return lp
->stopped_data_address_p
;
2417 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2420 sigtrap_is_event (int status
)
2422 return WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
;
2425 /* Set alternative SIGTRAP-like events recognizer. If
2426 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2430 linux_nat_set_status_is_event (struct target_ops
*t
,
2431 int (*status_is_event
) (int status
))
2433 linux_nat_status_is_event
= status_is_event
;
2436 /* Wait until LP is stopped. */
2439 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2441 struct inferior
*inf
= find_inferior_ptid (lp
->ptid
);
2443 /* If this is a vfork parent, bail out, it is not going to report
2444 any SIGSTOP until the vfork is done with. */
2445 if (inf
->vfork_child
!= NULL
)
2452 status
= wait_lwp (lp
);
2456 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2457 && WSTOPSIG (status
) == SIGINT
)
2459 lp
->ignore_sigint
= 0;
2462 ptrace (PTRACE_CONT
, ptid_get_lwp (lp
->ptid
), 0, 0);
2464 if (debug_linux_nat
)
2465 fprintf_unfiltered (gdb_stdlog
,
2466 "PTRACE_CONT %s, 0, 0 (%s) "
2467 "(discarding SIGINT)\n",
2468 target_pid_to_str (lp
->ptid
),
2469 errno
? safe_strerror (errno
) : "OK");
2471 return stop_wait_callback (lp
, NULL
);
2474 maybe_clear_ignore_sigint (lp
);
2476 if (WSTOPSIG (status
) != SIGSTOP
)
2478 /* The thread was stopped with a signal other than SIGSTOP. */
2480 if (debug_linux_nat
)
2481 fprintf_unfiltered (gdb_stdlog
,
2482 "SWC: Pending event %s in %s\n",
2483 status_to_str ((int) status
),
2484 target_pid_to_str (lp
->ptid
));
2486 /* Save the sigtrap event. */
2487 lp
->status
= status
;
2488 gdb_assert (lp
->signalled
);
2493 /* We caught the SIGSTOP that we intended to catch, so
2494 there's no SIGSTOP pending. */
2496 if (debug_linux_nat
)
2497 fprintf_unfiltered (gdb_stdlog
,
2498 "SWC: Expected SIGSTOP caught for %s.\n",
2499 target_pid_to_str (lp
->ptid
));
2501 /* Reset SIGNALLED only after the stop_wait_callback call
2502 above as it does gdb_assert on SIGNALLED. */
2510 /* Return non-zero if LP has a wait status pending. Discard the
2511 pending event and resume the LWP if the event that originally
2512 caused the stop became uninteresting. */
2515 status_callback (struct lwp_info
*lp
, void *data
)
2517 /* Only report a pending wait status if we pretend that this has
2518 indeed been resumed. */
2522 if (!lwp_status_pending_p (lp
))
2525 if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
2526 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
2528 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2529 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2533 pc
= regcache_read_pc (regcache
);
2535 if (pc
!= lp
->stop_pc
)
2537 if (debug_linux_nat
)
2538 fprintf_unfiltered (gdb_stdlog
,
2539 "SC: PC of %s changed. was=%s, now=%s\n",
2540 target_pid_to_str (lp
->ptid
),
2541 paddress (target_gdbarch (), lp
->stop_pc
),
2542 paddress (target_gdbarch (), pc
));
2546 #if !USE_SIGTRAP_SIGINFO
2547 else if (!breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
2549 if (debug_linux_nat
)
2550 fprintf_unfiltered (gdb_stdlog
,
2551 "SC: previous breakpoint of %s, at %s gone\n",
2552 target_pid_to_str (lp
->ptid
),
2553 paddress (target_gdbarch (), lp
->stop_pc
));
2561 if (debug_linux_nat
)
2562 fprintf_unfiltered (gdb_stdlog
,
2563 "SC: pending event of %s cancelled.\n",
2564 target_pid_to_str (lp
->ptid
));
2567 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
2575 /* Return non-zero if LP isn't stopped. */
2578 running_callback (struct lwp_info
*lp
, void *data
)
2580 return (!lp
->stopped
2581 || (lwp_status_pending_p (lp
) && lp
->resumed
));
2584 /* Count the LWP's that have had events. */
2587 count_events_callback (struct lwp_info
*lp
, void *data
)
2589 int *count
= (int *) data
;
2591 gdb_assert (count
!= NULL
);
2593 /* Select only resumed LWPs that have an event pending. */
2594 if (lp
->resumed
&& lwp_status_pending_p (lp
))
2600 /* Select the LWP (if any) that is currently being single-stepped. */
2603 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2605 if (lp
->last_resume_kind
== resume_step
2612 /* Returns true if LP has a status pending. */
2615 lwp_status_pending_p (struct lwp_info
*lp
)
2617 /* We check for lp->waitstatus in addition to lp->status, because we
2618 can have pending process exits recorded in lp->status and
2619 W_EXITCODE(0,0) happens to be 0. */
2620 return lp
->status
!= 0 || lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
;
2623 /* Select the Nth LWP that has had an event. */
2626 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2628 int *selector
= (int *) data
;
2630 gdb_assert (selector
!= NULL
);
2632 /* Select only resumed LWPs that have an event pending. */
2633 if (lp
->resumed
&& lwp_status_pending_p (lp
))
2634 if ((*selector
)-- == 0)
2640 /* Called when the LWP got a signal/trap that could be explained by a
2641 software or hardware breakpoint. */
2644 check_stopped_by_breakpoint (struct lwp_info
*lp
)
2646 /* Arrange for a breakpoint to be hit again later. We don't keep
2647 the SIGTRAP status and don't forward the SIGTRAP signal to the
2648 LWP. We will handle the current event, eventually we will resume
2649 this LWP, and this breakpoint will trap again.
2651 If we do not do this, then we run the risk that the user will
2652 delete or disable the breakpoint, but the LWP will have already
2655 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2656 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2659 #if USE_SIGTRAP_SIGINFO
2663 pc
= regcache_read_pc (regcache
);
2664 sw_bp_pc
= pc
- gdbarch_decr_pc_after_break (gdbarch
);
2666 #if USE_SIGTRAP_SIGINFO
2667 if (linux_nat_get_siginfo (lp
->ptid
, &siginfo
))
2669 if (siginfo
.si_signo
== SIGTRAP
)
2671 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
))
2673 if (debug_linux_nat
)
2674 fprintf_unfiltered (gdb_stdlog
,
2675 "CSBB: %s stopped by software "
2677 target_pid_to_str (lp
->ptid
));
2679 /* Back up the PC if necessary. */
2681 regcache_write_pc (regcache
, sw_bp_pc
);
2683 lp
->stop_pc
= sw_bp_pc
;
2684 lp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
2687 else if (siginfo
.si_code
== TRAP_HWBKPT
)
2689 if (debug_linux_nat
)
2690 fprintf_unfiltered (gdb_stdlog
,
2691 "CSBB: %s stopped by hardware "
2692 "breakpoint/watchpoint\n",
2693 target_pid_to_str (lp
->ptid
));
2696 lp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
2699 else if (siginfo
.si_code
== TRAP_TRACE
)
2701 if (debug_linux_nat
)
2702 fprintf_unfiltered (gdb_stdlog
,
2703 "CSBB: %s stopped by trace\n",
2704 target_pid_to_str (lp
->ptid
));
2709 if ((!lp
->step
|| lp
->stop_pc
== sw_bp_pc
)
2710 && software_breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
2713 /* The LWP was either continued, or stepped a software
2714 breakpoint instruction. */
2715 if (debug_linux_nat
)
2716 fprintf_unfiltered (gdb_stdlog
,
2717 "CSBB: %s stopped by software breakpoint\n",
2718 target_pid_to_str (lp
->ptid
));
2720 /* Back up the PC if necessary. */
2722 regcache_write_pc (regcache
, sw_bp_pc
);
2724 lp
->stop_pc
= sw_bp_pc
;
2725 lp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
2729 if (hardware_breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
2731 if (debug_linux_nat
)
2732 fprintf_unfiltered (gdb_stdlog
,
2733 "CSBB: stopped by hardware breakpoint %s\n",
2734 target_pid_to_str (lp
->ptid
));
2737 lp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
2746 /* Returns true if the LWP had stopped for a software breakpoint. */
2749 linux_nat_stopped_by_sw_breakpoint (struct target_ops
*ops
)
2751 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2753 gdb_assert (lp
!= NULL
);
2755 return lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
;
2758 /* Implement the supports_stopped_by_sw_breakpoint method. */
2761 linux_nat_supports_stopped_by_sw_breakpoint (struct target_ops
*ops
)
2763 return USE_SIGTRAP_SIGINFO
;
2766 /* Returns true if the LWP had stopped for a hardware
2767 breakpoint/watchpoint. */
2770 linux_nat_stopped_by_hw_breakpoint (struct target_ops
*ops
)
2772 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2774 gdb_assert (lp
!= NULL
);
2776 return lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
;
2779 /* Implement the supports_stopped_by_hw_breakpoint method. */
2782 linux_nat_supports_stopped_by_hw_breakpoint (struct target_ops
*ops
)
2784 return USE_SIGTRAP_SIGINFO
;
2787 /* Select one LWP out of those that have events pending. */
2790 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
2793 int random_selector
;
2794 struct lwp_info
*event_lp
= NULL
;
2796 /* Record the wait status for the original LWP. */
2797 (*orig_lp
)->status
= *status
;
2799 /* In all-stop, give preference to the LWP that is being
2800 single-stepped. There will be at most one, and it will be the
2801 LWP that the core is most interested in. If we didn't do this,
2802 then we'd have to handle pending step SIGTRAPs somehow in case
2803 the core later continues the previously-stepped thread, as
2804 otherwise we'd report the pending SIGTRAP then, and the core, not
2805 having stepped the thread, wouldn't understand what the trap was
2806 for, and therefore would report it to the user as a random
2808 if (!target_is_non_stop_p ())
2810 event_lp
= iterate_over_lwps (filter
,
2811 select_singlestep_lwp_callback
, NULL
);
2812 if (event_lp
!= NULL
)
2814 if (debug_linux_nat
)
2815 fprintf_unfiltered (gdb_stdlog
,
2816 "SEL: Select single-step %s\n",
2817 target_pid_to_str (event_lp
->ptid
));
2821 if (event_lp
== NULL
)
2823 /* Pick one at random, out of those which have had events. */
2825 /* First see how many events we have. */
2826 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
2827 gdb_assert (num_events
> 0);
2829 /* Now randomly pick a LWP out of those that have had
2831 random_selector
= (int)
2832 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2834 if (debug_linux_nat
&& num_events
> 1)
2835 fprintf_unfiltered (gdb_stdlog
,
2836 "SEL: Found %d events, selecting #%d\n",
2837 num_events
, random_selector
);
2839 event_lp
= iterate_over_lwps (filter
,
2840 select_event_lwp_callback
,
2844 if (event_lp
!= NULL
)
2846 /* Switch the event LWP. */
2847 *orig_lp
= event_lp
;
2848 *status
= event_lp
->status
;
2851 /* Flush the wait status for the event LWP. */
2852 (*orig_lp
)->status
= 0;
2855 /* Return non-zero if LP has been resumed. */
2858 resumed_callback (struct lwp_info
*lp
, void *data
)
2863 /* Stop an active thread, verify it still exists, then resume it. If
2864 the thread ends up with a pending status, then it is not resumed,
2865 and *DATA (really a pointer to int), is set. */
2868 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2872 ptid_t ptid
= lp
->ptid
;
2874 stop_callback (lp
, NULL
);
2875 stop_wait_callback (lp
, NULL
);
2877 /* Resume if the lwp still exists, and the core wanted it
2879 lp
= find_lwp_pid (ptid
);
2882 if (lp
->last_resume_kind
== resume_stop
2883 && !lwp_status_pending_p (lp
))
2885 /* The core wanted the LWP to stop. Even if it stopped
2886 cleanly (with SIGSTOP), leave the event pending. */
2887 if (debug_linux_nat
)
2888 fprintf_unfiltered (gdb_stdlog
,
2889 "SARC: core wanted LWP %ld stopped "
2890 "(leaving SIGSTOP pending)\n",
2891 ptid_get_lwp (lp
->ptid
));
2892 lp
->status
= W_STOPCODE (SIGSTOP
);
2895 if (!lwp_status_pending_p (lp
))
2897 if (debug_linux_nat
)
2898 fprintf_unfiltered (gdb_stdlog
,
2899 "SARC: re-resuming LWP %ld\n",
2900 ptid_get_lwp (lp
->ptid
));
2901 resume_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
2905 if (debug_linux_nat
)
2906 fprintf_unfiltered (gdb_stdlog
,
2907 "SARC: not re-resuming LWP %ld "
2909 ptid_get_lwp (lp
->ptid
));
2916 /* Check if we should go on and pass this event to common code.
2917 Return the affected lwp if we are, or NULL otherwise. */
2919 static struct lwp_info
*
2920 linux_nat_filter_event (int lwpid
, int status
)
2922 struct lwp_info
*lp
;
2923 int event
= linux_ptrace_get_extended_event (status
);
2925 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2927 /* Check for stop events reported by a process we didn't already
2928 know about - anything not already in our LWP list.
2930 If we're expecting to receive stopped processes after
2931 fork, vfork, and clone events, then we'll just add the
2932 new one to our list and go back to waiting for the event
2933 to be reported - the stopped process might be returned
2934 from waitpid before or after the event is.
2936 But note the case of a non-leader thread exec'ing after the
2937 leader having exited, and gone from our lists. The non-leader
2938 thread changes its tid to the tgid. */
2940 if (WIFSTOPPED (status
) && lp
== NULL
2941 && (WSTOPSIG (status
) == SIGTRAP
&& event
== PTRACE_EVENT_EXEC
))
2943 /* A multi-thread exec after we had seen the leader exiting. */
2944 if (debug_linux_nat
)
2945 fprintf_unfiltered (gdb_stdlog
,
2946 "LLW: Re-adding thread group leader LWP %d.\n",
2949 lp
= add_lwp (ptid_build (lwpid
, lwpid
, 0));
2952 add_thread (lp
->ptid
);
2955 if (WIFSTOPPED (status
) && !lp
)
2957 if (debug_linux_nat
)
2958 fprintf_unfiltered (gdb_stdlog
,
2959 "LHEW: saving LWP %ld status %s in stopped_pids list\n",
2960 (long) lwpid
, status_to_str (status
));
2961 add_to_pid_list (&stopped_pids
, lwpid
, status
);
2965 /* Make sure we don't report an event for the exit of an LWP not in
2966 our list, i.e. not part of the current process. This can happen
2967 if we detach from a program we originally forked and then it
2969 if (!WIFSTOPPED (status
) && !lp
)
2972 /* This LWP is stopped now. (And if dead, this prevents it from
2973 ever being continued.) */
2976 if (WIFSTOPPED (status
) && lp
->must_set_ptrace_flags
)
2978 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
2979 int options
= linux_nat_ptrace_options (inf
->attach_flag
);
2981 linux_enable_event_reporting (ptid_get_lwp (lp
->ptid
), options
);
2982 lp
->must_set_ptrace_flags
= 0;
2985 /* Handle GNU/Linux's syscall SIGTRAPs. */
2986 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2988 /* No longer need the sysgood bit. The ptrace event ends up
2989 recorded in lp->waitstatus if we care for it. We can carry
2990 on handling the event like a regular SIGTRAP from here
2992 status
= W_STOPCODE (SIGTRAP
);
2993 if (linux_handle_syscall_trap (lp
, 0))
2998 /* Almost all other ptrace-stops are known to be outside of system
2999 calls, with further exceptions in linux_handle_extended_wait. */
3000 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
3003 /* Handle GNU/Linux's extended waitstatus for trace events. */
3004 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
3005 && linux_is_extended_waitstatus (status
))
3007 if (debug_linux_nat
)
3008 fprintf_unfiltered (gdb_stdlog
,
3009 "LLW: Handling extended status 0x%06x\n",
3011 if (linux_handle_extended_wait (lp
, status
))
3015 /* Check if the thread has exited. */
3016 if (WIFEXITED (status
) || WIFSIGNALED (status
))
3018 if (num_lwps (ptid_get_pid (lp
->ptid
)) > 1)
3020 /* If this is the main thread, we must stop all threads and
3021 verify if they are still alive. This is because in the
3022 nptl thread model on Linux 2.4, there is no signal issued
3023 for exiting LWPs other than the main thread. We only get
3024 the main thread exit signal once all child threads have
3025 already exited. If we stop all the threads and use the
3026 stop_wait_callback to check if they have exited we can
3027 determine whether this signal should be ignored or
3028 whether it means the end of the debugged application,
3029 regardless of which threading model is being used. */
3030 if (ptid_get_pid (lp
->ptid
) == ptid_get_lwp (lp
->ptid
))
3032 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp
->ptid
)),
3033 stop_and_resume_callback
, NULL
);
3036 if (debug_linux_nat
)
3037 fprintf_unfiltered (gdb_stdlog
,
3038 "LLW: %s exited.\n",
3039 target_pid_to_str (lp
->ptid
));
3041 if (num_lwps (ptid_get_pid (lp
->ptid
)) > 1)
3043 /* If there is at least one more LWP, then the exit signal
3044 was not the end of the debugged application and should be
3051 /* Note that even if the leader was ptrace-stopped, it can still
3052 exit, if e.g., some other thread brings down the whole
3053 process (calls `exit'). So don't assert that the lwp is
3055 if (debug_linux_nat
)
3056 fprintf_unfiltered (gdb_stdlog
,
3057 "Process %ld exited (resumed=%d)\n",
3058 ptid_get_lwp (lp
->ptid
), lp
->resumed
);
3060 /* This was the last lwp in the process. Since events are
3061 serialized to GDB core, we may not be able report this one
3062 right now, but GDB core and the other target layers will want
3063 to be notified about the exit code/signal, leave the status
3064 pending for the next time we're able to report it. */
3066 /* Dead LWP's aren't expected to reported a pending sigstop. */
3069 /* Store the pending event in the waitstatus, because
3070 W_EXITCODE(0,0) == 0. */
3071 store_waitstatus (&lp
->waitstatus
, status
);
3075 /* Check if the current LWP has previously exited. In the nptl
3076 thread model, LWPs other than the main thread do not issue
3077 signals when they exit so we must check whenever the thread has
3078 stopped. A similar check is made in stop_wait_callback(). */
3079 if (num_lwps (ptid_get_pid (lp
->ptid
)) > 1 && !linux_thread_alive (lp
->ptid
))
3081 ptid_t ptid
= pid_to_ptid (ptid_get_pid (lp
->ptid
));
3083 if (debug_linux_nat
)
3084 fprintf_unfiltered (gdb_stdlog
,
3085 "LLW: %s exited.\n",
3086 target_pid_to_str (lp
->ptid
));
3090 /* Make sure there is at least one thread running. */
3091 gdb_assert (iterate_over_lwps (ptid
, running_callback
, NULL
));
3093 /* Discard the event. */
3097 /* Make sure we don't report a SIGSTOP that we sent ourselves in
3098 an attempt to stop an LWP. */
3100 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
3104 if (lp
->last_resume_kind
== resume_stop
)
3106 if (debug_linux_nat
)
3107 fprintf_unfiltered (gdb_stdlog
,
3108 "LLW: resume_stop SIGSTOP caught for %s.\n",
3109 target_pid_to_str (lp
->ptid
));
3113 /* This is a delayed SIGSTOP. Filter out the event. */
3115 if (debug_linux_nat
)
3116 fprintf_unfiltered (gdb_stdlog
,
3117 "LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
3119 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3120 target_pid_to_str (lp
->ptid
));
3122 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
3123 gdb_assert (lp
->resumed
);
3128 /* Make sure we don't report a SIGINT that we have already displayed
3129 for another thread. */
3130 if (lp
->ignore_sigint
3131 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
3133 if (debug_linux_nat
)
3134 fprintf_unfiltered (gdb_stdlog
,
3135 "LLW: Delayed SIGINT caught for %s.\n",
3136 target_pid_to_str (lp
->ptid
));
3138 /* This is a delayed SIGINT. */
3139 lp
->ignore_sigint
= 0;
3141 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
3142 if (debug_linux_nat
)
3143 fprintf_unfiltered (gdb_stdlog
,
3144 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
3146 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3147 target_pid_to_str (lp
->ptid
));
3148 gdb_assert (lp
->resumed
);
3150 /* Discard the event. */
3154 /* Don't report signals that GDB isn't interested in, such as
3155 signals that are neither printed nor stopped upon. Stopping all
3156 threads can be a bit time-consuming so if we want decent
3157 performance with heavily multi-threaded programs, especially when
3158 they're using a high frequency timer, we'd better avoid it if we
3160 if (WIFSTOPPED (status
))
3162 enum gdb_signal signo
= gdb_signal_from_host (WSTOPSIG (status
));
3164 if (!target_is_non_stop_p ())
3166 /* Only do the below in all-stop, as we currently use SIGSTOP
3167 to implement target_stop (see linux_nat_stop) in
3169 if (signo
== GDB_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3171 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3172 forwarded to the entire process group, that is, all LWPs
3173 will receive it - unless they're using CLONE_THREAD to
3174 share signals. Since we only want to report it once, we
3175 mark it as ignored for all LWPs except this one. */
3176 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp
->ptid
)),
3177 set_ignore_sigint
, NULL
);
3178 lp
->ignore_sigint
= 0;
3181 maybe_clear_ignore_sigint (lp
);
3184 /* When using hardware single-step, we need to report every signal.
3185 Otherwise, signals in pass_mask may be short-circuited
3186 except signals that might be caused by a breakpoint. */
3188 && WSTOPSIG (status
) && sigismember (&pass_mask
, WSTOPSIG (status
))
3189 && !linux_wstatus_maybe_breakpoint (status
))
3191 linux_resume_one_lwp (lp
, lp
->step
, signo
);
3192 if (debug_linux_nat
)
3193 fprintf_unfiltered (gdb_stdlog
,
3194 "LLW: %s %s, %s (preempt 'handle')\n",
3196 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3197 target_pid_to_str (lp
->ptid
),
3198 (signo
!= GDB_SIGNAL_0
3199 ? strsignal (gdb_signal_to_host (signo
))
3205 /* An interesting event. */
3207 lp
->status
= status
;
3212 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3213 their exits until all other threads in the group have exited. */
3216 check_zombie_leaders (void)
3218 struct inferior
*inf
;
3222 struct lwp_info
*leader_lp
;
3227 leader_lp
= find_lwp_pid (pid_to_ptid (inf
->pid
));
3228 if (leader_lp
!= NULL
3229 /* Check if there are other threads in the group, as we may
3230 have raced with the inferior simply exiting. */
3231 && num_lwps (inf
->pid
) > 1
3232 && linux_proc_pid_is_zombie (inf
->pid
))
3234 if (debug_linux_nat
)
3235 fprintf_unfiltered (gdb_stdlog
,
3236 "CZL: Thread group leader %d zombie "
3237 "(it exited, or another thread execd).\n",
3240 /* A leader zombie can mean one of two things:
3242 - It exited, and there's an exit status pending
3243 available, or only the leader exited (not the whole
3244 program). In the latter case, we can't waitpid the
3245 leader's exit status until all other threads are gone.
3247 - There are 3 or more threads in the group, and a thread
3248 other than the leader exec'd. On an exec, the Linux
3249 kernel destroys all other threads (except the execing
3250 one) in the thread group, and resets the execing thread's
3251 tid to the tgid. No exit notification is sent for the
3252 execing thread -- from the ptracer's perspective, it
3253 appears as though the execing thread just vanishes.
3254 Until we reap all other threads except the leader and the
3255 execing thread, the leader will be zombie, and the
3256 execing thread will be in `D (disc sleep)'. As soon as
3257 all other threads are reaped, the execing thread changes
3258 it's tid to the tgid, and the previous (zombie) leader
3259 vanishes, giving place to the "new" leader. We could try
3260 distinguishing the exit and exec cases, by waiting once
3261 more, and seeing if something comes out, but it doesn't
3262 sound useful. The previous leader _does_ go away, and
3263 we'll re-add the new one once we see the exec event
3264 (which is just the same as what would happen if the
3265 previous leader did exit voluntarily before some other
3268 if (debug_linux_nat
)
3269 fprintf_unfiltered (gdb_stdlog
,
3270 "CZL: Thread group leader %d vanished.\n",
3272 exit_lwp (leader_lp
);
3278 linux_nat_wait_1 (struct target_ops
*ops
,
3279 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3283 enum resume_kind last_resume_kind
;
3284 struct lwp_info
*lp
;
3287 if (debug_linux_nat
)
3288 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
3290 /* The first time we get here after starting a new inferior, we may
3291 not have added it to the LWP list yet - this is the earliest
3292 moment at which we know its PID. */
3293 if (ptid_is_pid (inferior_ptid
))
3295 /* Upgrade the main thread's ptid. */
3296 thread_change_ptid (inferior_ptid
,
3297 ptid_build (ptid_get_pid (inferior_ptid
),
3298 ptid_get_pid (inferior_ptid
), 0));
3300 lp
= add_initial_lwp (inferior_ptid
);
3304 /* Make sure SIGCHLD is blocked until the sigsuspend below. */
3305 block_child_signals (&prev_mask
);
3307 /* First check if there is a LWP with a wait status pending. */
3308 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3311 if (debug_linux_nat
)
3312 fprintf_unfiltered (gdb_stdlog
,
3313 "LLW: Using pending wait status %s for %s.\n",
3314 status_to_str (lp
->status
),
3315 target_pid_to_str (lp
->ptid
));
3318 /* But if we don't find a pending event, we'll have to wait. Always
3319 pull all events out of the kernel. We'll randomly select an
3320 event LWP out of all that have events, to prevent starvation. */
3326 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3329 - If the thread group leader exits while other threads in the
3330 thread group still exist, waitpid(TGID, ...) hangs. That
3331 waitpid won't return an exit status until the other threads
3332 in the group are reapped.
3334 - When a non-leader thread execs, that thread just vanishes
3335 without reporting an exit (so we'd hang if we waited for it
3336 explicitly in that case). The exec event is reported to
3340 lwpid
= my_waitpid (-1, &status
, __WCLONE
| WNOHANG
);
3341 if (lwpid
== 0 || (lwpid
== -1 && errno
== ECHILD
))
3342 lwpid
= my_waitpid (-1, &status
, WNOHANG
);
3344 if (debug_linux_nat
)
3345 fprintf_unfiltered (gdb_stdlog
,
3346 "LNW: waitpid(-1, ...) returned %d, %s\n",
3347 lwpid
, errno
? safe_strerror (errno
) : "ERRNO-OK");
3351 if (debug_linux_nat
)
3353 fprintf_unfiltered (gdb_stdlog
,
3354 "LLW: waitpid %ld received %s\n",
3355 (long) lwpid
, status_to_str (status
));
3358 linux_nat_filter_event (lwpid
, status
);
3359 /* Retry until nothing comes out of waitpid. A single
3360 SIGCHLD can indicate more than one child stopped. */
3364 /* Now that we've pulled all events out of the kernel, resume
3365 LWPs that don't have an interesting event to report. */
3366 iterate_over_lwps (minus_one_ptid
,
3367 resume_stopped_resumed_lwps
, &minus_one_ptid
);
3369 /* ... and find an LWP with a status to report to the core, if
3371 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3375 /* Check for zombie thread group leaders. Those can't be reaped
3376 until all other threads in the thread group are. */
3377 check_zombie_leaders ();
3379 /* If there are no resumed children left, bail. We'd be stuck
3380 forever in the sigsuspend call below otherwise. */
3381 if (iterate_over_lwps (ptid
, resumed_callback
, NULL
) == NULL
)
3383 if (debug_linux_nat
)
3384 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (no resumed LWP)\n");
3386 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3388 restore_child_signals_mask (&prev_mask
);
3389 return minus_one_ptid
;
3392 /* No interesting event to report to the core. */
3394 if (target_options
& TARGET_WNOHANG
)
3396 if (debug_linux_nat
)
3397 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
3399 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3400 restore_child_signals_mask (&prev_mask
);
3401 return minus_one_ptid
;
3404 /* We shouldn't end up here unless we want to try again. */
3405 gdb_assert (lp
== NULL
);
3407 /* Block until we get an event reported with SIGCHLD. */
3408 if (debug_linux_nat
)
3409 fprintf_unfiltered (gdb_stdlog
, "LNW: about to sigsuspend\n");
3410 sigsuspend (&suspend_mask
);
3415 status
= lp
->status
;
3418 if (!target_is_non_stop_p ())
3420 /* Now stop all other LWP's ... */
3421 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3423 /* ... and wait until all of them have reported back that
3424 they're no longer running. */
3425 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3428 /* If we're not waiting for a specific LWP, choose an event LWP from
3429 among those that have had events. Giving equal priority to all
3430 LWPs that have had events helps prevent starvation. */
3431 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3432 select_event_lwp (ptid
, &lp
, &status
);
3434 gdb_assert (lp
!= NULL
);
3436 /* Now that we've selected our final event LWP, un-adjust its PC if
3437 it was a software breakpoint, and we can't reliably support the
3438 "stopped by software breakpoint" stop reason. */
3439 if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3440 && !USE_SIGTRAP_SIGINFO
)
3442 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3443 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3444 int decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
3450 pc
= regcache_read_pc (regcache
);
3451 regcache_write_pc (regcache
, pc
+ decr_pc
);
3455 /* We'll need this to determine whether to report a SIGSTOP as
3456 GDB_SIGNAL_0. Need to take a copy because resume_clear_callback
3458 last_resume_kind
= lp
->last_resume_kind
;
3460 if (!target_is_non_stop_p ())
3462 /* In all-stop, from the core's perspective, all LWPs are now
3463 stopped until a new resume action is sent over. */
3464 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
3468 resume_clear_callback (lp
, NULL
);
3471 if (linux_nat_status_is_event (status
))
3473 if (debug_linux_nat
)
3474 fprintf_unfiltered (gdb_stdlog
,
3475 "LLW: trap ptid is %s.\n",
3476 target_pid_to_str (lp
->ptid
));
3479 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3481 *ourstatus
= lp
->waitstatus
;
3482 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3485 store_waitstatus (ourstatus
, status
);
3487 if (debug_linux_nat
)
3488 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3490 restore_child_signals_mask (&prev_mask
);
3492 if (last_resume_kind
== resume_stop
3493 && ourstatus
->kind
== TARGET_WAITKIND_STOPPED
3494 && WSTOPSIG (status
) == SIGSTOP
)
3496 /* A thread that has been requested to stop by GDB with
3497 target_stop, and it stopped cleanly, so report as SIG0. The
3498 use of SIGSTOP is an implementation detail. */
3499 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3502 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
3503 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
3506 lp
->core
= linux_common_core_of_thread (lp
->ptid
);
3511 /* Resume LWPs that are currently stopped without any pending status
3512 to report, but are resumed from the core's perspective. */
3515 resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
)
3517 ptid_t
*wait_ptid_p
= (ptid_t
*) data
;
3521 if (debug_linux_nat
)
3522 fprintf_unfiltered (gdb_stdlog
,
3523 "RSRL: NOT resuming LWP %s, not stopped\n",
3524 target_pid_to_str (lp
->ptid
));
3526 else if (!lp
->resumed
)
3528 if (debug_linux_nat
)
3529 fprintf_unfiltered (gdb_stdlog
,
3530 "RSRL: NOT resuming LWP %s, not resumed\n",
3531 target_pid_to_str (lp
->ptid
));
3533 else if (lwp_status_pending_p (lp
))
3535 if (debug_linux_nat
)
3536 fprintf_unfiltered (gdb_stdlog
,
3537 "RSRL: NOT resuming LWP %s, has pending status\n",
3538 target_pid_to_str (lp
->ptid
));
3542 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3543 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3547 CORE_ADDR pc
= regcache_read_pc (regcache
);
3548 int leave_stopped
= 0;
3550 /* Don't bother if there's a breakpoint at PC that we'd hit
3551 immediately, and we're not waiting for this LWP. */
3552 if (!ptid_match (lp
->ptid
, *wait_ptid_p
))
3554 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
3560 if (debug_linux_nat
)
3561 fprintf_unfiltered (gdb_stdlog
,
3562 "RSRL: resuming stopped-resumed LWP %s at "
3564 target_pid_to_str (lp
->ptid
),
3565 paddress (gdbarch
, pc
),
3568 linux_resume_one_lwp_throw (lp
, lp
->step
, GDB_SIGNAL_0
);
3571 CATCH (ex
, RETURN_MASK_ERROR
)
3573 if (!check_ptrace_stopped_lwp_gone (lp
))
3574 throw_exception (ex
);
3583 linux_nat_wait (struct target_ops
*ops
,
3584 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3589 if (debug_linux_nat
)
3591 char *options_string
;
3593 options_string
= target_options_to_string (target_options
);
3594 fprintf_unfiltered (gdb_stdlog
,
3595 "linux_nat_wait: [%s], [%s]\n",
3596 target_pid_to_str (ptid
),
3598 xfree (options_string
);
3601 /* Flush the async file first. */
3602 if (target_is_async_p ())
3603 async_file_flush ();
3605 /* Resume LWPs that are currently stopped without any pending status
3606 to report, but are resumed from the core's perspective. LWPs get
3607 in this state if we find them stopping at a time we're not
3608 interested in reporting the event (target_wait on a
3609 specific_process, for example, see linux_nat_wait_1), and
3610 meanwhile the event became uninteresting. Don't bother resuming
3611 LWPs we're not going to wait for if they'd stop immediately. */
3612 if (target_is_non_stop_p ())
3613 iterate_over_lwps (minus_one_ptid
, resume_stopped_resumed_lwps
, &ptid
);
3615 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
, target_options
);
3617 /* If we requested any event, and something came out, assume there
3618 may be more. If we requested a specific lwp or process, also
3619 assume there may be more. */
3620 if (target_is_async_p ()
3621 && ((ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
3622 && ourstatus
->kind
!= TARGET_WAITKIND_NO_RESUMED
)
3623 || !ptid_equal (ptid
, minus_one_ptid
)))
3630 kill_callback (struct lwp_info
*lp
, void *data
)
3632 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
3635 kill_lwp (ptid_get_lwp (lp
->ptid
), SIGKILL
);
3636 if (debug_linux_nat
)
3638 int save_errno
= errno
;
3640 fprintf_unfiltered (gdb_stdlog
,
3641 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
3642 target_pid_to_str (lp
->ptid
),
3643 save_errno
? safe_strerror (save_errno
) : "OK");
3646 /* Some kernels ignore even SIGKILL for processes under ptrace. */
3649 ptrace (PTRACE_KILL
, ptid_get_lwp (lp
->ptid
), 0, 0);
3650 if (debug_linux_nat
)
3652 int save_errno
= errno
;
3654 fprintf_unfiltered (gdb_stdlog
,
3655 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3656 target_pid_to_str (lp
->ptid
),
3657 save_errno
? safe_strerror (save_errno
) : "OK");
3664 kill_wait_callback (struct lwp_info
*lp
, void *data
)
3668 /* We must make sure that there are no pending events (delayed
3669 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3670 program doesn't interfere with any following debugging session. */
3672 /* For cloned processes we must check both with __WCLONE and
3673 without, since the exit status of a cloned process isn't reported
3679 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), NULL
, __WCLONE
);
3680 if (pid
!= (pid_t
) -1)
3682 if (debug_linux_nat
)
3683 fprintf_unfiltered (gdb_stdlog
,
3684 "KWC: wait %s received unknown.\n",
3685 target_pid_to_str (lp
->ptid
));
3686 /* The Linux kernel sometimes fails to kill a thread
3687 completely after PTRACE_KILL; that goes from the stop
3688 point in do_fork out to the one in
3689 get_signal_to_deliever and waits again. So kill it
3691 kill_callback (lp
, NULL
);
3694 while (pid
== ptid_get_lwp (lp
->ptid
));
3696 gdb_assert (pid
== -1 && errno
== ECHILD
);
3701 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), NULL
, 0);
3702 if (pid
!= (pid_t
) -1)
3704 if (debug_linux_nat
)
3705 fprintf_unfiltered (gdb_stdlog
,
3706 "KWC: wait %s received unk.\n",
3707 target_pid_to_str (lp
->ptid
));
3708 /* See the call to kill_callback above. */
3709 kill_callback (lp
, NULL
);
3712 while (pid
== ptid_get_lwp (lp
->ptid
));
3714 gdb_assert (pid
== -1 && errno
== ECHILD
);
3719 linux_nat_kill (struct target_ops
*ops
)
3721 struct target_waitstatus last
;
3725 /* If we're stopped while forking and we haven't followed yet,
3726 kill the other task. We need to do this first because the
3727 parent will be sleeping if this is a vfork. */
3729 get_last_target_status (&last_ptid
, &last
);
3731 if (last
.kind
== TARGET_WAITKIND_FORKED
3732 || last
.kind
== TARGET_WAITKIND_VFORKED
)
3734 ptrace (PT_KILL
, ptid_get_pid (last
.value
.related_pid
), 0, 0);
3737 /* Let the arch-specific native code know this process is
3739 linux_nat_forget_process (ptid_get_pid (last
.value
.related_pid
));
3742 if (forks_exist_p ())
3743 linux_fork_killall ();
3746 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3748 /* Stop all threads before killing them, since ptrace requires
3749 that the thread is stopped to sucessfully PTRACE_KILL. */
3750 iterate_over_lwps (ptid
, stop_callback
, NULL
);
3751 /* ... and wait until all of them have reported back that
3752 they're no longer running. */
3753 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
3755 /* Kill all LWP's ... */
3756 iterate_over_lwps (ptid
, kill_callback
, NULL
);
3758 /* ... and wait until we've flushed all events. */
3759 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
3762 target_mourn_inferior ();
3766 linux_nat_mourn_inferior (struct target_ops
*ops
)
3768 int pid
= ptid_get_pid (inferior_ptid
);
3770 purge_lwp_list (pid
);
3772 if (! forks_exist_p ())
3773 /* Normal case, no other forks available. */
3774 linux_ops
->to_mourn_inferior (ops
);
3776 /* Multi-fork case. The current inferior_ptid has exited, but
3777 there are other viable forks to debug. Delete the exiting
3778 one and context-switch to the first available. */
3779 linux_fork_mourn_inferior ();
3781 /* Let the arch-specific native code know this process is gone. */
3782 linux_nat_forget_process (pid
);
3785 /* Convert a native/host siginfo object, into/from the siginfo in the
3786 layout of the inferiors' architecture. */
3789 siginfo_fixup (siginfo_t
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
3793 if (linux_nat_siginfo_fixup
!= NULL
)
3794 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
3796 /* If there was no callback, or the callback didn't do anything,
3797 then just do a straight memcpy. */
3801 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
3803 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
3807 static enum target_xfer_status
3808 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
3809 const char *annex
, gdb_byte
*readbuf
,
3810 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
3811 ULONGEST
*xfered_len
)
3815 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
3817 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
3818 gdb_assert (readbuf
|| writebuf
);
3820 pid
= ptid_get_lwp (inferior_ptid
);
3822 pid
= ptid_get_pid (inferior_ptid
);
3824 if (offset
> sizeof (siginfo
))
3825 return TARGET_XFER_E_IO
;
3828 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3830 return TARGET_XFER_E_IO
;
3832 /* When GDB is built as a 64-bit application, ptrace writes into
3833 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3834 inferior with a 64-bit GDB should look the same as debugging it
3835 with a 32-bit GDB, we need to convert it. GDB core always sees
3836 the converted layout, so any read/write will have to be done
3838 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
3840 if (offset
+ len
> sizeof (siginfo
))
3841 len
= sizeof (siginfo
) - offset
;
3843 if (readbuf
!= NULL
)
3844 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
3847 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
3849 /* Convert back to ptrace layout before flushing it out. */
3850 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
3853 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3855 return TARGET_XFER_E_IO
;
3859 return TARGET_XFER_OK
;
3862 static enum target_xfer_status
3863 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3864 const char *annex
, gdb_byte
*readbuf
,
3865 const gdb_byte
*writebuf
,
3866 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
3868 struct cleanup
*old_chain
;
3869 enum target_xfer_status xfer
;
3871 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
3872 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
3873 offset
, len
, xfered_len
);
3875 /* The target is connected but no live inferior is selected. Pass
3876 this request down to a lower stratum (e.g., the executable
3878 if (object
== TARGET_OBJECT_MEMORY
&& ptid_equal (inferior_ptid
, null_ptid
))
3879 return TARGET_XFER_EOF
;
3881 old_chain
= save_inferior_ptid ();
3883 if (ptid_lwp_p (inferior_ptid
))
3884 inferior_ptid
= pid_to_ptid (ptid_get_lwp (inferior_ptid
));
3886 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3887 offset
, len
, xfered_len
);
3889 do_cleanups (old_chain
);
3894 linux_thread_alive (ptid_t ptid
)
3898 gdb_assert (ptid_lwp_p (ptid
));
3900 /* Send signal 0 instead of anything ptrace, because ptracing a
3901 running thread errors out claiming that the thread doesn't
3903 err
= kill_lwp (ptid_get_lwp (ptid
), 0);
3905 if (debug_linux_nat
)
3906 fprintf_unfiltered (gdb_stdlog
,
3907 "LLTA: KILL(SIG0) %s (%s)\n",
3908 target_pid_to_str (ptid
),
3909 err
? safe_strerror (tmp_errno
) : "OK");
3918 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
3920 return linux_thread_alive (ptid
);
3923 /* Implement the to_update_thread_list target method for this
3927 linux_nat_update_thread_list (struct target_ops
*ops
)
3929 if (linux_supports_traceclone ())
3931 /* With support for clone events, we add/delete threads from the
3932 list as clone/exit events are processed, so just try deleting
3933 exited threads still in the thread list. */
3934 delete_exited_threads ();
3941 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3943 static char buf
[64];
3945 if (ptid_lwp_p (ptid
)
3946 && (ptid_get_pid (ptid
) != ptid_get_lwp (ptid
)
3947 || num_lwps (ptid_get_pid (ptid
)) > 1))
3949 snprintf (buf
, sizeof (buf
), "LWP %ld", ptid_get_lwp (ptid
));
3953 return normal_pid_to_str (ptid
);
3957 linux_nat_thread_name (struct target_ops
*self
, struct thread_info
*thr
)
3959 return linux_proc_tid_get_name (thr
->ptid
);
3962 /* Accepts an integer PID; Returns a string representing a file that
3963 can be opened to get the symbols for the child process. */
3966 linux_child_pid_to_exec_file (struct target_ops
*self
, int pid
)
3968 return linux_proc_pid_to_exec_file (pid
);
3971 /* Implement the to_xfer_partial interface for memory reads using the /proc
3972 filesystem. Because we can use a single read() call for /proc, this
3973 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3974 but it doesn't support writes. */
3976 static enum target_xfer_status
3977 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3978 const char *annex
, gdb_byte
*readbuf
,
3979 const gdb_byte
*writebuf
,
3980 ULONGEST offset
, LONGEST len
, ULONGEST
*xfered_len
)
3986 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
3987 return TARGET_XFER_EOF
;
3989 /* Don't bother for one word. */
3990 if (len
< 3 * sizeof (long))
3991 return TARGET_XFER_EOF
;
3993 /* We could keep this file open and cache it - possibly one per
3994 thread. That requires some juggling, but is even faster. */
3995 xsnprintf (filename
, sizeof filename
, "/proc/%d/mem",
3996 ptid_get_pid (inferior_ptid
));
3997 fd
= gdb_open_cloexec (filename
, O_RDONLY
| O_LARGEFILE
, 0);
3999 return TARGET_XFER_EOF
;
4001 /* If pread64 is available, use it. It's faster if the kernel
4002 supports it (only one syscall), and it's 64-bit safe even on
4003 32-bit platforms (for instance, SPARC debugging a SPARC64
4006 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
4008 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
4017 return TARGET_XFER_EOF
;
4021 return TARGET_XFER_OK
;
4026 /* Enumerate spufs IDs for process PID. */
4028 spu_enumerate_spu_ids (int pid
, gdb_byte
*buf
, ULONGEST offset
, ULONGEST len
)
4030 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
4032 LONGEST written
= 0;
4035 struct dirent
*entry
;
4037 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
4038 dir
= opendir (path
);
4043 while ((entry
= readdir (dir
)) != NULL
)
4049 fd
= atoi (entry
->d_name
);
4053 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
4054 if (stat (path
, &st
) != 0)
4056 if (!S_ISDIR (st
.st_mode
))
4059 if (statfs (path
, &stfs
) != 0)
4061 if (stfs
.f_type
!= SPUFS_MAGIC
)
4064 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
4066 store_unsigned_integer (buf
+ pos
- offset
, 4, byte_order
, fd
);
4076 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4077 object type, using the /proc file system. */
4079 static enum target_xfer_status
4080 linux_proc_xfer_spu (struct target_ops
*ops
, enum target_object object
,
4081 const char *annex
, gdb_byte
*readbuf
,
4082 const gdb_byte
*writebuf
,
4083 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
4088 int pid
= ptid_get_pid (inferior_ptid
);
4093 return TARGET_XFER_E_IO
;
4096 LONGEST l
= spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
4099 return TARGET_XFER_E_IO
;
4101 return TARGET_XFER_EOF
;
4104 *xfered_len
= (ULONGEST
) l
;
4105 return TARGET_XFER_OK
;
4110 xsnprintf (buf
, sizeof buf
, "/proc/%d/fd/%s", pid
, annex
);
4111 fd
= gdb_open_cloexec (buf
, writebuf
? O_WRONLY
: O_RDONLY
, 0);
4113 return TARGET_XFER_E_IO
;
4116 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4119 return TARGET_XFER_EOF
;
4123 ret
= write (fd
, writebuf
, (size_t) len
);
4125 ret
= read (fd
, readbuf
, (size_t) len
);
4130 return TARGET_XFER_E_IO
;
4132 return TARGET_XFER_EOF
;
4135 *xfered_len
= (ULONGEST
) ret
;
4136 return TARGET_XFER_OK
;
4141 /* Parse LINE as a signal set and add its set bits to SIGS. */
4144 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4146 int len
= strlen (line
) - 1;
4150 if (line
[len
] != '\n')
4151 error (_("Could not parse signal set: %s"), line
);
4159 if (*p
>= '0' && *p
<= '9')
4161 else if (*p
>= 'a' && *p
<= 'f')
4162 digit
= *p
- 'a' + 10;
4164 error (_("Could not parse signal set: %s"), line
);
4169 sigaddset (sigs
, signum
+ 1);
4171 sigaddset (sigs
, signum
+ 2);
4173 sigaddset (sigs
, signum
+ 3);
4175 sigaddset (sigs
, signum
+ 4);
4181 /* Find process PID's pending signals from /proc/pid/status and set
4185 linux_proc_pending_signals (int pid
, sigset_t
*pending
,
4186 sigset_t
*blocked
, sigset_t
*ignored
)
4189 char buffer
[PATH_MAX
], fname
[PATH_MAX
];
4190 struct cleanup
*cleanup
;
4192 sigemptyset (pending
);
4193 sigemptyset (blocked
);
4194 sigemptyset (ignored
);
4195 xsnprintf (fname
, sizeof fname
, "/proc/%d/status", pid
);
4196 procfile
= gdb_fopen_cloexec (fname
, "r");
4197 if (procfile
== NULL
)
4198 error (_("Could not open %s"), fname
);
4199 cleanup
= make_cleanup_fclose (procfile
);
4201 while (fgets (buffer
, PATH_MAX
, procfile
) != NULL
)
4203 /* Normal queued signals are on the SigPnd line in the status
4204 file. However, 2.6 kernels also have a "shared" pending
4205 queue for delivering signals to a thread group, so check for
4208 Unfortunately some Red Hat kernels include the shared pending
4209 queue but not the ShdPnd status field. */
4211 if (startswith (buffer
, "SigPnd:\t"))
4212 add_line_to_sigset (buffer
+ 8, pending
);
4213 else if (startswith (buffer
, "ShdPnd:\t"))
4214 add_line_to_sigset (buffer
+ 8, pending
);
4215 else if (startswith (buffer
, "SigBlk:\t"))
4216 add_line_to_sigset (buffer
+ 8, blocked
);
4217 else if (startswith (buffer
, "SigIgn:\t"))
4218 add_line_to_sigset (buffer
+ 8, ignored
);
4221 do_cleanups (cleanup
);
4224 static enum target_xfer_status
4225 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4226 const char *annex
, gdb_byte
*readbuf
,
4227 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
4228 ULONGEST
*xfered_len
)
4230 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4232 *xfered_len
= linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4233 if (*xfered_len
== 0)
4234 return TARGET_XFER_EOF
;
4236 return TARGET_XFER_OK
;
4239 static enum target_xfer_status
4240 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4241 const char *annex
, gdb_byte
*readbuf
,
4242 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
4243 ULONGEST
*xfered_len
)
4245 enum target_xfer_status xfer
;
4247 if (object
== TARGET_OBJECT_AUXV
)
4248 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4249 offset
, len
, xfered_len
);
4251 if (object
== TARGET_OBJECT_OSDATA
)
4252 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4253 offset
, len
, xfered_len
);
4255 if (object
== TARGET_OBJECT_SPU
)
4256 return linux_proc_xfer_spu (ops
, object
, annex
, readbuf
, writebuf
,
4257 offset
, len
, xfered_len
);
4259 /* GDB calculates all the addresses in possibly larget width of the address.
4260 Address width needs to be masked before its final use - either by
4261 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4263 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4265 if (object
== TARGET_OBJECT_MEMORY
)
4267 int addr_bit
= gdbarch_addr_bit (target_gdbarch ());
4269 if (addr_bit
< (sizeof (ULONGEST
) * HOST_CHAR_BIT
))
4270 offset
&= ((ULONGEST
) 1 << addr_bit
) - 1;
4273 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4274 offset
, len
, xfered_len
);
4275 if (xfer
!= TARGET_XFER_EOF
)
4278 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4279 offset
, len
, xfered_len
);
4283 cleanup_target_stop (void *arg
)
4285 ptid_t
*ptid
= (ptid_t
*) arg
;
4287 gdb_assert (arg
!= NULL
);
4290 target_resume (*ptid
, 0, GDB_SIGNAL_0
);
4293 static VEC(static_tracepoint_marker_p
) *
4294 linux_child_static_tracepoint_markers_by_strid (struct target_ops
*self
,
4297 char s
[IPA_CMD_BUF_SIZE
];
4298 struct cleanup
*old_chain
;
4299 int pid
= ptid_get_pid (inferior_ptid
);
4300 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
4301 struct static_tracepoint_marker
*marker
= NULL
;
4303 ptid_t ptid
= ptid_build (pid
, 0, 0);
4308 memcpy (s
, "qTfSTM", sizeof ("qTfSTM"));
4309 s
[sizeof ("qTfSTM")] = 0;
4311 agent_run_command (pid
, s
, strlen (s
) + 1);
4313 old_chain
= make_cleanup (free_current_marker
, &marker
);
4314 make_cleanup (cleanup_target_stop
, &ptid
);
4319 marker
= XCNEW (struct static_tracepoint_marker
);
4323 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
4325 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
4327 VEC_safe_push (static_tracepoint_marker_p
,
4333 release_static_tracepoint_marker (marker
);
4334 memset (marker
, 0, sizeof (*marker
));
4337 while (*p
++ == ','); /* comma-separated list */
4339 memcpy (s
, "qTsSTM", sizeof ("qTsSTM"));
4340 s
[sizeof ("qTsSTM")] = 0;
4341 agent_run_command (pid
, s
, strlen (s
) + 1);
4345 do_cleanups (old_chain
);
4350 /* Create a prototype generic GNU/Linux target. The client can override
4351 it with local methods. */
4354 linux_target_install_ops (struct target_ops
*t
)
4356 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4357 t
->to_remove_fork_catchpoint
= linux_child_remove_fork_catchpoint
;
4358 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4359 t
->to_remove_vfork_catchpoint
= linux_child_remove_vfork_catchpoint
;
4360 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4361 t
->to_remove_exec_catchpoint
= linux_child_remove_exec_catchpoint
;
4362 t
->to_set_syscall_catchpoint
= linux_child_set_syscall_catchpoint
;
4363 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4364 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4365 t
->to_post_attach
= linux_child_post_attach
;
4366 t
->to_follow_fork
= linux_child_follow_fork
;
4368 super_xfer_partial
= t
->to_xfer_partial
;
4369 t
->to_xfer_partial
= linux_xfer_partial
;
4371 t
->to_static_tracepoint_markers_by_strid
4372 = linux_child_static_tracepoint_markers_by_strid
;
4378 struct target_ops
*t
;
4380 t
= inf_ptrace_target ();
4381 linux_target_install_ops (t
);
4387 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4389 struct target_ops
*t
;
4391 t
= inf_ptrace_trad_target (register_u_offset
);
4392 linux_target_install_ops (t
);
4397 /* target_is_async_p implementation. */
4400 linux_nat_is_async_p (struct target_ops
*ops
)
4402 return linux_is_async_p ();
4405 /* target_can_async_p implementation. */
4408 linux_nat_can_async_p (struct target_ops
*ops
)
4410 /* NOTE: palves 2008-03-21: We're only async when the user requests
4411 it explicitly with the "set target-async" command.
4412 Someday, linux will always be async. */
4413 return target_async_permitted
;
4417 linux_nat_supports_non_stop (struct target_ops
*self
)
4422 /* to_always_non_stop_p implementation. */
4425 linux_nat_always_non_stop_p (struct target_ops
*self
)
4430 /* True if we want to support multi-process. To be removed when GDB
4431 supports multi-exec. */
4433 int linux_multi_process
= 1;
4436 linux_nat_supports_multi_process (struct target_ops
*self
)
4438 return linux_multi_process
;
4442 linux_nat_supports_disable_randomization (struct target_ops
*self
)
4444 #ifdef HAVE_PERSONALITY
4451 static int async_terminal_is_ours
= 1;
4453 /* target_terminal_inferior implementation.
4455 This is a wrapper around child_terminal_inferior to add async support. */
4458 linux_nat_terminal_inferior (struct target_ops
*self
)
4460 child_terminal_inferior (self
);
4462 /* Calls to target_terminal_*() are meant to be idempotent. */
4463 if (!async_terminal_is_ours
)
4466 delete_file_handler (input_fd
);
4467 async_terminal_is_ours
= 0;
4471 /* target_terminal_ours implementation.
4473 This is a wrapper around child_terminal_ours to add async support (and
4474 implement the target_terminal_ours vs target_terminal_ours_for_output
4475 distinction). child_terminal_ours is currently no different than
4476 child_terminal_ours_for_output.
4477 We leave target_terminal_ours_for_output alone, leaving it to
4478 child_terminal_ours_for_output. */
4481 linux_nat_terminal_ours (struct target_ops
*self
)
4483 /* GDB should never give the terminal to the inferior if the
4484 inferior is running in the background (run&, continue&, etc.),
4485 but claiming it sure should. */
4486 child_terminal_ours (self
);
4488 if (async_terminal_is_ours
)
4491 clear_sigint_trap ();
4492 add_file_handler (input_fd
, stdin_event_handler
, 0);
4493 async_terminal_is_ours
= 1;
4496 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4497 so we notice when any child changes state, and notify the
4498 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4499 above to wait for the arrival of a SIGCHLD. */
4502 sigchld_handler (int signo
)
4504 int old_errno
= errno
;
4506 if (debug_linux_nat
)
4507 ui_file_write_async_safe (gdb_stdlog
,
4508 "sigchld\n", sizeof ("sigchld\n") - 1);
4510 if (signo
== SIGCHLD
4511 && linux_nat_event_pipe
[0] != -1)
4512 async_file_mark (); /* Let the event loop know that there are
4513 events to handle. */
4518 /* Callback registered with the target events file descriptor. */
4521 handle_target_event (int error
, gdb_client_data client_data
)
4523 inferior_event_handler (INF_REG_EVENT
, NULL
);
4526 /* Create/destroy the target events pipe. Returns previous state. */
4529 linux_async_pipe (int enable
)
4531 int previous
= linux_is_async_p ();
4533 if (previous
!= enable
)
4537 /* Block child signals while we create/destroy the pipe, as
4538 their handler writes to it. */
4539 block_child_signals (&prev_mask
);
4543 if (gdb_pipe_cloexec (linux_nat_event_pipe
) == -1)
4544 internal_error (__FILE__
, __LINE__
,
4545 "creating event pipe failed.");
4547 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4548 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4552 close (linux_nat_event_pipe
[0]);
4553 close (linux_nat_event_pipe
[1]);
4554 linux_nat_event_pipe
[0] = -1;
4555 linux_nat_event_pipe
[1] = -1;
4558 restore_child_signals_mask (&prev_mask
);
4564 /* target_async implementation. */
4567 linux_nat_async (struct target_ops
*ops
, int enable
)
4571 if (!linux_async_pipe (1))
4573 add_file_handler (linux_nat_event_pipe
[0],
4574 handle_target_event
, NULL
);
4575 /* There may be pending events to handle. Tell the event loop
4582 delete_file_handler (linux_nat_event_pipe
[0]);
4583 linux_async_pipe (0);
4588 /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
4592 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4596 if (debug_linux_nat
)
4597 fprintf_unfiltered (gdb_stdlog
,
4598 "LNSL: running -> suspending %s\n",
4599 target_pid_to_str (lwp
->ptid
));
4602 if (lwp
->last_resume_kind
== resume_stop
)
4604 if (debug_linux_nat
)
4605 fprintf_unfiltered (gdb_stdlog
,
4606 "linux-nat: already stopping LWP %ld at "
4608 ptid_get_lwp (lwp
->ptid
));
4612 stop_callback (lwp
, NULL
);
4613 lwp
->last_resume_kind
= resume_stop
;
4617 /* Already known to be stopped; do nothing. */
4619 if (debug_linux_nat
)
4621 if (find_thread_ptid (lwp
->ptid
)->stop_requested
)
4622 fprintf_unfiltered (gdb_stdlog
,
4623 "LNSL: already stopped/stop_requested %s\n",
4624 target_pid_to_str (lwp
->ptid
));
4626 fprintf_unfiltered (gdb_stdlog
,
4627 "LNSL: already stopped/no "
4628 "stop_requested yet %s\n",
4629 target_pid_to_str (lwp
->ptid
));
4636 linux_nat_stop (struct target_ops
*self
, ptid_t ptid
)
4638 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
4642 linux_nat_interrupt (struct target_ops
*self
, ptid_t ptid
)
4645 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
4647 linux_ops
->to_interrupt (linux_ops
, ptid
);
4651 linux_nat_close (struct target_ops
*self
)
4653 /* Unregister from the event loop. */
4654 if (linux_nat_is_async_p (self
))
4655 linux_nat_async (self
, 0);
4657 if (linux_ops
->to_close
)
4658 linux_ops
->to_close (linux_ops
);
4663 /* When requests are passed down from the linux-nat layer to the
4664 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
4665 used. The address space pointer is stored in the inferior object,
4666 but the common code that is passed such ptid can't tell whether
4667 lwpid is a "main" process id or not (it assumes so). We reverse
4668 look up the "main" process id from the lwp here. */
4670 static struct address_space
*
4671 linux_nat_thread_address_space (struct target_ops
*t
, ptid_t ptid
)
4673 struct lwp_info
*lwp
;
4674 struct inferior
*inf
;
4677 if (ptid_get_lwp (ptid
) == 0)
4679 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
4681 lwp
= find_lwp_pid (ptid
);
4682 pid
= ptid_get_pid (lwp
->ptid
);
4686 /* A (pid,lwpid,0) ptid. */
4687 pid
= ptid_get_pid (ptid
);
4690 inf
= find_inferior_pid (pid
);
4691 gdb_assert (inf
!= NULL
);
4695 /* Return the cached value of the processor core for thread PTID. */
4698 linux_nat_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
4700 struct lwp_info
*info
= find_lwp_pid (ptid
);
4707 /* Implementation of to_filesystem_is_local. */
4710 linux_nat_filesystem_is_local (struct target_ops
*ops
)
4712 struct inferior
*inf
= current_inferior ();
4714 if (inf
->fake_pid_p
|| inf
->pid
== 0)
4717 return linux_ns_same (inf
->pid
, LINUX_NS_MNT
);
4720 /* Convert the INF argument passed to a to_fileio_* method
4721 to a process ID suitable for passing to its corresponding
4722 linux_mntns_* function. If INF is non-NULL then the
4723 caller is requesting the filesystem seen by INF. If INF
4724 is NULL then the caller is requesting the filesystem seen
4725 by the GDB. We fall back to GDB's filesystem in the case
4726 that INF is non-NULL but its PID is unknown. */
4729 linux_nat_fileio_pid_of (struct inferior
*inf
)
4731 if (inf
== NULL
|| inf
->fake_pid_p
|| inf
->pid
== 0)
4737 /* Implementation of to_fileio_open. */
4740 linux_nat_fileio_open (struct target_ops
*self
,
4741 struct inferior
*inf
, const char *filename
,
4742 int flags
, int mode
, int warn_if_slow
,
4749 if (fileio_to_host_openflags (flags
, &nat_flags
) == -1
4750 || fileio_to_host_mode (mode
, &nat_mode
) == -1)
4752 *target_errno
= FILEIO_EINVAL
;
4756 fd
= linux_mntns_open_cloexec (linux_nat_fileio_pid_of (inf
),
4757 filename
, nat_flags
, nat_mode
);
4759 *target_errno
= host_to_fileio_error (errno
);
4764 /* Implementation of to_fileio_readlink. */
4767 linux_nat_fileio_readlink (struct target_ops
*self
,
4768 struct inferior
*inf
, const char *filename
,
4775 len
= linux_mntns_readlink (linux_nat_fileio_pid_of (inf
),
4776 filename
, buf
, sizeof (buf
));
4779 *target_errno
= host_to_fileio_error (errno
);
4783 ret
= (char *) xmalloc (len
+ 1);
4784 memcpy (ret
, buf
, len
);
4789 /* Implementation of to_fileio_unlink. */
4792 linux_nat_fileio_unlink (struct target_ops
*self
,
4793 struct inferior
*inf
, const char *filename
,
4798 ret
= linux_mntns_unlink (linux_nat_fileio_pid_of (inf
),
4801 *target_errno
= host_to_fileio_error (errno
);
4807 linux_nat_add_target (struct target_ops
*t
)
4809 /* Save the provided single-threaded target. We save this in a separate
4810 variable because another target we've inherited from (e.g. inf-ptrace)
4811 may have saved a pointer to T; we want to use it for the final
4812 process stratum target. */
4813 linux_ops_saved
= *t
;
4814 linux_ops
= &linux_ops_saved
;
4816 /* Override some methods for multithreading. */
4817 t
->to_create_inferior
= linux_nat_create_inferior
;
4818 t
->to_attach
= linux_nat_attach
;
4819 t
->to_detach
= linux_nat_detach
;
4820 t
->to_resume
= linux_nat_resume
;
4821 t
->to_wait
= linux_nat_wait
;
4822 t
->to_pass_signals
= linux_nat_pass_signals
;
4823 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4824 t
->to_kill
= linux_nat_kill
;
4825 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4826 t
->to_thread_alive
= linux_nat_thread_alive
;
4827 t
->to_update_thread_list
= linux_nat_update_thread_list
;
4828 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4829 t
->to_thread_name
= linux_nat_thread_name
;
4830 t
->to_has_thread_control
= tc_schedlock
;
4831 t
->to_thread_address_space
= linux_nat_thread_address_space
;
4832 t
->to_stopped_by_watchpoint
= linux_nat_stopped_by_watchpoint
;
4833 t
->to_stopped_data_address
= linux_nat_stopped_data_address
;
4834 t
->to_stopped_by_sw_breakpoint
= linux_nat_stopped_by_sw_breakpoint
;
4835 t
->to_supports_stopped_by_sw_breakpoint
= linux_nat_supports_stopped_by_sw_breakpoint
;
4836 t
->to_stopped_by_hw_breakpoint
= linux_nat_stopped_by_hw_breakpoint
;
4837 t
->to_supports_stopped_by_hw_breakpoint
= linux_nat_supports_stopped_by_hw_breakpoint
;
4839 t
->to_can_async_p
= linux_nat_can_async_p
;
4840 t
->to_is_async_p
= linux_nat_is_async_p
;
4841 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
4842 t
->to_always_non_stop_p
= linux_nat_always_non_stop_p
;
4843 t
->to_async
= linux_nat_async
;
4844 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4845 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4847 super_close
= t
->to_close
;
4848 t
->to_close
= linux_nat_close
;
4850 t
->to_stop
= linux_nat_stop
;
4851 t
->to_interrupt
= linux_nat_interrupt
;
4853 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
4855 t
->to_supports_disable_randomization
4856 = linux_nat_supports_disable_randomization
;
4858 t
->to_core_of_thread
= linux_nat_core_of_thread
;
4860 t
->to_filesystem_is_local
= linux_nat_filesystem_is_local
;
4861 t
->to_fileio_open
= linux_nat_fileio_open
;
4862 t
->to_fileio_readlink
= linux_nat_fileio_readlink
;
4863 t
->to_fileio_unlink
= linux_nat_fileio_unlink
;
4865 /* We don't change the stratum; this target will sit at
4866 process_stratum and thread_db will set at thread_stratum. This
4867 is a little strange, since this is a multi-threaded-capable
4868 target, but we want to be on the stack below thread_db, and we
4869 also want to be used for single-threaded processes. */
4874 /* Register a method to call whenever a new thread is attached. */
4876 linux_nat_set_new_thread (struct target_ops
*t
,
4877 void (*new_thread
) (struct lwp_info
*))
4879 /* Save the pointer. We only support a single registered instance
4880 of the GNU/Linux native target, so we do not need to map this to
4882 linux_nat_new_thread
= new_thread
;
4885 /* See declaration in linux-nat.h. */
4888 linux_nat_set_new_fork (struct target_ops
*t
,
4889 linux_nat_new_fork_ftype
*new_fork
)
4891 /* Save the pointer. */
4892 linux_nat_new_fork
= new_fork
;
4895 /* See declaration in linux-nat.h. */
4898 linux_nat_set_forget_process (struct target_ops
*t
,
4899 linux_nat_forget_process_ftype
*fn
)
4901 /* Save the pointer. */
4902 linux_nat_forget_process_hook
= fn
;
4905 /* See declaration in linux-nat.h. */
4908 linux_nat_forget_process (pid_t pid
)
4910 if (linux_nat_forget_process_hook
!= NULL
)
4911 linux_nat_forget_process_hook (pid
);
4914 /* Register a method that converts a siginfo object between the layout
4915 that ptrace returns, and the layout in the architecture of the
4918 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
4919 int (*siginfo_fixup
) (siginfo_t
*,
4923 /* Save the pointer. */
4924 linux_nat_siginfo_fixup
= siginfo_fixup
;
4927 /* Register a method to call prior to resuming a thread. */
4930 linux_nat_set_prepare_to_resume (struct target_ops
*t
,
4931 void (*prepare_to_resume
) (struct lwp_info
*))
4933 /* Save the pointer. */
4934 linux_nat_prepare_to_resume
= prepare_to_resume
;
4937 /* See linux-nat.h. */
4940 linux_nat_get_siginfo (ptid_t ptid
, siginfo_t
*siginfo
)
4944 pid
= ptid_get_lwp (ptid
);
4946 pid
= ptid_get_pid (ptid
);
4949 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, siginfo
);
4952 memset (siginfo
, 0, sizeof (*siginfo
));
4958 /* See nat/linux-nat.h. */
4961 current_lwp_ptid (void)
4963 gdb_assert (ptid_lwp_p (inferior_ptid
));
4964 return inferior_ptid
;
4967 /* Provide a prototype to silence -Wmissing-prototypes. */
4968 extern initialize_file_ftype _initialize_linux_nat
;
4971 _initialize_linux_nat (void)
4973 add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance
,
4974 &debug_linux_nat
, _("\
4975 Set debugging of GNU/Linux lwp module."), _("\
4976 Show debugging of GNU/Linux lwp module."), _("\
4977 Enables printf debugging output."),
4979 show_debug_linux_nat
,
4980 &setdebuglist
, &showdebuglist
);
4982 add_setshow_boolean_cmd ("linux-namespaces", class_maintenance
,
4983 &debug_linux_namespaces
, _("\
4984 Set debugging of GNU/Linux namespaces module."), _("\
4985 Show debugging of GNU/Linux namespaces module."), _("\
4986 Enables printf debugging output."),
4989 &setdebuglist
, &showdebuglist
);
4991 /* Save this mask as the default. */
4992 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4994 /* Install a SIGCHLD handler. */
4995 sigchld_action
.sa_handler
= sigchld_handler
;
4996 sigemptyset (&sigchld_action
.sa_mask
);
4997 sigchld_action
.sa_flags
= SA_RESTART
;
4999 /* Make it the default. */
5000 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
5002 /* Make sure we don't block SIGCHLD during a sigsuspend. */
5003 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
5004 sigdelset (&suspend_mask
, SIGCHLD
);
5006 sigemptyset (&blocked_mask
);
5010 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
5011 the GNU/Linux Threads library and therefore doesn't really belong
5014 /* Read variable NAME in the target and return its value if found.
5015 Otherwise return zero. It is assumed that the type of the variable
5019 get_signo (const char *name
)
5021 struct bound_minimal_symbol ms
;
5024 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
5025 if (ms
.minsym
== NULL
)
5028 if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
5029 sizeof (signo
)) != 0)
5035 /* Return the set of signals used by the threads library in *SET. */
5038 lin_thread_get_thread_signals (sigset_t
*set
)
5040 struct sigaction action
;
5041 int restart
, cancel
;
5043 sigemptyset (&blocked_mask
);
5046 restart
= get_signo ("__pthread_sig_restart");
5047 cancel
= get_signo ("__pthread_sig_cancel");
5049 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5050 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5051 not provide any way for the debugger to query the signal numbers -
5052 fortunately they don't change! */
5055 restart
= __SIGRTMIN
;
5058 cancel
= __SIGRTMIN
+ 1;
5060 sigaddset (set
, restart
);
5061 sigaddset (set
, cancel
);
5063 /* The GNU/Linux Threads library makes terminating threads send a
5064 special "cancel" signal instead of SIGCHLD. Make sure we catch
5065 those (to prevent them from terminating GDB itself, which is
5066 likely to be their default action) and treat them the same way as
5069 action
.sa_handler
= sigchld_handler
;
5070 sigemptyset (&action
.sa_mask
);
5071 action
.sa_flags
= SA_RESTART
;
5072 sigaction (cancel
, &action
, NULL
);
5074 /* We block the "cancel" signal throughout this code ... */
5075 sigaddset (&blocked_mask
, cancel
);
5076 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
5078 /* ... except during a sigsuspend. */
5079 sigdelset (&suspend_mask
, cancel
);