1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001-2014 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 "linux-fork.h"
36 #include "gdbthread.h"
40 #include "inf-child.h"
41 #include "inf-ptrace.h"
43 #include <sys/procfs.h> /* for elf_gregset etc. */
44 #include "elf-bfd.h" /* for elfcore_write_* */
45 #include "gregset.h" /* for gregset */
46 #include "gdbcore.h" /* for get_exec_file */
47 #include <ctype.h> /* for isdigit */
48 #include <sys/stat.h> /* for struct stat */
49 #include <fcntl.h> /* for O_RDONLY */
51 #include "event-loop.h"
52 #include "event-top.h"
54 #include <sys/types.h>
56 #include "xml-support.h"
59 #include "nat/linux-osdata.h"
60 #include "linux-tdep.h"
63 #include "tracepoint.h"
64 #include "exceptions.h"
66 #include "target-descriptions.h"
67 #include "filestuff.h"
71 #define SPUFS_MAGIC 0x23c9b64e
74 #ifdef HAVE_PERSONALITY
75 # include <sys/personality.h>
76 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
77 # define ADDR_NO_RANDOMIZE 0x0040000
79 #endif /* HAVE_PERSONALITY */
81 /* This comment documents high-level logic of this file.
83 Waiting for events in sync mode
84 ===============================
86 When waiting for an event in a specific thread, we just use waitpid, passing
87 the specific pid, and not passing WNOHANG.
89 When waiting for an event in all threads, waitpid is not quite good. Prior to
90 version 2.4, Linux can either wait for event in main thread, or in secondary
91 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
92 miss an event. The solution is to use non-blocking waitpid, together with
93 sigsuspend. First, we use non-blocking waitpid to get an event in the main
94 process, if any. Second, we use non-blocking waitpid with the __WCLONED
95 flag to check for events in cloned processes. If nothing is found, we use
96 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
97 happened to a child process -- and SIGCHLD will be delivered both for events
98 in main debugged process and in cloned processes. As soon as we know there's
99 an event, we get back to calling nonblocking waitpid with and without
102 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
103 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
104 blocked, the signal becomes pending and sigsuspend immediately
105 notices it and returns.
107 Waiting for events in async mode
108 ================================
110 In async mode, GDB should always be ready to handle both user input
111 and target events, so neither blocking waitpid nor sigsuspend are
112 viable options. Instead, we should asynchronously notify the GDB main
113 event loop whenever there's an unprocessed event from the target. We
114 detect asynchronous target events by handling SIGCHLD signals. To
115 notify the event loop about target events, the self-pipe trick is used
116 --- a pipe is registered as waitable event source in the event loop,
117 the event loop select/poll's on the read end of this pipe (as well on
118 other event sources, e.g., stdin), and the SIGCHLD handler writes a
119 byte to this pipe. This is more portable than relying on
120 pselect/ppoll, since on kernels that lack those syscalls, libc
121 emulates them with select/poll+sigprocmask, and that is racy
122 (a.k.a. plain broken).
124 Obviously, if we fail to notify the event loop if there's a target
125 event, it's bad. OTOH, if we notify the event loop when there's no
126 event from the target, linux_nat_wait will detect that there's no real
127 event to report, and return event of type TARGET_WAITKIND_IGNORE.
128 This is mostly harmless, but it will waste time and is better avoided.
130 The main design point is that every time GDB is outside linux-nat.c,
131 we have a SIGCHLD handler installed that is called when something
132 happens to the target and notifies the GDB event loop. Whenever GDB
133 core decides to handle the event, and calls into linux-nat.c, we
134 process things as in sync mode, except that the we never block in
137 While processing an event, we may end up momentarily blocked in
138 waitpid calls. Those waitpid calls, while blocking, are guarantied to
139 return quickly. E.g., in all-stop mode, before reporting to the core
140 that an LWP hit a breakpoint, all LWPs are stopped by sending them
141 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
142 Note that this is different from blocking indefinitely waiting for the
143 next event --- here, we're already handling an event.
148 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
149 signal is not entirely significant; we just need for a signal to be delivered,
150 so that we can intercept it. SIGSTOP's advantage is that it can not be
151 blocked. A disadvantage is that it is not a real-time signal, so it can only
152 be queued once; we do not keep track of other sources of SIGSTOP.
154 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
155 use them, because they have special behavior when the signal is generated -
156 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
157 kills the entire thread group.
159 A delivered SIGSTOP would stop the entire thread group, not just the thread we
160 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
161 cancel it (by PTRACE_CONT without passing SIGSTOP).
163 We could use a real-time signal instead. This would solve those problems; we
164 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
165 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
166 generates it, and there are races with trying to find a signal that is not
170 #define O_LARGEFILE 0
173 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
174 the use of the multi-threaded target. */
175 static struct target_ops
*linux_ops
;
176 static struct target_ops linux_ops_saved
;
178 /* The method to call, if any, when a new thread is attached. */
179 static void (*linux_nat_new_thread
) (struct lwp_info
*);
181 /* The method to call, if any, when a new fork is attached. */
182 static linux_nat_new_fork_ftype
*linux_nat_new_fork
;
184 /* The method to call, if any, when a process is no longer
186 static linux_nat_forget_process_ftype
*linux_nat_forget_process_hook
;
188 /* Hook to call prior to resuming a thread. */
189 static void (*linux_nat_prepare_to_resume
) (struct lwp_info
*);
191 /* The method to call, if any, when the siginfo object needs to be
192 converted between the layout returned by ptrace, and the layout in
193 the architecture of the inferior. */
194 static int (*linux_nat_siginfo_fixup
) (siginfo_t
*,
198 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
199 Called by our to_xfer_partial. */
200 static target_xfer_partial_ftype
*super_xfer_partial
;
202 /* The saved to_close method, inherited from inf-ptrace.c.
203 Called by our to_close. */
204 static void (*super_close
) (struct target_ops
*);
206 static unsigned int debug_linux_nat
;
208 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
209 struct cmd_list_element
*c
, const char *value
)
211 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
215 struct simple_pid_list
219 struct simple_pid_list
*next
;
221 struct simple_pid_list
*stopped_pids
;
223 /* Async mode support. */
225 /* The read/write ends of the pipe registered as waitable file in the
227 static int linux_nat_event_pipe
[2] = { -1, -1 };
229 /* Flush the event pipe. */
232 async_file_flush (void)
239 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
241 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
244 /* Put something (anything, doesn't matter what, or how much) in event
245 pipe, so that the select/poll in the event-loop realizes we have
246 something to process. */
249 async_file_mark (void)
253 /* It doesn't really matter what the pipe contains, as long we end
254 up with something in it. Might as well flush the previous
260 ret
= write (linux_nat_event_pipe
[1], "+", 1);
262 while (ret
== -1 && errno
== EINTR
);
264 /* Ignore EAGAIN. If the pipe is full, the event loop will already
265 be awakened anyway. */
268 static int kill_lwp (int lwpid
, int signo
);
270 static int stop_callback (struct lwp_info
*lp
, void *data
);
272 static void block_child_signals (sigset_t
*prev_mask
);
273 static void restore_child_signals_mask (sigset_t
*prev_mask
);
276 static struct lwp_info
*add_lwp (ptid_t ptid
);
277 static void purge_lwp_list (int pid
);
278 static void delete_lwp (ptid_t ptid
);
279 static struct lwp_info
*find_lwp_pid (ptid_t ptid
);
282 /* Trivial list manipulation functions to keep track of a list of
283 new stopped processes. */
285 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
287 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
290 new_pid
->status
= status
;
291 new_pid
->next
= *listp
;
296 in_pid_list_p (struct simple_pid_list
*list
, int pid
)
298 struct simple_pid_list
*p
;
300 for (p
= list
; p
!= NULL
; p
= p
->next
)
307 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
309 struct simple_pid_list
**p
;
311 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
312 if ((*p
)->pid
== pid
)
314 struct simple_pid_list
*next
= (*p
)->next
;
316 *statusp
= (*p
)->status
;
324 /* Initialize ptrace warnings and check for supported ptrace
325 features given PID. */
328 linux_init_ptrace (pid_t pid
)
330 linux_enable_event_reporting (pid
);
331 linux_ptrace_init_warnings ();
335 linux_child_post_attach (struct target_ops
*self
, int pid
)
337 linux_init_ptrace (pid
);
341 linux_child_post_startup_inferior (struct target_ops
*self
, ptid_t ptid
)
343 linux_init_ptrace (ptid_get_pid (ptid
));
346 /* Return the number of known LWPs in the tgid given by PID. */
354 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
355 if (ptid_get_pid (lp
->ptid
) == pid
)
361 /* Call delete_lwp with prototype compatible for make_cleanup. */
364 delete_lwp_cleanup (void *lp_voidp
)
366 struct lwp_info
*lp
= lp_voidp
;
368 delete_lwp (lp
->ptid
);
371 /* Target hook for follow_fork. On entry inferior_ptid must be the
372 ptid of the followed inferior. At return, inferior_ptid will be
376 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
,
381 struct lwp_info
*child_lp
= NULL
;
382 int status
= W_STOPCODE (0);
383 struct cleanup
*old_chain
;
385 int parent_pid
, child_pid
;
387 has_vforked
= (inferior_thread ()->pending_follow
.kind
388 == TARGET_WAITKIND_VFORKED
);
389 parent_pid
= ptid_get_lwp (inferior_ptid
);
391 parent_pid
= ptid_get_pid (inferior_ptid
);
393 = ptid_get_pid (inferior_thread ()->pending_follow
.value
.related_pid
);
396 /* We're already attached to the parent, by default. */
397 old_chain
= save_inferior_ptid ();
398 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
399 child_lp
= add_lwp (inferior_ptid
);
400 child_lp
->stopped
= 1;
401 child_lp
->last_resume_kind
= resume_stop
;
403 /* Detach new forked process? */
406 make_cleanup (delete_lwp_cleanup
, child_lp
);
408 if (linux_nat_prepare_to_resume
!= NULL
)
409 linux_nat_prepare_to_resume (child_lp
);
411 /* When debugging an inferior in an architecture that supports
412 hardware single stepping on a kernel without commit
413 6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child
414 process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits
415 set if the parent process had them set.
416 To work around this, single step the child process
417 once before detaching to clear the flags. */
419 if (!gdbarch_software_single_step_p (target_thread_architecture
422 linux_disable_event_reporting (child_pid
);
423 if (ptrace (PTRACE_SINGLESTEP
, child_pid
, 0, 0) < 0)
424 perror_with_name (_("Couldn't do single step"));
425 if (my_waitpid (child_pid
, &status
, 0) < 0)
426 perror_with_name (_("Couldn't wait vfork process"));
429 if (WIFSTOPPED (status
))
433 signo
= WSTOPSIG (status
);
435 && !signal_pass_state (gdb_signal_from_host (signo
)))
437 ptrace (PTRACE_DETACH
, child_pid
, 0, signo
);
440 /* Resets value of inferior_ptid to parent ptid. */
441 do_cleanups (old_chain
);
445 /* Let the thread_db layer learn about this new process. */
446 check_for_thread_db ();
449 do_cleanups (old_chain
);
453 struct lwp_info
*parent_lp
;
455 parent_lp
= find_lwp_pid (pid_to_ptid (parent_pid
));
456 gdb_assert (linux_supports_tracefork () >= 0);
458 if (linux_supports_tracevforkdone ())
461 fprintf_unfiltered (gdb_stdlog
,
462 "LCFF: waiting for VFORK_DONE on %d\n",
464 parent_lp
->stopped
= 1;
466 /* We'll handle the VFORK_DONE event like any other
467 event, in target_wait. */
471 /* We can't insert breakpoints until the child has
472 finished with the shared memory region. We need to
473 wait until that happens. Ideal would be to just
475 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
476 - waitpid (parent_pid, &status, __WALL);
477 However, most architectures can't handle a syscall
478 being traced on the way out if it wasn't traced on
481 We might also think to loop, continuing the child
482 until it exits or gets a SIGTRAP. One problem is
483 that the child might call ptrace with PTRACE_TRACEME.
485 There's no simple and reliable way to figure out when
486 the vforked child will be done with its copy of the
487 shared memory. We could step it out of the syscall,
488 two instructions, let it go, and then single-step the
489 parent once. When we have hardware single-step, this
490 would work; with software single-step it could still
491 be made to work but we'd have to be able to insert
492 single-step breakpoints in the child, and we'd have
493 to insert -just- the single-step breakpoint in the
494 parent. Very awkward.
496 In the end, the best we can do is to make sure it
497 runs for a little while. Hopefully it will be out of
498 range of any breakpoints we reinsert. Usually this
499 is only the single-step breakpoint at vfork's return
503 fprintf_unfiltered (gdb_stdlog
,
504 "LCFF: no VFORK_DONE "
505 "support, sleeping a bit\n");
509 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
510 and leave it pending. The next linux_nat_resume call
511 will notice a pending event, and bypasses actually
512 resuming the inferior. */
513 parent_lp
->status
= 0;
514 parent_lp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
515 parent_lp
->stopped
= 1;
517 /* If we're in async mode, need to tell the event loop
518 there's something here to process. */
519 if (target_can_async_p ())
526 struct lwp_info
*child_lp
;
528 child_lp
= add_lwp (inferior_ptid
);
529 child_lp
->stopped
= 1;
530 child_lp
->last_resume_kind
= resume_stop
;
532 /* Let the thread_db layer learn about this new process. */
533 check_for_thread_db ();
541 linux_child_insert_fork_catchpoint (struct target_ops
*self
, int pid
)
543 return !linux_supports_tracefork ();
547 linux_child_remove_fork_catchpoint (struct target_ops
*self
, int pid
)
553 linux_child_insert_vfork_catchpoint (struct target_ops
*self
, int pid
)
555 return !linux_supports_tracefork ();
559 linux_child_remove_vfork_catchpoint (struct target_ops
*self
, int pid
)
565 linux_child_insert_exec_catchpoint (struct target_ops
*self
, int pid
)
567 return !linux_supports_tracefork ();
571 linux_child_remove_exec_catchpoint (struct target_ops
*self
, int pid
)
577 linux_child_set_syscall_catchpoint (struct target_ops
*self
,
578 int pid
, int needed
, int any_count
,
579 int table_size
, int *table
)
581 if (!linux_supports_tracesysgood ())
584 /* On GNU/Linux, we ignore the arguments. It means that we only
585 enable the syscall catchpoints, but do not disable them.
587 Also, we do not use the `table' information because we do not
588 filter system calls here. We let GDB do the logic for us. */
592 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
593 are processes sharing the same VM space. A multi-threaded process
594 is basically a group of such processes. However, such a grouping
595 is almost entirely a user-space issue; the kernel doesn't enforce
596 such a grouping at all (this might change in the future). In
597 general, we'll rely on the threads library (i.e. the GNU/Linux
598 Threads library) to provide such a grouping.
600 It is perfectly well possible to write a multi-threaded application
601 without the assistance of a threads library, by using the clone
602 system call directly. This module should be able to give some
603 rudimentary support for debugging such applications if developers
604 specify the CLONE_PTRACE flag in the clone system call, and are
605 using the Linux kernel 2.4 or above.
607 Note that there are some peculiarities in GNU/Linux that affect
610 - In general one should specify the __WCLONE flag to waitpid in
611 order to make it report events for any of the cloned processes
612 (and leave it out for the initial process). However, if a cloned
613 process has exited the exit status is only reported if the
614 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
615 we cannot use it since GDB must work on older systems too.
617 - When a traced, cloned process exits and is waited for by the
618 debugger, the kernel reassigns it to the original parent and
619 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
620 library doesn't notice this, which leads to the "zombie problem":
621 When debugged a multi-threaded process that spawns a lot of
622 threads will run out of processes, even if the threads exit,
623 because the "zombies" stay around. */
625 /* List of known LWPs. */
626 struct lwp_info
*lwp_list
;
629 /* Original signal mask. */
630 static sigset_t normal_mask
;
632 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
633 _initialize_linux_nat. */
634 static sigset_t suspend_mask
;
636 /* Signals to block to make that sigsuspend work. */
637 static sigset_t blocked_mask
;
639 /* SIGCHLD action. */
640 struct sigaction sigchld_action
;
642 /* Block child signals (SIGCHLD and linux threads signals), and store
643 the previous mask in PREV_MASK. */
646 block_child_signals (sigset_t
*prev_mask
)
648 /* Make sure SIGCHLD is blocked. */
649 if (!sigismember (&blocked_mask
, SIGCHLD
))
650 sigaddset (&blocked_mask
, SIGCHLD
);
652 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
655 /* Restore child signals mask, previously returned by
656 block_child_signals. */
659 restore_child_signals_mask (sigset_t
*prev_mask
)
661 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
664 /* Mask of signals to pass directly to the inferior. */
665 static sigset_t pass_mask
;
667 /* Update signals to pass to the inferior. */
669 linux_nat_pass_signals (struct target_ops
*self
,
670 int numsigs
, unsigned char *pass_signals
)
674 sigemptyset (&pass_mask
);
676 for (signo
= 1; signo
< NSIG
; signo
++)
678 int target_signo
= gdb_signal_from_host (signo
);
679 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
680 sigaddset (&pass_mask
, signo
);
686 /* Prototypes for local functions. */
687 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
688 static int linux_thread_alive (ptid_t ptid
);
689 static char *linux_child_pid_to_exec_file (struct target_ops
*self
, int pid
);
693 /* Destroy and free LP. */
696 lwp_free (struct lwp_info
*lp
)
698 xfree (lp
->arch_private
);
702 /* Remove all LWPs belong to PID from the lwp list. */
705 purge_lwp_list (int pid
)
707 struct lwp_info
*lp
, *lpprev
, *lpnext
;
711 for (lp
= lwp_list
; lp
; lp
= lpnext
)
715 if (ptid_get_pid (lp
->ptid
) == pid
)
720 lpprev
->next
= lp
->next
;
729 /* Add the LWP specified by PTID to the list. PTID is the first LWP
730 in the process. Return a pointer to the structure describing the
733 This differs from add_lwp in that we don't let the arch specific
734 bits know about this new thread. Current clients of this callback
735 take the opportunity to install watchpoints in the new thread, and
736 we shouldn't do that for the first thread. If we're spawning a
737 child ("run"), the thread executes the shell wrapper first, and we
738 shouldn't touch it until it execs the program we want to debug.
739 For "attach", it'd be okay to call the callback, but it's not
740 necessary, because watchpoints can't yet have been inserted into
743 static struct lwp_info
*
744 add_initial_lwp (ptid_t ptid
)
748 gdb_assert (ptid_lwp_p (ptid
));
750 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
752 memset (lp
, 0, sizeof (struct lwp_info
));
754 lp
->last_resume_kind
= resume_continue
;
755 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
766 /* Add the LWP specified by PID to the list. Return a pointer to the
767 structure describing the new LWP. The LWP should already be
770 static struct lwp_info
*
771 add_lwp (ptid_t ptid
)
775 lp
= add_initial_lwp (ptid
);
777 /* Let the arch specific bits know about this new thread. Current
778 clients of this callback take the opportunity to install
779 watchpoints in the new thread. We don't do this for the first
780 thread though. See add_initial_lwp. */
781 if (linux_nat_new_thread
!= NULL
)
782 linux_nat_new_thread (lp
);
787 /* Remove the LWP specified by PID from the list. */
790 delete_lwp (ptid_t ptid
)
792 struct lwp_info
*lp
, *lpprev
;
796 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
797 if (ptid_equal (lp
->ptid
, ptid
))
804 lpprev
->next
= lp
->next
;
811 /* Return a pointer to the structure describing the LWP corresponding
812 to PID. If no corresponding LWP could be found, return NULL. */
814 static struct lwp_info
*
815 find_lwp_pid (ptid_t ptid
)
820 if (ptid_lwp_p (ptid
))
821 lwp
= ptid_get_lwp (ptid
);
823 lwp
= ptid_get_pid (ptid
);
825 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
826 if (lwp
== ptid_get_lwp (lp
->ptid
))
832 /* Call CALLBACK with its second argument set to DATA for every LWP in
833 the list. If CALLBACK returns 1 for a particular LWP, return a
834 pointer to the structure describing that LWP immediately.
835 Otherwise return NULL. */
838 iterate_over_lwps (ptid_t filter
,
839 int (*callback
) (struct lwp_info
*, void *),
842 struct lwp_info
*lp
, *lpnext
;
844 for (lp
= lwp_list
; lp
; lp
= lpnext
)
848 if (ptid_match (lp
->ptid
, filter
))
850 if ((*callback
) (lp
, data
))
858 /* Update our internal state when changing from one checkpoint to
859 another indicated by NEW_PTID. We can only switch single-threaded
860 applications, so we only create one new LWP, and the previous list
864 linux_nat_switch_fork (ptid_t new_ptid
)
868 purge_lwp_list (ptid_get_pid (inferior_ptid
));
870 lp
= add_lwp (new_ptid
);
873 /* This changes the thread's ptid while preserving the gdb thread
874 num. Also changes the inferior pid, while preserving the
876 thread_change_ptid (inferior_ptid
, new_ptid
);
878 /* We've just told GDB core that the thread changed target id, but,
879 in fact, it really is a different thread, with different register
881 registers_changed ();
884 /* Handle the exit of a single thread LP. */
887 exit_lwp (struct lwp_info
*lp
)
889 struct thread_info
*th
= find_thread_ptid (lp
->ptid
);
893 if (print_thread_events
)
894 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
896 delete_thread (lp
->ptid
);
899 delete_lwp (lp
->ptid
);
902 /* Wait for the LWP specified by LP, which we have just attached to.
903 Returns a wait status for that LWP, to cache. */
906 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
909 pid_t new_pid
, pid
= ptid_get_lwp (ptid
);
912 if (linux_proc_pid_is_stopped (pid
))
915 fprintf_unfiltered (gdb_stdlog
,
916 "LNPAW: Attaching to a stopped process\n");
918 /* The process is definitely stopped. It is in a job control
919 stop, unless the kernel predates the TASK_STOPPED /
920 TASK_TRACED distinction, in which case it might be in a
921 ptrace stop. Make sure it is in a ptrace stop; from there we
922 can kill it, signal it, et cetera.
924 First make sure there is a pending SIGSTOP. Since we are
925 already attached, the process can not transition from stopped
926 to running without a PTRACE_CONT; so we know this signal will
927 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
928 probably already in the queue (unless this kernel is old
929 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
930 is not an RT signal, it can only be queued once. */
931 kill_lwp (pid
, SIGSTOP
);
933 /* Finally, resume the stopped process. This will deliver the SIGSTOP
934 (or a higher priority signal, just like normal PTRACE_ATTACH). */
935 ptrace (PTRACE_CONT
, pid
, 0, 0);
938 /* Make sure the initial process is stopped. The user-level threads
939 layer might want to poke around in the inferior, and that won't
940 work if things haven't stabilized yet. */
941 new_pid
= my_waitpid (pid
, &status
, 0);
942 if (new_pid
== -1 && errno
== ECHILD
)
945 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
947 /* Try again with __WCLONE to check cloned processes. */
948 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
952 gdb_assert (pid
== new_pid
);
954 if (!WIFSTOPPED (status
))
956 /* The pid we tried to attach has apparently just exited. */
958 fprintf_unfiltered (gdb_stdlog
, "LNPAW: Failed to stop %d: %s",
959 pid
, status_to_str (status
));
963 if (WSTOPSIG (status
) != SIGSTOP
)
967 fprintf_unfiltered (gdb_stdlog
,
968 "LNPAW: Received %s after attaching\n",
969 status_to_str (status
));
975 /* Attach to the LWP specified by PID. Return 0 if successful, -1 if
976 the new LWP could not be attached, or 1 if we're already auto
977 attached to this thread, but haven't processed the
978 PTRACE_EVENT_CLONE event of its parent thread, so we just ignore
979 its existance, without considering it an error. */
982 lin_lwp_attach_lwp (ptid_t ptid
)
987 gdb_assert (ptid_lwp_p (ptid
));
989 lp
= find_lwp_pid (ptid
);
990 lwpid
= ptid_get_lwp (ptid
);
992 /* We assume that we're already attached to any LWP that has an id
993 equal to the overall process id, and to any LWP that is already
994 in our list of LWPs. If we're not seeing exit events from threads
995 and we've had PID wraparound since we last tried to stop all threads,
996 this assumption might be wrong; fortunately, this is very unlikely
998 if (lwpid
!= ptid_get_pid (ptid
) && lp
== NULL
)
1000 int status
, cloned
= 0, signalled
= 0;
1002 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) < 0)
1004 if (linux_supports_tracefork ())
1006 /* If we haven't stopped all threads when we get here,
1007 we may have seen a thread listed in thread_db's list,
1008 but not processed the PTRACE_EVENT_CLONE yet. If
1009 that's the case, ignore this new thread, and let
1010 normal event handling discover it later. */
1011 if (in_pid_list_p (stopped_pids
, lwpid
))
1013 /* We've already seen this thread stop, but we
1014 haven't seen the PTRACE_EVENT_CLONE extended
1023 /* See if we've got a stop for this new child
1024 pending. If so, we're already attached. */
1025 new_pid
= my_waitpid (lwpid
, &status
, WNOHANG
);
1026 if (new_pid
== -1 && errno
== ECHILD
)
1027 new_pid
= my_waitpid (lwpid
, &status
, __WCLONE
| WNOHANG
);
1030 if (WIFSTOPPED (status
))
1031 add_to_pid_list (&stopped_pids
, lwpid
, status
);
1037 /* If we fail to attach to the thread, issue a warning,
1038 but continue. One way this can happen is if thread
1039 creation is interrupted; as of Linux kernel 2.6.19, a
1040 bug may place threads in the thread list and then fail
1042 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1043 safe_strerror (errno
));
1047 if (debug_linux_nat
)
1048 fprintf_unfiltered (gdb_stdlog
,
1049 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1050 target_pid_to_str (ptid
));
1052 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1053 if (!WIFSTOPPED (status
))
1056 lp
= add_lwp (ptid
);
1058 lp
->cloned
= cloned
;
1059 lp
->signalled
= signalled
;
1060 if (WSTOPSIG (status
) != SIGSTOP
)
1063 lp
->status
= status
;
1066 target_post_attach (ptid_get_lwp (lp
->ptid
));
1068 if (debug_linux_nat
)
1070 fprintf_unfiltered (gdb_stdlog
,
1071 "LLAL: waitpid %s received %s\n",
1072 target_pid_to_str (ptid
),
1073 status_to_str (status
));
1078 /* We assume that the LWP representing the original process is
1079 already stopped. Mark it as stopped in the data structure
1080 that the GNU/linux ptrace layer uses to keep track of
1081 threads. Note that this won't have already been done since
1082 the main thread will have, we assume, been stopped by an
1083 attach from a different layer. */
1085 lp
= add_lwp (ptid
);
1089 lp
->last_resume_kind
= resume_stop
;
1094 linux_nat_create_inferior (struct target_ops
*ops
,
1095 char *exec_file
, char *allargs
, char **env
,
1098 #ifdef HAVE_PERSONALITY
1099 int personality_orig
= 0, personality_set
= 0;
1100 #endif /* HAVE_PERSONALITY */
1102 /* The fork_child mechanism is synchronous and calls target_wait, so
1103 we have to mask the async mode. */
1105 #ifdef HAVE_PERSONALITY
1106 if (disable_randomization
)
1109 personality_orig
= personality (0xffffffff);
1110 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
1112 personality_set
= 1;
1113 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
1115 if (errno
!= 0 || (personality_set
1116 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
1117 warning (_("Error disabling address space randomization: %s"),
1118 safe_strerror (errno
));
1120 #endif /* HAVE_PERSONALITY */
1122 /* Make sure we report all signals during startup. */
1123 linux_nat_pass_signals (ops
, 0, NULL
);
1125 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1127 #ifdef HAVE_PERSONALITY
1128 if (personality_set
)
1131 personality (personality_orig
);
1133 warning (_("Error restoring address space randomization: %s"),
1134 safe_strerror (errno
));
1136 #endif /* HAVE_PERSONALITY */
1140 linux_nat_attach (struct target_ops
*ops
, const char *args
, int from_tty
)
1142 struct lwp_info
*lp
;
1145 volatile struct gdb_exception ex
;
1147 /* Make sure we report all signals during attach. */
1148 linux_nat_pass_signals (ops
, 0, NULL
);
1150 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
1152 linux_ops
->to_attach (ops
, args
, from_tty
);
1156 pid_t pid
= parse_pid_to_attach (args
);
1157 struct buffer buffer
;
1158 char *message
, *buffer_s
;
1160 message
= xstrdup (ex
.message
);
1161 make_cleanup (xfree
, message
);
1163 buffer_init (&buffer
);
1164 linux_ptrace_attach_fail_reason (pid
, &buffer
);
1166 buffer_grow_str0 (&buffer
, "");
1167 buffer_s
= buffer_finish (&buffer
);
1168 make_cleanup (xfree
, buffer_s
);
1170 if (*buffer_s
!= '\0')
1171 throw_error (ex
.error
, "warning: %s\n%s", buffer_s
, message
);
1173 throw_error (ex
.error
, "%s", message
);
1176 /* The ptrace base target adds the main thread with (pid,0,0)
1177 format. Decorate it with lwp info. */
1178 ptid
= ptid_build (ptid_get_pid (inferior_ptid
),
1179 ptid_get_pid (inferior_ptid
),
1181 thread_change_ptid (inferior_ptid
, ptid
);
1183 /* Add the initial process as the first LWP to the list. */
1184 lp
= add_initial_lwp (ptid
);
1186 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1188 if (!WIFSTOPPED (status
))
1190 if (WIFEXITED (status
))
1192 int exit_code
= WEXITSTATUS (status
);
1194 target_terminal_ours ();
1195 target_mourn_inferior ();
1197 error (_("Unable to attach: program exited normally."));
1199 error (_("Unable to attach: program exited with code %d."),
1202 else if (WIFSIGNALED (status
))
1204 enum gdb_signal signo
;
1206 target_terminal_ours ();
1207 target_mourn_inferior ();
1209 signo
= gdb_signal_from_host (WTERMSIG (status
));
1210 error (_("Unable to attach: program terminated with signal "
1212 gdb_signal_to_name (signo
),
1213 gdb_signal_to_string (signo
));
1216 internal_error (__FILE__
, __LINE__
,
1217 _("unexpected status %d for PID %ld"),
1218 status
, (long) ptid_get_lwp (ptid
));
1223 /* Save the wait status to report later. */
1225 if (debug_linux_nat
)
1226 fprintf_unfiltered (gdb_stdlog
,
1227 "LNA: waitpid %ld, saving status %s\n",
1228 (long) ptid_get_pid (lp
->ptid
), status_to_str (status
));
1230 lp
->status
= status
;
1232 if (target_can_async_p ())
1233 target_async (inferior_event_handler
, 0);
1236 /* Get pending status of LP. */
1238 get_pending_status (struct lwp_info
*lp
, int *status
)
1240 enum gdb_signal signo
= GDB_SIGNAL_0
;
1242 /* If we paused threads momentarily, we may have stored pending
1243 events in lp->status or lp->waitstatus (see stop_wait_callback),
1244 and GDB core hasn't seen any signal for those threads.
1245 Otherwise, the last signal reported to the core is found in the
1246 thread object's stop_signal.
1248 There's a corner case that isn't handled here at present. Only
1249 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1250 stop_signal make sense as a real signal to pass to the inferior.
1251 Some catchpoint related events, like
1252 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1253 to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1254 those traps are debug API (ptrace in our case) related and
1255 induced; the inferior wouldn't see them if it wasn't being
1256 traced. Hence, we should never pass them to the inferior, even
1257 when set to pass state. Since this corner case isn't handled by
1258 infrun.c when proceeding with a signal, for consistency, neither
1259 do we handle it here (or elsewhere in the file we check for
1260 signal pass state). Normally SIGTRAP isn't set to pass state, so
1261 this is really a corner case. */
1263 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
1264 signo
= GDB_SIGNAL_0
; /* a pending ptrace event, not a real signal. */
1265 else if (lp
->status
)
1266 signo
= gdb_signal_from_host (WSTOPSIG (lp
->status
));
1267 else if (non_stop
&& !is_executing (lp
->ptid
))
1269 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1271 signo
= tp
->suspend
.stop_signal
;
1275 struct target_waitstatus last
;
1278 get_last_target_status (&last_ptid
, &last
);
1280 if (ptid_get_lwp (lp
->ptid
) == ptid_get_lwp (last_ptid
))
1282 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1284 signo
= tp
->suspend
.stop_signal
;
1290 if (signo
== GDB_SIGNAL_0
)
1292 if (debug_linux_nat
)
1293 fprintf_unfiltered (gdb_stdlog
,
1294 "GPT: lwp %s has no pending signal\n",
1295 target_pid_to_str (lp
->ptid
));
1297 else if (!signal_pass_state (signo
))
1299 if (debug_linux_nat
)
1300 fprintf_unfiltered (gdb_stdlog
,
1301 "GPT: lwp %s had signal %s, "
1302 "but it is in no pass state\n",
1303 target_pid_to_str (lp
->ptid
),
1304 gdb_signal_to_string (signo
));
1308 *status
= W_STOPCODE (gdb_signal_to_host (signo
));
1310 if (debug_linux_nat
)
1311 fprintf_unfiltered (gdb_stdlog
,
1312 "GPT: lwp %s has pending signal %s\n",
1313 target_pid_to_str (lp
->ptid
),
1314 gdb_signal_to_string (signo
));
1321 detach_callback (struct lwp_info
*lp
, void *data
)
1323 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1325 if (debug_linux_nat
&& lp
->status
)
1326 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1327 strsignal (WSTOPSIG (lp
->status
)),
1328 target_pid_to_str (lp
->ptid
));
1330 /* If there is a pending SIGSTOP, get rid of it. */
1333 if (debug_linux_nat
)
1334 fprintf_unfiltered (gdb_stdlog
,
1335 "DC: Sending SIGCONT to %s\n",
1336 target_pid_to_str (lp
->ptid
));
1338 kill_lwp (ptid_get_lwp (lp
->ptid
), SIGCONT
);
1342 /* We don't actually detach from the LWP that has an id equal to the
1343 overall process id just yet. */
1344 if (ptid_get_lwp (lp
->ptid
) != ptid_get_pid (lp
->ptid
))
1348 /* Pass on any pending signal for this LWP. */
1349 get_pending_status (lp
, &status
);
1351 if (linux_nat_prepare_to_resume
!= NULL
)
1352 linux_nat_prepare_to_resume (lp
);
1354 if (ptrace (PTRACE_DETACH
, ptid_get_lwp (lp
->ptid
), 0,
1355 WSTOPSIG (status
)) < 0)
1356 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1357 safe_strerror (errno
));
1359 if (debug_linux_nat
)
1360 fprintf_unfiltered (gdb_stdlog
,
1361 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1362 target_pid_to_str (lp
->ptid
),
1363 strsignal (WSTOPSIG (status
)));
1365 delete_lwp (lp
->ptid
);
1372 linux_nat_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
1376 struct lwp_info
*main_lwp
;
1378 pid
= ptid_get_pid (inferior_ptid
);
1380 /* Don't unregister from the event loop, as there may be other
1381 inferiors running. */
1383 /* Stop all threads before detaching. ptrace requires that the
1384 thread is stopped to sucessfully detach. */
1385 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1386 /* ... and wait until all of them have reported back that
1387 they're no longer running. */
1388 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1390 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1392 /* Only the initial process should be left right now. */
1393 gdb_assert (num_lwps (ptid_get_pid (inferior_ptid
)) == 1);
1395 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1397 /* Pass on any pending signal for the last LWP. */
1398 if ((args
== NULL
|| *args
== '\0')
1399 && get_pending_status (main_lwp
, &status
) != -1
1400 && WIFSTOPPED (status
))
1404 /* Put the signal number in ARGS so that inf_ptrace_detach will
1405 pass it along with PTRACE_DETACH. */
1407 xsnprintf (tem
, 8, "%d", (int) WSTOPSIG (status
));
1409 if (debug_linux_nat
)
1410 fprintf_unfiltered (gdb_stdlog
,
1411 "LND: Sending signal %s to %s\n",
1413 target_pid_to_str (main_lwp
->ptid
));
1416 if (linux_nat_prepare_to_resume
!= NULL
)
1417 linux_nat_prepare_to_resume (main_lwp
);
1418 delete_lwp (main_lwp
->ptid
);
1420 if (forks_exist_p ())
1422 /* Multi-fork case. The current inferior_ptid is being detached
1423 from, but there are other viable forks to debug. Detach from
1424 the current fork, and context-switch to the first
1426 linux_fork_detach (args
, from_tty
);
1429 linux_ops
->to_detach (ops
, args
, from_tty
);
1435 resume_lwp (struct lwp_info
*lp
, int step
, enum gdb_signal signo
)
1439 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
1441 if (inf
->vfork_child
!= NULL
)
1443 if (debug_linux_nat
)
1444 fprintf_unfiltered (gdb_stdlog
,
1445 "RC: Not resuming %s (vfork parent)\n",
1446 target_pid_to_str (lp
->ptid
));
1448 else if (lp
->status
== 0
1449 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
1451 if (debug_linux_nat
)
1452 fprintf_unfiltered (gdb_stdlog
,
1453 "RC: Resuming sibling %s, %s, %s\n",
1454 target_pid_to_str (lp
->ptid
),
1455 (signo
!= GDB_SIGNAL_0
1456 ? strsignal (gdb_signal_to_host (signo
))
1458 step
? "step" : "resume");
1460 if (linux_nat_prepare_to_resume
!= NULL
)
1461 linux_nat_prepare_to_resume (lp
);
1462 linux_ops
->to_resume (linux_ops
,
1463 pid_to_ptid (ptid_get_lwp (lp
->ptid
)),
1467 lp
->stopped_by_watchpoint
= 0;
1471 if (debug_linux_nat
)
1472 fprintf_unfiltered (gdb_stdlog
,
1473 "RC: Not resuming sibling %s (has pending)\n",
1474 target_pid_to_str (lp
->ptid
));
1479 if (debug_linux_nat
)
1480 fprintf_unfiltered (gdb_stdlog
,
1481 "RC: Not resuming sibling %s (not stopped)\n",
1482 target_pid_to_str (lp
->ptid
));
1486 /* Callback for iterate_over_lwps. If LWP is EXCEPT, do nothing.
1487 Resume LWP with the last stop signal, if it is in pass state. */
1490 linux_nat_resume_callback (struct lwp_info
*lp
, void *except
)
1492 enum gdb_signal signo
= GDB_SIGNAL_0
;
1499 struct thread_info
*thread
;
1501 thread
= find_thread_ptid (lp
->ptid
);
1504 signo
= thread
->suspend
.stop_signal
;
1505 thread
->suspend
.stop_signal
= GDB_SIGNAL_0
;
1509 resume_lwp (lp
, 0, signo
);
1514 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1517 lp
->last_resume_kind
= resume_stop
;
1522 resume_set_callback (struct lwp_info
*lp
, void *data
)
1525 lp
->last_resume_kind
= resume_continue
;
1530 linux_nat_resume (struct target_ops
*ops
,
1531 ptid_t ptid
, int step
, enum gdb_signal signo
)
1533 struct lwp_info
*lp
;
1536 if (debug_linux_nat
)
1537 fprintf_unfiltered (gdb_stdlog
,
1538 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1539 step
? "step" : "resume",
1540 target_pid_to_str (ptid
),
1541 (signo
!= GDB_SIGNAL_0
1542 ? strsignal (gdb_signal_to_host (signo
)) : "0"),
1543 target_pid_to_str (inferior_ptid
));
1545 /* A specific PTID means `step only this process id'. */
1546 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
1547 || ptid_is_pid (ptid
));
1549 /* Mark the lwps we're resuming as resumed. */
1550 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
1552 /* See if it's the current inferior that should be handled
1555 lp
= find_lwp_pid (inferior_ptid
);
1557 lp
= find_lwp_pid (ptid
);
1558 gdb_assert (lp
!= NULL
);
1560 /* Remember if we're stepping. */
1562 lp
->last_resume_kind
= step
? resume_step
: resume_continue
;
1564 /* If we have a pending wait status for this thread, there is no
1565 point in resuming the process. But first make sure that
1566 linux_nat_wait won't preemptively handle the event - we
1567 should never take this short-circuit if we are going to
1568 leave LP running, since we have skipped resuming all the
1569 other threads. This bit of code needs to be synchronized
1570 with linux_nat_wait. */
1572 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1575 && WSTOPSIG (lp
->status
)
1576 && sigismember (&pass_mask
, WSTOPSIG (lp
->status
)))
1578 if (debug_linux_nat
)
1579 fprintf_unfiltered (gdb_stdlog
,
1580 "LLR: Not short circuiting for ignored "
1581 "status 0x%x\n", lp
->status
);
1583 /* FIXME: What should we do if we are supposed to continue
1584 this thread with a signal? */
1585 gdb_assert (signo
== GDB_SIGNAL_0
);
1586 signo
= gdb_signal_from_host (WSTOPSIG (lp
->status
));
1591 if (lp
->status
|| lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
1593 /* FIXME: What should we do if we are supposed to continue
1594 this thread with a signal? */
1595 gdb_assert (signo
== GDB_SIGNAL_0
);
1597 if (debug_linux_nat
)
1598 fprintf_unfiltered (gdb_stdlog
,
1599 "LLR: Short circuiting for status 0x%x\n",
1602 if (target_can_async_p ())
1604 target_async (inferior_event_handler
, 0);
1605 /* Tell the event loop we have something to process. */
1612 iterate_over_lwps (ptid
, linux_nat_resume_callback
, lp
);
1614 /* Convert to something the lower layer understands. */
1615 ptid
= pid_to_ptid (ptid_get_lwp (lp
->ptid
));
1617 if (linux_nat_prepare_to_resume
!= NULL
)
1618 linux_nat_prepare_to_resume (lp
);
1619 linux_ops
->to_resume (linux_ops
, ptid
, step
, signo
);
1620 lp
->stopped_by_watchpoint
= 0;
1623 if (debug_linux_nat
)
1624 fprintf_unfiltered (gdb_stdlog
,
1625 "LLR: %s %s, %s (resume event thread)\n",
1626 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1627 target_pid_to_str (ptid
),
1628 (signo
!= GDB_SIGNAL_0
1629 ? strsignal (gdb_signal_to_host (signo
)) : "0"));
1631 if (target_can_async_p ())
1632 target_async (inferior_event_handler
, 0);
1635 /* Send a signal to an LWP. */
1638 kill_lwp (int lwpid
, int signo
)
1640 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1641 fails, then we are not using nptl threads and we should be using kill. */
1643 #ifdef HAVE_TKILL_SYSCALL
1645 static int tkill_failed
;
1652 ret
= syscall (__NR_tkill
, lwpid
, signo
);
1653 if (errno
!= ENOSYS
)
1660 return kill (lwpid
, signo
);
1663 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
1664 event, check if the core is interested in it: if not, ignore the
1665 event, and keep waiting; otherwise, we need to toggle the LWP's
1666 syscall entry/exit status, since the ptrace event itself doesn't
1667 indicate it, and report the trap to higher layers. */
1670 linux_handle_syscall_trap (struct lwp_info
*lp
, int stopping
)
1672 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1673 struct gdbarch
*gdbarch
= target_thread_architecture (lp
->ptid
);
1674 int syscall_number
= (int) gdbarch_get_syscall_number (gdbarch
, lp
->ptid
);
1678 /* If we're stopping threads, there's a SIGSTOP pending, which
1679 makes it so that the LWP reports an immediate syscall return,
1680 followed by the SIGSTOP. Skip seeing that "return" using
1681 PTRACE_CONT directly, and let stop_wait_callback collect the
1682 SIGSTOP. Later when the thread is resumed, a new syscall
1683 entry event. If we didn't do this (and returned 0), we'd
1684 leave a syscall entry pending, and our caller, by using
1685 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
1686 itself. Later, when the user re-resumes this LWP, we'd see
1687 another syscall entry event and we'd mistake it for a return.
1689 If stop_wait_callback didn't force the SIGSTOP out of the LWP
1690 (leaving immediately with LWP->signalled set, without issuing
1691 a PTRACE_CONT), it would still be problematic to leave this
1692 syscall enter pending, as later when the thread is resumed,
1693 it would then see the same syscall exit mentioned above,
1694 followed by the delayed SIGSTOP, while the syscall didn't
1695 actually get to execute. It seems it would be even more
1696 confusing to the user. */
1698 if (debug_linux_nat
)
1699 fprintf_unfiltered (gdb_stdlog
,
1700 "LHST: ignoring syscall %d "
1701 "for LWP %ld (stopping threads), "
1702 "resuming with PTRACE_CONT for SIGSTOP\n",
1704 ptid_get_lwp (lp
->ptid
));
1706 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
1707 ptrace (PTRACE_CONT
, ptid_get_lwp (lp
->ptid
), 0, 0);
1712 if (catch_syscall_enabled ())
1714 /* Always update the entry/return state, even if this particular
1715 syscall isn't interesting to the core now. In async mode,
1716 the user could install a new catchpoint for this syscall
1717 between syscall enter/return, and we'll need to know to
1718 report a syscall return if that happens. */
1719 lp
->syscall_state
= (lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1720 ? TARGET_WAITKIND_SYSCALL_RETURN
1721 : TARGET_WAITKIND_SYSCALL_ENTRY
);
1723 if (catching_syscall_number (syscall_number
))
1725 /* Alright, an event to report. */
1726 ourstatus
->kind
= lp
->syscall_state
;
1727 ourstatus
->value
.syscall_number
= syscall_number
;
1729 if (debug_linux_nat
)
1730 fprintf_unfiltered (gdb_stdlog
,
1731 "LHST: stopping for %s of syscall %d"
1734 == TARGET_WAITKIND_SYSCALL_ENTRY
1735 ? "entry" : "return",
1737 ptid_get_lwp (lp
->ptid
));
1741 if (debug_linux_nat
)
1742 fprintf_unfiltered (gdb_stdlog
,
1743 "LHST: ignoring %s of syscall %d "
1745 lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1746 ? "entry" : "return",
1748 ptid_get_lwp (lp
->ptid
));
1752 /* If we had been syscall tracing, and hence used PT_SYSCALL
1753 before on this LWP, it could happen that the user removes all
1754 syscall catchpoints before we get to process this event.
1755 There are two noteworthy issues here:
1757 - When stopped at a syscall entry event, resuming with
1758 PT_STEP still resumes executing the syscall and reports a
1761 - Only PT_SYSCALL catches syscall enters. If we last
1762 single-stepped this thread, then this event can't be a
1763 syscall enter. If we last single-stepped this thread, this
1764 has to be a syscall exit.
1766 The points above mean that the next resume, be it PT_STEP or
1767 PT_CONTINUE, can not trigger a syscall trace event. */
1768 if (debug_linux_nat
)
1769 fprintf_unfiltered (gdb_stdlog
,
1770 "LHST: caught syscall event "
1771 "with no syscall catchpoints."
1772 " %d for LWP %ld, ignoring\n",
1774 ptid_get_lwp (lp
->ptid
));
1775 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
1778 /* The core isn't interested in this event. For efficiency, avoid
1779 stopping all threads only to have the core resume them all again.
1780 Since we're not stopping threads, if we're still syscall tracing
1781 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
1782 subsequent syscall. Simply resume using the inf-ptrace layer,
1783 which knows when to use PT_SYSCALL or PT_CONTINUE. */
1785 /* Note that gdbarch_get_syscall_number may access registers, hence
1787 registers_changed ();
1788 if (linux_nat_prepare_to_resume
!= NULL
)
1789 linux_nat_prepare_to_resume (lp
);
1790 linux_ops
->to_resume (linux_ops
, pid_to_ptid (ptid_get_lwp (lp
->ptid
)),
1791 lp
->step
, GDB_SIGNAL_0
);
1796 /* Handle a GNU/Linux extended wait response. If we see a clone
1797 event, we need to add the new LWP to our list (and not report the
1798 trap to higher layers). This function returns non-zero if the
1799 event should be ignored and we should wait again. If STOPPING is
1800 true, the new LWP remains stopped, otherwise it is continued. */
1803 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
1806 int pid
= ptid_get_lwp (lp
->ptid
);
1807 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1808 int event
= linux_ptrace_get_extended_event (status
);
1810 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1811 || event
== PTRACE_EVENT_CLONE
)
1813 unsigned long new_pid
;
1816 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1818 /* If we haven't already seen the new PID stop, wait for it now. */
1819 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1821 /* The new child has a pending SIGSTOP. We can't affect it until it
1822 hits the SIGSTOP, but we're already attached. */
1823 ret
= my_waitpid (new_pid
, &status
,
1824 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
1826 perror_with_name (_("waiting for new child"));
1827 else if (ret
!= new_pid
)
1828 internal_error (__FILE__
, __LINE__
,
1829 _("wait returned unexpected PID %d"), ret
);
1830 else if (!WIFSTOPPED (status
))
1831 internal_error (__FILE__
, __LINE__
,
1832 _("wait returned unexpected status 0x%x"), status
);
1835 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
1837 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
1839 /* The arch-specific native code may need to know about new
1840 forks even if those end up never mapped to an
1842 if (linux_nat_new_fork
!= NULL
)
1843 linux_nat_new_fork (lp
, new_pid
);
1846 if (event
== PTRACE_EVENT_FORK
1847 && linux_fork_checkpointing_p (ptid_get_pid (lp
->ptid
)))
1849 /* Handle checkpointing by linux-fork.c here as a special
1850 case. We don't want the follow-fork-mode or 'catch fork'
1851 to interfere with this. */
1853 /* This won't actually modify the breakpoint list, but will
1854 physically remove the breakpoints from the child. */
1855 detach_breakpoints (ptid_build (new_pid
, new_pid
, 0));
1857 /* Retain child fork in ptrace (stopped) state. */
1858 if (!find_fork_pid (new_pid
))
1861 /* Report as spurious, so that infrun doesn't want to follow
1862 this fork. We're actually doing an infcall in
1864 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1866 /* Report the stop to the core. */
1870 if (event
== PTRACE_EVENT_FORK
)
1871 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1872 else if (event
== PTRACE_EVENT_VFORK
)
1873 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1876 struct lwp_info
*new_lp
;
1878 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1880 if (debug_linux_nat
)
1881 fprintf_unfiltered (gdb_stdlog
,
1882 "LHEW: Got clone event "
1883 "from LWP %d, new child is LWP %ld\n",
1886 new_lp
= add_lwp (ptid_build (ptid_get_pid (lp
->ptid
), new_pid
, 0));
1888 new_lp
->stopped
= 1;
1890 if (WSTOPSIG (status
) != SIGSTOP
)
1892 /* This can happen if someone starts sending signals to
1893 the new thread before it gets a chance to run, which
1894 have a lower number than SIGSTOP (e.g. SIGUSR1).
1895 This is an unlikely case, and harder to handle for
1896 fork / vfork than for clone, so we do not try - but
1897 we handle it for clone events here. We'll send
1898 the other signal on to the thread below. */
1900 new_lp
->signalled
= 1;
1904 struct thread_info
*tp
;
1906 /* When we stop for an event in some other thread, and
1907 pull the thread list just as this thread has cloned,
1908 we'll have seen the new thread in the thread_db list
1909 before handling the CLONE event (glibc's
1910 pthread_create adds the new thread to the thread list
1911 before clone'ing, and has the kernel fill in the
1912 thread's tid on the clone call with
1913 CLONE_PARENT_SETTID). If that happened, and the core
1914 had requested the new thread to stop, we'll have
1915 killed it with SIGSTOP. But since SIGSTOP is not an
1916 RT signal, it can only be queued once. We need to be
1917 careful to not resume the LWP if we wanted it to
1918 stop. In that case, we'll leave the SIGSTOP pending.
1919 It will later be reported as GDB_SIGNAL_0. */
1920 tp
= find_thread_ptid (new_lp
->ptid
);
1921 if (tp
!= NULL
&& tp
->stop_requested
)
1922 new_lp
->last_resume_kind
= resume_stop
;
1929 /* Add the new thread to GDB's lists as soon as possible
1932 1) the frontend doesn't have to wait for a stop to
1935 2) we tag it with the correct running state. */
1937 /* If the thread_db layer is active, let it know about
1938 this new thread, and add it to GDB's list. */
1939 if (!thread_db_attach_lwp (new_lp
->ptid
))
1941 /* We're not using thread_db. Add it to GDB's
1943 target_post_attach (ptid_get_lwp (new_lp
->ptid
));
1944 add_thread (new_lp
->ptid
);
1949 set_running (new_lp
->ptid
, 1);
1950 set_executing (new_lp
->ptid
, 1);
1951 /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
1953 new_lp
->last_resume_kind
= resume_continue
;
1959 /* We created NEW_LP so it cannot yet contain STATUS. */
1960 gdb_assert (new_lp
->status
== 0);
1962 /* Save the wait status to report later. */
1963 if (debug_linux_nat
)
1964 fprintf_unfiltered (gdb_stdlog
,
1965 "LHEW: waitpid of new LWP %ld, "
1966 "saving status %s\n",
1967 (long) ptid_get_lwp (new_lp
->ptid
),
1968 status_to_str (status
));
1969 new_lp
->status
= status
;
1972 /* Note the need to use the low target ops to resume, to
1973 handle resuming with PT_SYSCALL if we have syscall
1977 new_lp
->resumed
= 1;
1981 gdb_assert (new_lp
->last_resume_kind
== resume_continue
);
1982 if (debug_linux_nat
)
1983 fprintf_unfiltered (gdb_stdlog
,
1984 "LHEW: resuming new LWP %ld\n",
1985 ptid_get_lwp (new_lp
->ptid
));
1986 if (linux_nat_prepare_to_resume
!= NULL
)
1987 linux_nat_prepare_to_resume (new_lp
);
1988 linux_ops
->to_resume (linux_ops
, pid_to_ptid (new_pid
),
1990 new_lp
->stopped
= 0;
1994 if (debug_linux_nat
)
1995 fprintf_unfiltered (gdb_stdlog
,
1996 "LHEW: resuming parent LWP %d\n", pid
);
1997 if (linux_nat_prepare_to_resume
!= NULL
)
1998 linux_nat_prepare_to_resume (lp
);
1999 linux_ops
->to_resume (linux_ops
,
2000 pid_to_ptid (ptid_get_lwp (lp
->ptid
)),
2009 if (event
== PTRACE_EVENT_EXEC
)
2011 if (debug_linux_nat
)
2012 fprintf_unfiltered (gdb_stdlog
,
2013 "LHEW: Got exec event from LWP %ld\n",
2014 ptid_get_lwp (lp
->ptid
));
2016 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
2017 ourstatus
->value
.execd_pathname
2018 = xstrdup (linux_child_pid_to_exec_file (NULL
, pid
));
2023 if (event
== PTRACE_EVENT_VFORK_DONE
)
2025 if (current_inferior ()->waiting_for_vfork_done
)
2027 if (debug_linux_nat
)
2028 fprintf_unfiltered (gdb_stdlog
,
2029 "LHEW: Got expected PTRACE_EVENT_"
2030 "VFORK_DONE from LWP %ld: stopping\n",
2031 ptid_get_lwp (lp
->ptid
));
2033 ourstatus
->kind
= TARGET_WAITKIND_VFORK_DONE
;
2037 if (debug_linux_nat
)
2038 fprintf_unfiltered (gdb_stdlog
,
2039 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2040 "from LWP %ld: resuming\n",
2041 ptid_get_lwp (lp
->ptid
));
2042 ptrace (PTRACE_CONT
, ptid_get_lwp (lp
->ptid
), 0, 0);
2046 internal_error (__FILE__
, __LINE__
,
2047 _("unknown ptrace event %d"), event
);
2050 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2054 wait_lwp (struct lwp_info
*lp
)
2058 int thread_dead
= 0;
2061 gdb_assert (!lp
->stopped
);
2062 gdb_assert (lp
->status
== 0);
2064 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2065 block_child_signals (&prev_mask
);
2069 /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2070 was right and we should just call sigsuspend. */
2072 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), &status
, WNOHANG
);
2073 if (pid
== -1 && errno
== ECHILD
)
2074 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), &status
, __WCLONE
| WNOHANG
);
2075 if (pid
== -1 && errno
== ECHILD
)
2077 /* The thread has previously exited. We need to delete it
2078 now because, for some vendor 2.4 kernels with NPTL
2079 support backported, there won't be an exit event unless
2080 it is the main thread. 2.6 kernels will report an exit
2081 event for each thread that exits, as expected. */
2083 if (debug_linux_nat
)
2084 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2085 target_pid_to_str (lp
->ptid
));
2090 /* Bugs 10970, 12702.
2091 Thread group leader may have exited in which case we'll lock up in
2092 waitpid if there are other threads, even if they are all zombies too.
2093 Basically, we're not supposed to use waitpid this way.
2094 __WCLONE is not applicable for the leader so we can't use that.
2095 LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2096 process; it gets ESRCH both for the zombie and for running processes.
2098 As a workaround, check if we're waiting for the thread group leader and
2099 if it's a zombie, and avoid calling waitpid if it is.
2101 This is racy, what if the tgl becomes a zombie right after we check?
2102 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2103 waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
2105 if (ptid_get_pid (lp
->ptid
) == ptid_get_lwp (lp
->ptid
)
2106 && linux_proc_pid_is_zombie (ptid_get_lwp (lp
->ptid
)))
2109 if (debug_linux_nat
)
2110 fprintf_unfiltered (gdb_stdlog
,
2111 "WL: Thread group leader %s vanished.\n",
2112 target_pid_to_str (lp
->ptid
));
2116 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2117 get invoked despite our caller had them intentionally blocked by
2118 block_child_signals. This is sensitive only to the loop of
2119 linux_nat_wait_1 and there if we get called my_waitpid gets called
2120 again before it gets to sigsuspend so we can safely let the handlers
2121 get executed here. */
2123 if (debug_linux_nat
)
2124 fprintf_unfiltered (gdb_stdlog
, "WL: about to sigsuspend\n");
2125 sigsuspend (&suspend_mask
);
2128 restore_child_signals_mask (&prev_mask
);
2132 gdb_assert (pid
== ptid_get_lwp (lp
->ptid
));
2134 if (debug_linux_nat
)
2136 fprintf_unfiltered (gdb_stdlog
,
2137 "WL: waitpid %s received %s\n",
2138 target_pid_to_str (lp
->ptid
),
2139 status_to_str (status
));
2142 /* Check if the thread has exited. */
2143 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2146 if (debug_linux_nat
)
2147 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2148 target_pid_to_str (lp
->ptid
));
2158 gdb_assert (WIFSTOPPED (status
));
2161 /* Handle GNU/Linux's syscall SIGTRAPs. */
2162 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2164 /* No longer need the sysgood bit. The ptrace event ends up
2165 recorded in lp->waitstatus if we care for it. We can carry
2166 on handling the event like a regular SIGTRAP from here
2168 status
= W_STOPCODE (SIGTRAP
);
2169 if (linux_handle_syscall_trap (lp
, 1))
2170 return wait_lwp (lp
);
2173 /* Handle GNU/Linux's extended waitstatus for trace events. */
2174 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
2175 && linux_is_extended_waitstatus (status
))
2177 if (debug_linux_nat
)
2178 fprintf_unfiltered (gdb_stdlog
,
2179 "WL: Handling extended status 0x%06x\n",
2181 if (linux_handle_extended_wait (lp
, status
, 1))
2182 return wait_lwp (lp
);
2188 /* Send a SIGSTOP to LP. */
2191 stop_callback (struct lwp_info
*lp
, void *data
)
2193 if (!lp
->stopped
&& !lp
->signalled
)
2197 if (debug_linux_nat
)
2199 fprintf_unfiltered (gdb_stdlog
,
2200 "SC: kill %s **<SIGSTOP>**\n",
2201 target_pid_to_str (lp
->ptid
));
2204 ret
= kill_lwp (ptid_get_lwp (lp
->ptid
), SIGSTOP
);
2205 if (debug_linux_nat
)
2207 fprintf_unfiltered (gdb_stdlog
,
2208 "SC: lwp kill %d %s\n",
2210 errno
? safe_strerror (errno
) : "ERRNO-OK");
2214 gdb_assert (lp
->status
== 0);
2220 /* Request a stop on LWP. */
2223 linux_stop_lwp (struct lwp_info
*lwp
)
2225 stop_callback (lwp
, NULL
);
2228 /* Return non-zero if LWP PID has a pending SIGINT. */
2231 linux_nat_has_pending_sigint (int pid
)
2233 sigset_t pending
, blocked
, ignored
;
2235 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2237 if (sigismember (&pending
, SIGINT
)
2238 && !sigismember (&ignored
, SIGINT
))
2244 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2247 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2249 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2250 flag to consume the next one. */
2251 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2252 && WSTOPSIG (lp
->status
) == SIGINT
)
2255 lp
->ignore_sigint
= 1;
2260 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2261 This function is called after we know the LWP has stopped; if the LWP
2262 stopped before the expected SIGINT was delivered, then it will never have
2263 arrived. Also, if the signal was delivered to a shared queue and consumed
2264 by a different thread, it will never be delivered to this LWP. */
2267 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2269 if (!lp
->ignore_sigint
)
2272 if (!linux_nat_has_pending_sigint (ptid_get_lwp (lp
->ptid
)))
2274 if (debug_linux_nat
)
2275 fprintf_unfiltered (gdb_stdlog
,
2276 "MCIS: Clearing bogus flag for %s\n",
2277 target_pid_to_str (lp
->ptid
));
2278 lp
->ignore_sigint
= 0;
2282 /* Fetch the possible triggered data watchpoint info and store it in
2285 On some archs, like x86, that use debug registers to set
2286 watchpoints, it's possible that the way to know which watched
2287 address trapped, is to check the register that is used to select
2288 which address to watch. Problem is, between setting the watchpoint
2289 and reading back which data address trapped, the user may change
2290 the set of watchpoints, and, as a consequence, GDB changes the
2291 debug registers in the inferior. To avoid reading back a stale
2292 stopped-data-address when that happens, we cache in LP the fact
2293 that a watchpoint trapped, and the corresponding data address, as
2294 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2295 registers meanwhile, we have the cached data we can rely on. */
2298 save_sigtrap (struct lwp_info
*lp
)
2300 struct cleanup
*old_chain
;
2302 if (linux_ops
->to_stopped_by_watchpoint
== NULL
)
2304 lp
->stopped_by_watchpoint
= 0;
2308 old_chain
= save_inferior_ptid ();
2309 inferior_ptid
= lp
->ptid
;
2311 lp
->stopped_by_watchpoint
= linux_ops
->to_stopped_by_watchpoint (linux_ops
);
2313 if (lp
->stopped_by_watchpoint
)
2315 if (linux_ops
->to_stopped_data_address
!= NULL
)
2316 lp
->stopped_data_address_p
=
2317 linux_ops
->to_stopped_data_address (¤t_target
,
2318 &lp
->stopped_data_address
);
2320 lp
->stopped_data_address_p
= 0;
2323 do_cleanups (old_chain
);
2326 /* See save_sigtrap. */
2329 linux_nat_stopped_by_watchpoint (struct target_ops
*ops
)
2331 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2333 gdb_assert (lp
!= NULL
);
2335 return lp
->stopped_by_watchpoint
;
2339 linux_nat_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
2341 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2343 gdb_assert (lp
!= NULL
);
2345 *addr_p
= lp
->stopped_data_address
;
2347 return lp
->stopped_data_address_p
;
2350 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2353 sigtrap_is_event (int status
)
2355 return WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
;
2358 /* SIGTRAP-like events recognizer. */
2360 static int (*linux_nat_status_is_event
) (int status
) = sigtrap_is_event
;
2362 /* Check for SIGTRAP-like events in LP. */
2365 linux_nat_lp_status_is_event (struct lwp_info
*lp
)
2367 /* We check for lp->waitstatus in addition to lp->status, because we can
2368 have pending process exits recorded in lp->status
2369 and W_EXITCODE(0,0) == 0. We should probably have an additional
2370 lp->status_p flag. */
2372 return (lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
2373 && linux_nat_status_is_event (lp
->status
));
2376 /* Set alternative SIGTRAP-like events recognizer. If
2377 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2381 linux_nat_set_status_is_event (struct target_ops
*t
,
2382 int (*status_is_event
) (int status
))
2384 linux_nat_status_is_event
= status_is_event
;
2387 /* Wait until LP is stopped. */
2390 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2392 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
2394 /* If this is a vfork parent, bail out, it is not going to report
2395 any SIGSTOP until the vfork is done with. */
2396 if (inf
->vfork_child
!= NULL
)
2403 status
= wait_lwp (lp
);
2407 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2408 && WSTOPSIG (status
) == SIGINT
)
2410 lp
->ignore_sigint
= 0;
2413 ptrace (PTRACE_CONT
, ptid_get_lwp (lp
->ptid
), 0, 0);
2415 if (debug_linux_nat
)
2416 fprintf_unfiltered (gdb_stdlog
,
2417 "PTRACE_CONT %s, 0, 0 (%s) "
2418 "(discarding SIGINT)\n",
2419 target_pid_to_str (lp
->ptid
),
2420 errno
? safe_strerror (errno
) : "OK");
2422 return stop_wait_callback (lp
, NULL
);
2425 maybe_clear_ignore_sigint (lp
);
2427 if (WSTOPSIG (status
) != SIGSTOP
)
2429 /* The thread was stopped with a signal other than SIGSTOP. */
2433 if (debug_linux_nat
)
2434 fprintf_unfiltered (gdb_stdlog
,
2435 "SWC: Pending event %s in %s\n",
2436 status_to_str ((int) status
),
2437 target_pid_to_str (lp
->ptid
));
2439 /* Save the sigtrap event. */
2440 lp
->status
= status
;
2441 gdb_assert (lp
->signalled
);
2445 /* We caught the SIGSTOP that we intended to catch, so
2446 there's no SIGSTOP pending. */
2448 if (debug_linux_nat
)
2449 fprintf_unfiltered (gdb_stdlog
,
2450 "SWC: Delayed SIGSTOP caught for %s.\n",
2451 target_pid_to_str (lp
->ptid
));
2453 /* Reset SIGNALLED only after the stop_wait_callback call
2454 above as it does gdb_assert on SIGNALLED. */
2462 /* Return non-zero if LP has a wait status pending. */
2465 status_callback (struct lwp_info
*lp
, void *data
)
2467 /* Only report a pending wait status if we pretend that this has
2468 indeed been resumed. */
2472 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2474 /* A ptrace event, like PTRACE_FORK|VFORK|EXEC, syscall event,
2475 or a pending process exit. Note that `W_EXITCODE(0,0) ==
2476 0', so a clean process exit can not be stored pending in
2477 lp->status, it is indistinguishable from
2478 no-pending-status. */
2482 if (lp
->status
!= 0)
2488 /* Return non-zero if LP isn't stopped. */
2491 running_callback (struct lwp_info
*lp
, void *data
)
2493 return (!lp
->stopped
2494 || ((lp
->status
!= 0
2495 || lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2499 /* Count the LWP's that have had events. */
2502 count_events_callback (struct lwp_info
*lp
, void *data
)
2506 gdb_assert (count
!= NULL
);
2508 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2509 if (lp
->resumed
&& linux_nat_lp_status_is_event (lp
))
2515 /* Select the LWP (if any) that is currently being single-stepped. */
2518 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2520 if (lp
->last_resume_kind
== resume_step
2527 /* Select the Nth LWP that has had a SIGTRAP event. */
2530 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2532 int *selector
= data
;
2534 gdb_assert (selector
!= NULL
);
2536 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2537 if (lp
->resumed
&& linux_nat_lp_status_is_event (lp
))
2538 if ((*selector
)-- == 0)
2545 cancel_breakpoint (struct lwp_info
*lp
)
2547 /* Arrange for a breakpoint to be hit again later. We don't keep
2548 the SIGTRAP status and don't forward the SIGTRAP signal to the
2549 LWP. We will handle the current event, eventually we will resume
2550 this LWP, and this breakpoint will trap again.
2552 If we do not do this, then we run the risk that the user will
2553 delete or disable the breakpoint, but the LWP will have already
2556 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2557 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2560 pc
= regcache_read_pc (regcache
) - target_decr_pc_after_break (gdbarch
);
2561 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
2563 if (debug_linux_nat
)
2564 fprintf_unfiltered (gdb_stdlog
,
2565 "CB: Push back breakpoint for %s\n",
2566 target_pid_to_str (lp
->ptid
));
2568 /* Back up the PC if necessary. */
2569 if (target_decr_pc_after_break (gdbarch
))
2570 regcache_write_pc (regcache
, pc
);
2578 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
2580 struct lwp_info
*event_lp
= data
;
2582 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2586 /* If a LWP other than the LWP that we're reporting an event for has
2587 hit a GDB breakpoint (as opposed to some random trap signal),
2588 then just arrange for it to hit it again later. We don't keep
2589 the SIGTRAP status and don't forward the SIGTRAP signal to the
2590 LWP. We will handle the current event, eventually we will resume
2591 all LWPs, and this one will get its breakpoint trap again.
2593 If we do not do this, then we run the risk that the user will
2594 delete or disable the breakpoint, but the LWP will have already
2597 if (linux_nat_lp_status_is_event (lp
)
2598 && cancel_breakpoint (lp
))
2599 /* Throw away the SIGTRAP. */
2605 /* Select one LWP out of those that have events pending. */
2608 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
2611 int random_selector
;
2612 struct lwp_info
*event_lp
;
2614 /* Record the wait status for the original LWP. */
2615 (*orig_lp
)->status
= *status
;
2617 /* Give preference to any LWP that is being single-stepped. */
2618 event_lp
= iterate_over_lwps (filter
,
2619 select_singlestep_lwp_callback
, NULL
);
2620 if (event_lp
!= NULL
)
2622 if (debug_linux_nat
)
2623 fprintf_unfiltered (gdb_stdlog
,
2624 "SEL: Select single-step %s\n",
2625 target_pid_to_str (event_lp
->ptid
));
2629 /* No single-stepping LWP. Select one at random, out of those
2630 which have had SIGTRAP events. */
2632 /* First see how many SIGTRAP events we have. */
2633 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
2635 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2636 random_selector
= (int)
2637 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2639 if (debug_linux_nat
&& num_events
> 1)
2640 fprintf_unfiltered (gdb_stdlog
,
2641 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2642 num_events
, random_selector
);
2644 event_lp
= iterate_over_lwps (filter
,
2645 select_event_lwp_callback
,
2649 if (event_lp
!= NULL
)
2651 /* Switch the event LWP. */
2652 *orig_lp
= event_lp
;
2653 *status
= event_lp
->status
;
2656 /* Flush the wait status for the event LWP. */
2657 (*orig_lp
)->status
= 0;
2660 /* Return non-zero if LP has been resumed. */
2663 resumed_callback (struct lwp_info
*lp
, void *data
)
2668 /* Stop an active thread, verify it still exists, then resume it. If
2669 the thread ends up with a pending status, then it is not resumed,
2670 and *DATA (really a pointer to int), is set. */
2673 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2675 int *new_pending_p
= data
;
2679 ptid_t ptid
= lp
->ptid
;
2681 stop_callback (lp
, NULL
);
2682 stop_wait_callback (lp
, NULL
);
2684 /* Resume if the lwp still exists, and the core wanted it
2686 lp
= find_lwp_pid (ptid
);
2689 if (lp
->last_resume_kind
== resume_stop
2692 /* The core wanted the LWP to stop. Even if it stopped
2693 cleanly (with SIGSTOP), leave the event pending. */
2694 if (debug_linux_nat
)
2695 fprintf_unfiltered (gdb_stdlog
,
2696 "SARC: core wanted LWP %ld stopped "
2697 "(leaving SIGSTOP pending)\n",
2698 ptid_get_lwp (lp
->ptid
));
2699 lp
->status
= W_STOPCODE (SIGSTOP
);
2702 if (lp
->status
== 0)
2704 if (debug_linux_nat
)
2705 fprintf_unfiltered (gdb_stdlog
,
2706 "SARC: re-resuming LWP %ld\n",
2707 ptid_get_lwp (lp
->ptid
));
2708 resume_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
2712 if (debug_linux_nat
)
2713 fprintf_unfiltered (gdb_stdlog
,
2714 "SARC: not re-resuming LWP %ld "
2716 ptid_get_lwp (lp
->ptid
));
2725 /* Check if we should go on and pass this event to common code.
2726 Return the affected lwp if we are, or NULL otherwise. If we stop
2727 all lwps temporarily, we may end up with new pending events in some
2728 other lwp. In that case set *NEW_PENDING_P to true. */
2730 static struct lwp_info
*
2731 linux_nat_filter_event (int lwpid
, int status
, int *new_pending_p
)
2733 struct lwp_info
*lp
;
2734 int event
= linux_ptrace_get_extended_event (status
);
2738 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2740 /* Check for stop events reported by a process we didn't already
2741 know about - anything not already in our LWP list.
2743 If we're expecting to receive stopped processes after
2744 fork, vfork, and clone events, then we'll just add the
2745 new one to our list and go back to waiting for the event
2746 to be reported - the stopped process might be returned
2747 from waitpid before or after the event is.
2749 But note the case of a non-leader thread exec'ing after the
2750 leader having exited, and gone from our lists. The non-leader
2751 thread changes its tid to the tgid. */
2753 if (WIFSTOPPED (status
) && lp
== NULL
2754 && (WSTOPSIG (status
) == SIGTRAP
&& event
== PTRACE_EVENT_EXEC
))
2756 /* A multi-thread exec after we had seen the leader exiting. */
2757 if (debug_linux_nat
)
2758 fprintf_unfiltered (gdb_stdlog
,
2759 "LLW: Re-adding thread group leader LWP %d.\n",
2762 lp
= add_lwp (ptid_build (lwpid
, lwpid
, 0));
2765 add_thread (lp
->ptid
);
2768 if (WIFSTOPPED (status
) && !lp
)
2770 add_to_pid_list (&stopped_pids
, lwpid
, status
);
2774 /* Make sure we don't report an event for the exit of an LWP not in
2775 our list, i.e. not part of the current process. This can happen
2776 if we detach from a program we originally forked and then it
2778 if (!WIFSTOPPED (status
) && !lp
)
2781 /* This LWP is stopped now. (And if dead, this prevents it from
2782 ever being continued.) */
2785 /* Handle GNU/Linux's syscall SIGTRAPs. */
2786 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2788 /* No longer need the sysgood bit. The ptrace event ends up
2789 recorded in lp->waitstatus if we care for it. We can carry
2790 on handling the event like a regular SIGTRAP from here
2792 status
= W_STOPCODE (SIGTRAP
);
2793 if (linux_handle_syscall_trap (lp
, 0))
2797 /* Handle GNU/Linux's extended waitstatus for trace events. */
2798 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
2799 && linux_is_extended_waitstatus (status
))
2801 if (debug_linux_nat
)
2802 fprintf_unfiltered (gdb_stdlog
,
2803 "LLW: Handling extended status 0x%06x\n",
2805 if (linux_handle_extended_wait (lp
, status
, 0))
2809 if (linux_nat_status_is_event (status
))
2812 /* Check if the thread has exited. */
2813 if ((WIFEXITED (status
) || WIFSIGNALED (status
))
2814 && num_lwps (ptid_get_pid (lp
->ptid
)) > 1)
2816 /* If this is the main thread, we must stop all threads and verify
2817 if they are still alive. This is because in the nptl thread model
2818 on Linux 2.4, there is no signal issued for exiting LWPs
2819 other than the main thread. We only get the main thread exit
2820 signal once all child threads have already exited. If we
2821 stop all the threads and use the stop_wait_callback to check
2822 if they have exited we can determine whether this signal
2823 should be ignored or whether it means the end of the debugged
2824 application, regardless of which threading model is being
2826 if (ptid_get_pid (lp
->ptid
) == ptid_get_lwp (lp
->ptid
))
2828 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp
->ptid
)),
2829 stop_and_resume_callback
, new_pending_p
);
2832 if (debug_linux_nat
)
2833 fprintf_unfiltered (gdb_stdlog
,
2834 "LLW: %s exited.\n",
2835 target_pid_to_str (lp
->ptid
));
2837 if (num_lwps (ptid_get_pid (lp
->ptid
)) > 1)
2839 /* If there is at least one more LWP, then the exit signal
2840 was not the end of the debugged application and should be
2847 /* Check if the current LWP has previously exited. In the nptl
2848 thread model, LWPs other than the main thread do not issue
2849 signals when they exit so we must check whenever the thread has
2850 stopped. A similar check is made in stop_wait_callback(). */
2851 if (num_lwps (ptid_get_pid (lp
->ptid
)) > 1 && !linux_thread_alive (lp
->ptid
))
2853 ptid_t ptid
= pid_to_ptid (ptid_get_pid (lp
->ptid
));
2855 if (debug_linux_nat
)
2856 fprintf_unfiltered (gdb_stdlog
,
2857 "LLW: %s exited.\n",
2858 target_pid_to_str (lp
->ptid
));
2862 /* Make sure there is at least one thread running. */
2863 gdb_assert (iterate_over_lwps (ptid
, running_callback
, NULL
));
2865 /* Discard the event. */
2869 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2870 an attempt to stop an LWP. */
2872 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2874 if (debug_linux_nat
)
2875 fprintf_unfiltered (gdb_stdlog
,
2876 "LLW: Delayed SIGSTOP caught for %s.\n",
2877 target_pid_to_str (lp
->ptid
));
2881 if (lp
->last_resume_kind
!= resume_stop
)
2883 /* This is a delayed SIGSTOP. */
2885 registers_changed ();
2887 if (linux_nat_prepare_to_resume
!= NULL
)
2888 linux_nat_prepare_to_resume (lp
);
2889 linux_ops
->to_resume (linux_ops
,
2890 pid_to_ptid (ptid_get_lwp (lp
->ptid
)),
2891 lp
->step
, GDB_SIGNAL_0
);
2892 if (debug_linux_nat
)
2893 fprintf_unfiltered (gdb_stdlog
,
2894 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2896 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2897 target_pid_to_str (lp
->ptid
));
2900 gdb_assert (lp
->resumed
);
2902 /* Discard the event. */
2907 /* Make sure we don't report a SIGINT that we have already displayed
2908 for another thread. */
2909 if (lp
->ignore_sigint
2910 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
2912 if (debug_linux_nat
)
2913 fprintf_unfiltered (gdb_stdlog
,
2914 "LLW: Delayed SIGINT caught for %s.\n",
2915 target_pid_to_str (lp
->ptid
));
2917 /* This is a delayed SIGINT. */
2918 lp
->ignore_sigint
= 0;
2920 registers_changed ();
2921 if (linux_nat_prepare_to_resume
!= NULL
)
2922 linux_nat_prepare_to_resume (lp
);
2923 linux_ops
->to_resume (linux_ops
, pid_to_ptid (ptid_get_lwp (lp
->ptid
)),
2924 lp
->step
, GDB_SIGNAL_0
);
2925 if (debug_linux_nat
)
2926 fprintf_unfiltered (gdb_stdlog
,
2927 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2929 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2930 target_pid_to_str (lp
->ptid
));
2933 gdb_assert (lp
->resumed
);
2935 /* Discard the event. */
2939 /* An interesting event. */
2941 lp
->status
= status
;
2945 /* Detect zombie thread group leaders, and "exit" them. We can't reap
2946 their exits until all other threads in the group have exited. */
2949 check_zombie_leaders (void)
2951 struct inferior
*inf
;
2955 struct lwp_info
*leader_lp
;
2960 leader_lp
= find_lwp_pid (pid_to_ptid (inf
->pid
));
2961 if (leader_lp
!= NULL
2962 /* Check if there are other threads in the group, as we may
2963 have raced with the inferior simply exiting. */
2964 && num_lwps (inf
->pid
) > 1
2965 && linux_proc_pid_is_zombie (inf
->pid
))
2967 if (debug_linux_nat
)
2968 fprintf_unfiltered (gdb_stdlog
,
2969 "CZL: Thread group leader %d zombie "
2970 "(it exited, or another thread execd).\n",
2973 /* A leader zombie can mean one of two things:
2975 - It exited, and there's an exit status pending
2976 available, or only the leader exited (not the whole
2977 program). In the latter case, we can't waitpid the
2978 leader's exit status until all other threads are gone.
2980 - There are 3 or more threads in the group, and a thread
2981 other than the leader exec'd. On an exec, the Linux
2982 kernel destroys all other threads (except the execing
2983 one) in the thread group, and resets the execing thread's
2984 tid to the tgid. No exit notification is sent for the
2985 execing thread -- from the ptracer's perspective, it
2986 appears as though the execing thread just vanishes.
2987 Until we reap all other threads except the leader and the
2988 execing thread, the leader will be zombie, and the
2989 execing thread will be in `D (disc sleep)'. As soon as
2990 all other threads are reaped, the execing thread changes
2991 it's tid to the tgid, and the previous (zombie) leader
2992 vanishes, giving place to the "new" leader. We could try
2993 distinguishing the exit and exec cases, by waiting once
2994 more, and seeing if something comes out, but it doesn't
2995 sound useful. The previous leader _does_ go away, and
2996 we'll re-add the new one once we see the exec event
2997 (which is just the same as what would happen if the
2998 previous leader did exit voluntarily before some other
3001 if (debug_linux_nat
)
3002 fprintf_unfiltered (gdb_stdlog
,
3003 "CZL: Thread group leader %d vanished.\n",
3005 exit_lwp (leader_lp
);
3011 linux_nat_wait_1 (struct target_ops
*ops
,
3012 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3015 static sigset_t prev_mask
;
3016 enum resume_kind last_resume_kind
;
3017 struct lwp_info
*lp
;
3020 if (debug_linux_nat
)
3021 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
3023 /* The first time we get here after starting a new inferior, we may
3024 not have added it to the LWP list yet - this is the earliest
3025 moment at which we know its PID. */
3026 if (ptid_is_pid (inferior_ptid
))
3028 /* Upgrade the main thread's ptid. */
3029 thread_change_ptid (inferior_ptid
,
3030 ptid_build (ptid_get_pid (inferior_ptid
),
3031 ptid_get_pid (inferior_ptid
), 0));
3033 lp
= add_initial_lwp (inferior_ptid
);
3037 /* Make sure SIGCHLD is blocked until the sigsuspend below. */
3038 block_child_signals (&prev_mask
);
3044 /* First check if there is a LWP with a wait status pending. */
3045 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3047 /* Any LWP in the PTID group that's been resumed will do. */
3048 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3051 if (debug_linux_nat
&& lp
->status
)
3052 fprintf_unfiltered (gdb_stdlog
,
3053 "LLW: Using pending wait status %s for %s.\n",
3054 status_to_str (lp
->status
),
3055 target_pid_to_str (lp
->ptid
));
3058 else if (ptid_lwp_p (ptid
))
3060 if (debug_linux_nat
)
3061 fprintf_unfiltered (gdb_stdlog
,
3062 "LLW: Waiting for specific LWP %s.\n",
3063 target_pid_to_str (ptid
));
3065 /* We have a specific LWP to check. */
3066 lp
= find_lwp_pid (ptid
);
3069 if (debug_linux_nat
&& lp
->status
)
3070 fprintf_unfiltered (gdb_stdlog
,
3071 "LLW: Using pending wait status %s for %s.\n",
3072 status_to_str (lp
->status
),
3073 target_pid_to_str (lp
->ptid
));
3075 /* We check for lp->waitstatus in addition to lp->status,
3076 because we can have pending process exits recorded in
3077 lp->status and W_EXITCODE(0,0) == 0. We should probably have
3078 an additional lp->status_p flag. */
3079 if (lp
->status
== 0 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
3083 if (!target_can_async_p ())
3085 /* Causes SIGINT to be passed on to the attached process. */
3089 /* But if we don't find a pending event, we'll have to wait. */
3095 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3098 - If the thread group leader exits while other threads in the
3099 thread group still exist, waitpid(TGID, ...) hangs. That
3100 waitpid won't return an exit status until the other threads
3101 in the group are reapped.
3103 - When a non-leader thread execs, that thread just vanishes
3104 without reporting an exit (so we'd hang if we waited for it
3105 explicitly in that case). The exec event is reported to
3109 lwpid
= my_waitpid (-1, &status
, __WCLONE
| WNOHANG
);
3110 if (lwpid
== 0 || (lwpid
== -1 && errno
== ECHILD
))
3111 lwpid
= my_waitpid (-1, &status
, WNOHANG
);
3113 if (debug_linux_nat
)
3114 fprintf_unfiltered (gdb_stdlog
,
3115 "LNW: waitpid(-1, ...) returned %d, %s\n",
3116 lwpid
, errno
? safe_strerror (errno
) : "ERRNO-OK");
3120 /* If this is true, then we paused LWPs momentarily, and may
3121 now have pending events to handle. */
3124 if (debug_linux_nat
)
3126 fprintf_unfiltered (gdb_stdlog
,
3127 "LLW: waitpid %ld received %s\n",
3128 (long) lwpid
, status_to_str (status
));
3131 lp
= linux_nat_filter_event (lwpid
, status
, &new_pending
);
3133 /* STATUS is now no longer valid, use LP->STATUS instead. */
3136 if (lp
&& !ptid_match (lp
->ptid
, ptid
))
3138 gdb_assert (lp
->resumed
);
3140 if (debug_linux_nat
)
3141 fprintf_unfiltered (gdb_stdlog
,
3142 "LWP %ld got an event %06x, "
3143 "leaving pending.\n",
3144 ptid_get_lwp (lp
->ptid
), lp
->status
);
3146 if (WIFSTOPPED (lp
->status
))
3148 if (WSTOPSIG (lp
->status
) != SIGSTOP
)
3150 /* Cancel breakpoint hits. The breakpoint may
3151 be removed before we fetch events from this
3152 process to report to the core. It is best
3153 not to assume the moribund breakpoints
3154 heuristic always handles these cases --- it
3155 could be too many events go through to the
3156 core before this one is handled. All-stop
3157 always cancels breakpoint hits in all
3160 && linux_nat_lp_status_is_event (lp
)
3161 && cancel_breakpoint (lp
))
3163 /* Throw away the SIGTRAP. */
3166 if (debug_linux_nat
)
3167 fprintf_unfiltered (gdb_stdlog
,
3168 "LLW: LWP %ld hit a "
3170 "waiting for another "
3173 ptid_get_lwp (lp
->ptid
));
3179 else if (WIFEXITED (lp
->status
) || WIFSIGNALED (lp
->status
))
3181 if (debug_linux_nat
)
3182 fprintf_unfiltered (gdb_stdlog
,
3183 "Process %ld exited while stopping "
3185 ptid_get_lwp (lp
->ptid
));
3187 /* This was the last lwp in the process. Since
3188 events are serialized to GDB core, and we can't
3189 report this one right now, but GDB core and the
3190 other target layers will want to be notified
3191 about the exit code/signal, leave the status
3192 pending for the next time we're able to report
3195 /* Dead LWP's aren't expected to reported a pending
3199 /* Store the pending event in the waitstatus as
3200 well, because W_EXITCODE(0,0) == 0. */
3201 store_waitstatus (&lp
->waitstatus
, lp
->status
);
3210 /* Some LWP now has a pending event. Go all the way
3211 back to check it. */
3217 /* We got an event to report to the core. */
3221 /* Retry until nothing comes out of waitpid. A single
3222 SIGCHLD can indicate more than one child stopped. */
3226 /* Check for zombie thread group leaders. Those can't be reaped
3227 until all other threads in the thread group are. */
3228 check_zombie_leaders ();
3230 /* If there are no resumed children left, bail. We'd be stuck
3231 forever in the sigsuspend call below otherwise. */
3232 if (iterate_over_lwps (ptid
, resumed_callback
, NULL
) == NULL
)
3234 if (debug_linux_nat
)
3235 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (no resumed LWP)\n");
3237 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3239 if (!target_can_async_p ())
3240 clear_sigint_trap ();
3242 restore_child_signals_mask (&prev_mask
);
3243 return minus_one_ptid
;
3246 /* No interesting event to report to the core. */
3248 if (target_options
& TARGET_WNOHANG
)
3250 if (debug_linux_nat
)
3251 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
3253 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3254 restore_child_signals_mask (&prev_mask
);
3255 return minus_one_ptid
;
3258 /* We shouldn't end up here unless we want to try again. */
3259 gdb_assert (lp
== NULL
);
3261 /* Block until we get an event reported with SIGCHLD. */
3262 if (debug_linux_nat
)
3263 fprintf_unfiltered (gdb_stdlog
, "LNW: about to sigsuspend\n");
3264 sigsuspend (&suspend_mask
);
3267 if (!target_can_async_p ())
3268 clear_sigint_trap ();
3272 status
= lp
->status
;
3275 /* Don't report signals that GDB isn't interested in, such as
3276 signals that are neither printed nor stopped upon. Stopping all
3277 threads can be a bit time-consuming so if we want decent
3278 performance with heavily multi-threaded programs, especially when
3279 they're using a high frequency timer, we'd better avoid it if we
3282 if (WIFSTOPPED (status
))
3284 enum gdb_signal signo
= gdb_signal_from_host (WSTOPSIG (status
));
3286 /* When using hardware single-step, we need to report every signal.
3287 Otherwise, signals in pass_mask may be short-circuited. */
3289 && WSTOPSIG (status
) && sigismember (&pass_mask
, WSTOPSIG (status
)))
3291 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3292 here? It is not clear we should. GDB may not expect
3293 other threads to run. On the other hand, not resuming
3294 newly attached threads may cause an unwanted delay in
3295 getting them running. */
3296 registers_changed ();
3297 if (linux_nat_prepare_to_resume
!= NULL
)
3298 linux_nat_prepare_to_resume (lp
);
3299 linux_ops
->to_resume (linux_ops
,
3300 pid_to_ptid (ptid_get_lwp (lp
->ptid
)),
3302 if (debug_linux_nat
)
3303 fprintf_unfiltered (gdb_stdlog
,
3304 "LLW: %s %s, %s (preempt 'handle')\n",
3306 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3307 target_pid_to_str (lp
->ptid
),
3308 (signo
!= GDB_SIGNAL_0
3309 ? strsignal (gdb_signal_to_host (signo
))
3317 /* Only do the below in all-stop, as we currently use SIGINT
3318 to implement target_stop (see linux_nat_stop) in
3320 if (signo
== GDB_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3322 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3323 forwarded to the entire process group, that is, all LWPs
3324 will receive it - unless they're using CLONE_THREAD to
3325 share signals. Since we only want to report it once, we
3326 mark it as ignored for all LWPs except this one. */
3327 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid
)),
3328 set_ignore_sigint
, NULL
);
3329 lp
->ignore_sigint
= 0;
3332 maybe_clear_ignore_sigint (lp
);
3336 /* This LWP is stopped now. */
3339 if (debug_linux_nat
)
3340 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
3341 status_to_str (status
), target_pid_to_str (lp
->ptid
));
3345 /* Now stop all other LWP's ... */
3346 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3348 /* ... and wait until all of them have reported back that
3349 they're no longer running. */
3350 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3352 /* If we're not waiting for a specific LWP, choose an event LWP
3353 from among those that have had events. Giving equal priority
3354 to all LWPs that have had events helps prevent
3356 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3357 select_event_lwp (ptid
, &lp
, &status
);
3359 /* Now that we've selected our final event LWP, cancel any
3360 breakpoints in other LWPs that have hit a GDB breakpoint.
3361 See the comment in cancel_breakpoints_callback to find out
3363 iterate_over_lwps (minus_one_ptid
, cancel_breakpoints_callback
, lp
);
3365 /* We'll need this to determine whether to report a SIGSTOP as
3366 TARGET_WAITKIND_0. Need to take a copy because
3367 resume_clear_callback clears it. */
3368 last_resume_kind
= lp
->last_resume_kind
;
3370 /* In all-stop, from the core's perspective, all LWPs are now
3371 stopped until a new resume action is sent over. */
3372 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
3377 last_resume_kind
= lp
->last_resume_kind
;
3378 resume_clear_callback (lp
, NULL
);
3381 if (linux_nat_status_is_event (status
))
3383 if (debug_linux_nat
)
3384 fprintf_unfiltered (gdb_stdlog
,
3385 "LLW: trap ptid is %s.\n",
3386 target_pid_to_str (lp
->ptid
));
3389 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3391 *ourstatus
= lp
->waitstatus
;
3392 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3395 store_waitstatus (ourstatus
, status
);
3397 if (debug_linux_nat
)
3398 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3400 restore_child_signals_mask (&prev_mask
);
3402 if (last_resume_kind
== resume_stop
3403 && ourstatus
->kind
== TARGET_WAITKIND_STOPPED
3404 && WSTOPSIG (status
) == SIGSTOP
)
3406 /* A thread that has been requested to stop by GDB with
3407 target_stop, and it stopped cleanly, so report as SIG0. The
3408 use of SIGSTOP is an implementation detail. */
3409 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3412 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
3413 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
3416 lp
->core
= linux_common_core_of_thread (lp
->ptid
);
3421 /* Resume LWPs that are currently stopped without any pending status
3422 to report, but are resumed from the core's perspective. */
3425 resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
)
3427 ptid_t
*wait_ptid_p
= data
;
3432 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
3434 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3435 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3436 CORE_ADDR pc
= regcache_read_pc (regcache
);
3438 gdb_assert (is_executing (lp
->ptid
));
3440 /* Don't bother if there's a breakpoint at PC that we'd hit
3441 immediately, and we're not waiting for this LWP. */
3442 if (!ptid_match (lp
->ptid
, *wait_ptid_p
))
3444 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
3448 if (debug_linux_nat
)
3449 fprintf_unfiltered (gdb_stdlog
,
3450 "RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
3451 target_pid_to_str (lp
->ptid
),
3452 paddress (gdbarch
, pc
),
3455 registers_changed ();
3456 if (linux_nat_prepare_to_resume
!= NULL
)
3457 linux_nat_prepare_to_resume (lp
);
3458 linux_ops
->to_resume (linux_ops
, pid_to_ptid (ptid_get_lwp (lp
->ptid
)),
3459 lp
->step
, GDB_SIGNAL_0
);
3461 lp
->stopped_by_watchpoint
= 0;
3468 linux_nat_wait (struct target_ops
*ops
,
3469 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3474 if (debug_linux_nat
)
3476 char *options_string
;
3478 options_string
= target_options_to_string (target_options
);
3479 fprintf_unfiltered (gdb_stdlog
,
3480 "linux_nat_wait: [%s], [%s]\n",
3481 target_pid_to_str (ptid
),
3483 xfree (options_string
);
3486 /* Flush the async file first. */
3487 if (target_can_async_p ())
3488 async_file_flush ();
3490 /* Resume LWPs that are currently stopped without any pending status
3491 to report, but are resumed from the core's perspective. LWPs get
3492 in this state if we find them stopping at a time we're not
3493 interested in reporting the event (target_wait on a
3494 specific_process, for example, see linux_nat_wait_1), and
3495 meanwhile the event became uninteresting. Don't bother resuming
3496 LWPs we're not going to wait for if they'd stop immediately. */
3498 iterate_over_lwps (minus_one_ptid
, resume_stopped_resumed_lwps
, &ptid
);
3500 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
, target_options
);
3502 /* If we requested any event, and something came out, assume there
3503 may be more. If we requested a specific lwp or process, also
3504 assume there may be more. */
3505 if (target_can_async_p ()
3506 && ((ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
3507 && ourstatus
->kind
!= TARGET_WAITKIND_NO_RESUMED
)
3508 || !ptid_equal (ptid
, minus_one_ptid
)))
3511 /* Get ready for the next event. */
3512 if (target_can_async_p ())
3513 target_async (inferior_event_handler
, 0);
3519 kill_callback (struct lwp_info
*lp
, void *data
)
3521 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
3524 kill_lwp (ptid_get_lwp (lp
->ptid
), SIGKILL
);
3525 if (debug_linux_nat
)
3527 int save_errno
= errno
;
3529 fprintf_unfiltered (gdb_stdlog
,
3530 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
3531 target_pid_to_str (lp
->ptid
),
3532 save_errno
? safe_strerror (save_errno
) : "OK");
3535 /* Some kernels ignore even SIGKILL for processes under ptrace. */
3538 ptrace (PTRACE_KILL
, ptid_get_lwp (lp
->ptid
), 0, 0);
3539 if (debug_linux_nat
)
3541 int save_errno
= errno
;
3543 fprintf_unfiltered (gdb_stdlog
,
3544 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3545 target_pid_to_str (lp
->ptid
),
3546 save_errno
? safe_strerror (save_errno
) : "OK");
3553 kill_wait_callback (struct lwp_info
*lp
, void *data
)
3557 /* We must make sure that there are no pending events (delayed
3558 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3559 program doesn't interfere with any following debugging session. */
3561 /* For cloned processes we must check both with __WCLONE and
3562 without, since the exit status of a cloned process isn't reported
3568 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), NULL
, __WCLONE
);
3569 if (pid
!= (pid_t
) -1)
3571 if (debug_linux_nat
)
3572 fprintf_unfiltered (gdb_stdlog
,
3573 "KWC: wait %s received unknown.\n",
3574 target_pid_to_str (lp
->ptid
));
3575 /* The Linux kernel sometimes fails to kill a thread
3576 completely after PTRACE_KILL; that goes from the stop
3577 point in do_fork out to the one in
3578 get_signal_to_deliever and waits again. So kill it
3580 kill_callback (lp
, NULL
);
3583 while (pid
== ptid_get_lwp (lp
->ptid
));
3585 gdb_assert (pid
== -1 && errno
== ECHILD
);
3590 pid
= my_waitpid (ptid_get_lwp (lp
->ptid
), NULL
, 0);
3591 if (pid
!= (pid_t
) -1)
3593 if (debug_linux_nat
)
3594 fprintf_unfiltered (gdb_stdlog
,
3595 "KWC: wait %s received unk.\n",
3596 target_pid_to_str (lp
->ptid
));
3597 /* See the call to kill_callback above. */
3598 kill_callback (lp
, NULL
);
3601 while (pid
== ptid_get_lwp (lp
->ptid
));
3603 gdb_assert (pid
== -1 && errno
== ECHILD
);
3608 linux_nat_kill (struct target_ops
*ops
)
3610 struct target_waitstatus last
;
3614 /* If we're stopped while forking and we haven't followed yet,
3615 kill the other task. We need to do this first because the
3616 parent will be sleeping if this is a vfork. */
3618 get_last_target_status (&last_ptid
, &last
);
3620 if (last
.kind
== TARGET_WAITKIND_FORKED
3621 || last
.kind
== TARGET_WAITKIND_VFORKED
)
3623 ptrace (PT_KILL
, ptid_get_pid (last
.value
.related_pid
), 0, 0);
3626 /* Let the arch-specific native code know this process is
3628 linux_nat_forget_process (ptid_get_pid (last
.value
.related_pid
));
3631 if (forks_exist_p ())
3632 linux_fork_killall ();
3635 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3637 /* Stop all threads before killing them, since ptrace requires
3638 that the thread is stopped to sucessfully PTRACE_KILL. */
3639 iterate_over_lwps (ptid
, stop_callback
, NULL
);
3640 /* ... and wait until all of them have reported back that
3641 they're no longer running. */
3642 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
3644 /* Kill all LWP's ... */
3645 iterate_over_lwps (ptid
, kill_callback
, NULL
);
3647 /* ... and wait until we've flushed all events. */
3648 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
3651 target_mourn_inferior ();
3655 linux_nat_mourn_inferior (struct target_ops
*ops
)
3657 int pid
= ptid_get_pid (inferior_ptid
);
3659 purge_lwp_list (pid
);
3661 if (! forks_exist_p ())
3662 /* Normal case, no other forks available. */
3663 linux_ops
->to_mourn_inferior (ops
);
3665 /* Multi-fork case. The current inferior_ptid has exited, but
3666 there are other viable forks to debug. Delete the exiting
3667 one and context-switch to the first available. */
3668 linux_fork_mourn_inferior ();
3670 /* Let the arch-specific native code know this process is gone. */
3671 linux_nat_forget_process (pid
);
3674 /* Convert a native/host siginfo object, into/from the siginfo in the
3675 layout of the inferiors' architecture. */
3678 siginfo_fixup (siginfo_t
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
3682 if (linux_nat_siginfo_fixup
!= NULL
)
3683 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
3685 /* If there was no callback, or the callback didn't do anything,
3686 then just do a straight memcpy. */
3690 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
3692 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
3696 static enum target_xfer_status
3697 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
3698 const char *annex
, gdb_byte
*readbuf
,
3699 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
3700 ULONGEST
*xfered_len
)
3704 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
3706 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
3707 gdb_assert (readbuf
|| writebuf
);
3709 pid
= ptid_get_lwp (inferior_ptid
);
3711 pid
= ptid_get_pid (inferior_ptid
);
3713 if (offset
> sizeof (siginfo
))
3714 return TARGET_XFER_E_IO
;
3717 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3719 return TARGET_XFER_E_IO
;
3721 /* When GDB is built as a 64-bit application, ptrace writes into
3722 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3723 inferior with a 64-bit GDB should look the same as debugging it
3724 with a 32-bit GDB, we need to convert it. GDB core always sees
3725 the converted layout, so any read/write will have to be done
3727 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
3729 if (offset
+ len
> sizeof (siginfo
))
3730 len
= sizeof (siginfo
) - offset
;
3732 if (readbuf
!= NULL
)
3733 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
3736 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
3738 /* Convert back to ptrace layout before flushing it out. */
3739 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
3742 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3744 return TARGET_XFER_E_IO
;
3748 return TARGET_XFER_OK
;
3751 static enum target_xfer_status
3752 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3753 const char *annex
, gdb_byte
*readbuf
,
3754 const gdb_byte
*writebuf
,
3755 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
3757 struct cleanup
*old_chain
;
3758 enum target_xfer_status xfer
;
3760 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
3761 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
3762 offset
, len
, xfered_len
);
3764 /* The target is connected but no live inferior is selected. Pass
3765 this request down to a lower stratum (e.g., the executable
3767 if (object
== TARGET_OBJECT_MEMORY
&& ptid_equal (inferior_ptid
, null_ptid
))
3768 return TARGET_XFER_EOF
;
3770 old_chain
= save_inferior_ptid ();
3772 if (ptid_lwp_p (inferior_ptid
))
3773 inferior_ptid
= pid_to_ptid (ptid_get_lwp (inferior_ptid
));
3775 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3776 offset
, len
, xfered_len
);
3778 do_cleanups (old_chain
);
3783 linux_thread_alive (ptid_t ptid
)
3787 gdb_assert (ptid_lwp_p (ptid
));
3789 /* Send signal 0 instead of anything ptrace, because ptracing a
3790 running thread errors out claiming that the thread doesn't
3792 err
= kill_lwp (ptid_get_lwp (ptid
), 0);
3794 if (debug_linux_nat
)
3795 fprintf_unfiltered (gdb_stdlog
,
3796 "LLTA: KILL(SIG0) %s (%s)\n",
3797 target_pid_to_str (ptid
),
3798 err
? safe_strerror (tmp_errno
) : "OK");
3807 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
3809 return linux_thread_alive (ptid
);
3813 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3815 static char buf
[64];
3817 if (ptid_lwp_p (ptid
)
3818 && (ptid_get_pid (ptid
) != ptid_get_lwp (ptid
)
3819 || num_lwps (ptid_get_pid (ptid
)) > 1))
3821 snprintf (buf
, sizeof (buf
), "LWP %ld", ptid_get_lwp (ptid
));
3825 return normal_pid_to_str (ptid
);
3829 linux_nat_thread_name (struct target_ops
*self
, struct thread_info
*thr
)
3831 int pid
= ptid_get_pid (thr
->ptid
);
3832 long lwp
= ptid_get_lwp (thr
->ptid
);
3833 #define FORMAT "/proc/%d/task/%ld/comm"
3834 char buf
[sizeof (FORMAT
) + 30];
3836 char *result
= NULL
;
3838 snprintf (buf
, sizeof (buf
), FORMAT
, pid
, lwp
);
3839 comm_file
= gdb_fopen_cloexec (buf
, "r");
3842 /* Not exported by the kernel, so we define it here. */
3844 static char line
[COMM_LEN
+ 1];
3846 if (fgets (line
, sizeof (line
), comm_file
))
3848 char *nl
= strchr (line
, '\n');
3865 /* Accepts an integer PID; Returns a string representing a file that
3866 can be opened to get the symbols for the child process. */
3869 linux_child_pid_to_exec_file (struct target_ops
*self
, int pid
)
3871 static char buf
[PATH_MAX
];
3872 char name
[PATH_MAX
];
3874 xsnprintf (name
, PATH_MAX
, "/proc/%d/exe", pid
);
3875 memset (buf
, 0, PATH_MAX
);
3876 if (readlink (name
, buf
, PATH_MAX
- 1) <= 0)
3882 /* Implement the to_xfer_partial interface for memory reads using the /proc
3883 filesystem. Because we can use a single read() call for /proc, this
3884 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3885 but it doesn't support writes. */
3887 static enum target_xfer_status
3888 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3889 const char *annex
, gdb_byte
*readbuf
,
3890 const gdb_byte
*writebuf
,
3891 ULONGEST offset
, LONGEST len
, ULONGEST
*xfered_len
)
3897 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
3900 /* Don't bother for one word. */
3901 if (len
< 3 * sizeof (long))
3902 return TARGET_XFER_EOF
;
3904 /* We could keep this file open and cache it - possibly one per
3905 thread. That requires some juggling, but is even faster. */
3906 xsnprintf (filename
, sizeof filename
, "/proc/%d/mem",
3907 ptid_get_pid (inferior_ptid
));
3908 fd
= gdb_open_cloexec (filename
, O_RDONLY
| O_LARGEFILE
, 0);
3910 return TARGET_XFER_EOF
;
3912 /* If pread64 is available, use it. It's faster if the kernel
3913 supports it (only one syscall), and it's 64-bit safe even on
3914 32-bit platforms (for instance, SPARC debugging a SPARC64
3917 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
3919 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
3928 return TARGET_XFER_EOF
;
3932 return TARGET_XFER_OK
;
3937 /* Enumerate spufs IDs for process PID. */
3939 spu_enumerate_spu_ids (int pid
, gdb_byte
*buf
, ULONGEST offset
, ULONGEST len
)
3941 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
3943 LONGEST written
= 0;
3946 struct dirent
*entry
;
3948 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
3949 dir
= opendir (path
);
3954 while ((entry
= readdir (dir
)) != NULL
)
3960 fd
= atoi (entry
->d_name
);
3964 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
3965 if (stat (path
, &st
) != 0)
3967 if (!S_ISDIR (st
.st_mode
))
3970 if (statfs (path
, &stfs
) != 0)
3972 if (stfs
.f_type
!= SPUFS_MAGIC
)
3975 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
3977 store_unsigned_integer (buf
+ pos
- offset
, 4, byte_order
, fd
);
3987 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
3988 object type, using the /proc file system. */
3990 static enum target_xfer_status
3991 linux_proc_xfer_spu (struct target_ops
*ops
, enum target_object object
,
3992 const char *annex
, gdb_byte
*readbuf
,
3993 const gdb_byte
*writebuf
,
3994 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
3999 int pid
= ptid_get_pid (inferior_ptid
);
4004 return TARGET_XFER_E_IO
;
4007 LONGEST l
= spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
4010 return TARGET_XFER_E_IO
;
4012 return TARGET_XFER_EOF
;
4015 *xfered_len
= (ULONGEST
) l
;
4016 return TARGET_XFER_OK
;
4021 xsnprintf (buf
, sizeof buf
, "/proc/%d/fd/%s", pid
, annex
);
4022 fd
= gdb_open_cloexec (buf
, writebuf
? O_WRONLY
: O_RDONLY
, 0);
4024 return TARGET_XFER_E_IO
;
4027 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4030 return TARGET_XFER_EOF
;
4034 ret
= write (fd
, writebuf
, (size_t) len
);
4036 ret
= read (fd
, readbuf
, (size_t) len
);
4041 return TARGET_XFER_E_IO
;
4043 return TARGET_XFER_EOF
;
4046 *xfered_len
= (ULONGEST
) ret
;
4047 return TARGET_XFER_OK
;
4052 /* Parse LINE as a signal set and add its set bits to SIGS. */
4055 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4057 int len
= strlen (line
) - 1;
4061 if (line
[len
] != '\n')
4062 error (_("Could not parse signal set: %s"), line
);
4070 if (*p
>= '0' && *p
<= '9')
4072 else if (*p
>= 'a' && *p
<= 'f')
4073 digit
= *p
- 'a' + 10;
4075 error (_("Could not parse signal set: %s"), line
);
4080 sigaddset (sigs
, signum
+ 1);
4082 sigaddset (sigs
, signum
+ 2);
4084 sigaddset (sigs
, signum
+ 3);
4086 sigaddset (sigs
, signum
+ 4);
4092 /* Find process PID's pending signals from /proc/pid/status and set
4096 linux_proc_pending_signals (int pid
, sigset_t
*pending
,
4097 sigset_t
*blocked
, sigset_t
*ignored
)
4100 char buffer
[PATH_MAX
], fname
[PATH_MAX
];
4101 struct cleanup
*cleanup
;
4103 sigemptyset (pending
);
4104 sigemptyset (blocked
);
4105 sigemptyset (ignored
);
4106 xsnprintf (fname
, sizeof fname
, "/proc/%d/status", pid
);
4107 procfile
= gdb_fopen_cloexec (fname
, "r");
4108 if (procfile
== NULL
)
4109 error (_("Could not open %s"), fname
);
4110 cleanup
= make_cleanup_fclose (procfile
);
4112 while (fgets (buffer
, PATH_MAX
, procfile
) != NULL
)
4114 /* Normal queued signals are on the SigPnd line in the status
4115 file. However, 2.6 kernels also have a "shared" pending
4116 queue for delivering signals to a thread group, so check for
4119 Unfortunately some Red Hat kernels include the shared pending
4120 queue but not the ShdPnd status field. */
4122 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
4123 add_line_to_sigset (buffer
+ 8, pending
);
4124 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
4125 add_line_to_sigset (buffer
+ 8, pending
);
4126 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
4127 add_line_to_sigset (buffer
+ 8, blocked
);
4128 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
4129 add_line_to_sigset (buffer
+ 8, ignored
);
4132 do_cleanups (cleanup
);
4135 static enum target_xfer_status
4136 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4137 const char *annex
, gdb_byte
*readbuf
,
4138 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
4139 ULONGEST
*xfered_len
)
4141 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4143 *xfered_len
= linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4144 if (*xfered_len
== 0)
4145 return TARGET_XFER_EOF
;
4147 return TARGET_XFER_OK
;
4150 static enum target_xfer_status
4151 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4152 const char *annex
, gdb_byte
*readbuf
,
4153 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
4154 ULONGEST
*xfered_len
)
4156 enum target_xfer_status xfer
;
4158 if (object
== TARGET_OBJECT_AUXV
)
4159 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4160 offset
, len
, xfered_len
);
4162 if (object
== TARGET_OBJECT_OSDATA
)
4163 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4164 offset
, len
, xfered_len
);
4166 if (object
== TARGET_OBJECT_SPU
)
4167 return linux_proc_xfer_spu (ops
, object
, annex
, readbuf
, writebuf
,
4168 offset
, len
, xfered_len
);
4170 /* GDB calculates all the addresses in possibly larget width of the address.
4171 Address width needs to be masked before its final use - either by
4172 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4174 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4176 if (object
== TARGET_OBJECT_MEMORY
)
4178 int addr_bit
= gdbarch_addr_bit (target_gdbarch ());
4180 if (addr_bit
< (sizeof (ULONGEST
) * HOST_CHAR_BIT
))
4181 offset
&= ((ULONGEST
) 1 << addr_bit
) - 1;
4184 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4185 offset
, len
, xfered_len
);
4186 if (xfer
!= TARGET_XFER_EOF
)
4189 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4190 offset
, len
, xfered_len
);
4194 cleanup_target_stop (void *arg
)
4196 ptid_t
*ptid
= (ptid_t
*) arg
;
4198 gdb_assert (arg
!= NULL
);
4201 target_resume (*ptid
, 0, GDB_SIGNAL_0
);
4204 static VEC(static_tracepoint_marker_p
) *
4205 linux_child_static_tracepoint_markers_by_strid (struct target_ops
*self
,
4208 char s
[IPA_CMD_BUF_SIZE
];
4209 struct cleanup
*old_chain
;
4210 int pid
= ptid_get_pid (inferior_ptid
);
4211 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
4212 struct static_tracepoint_marker
*marker
= NULL
;
4214 ptid_t ptid
= ptid_build (pid
, 0, 0);
4219 memcpy (s
, "qTfSTM", sizeof ("qTfSTM"));
4220 s
[sizeof ("qTfSTM")] = 0;
4222 agent_run_command (pid
, s
, strlen (s
) + 1);
4224 old_chain
= make_cleanup (free_current_marker
, &marker
);
4225 make_cleanup (cleanup_target_stop
, &ptid
);
4230 marker
= XCNEW (struct static_tracepoint_marker
);
4234 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
4236 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
4238 VEC_safe_push (static_tracepoint_marker_p
,
4244 release_static_tracepoint_marker (marker
);
4245 memset (marker
, 0, sizeof (*marker
));
4248 while (*p
++ == ','); /* comma-separated list */
4250 memcpy (s
, "qTsSTM", sizeof ("qTsSTM"));
4251 s
[sizeof ("qTsSTM")] = 0;
4252 agent_run_command (pid
, s
, strlen (s
) + 1);
4256 do_cleanups (old_chain
);
4261 /* Create a prototype generic GNU/Linux target. The client can override
4262 it with local methods. */
4265 linux_target_install_ops (struct target_ops
*t
)
4267 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4268 t
->to_remove_fork_catchpoint
= linux_child_remove_fork_catchpoint
;
4269 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4270 t
->to_remove_vfork_catchpoint
= linux_child_remove_vfork_catchpoint
;
4271 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4272 t
->to_remove_exec_catchpoint
= linux_child_remove_exec_catchpoint
;
4273 t
->to_set_syscall_catchpoint
= linux_child_set_syscall_catchpoint
;
4274 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4275 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4276 t
->to_post_attach
= linux_child_post_attach
;
4277 t
->to_follow_fork
= linux_child_follow_fork
;
4279 super_xfer_partial
= t
->to_xfer_partial
;
4280 t
->to_xfer_partial
= linux_xfer_partial
;
4282 t
->to_static_tracepoint_markers_by_strid
4283 = linux_child_static_tracepoint_markers_by_strid
;
4289 struct target_ops
*t
;
4291 t
= inf_ptrace_target ();
4292 linux_target_install_ops (t
);
4298 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4300 struct target_ops
*t
;
4302 t
= inf_ptrace_trad_target (register_u_offset
);
4303 linux_target_install_ops (t
);
4308 /* target_is_async_p implementation. */
4311 linux_nat_is_async_p (struct target_ops
*ops
)
4313 /* NOTE: palves 2008-03-21: We're only async when the user requests
4314 it explicitly with the "set target-async" command.
4315 Someday, linux will always be async. */
4316 return target_async_permitted
;
4319 /* target_can_async_p implementation. */
4322 linux_nat_can_async_p (struct target_ops
*ops
)
4324 /* NOTE: palves 2008-03-21: We're only async when the user requests
4325 it explicitly with the "set target-async" command.
4326 Someday, linux will always be async. */
4327 return target_async_permitted
;
4331 linux_nat_supports_non_stop (struct target_ops
*self
)
4336 /* True if we want to support multi-process. To be removed when GDB
4337 supports multi-exec. */
4339 int linux_multi_process
= 1;
4342 linux_nat_supports_multi_process (struct target_ops
*self
)
4344 return linux_multi_process
;
4348 linux_nat_supports_disable_randomization (struct target_ops
*self
)
4350 #ifdef HAVE_PERSONALITY
4357 static int async_terminal_is_ours
= 1;
4359 /* target_terminal_inferior implementation.
4361 This is a wrapper around child_terminal_inferior to add async support. */
4364 linux_nat_terminal_inferior (struct target_ops
*self
)
4366 if (!target_is_async_p ())
4368 /* Async mode is disabled. */
4369 child_terminal_inferior (self
);
4373 child_terminal_inferior (self
);
4375 /* Calls to target_terminal_*() are meant to be idempotent. */
4376 if (!async_terminal_is_ours
)
4379 delete_file_handler (input_fd
);
4380 async_terminal_is_ours
= 0;
4384 /* target_terminal_ours implementation.
4386 This is a wrapper around child_terminal_ours to add async support (and
4387 implement the target_terminal_ours vs target_terminal_ours_for_output
4388 distinction). child_terminal_ours is currently no different than
4389 child_terminal_ours_for_output.
4390 We leave target_terminal_ours_for_output alone, leaving it to
4391 child_terminal_ours_for_output. */
4394 linux_nat_terminal_ours (struct target_ops
*self
)
4396 if (!target_is_async_p ())
4398 /* Async mode is disabled. */
4399 child_terminal_ours (self
);
4403 /* GDB should never give the terminal to the inferior if the
4404 inferior is running in the background (run&, continue&, etc.),
4405 but claiming it sure should. */
4406 child_terminal_ours (self
);
4408 if (async_terminal_is_ours
)
4411 clear_sigint_trap ();
4412 add_file_handler (input_fd
, stdin_event_handler
, 0);
4413 async_terminal_is_ours
= 1;
4416 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4418 static void *async_client_context
;
4420 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4421 so we notice when any child changes state, and notify the
4422 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4423 above to wait for the arrival of a SIGCHLD. */
4426 sigchld_handler (int signo
)
4428 int old_errno
= errno
;
4430 if (debug_linux_nat
)
4431 ui_file_write_async_safe (gdb_stdlog
,
4432 "sigchld\n", sizeof ("sigchld\n") - 1);
4434 if (signo
== SIGCHLD
4435 && linux_nat_event_pipe
[0] != -1)
4436 async_file_mark (); /* Let the event loop know that there are
4437 events to handle. */
4442 /* Callback registered with the target events file descriptor. */
4445 handle_target_event (int error
, gdb_client_data client_data
)
4447 (*async_client_callback
) (INF_REG_EVENT
, async_client_context
);
4450 /* Create/destroy the target events pipe. Returns previous state. */
4453 linux_async_pipe (int enable
)
4455 int previous
= (linux_nat_event_pipe
[0] != -1);
4457 if (previous
!= enable
)
4461 /* Block child signals while we create/destroy the pipe, as
4462 their handler writes to it. */
4463 block_child_signals (&prev_mask
);
4467 if (gdb_pipe_cloexec (linux_nat_event_pipe
) == -1)
4468 internal_error (__FILE__
, __LINE__
,
4469 "creating event pipe failed.");
4471 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4472 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4476 close (linux_nat_event_pipe
[0]);
4477 close (linux_nat_event_pipe
[1]);
4478 linux_nat_event_pipe
[0] = -1;
4479 linux_nat_event_pipe
[1] = -1;
4482 restore_child_signals_mask (&prev_mask
);
4488 /* target_async implementation. */
4491 linux_nat_async (struct target_ops
*ops
,
4492 void (*callback
) (enum inferior_event_type event_type
,
4496 if (callback
!= NULL
)
4498 async_client_callback
= callback
;
4499 async_client_context
= context
;
4500 if (!linux_async_pipe (1))
4502 add_file_handler (linux_nat_event_pipe
[0],
4503 handle_target_event
, NULL
);
4504 /* There may be pending events to handle. Tell the event loop
4511 async_client_callback
= callback
;
4512 async_client_context
= context
;
4513 delete_file_handler (linux_nat_event_pipe
[0]);
4514 linux_async_pipe (0);
4519 /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
4523 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4527 if (debug_linux_nat
)
4528 fprintf_unfiltered (gdb_stdlog
,
4529 "LNSL: running -> suspending %s\n",
4530 target_pid_to_str (lwp
->ptid
));
4533 if (lwp
->last_resume_kind
== resume_stop
)
4535 if (debug_linux_nat
)
4536 fprintf_unfiltered (gdb_stdlog
,
4537 "linux-nat: already stopping LWP %ld at "
4539 ptid_get_lwp (lwp
->ptid
));
4543 stop_callback (lwp
, NULL
);
4544 lwp
->last_resume_kind
= resume_stop
;
4548 /* Already known to be stopped; do nothing. */
4550 if (debug_linux_nat
)
4552 if (find_thread_ptid (lwp
->ptid
)->stop_requested
)
4553 fprintf_unfiltered (gdb_stdlog
,
4554 "LNSL: already stopped/stop_requested %s\n",
4555 target_pid_to_str (lwp
->ptid
));
4557 fprintf_unfiltered (gdb_stdlog
,
4558 "LNSL: already stopped/no "
4559 "stop_requested yet %s\n",
4560 target_pid_to_str (lwp
->ptid
));
4567 linux_nat_stop (struct target_ops
*self
, ptid_t ptid
)
4570 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
4572 linux_ops
->to_stop (linux_ops
, ptid
);
4576 linux_nat_close (struct target_ops
*self
)
4578 /* Unregister from the event loop. */
4579 if (linux_nat_is_async_p (self
))
4580 linux_nat_async (self
, NULL
, NULL
);
4582 if (linux_ops
->to_close
)
4583 linux_ops
->to_close (linux_ops
);
4588 /* When requests are passed down from the linux-nat layer to the
4589 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
4590 used. The address space pointer is stored in the inferior object,
4591 but the common code that is passed such ptid can't tell whether
4592 lwpid is a "main" process id or not (it assumes so). We reverse
4593 look up the "main" process id from the lwp here. */
4595 static struct address_space
*
4596 linux_nat_thread_address_space (struct target_ops
*t
, ptid_t ptid
)
4598 struct lwp_info
*lwp
;
4599 struct inferior
*inf
;
4602 if (ptid_get_lwp (ptid
) == 0)
4604 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
4606 lwp
= find_lwp_pid (ptid
);
4607 pid
= ptid_get_pid (lwp
->ptid
);
4611 /* A (pid,lwpid,0) ptid. */
4612 pid
= ptid_get_pid (ptid
);
4615 inf
= find_inferior_pid (pid
);
4616 gdb_assert (inf
!= NULL
);
4620 /* Return the cached value of the processor core for thread PTID. */
4623 linux_nat_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
4625 struct lwp_info
*info
= find_lwp_pid (ptid
);
4633 linux_nat_add_target (struct target_ops
*t
)
4635 /* Save the provided single-threaded target. We save this in a separate
4636 variable because another target we've inherited from (e.g. inf-ptrace)
4637 may have saved a pointer to T; we want to use it for the final
4638 process stratum target. */
4639 linux_ops_saved
= *t
;
4640 linux_ops
= &linux_ops_saved
;
4642 /* Override some methods for multithreading. */
4643 t
->to_create_inferior
= linux_nat_create_inferior
;
4644 t
->to_attach
= linux_nat_attach
;
4645 t
->to_detach
= linux_nat_detach
;
4646 t
->to_resume
= linux_nat_resume
;
4647 t
->to_wait
= linux_nat_wait
;
4648 t
->to_pass_signals
= linux_nat_pass_signals
;
4649 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4650 t
->to_kill
= linux_nat_kill
;
4651 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4652 t
->to_thread_alive
= linux_nat_thread_alive
;
4653 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4654 t
->to_thread_name
= linux_nat_thread_name
;
4655 t
->to_has_thread_control
= tc_schedlock
;
4656 t
->to_thread_address_space
= linux_nat_thread_address_space
;
4657 t
->to_stopped_by_watchpoint
= linux_nat_stopped_by_watchpoint
;
4658 t
->to_stopped_data_address
= linux_nat_stopped_data_address
;
4660 t
->to_can_async_p
= linux_nat_can_async_p
;
4661 t
->to_is_async_p
= linux_nat_is_async_p
;
4662 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
4663 t
->to_async
= linux_nat_async
;
4664 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4665 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4667 super_close
= t
->to_close
;
4668 t
->to_close
= linux_nat_close
;
4670 /* Methods for non-stop support. */
4671 t
->to_stop
= linux_nat_stop
;
4673 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
4675 t
->to_supports_disable_randomization
4676 = linux_nat_supports_disable_randomization
;
4678 t
->to_core_of_thread
= linux_nat_core_of_thread
;
4680 /* We don't change the stratum; this target will sit at
4681 process_stratum and thread_db will set at thread_stratum. This
4682 is a little strange, since this is a multi-threaded-capable
4683 target, but we want to be on the stack below thread_db, and we
4684 also want to be used for single-threaded processes. */
4689 /* Register a method to call whenever a new thread is attached. */
4691 linux_nat_set_new_thread (struct target_ops
*t
,
4692 void (*new_thread
) (struct lwp_info
*))
4694 /* Save the pointer. We only support a single registered instance
4695 of the GNU/Linux native target, so we do not need to map this to
4697 linux_nat_new_thread
= new_thread
;
4700 /* See declaration in linux-nat.h. */
4703 linux_nat_set_new_fork (struct target_ops
*t
,
4704 linux_nat_new_fork_ftype
*new_fork
)
4706 /* Save the pointer. */
4707 linux_nat_new_fork
= new_fork
;
4710 /* See declaration in linux-nat.h. */
4713 linux_nat_set_forget_process (struct target_ops
*t
,
4714 linux_nat_forget_process_ftype
*fn
)
4716 /* Save the pointer. */
4717 linux_nat_forget_process_hook
= fn
;
4720 /* See declaration in linux-nat.h. */
4723 linux_nat_forget_process (pid_t pid
)
4725 if (linux_nat_forget_process_hook
!= NULL
)
4726 linux_nat_forget_process_hook (pid
);
4729 /* Register a method that converts a siginfo object between the layout
4730 that ptrace returns, and the layout in the architecture of the
4733 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
4734 int (*siginfo_fixup
) (siginfo_t
*,
4738 /* Save the pointer. */
4739 linux_nat_siginfo_fixup
= siginfo_fixup
;
4742 /* Register a method to call prior to resuming a thread. */
4745 linux_nat_set_prepare_to_resume (struct target_ops
*t
,
4746 void (*prepare_to_resume
) (struct lwp_info
*))
4748 /* Save the pointer. */
4749 linux_nat_prepare_to_resume
= prepare_to_resume
;
4752 /* See linux-nat.h. */
4755 linux_nat_get_siginfo (ptid_t ptid
, siginfo_t
*siginfo
)
4759 pid
= ptid_get_lwp (ptid
);
4761 pid
= ptid_get_pid (ptid
);
4764 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, siginfo
);
4767 memset (siginfo
, 0, sizeof (*siginfo
));
4773 /* Provide a prototype to silence -Wmissing-prototypes. */
4774 extern initialize_file_ftype _initialize_linux_nat
;
4777 _initialize_linux_nat (void)
4779 add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance
,
4780 &debug_linux_nat
, _("\
4781 Set debugging of GNU/Linux lwp module."), _("\
4782 Show debugging of GNU/Linux lwp module."), _("\
4783 Enables printf debugging output."),
4785 show_debug_linux_nat
,
4786 &setdebuglist
, &showdebuglist
);
4788 /* Save this mask as the default. */
4789 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4791 /* Install a SIGCHLD handler. */
4792 sigchld_action
.sa_handler
= sigchld_handler
;
4793 sigemptyset (&sigchld_action
.sa_mask
);
4794 sigchld_action
.sa_flags
= SA_RESTART
;
4796 /* Make it the default. */
4797 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
4799 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4800 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
4801 sigdelset (&suspend_mask
, SIGCHLD
);
4803 sigemptyset (&blocked_mask
);
4805 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to
4806 support read-only process state. */
4807 linux_ptrace_set_additional_flags (PTRACE_O_TRACESYSGOOD
4808 | PTRACE_O_TRACEVFORKDONE
4809 | PTRACE_O_TRACEVFORK
4810 | PTRACE_O_TRACEFORK
4811 | PTRACE_O_TRACEEXEC
);
4815 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4816 the GNU/Linux Threads library and therefore doesn't really belong
4819 /* Read variable NAME in the target and return its value if found.
4820 Otherwise return zero. It is assumed that the type of the variable
4824 get_signo (const char *name
)
4826 struct bound_minimal_symbol ms
;
4829 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
4830 if (ms
.minsym
== NULL
)
4833 if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
4834 sizeof (signo
)) != 0)
4840 /* Return the set of signals used by the threads library in *SET. */
4843 lin_thread_get_thread_signals (sigset_t
*set
)
4845 struct sigaction action
;
4846 int restart
, cancel
;
4848 sigemptyset (&blocked_mask
);
4851 restart
= get_signo ("__pthread_sig_restart");
4852 cancel
= get_signo ("__pthread_sig_cancel");
4854 /* LinuxThreads normally uses the first two RT signals, but in some legacy
4855 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
4856 not provide any way for the debugger to query the signal numbers -
4857 fortunately they don't change! */
4860 restart
= __SIGRTMIN
;
4863 cancel
= __SIGRTMIN
+ 1;
4865 sigaddset (set
, restart
);
4866 sigaddset (set
, cancel
);
4868 /* The GNU/Linux Threads library makes terminating threads send a
4869 special "cancel" signal instead of SIGCHLD. Make sure we catch
4870 those (to prevent them from terminating GDB itself, which is
4871 likely to be their default action) and treat them the same way as
4874 action
.sa_handler
= sigchld_handler
;
4875 sigemptyset (&action
.sa_mask
);
4876 action
.sa_flags
= SA_RESTART
;
4877 sigaction (cancel
, &action
, NULL
);
4879 /* We block the "cancel" signal throughout this code ... */
4880 sigaddset (&blocked_mask
, cancel
);
4881 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
4883 /* ... except during a sigsuspend. */
4884 sigdelset (&suspend_mask
, cancel
);