1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
25 #include "gdb_assert.h"
26 #ifdef HAVE_TKILL_SYSCALL
28 #include <sys/syscall.h>
30 #include <sys/ptrace.h>
31 #include "linux-nat.h"
32 #include "linux-ptrace.h"
33 #include "linux-procfs.h"
34 #include "linux-fork.h"
35 #include "gdbthread.h"
39 #include "inf-ptrace.h"
41 #include <sys/param.h> /* for MAXPATHLEN */
42 #include <sys/procfs.h> /* for elf_gregset etc. */
43 #include "elf-bfd.h" /* for elfcore_write_* */
44 #include "gregset.h" /* for gregset */
45 #include "gdbcore.h" /* for get_exec_file */
46 #include <ctype.h> /* for isdigit */
47 #include "gdbthread.h" /* for struct thread_info etc. */
48 #include "gdb_stat.h" /* for struct stat */
49 #include <fcntl.h> /* for O_RDONLY */
51 #include "event-loop.h"
52 #include "event-top.h"
54 #include <sys/types.h>
55 #include "gdb_dirent.h"
56 #include "xml-support.h"
60 #include "linux-osdata.h"
61 #include "linux-tdep.h"
64 #define SPUFS_MAGIC 0x23c9b64e
67 #ifdef HAVE_PERSONALITY
68 # include <sys/personality.h>
69 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
70 # define ADDR_NO_RANDOMIZE 0x0040000
72 #endif /* HAVE_PERSONALITY */
74 /* This comment documents high-level logic of this file.
76 Waiting for events in sync mode
77 ===============================
79 When waiting for an event in a specific thread, we just use waitpid, passing
80 the specific pid, and not passing WNOHANG.
82 When waiting for an event in all threads, waitpid is not quite good. Prior to
83 version 2.4, Linux can either wait for event in main thread, or in secondary
84 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
85 miss an event. The solution is to use non-blocking waitpid, together with
86 sigsuspend. First, we use non-blocking waitpid to get an event in the main
87 process, if any. Second, we use non-blocking waitpid with the __WCLONED
88 flag to check for events in cloned processes. If nothing is found, we use
89 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
90 happened to a child process -- and SIGCHLD will be delivered both for events
91 in main debugged process and in cloned processes. As soon as we know there's
92 an event, we get back to calling nonblocking waitpid with and without
95 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
96 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
97 blocked, the signal becomes pending and sigsuspend immediately
98 notices it and returns.
100 Waiting for events in async mode
101 ================================
103 In async mode, GDB should always be ready to handle both user input
104 and target events, so neither blocking waitpid nor sigsuspend are
105 viable options. Instead, we should asynchronously notify the GDB main
106 event loop whenever there's an unprocessed event from the target. We
107 detect asynchronous target events by handling SIGCHLD signals. To
108 notify the event loop about target events, the self-pipe trick is used
109 --- a pipe is registered as waitable event source in the event loop,
110 the event loop select/poll's on the read end of this pipe (as well on
111 other event sources, e.g., stdin), and the SIGCHLD handler writes a
112 byte to this pipe. This is more portable than relying on
113 pselect/ppoll, since on kernels that lack those syscalls, libc
114 emulates them with select/poll+sigprocmask, and that is racy
115 (a.k.a. plain broken).
117 Obviously, if we fail to notify the event loop if there's a target
118 event, it's bad. OTOH, if we notify the event loop when there's no
119 event from the target, linux_nat_wait will detect that there's no real
120 event to report, and return event of type TARGET_WAITKIND_IGNORE.
121 This is mostly harmless, but it will waste time and is better avoided.
123 The main design point is that every time GDB is outside linux-nat.c,
124 we have a SIGCHLD handler installed that is called when something
125 happens to the target and notifies the GDB event loop. Whenever GDB
126 core decides to handle the event, and calls into linux-nat.c, we
127 process things as in sync mode, except that the we never block in
130 While processing an event, we may end up momentarily blocked in
131 waitpid calls. Those waitpid calls, while blocking, are guarantied to
132 return quickly. E.g., in all-stop mode, before reporting to the core
133 that an LWP hit a breakpoint, all LWPs are stopped by sending them
134 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
135 Note that this is different from blocking indefinitely waiting for the
136 next event --- here, we're already handling an event.
141 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
142 signal is not entirely significant; we just need for a signal to be delivered,
143 so that we can intercept it. SIGSTOP's advantage is that it can not be
144 blocked. A disadvantage is that it is not a real-time signal, so it can only
145 be queued once; we do not keep track of other sources of SIGSTOP.
147 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
148 use them, because they have special behavior when the signal is generated -
149 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
150 kills the entire thread group.
152 A delivered SIGSTOP would stop the entire thread group, not just the thread we
153 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
154 cancel it (by PTRACE_CONT without passing SIGSTOP).
156 We could use a real-time signal instead. This would solve those problems; we
157 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
158 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
159 generates it, and there are races with trying to find a signal that is not
163 #define O_LARGEFILE 0
166 /* Unlike other extended result codes, WSTOPSIG (status) on
167 PTRACE_O_TRACESYSGOOD syscall events doesn't return SIGTRAP, but
168 instead SIGTRAP with bit 7 set. */
169 #define SYSCALL_SIGTRAP (SIGTRAP | 0x80)
171 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
172 the use of the multi-threaded target. */
173 static struct target_ops
*linux_ops
;
174 static struct target_ops linux_ops_saved
;
176 /* The method to call, if any, when a new thread is attached. */
177 static void (*linux_nat_new_thread
) (struct lwp_info
*);
179 /* Hook to call prior to resuming a thread. */
180 static void (*linux_nat_prepare_to_resume
) (struct lwp_info
*);
182 /* The method to call, if any, when the siginfo object needs to be
183 converted between the layout returned by ptrace, and the layout in
184 the architecture of the inferior. */
185 static int (*linux_nat_siginfo_fixup
) (struct siginfo
*,
189 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
190 Called by our to_xfer_partial. */
191 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
193 const char *, gdb_byte
*,
197 static int debug_linux_nat
;
199 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
200 struct cmd_list_element
*c
, const char *value
)
202 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
206 struct simple_pid_list
210 struct simple_pid_list
*next
;
212 struct simple_pid_list
*stopped_pids
;
214 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
215 can not be used, 1 if it can. */
217 static int linux_supports_tracefork_flag
= -1;
219 /* This variable is a tri-state flag: -1 for unknown, 0 if
220 PTRACE_O_TRACESYSGOOD can not be used, 1 if it can. */
222 static int linux_supports_tracesysgood_flag
= -1;
224 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
225 PTRACE_O_TRACEVFORKDONE. */
227 static int linux_supports_tracevforkdone_flag
= -1;
229 /* Stores the current used ptrace() options. */
230 static int current_ptrace_options
= 0;
232 /* Async mode support. */
234 /* The read/write ends of the pipe registered as waitable file in the
236 static int linux_nat_event_pipe
[2] = { -1, -1 };
238 /* Flush the event pipe. */
241 async_file_flush (void)
248 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
250 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
253 /* Put something (anything, doesn't matter what, or how much) in event
254 pipe, so that the select/poll in the event-loop realizes we have
255 something to process. */
258 async_file_mark (void)
262 /* It doesn't really matter what the pipe contains, as long we end
263 up with something in it. Might as well flush the previous
269 ret
= write (linux_nat_event_pipe
[1], "+", 1);
271 while (ret
== -1 && errno
== EINTR
);
273 /* Ignore EAGAIN. If the pipe is full, the event loop will already
274 be awakened anyway. */
277 static void linux_nat_async (void (*callback
)
278 (enum inferior_event_type event_type
,
281 static int kill_lwp (int lwpid
, int signo
);
283 static int stop_callback (struct lwp_info
*lp
, void *data
);
285 static void block_child_signals (sigset_t
*prev_mask
);
286 static void restore_child_signals_mask (sigset_t
*prev_mask
);
289 static struct lwp_info
*add_lwp (ptid_t ptid
);
290 static void purge_lwp_list (int pid
);
291 static void delete_lwp (ptid_t ptid
);
292 static struct lwp_info
*find_lwp_pid (ptid_t ptid
);
295 /* Trivial list manipulation functions to keep track of a list of
296 new stopped processes. */
298 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
300 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
303 new_pid
->status
= status
;
304 new_pid
->next
= *listp
;
309 in_pid_list_p (struct simple_pid_list
*list
, int pid
)
311 struct simple_pid_list
*p
;
313 for (p
= list
; p
!= NULL
; p
= p
->next
)
320 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
322 struct simple_pid_list
**p
;
324 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
325 if ((*p
)->pid
== pid
)
327 struct simple_pid_list
*next
= (*p
)->next
;
329 *statusp
= (*p
)->status
;
338 /* A helper function for linux_test_for_tracefork, called after fork (). */
341 linux_tracefork_child (void)
343 ptrace (PTRACE_TRACEME
, 0, 0, 0);
344 kill (getpid (), SIGSTOP
);
349 /* Wrapper function for waitpid which handles EINTR. */
352 my_waitpid (int pid
, int *statusp
, int flags
)
358 ret
= waitpid (pid
, statusp
, flags
);
360 while (ret
== -1 && errno
== EINTR
);
365 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
367 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
368 we know that the feature is not available. This may change the tracing
369 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
371 However, if it succeeds, we don't know for sure that the feature is
372 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
373 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
374 fork tracing, and let it fork. If the process exits, we assume that we
375 can't use TRACEFORK; if we get the fork notification, and we can extract
376 the new child's PID, then we assume that we can. */
379 linux_test_for_tracefork (int original_pid
)
381 int child_pid
, ret
, status
;
385 /* We don't want those ptrace calls to be interrupted. */
386 block_child_signals (&prev_mask
);
388 linux_supports_tracefork_flag
= 0;
389 linux_supports_tracevforkdone_flag
= 0;
391 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
394 restore_child_signals_mask (&prev_mask
);
400 perror_with_name (("fork"));
403 linux_tracefork_child ();
405 ret
= my_waitpid (child_pid
, &status
, 0);
407 perror_with_name (("waitpid"));
408 else if (ret
!= child_pid
)
409 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
410 if (! WIFSTOPPED (status
))
411 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."),
414 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
417 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
420 warning (_("linux_test_for_tracefork: failed to kill child"));
421 restore_child_signals_mask (&prev_mask
);
425 ret
= my_waitpid (child_pid
, &status
, 0);
426 if (ret
!= child_pid
)
427 warning (_("linux_test_for_tracefork: failed "
428 "to wait for killed child"));
429 else if (!WIFSIGNALED (status
))
430 warning (_("linux_test_for_tracefork: unexpected "
431 "wait status 0x%x from killed child"), status
);
433 restore_child_signals_mask (&prev_mask
);
437 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
438 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
439 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
440 linux_supports_tracevforkdone_flag
= (ret
== 0);
442 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
444 warning (_("linux_test_for_tracefork: failed to resume child"));
446 ret
= my_waitpid (child_pid
, &status
, 0);
448 if (ret
== child_pid
&& WIFSTOPPED (status
)
449 && status
>> 16 == PTRACE_EVENT_FORK
)
452 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
453 if (ret
== 0 && second_pid
!= 0)
457 linux_supports_tracefork_flag
= 1;
458 my_waitpid (second_pid
, &second_status
, 0);
459 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
461 warning (_("linux_test_for_tracefork: "
462 "failed to kill second child"));
463 my_waitpid (second_pid
, &status
, 0);
467 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
468 "(%d, status 0x%x)"), ret
, status
);
470 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
472 warning (_("linux_test_for_tracefork: failed to kill child"));
473 my_waitpid (child_pid
, &status
, 0);
475 restore_child_signals_mask (&prev_mask
);
478 /* Determine if PTRACE_O_TRACESYSGOOD can be used to follow syscalls.
480 We try to enable syscall tracing on ORIGINAL_PID. If this fails,
481 we know that the feature is not available. This may change the tracing
482 options for ORIGINAL_PID, but we'll be setting them shortly anyway. */
485 linux_test_for_tracesysgood (int original_pid
)
490 /* We don't want those ptrace calls to be interrupted. */
491 block_child_signals (&prev_mask
);
493 linux_supports_tracesysgood_flag
= 0;
495 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACESYSGOOD
);
499 linux_supports_tracesysgood_flag
= 1;
501 restore_child_signals_mask (&prev_mask
);
504 /* Determine wether we support PTRACE_O_TRACESYSGOOD option available.
505 This function also sets linux_supports_tracesysgood_flag. */
508 linux_supports_tracesysgood (int pid
)
510 if (linux_supports_tracesysgood_flag
== -1)
511 linux_test_for_tracesysgood (pid
);
512 return linux_supports_tracesysgood_flag
;
515 /* Return non-zero iff we have tracefork functionality available.
516 This function also sets linux_supports_tracefork_flag. */
519 linux_supports_tracefork (int pid
)
521 if (linux_supports_tracefork_flag
== -1)
522 linux_test_for_tracefork (pid
);
523 return linux_supports_tracefork_flag
;
527 linux_supports_tracevforkdone (int pid
)
529 if (linux_supports_tracefork_flag
== -1)
530 linux_test_for_tracefork (pid
);
531 return linux_supports_tracevforkdone_flag
;
535 linux_enable_tracesysgood (ptid_t ptid
)
537 int pid
= ptid_get_lwp (ptid
);
540 pid
= ptid_get_pid (ptid
);
542 if (linux_supports_tracesysgood (pid
) == 0)
545 current_ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
547 ptrace (PTRACE_SETOPTIONS
, pid
, 0, current_ptrace_options
);
552 linux_enable_event_reporting (ptid_t ptid
)
554 int pid
= ptid_get_lwp (ptid
);
557 pid
= ptid_get_pid (ptid
);
559 if (! linux_supports_tracefork (pid
))
562 current_ptrace_options
|= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
563 | PTRACE_O_TRACEEXEC
| PTRACE_O_TRACECLONE
;
565 if (linux_supports_tracevforkdone (pid
))
566 current_ptrace_options
|= PTRACE_O_TRACEVFORKDONE
;
568 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
569 read-only process state. */
571 ptrace (PTRACE_SETOPTIONS
, pid
, 0, current_ptrace_options
);
575 linux_child_post_attach (int pid
)
577 linux_enable_event_reporting (pid_to_ptid (pid
));
578 linux_enable_tracesysgood (pid_to_ptid (pid
));
582 linux_child_post_startup_inferior (ptid_t ptid
)
584 linux_enable_event_reporting (ptid
);
585 linux_enable_tracesysgood (ptid
);
588 /* Return the number of known LWPs in the tgid given by PID. */
596 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
597 if (ptid_get_pid (lp
->ptid
) == pid
)
603 /* Call delete_lwp with prototype compatible for make_cleanup. */
606 delete_lwp_cleanup (void *lp_voidp
)
608 struct lwp_info
*lp
= lp_voidp
;
610 delete_lwp (lp
->ptid
);
614 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
618 int parent_pid
, child_pid
;
620 block_child_signals (&prev_mask
);
622 has_vforked
= (inferior_thread ()->pending_follow
.kind
623 == TARGET_WAITKIND_VFORKED
);
624 parent_pid
= ptid_get_lwp (inferior_ptid
);
626 parent_pid
= ptid_get_pid (inferior_ptid
);
627 child_pid
= PIDGET (inferior_thread ()->pending_follow
.value
.related_pid
);
630 linux_enable_event_reporting (pid_to_ptid (child_pid
));
633 && !non_stop
/* Non-stop always resumes both branches. */
634 && (!target_is_async_p () || sync_execution
)
635 && !(follow_child
|| detach_fork
|| sched_multi
))
637 /* The parent stays blocked inside the vfork syscall until the
638 child execs or exits. If we don't let the child run, then
639 the parent stays blocked. If we're telling the parent to run
640 in the foreground, the user will not be able to ctrl-c to get
641 back the terminal, effectively hanging the debug session. */
642 fprintf_filtered (gdb_stderr
, _("\
643 Can not resume the parent process over vfork in the foreground while\n\
644 holding the child stopped. Try \"set detach-on-fork\" or \
645 \"set schedule-multiple\".\n"));
646 /* FIXME output string > 80 columns. */
652 struct lwp_info
*child_lp
= NULL
;
654 /* We're already attached to the parent, by default. */
656 /* Detach new forked process? */
659 struct cleanup
*old_chain
;
661 /* Before detaching from the child, remove all breakpoints
662 from it. If we forked, then this has already been taken
663 care of by infrun.c. If we vforked however, any
664 breakpoint inserted in the parent is visible in the
665 child, even those added while stopped in a vfork
666 catchpoint. This will remove the breakpoints from the
667 parent also, but they'll be reinserted below. */
670 /* keep breakpoints list in sync. */
671 remove_breakpoints_pid (GET_PID (inferior_ptid
));
674 if (info_verbose
|| debug_linux_nat
)
676 target_terminal_ours ();
677 fprintf_filtered (gdb_stdlog
,
678 "Detaching after fork from "
679 "child process %d.\n",
683 old_chain
= save_inferior_ptid ();
684 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
686 child_lp
= add_lwp (inferior_ptid
);
687 child_lp
->stopped
= 1;
688 child_lp
->last_resume_kind
= resume_stop
;
689 make_cleanup (delete_lwp_cleanup
, child_lp
);
691 /* CHILD_LP has new PID, therefore linux_nat_new_thread is not called for it.
692 See i386_inferior_data_get for the Linux kernel specifics.
693 Ensure linux_nat_prepare_to_resume will reset the hardware debug
694 registers. It is done by the linux_nat_new_thread call, which is
695 being skipped in add_lwp above for the first lwp of a pid. */
696 gdb_assert (num_lwps (GET_PID (child_lp
->ptid
)) == 1);
697 if (linux_nat_new_thread
!= NULL
)
698 linux_nat_new_thread (child_lp
);
700 if (linux_nat_prepare_to_resume
!= NULL
)
701 linux_nat_prepare_to_resume (child_lp
);
702 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
704 do_cleanups (old_chain
);
708 struct inferior
*parent_inf
, *child_inf
;
709 struct cleanup
*old_chain
;
711 /* Add process to GDB's tables. */
712 child_inf
= add_inferior (child_pid
);
714 parent_inf
= current_inferior ();
715 child_inf
->attach_flag
= parent_inf
->attach_flag
;
716 copy_terminal_info (child_inf
, parent_inf
);
718 old_chain
= save_inferior_ptid ();
719 save_current_program_space ();
721 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
722 add_thread (inferior_ptid
);
723 child_lp
= add_lwp (inferior_ptid
);
724 child_lp
->stopped
= 1;
725 child_lp
->last_resume_kind
= resume_stop
;
727 /* If this is a vfork child, then the address-space is
728 shared with the parent. */
731 child_inf
->pspace
= parent_inf
->pspace
;
732 child_inf
->aspace
= parent_inf
->aspace
;
734 /* The parent will be frozen until the child is done
735 with the shared region. Keep track of the
737 child_inf
->vfork_parent
= parent_inf
;
738 child_inf
->pending_detach
= 0;
739 parent_inf
->vfork_child
= child_inf
;
740 parent_inf
->pending_detach
= 0;
744 child_inf
->aspace
= new_address_space ();
745 child_inf
->pspace
= add_program_space (child_inf
->aspace
);
746 child_inf
->removable
= 1;
747 set_current_program_space (child_inf
->pspace
);
748 clone_program_space (child_inf
->pspace
, parent_inf
->pspace
);
750 /* Let the shared library layer (solib-svr4) learn about
751 this new process, relocate the cloned exec, pull in
752 shared libraries, and install the solib event
753 breakpoint. If a "cloned-VM" event was propagated
754 better throughout the core, this wouldn't be
756 solib_create_inferior_hook (0);
759 /* Let the thread_db layer learn about this new process. */
760 check_for_thread_db ();
762 do_cleanups (old_chain
);
767 struct lwp_info
*parent_lp
;
768 struct inferior
*parent_inf
;
770 parent_inf
= current_inferior ();
772 /* If we detached from the child, then we have to be careful
773 to not insert breakpoints in the parent until the child
774 is done with the shared memory region. However, if we're
775 staying attached to the child, then we can and should
776 insert breakpoints, so that we can debug it. A
777 subsequent child exec or exit is enough to know when does
778 the child stops using the parent's address space. */
779 parent_inf
->waiting_for_vfork_done
= detach_fork
;
780 parent_inf
->pspace
->breakpoints_not_allowed
= detach_fork
;
782 parent_lp
= find_lwp_pid (pid_to_ptid (parent_pid
));
783 gdb_assert (linux_supports_tracefork_flag
>= 0);
785 if (linux_supports_tracevforkdone (0))
788 fprintf_unfiltered (gdb_stdlog
,
789 "LCFF: waiting for VFORK_DONE on %d\n",
791 parent_lp
->stopped
= 1;
793 /* We'll handle the VFORK_DONE event like any other
794 event, in target_wait. */
798 /* We can't insert breakpoints until the child has
799 finished with the shared memory region. We need to
800 wait until that happens. Ideal would be to just
802 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
803 - waitpid (parent_pid, &status, __WALL);
804 However, most architectures can't handle a syscall
805 being traced on the way out if it wasn't traced on
808 We might also think to loop, continuing the child
809 until it exits or gets a SIGTRAP. One problem is
810 that the child might call ptrace with PTRACE_TRACEME.
812 There's no simple and reliable way to figure out when
813 the vforked child will be done with its copy of the
814 shared memory. We could step it out of the syscall,
815 two instructions, let it go, and then single-step the
816 parent once. When we have hardware single-step, this
817 would work; with software single-step it could still
818 be made to work but we'd have to be able to insert
819 single-step breakpoints in the child, and we'd have
820 to insert -just- the single-step breakpoint in the
821 parent. Very awkward.
823 In the end, the best we can do is to make sure it
824 runs for a little while. Hopefully it will be out of
825 range of any breakpoints we reinsert. Usually this
826 is only the single-step breakpoint at vfork's return
830 fprintf_unfiltered (gdb_stdlog
,
831 "LCFF: no VFORK_DONE "
832 "support, sleeping a bit\n");
836 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
837 and leave it pending. The next linux_nat_resume call
838 will notice a pending event, and bypasses actually
839 resuming the inferior. */
840 parent_lp
->status
= 0;
841 parent_lp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
842 parent_lp
->stopped
= 1;
844 /* If we're in async mode, need to tell the event loop
845 there's something here to process. */
846 if (target_can_async_p ())
853 struct inferior
*parent_inf
, *child_inf
;
854 struct lwp_info
*child_lp
;
855 struct program_space
*parent_pspace
;
857 if (info_verbose
|| debug_linux_nat
)
859 target_terminal_ours ();
861 fprintf_filtered (gdb_stdlog
,
862 _("Attaching after process %d "
863 "vfork to child process %d.\n"),
864 parent_pid
, child_pid
);
866 fprintf_filtered (gdb_stdlog
,
867 _("Attaching after process %d "
868 "fork to child process %d.\n"),
869 parent_pid
, child_pid
);
872 /* Add the new inferior first, so that the target_detach below
873 doesn't unpush the target. */
875 child_inf
= add_inferior (child_pid
);
877 parent_inf
= current_inferior ();
878 child_inf
->attach_flag
= parent_inf
->attach_flag
;
879 copy_terminal_info (child_inf
, parent_inf
);
881 parent_pspace
= parent_inf
->pspace
;
883 /* If we're vforking, we want to hold on to the parent until the
884 child exits or execs. At child exec or exit time we can
885 remove the old breakpoints from the parent and detach or
886 resume debugging it. Otherwise, detach the parent now; we'll
887 want to reuse it's program/address spaces, but we can't set
888 them to the child before removing breakpoints from the
889 parent, otherwise, the breakpoints module could decide to
890 remove breakpoints from the wrong process (since they'd be
891 assigned to the same address space). */
895 gdb_assert (child_inf
->vfork_parent
== NULL
);
896 gdb_assert (parent_inf
->vfork_child
== NULL
);
897 child_inf
->vfork_parent
= parent_inf
;
898 child_inf
->pending_detach
= 0;
899 parent_inf
->vfork_child
= child_inf
;
900 parent_inf
->pending_detach
= detach_fork
;
901 parent_inf
->waiting_for_vfork_done
= 0;
903 else if (detach_fork
)
904 target_detach (NULL
, 0);
906 /* Note that the detach above makes PARENT_INF dangling. */
908 /* Add the child thread to the appropriate lists, and switch to
909 this new thread, before cloning the program space, and
910 informing the solib layer about this new process. */
912 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
913 add_thread (inferior_ptid
);
914 child_lp
= add_lwp (inferior_ptid
);
915 child_lp
->stopped
= 1;
916 child_lp
->last_resume_kind
= resume_stop
;
918 /* If this is a vfork child, then the address-space is shared
919 with the parent. If we detached from the parent, then we can
920 reuse the parent's program/address spaces. */
921 if (has_vforked
|| detach_fork
)
923 child_inf
->pspace
= parent_pspace
;
924 child_inf
->aspace
= child_inf
->pspace
->aspace
;
928 child_inf
->aspace
= new_address_space ();
929 child_inf
->pspace
= add_program_space (child_inf
->aspace
);
930 child_inf
->removable
= 1;
931 set_current_program_space (child_inf
->pspace
);
932 clone_program_space (child_inf
->pspace
, parent_pspace
);
934 /* Let the shared library layer (solib-svr4) learn about
935 this new process, relocate the cloned exec, pull in
936 shared libraries, and install the solib event breakpoint.
937 If a "cloned-VM" event was propagated better throughout
938 the core, this wouldn't be required. */
939 solib_create_inferior_hook (0);
942 /* Let the thread_db layer learn about this new process. */
943 check_for_thread_db ();
946 restore_child_signals_mask (&prev_mask
);
952 linux_child_insert_fork_catchpoint (int pid
)
954 return !linux_supports_tracefork (pid
);
958 linux_child_remove_fork_catchpoint (int pid
)
964 linux_child_insert_vfork_catchpoint (int pid
)
966 return !linux_supports_tracefork (pid
);
970 linux_child_remove_vfork_catchpoint (int pid
)
976 linux_child_insert_exec_catchpoint (int pid
)
978 return !linux_supports_tracefork (pid
);
982 linux_child_remove_exec_catchpoint (int pid
)
988 linux_child_set_syscall_catchpoint (int pid
, int needed
, int any_count
,
989 int table_size
, int *table
)
991 if (!linux_supports_tracesysgood (pid
))
994 /* On GNU/Linux, we ignore the arguments. It means that we only
995 enable the syscall catchpoints, but do not disable them.
997 Also, we do not use the `table' information because we do not
998 filter system calls here. We let GDB do the logic for us. */
1002 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
1003 are processes sharing the same VM space. A multi-threaded process
1004 is basically a group of such processes. However, such a grouping
1005 is almost entirely a user-space issue; the kernel doesn't enforce
1006 such a grouping at all (this might change in the future). In
1007 general, we'll rely on the threads library (i.e. the GNU/Linux
1008 Threads library) to provide such a grouping.
1010 It is perfectly well possible to write a multi-threaded application
1011 without the assistance of a threads library, by using the clone
1012 system call directly. This module should be able to give some
1013 rudimentary support for debugging such applications if developers
1014 specify the CLONE_PTRACE flag in the clone system call, and are
1015 using the Linux kernel 2.4 or above.
1017 Note that there are some peculiarities in GNU/Linux that affect
1020 - In general one should specify the __WCLONE flag to waitpid in
1021 order to make it report events for any of the cloned processes
1022 (and leave it out for the initial process). However, if a cloned
1023 process has exited the exit status is only reported if the
1024 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
1025 we cannot use it since GDB must work on older systems too.
1027 - When a traced, cloned process exits and is waited for by the
1028 debugger, the kernel reassigns it to the original parent and
1029 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
1030 library doesn't notice this, which leads to the "zombie problem":
1031 When debugged a multi-threaded process that spawns a lot of
1032 threads will run out of processes, even if the threads exit,
1033 because the "zombies" stay around. */
1035 /* List of known LWPs. */
1036 struct lwp_info
*lwp_list
;
1039 /* Original signal mask. */
1040 static sigset_t normal_mask
;
1042 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
1043 _initialize_linux_nat. */
1044 static sigset_t suspend_mask
;
1046 /* Signals to block to make that sigsuspend work. */
1047 static sigset_t blocked_mask
;
1049 /* SIGCHLD action. */
1050 struct sigaction sigchld_action
;
1052 /* Block child signals (SIGCHLD and linux threads signals), and store
1053 the previous mask in PREV_MASK. */
1056 block_child_signals (sigset_t
*prev_mask
)
1058 /* Make sure SIGCHLD is blocked. */
1059 if (!sigismember (&blocked_mask
, SIGCHLD
))
1060 sigaddset (&blocked_mask
, SIGCHLD
);
1062 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
1065 /* Restore child signals mask, previously returned by
1066 block_child_signals. */
1069 restore_child_signals_mask (sigset_t
*prev_mask
)
1071 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
1074 /* Mask of signals to pass directly to the inferior. */
1075 static sigset_t pass_mask
;
1077 /* Update signals to pass to the inferior. */
1079 linux_nat_pass_signals (int numsigs
, unsigned char *pass_signals
)
1083 sigemptyset (&pass_mask
);
1085 for (signo
= 1; signo
< NSIG
; signo
++)
1087 int target_signo
= target_signal_from_host (signo
);
1088 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
1089 sigaddset (&pass_mask
, signo
);
1095 /* Prototypes for local functions. */
1096 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
1097 static int linux_thread_alive (ptid_t ptid
);
1098 static char *linux_child_pid_to_exec_file (int pid
);
1101 /* Convert wait status STATUS to a string. Used for printing debug
1105 status_to_str (int status
)
1107 static char buf
[64];
1109 if (WIFSTOPPED (status
))
1111 if (WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
1112 snprintf (buf
, sizeof (buf
), "%s (stopped at syscall)",
1113 strsignal (SIGTRAP
));
1115 snprintf (buf
, sizeof (buf
), "%s (stopped)",
1116 strsignal (WSTOPSIG (status
)));
1118 else if (WIFSIGNALED (status
))
1119 snprintf (buf
, sizeof (buf
), "%s (terminated)",
1120 strsignal (WTERMSIG (status
)));
1122 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
1127 /* Destroy and free LP. */
1130 lwp_free (struct lwp_info
*lp
)
1132 xfree (lp
->arch_private
);
1136 /* Remove all LWPs belong to PID from the lwp list. */
1139 purge_lwp_list (int pid
)
1141 struct lwp_info
*lp
, *lpprev
, *lpnext
;
1145 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1149 if (ptid_get_pid (lp
->ptid
) == pid
)
1152 lwp_list
= lp
->next
;
1154 lpprev
->next
= lp
->next
;
1163 /* Add the LWP specified by PID to the list. Return a pointer to the
1164 structure describing the new LWP. The LWP should already be stopped
1165 (with an exception for the very first LWP). */
1167 static struct lwp_info
*
1168 add_lwp (ptid_t ptid
)
1170 struct lwp_info
*lp
;
1172 gdb_assert (is_lwp (ptid
));
1174 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
1176 memset (lp
, 0, sizeof (struct lwp_info
));
1178 lp
->last_resume_kind
= resume_continue
;
1179 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1184 lp
->next
= lwp_list
;
1187 /* Let the arch specific bits know about this new thread. Current
1188 clients of this callback take the opportunity to install
1189 watchpoints in the new thread. Don't do this for the first
1190 thread though. If we're spawning a child ("run"), the thread
1191 executes the shell wrapper first, and we shouldn't touch it until
1192 it execs the program we want to debug. For "attach", it'd be
1193 okay to call the callback, but it's not necessary, because
1194 watchpoints can't yet have been inserted into the inferior. */
1195 if (num_lwps (GET_PID (ptid
)) > 1 && linux_nat_new_thread
!= NULL
)
1196 linux_nat_new_thread (lp
);
1201 /* Remove the LWP specified by PID from the list. */
1204 delete_lwp (ptid_t ptid
)
1206 struct lwp_info
*lp
, *lpprev
;
1210 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
1211 if (ptid_equal (lp
->ptid
, ptid
))
1218 lpprev
->next
= lp
->next
;
1220 lwp_list
= lp
->next
;
1225 /* Return a pointer to the structure describing the LWP corresponding
1226 to PID. If no corresponding LWP could be found, return NULL. */
1228 static struct lwp_info
*
1229 find_lwp_pid (ptid_t ptid
)
1231 struct lwp_info
*lp
;
1235 lwp
= GET_LWP (ptid
);
1237 lwp
= GET_PID (ptid
);
1239 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1240 if (lwp
== GET_LWP (lp
->ptid
))
1246 /* Call CALLBACK with its second argument set to DATA for every LWP in
1247 the list. If CALLBACK returns 1 for a particular LWP, return a
1248 pointer to the structure describing that LWP immediately.
1249 Otherwise return NULL. */
1252 iterate_over_lwps (ptid_t filter
,
1253 int (*callback
) (struct lwp_info
*, void *),
1256 struct lwp_info
*lp
, *lpnext
;
1258 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1262 if (ptid_match (lp
->ptid
, filter
))
1264 if ((*callback
) (lp
, data
))
1272 /* Iterate like iterate_over_lwps does except when forking-off a child call
1273 CALLBACK with CALLBACK_DATA specifically only for that new child PID. */
1276 linux_nat_iterate_watchpoint_lwps
1277 (linux_nat_iterate_watchpoint_lwps_ftype callback
, void *callback_data
)
1279 int inferior_pid
= ptid_get_pid (inferior_ptid
);
1280 struct inferior
*inf
= current_inferior ();
1282 if (inf
->pid
== inferior_pid
)
1284 /* Iterate all the threads of the current inferior. Without specifying
1285 INFERIOR_PID it would iterate all threads of all inferiors, which is
1286 inappropriate for watchpoints. */
1288 iterate_over_lwps (pid_to_ptid (inferior_pid
), callback
, callback_data
);
1292 /* Detaching a new child PID temporarily present in INFERIOR_PID. */
1294 struct lwp_info
*child_lp
;
1295 struct cleanup
*old_chain
;
1296 pid_t child_pid
= GET_PID (inferior_ptid
);
1297 ptid_t child_ptid
= ptid_build (child_pid
, child_pid
, 0);
1299 gdb_assert (!is_lwp (inferior_ptid
));
1300 gdb_assert (find_lwp_pid (child_ptid
) == NULL
);
1301 child_lp
= add_lwp (child_ptid
);
1302 child_lp
->stopped
= 1;
1303 child_lp
->last_resume_kind
= resume_stop
;
1304 old_chain
= make_cleanup (delete_lwp_cleanup
, child_lp
);
1306 callback (child_lp
, callback_data
);
1308 do_cleanups (old_chain
);
1312 /* Update our internal state when changing from one checkpoint to
1313 another indicated by NEW_PTID. We can only switch single-threaded
1314 applications, so we only create one new LWP, and the previous list
1318 linux_nat_switch_fork (ptid_t new_ptid
)
1320 struct lwp_info
*lp
;
1322 purge_lwp_list (GET_PID (inferior_ptid
));
1324 lp
= add_lwp (new_ptid
);
1327 /* This changes the thread's ptid while preserving the gdb thread
1328 num. Also changes the inferior pid, while preserving the
1330 thread_change_ptid (inferior_ptid
, new_ptid
);
1332 /* We've just told GDB core that the thread changed target id, but,
1333 in fact, it really is a different thread, with different register
1335 registers_changed ();
1338 /* Handle the exit of a single thread LP. */
1341 exit_lwp (struct lwp_info
*lp
)
1343 struct thread_info
*th
= find_thread_ptid (lp
->ptid
);
1347 if (print_thread_events
)
1348 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1350 delete_thread (lp
->ptid
);
1353 delete_lwp (lp
->ptid
);
1356 /* Detect `T (stopped)' in `/proc/PID/status'.
1357 Other states including `T (tracing stop)' are reported as false. */
1360 pid_is_stopped (pid_t pid
)
1366 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) pid
);
1367 status_file
= fopen (buf
, "r");
1368 if (status_file
!= NULL
)
1372 while (fgets (buf
, sizeof (buf
), status_file
))
1374 if (strncmp (buf
, "State:", 6) == 0)
1380 if (have_state
&& strstr (buf
, "T (stopped)") != NULL
)
1382 fclose (status_file
);
1387 /* Wait for the LWP specified by LP, which we have just attached to.
1388 Returns a wait status for that LWP, to cache. */
1391 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1394 pid_t new_pid
, pid
= GET_LWP (ptid
);
1397 if (pid_is_stopped (pid
))
1399 if (debug_linux_nat
)
1400 fprintf_unfiltered (gdb_stdlog
,
1401 "LNPAW: Attaching to a stopped process\n");
1403 /* The process is definitely stopped. It is in a job control
1404 stop, unless the kernel predates the TASK_STOPPED /
1405 TASK_TRACED distinction, in which case it might be in a
1406 ptrace stop. Make sure it is in a ptrace stop; from there we
1407 can kill it, signal it, et cetera.
1409 First make sure there is a pending SIGSTOP. Since we are
1410 already attached, the process can not transition from stopped
1411 to running without a PTRACE_CONT; so we know this signal will
1412 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1413 probably already in the queue (unless this kernel is old
1414 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1415 is not an RT signal, it can only be queued once. */
1416 kill_lwp (pid
, SIGSTOP
);
1418 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1419 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1420 ptrace (PTRACE_CONT
, pid
, 0, 0);
1423 /* Make sure the initial process is stopped. The user-level threads
1424 layer might want to poke around in the inferior, and that won't
1425 work if things haven't stabilized yet. */
1426 new_pid
= my_waitpid (pid
, &status
, 0);
1427 if (new_pid
== -1 && errno
== ECHILD
)
1430 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1432 /* Try again with __WCLONE to check cloned processes. */
1433 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1437 gdb_assert (pid
== new_pid
);
1439 if (!WIFSTOPPED (status
))
1441 /* The pid we tried to attach has apparently just exited. */
1442 if (debug_linux_nat
)
1443 fprintf_unfiltered (gdb_stdlog
, "LNPAW: Failed to stop %d: %s",
1444 pid
, status_to_str (status
));
1448 if (WSTOPSIG (status
) != SIGSTOP
)
1451 if (debug_linux_nat
)
1452 fprintf_unfiltered (gdb_stdlog
,
1453 "LNPAW: Received %s after attaching\n",
1454 status_to_str (status
));
1460 /* Attach to the LWP specified by PID. Return 0 if successful, -1 if
1461 the new LWP could not be attached, or 1 if we're already auto
1462 attached to this thread, but haven't processed the
1463 PTRACE_EVENT_CLONE event of its parent thread, so we just ignore
1464 its existance, without considering it an error. */
1467 lin_lwp_attach_lwp (ptid_t ptid
)
1469 struct lwp_info
*lp
;
1473 gdb_assert (is_lwp (ptid
));
1475 block_child_signals (&prev_mask
);
1477 lp
= find_lwp_pid (ptid
);
1478 lwpid
= GET_LWP (ptid
);
1480 /* We assume that we're already attached to any LWP that has an id
1481 equal to the overall process id, and to any LWP that is already
1482 in our list of LWPs. If we're not seeing exit events from threads
1483 and we've had PID wraparound since we last tried to stop all threads,
1484 this assumption might be wrong; fortunately, this is very unlikely
1486 if (lwpid
!= GET_PID (ptid
) && lp
== NULL
)
1488 int status
, cloned
= 0, signalled
= 0;
1490 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) < 0)
1492 if (linux_supports_tracefork_flag
)
1494 /* If we haven't stopped all threads when we get here,
1495 we may have seen a thread listed in thread_db's list,
1496 but not processed the PTRACE_EVENT_CLONE yet. If
1497 that's the case, ignore this new thread, and let
1498 normal event handling discover it later. */
1499 if (in_pid_list_p (stopped_pids
, lwpid
))
1501 /* We've already seen this thread stop, but we
1502 haven't seen the PTRACE_EVENT_CLONE extended
1504 restore_child_signals_mask (&prev_mask
);
1512 /* See if we've got a stop for this new child
1513 pending. If so, we're already attached. */
1514 new_pid
= my_waitpid (lwpid
, &status
, WNOHANG
);
1515 if (new_pid
== -1 && errno
== ECHILD
)
1516 new_pid
= my_waitpid (lwpid
, &status
, __WCLONE
| WNOHANG
);
1519 if (WIFSTOPPED (status
))
1520 add_to_pid_list (&stopped_pids
, lwpid
, status
);
1522 restore_child_signals_mask (&prev_mask
);
1528 /* If we fail to attach to the thread, issue a warning,
1529 but continue. One way this can happen is if thread
1530 creation is interrupted; as of Linux kernel 2.6.19, a
1531 bug may place threads in the thread list and then fail
1533 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1534 safe_strerror (errno
));
1535 restore_child_signals_mask (&prev_mask
);
1539 if (debug_linux_nat
)
1540 fprintf_unfiltered (gdb_stdlog
,
1541 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1542 target_pid_to_str (ptid
));
1544 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1545 if (!WIFSTOPPED (status
))
1547 restore_child_signals_mask (&prev_mask
);
1551 lp
= add_lwp (ptid
);
1553 lp
->cloned
= cloned
;
1554 lp
->signalled
= signalled
;
1555 if (WSTOPSIG (status
) != SIGSTOP
)
1558 lp
->status
= status
;
1561 target_post_attach (GET_LWP (lp
->ptid
));
1563 if (debug_linux_nat
)
1565 fprintf_unfiltered (gdb_stdlog
,
1566 "LLAL: waitpid %s received %s\n",
1567 target_pid_to_str (ptid
),
1568 status_to_str (status
));
1573 /* We assume that the LWP representing the original process is
1574 already stopped. Mark it as stopped in the data structure
1575 that the GNU/linux ptrace layer uses to keep track of
1576 threads. Note that this won't have already been done since
1577 the main thread will have, we assume, been stopped by an
1578 attach from a different layer. */
1580 lp
= add_lwp (ptid
);
1584 lp
->last_resume_kind
= resume_stop
;
1585 restore_child_signals_mask (&prev_mask
);
1590 linux_nat_create_inferior (struct target_ops
*ops
,
1591 char *exec_file
, char *allargs
, char **env
,
1594 #ifdef HAVE_PERSONALITY
1595 int personality_orig
= 0, personality_set
= 0;
1596 #endif /* HAVE_PERSONALITY */
1598 /* The fork_child mechanism is synchronous and calls target_wait, so
1599 we have to mask the async mode. */
1601 #ifdef HAVE_PERSONALITY
1602 if (disable_randomization
)
1605 personality_orig
= personality (0xffffffff);
1606 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
1608 personality_set
= 1;
1609 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
1611 if (errno
!= 0 || (personality_set
1612 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
1613 warning (_("Error disabling address space randomization: %s"),
1614 safe_strerror (errno
));
1616 #endif /* HAVE_PERSONALITY */
1618 /* Make sure we report all signals during startup. */
1619 linux_nat_pass_signals (0, NULL
);
1621 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1623 #ifdef HAVE_PERSONALITY
1624 if (personality_set
)
1627 personality (personality_orig
);
1629 warning (_("Error restoring address space randomization: %s"),
1630 safe_strerror (errno
));
1632 #endif /* HAVE_PERSONALITY */
1636 linux_nat_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1638 struct lwp_info
*lp
;
1642 /* Make sure we report all signals during attach. */
1643 linux_nat_pass_signals (0, NULL
);
1645 linux_ops
->to_attach (ops
, args
, from_tty
);
1647 /* The ptrace base target adds the main thread with (pid,0,0)
1648 format. Decorate it with lwp info. */
1649 ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1650 thread_change_ptid (inferior_ptid
, ptid
);
1652 /* Add the initial process as the first LWP to the list. */
1653 lp
= add_lwp (ptid
);
1655 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1657 if (!WIFSTOPPED (status
))
1659 if (WIFEXITED (status
))
1661 int exit_code
= WEXITSTATUS (status
);
1663 target_terminal_ours ();
1664 target_mourn_inferior ();
1666 error (_("Unable to attach: program exited normally."));
1668 error (_("Unable to attach: program exited with code %d."),
1671 else if (WIFSIGNALED (status
))
1673 enum target_signal signo
;
1675 target_terminal_ours ();
1676 target_mourn_inferior ();
1678 signo
= target_signal_from_host (WTERMSIG (status
));
1679 error (_("Unable to attach: program terminated with signal "
1681 target_signal_to_name (signo
),
1682 target_signal_to_string (signo
));
1685 internal_error (__FILE__
, __LINE__
,
1686 _("unexpected status %d for PID %ld"),
1687 status
, (long) GET_LWP (ptid
));
1692 /* Save the wait status to report later. */
1694 if (debug_linux_nat
)
1695 fprintf_unfiltered (gdb_stdlog
,
1696 "LNA: waitpid %ld, saving status %s\n",
1697 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1699 lp
->status
= status
;
1701 if (target_can_async_p ())
1702 target_async (inferior_event_handler
, 0);
1705 /* Get pending status of LP. */
1707 get_pending_status (struct lwp_info
*lp
, int *status
)
1709 enum target_signal signo
= TARGET_SIGNAL_0
;
1711 /* If we paused threads momentarily, we may have stored pending
1712 events in lp->status or lp->waitstatus (see stop_wait_callback),
1713 and GDB core hasn't seen any signal for those threads.
1714 Otherwise, the last signal reported to the core is found in the
1715 thread object's stop_signal.
1717 There's a corner case that isn't handled here at present. Only
1718 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1719 stop_signal make sense as a real signal to pass to the inferior.
1720 Some catchpoint related events, like
1721 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1722 to TARGET_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1723 those traps are debug API (ptrace in our case) related and
1724 induced; the inferior wouldn't see them if it wasn't being
1725 traced. Hence, we should never pass them to the inferior, even
1726 when set to pass state. Since this corner case isn't handled by
1727 infrun.c when proceeding with a signal, for consistency, neither
1728 do we handle it here (or elsewhere in the file we check for
1729 signal pass state). Normally SIGTRAP isn't set to pass state, so
1730 this is really a corner case. */
1732 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
1733 signo
= TARGET_SIGNAL_0
; /* a pending ptrace event, not a real signal. */
1734 else if (lp
->status
)
1735 signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1736 else if (non_stop
&& !is_executing (lp
->ptid
))
1738 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1740 signo
= tp
->suspend
.stop_signal
;
1744 struct target_waitstatus last
;
1747 get_last_target_status (&last_ptid
, &last
);
1749 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1751 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1753 signo
= tp
->suspend
.stop_signal
;
1759 if (signo
== TARGET_SIGNAL_0
)
1761 if (debug_linux_nat
)
1762 fprintf_unfiltered (gdb_stdlog
,
1763 "GPT: lwp %s has no pending signal\n",
1764 target_pid_to_str (lp
->ptid
));
1766 else if (!signal_pass_state (signo
))
1768 if (debug_linux_nat
)
1769 fprintf_unfiltered (gdb_stdlog
,
1770 "GPT: lwp %s had signal %s, "
1771 "but it is in no pass state\n",
1772 target_pid_to_str (lp
->ptid
),
1773 target_signal_to_string (signo
));
1777 *status
= W_STOPCODE (target_signal_to_host (signo
));
1779 if (debug_linux_nat
)
1780 fprintf_unfiltered (gdb_stdlog
,
1781 "GPT: lwp %s has pending signal %s\n",
1782 target_pid_to_str (lp
->ptid
),
1783 target_signal_to_string (signo
));
1790 detach_callback (struct lwp_info
*lp
, void *data
)
1792 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1794 if (debug_linux_nat
&& lp
->status
)
1795 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1796 strsignal (WSTOPSIG (lp
->status
)),
1797 target_pid_to_str (lp
->ptid
));
1799 /* If there is a pending SIGSTOP, get rid of it. */
1802 if (debug_linux_nat
)
1803 fprintf_unfiltered (gdb_stdlog
,
1804 "DC: Sending SIGCONT to %s\n",
1805 target_pid_to_str (lp
->ptid
));
1807 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1811 /* We don't actually detach from the LWP that has an id equal to the
1812 overall process id just yet. */
1813 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1817 /* Pass on any pending signal for this LWP. */
1818 get_pending_status (lp
, &status
);
1820 if (linux_nat_prepare_to_resume
!= NULL
)
1821 linux_nat_prepare_to_resume (lp
);
1823 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1824 WSTOPSIG (status
)) < 0)
1825 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1826 safe_strerror (errno
));
1828 if (debug_linux_nat
)
1829 fprintf_unfiltered (gdb_stdlog
,
1830 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1831 target_pid_to_str (lp
->ptid
),
1832 strsignal (WSTOPSIG (status
)));
1834 delete_lwp (lp
->ptid
);
1841 linux_nat_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1845 struct lwp_info
*main_lwp
;
1847 pid
= GET_PID (inferior_ptid
);
1849 if (target_can_async_p ())
1850 linux_nat_async (NULL
, 0);
1852 /* Stop all threads before detaching. ptrace requires that the
1853 thread is stopped to sucessfully detach. */
1854 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1855 /* ... and wait until all of them have reported back that
1856 they're no longer running. */
1857 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1859 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1861 /* Only the initial process should be left right now. */
1862 gdb_assert (num_lwps (GET_PID (inferior_ptid
)) == 1);
1864 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1866 /* Pass on any pending signal for the last LWP. */
1867 if ((args
== NULL
|| *args
== '\0')
1868 && get_pending_status (main_lwp
, &status
) != -1
1869 && WIFSTOPPED (status
))
1871 /* Put the signal number in ARGS so that inf_ptrace_detach will
1872 pass it along with PTRACE_DETACH. */
1874 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1875 if (debug_linux_nat
)
1876 fprintf_unfiltered (gdb_stdlog
,
1877 "LND: Sending signal %s to %s\n",
1879 target_pid_to_str (main_lwp
->ptid
));
1882 if (linux_nat_prepare_to_resume
!= NULL
)
1883 linux_nat_prepare_to_resume (main_lwp
);
1884 delete_lwp (main_lwp
->ptid
);
1886 if (forks_exist_p ())
1888 /* Multi-fork case. The current inferior_ptid is being detached
1889 from, but there are other viable forks to debug. Detach from
1890 the current fork, and context-switch to the first
1892 linux_fork_detach (args
, from_tty
);
1894 if (non_stop
&& target_can_async_p ())
1895 target_async (inferior_event_handler
, 0);
1898 linux_ops
->to_detach (ops
, args
, from_tty
);
1904 resume_lwp (struct lwp_info
*lp
, int step
)
1908 struct inferior
*inf
= find_inferior_pid (GET_PID (lp
->ptid
));
1910 if (inf
->vfork_child
!= NULL
)
1912 if (debug_linux_nat
)
1913 fprintf_unfiltered (gdb_stdlog
,
1914 "RC: Not resuming %s (vfork parent)\n",
1915 target_pid_to_str (lp
->ptid
));
1917 else if (lp
->status
== 0
1918 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
1920 if (debug_linux_nat
)
1921 fprintf_unfiltered (gdb_stdlog
,
1922 "RC: PTRACE_CONT %s, 0, 0 (resuming sibling)\n",
1923 target_pid_to_str (lp
->ptid
));
1925 if (linux_nat_prepare_to_resume
!= NULL
)
1926 linux_nat_prepare_to_resume (lp
);
1927 linux_ops
->to_resume (linux_ops
,
1928 pid_to_ptid (GET_LWP (lp
->ptid
)),
1929 step
, TARGET_SIGNAL_0
);
1932 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1933 lp
->stopped_by_watchpoint
= 0;
1937 if (debug_linux_nat
)
1938 fprintf_unfiltered (gdb_stdlog
,
1939 "RC: Not resuming sibling %s (has pending)\n",
1940 target_pid_to_str (lp
->ptid
));
1945 if (debug_linux_nat
)
1946 fprintf_unfiltered (gdb_stdlog
,
1947 "RC: Not resuming sibling %s (not stopped)\n",
1948 target_pid_to_str (lp
->ptid
));
1953 resume_callback (struct lwp_info
*lp
, void *data
)
1960 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1963 lp
->last_resume_kind
= resume_stop
;
1968 resume_set_callback (struct lwp_info
*lp
, void *data
)
1971 lp
->last_resume_kind
= resume_continue
;
1976 linux_nat_resume (struct target_ops
*ops
,
1977 ptid_t ptid
, int step
, enum target_signal signo
)
1980 struct lwp_info
*lp
;
1983 if (debug_linux_nat
)
1984 fprintf_unfiltered (gdb_stdlog
,
1985 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1986 step
? "step" : "resume",
1987 target_pid_to_str (ptid
),
1988 (signo
!= TARGET_SIGNAL_0
1989 ? strsignal (target_signal_to_host (signo
)) : "0"),
1990 target_pid_to_str (inferior_ptid
));
1992 block_child_signals (&prev_mask
);
1994 /* A specific PTID means `step only this process id'. */
1995 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
1996 || ptid_is_pid (ptid
));
1998 /* Mark the lwps we're resuming as resumed. */
1999 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
2001 /* See if it's the current inferior that should be handled
2004 lp
= find_lwp_pid (inferior_ptid
);
2006 lp
= find_lwp_pid (ptid
);
2007 gdb_assert (lp
!= NULL
);
2009 /* Remember if we're stepping. */
2011 lp
->last_resume_kind
= step
? resume_step
: resume_continue
;
2013 /* If we have a pending wait status for this thread, there is no
2014 point in resuming the process. But first make sure that
2015 linux_nat_wait won't preemptively handle the event - we
2016 should never take this short-circuit if we are going to
2017 leave LP running, since we have skipped resuming all the
2018 other threads. This bit of code needs to be synchronized
2019 with linux_nat_wait. */
2021 if (lp
->status
&& WIFSTOPPED (lp
->status
))
2024 && WSTOPSIG (lp
->status
)
2025 && sigismember (&pass_mask
, WSTOPSIG (lp
->status
)))
2027 if (debug_linux_nat
)
2028 fprintf_unfiltered (gdb_stdlog
,
2029 "LLR: Not short circuiting for ignored "
2030 "status 0x%x\n", lp
->status
);
2032 /* FIXME: What should we do if we are supposed to continue
2033 this thread with a signal? */
2034 gdb_assert (signo
== TARGET_SIGNAL_0
);
2035 signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
2040 if (lp
->status
|| lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2042 /* FIXME: What should we do if we are supposed to continue
2043 this thread with a signal? */
2044 gdb_assert (signo
== TARGET_SIGNAL_0
);
2046 if (debug_linux_nat
)
2047 fprintf_unfiltered (gdb_stdlog
,
2048 "LLR: Short circuiting for status 0x%x\n",
2051 restore_child_signals_mask (&prev_mask
);
2052 if (target_can_async_p ())
2054 target_async (inferior_event_handler
, 0);
2055 /* Tell the event loop we have something to process. */
2061 /* Mark LWP as not stopped to prevent it from being continued by
2066 iterate_over_lwps (ptid
, resume_callback
, NULL
);
2068 /* Convert to something the lower layer understands. */
2069 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
2071 if (linux_nat_prepare_to_resume
!= NULL
)
2072 linux_nat_prepare_to_resume (lp
);
2073 linux_ops
->to_resume (linux_ops
, ptid
, step
, signo
);
2074 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
2075 lp
->stopped_by_watchpoint
= 0;
2077 if (debug_linux_nat
)
2078 fprintf_unfiltered (gdb_stdlog
,
2079 "LLR: %s %s, %s (resume event thread)\n",
2080 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2081 target_pid_to_str (ptid
),
2082 (signo
!= TARGET_SIGNAL_0
2083 ? strsignal (target_signal_to_host (signo
)) : "0"));
2085 restore_child_signals_mask (&prev_mask
);
2086 if (target_can_async_p ())
2087 target_async (inferior_event_handler
, 0);
2090 /* Send a signal to an LWP. */
2093 kill_lwp (int lwpid
, int signo
)
2095 /* Use tkill, if possible, in case we are using nptl threads. If tkill
2096 fails, then we are not using nptl threads and we should be using kill. */
2098 #ifdef HAVE_TKILL_SYSCALL
2100 static int tkill_failed
;
2107 ret
= syscall (__NR_tkill
, lwpid
, signo
);
2108 if (errno
!= ENOSYS
)
2115 return kill (lwpid
, signo
);
2118 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
2119 event, check if the core is interested in it: if not, ignore the
2120 event, and keep waiting; otherwise, we need to toggle the LWP's
2121 syscall entry/exit status, since the ptrace event itself doesn't
2122 indicate it, and report the trap to higher layers. */
2125 linux_handle_syscall_trap (struct lwp_info
*lp
, int stopping
)
2127 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
2128 struct gdbarch
*gdbarch
= target_thread_architecture (lp
->ptid
);
2129 int syscall_number
= (int) gdbarch_get_syscall_number (gdbarch
, lp
->ptid
);
2133 /* If we're stopping threads, there's a SIGSTOP pending, which
2134 makes it so that the LWP reports an immediate syscall return,
2135 followed by the SIGSTOP. Skip seeing that "return" using
2136 PTRACE_CONT directly, and let stop_wait_callback collect the
2137 SIGSTOP. Later when the thread is resumed, a new syscall
2138 entry event. If we didn't do this (and returned 0), we'd
2139 leave a syscall entry pending, and our caller, by using
2140 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
2141 itself. Later, when the user re-resumes this LWP, we'd see
2142 another syscall entry event and we'd mistake it for a return.
2144 If stop_wait_callback didn't force the SIGSTOP out of the LWP
2145 (leaving immediately with LWP->signalled set, without issuing
2146 a PTRACE_CONT), it would still be problematic to leave this
2147 syscall enter pending, as later when the thread is resumed,
2148 it would then see the same syscall exit mentioned above,
2149 followed by the delayed SIGSTOP, while the syscall didn't
2150 actually get to execute. It seems it would be even more
2151 confusing to the user. */
2153 if (debug_linux_nat
)
2154 fprintf_unfiltered (gdb_stdlog
,
2155 "LHST: ignoring syscall %d "
2156 "for LWP %ld (stopping threads), "
2157 "resuming with PTRACE_CONT for SIGSTOP\n",
2159 GET_LWP (lp
->ptid
));
2161 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2162 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2166 if (catch_syscall_enabled ())
2168 /* Always update the entry/return state, even if this particular
2169 syscall isn't interesting to the core now. In async mode,
2170 the user could install a new catchpoint for this syscall
2171 between syscall enter/return, and we'll need to know to
2172 report a syscall return if that happens. */
2173 lp
->syscall_state
= (lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2174 ? TARGET_WAITKIND_SYSCALL_RETURN
2175 : TARGET_WAITKIND_SYSCALL_ENTRY
);
2177 if (catching_syscall_number (syscall_number
))
2179 /* Alright, an event to report. */
2180 ourstatus
->kind
= lp
->syscall_state
;
2181 ourstatus
->value
.syscall_number
= syscall_number
;
2183 if (debug_linux_nat
)
2184 fprintf_unfiltered (gdb_stdlog
,
2185 "LHST: stopping for %s of syscall %d"
2188 == TARGET_WAITKIND_SYSCALL_ENTRY
2189 ? "entry" : "return",
2191 GET_LWP (lp
->ptid
));
2195 if (debug_linux_nat
)
2196 fprintf_unfiltered (gdb_stdlog
,
2197 "LHST: ignoring %s of syscall %d "
2199 lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2200 ? "entry" : "return",
2202 GET_LWP (lp
->ptid
));
2206 /* If we had been syscall tracing, and hence used PT_SYSCALL
2207 before on this LWP, it could happen that the user removes all
2208 syscall catchpoints before we get to process this event.
2209 There are two noteworthy issues here:
2211 - When stopped at a syscall entry event, resuming with
2212 PT_STEP still resumes executing the syscall and reports a
2215 - Only PT_SYSCALL catches syscall enters. If we last
2216 single-stepped this thread, then this event can't be a
2217 syscall enter. If we last single-stepped this thread, this
2218 has to be a syscall exit.
2220 The points above mean that the next resume, be it PT_STEP or
2221 PT_CONTINUE, can not trigger a syscall trace event. */
2222 if (debug_linux_nat
)
2223 fprintf_unfiltered (gdb_stdlog
,
2224 "LHST: caught syscall event "
2225 "with no syscall catchpoints."
2226 " %d for LWP %ld, ignoring\n",
2228 GET_LWP (lp
->ptid
));
2229 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2232 /* The core isn't interested in this event. For efficiency, avoid
2233 stopping all threads only to have the core resume them all again.
2234 Since we're not stopping threads, if we're still syscall tracing
2235 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
2236 subsequent syscall. Simply resume using the inf-ptrace layer,
2237 which knows when to use PT_SYSCALL or PT_CONTINUE. */
2239 /* Note that gdbarch_get_syscall_number may access registers, hence
2241 registers_changed ();
2242 if (linux_nat_prepare_to_resume
!= NULL
)
2243 linux_nat_prepare_to_resume (lp
);
2244 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2245 lp
->step
, TARGET_SIGNAL_0
);
2249 /* Handle a GNU/Linux extended wait response. If we see a clone
2250 event, we need to add the new LWP to our list (and not report the
2251 trap to higher layers). This function returns non-zero if the
2252 event should be ignored and we should wait again. If STOPPING is
2253 true, the new LWP remains stopped, otherwise it is continued. */
2256 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
2259 int pid
= GET_LWP (lp
->ptid
);
2260 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
2261 int event
= status
>> 16;
2263 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
2264 || event
== PTRACE_EVENT_CLONE
)
2266 unsigned long new_pid
;
2269 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
2271 /* If we haven't already seen the new PID stop, wait for it now. */
2272 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
2274 /* The new child has a pending SIGSTOP. We can't affect it until it
2275 hits the SIGSTOP, but we're already attached. */
2276 ret
= my_waitpid (new_pid
, &status
,
2277 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
2279 perror_with_name (_("waiting for new child"));
2280 else if (ret
!= new_pid
)
2281 internal_error (__FILE__
, __LINE__
,
2282 _("wait returned unexpected PID %d"), ret
);
2283 else if (!WIFSTOPPED (status
))
2284 internal_error (__FILE__
, __LINE__
,
2285 _("wait returned unexpected status 0x%x"), status
);
2288 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
2290 if (event
== PTRACE_EVENT_FORK
2291 && linux_fork_checkpointing_p (GET_PID (lp
->ptid
)))
2293 /* Handle checkpointing by linux-fork.c here as a special
2294 case. We don't want the follow-fork-mode or 'catch fork'
2295 to interfere with this. */
2297 /* This won't actually modify the breakpoint list, but will
2298 physically remove the breakpoints from the child. */
2299 detach_breakpoints (new_pid
);
2301 /* Retain child fork in ptrace (stopped) state. */
2302 if (!find_fork_pid (new_pid
))
2305 /* Report as spurious, so that infrun doesn't want to follow
2306 this fork. We're actually doing an infcall in
2308 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
2309 linux_enable_event_reporting (pid_to_ptid (new_pid
));
2311 /* Report the stop to the core. */
2315 if (event
== PTRACE_EVENT_FORK
)
2316 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
2317 else if (event
== PTRACE_EVENT_VFORK
)
2318 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
2321 struct lwp_info
*new_lp
;
2323 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2325 if (debug_linux_nat
)
2326 fprintf_unfiltered (gdb_stdlog
,
2327 "LHEW: Got clone event "
2328 "from LWP %d, new child is LWP %ld\n",
2331 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (lp
->ptid
)));
2333 new_lp
->stopped
= 1;
2335 if (WSTOPSIG (status
) != SIGSTOP
)
2337 /* This can happen if someone starts sending signals to
2338 the new thread before it gets a chance to run, which
2339 have a lower number than SIGSTOP (e.g. SIGUSR1).
2340 This is an unlikely case, and harder to handle for
2341 fork / vfork than for clone, so we do not try - but
2342 we handle it for clone events here. We'll send
2343 the other signal on to the thread below. */
2345 new_lp
->signalled
= 1;
2349 struct thread_info
*tp
;
2351 /* When we stop for an event in some other thread, and
2352 pull the thread list just as this thread has cloned,
2353 we'll have seen the new thread in the thread_db list
2354 before handling the CLONE event (glibc's
2355 pthread_create adds the new thread to the thread list
2356 before clone'ing, and has the kernel fill in the
2357 thread's tid on the clone call with
2358 CLONE_PARENT_SETTID). If that happened, and the core
2359 had requested the new thread to stop, we'll have
2360 killed it with SIGSTOP. But since SIGSTOP is not an
2361 RT signal, it can only be queued once. We need to be
2362 careful to not resume the LWP if we wanted it to
2363 stop. In that case, we'll leave the SIGSTOP pending.
2364 It will later be reported as TARGET_SIGNAL_0. */
2365 tp
= find_thread_ptid (new_lp
->ptid
);
2366 if (tp
!= NULL
&& tp
->stop_requested
)
2367 new_lp
->last_resume_kind
= resume_stop
;
2374 /* Add the new thread to GDB's lists as soon as possible
2377 1) the frontend doesn't have to wait for a stop to
2380 2) we tag it with the correct running state. */
2382 /* If the thread_db layer is active, let it know about
2383 this new thread, and add it to GDB's list. */
2384 if (!thread_db_attach_lwp (new_lp
->ptid
))
2386 /* We're not using thread_db. Add it to GDB's
2388 target_post_attach (GET_LWP (new_lp
->ptid
));
2389 add_thread (new_lp
->ptid
);
2394 set_running (new_lp
->ptid
, 1);
2395 set_executing (new_lp
->ptid
, 1);
2396 /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
2398 new_lp
->last_resume_kind
= resume_continue
;
2404 /* We created NEW_LP so it cannot yet contain STATUS. */
2405 gdb_assert (new_lp
->status
== 0);
2407 /* Save the wait status to report later. */
2408 if (debug_linux_nat
)
2409 fprintf_unfiltered (gdb_stdlog
,
2410 "LHEW: waitpid of new LWP %ld, "
2411 "saving status %s\n",
2412 (long) GET_LWP (new_lp
->ptid
),
2413 status_to_str (status
));
2414 new_lp
->status
= status
;
2417 /* Note the need to use the low target ops to resume, to
2418 handle resuming with PT_SYSCALL if we have syscall
2422 new_lp
->resumed
= 1;
2426 gdb_assert (new_lp
->last_resume_kind
== resume_continue
);
2427 if (debug_linux_nat
)
2428 fprintf_unfiltered (gdb_stdlog
,
2429 "LHEW: resuming new LWP %ld\n",
2430 GET_LWP (new_lp
->ptid
));
2431 if (linux_nat_prepare_to_resume
!= NULL
)
2432 linux_nat_prepare_to_resume (new_lp
);
2433 linux_ops
->to_resume (linux_ops
, pid_to_ptid (new_pid
),
2434 0, TARGET_SIGNAL_0
);
2435 new_lp
->stopped
= 0;
2439 if (debug_linux_nat
)
2440 fprintf_unfiltered (gdb_stdlog
,
2441 "LHEW: resuming parent LWP %d\n", pid
);
2442 if (linux_nat_prepare_to_resume
!= NULL
)
2443 linux_nat_prepare_to_resume (lp
);
2444 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2445 0, TARGET_SIGNAL_0
);
2453 if (event
== PTRACE_EVENT_EXEC
)
2455 if (debug_linux_nat
)
2456 fprintf_unfiltered (gdb_stdlog
,
2457 "LHEW: Got exec event from LWP %ld\n",
2458 GET_LWP (lp
->ptid
));
2460 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
2461 ourstatus
->value
.execd_pathname
2462 = xstrdup (linux_child_pid_to_exec_file (pid
));
2467 if (event
== PTRACE_EVENT_VFORK_DONE
)
2469 if (current_inferior ()->waiting_for_vfork_done
)
2471 if (debug_linux_nat
)
2472 fprintf_unfiltered (gdb_stdlog
,
2473 "LHEW: Got expected PTRACE_EVENT_"
2474 "VFORK_DONE from LWP %ld: stopping\n",
2475 GET_LWP (lp
->ptid
));
2477 ourstatus
->kind
= TARGET_WAITKIND_VFORK_DONE
;
2481 if (debug_linux_nat
)
2482 fprintf_unfiltered (gdb_stdlog
,
2483 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2484 "from LWP %ld: resuming\n",
2485 GET_LWP (lp
->ptid
));
2486 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2490 internal_error (__FILE__
, __LINE__
,
2491 _("unknown ptrace event %d"), event
);
2494 /* Return non-zero if LWP is a zombie. */
2497 linux_lwp_is_zombie (long lwp
)
2499 char buffer
[MAXPATHLEN
];
2504 xsnprintf (buffer
, sizeof (buffer
), "/proc/%ld/status", lwp
);
2505 procfile
= fopen (buffer
, "r");
2506 if (procfile
== NULL
)
2508 warning (_("unable to open /proc file '%s'"), buffer
);
2513 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
2514 if (strncmp (buffer
, "State:", 6) == 0)
2519 retval
= (have_state
2520 && strcmp (buffer
, "State:\tZ (zombie)\n") == 0);
2525 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2529 wait_lwp (struct lwp_info
*lp
)
2533 int thread_dead
= 0;
2536 gdb_assert (!lp
->stopped
);
2537 gdb_assert (lp
->status
== 0);
2539 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2540 block_child_signals (&prev_mask
);
2544 /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2545 was right and we should just call sigsuspend. */
2547 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, WNOHANG
);
2548 if (pid
== -1 && errno
== ECHILD
)
2549 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
| WNOHANG
);
2550 if (pid
== -1 && errno
== ECHILD
)
2552 /* The thread has previously exited. We need to delete it
2553 now because, for some vendor 2.4 kernels with NPTL
2554 support backported, there won't be an exit event unless
2555 it is the main thread. 2.6 kernels will report an exit
2556 event for each thread that exits, as expected. */
2558 if (debug_linux_nat
)
2559 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2560 target_pid_to_str (lp
->ptid
));
2565 /* Bugs 10970, 12702.
2566 Thread group leader may have exited in which case we'll lock up in
2567 waitpid if there are other threads, even if they are all zombies too.
2568 Basically, we're not supposed to use waitpid this way.
2569 __WCLONE is not applicable for the leader so we can't use that.
2570 LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2571 process; it gets ESRCH both for the zombie and for running processes.
2573 As a workaround, check if we're waiting for the thread group leader and
2574 if it's a zombie, and avoid calling waitpid if it is.
2576 This is racy, what if the tgl becomes a zombie right after we check?
2577 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2578 waiting waitpid but the linux_lwp_is_zombie is safe this way. */
2580 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
)
2581 && linux_lwp_is_zombie (GET_LWP (lp
->ptid
)))
2584 if (debug_linux_nat
)
2585 fprintf_unfiltered (gdb_stdlog
,
2586 "WL: Thread group leader %s vanished.\n",
2587 target_pid_to_str (lp
->ptid
));
2591 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2592 get invoked despite our caller had them intentionally blocked by
2593 block_child_signals. This is sensitive only to the loop of
2594 linux_nat_wait_1 and there if we get called my_waitpid gets called
2595 again before it gets to sigsuspend so we can safely let the handlers
2596 get executed here. */
2598 sigsuspend (&suspend_mask
);
2601 restore_child_signals_mask (&prev_mask
);
2605 gdb_assert (pid
== GET_LWP (lp
->ptid
));
2607 if (debug_linux_nat
)
2609 fprintf_unfiltered (gdb_stdlog
,
2610 "WL: waitpid %s received %s\n",
2611 target_pid_to_str (lp
->ptid
),
2612 status_to_str (status
));
2615 /* Check if the thread has exited. */
2616 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2619 if (debug_linux_nat
)
2620 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2621 target_pid_to_str (lp
->ptid
));
2631 gdb_assert (WIFSTOPPED (status
));
2633 /* Handle GNU/Linux's syscall SIGTRAPs. */
2634 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2636 /* No longer need the sysgood bit. The ptrace event ends up
2637 recorded in lp->waitstatus if we care for it. We can carry
2638 on handling the event like a regular SIGTRAP from here
2640 status
= W_STOPCODE (SIGTRAP
);
2641 if (linux_handle_syscall_trap (lp
, 1))
2642 return wait_lwp (lp
);
2645 /* Handle GNU/Linux's extended waitstatus for trace events. */
2646 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2648 if (debug_linux_nat
)
2649 fprintf_unfiltered (gdb_stdlog
,
2650 "WL: Handling extended status 0x%06x\n",
2652 if (linux_handle_extended_wait (lp
, status
, 1))
2653 return wait_lwp (lp
);
2659 /* Save the most recent siginfo for LP. This is currently only called
2660 for SIGTRAP; some ports use the si_addr field for
2661 target_stopped_data_address. In the future, it may also be used to
2662 restore the siginfo of requeued signals. */
2665 save_siginfo (struct lwp_info
*lp
)
2668 ptrace (PTRACE_GETSIGINFO
, GET_LWP (lp
->ptid
),
2669 (PTRACE_TYPE_ARG3
) 0, &lp
->siginfo
);
2672 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
2675 /* Send a SIGSTOP to LP. */
2678 stop_callback (struct lwp_info
*lp
, void *data
)
2680 if (!lp
->stopped
&& !lp
->signalled
)
2684 if (debug_linux_nat
)
2686 fprintf_unfiltered (gdb_stdlog
,
2687 "SC: kill %s **<SIGSTOP>**\n",
2688 target_pid_to_str (lp
->ptid
));
2691 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
2692 if (debug_linux_nat
)
2694 fprintf_unfiltered (gdb_stdlog
,
2695 "SC: lwp kill %d %s\n",
2697 errno
? safe_strerror (errno
) : "ERRNO-OK");
2701 gdb_assert (lp
->status
== 0);
2707 /* Request a stop on LWP. */
2710 linux_stop_lwp (struct lwp_info
*lwp
)
2712 stop_callback (lwp
, NULL
);
2715 /* Return non-zero if LWP PID has a pending SIGINT. */
2718 linux_nat_has_pending_sigint (int pid
)
2720 sigset_t pending
, blocked
, ignored
;
2722 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2724 if (sigismember (&pending
, SIGINT
)
2725 && !sigismember (&ignored
, SIGINT
))
2731 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2734 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2736 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2737 flag to consume the next one. */
2738 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2739 && WSTOPSIG (lp
->status
) == SIGINT
)
2742 lp
->ignore_sigint
= 1;
2747 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2748 This function is called after we know the LWP has stopped; if the LWP
2749 stopped before the expected SIGINT was delivered, then it will never have
2750 arrived. Also, if the signal was delivered to a shared queue and consumed
2751 by a different thread, it will never be delivered to this LWP. */
2754 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2756 if (!lp
->ignore_sigint
)
2759 if (!linux_nat_has_pending_sigint (GET_LWP (lp
->ptid
)))
2761 if (debug_linux_nat
)
2762 fprintf_unfiltered (gdb_stdlog
,
2763 "MCIS: Clearing bogus flag for %s\n",
2764 target_pid_to_str (lp
->ptid
));
2765 lp
->ignore_sigint
= 0;
2769 /* Fetch the possible triggered data watchpoint info and store it in
2772 On some archs, like x86, that use debug registers to set
2773 watchpoints, it's possible that the way to know which watched
2774 address trapped, is to check the register that is used to select
2775 which address to watch. Problem is, between setting the watchpoint
2776 and reading back which data address trapped, the user may change
2777 the set of watchpoints, and, as a consequence, GDB changes the
2778 debug registers in the inferior. To avoid reading back a stale
2779 stopped-data-address when that happens, we cache in LP the fact
2780 that a watchpoint trapped, and the corresponding data address, as
2781 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2782 registers meanwhile, we have the cached data we can rely on. */
2785 save_sigtrap (struct lwp_info
*lp
)
2787 struct cleanup
*old_chain
;
2789 if (linux_ops
->to_stopped_by_watchpoint
== NULL
)
2791 lp
->stopped_by_watchpoint
= 0;
2795 old_chain
= save_inferior_ptid ();
2796 inferior_ptid
= lp
->ptid
;
2798 lp
->stopped_by_watchpoint
= linux_ops
->to_stopped_by_watchpoint ();
2800 if (lp
->stopped_by_watchpoint
)
2802 if (linux_ops
->to_stopped_data_address
!= NULL
)
2803 lp
->stopped_data_address_p
=
2804 linux_ops
->to_stopped_data_address (¤t_target
,
2805 &lp
->stopped_data_address
);
2807 lp
->stopped_data_address_p
= 0;
2810 do_cleanups (old_chain
);
2813 /* See save_sigtrap. */
2816 linux_nat_stopped_by_watchpoint (void)
2818 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2820 gdb_assert (lp
!= NULL
);
2822 return lp
->stopped_by_watchpoint
;
2826 linux_nat_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
2828 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2830 gdb_assert (lp
!= NULL
);
2832 *addr_p
= lp
->stopped_data_address
;
2834 return lp
->stopped_data_address_p
;
2837 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2840 sigtrap_is_event (int status
)
2842 return WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
;
2845 /* SIGTRAP-like events recognizer. */
2847 static int (*linux_nat_status_is_event
) (int status
) = sigtrap_is_event
;
2849 /* Check for SIGTRAP-like events in LP. */
2852 linux_nat_lp_status_is_event (struct lwp_info
*lp
)
2854 /* We check for lp->waitstatus in addition to lp->status, because we can
2855 have pending process exits recorded in lp->status
2856 and W_EXITCODE(0,0) == 0. We should probably have an additional
2857 lp->status_p flag. */
2859 return (lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
2860 && linux_nat_status_is_event (lp
->status
));
2863 /* Set alternative SIGTRAP-like events recognizer. If
2864 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2868 linux_nat_set_status_is_event (struct target_ops
*t
,
2869 int (*status_is_event
) (int status
))
2871 linux_nat_status_is_event
= status_is_event
;
2874 /* Wait until LP is stopped. */
2877 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2879 struct inferior
*inf
= find_inferior_pid (GET_PID (lp
->ptid
));
2881 /* If this is a vfork parent, bail out, it is not going to report
2882 any SIGSTOP until the vfork is done with. */
2883 if (inf
->vfork_child
!= NULL
)
2890 status
= wait_lwp (lp
);
2894 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2895 && WSTOPSIG (status
) == SIGINT
)
2897 lp
->ignore_sigint
= 0;
2900 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2901 if (debug_linux_nat
)
2902 fprintf_unfiltered (gdb_stdlog
,
2903 "PTRACE_CONT %s, 0, 0 (%s) "
2904 "(discarding SIGINT)\n",
2905 target_pid_to_str (lp
->ptid
),
2906 errno
? safe_strerror (errno
) : "OK");
2908 return stop_wait_callback (lp
, NULL
);
2911 maybe_clear_ignore_sigint (lp
);
2913 if (WSTOPSIG (status
) != SIGSTOP
)
2915 if (linux_nat_status_is_event (status
))
2917 /* If a LWP other than the LWP that we're reporting an
2918 event for has hit a GDB breakpoint (as opposed to
2919 some random trap signal), then just arrange for it to
2920 hit it again later. We don't keep the SIGTRAP status
2921 and don't forward the SIGTRAP signal to the LWP. We
2922 will handle the current event, eventually we will
2923 resume all LWPs, and this one will get its breakpoint
2926 If we do not do this, then we run the risk that the
2927 user will delete or disable the breakpoint, but the
2928 thread will have already tripped on it. */
2930 /* Save the trap's siginfo in case we need it later. */
2935 /* Now resume this LWP and get the SIGSTOP event. */
2937 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2938 if (debug_linux_nat
)
2940 fprintf_unfiltered (gdb_stdlog
,
2941 "PTRACE_CONT %s, 0, 0 (%s)\n",
2942 target_pid_to_str (lp
->ptid
),
2943 errno
? safe_strerror (errno
) : "OK");
2945 fprintf_unfiltered (gdb_stdlog
,
2946 "SWC: Candidate SIGTRAP event in %s\n",
2947 target_pid_to_str (lp
->ptid
));
2949 /* Hold this event/waitstatus while we check to see if
2950 there are any more (we still want to get that SIGSTOP). */
2951 stop_wait_callback (lp
, NULL
);
2953 /* Hold the SIGTRAP for handling by linux_nat_wait. If
2954 there's another event, throw it back into the
2958 if (debug_linux_nat
)
2959 fprintf_unfiltered (gdb_stdlog
,
2960 "SWC: kill %s, %s\n",
2961 target_pid_to_str (lp
->ptid
),
2962 status_to_str ((int) status
));
2963 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
2966 /* Save the sigtrap event. */
2967 lp
->status
= status
;
2972 /* The thread was stopped with a signal other than
2973 SIGSTOP, and didn't accidentally trip a breakpoint. */
2975 if (debug_linux_nat
)
2977 fprintf_unfiltered (gdb_stdlog
,
2978 "SWC: Pending event %s in %s\n",
2979 status_to_str ((int) status
),
2980 target_pid_to_str (lp
->ptid
));
2982 /* Now resume this LWP and get the SIGSTOP event. */
2984 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2985 if (debug_linux_nat
)
2986 fprintf_unfiltered (gdb_stdlog
,
2987 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2988 target_pid_to_str (lp
->ptid
),
2989 errno
? safe_strerror (errno
) : "OK");
2991 /* Hold this event/waitstatus while we check to see if
2992 there are any more (we still want to get that SIGSTOP). */
2993 stop_wait_callback (lp
, NULL
);
2995 /* If the lp->status field is still empty, use it to
2996 hold this event. If not, then this event must be
2997 returned to the event queue of the LWP. */
3000 if (debug_linux_nat
)
3002 fprintf_unfiltered (gdb_stdlog
,
3003 "SWC: kill %s, %s\n",
3004 target_pid_to_str (lp
->ptid
),
3005 status_to_str ((int) status
));
3007 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
3010 lp
->status
= status
;
3016 /* We caught the SIGSTOP that we intended to catch, so
3017 there's no SIGSTOP pending. */
3026 /* Return non-zero if LP has a wait status pending. */
3029 status_callback (struct lwp_info
*lp
, void *data
)
3031 /* Only report a pending wait status if we pretend that this has
3032 indeed been resumed. */
3036 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3038 /* A ptrace event, like PTRACE_FORK|VFORK|EXEC, syscall event,
3039 or a pending process exit. Note that `W_EXITCODE(0,0) ==
3040 0', so a clean process exit can not be stored pending in
3041 lp->status, it is indistinguishable from
3042 no-pending-status. */
3046 if (lp
->status
!= 0)
3052 /* Return non-zero if LP isn't stopped. */
3055 running_callback (struct lwp_info
*lp
, void *data
)
3057 return (!lp
->stopped
3058 || ((lp
->status
!= 0
3059 || lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3063 /* Count the LWP's that have had events. */
3066 count_events_callback (struct lwp_info
*lp
, void *data
)
3070 gdb_assert (count
!= NULL
);
3072 /* Count only resumed LWPs that have a SIGTRAP event pending. */
3073 if (lp
->resumed
&& linux_nat_lp_status_is_event (lp
))
3079 /* Select the LWP (if any) that is currently being single-stepped. */
3082 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
3084 if (lp
->last_resume_kind
== resume_step
3091 /* Select the Nth LWP that has had a SIGTRAP event. */
3094 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
3096 int *selector
= data
;
3098 gdb_assert (selector
!= NULL
);
3100 /* Select only resumed LWPs that have a SIGTRAP event pending. */
3101 if (lp
->resumed
&& linux_nat_lp_status_is_event (lp
))
3102 if ((*selector
)-- == 0)
3109 cancel_breakpoint (struct lwp_info
*lp
)
3111 /* Arrange for a breakpoint to be hit again later. We don't keep
3112 the SIGTRAP status and don't forward the SIGTRAP signal to the
3113 LWP. We will handle the current event, eventually we will resume
3114 this LWP, and this breakpoint will trap again.
3116 If we do not do this, then we run the risk that the user will
3117 delete or disable the breakpoint, but the LWP will have already
3120 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3121 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3124 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
3125 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
3127 if (debug_linux_nat
)
3128 fprintf_unfiltered (gdb_stdlog
,
3129 "CB: Push back breakpoint for %s\n",
3130 target_pid_to_str (lp
->ptid
));
3132 /* Back up the PC if necessary. */
3133 if (gdbarch_decr_pc_after_break (gdbarch
))
3134 regcache_write_pc (regcache
, pc
);
3142 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
3144 struct lwp_info
*event_lp
= data
;
3146 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
3150 /* If a LWP other than the LWP that we're reporting an event for has
3151 hit a GDB breakpoint (as opposed to some random trap signal),
3152 then just arrange for it to hit it again later. We don't keep
3153 the SIGTRAP status and don't forward the SIGTRAP signal to the
3154 LWP. We will handle the current event, eventually we will resume
3155 all LWPs, and this one will get its breakpoint trap again.
3157 If we do not do this, then we run the risk that the user will
3158 delete or disable the breakpoint, but the LWP will have already
3161 if (linux_nat_lp_status_is_event (lp
)
3162 && cancel_breakpoint (lp
))
3163 /* Throw away the SIGTRAP. */
3169 /* Select one LWP out of those that have events pending. */
3172 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
3175 int random_selector
;
3176 struct lwp_info
*event_lp
;
3178 /* Record the wait status for the original LWP. */
3179 (*orig_lp
)->status
= *status
;
3181 /* Give preference to any LWP that is being single-stepped. */
3182 event_lp
= iterate_over_lwps (filter
,
3183 select_singlestep_lwp_callback
, NULL
);
3184 if (event_lp
!= NULL
)
3186 if (debug_linux_nat
)
3187 fprintf_unfiltered (gdb_stdlog
,
3188 "SEL: Select single-step %s\n",
3189 target_pid_to_str (event_lp
->ptid
));
3193 /* No single-stepping LWP. Select one at random, out of those
3194 which have had SIGTRAP events. */
3196 /* First see how many SIGTRAP events we have. */
3197 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
3199 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
3200 random_selector
= (int)
3201 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
3203 if (debug_linux_nat
&& num_events
> 1)
3204 fprintf_unfiltered (gdb_stdlog
,
3205 "SEL: Found %d SIGTRAP events, selecting #%d\n",
3206 num_events
, random_selector
);
3208 event_lp
= iterate_over_lwps (filter
,
3209 select_event_lwp_callback
,
3213 if (event_lp
!= NULL
)
3215 /* Switch the event LWP. */
3216 *orig_lp
= event_lp
;
3217 *status
= event_lp
->status
;
3220 /* Flush the wait status for the event LWP. */
3221 (*orig_lp
)->status
= 0;
3224 /* Return non-zero if LP has been resumed. */
3227 resumed_callback (struct lwp_info
*lp
, void *data
)
3232 /* Stop an active thread, verify it still exists, then resume it. If
3233 the thread ends up with a pending status, then it is not resumed,
3234 and *DATA (really a pointer to int), is set. */
3237 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
3239 int *new_pending_p
= data
;
3243 ptid_t ptid
= lp
->ptid
;
3245 stop_callback (lp
, NULL
);
3246 stop_wait_callback (lp
, NULL
);
3248 /* Resume if the lwp still exists, and the core wanted it
3250 lp
= find_lwp_pid (ptid
);
3253 if (lp
->last_resume_kind
== resume_stop
3256 /* The core wanted the LWP to stop. Even if it stopped
3257 cleanly (with SIGSTOP), leave the event pending. */
3258 if (debug_linux_nat
)
3259 fprintf_unfiltered (gdb_stdlog
,
3260 "SARC: core wanted LWP %ld stopped "
3261 "(leaving SIGSTOP pending)\n",
3262 GET_LWP (lp
->ptid
));
3263 lp
->status
= W_STOPCODE (SIGSTOP
);
3266 if (lp
->status
== 0)
3268 if (debug_linux_nat
)
3269 fprintf_unfiltered (gdb_stdlog
,
3270 "SARC: re-resuming LWP %ld\n",
3271 GET_LWP (lp
->ptid
));
3272 resume_lwp (lp
, lp
->step
);
3276 if (debug_linux_nat
)
3277 fprintf_unfiltered (gdb_stdlog
,
3278 "SARC: not re-resuming LWP %ld "
3280 GET_LWP (lp
->ptid
));
3289 /* Check if we should go on and pass this event to common code.
3290 Return the affected lwp if we are, or NULL otherwise. If we stop
3291 all lwps temporarily, we may end up with new pending events in some
3292 other lwp. In that case set *NEW_PENDING_P to true. */
3294 static struct lwp_info
*
3295 linux_nat_filter_event (int lwpid
, int status
, int *new_pending_p
)
3297 struct lwp_info
*lp
;
3301 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
3303 /* Check for stop events reported by a process we didn't already
3304 know about - anything not already in our LWP list.
3306 If we're expecting to receive stopped processes after
3307 fork, vfork, and clone events, then we'll just add the
3308 new one to our list and go back to waiting for the event
3309 to be reported - the stopped process might be returned
3310 from waitpid before or after the event is.
3312 But note the case of a non-leader thread exec'ing after the
3313 leader having exited, and gone from our lists. The non-leader
3314 thread changes its tid to the tgid. */
3316 if (WIFSTOPPED (status
) && lp
== NULL
3317 && (WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 == PTRACE_EVENT_EXEC
))
3319 /* A multi-thread exec after we had seen the leader exiting. */
3320 if (debug_linux_nat
)
3321 fprintf_unfiltered (gdb_stdlog
,
3322 "LLW: Re-adding thread group leader LWP %d.\n",
3325 lp
= add_lwp (BUILD_LWP (lwpid
, lwpid
));
3328 add_thread (lp
->ptid
);
3331 if (WIFSTOPPED (status
) && !lp
)
3333 add_to_pid_list (&stopped_pids
, lwpid
, status
);
3337 /* Make sure we don't report an event for the exit of an LWP not in
3338 our list, i.e. not part of the current process. This can happen
3339 if we detach from a program we originally forked and then it
3341 if (!WIFSTOPPED (status
) && !lp
)
3344 /* Handle GNU/Linux's syscall SIGTRAPs. */
3345 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
3347 /* No longer need the sysgood bit. The ptrace event ends up
3348 recorded in lp->waitstatus if we care for it. We can carry
3349 on handling the event like a regular SIGTRAP from here
3351 status
= W_STOPCODE (SIGTRAP
);
3352 if (linux_handle_syscall_trap (lp
, 0))
3356 /* Handle GNU/Linux's extended waitstatus for trace events. */
3357 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
3359 if (debug_linux_nat
)
3360 fprintf_unfiltered (gdb_stdlog
,
3361 "LLW: Handling extended status 0x%06x\n",
3363 if (linux_handle_extended_wait (lp
, status
, 0))
3367 if (linux_nat_status_is_event (status
))
3369 /* Save the trap's siginfo in case we need it later. */
3375 /* Check if the thread has exited. */
3376 if ((WIFEXITED (status
) || WIFSIGNALED (status
))
3377 && num_lwps (GET_PID (lp
->ptid
)) > 1)
3379 /* If this is the main thread, we must stop all threads and verify
3380 if they are still alive. This is because in the nptl thread model
3381 on Linux 2.4, there is no signal issued for exiting LWPs
3382 other than the main thread. We only get the main thread exit
3383 signal once all child threads have already exited. If we
3384 stop all the threads and use the stop_wait_callback to check
3385 if they have exited we can determine whether this signal
3386 should be ignored or whether it means the end of the debugged
3387 application, regardless of which threading model is being
3389 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
3392 iterate_over_lwps (pid_to_ptid (GET_PID (lp
->ptid
)),
3393 stop_and_resume_callback
, new_pending_p
);
3396 if (debug_linux_nat
)
3397 fprintf_unfiltered (gdb_stdlog
,
3398 "LLW: %s exited.\n",
3399 target_pid_to_str (lp
->ptid
));
3401 if (num_lwps (GET_PID (lp
->ptid
)) > 1)
3403 /* If there is at least one more LWP, then the exit signal
3404 was not the end of the debugged application and should be
3411 /* Check if the current LWP has previously exited. In the nptl
3412 thread model, LWPs other than the main thread do not issue
3413 signals when they exit so we must check whenever the thread has
3414 stopped. A similar check is made in stop_wait_callback(). */
3415 if (num_lwps (GET_PID (lp
->ptid
)) > 1 && !linux_thread_alive (lp
->ptid
))
3417 ptid_t ptid
= pid_to_ptid (GET_PID (lp
->ptid
));
3419 if (debug_linux_nat
)
3420 fprintf_unfiltered (gdb_stdlog
,
3421 "LLW: %s exited.\n",
3422 target_pid_to_str (lp
->ptid
));
3426 /* Make sure there is at least one thread running. */
3427 gdb_assert (iterate_over_lwps (ptid
, running_callback
, NULL
));
3429 /* Discard the event. */
3433 /* Make sure we don't report a SIGSTOP that we sent ourselves in
3434 an attempt to stop an LWP. */
3436 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
3438 if (debug_linux_nat
)
3439 fprintf_unfiltered (gdb_stdlog
,
3440 "LLW: Delayed SIGSTOP caught for %s.\n",
3441 target_pid_to_str (lp
->ptid
));
3445 if (lp
->last_resume_kind
!= resume_stop
)
3447 /* This is a delayed SIGSTOP. */
3449 registers_changed ();
3451 if (linux_nat_prepare_to_resume
!= NULL
)
3452 linux_nat_prepare_to_resume (lp
);
3453 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3454 lp
->step
, TARGET_SIGNAL_0
);
3455 if (debug_linux_nat
)
3456 fprintf_unfiltered (gdb_stdlog
,
3457 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
3459 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3460 target_pid_to_str (lp
->ptid
));
3463 gdb_assert (lp
->resumed
);
3465 /* Discard the event. */
3470 /* Make sure we don't report a SIGINT that we have already displayed
3471 for another thread. */
3472 if (lp
->ignore_sigint
3473 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
3475 if (debug_linux_nat
)
3476 fprintf_unfiltered (gdb_stdlog
,
3477 "LLW: Delayed SIGINT caught for %s.\n",
3478 target_pid_to_str (lp
->ptid
));
3480 /* This is a delayed SIGINT. */
3481 lp
->ignore_sigint
= 0;
3483 registers_changed ();
3484 if (linux_nat_prepare_to_resume
!= NULL
)
3485 linux_nat_prepare_to_resume (lp
);
3486 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3487 lp
->step
, TARGET_SIGNAL_0
);
3488 if (debug_linux_nat
)
3489 fprintf_unfiltered (gdb_stdlog
,
3490 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
3492 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3493 target_pid_to_str (lp
->ptid
));
3496 gdb_assert (lp
->resumed
);
3498 /* Discard the event. */
3502 /* An interesting event. */
3504 lp
->status
= status
;
3508 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3509 their exits until all other threads in the group have exited. */
3512 check_zombie_leaders (void)
3514 struct inferior
*inf
;
3518 struct lwp_info
*leader_lp
;
3523 leader_lp
= find_lwp_pid (pid_to_ptid (inf
->pid
));
3524 if (leader_lp
!= NULL
3525 /* Check if there are other threads in the group, as we may
3526 have raced with the inferior simply exiting. */
3527 && num_lwps (inf
->pid
) > 1
3528 && linux_lwp_is_zombie (inf
->pid
))
3530 if (debug_linux_nat
)
3531 fprintf_unfiltered (gdb_stdlog
,
3532 "CZL: Thread group leader %d zombie "
3533 "(it exited, or another thread execd).\n",
3536 /* A leader zombie can mean one of two things:
3538 - It exited, and there's an exit status pending
3539 available, or only the leader exited (not the whole
3540 program). In the latter case, we can't waitpid the
3541 leader's exit status until all other threads are gone.
3543 - There are 3 or more threads in the group, and a thread
3544 other than the leader exec'd. On an exec, the Linux
3545 kernel destroys all other threads (except the execing
3546 one) in the thread group, and resets the execing thread's
3547 tid to the tgid. No exit notification is sent for the
3548 execing thread -- from the ptracer's perspective, it
3549 appears as though the execing thread just vanishes.
3550 Until we reap all other threads except the leader and the
3551 execing thread, the leader will be zombie, and the
3552 execing thread will be in `D (disc sleep)'. As soon as
3553 all other threads are reaped, the execing thread changes
3554 it's tid to the tgid, and the previous (zombie) leader
3555 vanishes, giving place to the "new" leader. We could try
3556 distinguishing the exit and exec cases, by waiting once
3557 more, and seeing if something comes out, but it doesn't
3558 sound useful. The previous leader _does_ go away, and
3559 we'll re-add the new one once we see the exec event
3560 (which is just the same as what would happen if the
3561 previous leader did exit voluntarily before some other
3564 if (debug_linux_nat
)
3565 fprintf_unfiltered (gdb_stdlog
,
3566 "CZL: Thread group leader %d vanished.\n",
3568 exit_lwp (leader_lp
);
3574 linux_nat_wait_1 (struct target_ops
*ops
,
3575 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3578 static sigset_t prev_mask
;
3579 enum resume_kind last_resume_kind
;
3580 struct lwp_info
*lp
;
3583 if (debug_linux_nat
)
3584 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
3586 /* The first time we get here after starting a new inferior, we may
3587 not have added it to the LWP list yet - this is the earliest
3588 moment at which we know its PID. */
3589 if (ptid_is_pid (inferior_ptid
))
3591 /* Upgrade the main thread's ptid. */
3592 thread_change_ptid (inferior_ptid
,
3593 BUILD_LWP (GET_PID (inferior_ptid
),
3594 GET_PID (inferior_ptid
)));
3596 lp
= add_lwp (inferior_ptid
);
3600 /* Make sure SIGCHLD is blocked. */
3601 block_child_signals (&prev_mask
);
3607 /* First check if there is a LWP with a wait status pending. */
3608 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3610 /* Any LWP in the PTID group that's been resumed will do. */
3611 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3614 if (debug_linux_nat
&& lp
->status
)
3615 fprintf_unfiltered (gdb_stdlog
,
3616 "LLW: Using pending wait status %s for %s.\n",
3617 status_to_str (lp
->status
),
3618 target_pid_to_str (lp
->ptid
));
3621 else if (is_lwp (ptid
))
3623 if (debug_linux_nat
)
3624 fprintf_unfiltered (gdb_stdlog
,
3625 "LLW: Waiting for specific LWP %s.\n",
3626 target_pid_to_str (ptid
));
3628 /* We have a specific LWP to check. */
3629 lp
= find_lwp_pid (ptid
);
3632 if (debug_linux_nat
&& lp
->status
)
3633 fprintf_unfiltered (gdb_stdlog
,
3634 "LLW: Using pending wait status %s for %s.\n",
3635 status_to_str (lp
->status
),
3636 target_pid_to_str (lp
->ptid
));
3638 /* We check for lp->waitstatus in addition to lp->status,
3639 because we can have pending process exits recorded in
3640 lp->status and W_EXITCODE(0,0) == 0. We should probably have
3641 an additional lp->status_p flag. */
3642 if (lp
->status
== 0 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
3646 if (lp
&& lp
->signalled
&& lp
->last_resume_kind
!= resume_stop
)
3648 /* A pending SIGSTOP may interfere with the normal stream of
3649 events. In a typical case where interference is a problem,
3650 we have a SIGSTOP signal pending for LWP A while
3651 single-stepping it, encounter an event in LWP B, and take the
3652 pending SIGSTOP while trying to stop LWP A. After processing
3653 the event in LWP B, LWP A is continued, and we'll never see
3654 the SIGTRAP associated with the last time we were
3655 single-stepping LWP A. */
3657 /* Resume the thread. It should halt immediately returning the
3659 registers_changed ();
3660 if (linux_nat_prepare_to_resume
!= NULL
)
3661 linux_nat_prepare_to_resume (lp
);
3662 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3663 lp
->step
, TARGET_SIGNAL_0
);
3664 if (debug_linux_nat
)
3665 fprintf_unfiltered (gdb_stdlog
,
3666 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
3667 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3668 target_pid_to_str (lp
->ptid
));
3670 gdb_assert (lp
->resumed
);
3672 /* Catch the pending SIGSTOP. */
3673 status
= lp
->status
;
3676 stop_wait_callback (lp
, NULL
);
3678 /* If the lp->status field isn't empty, we caught another signal
3679 while flushing the SIGSTOP. Return it back to the event
3680 queue of the LWP, as we already have an event to handle. */
3683 if (debug_linux_nat
)
3684 fprintf_unfiltered (gdb_stdlog
,
3685 "LLW: kill %s, %s\n",
3686 target_pid_to_str (lp
->ptid
),
3687 status_to_str (lp
->status
));
3688 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
3691 lp
->status
= status
;
3694 if (!target_can_async_p ())
3696 /* Causes SIGINT to be passed on to the attached process. */
3700 /* But if we don't find a pending event, we'll have to wait. */
3706 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3709 - If the thread group leader exits while other threads in the
3710 thread group still exist, waitpid(TGID, ...) hangs. That
3711 waitpid won't return an exit status until the other threads
3712 in the group are reapped.
3714 - When a non-leader thread execs, that thread just vanishes
3715 without reporting an exit (so we'd hang if we waited for it
3716 explicitly in that case). The exec event is reported to
3720 lwpid
= my_waitpid (-1, &status
, __WCLONE
| WNOHANG
);
3721 if (lwpid
== 0 || (lwpid
== -1 && errno
== ECHILD
))
3722 lwpid
= my_waitpid (-1, &status
, WNOHANG
);
3724 if (debug_linux_nat
)
3725 fprintf_unfiltered (gdb_stdlog
,
3726 "LNW: waitpid(-1, ...) returned %d, %s\n",
3727 lwpid
, errno
? safe_strerror (errno
) : "ERRNO-OK");
3731 /* If this is true, then we paused LWPs momentarily, and may
3732 now have pending events to handle. */
3735 if (debug_linux_nat
)
3737 fprintf_unfiltered (gdb_stdlog
,
3738 "LLW: waitpid %ld received %s\n",
3739 (long) lwpid
, status_to_str (status
));
3742 lp
= linux_nat_filter_event (lwpid
, status
, &new_pending
);
3744 /* STATUS is now no longer valid, use LP->STATUS instead. */
3747 if (lp
&& !ptid_match (lp
->ptid
, ptid
))
3749 gdb_assert (lp
->resumed
);
3751 if (debug_linux_nat
)
3753 "LWP %ld got an event %06x, leaving pending.\n",
3754 ptid_get_lwp (lp
->ptid
), lp
->status
);
3756 if (WIFSTOPPED (lp
->status
))
3758 if (WSTOPSIG (lp
->status
) != SIGSTOP
)
3760 /* Cancel breakpoint hits. The breakpoint may
3761 be removed before we fetch events from this
3762 process to report to the core. It is best
3763 not to assume the moribund breakpoints
3764 heuristic always handles these cases --- it
3765 could be too many events go through to the
3766 core before this one is handled. All-stop
3767 always cancels breakpoint hits in all
3770 && linux_nat_lp_status_is_event (lp
)
3771 && cancel_breakpoint (lp
))
3773 /* Throw away the SIGTRAP. */
3776 if (debug_linux_nat
)
3778 "LLW: LWP %ld hit a breakpoint while"
3779 " waiting for another process;"
3781 ptid_get_lwp (lp
->ptid
));
3791 else if (WIFEXITED (lp
->status
) || WIFSIGNALED (lp
->status
))
3793 if (debug_linux_nat
)
3795 "Process %ld exited while stopping LWPs\n",
3796 ptid_get_lwp (lp
->ptid
));
3798 /* This was the last lwp in the process. Since
3799 events are serialized to GDB core, and we can't
3800 report this one right now, but GDB core and the
3801 other target layers will want to be notified
3802 about the exit code/signal, leave the status
3803 pending for the next time we're able to report
3806 /* Prevent trying to stop this thread again. We'll
3807 never try to resume it because it has a pending
3811 /* Dead LWP's aren't expected to reported a pending
3815 /* Store the pending event in the waitstatus as
3816 well, because W_EXITCODE(0,0) == 0. */
3817 store_waitstatus (&lp
->waitstatus
, lp
->status
);
3826 /* Some LWP now has a pending event. Go all the way
3827 back to check it. */
3833 /* We got an event to report to the core. */
3837 /* Retry until nothing comes out of waitpid. A single
3838 SIGCHLD can indicate more than one child stopped. */
3842 /* Check for zombie thread group leaders. Those can't be reaped
3843 until all other threads in the thread group are. */
3844 check_zombie_leaders ();
3846 /* If there are no resumed children left, bail. We'd be stuck
3847 forever in the sigsuspend call below otherwise. */
3848 if (iterate_over_lwps (ptid
, resumed_callback
, NULL
) == NULL
)
3850 if (debug_linux_nat
)
3851 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (no resumed LWP)\n");
3853 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3855 if (!target_can_async_p ())
3856 clear_sigint_trap ();
3858 restore_child_signals_mask (&prev_mask
);
3859 return minus_one_ptid
;
3862 /* No interesting event to report to the core. */
3864 if (target_options
& TARGET_WNOHANG
)
3866 if (debug_linux_nat
)
3867 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
3869 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3870 restore_child_signals_mask (&prev_mask
);
3871 return minus_one_ptid
;
3874 /* We shouldn't end up here unless we want to try again. */
3875 gdb_assert (lp
== NULL
);
3877 /* Block until we get an event reported with SIGCHLD. */
3878 sigsuspend (&suspend_mask
);
3881 if (!target_can_async_p ())
3882 clear_sigint_trap ();
3886 status
= lp
->status
;
3889 /* Don't report signals that GDB isn't interested in, such as
3890 signals that are neither printed nor stopped upon. Stopping all
3891 threads can be a bit time-consuming so if we want decent
3892 performance with heavily multi-threaded programs, especially when
3893 they're using a high frequency timer, we'd better avoid it if we
3896 if (WIFSTOPPED (status
))
3898 enum target_signal signo
= target_signal_from_host (WSTOPSIG (status
));
3900 /* When using hardware single-step, we need to report every signal.
3901 Otherwise, signals in pass_mask may be short-circuited. */
3903 && WSTOPSIG (status
) && sigismember (&pass_mask
, WSTOPSIG (status
)))
3905 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3906 here? It is not clear we should. GDB may not expect
3907 other threads to run. On the other hand, not resuming
3908 newly attached threads may cause an unwanted delay in
3909 getting them running. */
3910 registers_changed ();
3911 if (linux_nat_prepare_to_resume
!= NULL
)
3912 linux_nat_prepare_to_resume (lp
);
3913 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3915 if (debug_linux_nat
)
3916 fprintf_unfiltered (gdb_stdlog
,
3917 "LLW: %s %s, %s (preempt 'handle')\n",
3919 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3920 target_pid_to_str (lp
->ptid
),
3921 (signo
!= TARGET_SIGNAL_0
3922 ? strsignal (target_signal_to_host (signo
))
3930 /* Only do the below in all-stop, as we currently use SIGINT
3931 to implement target_stop (see linux_nat_stop) in
3933 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3935 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3936 forwarded to the entire process group, that is, all LWPs
3937 will receive it - unless they're using CLONE_THREAD to
3938 share signals. Since we only want to report it once, we
3939 mark it as ignored for all LWPs except this one. */
3940 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid
)),
3941 set_ignore_sigint
, NULL
);
3942 lp
->ignore_sigint
= 0;
3945 maybe_clear_ignore_sigint (lp
);
3949 /* This LWP is stopped now. */
3952 if (debug_linux_nat
)
3953 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
3954 status_to_str (status
), target_pid_to_str (lp
->ptid
));
3958 /* Now stop all other LWP's ... */
3959 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3961 /* ... and wait until all of them have reported back that
3962 they're no longer running. */
3963 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3965 /* If we're not waiting for a specific LWP, choose an event LWP
3966 from among those that have had events. Giving equal priority
3967 to all LWPs that have had events helps prevent
3969 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3970 select_event_lwp (ptid
, &lp
, &status
);
3972 /* Now that we've selected our final event LWP, cancel any
3973 breakpoints in other LWPs that have hit a GDB breakpoint.
3974 See the comment in cancel_breakpoints_callback to find out
3976 iterate_over_lwps (minus_one_ptid
, cancel_breakpoints_callback
, lp
);
3978 /* We'll need this to determine whether to report a SIGSTOP as
3979 TARGET_WAITKIND_0. Need to take a copy because
3980 resume_clear_callback clears it. */
3981 last_resume_kind
= lp
->last_resume_kind
;
3983 /* In all-stop, from the core's perspective, all LWPs are now
3984 stopped until a new resume action is sent over. */
3985 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
3990 last_resume_kind
= lp
->last_resume_kind
;
3991 resume_clear_callback (lp
, NULL
);
3994 if (linux_nat_status_is_event (status
))
3996 if (debug_linux_nat
)
3997 fprintf_unfiltered (gdb_stdlog
,
3998 "LLW: trap ptid is %s.\n",
3999 target_pid_to_str (lp
->ptid
));
4002 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
4004 *ourstatus
= lp
->waitstatus
;
4005 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
4008 store_waitstatus (ourstatus
, status
);
4010 if (debug_linux_nat
)
4011 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
4013 restore_child_signals_mask (&prev_mask
);
4015 if (last_resume_kind
== resume_stop
4016 && ourstatus
->kind
== TARGET_WAITKIND_STOPPED
4017 && WSTOPSIG (status
) == SIGSTOP
)
4019 /* A thread that has been requested to stop by GDB with
4020 target_stop, and it stopped cleanly, so report as SIG0. The
4021 use of SIGSTOP is an implementation detail. */
4022 ourstatus
->value
.sig
= TARGET_SIGNAL_0
;
4025 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
4026 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
4029 lp
->core
= linux_nat_core_of_thread_1 (lp
->ptid
);
4034 /* Resume LWPs that are currently stopped without any pending status
4035 to report, but are resumed from the core's perspective. */
4038 resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
)
4040 ptid_t
*wait_ptid_p
= data
;
4045 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
4047 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
4048 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
4049 CORE_ADDR pc
= regcache_read_pc (regcache
);
4051 gdb_assert (is_executing (lp
->ptid
));
4053 /* Don't bother if there's a breakpoint at PC that we'd hit
4054 immediately, and we're not waiting for this LWP. */
4055 if (!ptid_match (lp
->ptid
, *wait_ptid_p
))
4057 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
4061 if (debug_linux_nat
)
4062 fprintf_unfiltered (gdb_stdlog
,
4063 "RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
4064 target_pid_to_str (lp
->ptid
),
4065 paddress (gdbarch
, pc
),
4068 registers_changed ();
4069 if (linux_nat_prepare_to_resume
!= NULL
)
4070 linux_nat_prepare_to_resume (lp
);
4071 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
4072 lp
->step
, TARGET_SIGNAL_0
);
4074 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
4075 lp
->stopped_by_watchpoint
= 0;
4082 linux_nat_wait (struct target_ops
*ops
,
4083 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
4088 if (debug_linux_nat
)
4089 fprintf_unfiltered (gdb_stdlog
,
4090 "linux_nat_wait: [%s]\n", target_pid_to_str (ptid
));
4092 /* Flush the async file first. */
4093 if (target_can_async_p ())
4094 async_file_flush ();
4096 /* Resume LWPs that are currently stopped without any pending status
4097 to report, but are resumed from the core's perspective. LWPs get
4098 in this state if we find them stopping at a time we're not
4099 interested in reporting the event (target_wait on a
4100 specific_process, for example, see linux_nat_wait_1), and
4101 meanwhile the event became uninteresting. Don't bother resuming
4102 LWPs we're not going to wait for if they'd stop immediately. */
4104 iterate_over_lwps (minus_one_ptid
, resume_stopped_resumed_lwps
, &ptid
);
4106 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
, target_options
);
4108 /* If we requested any event, and something came out, assume there
4109 may be more. If we requested a specific lwp or process, also
4110 assume there may be more. */
4111 if (target_can_async_p ()
4112 && ((ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
4113 && ourstatus
->kind
!= TARGET_WAITKIND_NO_RESUMED
)
4114 || !ptid_equal (ptid
, minus_one_ptid
)))
4117 /* Get ready for the next event. */
4118 if (target_can_async_p ())
4119 target_async (inferior_event_handler
, 0);
4125 kill_callback (struct lwp_info
*lp
, void *data
)
4127 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
4130 kill (GET_LWP (lp
->ptid
), SIGKILL
);
4131 if (debug_linux_nat
)
4132 fprintf_unfiltered (gdb_stdlog
,
4133 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
4134 target_pid_to_str (lp
->ptid
),
4135 errno
? safe_strerror (errno
) : "OK");
4137 /* Some kernels ignore even SIGKILL for processes under ptrace. */
4140 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
4141 if (debug_linux_nat
)
4142 fprintf_unfiltered (gdb_stdlog
,
4143 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
4144 target_pid_to_str (lp
->ptid
),
4145 errno
? safe_strerror (errno
) : "OK");
4151 kill_wait_callback (struct lwp_info
*lp
, void *data
)
4155 /* We must make sure that there are no pending events (delayed
4156 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
4157 program doesn't interfere with any following debugging session. */
4159 /* For cloned processes we must check both with __WCLONE and
4160 without, since the exit status of a cloned process isn't reported
4166 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
4167 if (pid
!= (pid_t
) -1)
4169 if (debug_linux_nat
)
4170 fprintf_unfiltered (gdb_stdlog
,
4171 "KWC: wait %s received unknown.\n",
4172 target_pid_to_str (lp
->ptid
));
4173 /* The Linux kernel sometimes fails to kill a thread
4174 completely after PTRACE_KILL; that goes from the stop
4175 point in do_fork out to the one in
4176 get_signal_to_deliever and waits again. So kill it
4178 kill_callback (lp
, NULL
);
4181 while (pid
== GET_LWP (lp
->ptid
));
4183 gdb_assert (pid
== -1 && errno
== ECHILD
);
4188 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
4189 if (pid
!= (pid_t
) -1)
4191 if (debug_linux_nat
)
4192 fprintf_unfiltered (gdb_stdlog
,
4193 "KWC: wait %s received unk.\n",
4194 target_pid_to_str (lp
->ptid
));
4195 /* See the call to kill_callback above. */
4196 kill_callback (lp
, NULL
);
4199 while (pid
== GET_LWP (lp
->ptid
));
4201 gdb_assert (pid
== -1 && errno
== ECHILD
);
4206 linux_nat_kill (struct target_ops
*ops
)
4208 struct target_waitstatus last
;
4212 /* If we're stopped while forking and we haven't followed yet,
4213 kill the other task. We need to do this first because the
4214 parent will be sleeping if this is a vfork. */
4216 get_last_target_status (&last_ptid
, &last
);
4218 if (last
.kind
== TARGET_WAITKIND_FORKED
4219 || last
.kind
== TARGET_WAITKIND_VFORKED
)
4221 ptrace (PT_KILL
, PIDGET (last
.value
.related_pid
), 0, 0);
4225 if (forks_exist_p ())
4226 linux_fork_killall ();
4229 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
4231 /* Stop all threads before killing them, since ptrace requires
4232 that the thread is stopped to sucessfully PTRACE_KILL. */
4233 iterate_over_lwps (ptid
, stop_callback
, NULL
);
4234 /* ... and wait until all of them have reported back that
4235 they're no longer running. */
4236 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
4238 /* Kill all LWP's ... */
4239 iterate_over_lwps (ptid
, kill_callback
, NULL
);
4241 /* ... and wait until we've flushed all events. */
4242 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
4245 target_mourn_inferior ();
4249 linux_nat_mourn_inferior (struct target_ops
*ops
)
4251 purge_lwp_list (ptid_get_pid (inferior_ptid
));
4253 if (! forks_exist_p ())
4254 /* Normal case, no other forks available. */
4255 linux_ops
->to_mourn_inferior (ops
);
4257 /* Multi-fork case. The current inferior_ptid has exited, but
4258 there are other viable forks to debug. Delete the exiting
4259 one and context-switch to the first available. */
4260 linux_fork_mourn_inferior ();
4263 /* Convert a native/host siginfo object, into/from the siginfo in the
4264 layout of the inferiors' architecture. */
4267 siginfo_fixup (struct siginfo
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
4271 if (linux_nat_siginfo_fixup
!= NULL
)
4272 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
4274 /* If there was no callback, or the callback didn't do anything,
4275 then just do a straight memcpy. */
4279 memcpy (siginfo
, inf_siginfo
, sizeof (struct siginfo
));
4281 memcpy (inf_siginfo
, siginfo
, sizeof (struct siginfo
));
4286 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
4287 const char *annex
, gdb_byte
*readbuf
,
4288 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4291 struct siginfo siginfo
;
4292 gdb_byte inf_siginfo
[sizeof (struct siginfo
)];
4294 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
4295 gdb_assert (readbuf
|| writebuf
);
4297 pid
= GET_LWP (inferior_ptid
);
4299 pid
= GET_PID (inferior_ptid
);
4301 if (offset
> sizeof (siginfo
))
4305 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
4309 /* When GDB is built as a 64-bit application, ptrace writes into
4310 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
4311 inferior with a 64-bit GDB should look the same as debugging it
4312 with a 32-bit GDB, we need to convert it. GDB core always sees
4313 the converted layout, so any read/write will have to be done
4315 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
4317 if (offset
+ len
> sizeof (siginfo
))
4318 len
= sizeof (siginfo
) - offset
;
4320 if (readbuf
!= NULL
)
4321 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
4324 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
4326 /* Convert back to ptrace layout before flushing it out. */
4327 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
4330 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
4339 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4340 const char *annex
, gdb_byte
*readbuf
,
4341 const gdb_byte
*writebuf
,
4342 ULONGEST offset
, LONGEST len
)
4344 struct cleanup
*old_chain
;
4347 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
4348 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
4351 /* The target is connected but no live inferior is selected. Pass
4352 this request down to a lower stratum (e.g., the executable
4354 if (object
== TARGET_OBJECT_MEMORY
&& ptid_equal (inferior_ptid
, null_ptid
))
4357 old_chain
= save_inferior_ptid ();
4359 if (is_lwp (inferior_ptid
))
4360 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
4362 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4365 do_cleanups (old_chain
);
4370 linux_thread_alive (ptid_t ptid
)
4374 gdb_assert (is_lwp (ptid
));
4376 /* Send signal 0 instead of anything ptrace, because ptracing a
4377 running thread errors out claiming that the thread doesn't
4379 err
= kill_lwp (GET_LWP (ptid
), 0);
4381 if (debug_linux_nat
)
4382 fprintf_unfiltered (gdb_stdlog
,
4383 "LLTA: KILL(SIG0) %s (%s)\n",
4384 target_pid_to_str (ptid
),
4385 err
? safe_strerror (tmp_errno
) : "OK");
4394 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
4396 return linux_thread_alive (ptid
);
4400 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
4402 static char buf
[64];
4405 && (GET_PID (ptid
) != GET_LWP (ptid
)
4406 || num_lwps (GET_PID (ptid
)) > 1))
4408 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
4412 return normal_pid_to_str (ptid
);
4416 linux_nat_thread_name (struct thread_info
*thr
)
4418 int pid
= ptid_get_pid (thr
->ptid
);
4419 long lwp
= ptid_get_lwp (thr
->ptid
);
4420 #define FORMAT "/proc/%d/task/%ld/comm"
4421 char buf
[sizeof (FORMAT
) + 30];
4423 char *result
= NULL
;
4425 snprintf (buf
, sizeof (buf
), FORMAT
, pid
, lwp
);
4426 comm_file
= fopen (buf
, "r");
4429 /* Not exported by the kernel, so we define it here. */
4431 static char line
[COMM_LEN
+ 1];
4433 if (fgets (line
, sizeof (line
), comm_file
))
4435 char *nl
= strchr (line
, '\n');
4452 /* Accepts an integer PID; Returns a string representing a file that
4453 can be opened to get the symbols for the child process. */
4456 linux_child_pid_to_exec_file (int pid
)
4458 char *name1
, *name2
;
4460 name1
= xmalloc (MAXPATHLEN
);
4461 name2
= xmalloc (MAXPATHLEN
);
4462 make_cleanup (xfree
, name1
);
4463 make_cleanup (xfree
, name2
);
4464 memset (name2
, 0, MAXPATHLEN
);
4466 sprintf (name1
, "/proc/%d/exe", pid
);
4467 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
4473 /* Records the thread's register state for the corefile note
4477 linux_nat_collect_thread_registers (const struct regcache
*regcache
,
4478 ptid_t ptid
, bfd
*obfd
,
4479 char *note_data
, int *note_size
,
4480 enum target_signal stop_signal
)
4482 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
4483 const struct regset
*regset
;
4485 gdb_gregset_t gregs
;
4486 gdb_fpregset_t fpregs
;
4488 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
4491 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
4493 != NULL
&& regset
->collect_regset
!= NULL
)
4494 regset
->collect_regset (regset
, regcache
, -1, &gregs
, sizeof (gregs
));
4496 fill_gregset (regcache
, &gregs
, -1);
4498 note_data
= (char *) elfcore_write_prstatus
4499 (obfd
, note_data
, note_size
, ptid_get_lwp (ptid
),
4500 target_signal_to_host (stop_signal
), &gregs
);
4503 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
4505 != NULL
&& regset
->collect_regset
!= NULL
)
4506 regset
->collect_regset (regset
, regcache
, -1, &fpregs
, sizeof (fpregs
));
4508 fill_fpregset (regcache
, &fpregs
, -1);
4510 note_data
= (char *) elfcore_write_prfpreg (obfd
, note_data
, note_size
,
4511 &fpregs
, sizeof (fpregs
));
4516 /* Fills the "to_make_corefile_note" target vector. Builds the note
4517 section for a corefile, and returns it in a malloc buffer. */
4520 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
4522 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
4523 converted to gdbarch_core_regset_sections, this function can go away. */
4524 return linux_make_corefile_notes (target_gdbarch
, obfd
, note_size
,
4525 linux_nat_collect_thread_registers
);
4528 /* Implement the to_xfer_partial interface for memory reads using the /proc
4529 filesystem. Because we can use a single read() call for /proc, this
4530 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4531 but it doesn't support writes. */
4534 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4535 const char *annex
, gdb_byte
*readbuf
,
4536 const gdb_byte
*writebuf
,
4537 ULONGEST offset
, LONGEST len
)
4543 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
4546 /* Don't bother for one word. */
4547 if (len
< 3 * sizeof (long))
4550 /* We could keep this file open and cache it - possibly one per
4551 thread. That requires some juggling, but is even faster. */
4552 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
4553 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4557 /* If pread64 is available, use it. It's faster if the kernel
4558 supports it (only one syscall), and it's 64-bit safe even on
4559 32-bit platforms (for instance, SPARC debugging a SPARC64
4562 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
4564 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
4575 /* Enumerate spufs IDs for process PID. */
4577 spu_enumerate_spu_ids (int pid
, gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
4579 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
4581 LONGEST written
= 0;
4584 struct dirent
*entry
;
4586 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
4587 dir
= opendir (path
);
4592 while ((entry
= readdir (dir
)) != NULL
)
4598 fd
= atoi (entry
->d_name
);
4602 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
4603 if (stat (path
, &st
) != 0)
4605 if (!S_ISDIR (st
.st_mode
))
4608 if (statfs (path
, &stfs
) != 0)
4610 if (stfs
.f_type
!= SPUFS_MAGIC
)
4613 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
4615 store_unsigned_integer (buf
+ pos
- offset
, 4, byte_order
, fd
);
4625 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4626 object type, using the /proc file system. */
4628 linux_proc_xfer_spu (struct target_ops
*ops
, enum target_object object
,
4629 const char *annex
, gdb_byte
*readbuf
,
4630 const gdb_byte
*writebuf
,
4631 ULONGEST offset
, LONGEST len
)
4636 int pid
= PIDGET (inferior_ptid
);
4643 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
4646 xsnprintf (buf
, sizeof buf
, "/proc/%d/fd/%s", pid
, annex
);
4647 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
4652 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4659 ret
= write (fd
, writebuf
, (size_t) len
);
4661 ret
= read (fd
, readbuf
, (size_t) len
);
4668 /* Parse LINE as a signal set and add its set bits to SIGS. */
4671 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4673 int len
= strlen (line
) - 1;
4677 if (line
[len
] != '\n')
4678 error (_("Could not parse signal set: %s"), line
);
4686 if (*p
>= '0' && *p
<= '9')
4688 else if (*p
>= 'a' && *p
<= 'f')
4689 digit
= *p
- 'a' + 10;
4691 error (_("Could not parse signal set: %s"), line
);
4696 sigaddset (sigs
, signum
+ 1);
4698 sigaddset (sigs
, signum
+ 2);
4700 sigaddset (sigs
, signum
+ 3);
4702 sigaddset (sigs
, signum
+ 4);
4708 /* Find process PID's pending signals from /proc/pid/status and set
4712 linux_proc_pending_signals (int pid
, sigset_t
*pending
,
4713 sigset_t
*blocked
, sigset_t
*ignored
)
4716 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
4717 struct cleanup
*cleanup
;
4719 sigemptyset (pending
);
4720 sigemptyset (blocked
);
4721 sigemptyset (ignored
);
4722 sprintf (fname
, "/proc/%d/status", pid
);
4723 procfile
= fopen (fname
, "r");
4724 if (procfile
== NULL
)
4725 error (_("Could not open %s"), fname
);
4726 cleanup
= make_cleanup_fclose (procfile
);
4728 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
4730 /* Normal queued signals are on the SigPnd line in the status
4731 file. However, 2.6 kernels also have a "shared" pending
4732 queue for delivering signals to a thread group, so check for
4735 Unfortunately some Red Hat kernels include the shared pending
4736 queue but not the ShdPnd status field. */
4738 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
4739 add_line_to_sigset (buffer
+ 8, pending
);
4740 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
4741 add_line_to_sigset (buffer
+ 8, pending
);
4742 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
4743 add_line_to_sigset (buffer
+ 8, blocked
);
4744 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
4745 add_line_to_sigset (buffer
+ 8, ignored
);
4748 do_cleanups (cleanup
);
4752 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4753 const char *annex
, gdb_byte
*readbuf
,
4754 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4756 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4758 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4762 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4763 const char *annex
, gdb_byte
*readbuf
,
4764 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4768 if (object
== TARGET_OBJECT_AUXV
)
4769 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4772 if (object
== TARGET_OBJECT_OSDATA
)
4773 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4776 if (object
== TARGET_OBJECT_SPU
)
4777 return linux_proc_xfer_spu (ops
, object
, annex
, readbuf
, writebuf
,
4780 /* GDB calculates all the addresses in possibly larget width of the address.
4781 Address width needs to be masked before its final use - either by
4782 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4784 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4786 if (object
== TARGET_OBJECT_MEMORY
)
4788 int addr_bit
= gdbarch_addr_bit (target_gdbarch
);
4790 if (addr_bit
< (sizeof (ULONGEST
) * HOST_CHAR_BIT
))
4791 offset
&= ((ULONGEST
) 1 << addr_bit
) - 1;
4794 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4799 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4803 /* Create a prototype generic GNU/Linux target. The client can override
4804 it with local methods. */
4807 linux_target_install_ops (struct target_ops
*t
)
4809 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4810 t
->to_remove_fork_catchpoint
= linux_child_remove_fork_catchpoint
;
4811 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4812 t
->to_remove_vfork_catchpoint
= linux_child_remove_vfork_catchpoint
;
4813 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4814 t
->to_remove_exec_catchpoint
= linux_child_remove_exec_catchpoint
;
4815 t
->to_set_syscall_catchpoint
= linux_child_set_syscall_catchpoint
;
4816 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4817 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4818 t
->to_post_attach
= linux_child_post_attach
;
4819 t
->to_follow_fork
= linux_child_follow_fork
;
4820 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
4822 super_xfer_partial
= t
->to_xfer_partial
;
4823 t
->to_xfer_partial
= linux_xfer_partial
;
4829 struct target_ops
*t
;
4831 t
= inf_ptrace_target ();
4832 linux_target_install_ops (t
);
4838 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4840 struct target_ops
*t
;
4842 t
= inf_ptrace_trad_target (register_u_offset
);
4843 linux_target_install_ops (t
);
4848 /* target_is_async_p implementation. */
4851 linux_nat_is_async_p (void)
4853 /* NOTE: palves 2008-03-21: We're only async when the user requests
4854 it explicitly with the "set target-async" command.
4855 Someday, linux will always be async. */
4856 return target_async_permitted
;
4859 /* target_can_async_p implementation. */
4862 linux_nat_can_async_p (void)
4864 /* NOTE: palves 2008-03-21: We're only async when the user requests
4865 it explicitly with the "set target-async" command.
4866 Someday, linux will always be async. */
4867 return target_async_permitted
;
4871 linux_nat_supports_non_stop (void)
4876 /* True if we want to support multi-process. To be removed when GDB
4877 supports multi-exec. */
4879 int linux_multi_process
= 1;
4882 linux_nat_supports_multi_process (void)
4884 return linux_multi_process
;
4888 linux_nat_supports_disable_randomization (void)
4890 #ifdef HAVE_PERSONALITY
4897 static int async_terminal_is_ours
= 1;
4899 /* target_terminal_inferior implementation. */
4902 linux_nat_terminal_inferior (void)
4904 if (!target_is_async_p ())
4906 /* Async mode is disabled. */
4907 terminal_inferior ();
4911 terminal_inferior ();
4913 /* Calls to target_terminal_*() are meant to be idempotent. */
4914 if (!async_terminal_is_ours
)
4917 delete_file_handler (input_fd
);
4918 async_terminal_is_ours
= 0;
4922 /* target_terminal_ours implementation. */
4925 linux_nat_terminal_ours (void)
4927 if (!target_is_async_p ())
4929 /* Async mode is disabled. */
4934 /* GDB should never give the terminal to the inferior if the
4935 inferior is running in the background (run&, continue&, etc.),
4936 but claiming it sure should. */
4939 if (async_terminal_is_ours
)
4942 clear_sigint_trap ();
4943 add_file_handler (input_fd
, stdin_event_handler
, 0);
4944 async_terminal_is_ours
= 1;
4947 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4949 static void *async_client_context
;
4951 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4952 so we notice when any child changes state, and notify the
4953 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4954 above to wait for the arrival of a SIGCHLD. */
4957 sigchld_handler (int signo
)
4959 int old_errno
= errno
;
4961 if (debug_linux_nat
)
4962 ui_file_write_async_safe (gdb_stdlog
,
4963 "sigchld\n", sizeof ("sigchld\n") - 1);
4965 if (signo
== SIGCHLD
4966 && linux_nat_event_pipe
[0] != -1)
4967 async_file_mark (); /* Let the event loop know that there are
4968 events to handle. */
4973 /* Callback registered with the target events file descriptor. */
4976 handle_target_event (int error
, gdb_client_data client_data
)
4978 (*async_client_callback
) (INF_REG_EVENT
, async_client_context
);
4981 /* Create/destroy the target events pipe. Returns previous state. */
4984 linux_async_pipe (int enable
)
4986 int previous
= (linux_nat_event_pipe
[0] != -1);
4988 if (previous
!= enable
)
4992 block_child_signals (&prev_mask
);
4996 if (pipe (linux_nat_event_pipe
) == -1)
4997 internal_error (__FILE__
, __LINE__
,
4998 "creating event pipe failed.");
5000 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
5001 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
5005 close (linux_nat_event_pipe
[0]);
5006 close (linux_nat_event_pipe
[1]);
5007 linux_nat_event_pipe
[0] = -1;
5008 linux_nat_event_pipe
[1] = -1;
5011 restore_child_signals_mask (&prev_mask
);
5017 /* target_async implementation. */
5020 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
5021 void *context
), void *context
)
5023 if (callback
!= NULL
)
5025 async_client_callback
= callback
;
5026 async_client_context
= context
;
5027 if (!linux_async_pipe (1))
5029 add_file_handler (linux_nat_event_pipe
[0],
5030 handle_target_event
, NULL
);
5031 /* There may be pending events to handle. Tell the event loop
5038 async_client_callback
= callback
;
5039 async_client_context
= context
;
5040 delete_file_handler (linux_nat_event_pipe
[0]);
5041 linux_async_pipe (0);
5046 /* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
5050 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
5054 ptid_t ptid
= lwp
->ptid
;
5056 if (debug_linux_nat
)
5057 fprintf_unfiltered (gdb_stdlog
,
5058 "LNSL: running -> suspending %s\n",
5059 target_pid_to_str (lwp
->ptid
));
5062 if (lwp
->last_resume_kind
== resume_stop
)
5064 if (debug_linux_nat
)
5065 fprintf_unfiltered (gdb_stdlog
,
5066 "linux-nat: already stopping LWP %ld at "
5068 ptid_get_lwp (lwp
->ptid
));
5072 stop_callback (lwp
, NULL
);
5073 lwp
->last_resume_kind
= resume_stop
;
5077 /* Already known to be stopped; do nothing. */
5079 if (debug_linux_nat
)
5081 if (find_thread_ptid (lwp
->ptid
)->stop_requested
)
5082 fprintf_unfiltered (gdb_stdlog
,
5083 "LNSL: already stopped/stop_requested %s\n",
5084 target_pid_to_str (lwp
->ptid
));
5086 fprintf_unfiltered (gdb_stdlog
,
5087 "LNSL: already stopped/no "
5088 "stop_requested yet %s\n",
5089 target_pid_to_str (lwp
->ptid
));
5096 linux_nat_stop (ptid_t ptid
)
5099 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
5101 linux_ops
->to_stop (ptid
);
5105 linux_nat_close (int quitting
)
5107 /* Unregister from the event loop. */
5108 if (linux_nat_is_async_p ())
5109 linux_nat_async (NULL
, 0);
5111 if (linux_ops
->to_close
)
5112 linux_ops
->to_close (quitting
);
5115 /* When requests are passed down from the linux-nat layer to the
5116 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
5117 used. The address space pointer is stored in the inferior object,
5118 but the common code that is passed such ptid can't tell whether
5119 lwpid is a "main" process id or not (it assumes so). We reverse
5120 look up the "main" process id from the lwp here. */
5122 struct address_space
*
5123 linux_nat_thread_address_space (struct target_ops
*t
, ptid_t ptid
)
5125 struct lwp_info
*lwp
;
5126 struct inferior
*inf
;
5129 pid
= GET_LWP (ptid
);
5130 if (GET_LWP (ptid
) == 0)
5132 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
5134 lwp
= find_lwp_pid (ptid
);
5135 pid
= GET_PID (lwp
->ptid
);
5139 /* A (pid,lwpid,0) ptid. */
5140 pid
= GET_PID (ptid
);
5143 inf
= find_inferior_pid (pid
);
5144 gdb_assert (inf
!= NULL
);
5149 linux_nat_core_of_thread_1 (ptid_t ptid
)
5151 struct cleanup
*back_to
;
5154 char *content
= NULL
;
5157 int content_read
= 0;
5161 filename
= xstrprintf ("/proc/%d/task/%ld/stat",
5162 GET_PID (ptid
), GET_LWP (ptid
));
5163 back_to
= make_cleanup (xfree
, filename
);
5165 f
= fopen (filename
, "r");
5168 do_cleanups (back_to
);
5172 make_cleanup_fclose (f
);
5178 content
= xrealloc (content
, content_read
+ 1024);
5179 n
= fread (content
+ content_read
, 1, 1024, f
);
5183 content
[content_read
] = '\0';
5188 make_cleanup (xfree
, content
);
5190 p
= strchr (content
, '(');
5194 p
= strchr (p
, ')');
5198 /* If the first field after program name has index 0, then core number is
5199 the field with index 36. There's no constant for that anywhere. */
5201 p
= strtok_r (p
, " ", &ts
);
5202 for (i
= 0; p
!= NULL
&& i
!= 36; ++i
)
5203 p
= strtok_r (NULL
, " ", &ts
);
5205 if (p
== NULL
|| sscanf (p
, "%d", &core
) == 0)
5208 do_cleanups (back_to
);
5213 /* Return the cached value of the processor core for thread PTID. */
5216 linux_nat_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
5218 struct lwp_info
*info
= find_lwp_pid (ptid
);
5226 linux_nat_add_target (struct target_ops
*t
)
5228 /* Save the provided single-threaded target. We save this in a separate
5229 variable because another target we've inherited from (e.g. inf-ptrace)
5230 may have saved a pointer to T; we want to use it for the final
5231 process stratum target. */
5232 linux_ops_saved
= *t
;
5233 linux_ops
= &linux_ops_saved
;
5235 /* Override some methods for multithreading. */
5236 t
->to_create_inferior
= linux_nat_create_inferior
;
5237 t
->to_attach
= linux_nat_attach
;
5238 t
->to_detach
= linux_nat_detach
;
5239 t
->to_resume
= linux_nat_resume
;
5240 t
->to_wait
= linux_nat_wait
;
5241 t
->to_pass_signals
= linux_nat_pass_signals
;
5242 t
->to_xfer_partial
= linux_nat_xfer_partial
;
5243 t
->to_kill
= linux_nat_kill
;
5244 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
5245 t
->to_thread_alive
= linux_nat_thread_alive
;
5246 t
->to_pid_to_str
= linux_nat_pid_to_str
;
5247 t
->to_thread_name
= linux_nat_thread_name
;
5248 t
->to_has_thread_control
= tc_schedlock
;
5249 t
->to_thread_address_space
= linux_nat_thread_address_space
;
5250 t
->to_stopped_by_watchpoint
= linux_nat_stopped_by_watchpoint
;
5251 t
->to_stopped_data_address
= linux_nat_stopped_data_address
;
5253 t
->to_can_async_p
= linux_nat_can_async_p
;
5254 t
->to_is_async_p
= linux_nat_is_async_p
;
5255 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
5256 t
->to_async
= linux_nat_async
;
5257 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
5258 t
->to_terminal_ours
= linux_nat_terminal_ours
;
5259 t
->to_close
= linux_nat_close
;
5261 /* Methods for non-stop support. */
5262 t
->to_stop
= linux_nat_stop
;
5264 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
5266 t
->to_supports_disable_randomization
5267 = linux_nat_supports_disable_randomization
;
5269 t
->to_core_of_thread
= linux_nat_core_of_thread
;
5271 /* We don't change the stratum; this target will sit at
5272 process_stratum and thread_db will set at thread_stratum. This
5273 is a little strange, since this is a multi-threaded-capable
5274 target, but we want to be on the stack below thread_db, and we
5275 also want to be used for single-threaded processes. */
5280 /* Register a method to call whenever a new thread is attached. */
5282 linux_nat_set_new_thread (struct target_ops
*t
,
5283 void (*new_thread
) (struct lwp_info
*))
5285 /* Save the pointer. We only support a single registered instance
5286 of the GNU/Linux native target, so we do not need to map this to
5288 linux_nat_new_thread
= new_thread
;
5291 /* Register a method that converts a siginfo object between the layout
5292 that ptrace returns, and the layout in the architecture of the
5295 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
5296 int (*siginfo_fixup
) (struct siginfo
*,
5300 /* Save the pointer. */
5301 linux_nat_siginfo_fixup
= siginfo_fixup
;
5304 /* Register a method to call prior to resuming a thread. */
5307 linux_nat_set_prepare_to_resume (struct target_ops
*t
,
5308 void (*prepare_to_resume
) (struct lwp_info
*))
5310 /* Save the pointer. */
5311 linux_nat_prepare_to_resume
= prepare_to_resume
;
5314 /* Return the saved siginfo associated with PTID. */
5316 linux_nat_get_siginfo (ptid_t ptid
)
5318 struct lwp_info
*lp
= find_lwp_pid (ptid
);
5320 gdb_assert (lp
!= NULL
);
5322 return &lp
->siginfo
;
5325 /* Provide a prototype to silence -Wmissing-prototypes. */
5326 extern initialize_file_ftype _initialize_linux_nat
;
5329 _initialize_linux_nat (void)
5331 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance
,
5332 &debug_linux_nat
, _("\
5333 Set debugging of GNU/Linux lwp module."), _("\
5334 Show debugging of GNU/Linux lwp module."), _("\
5335 Enables printf debugging output."),
5337 show_debug_linux_nat
,
5338 &setdebuglist
, &showdebuglist
);
5340 /* Save this mask as the default. */
5341 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
5343 /* Install a SIGCHLD handler. */
5344 sigchld_action
.sa_handler
= sigchld_handler
;
5345 sigemptyset (&sigchld_action
.sa_mask
);
5346 sigchld_action
.sa_flags
= SA_RESTART
;
5348 /* Make it the default. */
5349 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
5351 /* Make sure we don't block SIGCHLD during a sigsuspend. */
5352 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
5353 sigdelset (&suspend_mask
, SIGCHLD
);
5355 sigemptyset (&blocked_mask
);
5359 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
5360 the GNU/Linux Threads library and therefore doesn't really belong
5363 /* Read variable NAME in the target and return its value if found.
5364 Otherwise return zero. It is assumed that the type of the variable
5368 get_signo (const char *name
)
5370 struct minimal_symbol
*ms
;
5373 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
5377 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
5378 sizeof (signo
)) != 0)
5384 /* Return the set of signals used by the threads library in *SET. */
5387 lin_thread_get_thread_signals (sigset_t
*set
)
5389 struct sigaction action
;
5390 int restart
, cancel
;
5392 sigemptyset (&blocked_mask
);
5395 restart
= get_signo ("__pthread_sig_restart");
5396 cancel
= get_signo ("__pthread_sig_cancel");
5398 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5399 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5400 not provide any way for the debugger to query the signal numbers -
5401 fortunately they don't change! */
5404 restart
= __SIGRTMIN
;
5407 cancel
= __SIGRTMIN
+ 1;
5409 sigaddset (set
, restart
);
5410 sigaddset (set
, cancel
);
5412 /* The GNU/Linux Threads library makes terminating threads send a
5413 special "cancel" signal instead of SIGCHLD. Make sure we catch
5414 those (to prevent them from terminating GDB itself, which is
5415 likely to be their default action) and treat them the same way as
5418 action
.sa_handler
= sigchld_handler
;
5419 sigemptyset (&action
.sa_mask
);
5420 action
.sa_flags
= SA_RESTART
;
5421 sigaction (cancel
, &action
, NULL
);
5423 /* We block the "cancel" signal throughout this code ... */
5424 sigaddset (&blocked_mask
, cancel
);
5425 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
5427 /* ... except during a sigsuspend. */
5428 sigdelset (&suspend_mask
, cancel
);