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 <sys/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"
71 #define SPUFS_MAGIC 0x23c9b64e
74 /* This comment documents high-level logic of this file.
76 Waiting for events in sync mode
77 ===============================
79 When waiting for an event in a specific thread, we just use waitpid, passing
80 the specific pid, and not passing WNOHANG.
82 When waiting for an event in all threads, waitpid is not quite good. Prior to
83 version 2.4, Linux can either wait for event in main thread, or in secondary
84 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
85 miss an event. The solution is to use non-blocking waitpid, together with
86 sigsuspend. First, we use non-blocking waitpid to get an event in the main
87 process, if any. Second, we use non-blocking waitpid with the __WCLONED
88 flag to check for events in cloned processes. If nothing is found, we use
89 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
90 happened to a child process -- and SIGCHLD will be delivered both for events
91 in main debugged process and in cloned processes. As soon as we know there's
92 an event, we get back to calling nonblocking waitpid with and without
95 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
96 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
97 blocked, the signal becomes pending and sigsuspend immediately
98 notices it and returns.
100 Waiting for events in async mode
101 ================================
103 In async mode, GDB should always be ready to handle both user input
104 and target events, so neither blocking waitpid nor sigsuspend are
105 viable options. Instead, we should asynchronously notify the GDB main
106 event loop whenever there's an unprocessed event from the target. We
107 detect asynchronous target events by handling SIGCHLD signals. To
108 notify the event loop about target events, the self-pipe trick is used
109 --- a pipe is registered as waitable event source in the event loop,
110 the event loop select/poll's on the read end of this pipe (as well on
111 other event sources, e.g., stdin), and the SIGCHLD handler writes a
112 byte to this pipe. This is more portable than relying on
113 pselect/ppoll, since on kernels that lack those syscalls, libc
114 emulates them with select/poll+sigprocmask, and that is racy
115 (a.k.a. plain broken).
117 Obviously, if we fail to notify the event loop if there's a target
118 event, it's bad. OTOH, if we notify the event loop when there's no
119 event from the target, linux_nat_wait will detect that there's no real
120 event to report, and return event of type TARGET_WAITKIND_IGNORE.
121 This is mostly harmless, but it will waste time and is better avoided.
123 The main design point is that every time GDB is outside linux-nat.c,
124 we have a SIGCHLD handler installed that is called when something
125 happens to the target and notifies the GDB event loop. Whenever GDB
126 core decides to handle the event, and calls into linux-nat.c, we
127 process things as in sync mode, except that the we never block in
130 While processing an event, we may end up momentarily blocked in
131 waitpid calls. Those waitpid calls, while blocking, are guarantied to
132 return quickly. E.g., in all-stop mode, before reporting to the core
133 that an LWP hit a breakpoint, all LWPs are stopped by sending them
134 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
135 Note that this is different from blocking indefinitely waiting for the
136 next event --- here, we're already handling an event.
141 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
142 signal is not entirely significant; we just need for a signal to be delivered,
143 so that we can intercept it. SIGSTOP's advantage is that it can not be
144 blocked. A disadvantage is that it is not a real-time signal, so it can only
145 be queued once; we do not keep track of other sources of SIGSTOP.
147 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
148 use them, because they have special behavior when the signal is generated -
149 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
150 kills the entire thread group.
152 A delivered SIGSTOP would stop the entire thread group, not just the thread we
153 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
154 cancel it (by PTRACE_CONT without passing SIGSTOP).
156 We could use a real-time signal instead. This would solve those problems; we
157 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
158 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
159 generates it, and there are races with trying to find a signal that is not
163 #define O_LARGEFILE 0
166 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
167 the use of the multi-threaded target. */
168 static struct target_ops
*linux_ops
;
169 static struct target_ops linux_ops_saved
;
171 /* The method to call, if any, when a new thread is attached. */
172 static void (*linux_nat_new_thread
) (struct lwp_info
*);
174 /* The method to call, if any, when a new fork is attached. */
175 static linux_nat_new_fork_ftype
*linux_nat_new_fork
;
177 /* The method to call, if any, when a process is no longer
179 static linux_nat_forget_process_ftype
*linux_nat_forget_process_hook
;
181 /* Hook to call prior to resuming a thread. */
182 static void (*linux_nat_prepare_to_resume
) (struct lwp_info
*);
184 /* The method to call, if any, when the siginfo object needs to be
185 converted between the layout returned by ptrace, and the layout in
186 the architecture of the inferior. */
187 static int (*linux_nat_siginfo_fixup
) (siginfo_t
*,
191 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
192 Called by our to_xfer_partial. */
193 static target_xfer_partial_ftype
*super_xfer_partial
;
195 /* The saved to_close method, inherited from inf-ptrace.c.
196 Called by our to_close. */
197 static void (*super_close
) (struct target_ops
*);
199 static unsigned int debug_linux_nat
;
201 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
202 struct cmd_list_element
*c
, const char *value
)
204 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
208 struct simple_pid_list
212 struct simple_pid_list
*next
;
214 struct simple_pid_list
*stopped_pids
;
216 /* Async mode support. */
218 /* The read/write ends of the pipe registered as waitable file in the
220 static int linux_nat_event_pipe
[2] = { -1, -1 };
222 /* True if we're currently in async mode. */
223 #define linux_is_async_p() (linux_nat_event_pipe[0] != -1)
225 /* Flush the event pipe. */
228 async_file_flush (void)
235 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
237 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
240 /* Put something (anything, doesn't matter what, or how much) in event
241 pipe, so that the select/poll in the event-loop realizes we have
242 something to process. */
245 async_file_mark (void)
249 /* It doesn't really matter what the pipe contains, as long we end
250 up with something in it. Might as well flush the previous
256 ret
= write (linux_nat_event_pipe
[1], "+", 1);
258 while (ret
== -1 && errno
== EINTR
);
260 /* Ignore EAGAIN. If the pipe is full, the event loop will already
261 be awakened anyway. */
264 static int kill_lwp (int lwpid
, int signo
);
266 static int stop_callback (struct lwp_info
*lp
, void *data
);
267 static int resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
);
269 static void block_child_signals (sigset_t
*prev_mask
);
270 static void restore_child_signals_mask (sigset_t
*prev_mask
);
273 static struct lwp_info
*add_lwp (ptid_t ptid
);
274 static void purge_lwp_list (int pid
);
275 static void delete_lwp (ptid_t ptid
);
276 static struct lwp_info
*find_lwp_pid (ptid_t ptid
);
278 static int lwp_status_pending_p (struct lwp_info
*lp
);
280 static int check_stopped_by_breakpoint (struct lwp_info
*lp
);
281 static int sigtrap_is_event (int status
);
282 static int (*linux_nat_status_is_event
) (int status
) = sigtrap_is_event
;
285 /* Trivial list manipulation functions to keep track of a list of
286 new stopped processes. */
288 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
290 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
293 new_pid
->status
= status
;
294 new_pid
->next
= *listp
;
299 in_pid_list_p (struct simple_pid_list
*list
, int pid
)
301 struct simple_pid_list
*p
;
303 for (p
= list
; p
!= NULL
; p
= p
->next
)
310 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
312 struct simple_pid_list
**p
;
314 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
315 if ((*p
)->pid
== pid
)
317 struct simple_pid_list
*next
= (*p
)->next
;
319 *statusp
= (*p
)->status
;
327 /* Initialize ptrace warnings and check for supported ptrace
330 ATTACHED should be nonzero iff we attached to the inferior. */
333 linux_init_ptrace (pid_t pid
, int attached
)
335 linux_enable_event_reporting (pid
, attached
);
336 linux_ptrace_init_warnings ();
340 linux_child_post_attach (struct target_ops
*self
, int pid
)
342 linux_init_ptrace (pid
, 1);
346 linux_child_post_startup_inferior (struct target_ops
*self
, ptid_t ptid
)
348 linux_init_ptrace (ptid_get_pid (ptid
), 0);
351 /* Return the number of known LWPs in the tgid given by PID. */
359 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
360 if (ptid_get_pid (lp
->ptid
) == pid
)
366 /* Call delete_lwp with prototype compatible for make_cleanup. */
369 delete_lwp_cleanup (void *lp_voidp
)
371 struct lwp_info
*lp
= lp_voidp
;
373 delete_lwp (lp
->ptid
);
376 /* Target hook for follow_fork. On entry inferior_ptid must be the
377 ptid of the followed inferior. At return, inferior_ptid will be
381 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
,
386 struct lwp_info
*child_lp
= NULL
;
387 int status
= W_STOPCODE (0);
388 struct cleanup
*old_chain
;
390 ptid_t parent_ptid
, child_ptid
;
391 int parent_pid
, child_pid
;
393 has_vforked
= (inferior_thread ()->pending_follow
.kind
394 == TARGET_WAITKIND_VFORKED
);
395 parent_ptid
= inferior_ptid
;
396 child_ptid
= inferior_thread ()->pending_follow
.value
.related_pid
;
397 parent_pid
= ptid_get_lwp (parent_ptid
);
398 child_pid
= ptid_get_lwp (child_ptid
);
400 /* We're already attached to the parent, by default. */
401 old_chain
= save_inferior_ptid ();
402 inferior_ptid
= child_ptid
;
403 child_lp
= add_lwp (inferior_ptid
);
404 child_lp
->stopped
= 1;
405 child_lp
->last_resume_kind
= resume_stop
;
407 /* Detach new forked process? */
410 make_cleanup (delete_lwp_cleanup
, child_lp
);
412 if (linux_nat_prepare_to_resume
!= NULL
)
413 linux_nat_prepare_to_resume (child_lp
);
415 /* When debugging an inferior in an architecture that supports
416 hardware single stepping on a kernel without commit
417 6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child
418 process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits
419 set if the parent process had them set.
420 To work around this, single step the child process
421 once before detaching to clear the flags. */
423 if (!gdbarch_software_single_step_p (target_thread_architecture
426 linux_disable_event_reporting (child_pid
);
427 if (ptrace (PTRACE_SINGLESTEP
, child_pid
, 0, 0) < 0)
428 perror_with_name (_("Couldn't do single step"));
429 if (my_waitpid (child_pid
, &status
, 0) < 0)
430 perror_with_name (_("Couldn't wait vfork process"));
433 if (WIFSTOPPED (status
))
437 signo
= WSTOPSIG (status
);
439 && !signal_pass_state (gdb_signal_from_host (signo
)))
441 ptrace (PTRACE_DETACH
, child_pid
, 0, signo
);
444 /* Resets value of inferior_ptid to parent ptid. */
445 do_cleanups (old_chain
);
449 /* Let the thread_db layer learn about this new process. */
450 check_for_thread_db ();
453 do_cleanups (old_chain
);
457 struct lwp_info
*parent_lp
;
459 parent_lp
= find_lwp_pid (parent_ptid
);
460 gdb_assert (linux_supports_tracefork () >= 0);
462 if (linux_supports_tracevforkdone ())
465 fprintf_unfiltered (gdb_stdlog
,
466 "LCFF: waiting for VFORK_DONE on %d\n",
468 parent_lp
->stopped
= 1;
470 /* We'll handle the VFORK_DONE event like any other
471 event, in target_wait. */
475 /* We can't insert breakpoints until the child has
476 finished with the shared memory region. We need to
477 wait until that happens. Ideal would be to just
479 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
480 - waitpid (parent_pid, &status, __WALL);
481 However, most architectures can't handle a syscall
482 being traced on the way out if it wasn't traced on
485 We might also think to loop, continuing the child
486 until it exits or gets a SIGTRAP. One problem is
487 that the child might call ptrace with PTRACE_TRACEME.
489 There's no simple and reliable way to figure out when
490 the vforked child will be done with its copy of the
491 shared memory. We could step it out of the syscall,
492 two instructions, let it go, and then single-step the
493 parent once. When we have hardware single-step, this
494 would work; with software single-step it could still
495 be made to work but we'd have to be able to insert
496 single-step breakpoints in the child, and we'd have
497 to insert -just- the single-step breakpoint in the
498 parent. Very awkward.
500 In the end, the best we can do is to make sure it
501 runs for a little while. Hopefully it will be out of
502 range of any breakpoints we reinsert. Usually this
503 is only the single-step breakpoint at vfork's return
507 fprintf_unfiltered (gdb_stdlog
,
508 "LCFF: no VFORK_DONE "
509 "support, sleeping a bit\n");
513 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
514 and leave it pending. The next linux_nat_resume call
515 will notice a pending event, and bypasses actually
516 resuming the inferior. */
517 parent_lp
->status
= 0;
518 parent_lp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
519 parent_lp
->stopped
= 1;
521 /* If we're in async mode, need to tell the event loop
522 there's something here to process. */
523 if (target_is_async_p ())
530 struct lwp_info
*child_lp
;
532 child_lp
= add_lwp (inferior_ptid
);
533 child_lp
->stopped
= 1;
534 child_lp
->last_resume_kind
= resume_stop
;
536 /* Let the thread_db layer learn about this new process. */
537 check_for_thread_db ();
545 linux_child_insert_fork_catchpoint (struct target_ops
*self
, int pid
)
547 return !linux_supports_tracefork ();
551 linux_child_remove_fork_catchpoint (struct target_ops
*self
, int pid
)
557 linux_child_insert_vfork_catchpoint (struct target_ops
*self
, int pid
)
559 return !linux_supports_tracefork ();
563 linux_child_remove_vfork_catchpoint (struct target_ops
*self
, int pid
)
569 linux_child_insert_exec_catchpoint (struct target_ops
*self
, int pid
)
571 return !linux_supports_tracefork ();
575 linux_child_remove_exec_catchpoint (struct target_ops
*self
, int pid
)
581 linux_child_set_syscall_catchpoint (struct target_ops
*self
,
582 int pid
, int needed
, int any_count
,
583 int table_size
, int *table
)
585 if (!linux_supports_tracesysgood ())
588 /* On GNU/Linux, we ignore the arguments. It means that we only
589 enable the syscall catchpoints, but do not disable them.
591 Also, we do not use the `table' information because we do not
592 filter system calls here. We let GDB do the logic for us. */
596 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
597 are processes sharing the same VM space. A multi-threaded process
598 is basically a group of such processes. However, such a grouping
599 is almost entirely a user-space issue; the kernel doesn't enforce
600 such a grouping at all (this might change in the future). In
601 general, we'll rely on the threads library (i.e. the GNU/Linux
602 Threads library) to provide such a grouping.
604 It is perfectly well possible to write a multi-threaded application
605 without the assistance of a threads library, by using the clone
606 system call directly. This module should be able to give some
607 rudimentary support for debugging such applications if developers
608 specify the CLONE_PTRACE flag in the clone system call, and are
609 using the Linux kernel 2.4 or above.
611 Note that there are some peculiarities in GNU/Linux that affect
614 - In general one should specify the __WCLONE flag to waitpid in
615 order to make it report events for any of the cloned processes
616 (and leave it out for the initial process). However, if a cloned
617 process has exited the exit status is only reported if the
618 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
619 we cannot use it since GDB must work on older systems too.
621 - When a traced, cloned process exits and is waited for by the
622 debugger, the kernel reassigns it to the original parent and
623 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
624 library doesn't notice this, which leads to the "zombie problem":
625 When debugged a multi-threaded process that spawns a lot of
626 threads will run out of processes, even if the threads exit,
627 because the "zombies" stay around. */
629 /* List of known LWPs. */
630 struct lwp_info
*lwp_list
;
633 /* Original signal mask. */
634 static sigset_t normal_mask
;
636 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
637 _initialize_linux_nat. */
638 static sigset_t suspend_mask
;
640 /* Signals to block to make that sigsuspend work. */
641 static sigset_t blocked_mask
;
643 /* SIGCHLD action. */
644 struct sigaction sigchld_action
;
646 /* Block child signals (SIGCHLD and linux threads signals), and store
647 the previous mask in PREV_MASK. */
650 block_child_signals (sigset_t
*prev_mask
)
652 /* Make sure SIGCHLD is blocked. */
653 if (!sigismember (&blocked_mask
, SIGCHLD
))
654 sigaddset (&blocked_mask
, SIGCHLD
);
656 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
659 /* Restore child signals mask, previously returned by
660 block_child_signals. */
663 restore_child_signals_mask (sigset_t
*prev_mask
)
665 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
668 /* Mask of signals to pass directly to the inferior. */
669 static sigset_t pass_mask
;
671 /* Update signals to pass to the inferior. */
673 linux_nat_pass_signals (struct target_ops
*self
,
674 int numsigs
, unsigned char *pass_signals
)
678 sigemptyset (&pass_mask
);
680 for (signo
= 1; signo
< NSIG
; signo
++)
682 int target_signo
= gdb_signal_from_host (signo
);
683 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
684 sigaddset (&pass_mask
, signo
);
690 /* Prototypes for local functions. */
691 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
692 static int linux_thread_alive (ptid_t ptid
);
693 static char *linux_child_pid_to_exec_file (struct target_ops
*self
, int pid
);
694 static int resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
);
698 /* Destroy and free LP. */
701 lwp_free (struct lwp_info
*lp
)
703 xfree (lp
->arch_private
);
707 /* Remove all LWPs belong to PID from the lwp list. */
710 purge_lwp_list (int pid
)
712 struct lwp_info
*lp
, *lpprev
, *lpnext
;
716 for (lp
= lwp_list
; lp
; lp
= lpnext
)
720 if (ptid_get_pid (lp
->ptid
) == pid
)
725 lpprev
->next
= lp
->next
;
734 /* Add the LWP specified by PTID to the list. PTID is the first LWP
735 in the process. Return a pointer to the structure describing the
738 This differs from add_lwp in that we don't let the arch specific
739 bits know about this new thread. Current clients of this callback
740 take the opportunity to install watchpoints in the new thread, and
741 we shouldn't do that for the first thread. If we're spawning a
742 child ("run"), the thread executes the shell wrapper first, and we
743 shouldn't touch it until it execs the program we want to debug.
744 For "attach", it'd be okay to call the callback, but it's not
745 necessary, because watchpoints can't yet have been inserted into
748 static struct lwp_info
*
749 add_initial_lwp (ptid_t ptid
)
753 gdb_assert (ptid_lwp_p (ptid
));
755 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
757 memset (lp
, 0, sizeof (struct lwp_info
));
759 lp
->last_resume_kind
= resume_continue
;
760 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
771 /* Add the LWP specified by PID to the list. Return a pointer to the
772 structure describing the new LWP. The LWP should already be
775 static struct lwp_info
*
776 add_lwp (ptid_t ptid
)
780 lp
= add_initial_lwp (ptid
);
782 /* Let the arch specific bits know about this new thread. Current
783 clients of this callback take the opportunity to install
784 watchpoints in the new thread. We don't do this for the first
785 thread though. See add_initial_lwp. */
786 if (linux_nat_new_thread
!= NULL
)
787 linux_nat_new_thread (lp
);
792 /* Remove the LWP specified by PID from the list. */
795 delete_lwp (ptid_t ptid
)
797 struct lwp_info
*lp
, *lpprev
;
801 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
802 if (ptid_equal (lp
->ptid
, ptid
))
809 lpprev
->next
= lp
->next
;
816 /* Return a pointer to the structure describing the LWP corresponding
817 to PID. If no corresponding LWP could be found, return NULL. */
819 static struct lwp_info
*
820 find_lwp_pid (ptid_t ptid
)
825 if (ptid_lwp_p (ptid
))
826 lwp
= ptid_get_lwp (ptid
);
828 lwp
= ptid_get_pid (ptid
);
830 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
831 if (lwp
== ptid_get_lwp (lp
->ptid
))
837 /* See nat/linux-nat.h. */
840 iterate_over_lwps (ptid_t filter
,
841 iterate_over_lwps_ftype callback
,
844 struct lwp_info
*lp
, *lpnext
;
846 for (lp
= lwp_list
; lp
; lp
= lpnext
)
850 if (ptid_match (lp
->ptid
, filter
))
852 if ((*callback
) (lp
, data
) != 0)
860 /* Update our internal state when changing from one checkpoint to
861 another indicated by NEW_PTID. We can only switch single-threaded
862 applications, so we only create one new LWP, and the previous list
866 linux_nat_switch_fork (ptid_t new_ptid
)
870 purge_lwp_list (ptid_get_pid (inferior_ptid
));
872 lp
= add_lwp (new_ptid
);
875 /* This changes the thread's ptid while preserving the gdb thread
876 num. Also changes the inferior pid, while preserving the
878 thread_change_ptid (inferior_ptid
, new_ptid
);
880 /* We've just told GDB core that the thread changed target id, but,
881 in fact, it really is a different thread, with different register
883 registers_changed ();
886 /* Handle the exit of a single thread LP. */
889 exit_lwp (struct lwp_info
*lp
)
891 struct thread_info
*th
= find_thread_ptid (lp
->ptid
);
895 if (print_thread_events
)
896 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
898 delete_thread (lp
->ptid
);
901 delete_lwp (lp
->ptid
);
904 /* Wait for the LWP specified by LP, which we have just attached to.
905 Returns a wait status for that LWP, to cache. */
908 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
911 pid_t new_pid
, pid
= ptid_get_lwp (ptid
);
914 if (linux_proc_pid_is_stopped (pid
))
917 fprintf_unfiltered (gdb_stdlog
,
918 "LNPAW: Attaching to a stopped process\n");
920 /* The process is definitely stopped. It is in a job control
921 stop, unless the kernel predates the TASK_STOPPED /
922 TASK_TRACED distinction, in which case it might be in a
923 ptrace stop. Make sure it is in a ptrace stop; from there we
924 can kill it, signal it, et cetera.
926 First make sure there is a pending SIGSTOP. Since we are
927 already attached, the process can not transition from stopped
928 to running without a PTRACE_CONT; so we know this signal will
929 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
930 probably already in the queue (unless this kernel is old
931 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
932 is not an RT signal, it can only be queued once. */
933 kill_lwp (pid
, SIGSTOP
);
935 /* Finally, resume the stopped process. This will deliver the SIGSTOP
936 (or a higher priority signal, just like normal PTRACE_ATTACH). */
937 ptrace (PTRACE_CONT
, pid
, 0, 0);
940 /* Make sure the initial process is stopped. The user-level threads
941 layer might want to poke around in the inferior, and that won't
942 work if things haven't stabilized yet. */
943 new_pid
= my_waitpid (pid
, &status
, 0);
944 if (new_pid
== -1 && errno
== ECHILD
)
947 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
949 /* Try again with __WCLONE to check cloned processes. */
950 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
954 gdb_assert (pid
== new_pid
);
956 if (!WIFSTOPPED (status
))
958 /* The pid we tried to attach has apparently just exited. */
960 fprintf_unfiltered (gdb_stdlog
, "LNPAW: Failed to stop %d: %s",
961 pid
, status_to_str (status
));
965 if (WSTOPSIG (status
) != SIGSTOP
)
969 fprintf_unfiltered (gdb_stdlog
,
970 "LNPAW: Received %s after attaching\n",
971 status_to_str (status
));
977 /* Attach to the LWP specified by PID. Return 0 if successful, -1 if
978 the new LWP could not be attached, or 1 if we're already auto
979 attached to this thread, but haven't processed the
980 PTRACE_EVENT_CLONE event of its parent thread, so we just ignore
981 its existance, without considering it an error. */
984 lin_lwp_attach_lwp (ptid_t ptid
)
989 gdb_assert (ptid_lwp_p (ptid
));
991 lp
= find_lwp_pid (ptid
);
992 lwpid
= ptid_get_lwp (ptid
);
994 /* We assume that we're already attached to any LWP that is already
995 in our list of LWPs. If we're not seeing exit events from threads
996 and we've had PID wraparound since we last tried to stop all threads,
997 this assumption might be wrong; fortunately, this is very unlikely
1001 int status
, cloned
= 0, signalled
= 0;
1003 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) < 0)
1005 if (linux_supports_tracefork ())
1007 /* If we haven't stopped all threads when we get here,
1008 we may have seen a thread listed in thread_db's list,
1009 but not processed the PTRACE_EVENT_CLONE yet. If
1010 that's the case, ignore this new thread, and let
1011 normal event handling discover it later. */
1012 if (in_pid_list_p (stopped_pids
, lwpid
))
1014 /* We've already seen this thread stop, but we
1015 haven't seen the PTRACE_EVENT_CLONE extended
1017 if (debug_linux_nat
)
1018 fprintf_unfiltered (gdb_stdlog
,
1019 "LLAL: attach failed, but already seen "
1020 "this thread %s stop\n",
1021 target_pid_to_str (ptid
));
1029 if (debug_linux_nat
)
1030 fprintf_unfiltered (gdb_stdlog
,
1031 "LLAL: attach failed, and haven't seen "
1032 "this thread %s stop yet\n",
1033 target_pid_to_str (ptid
));
1035 /* We may or may not be attached to the LWP already.
1036 Try waitpid on it. If that errors, we're not
1037 attached to the LWP yet. Otherwise, we're
1038 already attached. */
1039 gdb_assert (lwpid
> 0);
1040 new_pid
= my_waitpid (lwpid
, &status
, WNOHANG
);
1041 if (new_pid
== -1 && errno
== ECHILD
)
1042 new_pid
= my_waitpid (lwpid
, &status
, __WCLONE
| WNOHANG
);
1047 /* The child hasn't stopped for its initial
1048 SIGSTOP stop yet. */
1049 if (debug_linux_nat
)
1050 fprintf_unfiltered (gdb_stdlog
,
1051 "LLAL: child hasn't "
1054 else if (WIFSTOPPED (status
))
1056 if (debug_linux_nat
)
1057 fprintf_unfiltered (gdb_stdlog
,
1058 "LLAL: adding to stopped_pids\n");
1059 add_to_pid_list (&stopped_pids
, lwpid
, status
);
1066 /* If we fail to attach to the thread, issue a warning,
1067 but continue. One way this can happen is if thread
1068 creation is interrupted; as of Linux kernel 2.6.19, a
1069 bug may place threads in the thread list and then fail
1071 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1072 safe_strerror (errno
));
1076 if (debug_linux_nat
)
1077 fprintf_unfiltered (gdb_stdlog
,
1078 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1079 target_pid_to_str (ptid
));
1081 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1082 if (!WIFSTOPPED (status
))
1085 lp
= add_lwp (ptid
);
1087 lp
->last_resume_kind
= resume_stop
;
1088 lp
->cloned
= cloned
;
1089 lp
->signalled
= signalled
;
1090 if (WSTOPSIG (status
) != SIGSTOP
)
1093 lp
->status
= status
;
1096 target_post_attach (ptid_get_lwp (lp
->ptid
));
1098 if (debug_linux_nat
)
1100 fprintf_unfiltered (gdb_stdlog
,
1101 "LLAL: waitpid %s received %s\n",
1102 target_pid_to_str (ptid
),
1103 status_to_str (status
));
1111 linux_nat_create_inferior (struct target_ops
*ops
,
1112 char *exec_file
, char *allargs
, char **env
,
1115 struct cleanup
*restore_personality
1116 = maybe_disable_address_space_randomization (disable_randomization
);
1118 /* The fork_child mechanism is synchronous and calls target_wait, so
1119 we have to mask the async mode. */
1121 /* Make sure we report all signals during startup. */
1122 linux_nat_pass_signals (ops
, 0, NULL
);
1124 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1126 do_cleanups (restore_personality
);
1129 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1130 already attached. Returns true if a new LWP is found, false
1134 attach_proc_task_lwp_callback (ptid_t ptid
)
1136 struct lwp_info
*lp
;
1138 /* Ignore LWPs we're already attached to. */
1139 lp
= find_lwp_pid (ptid
);
1142 int lwpid
= ptid_get_lwp (ptid
);
1144 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) < 0)
1148 /* Be quiet if we simply raced with the thread exiting.
1149 EPERM is returned if the thread's task still exists, and
1150 is marked as exited or zombie, as well as other
1151 conditions, so in that case, confirm the status in
1152 /proc/PID/status. */
1154 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
1156 if (debug_linux_nat
)
1158 fprintf_unfiltered (gdb_stdlog
,
1159 "Cannot attach to lwp %d: "
1160 "thread is gone (%d: %s)\n",
1161 lwpid
, err
, safe_strerror (err
));
1166 warning (_("Cannot attach to lwp %d: %s"),
1168 linux_ptrace_attach_fail_reason_string (ptid
,
1174 if (debug_linux_nat
)
1175 fprintf_unfiltered (gdb_stdlog
,
1176 "PTRACE_ATTACH %s, 0, 0 (OK)\n",
1177 target_pid_to_str (ptid
));
1179 lp
= add_lwp (ptid
);
1182 /* The next time we wait for this LWP we'll see a SIGSTOP as
1183 PTRACE_ATTACH brings it to a halt. */
1186 /* We need to wait for a stop before being able to make the
1187 next ptrace call on this LWP. */
1188 lp
->must_set_ptrace_flags
= 1;
1197 linux_nat_attach (struct target_ops
*ops
, const char *args
, int from_tty
)
1199 struct lwp_info
*lp
;
1203 /* Make sure we report all signals during attach. */
1204 linux_nat_pass_signals (ops
, 0, NULL
);
1208 linux_ops
->to_attach (ops
, args
, from_tty
);
1210 CATCH (ex
, RETURN_MASK_ERROR
)
1212 pid_t pid
= parse_pid_to_attach (args
);
1213 struct buffer buffer
;
1214 char *message
, *buffer_s
;
1216 message
= xstrdup (ex
.message
);
1217 make_cleanup (xfree
, message
);
1219 buffer_init (&buffer
);
1220 linux_ptrace_attach_fail_reason (pid
, &buffer
);
1222 buffer_grow_str0 (&buffer
, "");
1223 buffer_s
= buffer_finish (&buffer
);
1224 make_cleanup (xfree
, buffer_s
);
1226 if (*buffer_s
!= '\0')
1227 throw_error (ex
.error
, "warning: %s\n%s", buffer_s
, message
);
1229 throw_error (ex
.error
, "%s", message
);
1233 /* The ptrace base target adds the main thread with (pid,0,0)
1234 format. Decorate it with lwp info. */
1235 ptid
= ptid_build (ptid_get_pid (inferior_ptid
),
1236 ptid_get_pid (inferior_ptid
),
1238 thread_change_ptid (inferior_ptid
, ptid
);
1240 /* Add the initial process as the first LWP to the list. */
1241 lp
= add_initial_lwp (ptid
);
1243 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1245 if (!WIFSTOPPED (status
))
1247 if (WIFEXITED (status
))
1249 int exit_code
= WEXITSTATUS (status
);
1251 target_terminal_ours ();
1252 target_mourn_inferior ();
1254 error (_("Unable to attach: program exited normally."));
1256 error (_("Unable to attach: program exited with code %d."),
1259 else if (WIFSIGNALED (status
))
1261 enum gdb_signal signo
;
1263 target_terminal_ours ();
1264 target_mourn_inferior ();
1266 signo
= gdb_signal_from_host (WTERMSIG (status
));
1267 error (_("Unable to attach: program terminated with signal "
1269 gdb_signal_to_name (signo
),
1270 gdb_signal_to_string (signo
));
1273 internal_error (__FILE__
, __LINE__
,
1274 _("unexpected status %d for PID %ld"),
1275 status
, (long) ptid_get_lwp (ptid
));
1280 /* Save the wait status to report later. */
1282 if (debug_linux_nat
)
1283 fprintf_unfiltered (gdb_stdlog
,
1284 "LNA: waitpid %ld, saving status %s\n",
1285 (long) ptid_get_pid (lp
->ptid
), status_to_str (status
));
1287 lp
->status
= status
;
1289 /* We must attach to every LWP. If /proc is mounted, use that to
1290 find them now. The inferior may be using raw clone instead of
1291 using pthreads. But even if it is using pthreads, thread_db
1292 walks structures in the inferior's address space to find the list
1293 of threads/LWPs, and those structures may well be corrupted.
1294 Note that once thread_db is loaded, we'll still use it to list
1295 threads and associate pthread info with each LWP. */
1296 linux_proc_attach_tgid_threads (ptid_get_pid (lp
->ptid
),
1297 attach_proc_task_lwp_callback
);
1299 if (target_can_async_p ())
1300 target_async (inferior_event_handler
, 0);
1303 /* Get pending status of LP. */
1305 get_pending_status (struct lwp_info
*lp
, int *status
)
1307 enum gdb_signal signo
= GDB_SIGNAL_0
;
1309 /* If we paused threads momentarily, we may have stored pending
1310 events in lp->status or lp->waitstatus (see stop_wait_callback),
1311 and GDB core hasn't seen any signal for those threads.
1312 Otherwise, the last signal reported to the core is found in the
1313 thread object's stop_signal.
1315 There's a corner case that isn't handled here at present. Only
1316 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1317 stop_signal make sense as a real signal to pass to the inferior.
1318 Some catchpoint related events, like
1319 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1320 to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1321 those traps are debug API (ptrace in our case) related and
1322 induced; the inferior wouldn't see them if it wasn't being
1323 traced. Hence, we should never pass them to the inferior, even
1324 when set to pass state. Since this corner case isn't handled by
1325 infrun.c when proceeding with a signal, for consistency, neither
1326 do we handle it here (or elsewhere in the file we check for
1327 signal pass state). Normally SIGTRAP isn't set to pass state, so
1328 this is really a corner case. */
1330 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
1331 signo
= GDB_SIGNAL_0
; /* a pending ptrace event, not a real signal. */
1332 else if (lp
->status
)
1333 signo
= gdb_signal_from_host (WSTOPSIG (lp
->status
));
1334 else if (non_stop
&& !is_executing (lp
->ptid
))
1336 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1338 signo
= tp
->suspend
.stop_signal
;
1342 struct target_waitstatus last
;
1345 get_last_target_status (&last_ptid
, &last
);
1347 if (ptid_get_lwp (lp
->ptid
) == ptid_get_lwp (last_ptid
))
1349 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1351 signo
= tp
->suspend
.stop_signal
;
1357 if (signo
== GDB_SIGNAL_0
)
1359 if (debug_linux_nat
)
1360 fprintf_unfiltered (gdb_stdlog
,
1361 "GPT: lwp %s has no pending signal\n",
1362 target_pid_to_str (lp
->ptid
));
1364 else if (!signal_pass_state (signo
))
1366 if (debug_linux_nat
)
1367 fprintf_unfiltered (gdb_stdlog
,
1368 "GPT: lwp %s had signal %s, "
1369 "but it is in no pass state\n",
1370 target_pid_to_str (lp
->ptid
),
1371 gdb_signal_to_string (signo
));
1375 *status
= W_STOPCODE (gdb_signal_to_host (signo
));
1377 if (debug_linux_nat
)
1378 fprintf_unfiltered (gdb_stdlog
,
1379 "GPT: lwp %s has pending signal %s\n",
1380 target_pid_to_str (lp
->ptid
),
1381 gdb_signal_to_string (signo
));
1388 detach_callback (struct lwp_info
*lp
, void *data
)
1390 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1392 if (debug_linux_nat
&& lp
->status
)
1393 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1394 strsignal (WSTOPSIG (lp
->status
)),
1395 target_pid_to_str (lp
->ptid
));
1397 /* If there is a pending SIGSTOP, get rid of it. */
1400 if (debug_linux_nat
)
1401 fprintf_unfiltered (gdb_stdlog
,
1402 "DC: Sending SIGCONT to %s\n",
1403 target_pid_to_str (lp
->ptid
));
1405 kill_lwp (ptid_get_lwp (lp
->ptid
), SIGCONT
);
1409 /* We don't actually detach from the LWP that has an id equal to the
1410 overall process id just yet. */
1411 if (ptid_get_lwp (lp
->ptid
) != ptid_get_pid (lp
->ptid
))
1415 /* Pass on any pending signal for this LWP. */
1416 get_pending_status (lp
, &status
);
1418 if (linux_nat_prepare_to_resume
!= NULL
)
1419 linux_nat_prepare_to_resume (lp
);
1421 if (ptrace (PTRACE_DETACH
, ptid_get_lwp (lp
->ptid
), 0,
1422 WSTOPSIG (status
)) < 0)
1423 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1424 safe_strerror (errno
));
1426 if (debug_linux_nat
)
1427 fprintf_unfiltered (gdb_stdlog
,
1428 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1429 target_pid_to_str (lp
->ptid
),
1430 strsignal (WSTOPSIG (status
)));
1432 delete_lwp (lp
->ptid
);
1439 linux_nat_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
1443 struct lwp_info
*main_lwp
;
1445 pid
= ptid_get_pid (inferior_ptid
);
1447 /* Don't unregister from the event loop, as there may be other
1448 inferiors running. */
1450 /* Stop all threads before detaching. ptrace requires that the
1451 thread is stopped to sucessfully detach. */
1452 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1453 /* ... and wait until all of them have reported back that
1454 they're no longer running. */
1455 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1457 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1459 /* Only the initial process should be left right now. */
1460 gdb_assert (num_lwps (ptid_get_pid (inferior_ptid
)) == 1);
1462 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1464 /* Pass on any pending signal for the last LWP. */
1465 if ((args
== NULL
|| *args
== '\0')
1466 && get_pending_status (main_lwp
, &status
) != -1
1467 && WIFSTOPPED (status
))
1471 /* Put the signal number in ARGS so that inf_ptrace_detach will
1472 pass it along with PTRACE_DETACH. */
1474 xsnprintf (tem
, 8, "%d", (int) WSTOPSIG (status
));
1476 if (debug_linux_nat
)
1477 fprintf_unfiltered (gdb_stdlog
,
1478 "LND: Sending signal %s to %s\n",
1480 target_pid_to_str (main_lwp
->ptid
));
1483 if (linux_nat_prepare_to_resume
!= NULL
)
1484 linux_nat_prepare_to_resume (main_lwp
);
1485 delete_lwp (main_lwp
->ptid
);
1487 if (forks_exist_p ())
1489 /* Multi-fork case. The current inferior_ptid is being detached
1490 from, but there are other viable forks to debug. Detach from
1491 the current fork, and context-switch to the first
1493 linux_fork_detach (args
, from_tty
);
1496 linux_ops
->to_detach (ops
, args
, from_tty
);
1499 /* Resume execution of the inferior process. If STEP is nonzero,
1500 single-step it. If SIGNAL is nonzero, give it that signal. */
1503 linux_resume_one_lwp_throw (struct lwp_info
*lp
, int step
,
1504 enum gdb_signal signo
)
1508 /* stop_pc doubles as the PC the LWP had when it was last resumed.
1509 We only presently need that if the LWP is stepped though (to
1510 handle the case of stepping a breakpoint instruction). */
1513 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
1515 lp
->stop_pc
= regcache_read_pc (regcache
);
1520 if (linux_nat_prepare_to_resume
!= NULL
)
1521 linux_nat_prepare_to_resume (lp
);
1522 linux_ops
->to_resume (linux_ops
, lp
->ptid
, step
, signo
);
1524 /* Successfully resumed. Clear state that no longer makes sense,
1525 and mark the LWP as running. Must not do this before resuming
1526 otherwise if that fails other code will be confused. E.g., we'd
1527 later try to stop the LWP and hang forever waiting for a stop
1528 status. Note that we must not throw after this is cleared,
1529 otherwise handle_zombie_lwp_error would get confused. */
1531 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1532 registers_changed_ptid (lp
->ptid
);
1535 /* Called when we try to resume a stopped LWP and that errors out. If
1536 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
1537 or about to become), discard the error, clear any pending status
1538 the LWP may have, and return true (we'll collect the exit status
1539 soon enough). Otherwise, return false. */
1542 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
1544 /* If we get an error after resuming the LWP successfully, we'd
1545 confuse !T state for the LWP being gone. */
1546 gdb_assert (lp
->stopped
);
1548 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
1549 because even if ptrace failed with ESRCH, the tracee may be "not
1550 yet fully dead", but already refusing ptrace requests. In that
1551 case the tracee has 'R (Running)' state for a little bit
1552 (observed in Linux 3.18). See also the note on ESRCH in the
1553 ptrace(2) man page. Instead, check whether the LWP has any state
1554 other than ptrace-stopped. */
1556 /* Don't assume anything if /proc/PID/status can't be read. */
1557 if (linux_proc_pid_is_trace_stopped_nowarn (ptid_get_lwp (lp
->ptid
)) == 0)
1559 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1561 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1567 /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
1568 disappears while we try to resume it. */
1571 linux_resume_one_lwp (struct lwp_info
*lp
, int step
, enum gdb_signal signo
)
1575 linux_resume_one_lwp_throw (lp
, step
, signo
);
1577 CATCH (ex
, RETURN_MASK_ERROR
)
1579 if (!check_ptrace_stopped_lwp_gone (lp
))
1580 throw_exception (ex
);
1588 resume_lwp (struct lwp_info
*lp
, int step
, enum gdb_signal signo
)
1592 struct inferior
*inf
= find_inferior_ptid (lp
->ptid
);
1594 if (inf
->vfork_child
!= NULL
)
1596 if (debug_linux_nat
)
1597 fprintf_unfiltered (gdb_stdlog
,
1598 "RC: Not resuming %s (vfork parent)\n",
1599 target_pid_to_str (lp
->ptid
));
1601 else if (!lwp_status_pending_p (lp
))
1603 if (debug_linux_nat
)
1604 fprintf_unfiltered (gdb_stdlog
,
1605 "RC: Resuming sibling %s, %s, %s\n",
1606 target_pid_to_str (lp
->ptid
),
1607 (signo
!= GDB_SIGNAL_0
1608 ? strsignal (gdb_signal_to_host (signo
))
1610 step
? "step" : "resume");
1612 linux_resume_one_lwp (lp
, step
, signo
);
1616 if (debug_linux_nat
)
1617 fprintf_unfiltered (gdb_stdlog
,
1618 "RC: Not resuming sibling %s (has pending)\n",
1619 target_pid_to_str (lp
->ptid
));
1624 if (debug_linux_nat
)
1625 fprintf_unfiltered (gdb_stdlog
,
1626 "RC: Not resuming sibling %s (not stopped)\n",
1627 target_pid_to_str (lp
->ptid
));
1631 /* Callback for iterate_over_lwps. If LWP is EXCEPT, do nothing.
1632 Resume LWP with the last stop signal, if it is in pass state. */
1635 linux_nat_resume_callback (struct lwp_info
*lp
, void *except
)
1637 enum gdb_signal signo
= GDB_SIGNAL_0
;
1644 struct thread_info
*thread
;
1646 thread
= find_thread_ptid (lp
->ptid
);
1649 signo
= thread
->suspend
.stop_signal
;
1650 thread
->suspend
.stop_signal
= GDB_SIGNAL_0
;
1654 resume_lwp (lp
, 0, signo
);
1659 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1662 lp
->last_resume_kind
= resume_stop
;
1667 resume_set_callback (struct lwp_info
*lp
, void *data
)
1670 lp
->last_resume_kind
= resume_continue
;
1675 linux_nat_resume (struct target_ops
*ops
,
1676 ptid_t ptid
, int step
, enum gdb_signal signo
)
1678 struct lwp_info
*lp
;
1681 if (debug_linux_nat
)
1682 fprintf_unfiltered (gdb_stdlog
,
1683 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1684 step
? "step" : "resume",
1685 target_pid_to_str (ptid
),
1686 (signo
!= GDB_SIGNAL_0
1687 ? strsignal (gdb_signal_to_host (signo
)) : "0"),
1688 target_pid_to_str (inferior_ptid
));
1690 /* A specific PTID means `step only this process id'. */
1691 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
1692 || ptid_is_pid (ptid
));
1694 /* Mark the lwps we're resuming as resumed. */
1695 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
1697 /* See if it's the current inferior that should be handled
1700 lp
= find_lwp_pid (inferior_ptid
);
1702 lp
= find_lwp_pid (ptid
);
1703 gdb_assert (lp
!= NULL
);
1705 /* Remember if we're stepping. */
1706 lp
->last_resume_kind
= step
? resume_step
: resume_continue
;
1708 /* If we have a pending wait status for this thread, there is no
1709 point in resuming the process. But first make sure that
1710 linux_nat_wait won't preemptively handle the event - we
1711 should never take this short-circuit if we are going to
1712 leave LP running, since we have skipped resuming all the
1713 other threads. This bit of code needs to be synchronized
1714 with linux_nat_wait. */
1716 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1719 && WSTOPSIG (lp
->status
)
1720 && sigismember (&pass_mask
, WSTOPSIG (lp
->status
)))
1722 if (debug_linux_nat
)
1723 fprintf_unfiltered (gdb_stdlog
,
1724 "LLR: Not short circuiting for ignored "
1725 "status 0x%x\n", lp
->status
);
1727 /* FIXME: What should we do if we are supposed to continue
1728 this thread with a signal? */
1729 gdb_assert (signo
== GDB_SIGNAL_0
);
1730 signo
= gdb_signal_from_host (WSTOPSIG (lp
->status
));
1735 if (lwp_status_pending_p (lp
))
1737 /* FIXME: What should we do if we are supposed to continue
1738 this thread with a signal? */
1739 gdb_assert (signo
== GDB_SIGNAL_0
);
1741 if (debug_linux_nat
)
1742 fprintf_unfiltered (gdb_stdlog
,
1743 "LLR: Short circuiting for status 0x%x\n",
1746 if (target_can_async_p ())
1748 target_async (inferior_event_handler
, 0);
1749 /* Tell the event loop we have something to process. */
1756 iterate_over_lwps (ptid
, linux_nat_resume_callback
, lp
);
1758 linux_resume_one_lwp (lp
, step
, signo
);
1760 if (debug_linux_nat
)
1761 fprintf_unfiltered (gdb_stdlog
,
1762 "LLR: %s %s, %s (resume event thread)\n",
1763 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1764 target_pid_to_str (ptid
),
1765 (signo
!= GDB_SIGNAL_0
1766 ? strsignal (gdb_signal_to_host (signo
)) : "0"));
1768 if (target_can_async_p ())
1769 target_async (inferior_event_handler
, 0);
1772 /* Send a signal to an LWP. */
1775 kill_lwp (int lwpid
, int signo
)
1777 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1778 fails, then we are not using nptl threads and we should be using kill. */
1780 #ifdef HAVE_TKILL_SYSCALL
1782 static int tkill_failed
;
1789 ret
= syscall (__NR_tkill
, lwpid
, signo
);
1790 if (errno
!= ENOSYS
)
1797 return kill (lwpid
, signo
);
1800 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
1801 event, check if the core is interested in it: if not, ignore the
1802 event, and keep waiting; otherwise, we need to toggle the LWP's
1803 syscall entry/exit status, since the ptrace event itself doesn't
1804 indicate it, and report the trap to higher layers. */
1807 linux_handle_syscall_trap (struct lwp_info
*lp
, int stopping
)
1809 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1810 struct gdbarch
*gdbarch
= target_thread_architecture (lp
->ptid
);
1811 int syscall_number
= (int) gdbarch_get_syscall_number (gdbarch
, lp
->ptid
);
1815 /* If we're stopping threads, there's a SIGSTOP pending, which
1816 makes it so that the LWP reports an immediate syscall return,
1817 followed by the SIGSTOP. Skip seeing that "return" using
1818 PTRACE_CONT directly, and let stop_wait_callback collect the
1819 SIGSTOP. Later when the thread is resumed, a new syscall
1820 entry event. If we didn't do this (and returned 0), we'd
1821 leave a syscall entry pending, and our caller, by using
1822 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
1823 itself. Later, when the user re-resumes this LWP, we'd see
1824 another syscall entry event and we'd mistake it for a return.
1826 If stop_wait_callback didn't force the SIGSTOP out of the LWP
1827 (leaving immediately with LWP->signalled set, without issuing
1828 a PTRACE_CONT), it would still be problematic to leave this
1829 syscall enter pending, as later when the thread is resumed,
1830 it would then see the same syscall exit mentioned above,
1831 followed by the delayed SIGSTOP, while the syscall didn't
1832 actually get to execute. It seems it would be even more
1833 confusing to the user. */
1835 if (debug_linux_nat
)
1836 fprintf_unfiltered (gdb_stdlog
,
1837 "LHST: ignoring syscall %d "
1838 "for LWP %ld (stopping threads), "
1839 "resuming with PTRACE_CONT for SIGSTOP\n",
1841 ptid_get_lwp (lp
->ptid
));
1843 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
1844 ptrace (PTRACE_CONT
, ptid_get_lwp (lp
->ptid
), 0, 0);
1849 if (catch_syscall_enabled ())
1851 /* Always update the entry/return state, even if this particular
1852 syscall isn't interesting to the core now. In async mode,
1853 the user could install a new catchpoint for this syscall
1854 between syscall enter/return, and we'll need to know to
1855 report a syscall return if that happens. */
1856 lp
->syscall_state
= (lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1857 ? TARGET_WAITKIND_SYSCALL_RETURN
1858 : TARGET_WAITKIND_SYSCALL_ENTRY
);
1860 if (catching_syscall_number (syscall_number
))
1862 /* Alright, an event to report. */
1863 ourstatus
->kind
= lp
->syscall_state
;
1864 ourstatus
->value
.syscall_number
= syscall_number
;
1866 if (debug_linux_nat
)
1867 fprintf_unfiltered (gdb_stdlog
,
1868 "LHST: stopping for %s of syscall %d"
1871 == TARGET_WAITKIND_SYSCALL_ENTRY
1872 ? "entry" : "return",
1874 ptid_get_lwp (lp
->ptid
));
1878 if (debug_linux_nat
)
1879 fprintf_unfiltered (gdb_stdlog
,
1880 "LHST: ignoring %s of syscall %d "
1882 lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1883 ? "entry" : "return",
1885 ptid_get_lwp (lp
->ptid
));
1889 /* If we had been syscall tracing, and hence used PT_SYSCALL
1890 before on this LWP, it could happen that the user removes all
1891 syscall catchpoints before we get to process this event.
1892 There are two noteworthy issues here:
1894 - When stopped at a syscall entry event, resuming with
1895 PT_STEP still resumes executing the syscall and reports a
1898 - Only PT_SYSCALL catches syscall enters. If we last
1899 single-stepped this thread, then this event can't be a
1900 syscall enter. If we last single-stepped this thread, this
1901 has to be a syscall exit.
1903 The points above mean that the next resume, be it PT_STEP or
1904 PT_CONTINUE, can not trigger a syscall trace event. */
1905 if (debug_linux_nat
)
1906 fprintf_unfiltered (gdb_stdlog
,
1907 "LHST: caught syscall event "
1908 "with no syscall catchpoints."
1909 " %d for LWP %ld, ignoring\n",
1911 ptid_get_lwp (lp
->ptid
));
1912 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
1915 /* The core isn't interested in this event. For efficiency, avoid
1916 stopping all threads only to have the core resume them all again.
1917 Since we're not stopping threads, if we're still syscall tracing
1918 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
1919 subsequent syscall. Simply resume using the inf-ptrace layer,
1920 which knows when to use PT_SYSCALL or PT_CONTINUE. */
1922 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
1926 /* Handle a GNU/Linux extended wait response. If we see a clone
1927 event, we need to add the new LWP to our list (and not report the
1928 trap to higher layers). This function returns non-zero if the
1929 event should be ignored and we should wait again. If STOPPING is
1930 true, the new LWP remains stopped, otherwise it is continued. */
1933 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
1936 int pid
= ptid_get_lwp (lp
->ptid
);
1937 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1938 int event
= linux_ptrace_get_extended_event (status
);
1940 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1941 || event
== PTRACE_EVENT_CLONE
)
1943 unsigned long new_pid
;
1946 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1948 /* If we haven't already seen the new PID stop, wait for it now. */
1949 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1951 /* The new child has a pending SIGSTOP. We can't affect it until it
1952 hits the SIGSTOP, but we're already attached. */
1953 ret
= my_waitpid (new_pid
, &status
,
1954 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
1956 perror_with_name (_("waiting for new child"));
1957 else if (ret
!= new_pid
)
1958 internal_error (__FILE__
, __LINE__
,
1959 _("wait returned unexpected PID %d"), ret
);
1960 else if (!WIFSTOPPED (status
))
1961 internal_error (__FILE__
, __LINE__
,
1962 _("wait returned unexpected status 0x%x"), status
);
1965 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
1967 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
1969 /* The arch-specific native code may need to know about new
1970 forks even if those end up never mapped to an
1972 if (linux_nat_new_fork
!= NULL
)
1973 linux_nat_new_fork (lp
, new_pid
);
1976 if (event
== PTRACE_EVENT_FORK
1977 && linux_fork_checkpointing_p (ptid_get_pid (lp
->ptid
)))
1979 /* Handle checkpointing by linux-fork.c here as a special
1980 case. We don't want the follow-fork-mode or 'catch fork'
1981 to interfere with this. */
1983 /* This won't actually modify the breakpoint list, but will
1984 physically remove the breakpoints from the child. */
1985 detach_breakpoints (ptid_build (new_pid
, new_pid
, 0));
1987 /* Retain child fork in ptrace (stopped) state. */
1988 if (!find_fork_pid (new_pid
))
1991 /* Report as spurious, so that infrun doesn't want to follow
1992 this fork. We're actually doing an infcall in
1994 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1996 /* Report the stop to the core. */
2000 if (event
== PTRACE_EVENT_FORK
)
2001 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
2002 else if (event
== PTRACE_EVENT_VFORK
)
2003 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
2006 struct lwp_info
*new_lp
;
2008 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2010 if (debug_linux_nat
)
2011 fprintf_unfiltered (gdb_stdlog
,
2012 "LHEW: Got clone event "
2013 "from LWP %d, new child is LWP %ld\n",
2016 new_lp
= add_lwp (ptid_build (ptid_get_pid (lp
->ptid
), new_pid
, 0));
2018 new_lp
->stopped
= 1;
2020 if (WSTOPSIG (status
) != SIGSTOP
)
2022 /* This can happen if someone starts sending signals to
2023 the new thread before it gets a chance to run, which
2024 have a lower number than SIGSTOP (e.g. SIGUSR1).
2025 This is an unlikely case, and harder to handle for
2026 fork / vfork than for clone, so we do not try - but
2027 we handle it for clone events here. We'll send
2028 the other signal on to the thread below. */
2030 new_lp
->signalled
= 1;
2034 struct thread_info
*tp
;
2036 /* When we stop for an event in some other thread, and
2037 pull the thread list just as this thread has cloned,
2038 we'll have seen the new thread in the thread_db list
2039 before handling the CLONE event (glibc's
2040 pthread_create adds the new thread to the thread list
2041 before clone'ing, and has the kernel fill in the
2042 thread's tid on the clone call with
2043 CLONE_PARENT_SETTID). If that happened, and the core
2044 had requested the new thread to stop, we'll have
2045 killed it with SIGSTOP. But since SIGSTOP is not an
2046 RT signal, it can only be queued once. We need to be
2047 careful to not resume the LWP if we wanted it to
2048 stop. In that case, we'll leave the SIGSTOP pending.
2049 It will later be reported as GDB_SIGNAL_0. */
2050 tp
= find_thread_ptid (new_lp
->ptid
);
2051 if (tp
!= NULL
&& tp
->stop_requested
)
2052 new_lp
->last_resume_kind
= resume_stop
;
2057 /* If the thread_db layer is active, let it record the user
2058 level thread id and status, and add the thread to GDB's
2060 if (!thread_db_notice_clone (lp
->ptid
, new_lp
->ptid
))
2062 /* The process is not using thread_db. Add the LWP to
2064 target_post_attach (ptid_get_lwp (new_lp
->ptid
));
2065 add_thread (new_lp
->ptid
);
2070 set_running (new_lp
->ptid
, 1);
2071 set_executing (new_lp
->ptid
, 1);
2072 /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
2074 new_lp
->last_resume_kind
= resume_continue
;
2079 /* We created NEW_LP so it cannot yet contain STATUS. */
2080 gdb_assert (new_lp
->status
== 0);
2082 /* Save the wait status to report later. */
2083 if (debug_linux_nat
)
2084 fprintf_unfiltered (gdb_stdlog
,
2085 "LHEW: waitpid of new LWP %ld, "
2086 "saving status %s\n",
2087 (long) ptid_get_lwp (new_lp
->ptid
),
2088 status_to_str (status
));
2089 new_lp
->status
= status
;
2092 new_lp
->resumed
= !stopping
;
2099 if (event
== PTRACE_EVENT_EXEC
)
2101 if (debug_linux_nat
)
2102 fprintf_unfiltered (gdb_stdlog
,
2103 "LHEW: Got exec event from LWP %ld\n",
2104 ptid_get_lwp (lp
->ptid
));
2106 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
2107 ourstatus
->value
.execd_pathname
2108 = xstrdup (linux_child_pid_to_exec_file (NULL
, pid
));
2110 /* The thread that execed must have been resumed, but, when a
2111 thread execs, it changes its tid to the tgid, and the old
2112 tgid thread might have not been resumed. */
2117 if (event
== PTRACE_EVENT_VFORK_DONE
)
2119 if (current_inferior ()->waiting_for_vfork_done
)
2121 if (debug_linux_nat
)
2122 fprintf_unfiltered (gdb_stdlog
,
2123 "LHEW: Got expected PTRACE_EVENT_"
2124 "VFORK_DONE from LWP %ld: stopping\n",
2125 ptid_get_lwp (lp
->ptid
));
2127 ourstatus
->kind
= TARGET_WAITKIND_VFORK_DONE
;
2131 if (debug_linux_nat
)
2132 fprintf_unfiltered (gdb_stdlog
,
2133 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2134 "from LWP %ld: ignoring\n",
2135 ptid_get_lwp (lp
->ptid
));
2139 internal_error (__FILE__
, __LINE__
,
2140 _("unknown ptrace event %d"), event
);
2143 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2147 wait_lwp (struct lwp_info
*lp
)
2151 int thread_dead
= 0;
2154 gdb_assert (!lp
->stopped
);
2155 gdb_assert (lp
->status
== 0);
2157 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2158 block_child_signals (&prev_mask
);
2162 /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2163 was right and we should just call sigsuspend. */
2165 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), &status
, WNOHANG
);
2166 if (pid
== -1 && errno
== ECHILD
)
2167 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), &status
, __WCLONE
| WNOHANG
);
2168 if (pid
== -1 && errno
== ECHILD
)
2170 /* The thread has previously exited. We need to delete it
2171 now because, for some vendor 2.4 kernels with NPTL
2172 support backported, there won't be an exit event unless
2173 it is the main thread. 2.6 kernels will report an exit
2174 event for each thread that exits, as expected. */
2176 if (debug_linux_nat
)
2177 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2178 target_pid_to_str (lp
->ptid
));
2183 /* Bugs 10970, 12702.
2184 Thread group leader may have exited in which case we'll lock up in
2185 waitpid if there are other threads, even if they are all zombies too.
2186 Basically, we're not supposed to use waitpid this way.
2187 __WCLONE is not applicable for the leader so we can't use that.
2188 LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2189 process; it gets ESRCH both for the zombie and for running processes.
2191 As a workaround, check if we're waiting for the thread group leader and
2192 if it's a zombie, and avoid calling waitpid if it is.
2194 This is racy, what if the tgl becomes a zombie right after we check?
2195 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2196 waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
2198 if (ptid_get_pid (lp
->ptid
) == ptid_get_lwp (lp
->ptid
)
2199 && linux_proc_pid_is_zombie (ptid_get_lwp (lp
->ptid
)))
2202 if (debug_linux_nat
)
2203 fprintf_unfiltered (gdb_stdlog
,
2204 "WL: Thread group leader %s vanished.\n",
2205 target_pid_to_str (lp
->ptid
));
2209 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2210 get invoked despite our caller had them intentionally blocked by
2211 block_child_signals. This is sensitive only to the loop of
2212 linux_nat_wait_1 and there if we get called my_waitpid gets called
2213 again before it gets to sigsuspend so we can safely let the handlers
2214 get executed here. */
2216 if (debug_linux_nat
)
2217 fprintf_unfiltered (gdb_stdlog
, "WL: about to sigsuspend\n");
2218 sigsuspend (&suspend_mask
);
2221 restore_child_signals_mask (&prev_mask
);
2225 gdb_assert (pid
== ptid_get_lwp (lp
->ptid
));
2227 if (debug_linux_nat
)
2229 fprintf_unfiltered (gdb_stdlog
,
2230 "WL: waitpid %s received %s\n",
2231 target_pid_to_str (lp
->ptid
),
2232 status_to_str (status
));
2235 /* Check if the thread has exited. */
2236 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2239 if (debug_linux_nat
)
2240 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2241 target_pid_to_str (lp
->ptid
));
2251 gdb_assert (WIFSTOPPED (status
));
2254 if (lp
->must_set_ptrace_flags
)
2256 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
2258 linux_enable_event_reporting (ptid_get_lwp (lp
->ptid
), inf
->attach_flag
);
2259 lp
->must_set_ptrace_flags
= 0;
2262 /* Handle GNU/Linux's syscall SIGTRAPs. */
2263 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2265 /* No longer need the sysgood bit. The ptrace event ends up
2266 recorded in lp->waitstatus if we care for it. We can carry
2267 on handling the event like a regular SIGTRAP from here
2269 status
= W_STOPCODE (SIGTRAP
);
2270 if (linux_handle_syscall_trap (lp
, 1))
2271 return wait_lwp (lp
);
2274 /* Handle GNU/Linux's extended waitstatus for trace events. */
2275 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
2276 && linux_is_extended_waitstatus (status
))
2278 if (debug_linux_nat
)
2279 fprintf_unfiltered (gdb_stdlog
,
2280 "WL: Handling extended status 0x%06x\n",
2282 linux_handle_extended_wait (lp
, status
, 1);
2289 /* Send a SIGSTOP to LP. */
2292 stop_callback (struct lwp_info
*lp
, void *data
)
2294 if (!lp
->stopped
&& !lp
->signalled
)
2298 if (debug_linux_nat
)
2300 fprintf_unfiltered (gdb_stdlog
,
2301 "SC: kill %s **<SIGSTOP>**\n",
2302 target_pid_to_str (lp
->ptid
));
2305 ret
= kill_lwp (ptid_get_lwp (lp
->ptid
), SIGSTOP
);
2306 if (debug_linux_nat
)
2308 fprintf_unfiltered (gdb_stdlog
,
2309 "SC: lwp kill %d %s\n",
2311 errno
? safe_strerror (errno
) : "ERRNO-OK");
2315 gdb_assert (lp
->status
== 0);
2321 /* Request a stop on LWP. */
2324 linux_stop_lwp (struct lwp_info
*lwp
)
2326 stop_callback (lwp
, NULL
);
2329 /* See linux-nat.h */
2332 linux_stop_and_wait_all_lwps (void)
2334 /* Stop all LWP's ... */
2335 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
2337 /* ... and wait until all of them have reported back that
2338 they're no longer running. */
2339 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
2342 /* See linux-nat.h */
2345 linux_unstop_all_lwps (void)
2347 iterate_over_lwps (minus_one_ptid
,
2348 resume_stopped_resumed_lwps
, &minus_one_ptid
);
2351 /* Return non-zero if LWP PID has a pending SIGINT. */
2354 linux_nat_has_pending_sigint (int pid
)
2356 sigset_t pending
, blocked
, ignored
;
2358 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2360 if (sigismember (&pending
, SIGINT
)
2361 && !sigismember (&ignored
, SIGINT
))
2367 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2370 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2372 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2373 flag to consume the next one. */
2374 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2375 && WSTOPSIG (lp
->status
) == SIGINT
)
2378 lp
->ignore_sigint
= 1;
2383 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2384 This function is called after we know the LWP has stopped; if the LWP
2385 stopped before the expected SIGINT was delivered, then it will never have
2386 arrived. Also, if the signal was delivered to a shared queue and consumed
2387 by a different thread, it will never be delivered to this LWP. */
2390 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2392 if (!lp
->ignore_sigint
)
2395 if (!linux_nat_has_pending_sigint (ptid_get_lwp (lp
->ptid
)))
2397 if (debug_linux_nat
)
2398 fprintf_unfiltered (gdb_stdlog
,
2399 "MCIS: Clearing bogus flag for %s\n",
2400 target_pid_to_str (lp
->ptid
));
2401 lp
->ignore_sigint
= 0;
2405 /* Fetch the possible triggered data watchpoint info and store it in
2408 On some archs, like x86, that use debug registers to set
2409 watchpoints, it's possible that the way to know which watched
2410 address trapped, is to check the register that is used to select
2411 which address to watch. Problem is, between setting the watchpoint
2412 and reading back which data address trapped, the user may change
2413 the set of watchpoints, and, as a consequence, GDB changes the
2414 debug registers in the inferior. To avoid reading back a stale
2415 stopped-data-address when that happens, we cache in LP the fact
2416 that a watchpoint trapped, and the corresponding data address, as
2417 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2418 registers meanwhile, we have the cached data we can rely on. */
2421 check_stopped_by_watchpoint (struct lwp_info
*lp
)
2423 struct cleanup
*old_chain
;
2425 if (linux_ops
->to_stopped_by_watchpoint
== NULL
)
2428 old_chain
= save_inferior_ptid ();
2429 inferior_ptid
= lp
->ptid
;
2431 if (linux_ops
->to_stopped_by_watchpoint (linux_ops
))
2433 lp
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2435 if (linux_ops
->to_stopped_data_address
!= NULL
)
2436 lp
->stopped_data_address_p
=
2437 linux_ops
->to_stopped_data_address (¤t_target
,
2438 &lp
->stopped_data_address
);
2440 lp
->stopped_data_address_p
= 0;
2443 do_cleanups (old_chain
);
2445 return lp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2448 /* Called when the LWP stopped for a trap that could be explained by a
2449 watchpoint or a breakpoint. */
2452 save_sigtrap (struct lwp_info
*lp
)
2454 gdb_assert (lp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
);
2455 gdb_assert (lp
->status
!= 0);
2457 /* Check first if this was a SW/HW breakpoint before checking
2458 watchpoints, because at least s390 can't tell the data address of
2459 hardware watchpoint hits, and the kernel returns
2460 stopped-by-watchpoint as long as there's a watchpoint set. */
2461 if (linux_nat_status_is_event (lp
->status
))
2462 check_stopped_by_breakpoint (lp
);
2464 /* Note that TRAP_HWBKPT can indicate either a hardware breakpoint
2465 or hardware watchpoint. Check which is which if we got
2466 TARGET_STOPPED_BY_HW_BREAKPOINT. */
2467 if (lp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
2468 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
2469 check_stopped_by_watchpoint (lp
);
2472 /* Returns true if the LWP had stopped for a watchpoint. */
2475 linux_nat_stopped_by_watchpoint (struct target_ops
*ops
)
2477 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2479 gdb_assert (lp
!= NULL
);
2481 return lp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2485 linux_nat_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
2487 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2489 gdb_assert (lp
!= NULL
);
2491 *addr_p
= lp
->stopped_data_address
;
2493 return lp
->stopped_data_address_p
;
2496 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2499 sigtrap_is_event (int status
)
2501 return WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
;
2504 /* Set alternative SIGTRAP-like events recognizer. If
2505 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2509 linux_nat_set_status_is_event (struct target_ops
*t
,
2510 int (*status_is_event
) (int status
))
2512 linux_nat_status_is_event
= status_is_event
;
2515 /* Wait until LP is stopped. */
2518 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2520 struct inferior
*inf
= find_inferior_ptid (lp
->ptid
);
2522 /* If this is a vfork parent, bail out, it is not going to report
2523 any SIGSTOP until the vfork is done with. */
2524 if (inf
->vfork_child
!= NULL
)
2531 status
= wait_lwp (lp
);
2535 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2536 && WSTOPSIG (status
) == SIGINT
)
2538 lp
->ignore_sigint
= 0;
2541 ptrace (PTRACE_CONT
, ptid_get_lwp (lp
->ptid
), 0, 0);
2543 if (debug_linux_nat
)
2544 fprintf_unfiltered (gdb_stdlog
,
2545 "PTRACE_CONT %s, 0, 0 (%s) "
2546 "(discarding SIGINT)\n",
2547 target_pid_to_str (lp
->ptid
),
2548 errno
? safe_strerror (errno
) : "OK");
2550 return stop_wait_callback (lp
, NULL
);
2553 maybe_clear_ignore_sigint (lp
);
2555 if (WSTOPSIG (status
) != SIGSTOP
)
2557 /* The thread was stopped with a signal other than SIGSTOP. */
2559 if (debug_linux_nat
)
2560 fprintf_unfiltered (gdb_stdlog
,
2561 "SWC: Pending event %s in %s\n",
2562 status_to_str ((int) status
),
2563 target_pid_to_str (lp
->ptid
));
2565 /* Save the sigtrap event. */
2566 lp
->status
= status
;
2567 gdb_assert (lp
->signalled
);
2572 /* We caught the SIGSTOP that we intended to catch, so
2573 there's no SIGSTOP pending. */
2575 if (debug_linux_nat
)
2576 fprintf_unfiltered (gdb_stdlog
,
2577 "SWC: Delayed SIGSTOP caught for %s.\n",
2578 target_pid_to_str (lp
->ptid
));
2580 /* Reset SIGNALLED only after the stop_wait_callback call
2581 above as it does gdb_assert on SIGNALLED. */
2589 /* Return non-zero if LP has a wait status pending. Discard the
2590 pending event and resume the LWP if the event that originally
2591 caused the stop became uninteresting. */
2594 status_callback (struct lwp_info
*lp
, void *data
)
2596 /* Only report a pending wait status if we pretend that this has
2597 indeed been resumed. */
2601 if (!lwp_status_pending_p (lp
))
2604 if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
2605 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
2607 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2608 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2612 pc
= regcache_read_pc (regcache
);
2614 if (pc
!= lp
->stop_pc
)
2616 if (debug_linux_nat
)
2617 fprintf_unfiltered (gdb_stdlog
,
2618 "SC: PC of %s changed. was=%s, now=%s\n",
2619 target_pid_to_str (lp
->ptid
),
2620 paddress (target_gdbarch (), lp
->stop_pc
),
2621 paddress (target_gdbarch (), pc
));
2625 #if !USE_SIGTRAP_SIGINFO
2626 else if (!breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
2628 if (debug_linux_nat
)
2629 fprintf_unfiltered (gdb_stdlog
,
2630 "SC: previous breakpoint of %s, at %s gone\n",
2631 target_pid_to_str (lp
->ptid
),
2632 paddress (target_gdbarch (), lp
->stop_pc
));
2640 if (debug_linux_nat
)
2641 fprintf_unfiltered (gdb_stdlog
,
2642 "SC: pending event of %s cancelled.\n",
2643 target_pid_to_str (lp
->ptid
));
2646 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
2654 /* Return non-zero if LP isn't stopped. */
2657 running_callback (struct lwp_info
*lp
, void *data
)
2659 return (!lp
->stopped
2660 || (lwp_status_pending_p (lp
) && lp
->resumed
));
2663 /* Count the LWP's that have had events. */
2666 count_events_callback (struct lwp_info
*lp
, void *data
)
2670 gdb_assert (count
!= NULL
);
2672 /* Select only resumed LWPs that have an event pending. */
2673 if (lp
->resumed
&& lwp_status_pending_p (lp
))
2679 /* Select the LWP (if any) that is currently being single-stepped. */
2682 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2684 if (lp
->last_resume_kind
== resume_step
2691 /* Returns true if LP has a status pending. */
2694 lwp_status_pending_p (struct lwp_info
*lp
)
2696 /* We check for lp->waitstatus in addition to lp->status, because we
2697 can have pending process exits recorded in lp->status and
2698 W_EXITCODE(0,0) happens to be 0. */
2699 return lp
->status
!= 0 || lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
;
2702 /* Select the Nth LWP that has had an event. */
2705 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2707 int *selector
= data
;
2709 gdb_assert (selector
!= NULL
);
2711 /* Select only resumed LWPs that have an event pending. */
2712 if (lp
->resumed
&& lwp_status_pending_p (lp
))
2713 if ((*selector
)-- == 0)
2719 /* Called when the LWP got a signal/trap that could be explained by a
2720 software or hardware breakpoint. */
2723 check_stopped_by_breakpoint (struct lwp_info
*lp
)
2725 /* Arrange for a breakpoint to be hit again later. We don't keep
2726 the SIGTRAP status and don't forward the SIGTRAP signal to the
2727 LWP. We will handle the current event, eventually we will resume
2728 this LWP, and this breakpoint will trap again.
2730 If we do not do this, then we run the risk that the user will
2731 delete or disable the breakpoint, but the LWP will have already
2734 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2735 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2738 #if USE_SIGTRAP_SIGINFO
2742 pc
= regcache_read_pc (regcache
);
2743 sw_bp_pc
= pc
- gdbarch_decr_pc_after_break (gdbarch
);
2745 #if USE_SIGTRAP_SIGINFO
2746 if (linux_nat_get_siginfo (lp
->ptid
, &siginfo
))
2748 if (siginfo
.si_signo
== SIGTRAP
)
2750 if (siginfo
.si_code
== GDB_ARCH_TRAP_BRKPT
)
2752 if (debug_linux_nat
)
2753 fprintf_unfiltered (gdb_stdlog
,
2754 "CSBB: Push back software "
2755 "breakpoint for %s\n",
2756 target_pid_to_str (lp
->ptid
));
2758 /* Back up the PC if necessary. */
2760 regcache_write_pc (regcache
, sw_bp_pc
);
2762 lp
->stop_pc
= sw_bp_pc
;
2763 lp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
2766 else if (siginfo
.si_code
== TRAP_HWBKPT
)
2768 if (debug_linux_nat
)
2769 fprintf_unfiltered (gdb_stdlog
,
2770 "CSBB: Push back hardware "
2771 "breakpoint/watchpoint for %s\n",
2772 target_pid_to_str (lp
->ptid
));
2775 lp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
2781 if ((!lp
->step
|| lp
->stop_pc
== sw_bp_pc
)
2782 && software_breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
2785 /* The LWP was either continued, or stepped a software
2786 breakpoint instruction. */
2787 if (debug_linux_nat
)
2788 fprintf_unfiltered (gdb_stdlog
,
2789 "CB: Push back software breakpoint for %s\n",
2790 target_pid_to_str (lp
->ptid
));
2792 /* Back up the PC if necessary. */
2794 regcache_write_pc (regcache
, sw_bp_pc
);
2796 lp
->stop_pc
= sw_bp_pc
;
2797 lp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
2801 if (hardware_breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
2803 if (debug_linux_nat
)
2804 fprintf_unfiltered (gdb_stdlog
,
2805 "CB: Push back hardware breakpoint for %s\n",
2806 target_pid_to_str (lp
->ptid
));
2809 lp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
2818 /* Returns true if the LWP had stopped for a software breakpoint. */
2821 linux_nat_stopped_by_sw_breakpoint (struct target_ops
*ops
)
2823 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2825 gdb_assert (lp
!= NULL
);
2827 return lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
;
2830 /* Implement the supports_stopped_by_sw_breakpoint method. */
2833 linux_nat_supports_stopped_by_sw_breakpoint (struct target_ops
*ops
)
2835 return USE_SIGTRAP_SIGINFO
;
2838 /* Returns true if the LWP had stopped for a hardware
2839 breakpoint/watchpoint. */
2842 linux_nat_stopped_by_hw_breakpoint (struct target_ops
*ops
)
2844 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2846 gdb_assert (lp
!= NULL
);
2848 return lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
;
2851 /* Implement the supports_stopped_by_hw_breakpoint method. */
2854 linux_nat_supports_stopped_by_hw_breakpoint (struct target_ops
*ops
)
2856 return USE_SIGTRAP_SIGINFO
;
2859 /* Select one LWP out of those that have events pending. */
2862 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
2865 int random_selector
;
2866 struct lwp_info
*event_lp
= NULL
;
2868 /* Record the wait status for the original LWP. */
2869 (*orig_lp
)->status
= *status
;
2871 /* In all-stop, give preference to the LWP that is being
2872 single-stepped. There will be at most one, and it will be the
2873 LWP that the core is most interested in. If we didn't do this,
2874 then we'd have to handle pending step SIGTRAPs somehow in case
2875 the core later continues the previously-stepped thread, as
2876 otherwise we'd report the pending SIGTRAP then, and the core, not
2877 having stepped the thread, wouldn't understand what the trap was
2878 for, and therefore would report it to the user as a random
2882 event_lp
= iterate_over_lwps (filter
,
2883 select_singlestep_lwp_callback
, NULL
);
2884 if (event_lp
!= NULL
)
2886 if (debug_linux_nat
)
2887 fprintf_unfiltered (gdb_stdlog
,
2888 "SEL: Select single-step %s\n",
2889 target_pid_to_str (event_lp
->ptid
));
2893 if (event_lp
== NULL
)
2895 /* Pick one at random, out of those which have had events. */
2897 /* First see how many events we have. */
2898 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
2899 gdb_assert (num_events
> 0);
2901 /* Now randomly pick a LWP out of those that have had
2903 random_selector
= (int)
2904 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2906 if (debug_linux_nat
&& num_events
> 1)
2907 fprintf_unfiltered (gdb_stdlog
,
2908 "SEL: Found %d events, selecting #%d\n",
2909 num_events
, random_selector
);
2911 event_lp
= iterate_over_lwps (filter
,
2912 select_event_lwp_callback
,
2916 if (event_lp
!= NULL
)
2918 /* Switch the event LWP. */
2919 *orig_lp
= event_lp
;
2920 *status
= event_lp
->status
;
2923 /* Flush the wait status for the event LWP. */
2924 (*orig_lp
)->status
= 0;
2927 /* Return non-zero if LP has been resumed. */
2930 resumed_callback (struct lwp_info
*lp
, void *data
)
2935 /* Stop an active thread, verify it still exists, then resume it. If
2936 the thread ends up with a pending status, then it is not resumed,
2937 and *DATA (really a pointer to int), is set. */
2940 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2944 ptid_t ptid
= lp
->ptid
;
2946 stop_callback (lp
, NULL
);
2947 stop_wait_callback (lp
, NULL
);
2949 /* Resume if the lwp still exists, and the core wanted it
2951 lp
= find_lwp_pid (ptid
);
2954 if (lp
->last_resume_kind
== resume_stop
2955 && !lwp_status_pending_p (lp
))
2957 /* The core wanted the LWP to stop. Even if it stopped
2958 cleanly (with SIGSTOP), leave the event pending. */
2959 if (debug_linux_nat
)
2960 fprintf_unfiltered (gdb_stdlog
,
2961 "SARC: core wanted LWP %ld stopped "
2962 "(leaving SIGSTOP pending)\n",
2963 ptid_get_lwp (lp
->ptid
));
2964 lp
->status
= W_STOPCODE (SIGSTOP
);
2967 if (!lwp_status_pending_p (lp
))
2969 if (debug_linux_nat
)
2970 fprintf_unfiltered (gdb_stdlog
,
2971 "SARC: re-resuming LWP %ld\n",
2972 ptid_get_lwp (lp
->ptid
));
2973 resume_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
2977 if (debug_linux_nat
)
2978 fprintf_unfiltered (gdb_stdlog
,
2979 "SARC: not re-resuming LWP %ld "
2981 ptid_get_lwp (lp
->ptid
));
2988 /* Check if we should go on and pass this event to common code.
2989 Return the affected lwp if we are, or NULL otherwise. */
2991 static struct lwp_info
*
2992 linux_nat_filter_event (int lwpid
, int status
)
2994 struct lwp_info
*lp
;
2995 int event
= linux_ptrace_get_extended_event (status
);
2997 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2999 /* Check for stop events reported by a process we didn't already
3000 know about - anything not already in our LWP list.
3002 If we're expecting to receive stopped processes after
3003 fork, vfork, and clone events, then we'll just add the
3004 new one to our list and go back to waiting for the event
3005 to be reported - the stopped process might be returned
3006 from waitpid before or after the event is.
3008 But note the case of a non-leader thread exec'ing after the
3009 leader having exited, and gone from our lists. The non-leader
3010 thread changes its tid to the tgid. */
3012 if (WIFSTOPPED (status
) && lp
== NULL
3013 && (WSTOPSIG (status
) == SIGTRAP
&& event
== PTRACE_EVENT_EXEC
))
3015 /* A multi-thread exec after we had seen the leader exiting. */
3016 if (debug_linux_nat
)
3017 fprintf_unfiltered (gdb_stdlog
,
3018 "LLW: Re-adding thread group leader LWP %d.\n",
3021 lp
= add_lwp (ptid_build (lwpid
, lwpid
, 0));
3024 add_thread (lp
->ptid
);
3027 if (WIFSTOPPED (status
) && !lp
)
3029 if (debug_linux_nat
)
3030 fprintf_unfiltered (gdb_stdlog
,
3031 "LHEW: saving LWP %ld status %s in stopped_pids list\n",
3032 (long) lwpid
, status_to_str (status
));
3033 add_to_pid_list (&stopped_pids
, lwpid
, status
);
3037 /* Make sure we don't report an event for the exit of an LWP not in
3038 our list, i.e. not part of the current process. This can happen
3039 if we detach from a program we originally forked and then it
3041 if (!WIFSTOPPED (status
) && !lp
)
3044 /* This LWP is stopped now. (And if dead, this prevents it from
3045 ever being continued.) */
3048 if (WIFSTOPPED (status
) && lp
->must_set_ptrace_flags
)
3050 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
3052 linux_enable_event_reporting (ptid_get_lwp (lp
->ptid
), inf
->attach_flag
);
3053 lp
->must_set_ptrace_flags
= 0;
3056 /* Handle GNU/Linux's syscall SIGTRAPs. */
3057 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
3059 /* No longer need the sysgood bit. The ptrace event ends up
3060 recorded in lp->waitstatus if we care for it. We can carry
3061 on handling the event like a regular SIGTRAP from here
3063 status
= W_STOPCODE (SIGTRAP
);
3064 if (linux_handle_syscall_trap (lp
, 0))
3068 /* Handle GNU/Linux's extended waitstatus for trace events. */
3069 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
3070 && linux_is_extended_waitstatus (status
))
3072 if (debug_linux_nat
)
3073 fprintf_unfiltered (gdb_stdlog
,
3074 "LLW: Handling extended status 0x%06x\n",
3076 if (linux_handle_extended_wait (lp
, status
, 0))
3080 /* Check if the thread has exited. */
3081 if (WIFEXITED (status
) || WIFSIGNALED (status
))
3083 if (num_lwps (ptid_get_pid (lp
->ptid
)) > 1)
3085 /* If this is the main thread, we must stop all threads and
3086 verify if they are still alive. This is because in the
3087 nptl thread model on Linux 2.4, there is no signal issued
3088 for exiting LWPs other than the main thread. We only get
3089 the main thread exit signal once all child threads have
3090 already exited. If we stop all the threads and use the
3091 stop_wait_callback to check if they have exited we can
3092 determine whether this signal should be ignored or
3093 whether it means the end of the debugged application,
3094 regardless of which threading model is being used. */
3095 if (ptid_get_pid (lp
->ptid
) == ptid_get_lwp (lp
->ptid
))
3097 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp
->ptid
)),
3098 stop_and_resume_callback
, NULL
);
3101 if (debug_linux_nat
)
3102 fprintf_unfiltered (gdb_stdlog
,
3103 "LLW: %s exited.\n",
3104 target_pid_to_str (lp
->ptid
));
3106 if (num_lwps (ptid_get_pid (lp
->ptid
)) > 1)
3108 /* If there is at least one more LWP, then the exit signal
3109 was not the end of the debugged application and should be
3116 gdb_assert (lp
->resumed
);
3118 if (debug_linux_nat
)
3119 fprintf_unfiltered (gdb_stdlog
,
3120 "Process %ld exited\n",
3121 ptid_get_lwp (lp
->ptid
));
3123 /* This was the last lwp in the process. Since events are
3124 serialized to GDB core, we may not be able report this one
3125 right now, but GDB core and the other target layers will want
3126 to be notified about the exit code/signal, leave the status
3127 pending for the next time we're able to report it. */
3129 /* Dead LWP's aren't expected to reported a pending sigstop. */
3132 /* Store the pending event in the waitstatus, because
3133 W_EXITCODE(0,0) == 0. */
3134 store_waitstatus (&lp
->waitstatus
, status
);
3138 /* Check if the current LWP has previously exited. In the nptl
3139 thread model, LWPs other than the main thread do not issue
3140 signals when they exit so we must check whenever the thread has
3141 stopped. A similar check is made in stop_wait_callback(). */
3142 if (num_lwps (ptid_get_pid (lp
->ptid
)) > 1 && !linux_thread_alive (lp
->ptid
))
3144 ptid_t ptid
= pid_to_ptid (ptid_get_pid (lp
->ptid
));
3146 if (debug_linux_nat
)
3147 fprintf_unfiltered (gdb_stdlog
,
3148 "LLW: %s exited.\n",
3149 target_pid_to_str (lp
->ptid
));
3153 /* Make sure there is at least one thread running. */
3154 gdb_assert (iterate_over_lwps (ptid
, running_callback
, NULL
));
3156 /* Discard the event. */
3160 /* Make sure we don't report a SIGSTOP that we sent ourselves in
3161 an attempt to stop an LWP. */
3163 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
3165 if (debug_linux_nat
)
3166 fprintf_unfiltered (gdb_stdlog
,
3167 "LLW: Delayed SIGSTOP caught for %s.\n",
3168 target_pid_to_str (lp
->ptid
));
3172 if (lp
->last_resume_kind
!= resume_stop
)
3174 /* This is a delayed SIGSTOP. */
3176 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
3177 if (debug_linux_nat
)
3178 fprintf_unfiltered (gdb_stdlog
,
3179 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
3181 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3182 target_pid_to_str (lp
->ptid
));
3184 gdb_assert (lp
->resumed
);
3186 /* Discard the event. */
3191 /* Make sure we don't report a SIGINT that we have already displayed
3192 for another thread. */
3193 if (lp
->ignore_sigint
3194 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
3196 if (debug_linux_nat
)
3197 fprintf_unfiltered (gdb_stdlog
,
3198 "LLW: Delayed SIGINT caught for %s.\n",
3199 target_pid_to_str (lp
->ptid
));
3201 /* This is a delayed SIGINT. */
3202 lp
->ignore_sigint
= 0;
3204 linux_resume_one_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
3205 if (debug_linux_nat
)
3206 fprintf_unfiltered (gdb_stdlog
,
3207 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
3209 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3210 target_pid_to_str (lp
->ptid
));
3211 gdb_assert (lp
->resumed
);
3213 /* Discard the event. */
3217 /* Don't report signals that GDB isn't interested in, such as
3218 signals that are neither printed nor stopped upon. Stopping all
3219 threads can be a bit time-consuming so if we want decent
3220 performance with heavily multi-threaded programs, especially when
3221 they're using a high frequency timer, we'd better avoid it if we
3223 if (WIFSTOPPED (status
))
3225 enum gdb_signal signo
= gdb_signal_from_host (WSTOPSIG (status
));
3229 /* Only do the below in all-stop, as we currently use SIGSTOP
3230 to implement target_stop (see linux_nat_stop) in
3232 if (signo
== GDB_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3234 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3235 forwarded to the entire process group, that is, all LWPs
3236 will receive it - unless they're using CLONE_THREAD to
3237 share signals. Since we only want to report it once, we
3238 mark it as ignored for all LWPs except this one. */
3239 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp
->ptid
)),
3240 set_ignore_sigint
, NULL
);
3241 lp
->ignore_sigint
= 0;
3244 maybe_clear_ignore_sigint (lp
);
3247 /* When using hardware single-step, we need to report every signal.
3248 Otherwise, signals in pass_mask may be short-circuited
3249 except signals that might be caused by a breakpoint. */
3251 && WSTOPSIG (status
) && sigismember (&pass_mask
, WSTOPSIG (status
))
3252 && !linux_wstatus_maybe_breakpoint (status
))
3254 linux_resume_one_lwp (lp
, lp
->step
, signo
);
3255 if (debug_linux_nat
)
3256 fprintf_unfiltered (gdb_stdlog
,
3257 "LLW: %s %s, %s (preempt 'handle')\n",
3259 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3260 target_pid_to_str (lp
->ptid
),
3261 (signo
!= GDB_SIGNAL_0
3262 ? strsignal (gdb_signal_to_host (signo
))
3268 /* An interesting event. */
3270 lp
->status
= status
;
3275 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3276 their exits until all other threads in the group have exited. */
3279 check_zombie_leaders (void)
3281 struct inferior
*inf
;
3285 struct lwp_info
*leader_lp
;
3290 leader_lp
= find_lwp_pid (pid_to_ptid (inf
->pid
));
3291 if (leader_lp
!= NULL
3292 /* Check if there are other threads in the group, as we may
3293 have raced with the inferior simply exiting. */
3294 && num_lwps (inf
->pid
) > 1
3295 && linux_proc_pid_is_zombie (inf
->pid
))
3297 if (debug_linux_nat
)
3298 fprintf_unfiltered (gdb_stdlog
,
3299 "CZL: Thread group leader %d zombie "
3300 "(it exited, or another thread execd).\n",
3303 /* A leader zombie can mean one of two things:
3305 - It exited, and there's an exit status pending
3306 available, or only the leader exited (not the whole
3307 program). In the latter case, we can't waitpid the
3308 leader's exit status until all other threads are gone.
3310 - There are 3 or more threads in the group, and a thread
3311 other than the leader exec'd. On an exec, the Linux
3312 kernel destroys all other threads (except the execing
3313 one) in the thread group, and resets the execing thread's
3314 tid to the tgid. No exit notification is sent for the
3315 execing thread -- from the ptracer's perspective, it
3316 appears as though the execing thread just vanishes.
3317 Until we reap all other threads except the leader and the
3318 execing thread, the leader will be zombie, and the
3319 execing thread will be in `D (disc sleep)'. As soon as
3320 all other threads are reaped, the execing thread changes
3321 it's tid to the tgid, and the previous (zombie) leader
3322 vanishes, giving place to the "new" leader. We could try
3323 distinguishing the exit and exec cases, by waiting once
3324 more, and seeing if something comes out, but it doesn't
3325 sound useful. The previous leader _does_ go away, and
3326 we'll re-add the new one once we see the exec event
3327 (which is just the same as what would happen if the
3328 previous leader did exit voluntarily before some other
3331 if (debug_linux_nat
)
3332 fprintf_unfiltered (gdb_stdlog
,
3333 "CZL: Thread group leader %d vanished.\n",
3335 exit_lwp (leader_lp
);
3341 linux_nat_wait_1 (struct target_ops
*ops
,
3342 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3346 enum resume_kind last_resume_kind
;
3347 struct lwp_info
*lp
;
3350 if (debug_linux_nat
)
3351 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
3353 /* The first time we get here after starting a new inferior, we may
3354 not have added it to the LWP list yet - this is the earliest
3355 moment at which we know its PID. */
3356 if (ptid_is_pid (inferior_ptid
))
3358 /* Upgrade the main thread's ptid. */
3359 thread_change_ptid (inferior_ptid
,
3360 ptid_build (ptid_get_pid (inferior_ptid
),
3361 ptid_get_pid (inferior_ptid
), 0));
3363 lp
= add_initial_lwp (inferior_ptid
);
3367 /* Make sure SIGCHLD is blocked until the sigsuspend below. */
3368 block_child_signals (&prev_mask
);
3370 /* First check if there is a LWP with a wait status pending. */
3371 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3374 if (debug_linux_nat
)
3375 fprintf_unfiltered (gdb_stdlog
,
3376 "LLW: Using pending wait status %s for %s.\n",
3377 status_to_str (lp
->status
),
3378 target_pid_to_str (lp
->ptid
));
3381 if (!target_is_async_p ())
3383 /* Causes SIGINT to be passed on to the attached process. */
3387 /* But if we don't find a pending event, we'll have to wait. Always
3388 pull all events out of the kernel. We'll randomly select an
3389 event LWP out of all that have events, to prevent starvation. */
3395 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3398 - If the thread group leader exits while other threads in the
3399 thread group still exist, waitpid(TGID, ...) hangs. That
3400 waitpid won't return an exit status until the other threads
3401 in the group are reapped.
3403 - When a non-leader thread execs, that thread just vanishes
3404 without reporting an exit (so we'd hang if we waited for it
3405 explicitly in that case). The exec event is reported to
3409 lwpid
= my_waitpid (-1, &status
, __WCLONE
| WNOHANG
);
3410 if (lwpid
== 0 || (lwpid
== -1 && errno
== ECHILD
))
3411 lwpid
= my_waitpid (-1, &status
, WNOHANG
);
3413 if (debug_linux_nat
)
3414 fprintf_unfiltered (gdb_stdlog
,
3415 "LNW: waitpid(-1, ...) returned %d, %s\n",
3416 lwpid
, errno
? safe_strerror (errno
) : "ERRNO-OK");
3420 if (debug_linux_nat
)
3422 fprintf_unfiltered (gdb_stdlog
,
3423 "LLW: waitpid %ld received %s\n",
3424 (long) lwpid
, status_to_str (status
));
3427 linux_nat_filter_event (lwpid
, status
);
3428 /* Retry until nothing comes out of waitpid. A single
3429 SIGCHLD can indicate more than one child stopped. */
3433 /* Now that we've pulled all events out of the kernel, resume
3434 LWPs that don't have an interesting event to report. */
3435 iterate_over_lwps (minus_one_ptid
,
3436 resume_stopped_resumed_lwps
, &minus_one_ptid
);
3438 /* ... and find an LWP with a status to report to the core, if
3440 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3444 /* Check for zombie thread group leaders. Those can't be reaped
3445 until all other threads in the thread group are. */
3446 check_zombie_leaders ();
3448 /* If there are no resumed children left, bail. We'd be stuck
3449 forever in the sigsuspend call below otherwise. */
3450 if (iterate_over_lwps (ptid
, resumed_callback
, NULL
) == NULL
)
3452 if (debug_linux_nat
)
3453 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (no resumed LWP)\n");
3455 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3457 if (!target_is_async_p ())
3458 clear_sigint_trap ();
3460 restore_child_signals_mask (&prev_mask
);
3461 return minus_one_ptid
;
3464 /* No interesting event to report to the core. */
3466 if (target_options
& TARGET_WNOHANG
)
3468 if (debug_linux_nat
)
3469 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
3471 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3472 restore_child_signals_mask (&prev_mask
);
3473 return minus_one_ptid
;
3476 /* We shouldn't end up here unless we want to try again. */
3477 gdb_assert (lp
== NULL
);
3479 /* Block until we get an event reported with SIGCHLD. */
3480 if (debug_linux_nat
)
3481 fprintf_unfiltered (gdb_stdlog
, "LNW: about to sigsuspend\n");
3482 sigsuspend (&suspend_mask
);
3485 if (!target_is_async_p ())
3486 clear_sigint_trap ();
3490 status
= lp
->status
;
3495 /* Now stop all other LWP's ... */
3496 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3498 /* ... and wait until all of them have reported back that
3499 they're no longer running. */
3500 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3503 /* If we're not waiting for a specific LWP, choose an event LWP from
3504 among those that have had events. Giving equal priority to all
3505 LWPs that have had events helps prevent starvation. */
3506 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3507 select_event_lwp (ptid
, &lp
, &status
);
3509 gdb_assert (lp
!= NULL
);
3511 /* Now that we've selected our final event LWP, un-adjust its PC if
3512 it was a software breakpoint, and we can't reliably support the
3513 "stopped by software breakpoint" stop reason. */
3514 if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3515 && !USE_SIGTRAP_SIGINFO
)
3517 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3518 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3519 int decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
3525 pc
= regcache_read_pc (regcache
);
3526 regcache_write_pc (regcache
, pc
+ decr_pc
);
3530 /* We'll need this to determine whether to report a SIGSTOP as
3531 GDB_SIGNAL_0. Need to take a copy because resume_clear_callback
3533 last_resume_kind
= lp
->last_resume_kind
;
3537 /* In all-stop, from the core's perspective, all LWPs are now
3538 stopped until a new resume action is sent over. */
3539 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
3543 resume_clear_callback (lp
, NULL
);
3546 if (linux_nat_status_is_event (status
))
3548 if (debug_linux_nat
)
3549 fprintf_unfiltered (gdb_stdlog
,
3550 "LLW: trap ptid is %s.\n",
3551 target_pid_to_str (lp
->ptid
));
3554 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3556 *ourstatus
= lp
->waitstatus
;
3557 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3560 store_waitstatus (ourstatus
, status
);
3562 if (debug_linux_nat
)
3563 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3565 restore_child_signals_mask (&prev_mask
);
3567 if (last_resume_kind
== resume_stop
3568 && ourstatus
->kind
== TARGET_WAITKIND_STOPPED
3569 && WSTOPSIG (status
) == SIGSTOP
)
3571 /* A thread that has been requested to stop by GDB with
3572 target_stop, and it stopped cleanly, so report as SIG0. The
3573 use of SIGSTOP is an implementation detail. */
3574 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3577 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
3578 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
3581 lp
->core
= linux_common_core_of_thread (lp
->ptid
);
3586 /* Resume LWPs that are currently stopped without any pending status
3587 to report, but are resumed from the core's perspective. */
3590 resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
)
3592 ptid_t
*wait_ptid_p
= data
;
3596 && !lwp_status_pending_p (lp
))
3598 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3599 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3603 CORE_ADDR pc
= regcache_read_pc (regcache
);
3604 int leave_stopped
= 0;
3606 /* Don't bother if there's a breakpoint at PC that we'd hit
3607 immediately, and we're not waiting for this LWP. */
3608 if (!ptid_match (lp
->ptid
, *wait_ptid_p
))
3610 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
3616 if (debug_linux_nat
)
3617 fprintf_unfiltered (gdb_stdlog
,
3618 "RSRL: resuming stopped-resumed LWP %s at "
3620 target_pid_to_str (lp
->ptid
),
3621 paddress (gdbarch
, pc
),
3624 linux_resume_one_lwp_throw (lp
, lp
->step
, GDB_SIGNAL_0
);
3627 CATCH (ex
, RETURN_MASK_ERROR
)
3629 if (!check_ptrace_stopped_lwp_gone (lp
))
3630 throw_exception (ex
);
3639 linux_nat_wait (struct target_ops
*ops
,
3640 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3645 if (debug_linux_nat
)
3647 char *options_string
;
3649 options_string
= target_options_to_string (target_options
);
3650 fprintf_unfiltered (gdb_stdlog
,
3651 "linux_nat_wait: [%s], [%s]\n",
3652 target_pid_to_str (ptid
),
3654 xfree (options_string
);
3657 /* Flush the async file first. */
3658 if (target_is_async_p ())
3659 async_file_flush ();
3661 /* Resume LWPs that are currently stopped without any pending status
3662 to report, but are resumed from the core's perspective. LWPs get
3663 in this state if we find them stopping at a time we're not
3664 interested in reporting the event (target_wait on a
3665 specific_process, for example, see linux_nat_wait_1), and
3666 meanwhile the event became uninteresting. Don't bother resuming
3667 LWPs we're not going to wait for if they'd stop immediately. */
3669 iterate_over_lwps (minus_one_ptid
, resume_stopped_resumed_lwps
, &ptid
);
3671 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
, target_options
);
3673 /* If we requested any event, and something came out, assume there
3674 may be more. If we requested a specific lwp or process, also
3675 assume there may be more. */
3676 if (target_is_async_p ()
3677 && ((ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
3678 && ourstatus
->kind
!= TARGET_WAITKIND_NO_RESUMED
)
3679 || !ptid_equal (ptid
, minus_one_ptid
)))
3686 kill_callback (struct lwp_info
*lp
, void *data
)
3688 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
3691 kill_lwp (ptid_get_lwp (lp
->ptid
), SIGKILL
);
3692 if (debug_linux_nat
)
3694 int save_errno
= errno
;
3696 fprintf_unfiltered (gdb_stdlog
,
3697 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
3698 target_pid_to_str (lp
->ptid
),
3699 save_errno
? safe_strerror (save_errno
) : "OK");
3702 /* Some kernels ignore even SIGKILL for processes under ptrace. */
3705 ptrace (PTRACE_KILL
, ptid_get_lwp (lp
->ptid
), 0, 0);
3706 if (debug_linux_nat
)
3708 int save_errno
= errno
;
3710 fprintf_unfiltered (gdb_stdlog
,
3711 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3712 target_pid_to_str (lp
->ptid
),
3713 save_errno
? safe_strerror (save_errno
) : "OK");
3720 kill_wait_callback (struct lwp_info
*lp
, void *data
)
3724 /* We must make sure that there are no pending events (delayed
3725 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3726 program doesn't interfere with any following debugging session. */
3728 /* For cloned processes we must check both with __WCLONE and
3729 without, since the exit status of a cloned process isn't reported
3735 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), NULL
, __WCLONE
);
3736 if (pid
!= (pid_t
) -1)
3738 if (debug_linux_nat
)
3739 fprintf_unfiltered (gdb_stdlog
,
3740 "KWC: wait %s received unknown.\n",
3741 target_pid_to_str (lp
->ptid
));
3742 /* The Linux kernel sometimes fails to kill a thread
3743 completely after PTRACE_KILL; that goes from the stop
3744 point in do_fork out to the one in
3745 get_signal_to_deliever and waits again. So kill it
3747 kill_callback (lp
, NULL
);
3750 while (pid
== ptid_get_lwp (lp
->ptid
));
3752 gdb_assert (pid
== -1 && errno
== ECHILD
);
3757 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), NULL
, 0);
3758 if (pid
!= (pid_t
) -1)
3760 if (debug_linux_nat
)
3761 fprintf_unfiltered (gdb_stdlog
,
3762 "KWC: wait %s received unk.\n",
3763 target_pid_to_str (lp
->ptid
));
3764 /* See the call to kill_callback above. */
3765 kill_callback (lp
, NULL
);
3768 while (pid
== ptid_get_lwp (lp
->ptid
));
3770 gdb_assert (pid
== -1 && errno
== ECHILD
);
3775 linux_nat_kill (struct target_ops
*ops
)
3777 struct target_waitstatus last
;
3781 /* If we're stopped while forking and we haven't followed yet,
3782 kill the other task. We need to do this first because the
3783 parent will be sleeping if this is a vfork. */
3785 get_last_target_status (&last_ptid
, &last
);
3787 if (last
.kind
== TARGET_WAITKIND_FORKED
3788 || last
.kind
== TARGET_WAITKIND_VFORKED
)
3790 ptrace (PT_KILL
, ptid_get_pid (last
.value
.related_pid
), 0, 0);
3793 /* Let the arch-specific native code know this process is
3795 linux_nat_forget_process (ptid_get_pid (last
.value
.related_pid
));
3798 if (forks_exist_p ())
3799 linux_fork_killall ();
3802 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3804 /* Stop all threads before killing them, since ptrace requires
3805 that the thread is stopped to sucessfully PTRACE_KILL. */
3806 iterate_over_lwps (ptid
, stop_callback
, NULL
);
3807 /* ... and wait until all of them have reported back that
3808 they're no longer running. */
3809 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
3811 /* Kill all LWP's ... */
3812 iterate_over_lwps (ptid
, kill_callback
, NULL
);
3814 /* ... and wait until we've flushed all events. */
3815 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
3818 target_mourn_inferior ();
3822 linux_nat_mourn_inferior (struct target_ops
*ops
)
3824 int pid
= ptid_get_pid (inferior_ptid
);
3826 purge_lwp_list (pid
);
3828 if (! forks_exist_p ())
3829 /* Normal case, no other forks available. */
3830 linux_ops
->to_mourn_inferior (ops
);
3832 /* Multi-fork case. The current inferior_ptid has exited, but
3833 there are other viable forks to debug. Delete the exiting
3834 one and context-switch to the first available. */
3835 linux_fork_mourn_inferior ();
3837 /* Let the arch-specific native code know this process is gone. */
3838 linux_nat_forget_process (pid
);
3841 /* Convert a native/host siginfo object, into/from the siginfo in the
3842 layout of the inferiors' architecture. */
3845 siginfo_fixup (siginfo_t
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
3849 if (linux_nat_siginfo_fixup
!= NULL
)
3850 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
3852 /* If there was no callback, or the callback didn't do anything,
3853 then just do a straight memcpy. */
3857 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
3859 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
3863 static enum target_xfer_status
3864 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
3865 const char *annex
, gdb_byte
*readbuf
,
3866 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
3867 ULONGEST
*xfered_len
)
3871 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
3873 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
3874 gdb_assert (readbuf
|| writebuf
);
3876 pid
= ptid_get_lwp (inferior_ptid
);
3878 pid
= ptid_get_pid (inferior_ptid
);
3880 if (offset
> sizeof (siginfo
))
3881 return TARGET_XFER_E_IO
;
3884 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3886 return TARGET_XFER_E_IO
;
3888 /* When GDB is built as a 64-bit application, ptrace writes into
3889 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3890 inferior with a 64-bit GDB should look the same as debugging it
3891 with a 32-bit GDB, we need to convert it. GDB core always sees
3892 the converted layout, so any read/write will have to be done
3894 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
3896 if (offset
+ len
> sizeof (siginfo
))
3897 len
= sizeof (siginfo
) - offset
;
3899 if (readbuf
!= NULL
)
3900 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
3903 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
3905 /* Convert back to ptrace layout before flushing it out. */
3906 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
3909 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3911 return TARGET_XFER_E_IO
;
3915 return TARGET_XFER_OK
;
3918 static enum target_xfer_status
3919 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3920 const char *annex
, gdb_byte
*readbuf
,
3921 const gdb_byte
*writebuf
,
3922 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
3924 struct cleanup
*old_chain
;
3925 enum target_xfer_status xfer
;
3927 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
3928 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
3929 offset
, len
, xfered_len
);
3931 /* The target is connected but no live inferior is selected. Pass
3932 this request down to a lower stratum (e.g., the executable
3934 if (object
== TARGET_OBJECT_MEMORY
&& ptid_equal (inferior_ptid
, null_ptid
))
3935 return TARGET_XFER_EOF
;
3937 old_chain
= save_inferior_ptid ();
3939 if (ptid_lwp_p (inferior_ptid
))
3940 inferior_ptid
= pid_to_ptid (ptid_get_lwp (inferior_ptid
));
3942 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3943 offset
, len
, xfered_len
);
3945 do_cleanups (old_chain
);
3950 linux_thread_alive (ptid_t ptid
)
3954 gdb_assert (ptid_lwp_p (ptid
));
3956 /* Send signal 0 instead of anything ptrace, because ptracing a
3957 running thread errors out claiming that the thread doesn't
3959 err
= kill_lwp (ptid_get_lwp (ptid
), 0);
3961 if (debug_linux_nat
)
3962 fprintf_unfiltered (gdb_stdlog
,
3963 "LLTA: KILL(SIG0) %s (%s)\n",
3964 target_pid_to_str (ptid
),
3965 err
? safe_strerror (tmp_errno
) : "OK");
3974 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
3976 return linux_thread_alive (ptid
);
3980 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3982 static char buf
[64];
3984 if (ptid_lwp_p (ptid
)
3985 && (ptid_get_pid (ptid
) != ptid_get_lwp (ptid
)
3986 || num_lwps (ptid_get_pid (ptid
)) > 1))
3988 snprintf (buf
, sizeof (buf
), "LWP %ld", ptid_get_lwp (ptid
));
3992 return normal_pid_to_str (ptid
);
3996 linux_nat_thread_name (struct target_ops
*self
, struct thread_info
*thr
)
3998 int pid
= ptid_get_pid (thr
->ptid
);
3999 long lwp
= ptid_get_lwp (thr
->ptid
);
4000 #define FORMAT "/proc/%d/task/%ld/comm"
4001 char buf
[sizeof (FORMAT
) + 30];
4003 char *result
= NULL
;
4005 snprintf (buf
, sizeof (buf
), FORMAT
, pid
, lwp
);
4006 comm_file
= gdb_fopen_cloexec (buf
, "r");
4009 /* Not exported by the kernel, so we define it here. */
4011 static char line
[COMM_LEN
+ 1];
4013 if (fgets (line
, sizeof (line
), comm_file
))
4015 char *nl
= strchr (line
, '\n');
4032 /* Accepts an integer PID; Returns a string representing a file that
4033 can be opened to get the symbols for the child process. */
4036 linux_child_pid_to_exec_file (struct target_ops
*self
, int pid
)
4038 static char buf
[PATH_MAX
];
4039 char name
[PATH_MAX
];
4041 xsnprintf (name
, PATH_MAX
, "/proc/%d/exe", pid
);
4042 memset (buf
, 0, PATH_MAX
);
4043 if (readlink (name
, buf
, PATH_MAX
- 1) <= 0)
4049 /* Implement the to_xfer_partial interface for memory reads using the /proc
4050 filesystem. Because we can use a single read() call for /proc, this
4051 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4052 but it doesn't support writes. */
4054 static enum target_xfer_status
4055 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4056 const char *annex
, gdb_byte
*readbuf
,
4057 const gdb_byte
*writebuf
,
4058 ULONGEST offset
, LONGEST len
, ULONGEST
*xfered_len
)
4064 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
4067 /* Don't bother for one word. */
4068 if (len
< 3 * sizeof (long))
4069 return TARGET_XFER_EOF
;
4071 /* We could keep this file open and cache it - possibly one per
4072 thread. That requires some juggling, but is even faster. */
4073 xsnprintf (filename
, sizeof filename
, "/proc/%d/mem",
4074 ptid_get_pid (inferior_ptid
));
4075 fd
= gdb_open_cloexec (filename
, O_RDONLY
| O_LARGEFILE
, 0);
4077 return TARGET_XFER_EOF
;
4079 /* If pread64 is available, use it. It's faster if the kernel
4080 supports it (only one syscall), and it's 64-bit safe even on
4081 32-bit platforms (for instance, SPARC debugging a SPARC64
4084 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
4086 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
4095 return TARGET_XFER_EOF
;
4099 return TARGET_XFER_OK
;
4104 /* Enumerate spufs IDs for process PID. */
4106 spu_enumerate_spu_ids (int pid
, gdb_byte
*buf
, ULONGEST offset
, ULONGEST len
)
4108 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
4110 LONGEST written
= 0;
4113 struct dirent
*entry
;
4115 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
4116 dir
= opendir (path
);
4121 while ((entry
= readdir (dir
)) != NULL
)
4127 fd
= atoi (entry
->d_name
);
4131 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
4132 if (stat (path
, &st
) != 0)
4134 if (!S_ISDIR (st
.st_mode
))
4137 if (statfs (path
, &stfs
) != 0)
4139 if (stfs
.f_type
!= SPUFS_MAGIC
)
4142 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
4144 store_unsigned_integer (buf
+ pos
- offset
, 4, byte_order
, fd
);
4154 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4155 object type, using the /proc file system. */
4157 static enum target_xfer_status
4158 linux_proc_xfer_spu (struct target_ops
*ops
, enum target_object object
,
4159 const char *annex
, gdb_byte
*readbuf
,
4160 const gdb_byte
*writebuf
,
4161 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
4166 int pid
= ptid_get_pid (inferior_ptid
);
4171 return TARGET_XFER_E_IO
;
4174 LONGEST l
= spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
4177 return TARGET_XFER_E_IO
;
4179 return TARGET_XFER_EOF
;
4182 *xfered_len
= (ULONGEST
) l
;
4183 return TARGET_XFER_OK
;
4188 xsnprintf (buf
, sizeof buf
, "/proc/%d/fd/%s", pid
, annex
);
4189 fd
= gdb_open_cloexec (buf
, writebuf
? O_WRONLY
: O_RDONLY
, 0);
4191 return TARGET_XFER_E_IO
;
4194 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4197 return TARGET_XFER_EOF
;
4201 ret
= write (fd
, writebuf
, (size_t) len
);
4203 ret
= read (fd
, readbuf
, (size_t) len
);
4208 return TARGET_XFER_E_IO
;
4210 return TARGET_XFER_EOF
;
4213 *xfered_len
= (ULONGEST
) ret
;
4214 return TARGET_XFER_OK
;
4219 /* Parse LINE as a signal set and add its set bits to SIGS. */
4222 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4224 int len
= strlen (line
) - 1;
4228 if (line
[len
] != '\n')
4229 error (_("Could not parse signal set: %s"), line
);
4237 if (*p
>= '0' && *p
<= '9')
4239 else if (*p
>= 'a' && *p
<= 'f')
4240 digit
= *p
- 'a' + 10;
4242 error (_("Could not parse signal set: %s"), line
);
4247 sigaddset (sigs
, signum
+ 1);
4249 sigaddset (sigs
, signum
+ 2);
4251 sigaddset (sigs
, signum
+ 3);
4253 sigaddset (sigs
, signum
+ 4);
4259 /* Find process PID's pending signals from /proc/pid/status and set
4263 linux_proc_pending_signals (int pid
, sigset_t
*pending
,
4264 sigset_t
*blocked
, sigset_t
*ignored
)
4267 char buffer
[PATH_MAX
], fname
[PATH_MAX
];
4268 struct cleanup
*cleanup
;
4270 sigemptyset (pending
);
4271 sigemptyset (blocked
);
4272 sigemptyset (ignored
);
4273 xsnprintf (fname
, sizeof fname
, "/proc/%d/status", pid
);
4274 procfile
= gdb_fopen_cloexec (fname
, "r");
4275 if (procfile
== NULL
)
4276 error (_("Could not open %s"), fname
);
4277 cleanup
= make_cleanup_fclose (procfile
);
4279 while (fgets (buffer
, PATH_MAX
, procfile
) != NULL
)
4281 /* Normal queued signals are on the SigPnd line in the status
4282 file. However, 2.6 kernels also have a "shared" pending
4283 queue for delivering signals to a thread group, so check for
4286 Unfortunately some Red Hat kernels include the shared pending
4287 queue but not the ShdPnd status field. */
4289 if (startswith (buffer
, "SigPnd:\t"))
4290 add_line_to_sigset (buffer
+ 8, pending
);
4291 else if (startswith (buffer
, "ShdPnd:\t"))
4292 add_line_to_sigset (buffer
+ 8, pending
);
4293 else if (startswith (buffer
, "SigBlk:\t"))
4294 add_line_to_sigset (buffer
+ 8, blocked
);
4295 else if (startswith (buffer
, "SigIgn:\t"))
4296 add_line_to_sigset (buffer
+ 8, ignored
);
4299 do_cleanups (cleanup
);
4302 static enum target_xfer_status
4303 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4304 const char *annex
, gdb_byte
*readbuf
,
4305 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
4306 ULONGEST
*xfered_len
)
4308 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4310 *xfered_len
= linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4311 if (*xfered_len
== 0)
4312 return TARGET_XFER_EOF
;
4314 return TARGET_XFER_OK
;
4317 static enum target_xfer_status
4318 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4319 const char *annex
, gdb_byte
*readbuf
,
4320 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
4321 ULONGEST
*xfered_len
)
4323 enum target_xfer_status xfer
;
4325 if (object
== TARGET_OBJECT_AUXV
)
4326 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4327 offset
, len
, xfered_len
);
4329 if (object
== TARGET_OBJECT_OSDATA
)
4330 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4331 offset
, len
, xfered_len
);
4333 if (object
== TARGET_OBJECT_SPU
)
4334 return linux_proc_xfer_spu (ops
, object
, annex
, readbuf
, writebuf
,
4335 offset
, len
, xfered_len
);
4337 /* GDB calculates all the addresses in possibly larget width of the address.
4338 Address width needs to be masked before its final use - either by
4339 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4341 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4343 if (object
== TARGET_OBJECT_MEMORY
)
4345 int addr_bit
= gdbarch_addr_bit (target_gdbarch ());
4347 if (addr_bit
< (sizeof (ULONGEST
) * HOST_CHAR_BIT
))
4348 offset
&= ((ULONGEST
) 1 << addr_bit
) - 1;
4351 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4352 offset
, len
, xfered_len
);
4353 if (xfer
!= TARGET_XFER_EOF
)
4356 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4357 offset
, len
, xfered_len
);
4361 cleanup_target_stop (void *arg
)
4363 ptid_t
*ptid
= (ptid_t
*) arg
;
4365 gdb_assert (arg
!= NULL
);
4368 target_resume (*ptid
, 0, GDB_SIGNAL_0
);
4371 static VEC(static_tracepoint_marker_p
) *
4372 linux_child_static_tracepoint_markers_by_strid (struct target_ops
*self
,
4375 char s
[IPA_CMD_BUF_SIZE
];
4376 struct cleanup
*old_chain
;
4377 int pid
= ptid_get_pid (inferior_ptid
);
4378 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
4379 struct static_tracepoint_marker
*marker
= NULL
;
4381 ptid_t ptid
= ptid_build (pid
, 0, 0);
4386 memcpy (s
, "qTfSTM", sizeof ("qTfSTM"));
4387 s
[sizeof ("qTfSTM")] = 0;
4389 agent_run_command (pid
, s
, strlen (s
) + 1);
4391 old_chain
= make_cleanup (free_current_marker
, &marker
);
4392 make_cleanup (cleanup_target_stop
, &ptid
);
4397 marker
= XCNEW (struct static_tracepoint_marker
);
4401 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
4403 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
4405 VEC_safe_push (static_tracepoint_marker_p
,
4411 release_static_tracepoint_marker (marker
);
4412 memset (marker
, 0, sizeof (*marker
));
4415 while (*p
++ == ','); /* comma-separated list */
4417 memcpy (s
, "qTsSTM", sizeof ("qTsSTM"));
4418 s
[sizeof ("qTsSTM")] = 0;
4419 agent_run_command (pid
, s
, strlen (s
) + 1);
4423 do_cleanups (old_chain
);
4428 /* Create a prototype generic GNU/Linux target. The client can override
4429 it with local methods. */
4432 linux_target_install_ops (struct target_ops
*t
)
4434 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4435 t
->to_remove_fork_catchpoint
= linux_child_remove_fork_catchpoint
;
4436 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4437 t
->to_remove_vfork_catchpoint
= linux_child_remove_vfork_catchpoint
;
4438 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4439 t
->to_remove_exec_catchpoint
= linux_child_remove_exec_catchpoint
;
4440 t
->to_set_syscall_catchpoint
= linux_child_set_syscall_catchpoint
;
4441 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4442 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4443 t
->to_post_attach
= linux_child_post_attach
;
4444 t
->to_follow_fork
= linux_child_follow_fork
;
4446 super_xfer_partial
= t
->to_xfer_partial
;
4447 t
->to_xfer_partial
= linux_xfer_partial
;
4449 t
->to_static_tracepoint_markers_by_strid
4450 = linux_child_static_tracepoint_markers_by_strid
;
4456 struct target_ops
*t
;
4458 t
= inf_ptrace_target ();
4459 linux_target_install_ops (t
);
4465 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4467 struct target_ops
*t
;
4469 t
= inf_ptrace_trad_target (register_u_offset
);
4470 linux_target_install_ops (t
);
4475 /* target_is_async_p implementation. */
4478 linux_nat_is_async_p (struct target_ops
*ops
)
4480 return linux_is_async_p ();
4483 /* target_can_async_p implementation. */
4486 linux_nat_can_async_p (struct target_ops
*ops
)
4488 /* NOTE: palves 2008-03-21: We're only async when the user requests
4489 it explicitly with the "set target-async" command.
4490 Someday, linux will always be async. */
4491 return target_async_permitted
;
4495 linux_nat_supports_non_stop (struct target_ops
*self
)
4500 /* True if we want to support multi-process. To be removed when GDB
4501 supports multi-exec. */
4503 int linux_multi_process
= 1;
4506 linux_nat_supports_multi_process (struct target_ops
*self
)
4508 return linux_multi_process
;
4512 linux_nat_supports_disable_randomization (struct target_ops
*self
)
4514 #ifdef HAVE_PERSONALITY
4521 static int async_terminal_is_ours
= 1;
4523 /* target_terminal_inferior implementation.
4525 This is a wrapper around child_terminal_inferior to add async support. */
4528 linux_nat_terminal_inferior (struct target_ops
*self
)
4530 /* Like target_terminal_inferior, use target_can_async_p, not
4531 target_is_async_p, since at this point the target is not async
4532 yet. If it can async, then we know it will become async prior to
4534 if (!target_can_async_p ())
4536 /* Async mode is disabled. */
4537 child_terminal_inferior (self
);
4541 child_terminal_inferior (self
);
4543 /* Calls to target_terminal_*() are meant to be idempotent. */
4544 if (!async_terminal_is_ours
)
4547 delete_file_handler (input_fd
);
4548 async_terminal_is_ours
= 0;
4552 /* target_terminal_ours implementation.
4554 This is a wrapper around child_terminal_ours to add async support (and
4555 implement the target_terminal_ours vs target_terminal_ours_for_output
4556 distinction). child_terminal_ours is currently no different than
4557 child_terminal_ours_for_output.
4558 We leave target_terminal_ours_for_output alone, leaving it to
4559 child_terminal_ours_for_output. */
4562 linux_nat_terminal_ours (struct target_ops
*self
)
4564 /* GDB should never give the terminal to the inferior if the
4565 inferior is running in the background (run&, continue&, etc.),
4566 but claiming it sure should. */
4567 child_terminal_ours (self
);
4569 if (async_terminal_is_ours
)
4572 clear_sigint_trap ();
4573 add_file_handler (input_fd
, stdin_event_handler
, 0);
4574 async_terminal_is_ours
= 1;
4577 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4579 static void *async_client_context
;
4581 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4582 so we notice when any child changes state, and notify the
4583 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4584 above to wait for the arrival of a SIGCHLD. */
4587 sigchld_handler (int signo
)
4589 int old_errno
= errno
;
4591 if (debug_linux_nat
)
4592 ui_file_write_async_safe (gdb_stdlog
,
4593 "sigchld\n", sizeof ("sigchld\n") - 1);
4595 if (signo
== SIGCHLD
4596 && linux_nat_event_pipe
[0] != -1)
4597 async_file_mark (); /* Let the event loop know that there are
4598 events to handle. */
4603 /* Callback registered with the target events file descriptor. */
4606 handle_target_event (int error
, gdb_client_data client_data
)
4608 (*async_client_callback
) (INF_REG_EVENT
, async_client_context
);
4611 /* Create/destroy the target events pipe. Returns previous state. */
4614 linux_async_pipe (int enable
)
4616 int previous
= linux_is_async_p ();
4618 if (previous
!= enable
)
4622 /* Block child signals while we create/destroy the pipe, as
4623 their handler writes to it. */
4624 block_child_signals (&prev_mask
);
4628 if (gdb_pipe_cloexec (linux_nat_event_pipe
) == -1)
4629 internal_error (__FILE__
, __LINE__
,
4630 "creating event pipe failed.");
4632 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4633 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4637 close (linux_nat_event_pipe
[0]);
4638 close (linux_nat_event_pipe
[1]);
4639 linux_nat_event_pipe
[0] = -1;
4640 linux_nat_event_pipe
[1] = -1;
4643 restore_child_signals_mask (&prev_mask
);
4649 /* target_async implementation. */
4652 linux_nat_async (struct target_ops
*ops
,
4653 void (*callback
) (enum inferior_event_type event_type
,
4657 if (callback
!= NULL
)
4659 async_client_callback
= callback
;
4660 async_client_context
= context
;
4661 if (!linux_async_pipe (1))
4663 add_file_handler (linux_nat_event_pipe
[0],
4664 handle_target_event
, NULL
);
4665 /* There may be pending events to handle. Tell the event loop
4672 async_client_callback
= callback
;
4673 async_client_context
= context
;
4674 delete_file_handler (linux_nat_event_pipe
[0]);
4675 linux_async_pipe (0);
4680 /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
4684 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4688 if (debug_linux_nat
)
4689 fprintf_unfiltered (gdb_stdlog
,
4690 "LNSL: running -> suspending %s\n",
4691 target_pid_to_str (lwp
->ptid
));
4694 if (lwp
->last_resume_kind
== resume_stop
)
4696 if (debug_linux_nat
)
4697 fprintf_unfiltered (gdb_stdlog
,
4698 "linux-nat: already stopping LWP %ld at "
4700 ptid_get_lwp (lwp
->ptid
));
4704 stop_callback (lwp
, NULL
);
4705 lwp
->last_resume_kind
= resume_stop
;
4709 /* Already known to be stopped; do nothing. */
4711 if (debug_linux_nat
)
4713 if (find_thread_ptid (lwp
->ptid
)->stop_requested
)
4714 fprintf_unfiltered (gdb_stdlog
,
4715 "LNSL: already stopped/stop_requested %s\n",
4716 target_pid_to_str (lwp
->ptid
));
4718 fprintf_unfiltered (gdb_stdlog
,
4719 "LNSL: already stopped/no "
4720 "stop_requested yet %s\n",
4721 target_pid_to_str (lwp
->ptid
));
4728 linux_nat_stop (struct target_ops
*self
, ptid_t ptid
)
4731 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
4733 linux_ops
->to_stop (linux_ops
, ptid
);
4737 linux_nat_close (struct target_ops
*self
)
4739 /* Unregister from the event loop. */
4740 if (linux_nat_is_async_p (self
))
4741 linux_nat_async (self
, NULL
, NULL
);
4743 if (linux_ops
->to_close
)
4744 linux_ops
->to_close (linux_ops
);
4749 /* When requests are passed down from the linux-nat layer to the
4750 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
4751 used. The address space pointer is stored in the inferior object,
4752 but the common code that is passed such ptid can't tell whether
4753 lwpid is a "main" process id or not (it assumes so). We reverse
4754 look up the "main" process id from the lwp here. */
4756 static struct address_space
*
4757 linux_nat_thread_address_space (struct target_ops
*t
, ptid_t ptid
)
4759 struct lwp_info
*lwp
;
4760 struct inferior
*inf
;
4763 if (ptid_get_lwp (ptid
) == 0)
4765 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
4767 lwp
= find_lwp_pid (ptid
);
4768 pid
= ptid_get_pid (lwp
->ptid
);
4772 /* A (pid,lwpid,0) ptid. */
4773 pid
= ptid_get_pid (ptid
);
4776 inf
= find_inferior_pid (pid
);
4777 gdb_assert (inf
!= NULL
);
4781 /* Return the cached value of the processor core for thread PTID. */
4784 linux_nat_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
4786 struct lwp_info
*info
= find_lwp_pid (ptid
);
4794 linux_nat_add_target (struct target_ops
*t
)
4796 /* Save the provided single-threaded target. We save this in a separate
4797 variable because another target we've inherited from (e.g. inf-ptrace)
4798 may have saved a pointer to T; we want to use it for the final
4799 process stratum target. */
4800 linux_ops_saved
= *t
;
4801 linux_ops
= &linux_ops_saved
;
4803 /* Override some methods for multithreading. */
4804 t
->to_create_inferior
= linux_nat_create_inferior
;
4805 t
->to_attach
= linux_nat_attach
;
4806 t
->to_detach
= linux_nat_detach
;
4807 t
->to_resume
= linux_nat_resume
;
4808 t
->to_wait
= linux_nat_wait
;
4809 t
->to_pass_signals
= linux_nat_pass_signals
;
4810 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4811 t
->to_kill
= linux_nat_kill
;
4812 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4813 t
->to_thread_alive
= linux_nat_thread_alive
;
4814 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4815 t
->to_thread_name
= linux_nat_thread_name
;
4816 t
->to_has_thread_control
= tc_schedlock
;
4817 t
->to_thread_address_space
= linux_nat_thread_address_space
;
4818 t
->to_stopped_by_watchpoint
= linux_nat_stopped_by_watchpoint
;
4819 t
->to_stopped_data_address
= linux_nat_stopped_data_address
;
4820 t
->to_stopped_by_sw_breakpoint
= linux_nat_stopped_by_sw_breakpoint
;
4821 t
->to_supports_stopped_by_sw_breakpoint
= linux_nat_supports_stopped_by_sw_breakpoint
;
4822 t
->to_stopped_by_hw_breakpoint
= linux_nat_stopped_by_hw_breakpoint
;
4823 t
->to_supports_stopped_by_hw_breakpoint
= linux_nat_supports_stopped_by_hw_breakpoint
;
4825 t
->to_can_async_p
= linux_nat_can_async_p
;
4826 t
->to_is_async_p
= linux_nat_is_async_p
;
4827 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
4828 t
->to_async
= linux_nat_async
;
4829 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4830 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4832 super_close
= t
->to_close
;
4833 t
->to_close
= linux_nat_close
;
4835 /* Methods for non-stop support. */
4836 t
->to_stop
= linux_nat_stop
;
4838 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
4840 t
->to_supports_disable_randomization
4841 = linux_nat_supports_disable_randomization
;
4843 t
->to_core_of_thread
= linux_nat_core_of_thread
;
4845 /* We don't change the stratum; this target will sit at
4846 process_stratum and thread_db will set at thread_stratum. This
4847 is a little strange, since this is a multi-threaded-capable
4848 target, but we want to be on the stack below thread_db, and we
4849 also want to be used for single-threaded processes. */
4854 /* Register a method to call whenever a new thread is attached. */
4856 linux_nat_set_new_thread (struct target_ops
*t
,
4857 void (*new_thread
) (struct lwp_info
*))
4859 /* Save the pointer. We only support a single registered instance
4860 of the GNU/Linux native target, so we do not need to map this to
4862 linux_nat_new_thread
= new_thread
;
4865 /* See declaration in linux-nat.h. */
4868 linux_nat_set_new_fork (struct target_ops
*t
,
4869 linux_nat_new_fork_ftype
*new_fork
)
4871 /* Save the pointer. */
4872 linux_nat_new_fork
= new_fork
;
4875 /* See declaration in linux-nat.h. */
4878 linux_nat_set_forget_process (struct target_ops
*t
,
4879 linux_nat_forget_process_ftype
*fn
)
4881 /* Save the pointer. */
4882 linux_nat_forget_process_hook
= fn
;
4885 /* See declaration in linux-nat.h. */
4888 linux_nat_forget_process (pid_t pid
)
4890 if (linux_nat_forget_process_hook
!= NULL
)
4891 linux_nat_forget_process_hook (pid
);
4894 /* Register a method that converts a siginfo object between the layout
4895 that ptrace returns, and the layout in the architecture of the
4898 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
4899 int (*siginfo_fixup
) (siginfo_t
*,
4903 /* Save the pointer. */
4904 linux_nat_siginfo_fixup
= siginfo_fixup
;
4907 /* Register a method to call prior to resuming a thread. */
4910 linux_nat_set_prepare_to_resume (struct target_ops
*t
,
4911 void (*prepare_to_resume
) (struct lwp_info
*))
4913 /* Save the pointer. */
4914 linux_nat_prepare_to_resume
= prepare_to_resume
;
4917 /* See linux-nat.h. */
4920 linux_nat_get_siginfo (ptid_t ptid
, siginfo_t
*siginfo
)
4924 pid
= ptid_get_lwp (ptid
);
4926 pid
= ptid_get_pid (ptid
);
4929 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, siginfo
);
4932 memset (siginfo
, 0, sizeof (*siginfo
));
4938 /* See nat/linux-nat.h. */
4941 current_lwp_ptid (void)
4943 gdb_assert (ptid_lwp_p (inferior_ptid
));
4944 return inferior_ptid
;
4947 /* Provide a prototype to silence -Wmissing-prototypes. */
4948 extern initialize_file_ftype _initialize_linux_nat
;
4951 _initialize_linux_nat (void)
4953 add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance
,
4954 &debug_linux_nat
, _("\
4955 Set debugging of GNU/Linux lwp module."), _("\
4956 Show debugging of GNU/Linux lwp module."), _("\
4957 Enables printf debugging output."),
4959 show_debug_linux_nat
,
4960 &setdebuglist
, &showdebuglist
);
4962 /* Save this mask as the default. */
4963 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4965 /* Install a SIGCHLD handler. */
4966 sigchld_action
.sa_handler
= sigchld_handler
;
4967 sigemptyset (&sigchld_action
.sa_mask
);
4968 sigchld_action
.sa_flags
= SA_RESTART
;
4970 /* Make it the default. */
4971 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
4973 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4974 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
4975 sigdelset (&suspend_mask
, SIGCHLD
);
4977 sigemptyset (&blocked_mask
);
4979 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to
4980 support read-only process state. */
4981 linux_ptrace_set_additional_flags (PTRACE_O_TRACESYSGOOD
4982 | PTRACE_O_TRACEVFORKDONE
4983 | PTRACE_O_TRACEVFORK
4984 | PTRACE_O_TRACEFORK
4985 | PTRACE_O_TRACEEXEC
);
4989 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4990 the GNU/Linux Threads library and therefore doesn't really belong
4993 /* Read variable NAME in the target and return its value if found.
4994 Otherwise return zero. It is assumed that the type of the variable
4998 get_signo (const char *name
)
5000 struct bound_minimal_symbol ms
;
5003 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
5004 if (ms
.minsym
== NULL
)
5007 if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
5008 sizeof (signo
)) != 0)
5014 /* Return the set of signals used by the threads library in *SET. */
5017 lin_thread_get_thread_signals (sigset_t
*set
)
5019 struct sigaction action
;
5020 int restart
, cancel
;
5022 sigemptyset (&blocked_mask
);
5025 restart
= get_signo ("__pthread_sig_restart");
5026 cancel
= get_signo ("__pthread_sig_cancel");
5028 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5029 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5030 not provide any way for the debugger to query the signal numbers -
5031 fortunately they don't change! */
5034 restart
= __SIGRTMIN
;
5037 cancel
= __SIGRTMIN
+ 1;
5039 sigaddset (set
, restart
);
5040 sigaddset (set
, cancel
);
5042 /* The GNU/Linux Threads library makes terminating threads send a
5043 special "cancel" signal instead of SIGCHLD. Make sure we catch
5044 those (to prevent them from terminating GDB itself, which is
5045 likely to be their default action) and treat them the same way as
5048 action
.sa_handler
= sigchld_handler
;
5049 sigemptyset (&action
.sa_mask
);
5050 action
.sa_flags
= SA_RESTART
;
5051 sigaction (cancel
, &action
, NULL
);
5053 /* We block the "cancel" signal throughout this code ... */
5054 sigaddset (&blocked_mask
, cancel
);
5055 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
5057 /* ... except during a sigsuspend. */
5058 sigdelset (&suspend_mask
, cancel
);