1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001-2013 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-child.h"
40 #include "inf-ptrace.h"
42 #include <sys/param.h> /* for MAXPATHLEN */
43 #include <sys/procfs.h> /* for elf_gregset etc. */
44 #include "elf-bfd.h" /* for elfcore_write_* */
45 #include "gregset.h" /* for gregset */
46 #include "gdbcore.h" /* for get_exec_file */
47 #include <ctype.h> /* for isdigit */
48 #include "gdbthread.h" /* for struct thread_info etc. */
49 #include "gdb_stat.h" /* for struct stat */
50 #include <fcntl.h> /* for O_RDONLY */
52 #include "event-loop.h"
53 #include "event-top.h"
55 #include <sys/types.h>
56 #include "gdb_dirent.h"
57 #include "xml-support.h"
61 #include "linux-osdata.h"
62 #include "linux-tdep.h"
65 #include "tracepoint.h"
66 #include "exceptions.h"
67 #include "linux-ptrace.h"
69 #include "target-descriptions.h"
72 #define SPUFS_MAGIC 0x23c9b64e
75 #ifdef HAVE_PERSONALITY
76 # include <sys/personality.h>
77 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
78 # define ADDR_NO_RANDOMIZE 0x0040000
80 #endif /* HAVE_PERSONALITY */
82 /* This comment documents high-level logic of this file.
84 Waiting for events in sync mode
85 ===============================
87 When waiting for an event in a specific thread, we just use waitpid, passing
88 the specific pid, and not passing WNOHANG.
90 When waiting for an event in all threads, waitpid is not quite good. Prior to
91 version 2.4, Linux can either wait for event in main thread, or in secondary
92 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
93 miss an event. The solution is to use non-blocking waitpid, together with
94 sigsuspend. First, we use non-blocking waitpid to get an event in the main
95 process, if any. Second, we use non-blocking waitpid with the __WCLONED
96 flag to check for events in cloned processes. If nothing is found, we use
97 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
98 happened to a child process -- and SIGCHLD will be delivered both for events
99 in main debugged process and in cloned processes. As soon as we know there's
100 an event, we get back to calling nonblocking waitpid with and without
103 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
104 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
105 blocked, the signal becomes pending and sigsuspend immediately
106 notices it and returns.
108 Waiting for events in async mode
109 ================================
111 In async mode, GDB should always be ready to handle both user input
112 and target events, so neither blocking waitpid nor sigsuspend are
113 viable options. Instead, we should asynchronously notify the GDB main
114 event loop whenever there's an unprocessed event from the target. We
115 detect asynchronous target events by handling SIGCHLD signals. To
116 notify the event loop about target events, the self-pipe trick is used
117 --- a pipe is registered as waitable event source in the event loop,
118 the event loop select/poll's on the read end of this pipe (as well on
119 other event sources, e.g., stdin), and the SIGCHLD handler writes a
120 byte to this pipe. This is more portable than relying on
121 pselect/ppoll, since on kernels that lack those syscalls, libc
122 emulates them with select/poll+sigprocmask, and that is racy
123 (a.k.a. plain broken).
125 Obviously, if we fail to notify the event loop if there's a target
126 event, it's bad. OTOH, if we notify the event loop when there's no
127 event from the target, linux_nat_wait will detect that there's no real
128 event to report, and return event of type TARGET_WAITKIND_IGNORE.
129 This is mostly harmless, but it will waste time and is better avoided.
131 The main design point is that every time GDB is outside linux-nat.c,
132 we have a SIGCHLD handler installed that is called when something
133 happens to the target and notifies the GDB event loop. Whenever GDB
134 core decides to handle the event, and calls into linux-nat.c, we
135 process things as in sync mode, except that the we never block in
138 While processing an event, we may end up momentarily blocked in
139 waitpid calls. Those waitpid calls, while blocking, are guarantied to
140 return quickly. E.g., in all-stop mode, before reporting to the core
141 that an LWP hit a breakpoint, all LWPs are stopped by sending them
142 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
143 Note that this is different from blocking indefinitely waiting for the
144 next event --- here, we're already handling an event.
149 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
150 signal is not entirely significant; we just need for a signal to be delivered,
151 so that we can intercept it. SIGSTOP's advantage is that it can not be
152 blocked. A disadvantage is that it is not a real-time signal, so it can only
153 be queued once; we do not keep track of other sources of SIGSTOP.
155 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
156 use them, because they have special behavior when the signal is generated -
157 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
158 kills the entire thread group.
160 A delivered SIGSTOP would stop the entire thread group, not just the thread we
161 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
162 cancel it (by PTRACE_CONT without passing SIGSTOP).
164 We could use a real-time signal instead. This would solve those problems; we
165 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
166 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
167 generates it, and there are races with trying to find a signal that is not
171 #define O_LARGEFILE 0
174 /* Unlike other extended result codes, WSTOPSIG (status) on
175 PTRACE_O_TRACESYSGOOD syscall events doesn't return SIGTRAP, but
176 instead SIGTRAP with bit 7 set. */
177 #define SYSCALL_SIGTRAP (SIGTRAP | 0x80)
179 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
180 the use of the multi-threaded target. */
181 static struct target_ops
*linux_ops
;
182 static struct target_ops linux_ops_saved
;
184 /* The method to call, if any, when a new thread is attached. */
185 static void (*linux_nat_new_thread
) (struct lwp_info
*);
187 /* Hook to call prior to resuming a thread. */
188 static void (*linux_nat_prepare_to_resume
) (struct lwp_info
*);
190 /* The method to call, if any, when the siginfo object needs to be
191 converted between the layout returned by ptrace, and the layout in
192 the architecture of the inferior. */
193 static int (*linux_nat_siginfo_fixup
) (siginfo_t
*,
197 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
198 Called by our to_xfer_partial. */
199 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
201 const char *, gdb_byte
*,
205 static unsigned int debug_linux_nat
;
207 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
208 struct cmd_list_element
*c
, const char *value
)
210 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
214 struct simple_pid_list
218 struct simple_pid_list
*next
;
220 struct simple_pid_list
*stopped_pids
;
222 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
223 can not be used, 1 if it can. */
225 static int linux_supports_tracefork_flag
= -1;
227 /* This variable is a tri-state flag: -1 for unknown, 0 if
228 PTRACE_O_TRACESYSGOOD can not be used, 1 if it can. */
230 static int linux_supports_tracesysgood_flag
= -1;
232 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
233 PTRACE_O_TRACEVFORKDONE. */
235 static int linux_supports_tracevforkdone_flag
= -1;
237 /* Stores the current used ptrace() options. */
238 static int current_ptrace_options
= 0;
240 /* Async mode support. */
242 /* The read/write ends of the pipe registered as waitable file in the
244 static int linux_nat_event_pipe
[2] = { -1, -1 };
246 /* Flush the event pipe. */
249 async_file_flush (void)
256 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
258 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
261 /* Put something (anything, doesn't matter what, or how much) in event
262 pipe, so that the select/poll in the event-loop realizes we have
263 something to process. */
266 async_file_mark (void)
270 /* It doesn't really matter what the pipe contains, as long we end
271 up with something in it. Might as well flush the previous
277 ret
= write (linux_nat_event_pipe
[1], "+", 1);
279 while (ret
== -1 && errno
== EINTR
);
281 /* Ignore EAGAIN. If the pipe is full, the event loop will already
282 be awakened anyway. */
285 static void linux_nat_async (void (*callback
)
286 (enum inferior_event_type event_type
,
289 static int kill_lwp (int lwpid
, int signo
);
291 static int stop_callback (struct lwp_info
*lp
, void *data
);
293 static void block_child_signals (sigset_t
*prev_mask
);
294 static void restore_child_signals_mask (sigset_t
*prev_mask
);
297 static struct lwp_info
*add_lwp (ptid_t ptid
);
298 static void purge_lwp_list (int pid
);
299 static void delete_lwp (ptid_t ptid
);
300 static struct lwp_info
*find_lwp_pid (ptid_t ptid
);
303 /* Trivial list manipulation functions to keep track of a list of
304 new stopped processes. */
306 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
308 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
311 new_pid
->status
= status
;
312 new_pid
->next
= *listp
;
317 in_pid_list_p (struct simple_pid_list
*list
, int pid
)
319 struct simple_pid_list
*p
;
321 for (p
= list
; p
!= NULL
; p
= p
->next
)
328 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
330 struct simple_pid_list
**p
;
332 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
333 if ((*p
)->pid
== pid
)
335 struct simple_pid_list
*next
= (*p
)->next
;
337 *statusp
= (*p
)->status
;
346 /* A helper function for linux_test_for_tracefork, called after fork (). */
349 linux_tracefork_child (void)
351 ptrace (PTRACE_TRACEME
, 0, 0, 0);
352 kill (getpid (), SIGSTOP
);
357 /* Wrapper function for waitpid which handles EINTR. */
360 my_waitpid (int pid
, int *statusp
, int flags
)
366 ret
= waitpid (pid
, statusp
, flags
);
368 while (ret
== -1 && errno
== EINTR
);
373 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
375 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
376 we know that the feature is not available. This may change the tracing
377 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
379 However, if it succeeds, we don't know for sure that the feature is
380 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
381 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
382 fork tracing, and let it fork. If the process exits, we assume that we
383 can't use TRACEFORK; if we get the fork notification, and we can extract
384 the new child's PID, then we assume that we can. */
387 linux_test_for_tracefork (int original_pid
)
389 int child_pid
, ret
, status
;
393 /* We don't want those ptrace calls to be interrupted. */
394 block_child_signals (&prev_mask
);
396 linux_supports_tracefork_flag
= 0;
397 linux_supports_tracevforkdone_flag
= 0;
399 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
402 restore_child_signals_mask (&prev_mask
);
408 perror_with_name (("fork"));
411 linux_tracefork_child ();
413 ret
= my_waitpid (child_pid
, &status
, 0);
415 perror_with_name (("waitpid"));
416 else if (ret
!= child_pid
)
417 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
418 if (! WIFSTOPPED (status
))
419 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."),
422 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
425 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
428 warning (_("linux_test_for_tracefork: failed to kill child"));
429 restore_child_signals_mask (&prev_mask
);
433 ret
= my_waitpid (child_pid
, &status
, 0);
434 if (ret
!= child_pid
)
435 warning (_("linux_test_for_tracefork: failed "
436 "to wait for killed child"));
437 else if (!WIFSIGNALED (status
))
438 warning (_("linux_test_for_tracefork: unexpected "
439 "wait status 0x%x from killed child"), status
);
441 restore_child_signals_mask (&prev_mask
);
445 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
446 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
447 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
448 linux_supports_tracevforkdone_flag
= (ret
== 0);
450 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
452 warning (_("linux_test_for_tracefork: failed to resume child"));
454 ret
= my_waitpid (child_pid
, &status
, 0);
456 if (ret
== child_pid
&& WIFSTOPPED (status
)
457 && status
>> 16 == PTRACE_EVENT_FORK
)
460 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
461 if (ret
== 0 && second_pid
!= 0)
465 linux_supports_tracefork_flag
= 1;
466 my_waitpid (second_pid
, &second_status
, 0);
467 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
469 warning (_("linux_test_for_tracefork: "
470 "failed to kill second child"));
471 my_waitpid (second_pid
, &status
, 0);
475 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
476 "(%d, status 0x%x)"), ret
, status
);
478 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
480 warning (_("linux_test_for_tracefork: failed to kill child"));
481 my_waitpid (child_pid
, &status
, 0);
483 restore_child_signals_mask (&prev_mask
);
486 /* Determine if PTRACE_O_TRACESYSGOOD can be used to follow syscalls.
488 We try to enable syscall tracing on ORIGINAL_PID. If this fails,
489 we know that the feature is not available. This may change the tracing
490 options for ORIGINAL_PID, but we'll be setting them shortly anyway. */
493 linux_test_for_tracesysgood (int original_pid
)
498 /* We don't want those ptrace calls to be interrupted. */
499 block_child_signals (&prev_mask
);
501 linux_supports_tracesysgood_flag
= 0;
503 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACESYSGOOD
);
507 linux_supports_tracesysgood_flag
= 1;
509 restore_child_signals_mask (&prev_mask
);
512 /* Determine wether we support PTRACE_O_TRACESYSGOOD option available.
513 This function also sets linux_supports_tracesysgood_flag. */
516 linux_supports_tracesysgood (int pid
)
518 if (linux_supports_tracesysgood_flag
== -1)
519 linux_test_for_tracesysgood (pid
);
520 return linux_supports_tracesysgood_flag
;
523 /* Return non-zero iff we have tracefork functionality available.
524 This function also sets linux_supports_tracefork_flag. */
527 linux_supports_tracefork (int pid
)
529 if (linux_supports_tracefork_flag
== -1)
530 linux_test_for_tracefork (pid
);
531 return linux_supports_tracefork_flag
;
535 linux_supports_tracevforkdone (int pid
)
537 if (linux_supports_tracefork_flag
== -1)
538 linux_test_for_tracefork (pid
);
539 return linux_supports_tracevforkdone_flag
;
543 linux_enable_tracesysgood (ptid_t ptid
)
545 int pid
= ptid_get_lwp (ptid
);
548 pid
= ptid_get_pid (ptid
);
550 if (linux_supports_tracesysgood (pid
) == 0)
553 current_ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
555 ptrace (PTRACE_SETOPTIONS
, pid
, 0, current_ptrace_options
);
560 linux_enable_event_reporting (ptid_t ptid
)
562 int pid
= ptid_get_lwp (ptid
);
565 pid
= ptid_get_pid (ptid
);
567 if (! linux_supports_tracefork (pid
))
570 current_ptrace_options
|= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
571 | PTRACE_O_TRACEEXEC
| PTRACE_O_TRACECLONE
;
573 if (linux_supports_tracevforkdone (pid
))
574 current_ptrace_options
|= PTRACE_O_TRACEVFORKDONE
;
576 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
577 read-only process state. */
579 ptrace (PTRACE_SETOPTIONS
, pid
, 0, current_ptrace_options
);
583 linux_child_post_attach (int pid
)
585 linux_enable_event_reporting (pid_to_ptid (pid
));
586 linux_enable_tracesysgood (pid_to_ptid (pid
));
587 linux_ptrace_init_warnings ();
591 linux_child_post_startup_inferior (ptid_t ptid
)
593 linux_enable_event_reporting (ptid
);
594 linux_enable_tracesysgood (ptid
);
595 linux_ptrace_init_warnings ();
598 /* Return the number of known LWPs in the tgid given by PID. */
606 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
607 if (ptid_get_pid (lp
->ptid
) == pid
)
613 /* Call delete_lwp with prototype compatible for make_cleanup. */
616 delete_lwp_cleanup (void *lp_voidp
)
618 struct lwp_info
*lp
= lp_voidp
;
620 delete_lwp (lp
->ptid
);
624 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
628 int parent_pid
, child_pid
;
630 block_child_signals (&prev_mask
);
632 has_vforked
= (inferior_thread ()->pending_follow
.kind
633 == TARGET_WAITKIND_VFORKED
);
634 parent_pid
= ptid_get_lwp (inferior_ptid
);
636 parent_pid
= ptid_get_pid (inferior_ptid
);
637 child_pid
= PIDGET (inferior_thread ()->pending_follow
.value
.related_pid
);
640 linux_enable_event_reporting (pid_to_ptid (child_pid
));
643 && !non_stop
/* Non-stop always resumes both branches. */
644 && (!target_is_async_p () || sync_execution
)
645 && !(follow_child
|| detach_fork
|| sched_multi
))
647 /* The parent stays blocked inside the vfork syscall until the
648 child execs or exits. If we don't let the child run, then
649 the parent stays blocked. If we're telling the parent to run
650 in the foreground, the user will not be able to ctrl-c to get
651 back the terminal, effectively hanging the debug session. */
652 fprintf_filtered (gdb_stderr
, _("\
653 Can not resume the parent process over vfork in the foreground while\n\
654 holding the child stopped. Try \"set detach-on-fork\" or \
655 \"set schedule-multiple\".\n"));
656 /* FIXME output string > 80 columns. */
662 struct lwp_info
*child_lp
= NULL
;
664 /* We're already attached to the parent, by default. */
666 /* Detach new forked process? */
669 struct cleanup
*old_chain
;
671 /* Before detaching from the child, remove all breakpoints
672 from it. If we forked, then this has already been taken
673 care of by infrun.c. If we vforked however, any
674 breakpoint inserted in the parent is visible in the
675 child, even those added while stopped in a vfork
676 catchpoint. This will remove the breakpoints from the
677 parent also, but they'll be reinserted below. */
680 /* keep breakpoints list in sync. */
681 remove_breakpoints_pid (GET_PID (inferior_ptid
));
684 if (info_verbose
|| debug_linux_nat
)
686 target_terminal_ours ();
687 fprintf_filtered (gdb_stdlog
,
688 "Detaching after fork from "
689 "child process %d.\n",
693 old_chain
= save_inferior_ptid ();
694 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
696 child_lp
= add_lwp (inferior_ptid
);
697 child_lp
->stopped
= 1;
698 child_lp
->last_resume_kind
= resume_stop
;
699 make_cleanup (delete_lwp_cleanup
, child_lp
);
701 /* CHILD_LP has new PID, therefore linux_nat_new_thread is not called for it.
702 See i386_inferior_data_get for the Linux kernel specifics.
703 Ensure linux_nat_prepare_to_resume will reset the hardware debug
704 registers. It is done by the linux_nat_new_thread call, which is
705 being skipped in add_lwp above for the first lwp of a pid. */
706 gdb_assert (num_lwps (GET_PID (child_lp
->ptid
)) == 1);
707 if (linux_nat_new_thread
!= NULL
)
708 linux_nat_new_thread (child_lp
);
710 if (linux_nat_prepare_to_resume
!= NULL
)
711 linux_nat_prepare_to_resume (child_lp
);
712 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
714 do_cleanups (old_chain
);
718 struct inferior
*parent_inf
, *child_inf
;
719 struct cleanup
*old_chain
;
721 /* Add process to GDB's tables. */
722 child_inf
= add_inferior (child_pid
);
724 parent_inf
= current_inferior ();
725 child_inf
->attach_flag
= parent_inf
->attach_flag
;
726 copy_terminal_info (child_inf
, parent_inf
);
727 child_inf
->gdbarch
= parent_inf
->gdbarch
;
728 copy_inferior_target_desc_info (child_inf
, parent_inf
);
730 old_chain
= save_inferior_ptid ();
731 save_current_program_space ();
733 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
734 add_thread (inferior_ptid
);
735 child_lp
= add_lwp (inferior_ptid
);
736 child_lp
->stopped
= 1;
737 child_lp
->last_resume_kind
= resume_stop
;
738 child_inf
->symfile_flags
= SYMFILE_NO_READ
;
740 /* If this is a vfork child, then the address-space is
741 shared with the parent. */
744 child_inf
->pspace
= parent_inf
->pspace
;
745 child_inf
->aspace
= parent_inf
->aspace
;
747 /* The parent will be frozen until the child is done
748 with the shared region. Keep track of the
750 child_inf
->vfork_parent
= parent_inf
;
751 child_inf
->pending_detach
= 0;
752 parent_inf
->vfork_child
= child_inf
;
753 parent_inf
->pending_detach
= 0;
757 child_inf
->aspace
= new_address_space ();
758 child_inf
->pspace
= add_program_space (child_inf
->aspace
);
759 child_inf
->removable
= 1;
760 set_current_program_space (child_inf
->pspace
);
761 clone_program_space (child_inf
->pspace
, parent_inf
->pspace
);
763 /* Let the shared library layer (solib-svr4) learn about
764 this new process, relocate the cloned exec, pull in
765 shared libraries, and install the solib event
766 breakpoint. If a "cloned-VM" event was propagated
767 better throughout the core, this wouldn't be
769 solib_create_inferior_hook (0);
772 /* Let the thread_db layer learn about this new process. */
773 check_for_thread_db ();
775 do_cleanups (old_chain
);
780 struct lwp_info
*parent_lp
;
781 struct inferior
*parent_inf
;
783 parent_inf
= current_inferior ();
785 /* If we detached from the child, then we have to be careful
786 to not insert breakpoints in the parent until the child
787 is done with the shared memory region. However, if we're
788 staying attached to the child, then we can and should
789 insert breakpoints, so that we can debug it. A
790 subsequent child exec or exit is enough to know when does
791 the child stops using the parent's address space. */
792 parent_inf
->waiting_for_vfork_done
= detach_fork
;
793 parent_inf
->pspace
->breakpoints_not_allowed
= detach_fork
;
795 parent_lp
= find_lwp_pid (pid_to_ptid (parent_pid
));
796 gdb_assert (linux_supports_tracefork_flag
>= 0);
798 if (linux_supports_tracevforkdone (0))
801 fprintf_unfiltered (gdb_stdlog
,
802 "LCFF: waiting for VFORK_DONE on %d\n",
804 parent_lp
->stopped
= 1;
806 /* We'll handle the VFORK_DONE event like any other
807 event, in target_wait. */
811 /* We can't insert breakpoints until the child has
812 finished with the shared memory region. We need to
813 wait until that happens. Ideal would be to just
815 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
816 - waitpid (parent_pid, &status, __WALL);
817 However, most architectures can't handle a syscall
818 being traced on the way out if it wasn't traced on
821 We might also think to loop, continuing the child
822 until it exits or gets a SIGTRAP. One problem is
823 that the child might call ptrace with PTRACE_TRACEME.
825 There's no simple and reliable way to figure out when
826 the vforked child will be done with its copy of the
827 shared memory. We could step it out of the syscall,
828 two instructions, let it go, and then single-step the
829 parent once. When we have hardware single-step, this
830 would work; with software single-step it could still
831 be made to work but we'd have to be able to insert
832 single-step breakpoints in the child, and we'd have
833 to insert -just- the single-step breakpoint in the
834 parent. Very awkward.
836 In the end, the best we can do is to make sure it
837 runs for a little while. Hopefully it will be out of
838 range of any breakpoints we reinsert. Usually this
839 is only the single-step breakpoint at vfork's return
843 fprintf_unfiltered (gdb_stdlog
,
844 "LCFF: no VFORK_DONE "
845 "support, sleeping a bit\n");
849 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
850 and leave it pending. The next linux_nat_resume call
851 will notice a pending event, and bypasses actually
852 resuming the inferior. */
853 parent_lp
->status
= 0;
854 parent_lp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
855 parent_lp
->stopped
= 1;
857 /* If we're in async mode, need to tell the event loop
858 there's something here to process. */
859 if (target_can_async_p ())
866 struct inferior
*parent_inf
, *child_inf
;
867 struct lwp_info
*child_lp
;
868 struct program_space
*parent_pspace
;
870 if (info_verbose
|| debug_linux_nat
)
872 target_terminal_ours ();
874 fprintf_filtered (gdb_stdlog
,
875 _("Attaching after process %d "
876 "vfork to child process %d.\n"),
877 parent_pid
, child_pid
);
879 fprintf_filtered (gdb_stdlog
,
880 _("Attaching after process %d "
881 "fork to child process %d.\n"),
882 parent_pid
, child_pid
);
885 /* Add the new inferior first, so that the target_detach below
886 doesn't unpush the target. */
888 child_inf
= add_inferior (child_pid
);
890 parent_inf
= current_inferior ();
891 child_inf
->attach_flag
= parent_inf
->attach_flag
;
892 copy_terminal_info (child_inf
, parent_inf
);
893 child_inf
->gdbarch
= parent_inf
->gdbarch
;
894 copy_inferior_target_desc_info (child_inf
, parent_inf
);
896 parent_pspace
= parent_inf
->pspace
;
898 /* If we're vforking, we want to hold on to the parent until the
899 child exits or execs. At child exec or exit time we can
900 remove the old breakpoints from the parent and detach or
901 resume debugging it. Otherwise, detach the parent now; we'll
902 want to reuse it's program/address spaces, but we can't set
903 them to the child before removing breakpoints from the
904 parent, otherwise, the breakpoints module could decide to
905 remove breakpoints from the wrong process (since they'd be
906 assigned to the same address space). */
910 gdb_assert (child_inf
->vfork_parent
== NULL
);
911 gdb_assert (parent_inf
->vfork_child
== NULL
);
912 child_inf
->vfork_parent
= parent_inf
;
913 child_inf
->pending_detach
= 0;
914 parent_inf
->vfork_child
= child_inf
;
915 parent_inf
->pending_detach
= detach_fork
;
916 parent_inf
->waiting_for_vfork_done
= 0;
918 else if (detach_fork
)
919 target_detach (NULL
, 0);
921 /* Note that the detach above makes PARENT_INF dangling. */
923 /* Add the child thread to the appropriate lists, and switch to
924 this new thread, before cloning the program space, and
925 informing the solib layer about this new process. */
927 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
928 add_thread (inferior_ptid
);
929 child_lp
= add_lwp (inferior_ptid
);
930 child_lp
->stopped
= 1;
931 child_lp
->last_resume_kind
= resume_stop
;
933 /* If this is a vfork child, then the address-space is shared
934 with the parent. If we detached from the parent, then we can
935 reuse the parent's program/address spaces. */
936 if (has_vforked
|| detach_fork
)
938 child_inf
->pspace
= parent_pspace
;
939 child_inf
->aspace
= child_inf
->pspace
->aspace
;
943 child_inf
->aspace
= new_address_space ();
944 child_inf
->pspace
= add_program_space (child_inf
->aspace
);
945 child_inf
->removable
= 1;
946 child_inf
->symfile_flags
= SYMFILE_NO_READ
;
947 set_current_program_space (child_inf
->pspace
);
948 clone_program_space (child_inf
->pspace
, parent_pspace
);
950 /* Let the shared library layer (solib-svr4) learn about
951 this new process, relocate the cloned exec, pull in
952 shared libraries, and install the solib event breakpoint.
953 If a "cloned-VM" event was propagated better throughout
954 the core, this wouldn't be required. */
955 solib_create_inferior_hook (0);
958 /* Let the thread_db layer learn about this new process. */
959 check_for_thread_db ();
962 restore_child_signals_mask (&prev_mask
);
968 linux_child_insert_fork_catchpoint (int pid
)
970 return !linux_supports_tracefork (pid
);
974 linux_child_remove_fork_catchpoint (int pid
)
980 linux_child_insert_vfork_catchpoint (int pid
)
982 return !linux_supports_tracefork (pid
);
986 linux_child_remove_vfork_catchpoint (int pid
)
992 linux_child_insert_exec_catchpoint (int pid
)
994 return !linux_supports_tracefork (pid
);
998 linux_child_remove_exec_catchpoint (int pid
)
1004 linux_child_set_syscall_catchpoint (int pid
, int needed
, int any_count
,
1005 int table_size
, int *table
)
1007 if (!linux_supports_tracesysgood (pid
))
1010 /* On GNU/Linux, we ignore the arguments. It means that we only
1011 enable the syscall catchpoints, but do not disable them.
1013 Also, we do not use the `table' information because we do not
1014 filter system calls here. We let GDB do the logic for us. */
1018 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
1019 are processes sharing the same VM space. A multi-threaded process
1020 is basically a group of such processes. However, such a grouping
1021 is almost entirely a user-space issue; the kernel doesn't enforce
1022 such a grouping at all (this might change in the future). In
1023 general, we'll rely on the threads library (i.e. the GNU/Linux
1024 Threads library) to provide such a grouping.
1026 It is perfectly well possible to write a multi-threaded application
1027 without the assistance of a threads library, by using the clone
1028 system call directly. This module should be able to give some
1029 rudimentary support for debugging such applications if developers
1030 specify the CLONE_PTRACE flag in the clone system call, and are
1031 using the Linux kernel 2.4 or above.
1033 Note that there are some peculiarities in GNU/Linux that affect
1036 - In general one should specify the __WCLONE flag to waitpid in
1037 order to make it report events for any of the cloned processes
1038 (and leave it out for the initial process). However, if a cloned
1039 process has exited the exit status is only reported if the
1040 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
1041 we cannot use it since GDB must work on older systems too.
1043 - When a traced, cloned process exits and is waited for by the
1044 debugger, the kernel reassigns it to the original parent and
1045 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
1046 library doesn't notice this, which leads to the "zombie problem":
1047 When debugged a multi-threaded process that spawns a lot of
1048 threads will run out of processes, even if the threads exit,
1049 because the "zombies" stay around. */
1051 /* List of known LWPs. */
1052 struct lwp_info
*lwp_list
;
1055 /* Original signal mask. */
1056 static sigset_t normal_mask
;
1058 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
1059 _initialize_linux_nat. */
1060 static sigset_t suspend_mask
;
1062 /* Signals to block to make that sigsuspend work. */
1063 static sigset_t blocked_mask
;
1065 /* SIGCHLD action. */
1066 struct sigaction sigchld_action
;
1068 /* Block child signals (SIGCHLD and linux threads signals), and store
1069 the previous mask in PREV_MASK. */
1072 block_child_signals (sigset_t
*prev_mask
)
1074 /* Make sure SIGCHLD is blocked. */
1075 if (!sigismember (&blocked_mask
, SIGCHLD
))
1076 sigaddset (&blocked_mask
, SIGCHLD
);
1078 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
1081 /* Restore child signals mask, previously returned by
1082 block_child_signals. */
1085 restore_child_signals_mask (sigset_t
*prev_mask
)
1087 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
1090 /* Mask of signals to pass directly to the inferior. */
1091 static sigset_t pass_mask
;
1093 /* Update signals to pass to the inferior. */
1095 linux_nat_pass_signals (int numsigs
, unsigned char *pass_signals
)
1099 sigemptyset (&pass_mask
);
1101 for (signo
= 1; signo
< NSIG
; signo
++)
1103 int target_signo
= gdb_signal_from_host (signo
);
1104 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
1105 sigaddset (&pass_mask
, signo
);
1111 /* Prototypes for local functions. */
1112 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
1113 static int linux_thread_alive (ptid_t ptid
);
1114 static char *linux_child_pid_to_exec_file (int pid
);
1117 /* Convert wait status STATUS to a string. Used for printing debug
1121 status_to_str (int status
)
1123 static char buf
[64];
1125 if (WIFSTOPPED (status
))
1127 if (WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
1128 snprintf (buf
, sizeof (buf
), "%s (stopped at syscall)",
1129 strsignal (SIGTRAP
));
1131 snprintf (buf
, sizeof (buf
), "%s (stopped)",
1132 strsignal (WSTOPSIG (status
)));
1134 else if (WIFSIGNALED (status
))
1135 snprintf (buf
, sizeof (buf
), "%s (terminated)",
1136 strsignal (WTERMSIG (status
)));
1138 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
1143 /* Destroy and free LP. */
1146 lwp_free (struct lwp_info
*lp
)
1148 xfree (lp
->arch_private
);
1152 /* Remove all LWPs belong to PID from the lwp list. */
1155 purge_lwp_list (int pid
)
1157 struct lwp_info
*lp
, *lpprev
, *lpnext
;
1161 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1165 if (ptid_get_pid (lp
->ptid
) == pid
)
1168 lwp_list
= lp
->next
;
1170 lpprev
->next
= lp
->next
;
1179 /* Add the LWP specified by PID to the list. Return a pointer to the
1180 structure describing the new LWP. The LWP should already be stopped
1181 (with an exception for the very first LWP). */
1183 static struct lwp_info
*
1184 add_lwp (ptid_t ptid
)
1186 struct lwp_info
*lp
;
1188 gdb_assert (is_lwp (ptid
));
1190 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
1192 memset (lp
, 0, sizeof (struct lwp_info
));
1194 lp
->last_resume_kind
= resume_continue
;
1195 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1200 lp
->next
= lwp_list
;
1203 /* Let the arch specific bits know about this new thread. Current
1204 clients of this callback take the opportunity to install
1205 watchpoints in the new thread. Don't do this for the first
1206 thread though. If we're spawning a child ("run"), the thread
1207 executes the shell wrapper first, and we shouldn't touch it until
1208 it execs the program we want to debug. For "attach", it'd be
1209 okay to call the callback, but it's not necessary, because
1210 watchpoints can't yet have been inserted into the inferior. */
1211 if (num_lwps (GET_PID (ptid
)) > 1 && linux_nat_new_thread
!= NULL
)
1212 linux_nat_new_thread (lp
);
1217 /* Remove the LWP specified by PID from the list. */
1220 delete_lwp (ptid_t ptid
)
1222 struct lwp_info
*lp
, *lpprev
;
1226 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
1227 if (ptid_equal (lp
->ptid
, ptid
))
1234 lpprev
->next
= lp
->next
;
1236 lwp_list
= lp
->next
;
1241 /* Return a pointer to the structure describing the LWP corresponding
1242 to PID. If no corresponding LWP could be found, return NULL. */
1244 static struct lwp_info
*
1245 find_lwp_pid (ptid_t ptid
)
1247 struct lwp_info
*lp
;
1251 lwp
= GET_LWP (ptid
);
1253 lwp
= GET_PID (ptid
);
1255 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1256 if (lwp
== GET_LWP (lp
->ptid
))
1262 /* Call CALLBACK with its second argument set to DATA for every LWP in
1263 the list. If CALLBACK returns 1 for a particular LWP, return a
1264 pointer to the structure describing that LWP immediately.
1265 Otherwise return NULL. */
1268 iterate_over_lwps (ptid_t filter
,
1269 int (*callback
) (struct lwp_info
*, void *),
1272 struct lwp_info
*lp
, *lpnext
;
1274 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1278 if (ptid_match (lp
->ptid
, filter
))
1280 if ((*callback
) (lp
, data
))
1288 /* Iterate like iterate_over_lwps does except when forking-off a child call
1289 CALLBACK with CALLBACK_DATA specifically only for that new child PID. */
1292 linux_nat_iterate_watchpoint_lwps
1293 (linux_nat_iterate_watchpoint_lwps_ftype callback
, void *callback_data
)
1295 int inferior_pid
= ptid_get_pid (inferior_ptid
);
1296 struct inferior
*inf
= current_inferior ();
1298 if (inf
->pid
== inferior_pid
)
1300 /* Iterate all the threads of the current inferior. Without specifying
1301 INFERIOR_PID it would iterate all threads of all inferiors, which is
1302 inappropriate for watchpoints. */
1304 iterate_over_lwps (pid_to_ptid (inferior_pid
), callback
, callback_data
);
1308 /* Detaching a new child PID temporarily present in INFERIOR_PID. */
1310 struct lwp_info
*child_lp
;
1311 struct cleanup
*old_chain
;
1312 pid_t child_pid
= GET_PID (inferior_ptid
);
1313 ptid_t child_ptid
= ptid_build (child_pid
, child_pid
, 0);
1315 gdb_assert (find_lwp_pid (child_ptid
) == NULL
);
1316 child_lp
= add_lwp (child_ptid
);
1317 child_lp
->stopped
= 1;
1318 child_lp
->last_resume_kind
= resume_stop
;
1319 old_chain
= make_cleanup (delete_lwp_cleanup
, child_lp
);
1321 callback (child_lp
, callback_data
);
1323 do_cleanups (old_chain
);
1327 /* Update our internal state when changing from one checkpoint to
1328 another indicated by NEW_PTID. We can only switch single-threaded
1329 applications, so we only create one new LWP, and the previous list
1333 linux_nat_switch_fork (ptid_t new_ptid
)
1335 struct lwp_info
*lp
;
1337 purge_lwp_list (GET_PID (inferior_ptid
));
1339 lp
= add_lwp (new_ptid
);
1342 /* This changes the thread's ptid while preserving the gdb thread
1343 num. Also changes the inferior pid, while preserving the
1345 thread_change_ptid (inferior_ptid
, new_ptid
);
1347 /* We've just told GDB core that the thread changed target id, but,
1348 in fact, it really is a different thread, with different register
1350 registers_changed ();
1353 /* Handle the exit of a single thread LP. */
1356 exit_lwp (struct lwp_info
*lp
)
1358 struct thread_info
*th
= find_thread_ptid (lp
->ptid
);
1362 if (print_thread_events
)
1363 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1365 delete_thread (lp
->ptid
);
1368 delete_lwp (lp
->ptid
);
1371 /* Wait for the LWP specified by LP, which we have just attached to.
1372 Returns a wait status for that LWP, to cache. */
1375 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1378 pid_t new_pid
, pid
= GET_LWP (ptid
);
1381 if (linux_proc_pid_is_stopped (pid
))
1383 if (debug_linux_nat
)
1384 fprintf_unfiltered (gdb_stdlog
,
1385 "LNPAW: Attaching to a stopped process\n");
1387 /* The process is definitely stopped. It is in a job control
1388 stop, unless the kernel predates the TASK_STOPPED /
1389 TASK_TRACED distinction, in which case it might be in a
1390 ptrace stop. Make sure it is in a ptrace stop; from there we
1391 can kill it, signal it, et cetera.
1393 First make sure there is a pending SIGSTOP. Since we are
1394 already attached, the process can not transition from stopped
1395 to running without a PTRACE_CONT; so we know this signal will
1396 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1397 probably already in the queue (unless this kernel is old
1398 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1399 is not an RT signal, it can only be queued once. */
1400 kill_lwp (pid
, SIGSTOP
);
1402 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1403 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1404 ptrace (PTRACE_CONT
, pid
, 0, 0);
1407 /* Make sure the initial process is stopped. The user-level threads
1408 layer might want to poke around in the inferior, and that won't
1409 work if things haven't stabilized yet. */
1410 new_pid
= my_waitpid (pid
, &status
, 0);
1411 if (new_pid
== -1 && errno
== ECHILD
)
1414 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1416 /* Try again with __WCLONE to check cloned processes. */
1417 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1421 gdb_assert (pid
== new_pid
);
1423 if (!WIFSTOPPED (status
))
1425 /* The pid we tried to attach has apparently just exited. */
1426 if (debug_linux_nat
)
1427 fprintf_unfiltered (gdb_stdlog
, "LNPAW: Failed to stop %d: %s",
1428 pid
, status_to_str (status
));
1432 if (WSTOPSIG (status
) != SIGSTOP
)
1435 if (debug_linux_nat
)
1436 fprintf_unfiltered (gdb_stdlog
,
1437 "LNPAW: Received %s after attaching\n",
1438 status_to_str (status
));
1444 /* Attach to the LWP specified by PID. Return 0 if successful, -1 if
1445 the new LWP could not be attached, or 1 if we're already auto
1446 attached to this thread, but haven't processed the
1447 PTRACE_EVENT_CLONE event of its parent thread, so we just ignore
1448 its existance, without considering it an error. */
1451 lin_lwp_attach_lwp (ptid_t ptid
)
1453 struct lwp_info
*lp
;
1457 gdb_assert (is_lwp (ptid
));
1459 block_child_signals (&prev_mask
);
1461 lp
= find_lwp_pid (ptid
);
1462 lwpid
= GET_LWP (ptid
);
1464 /* We assume that we're already attached to any LWP that has an id
1465 equal to the overall process id, and to any LWP that is already
1466 in our list of LWPs. If we're not seeing exit events from threads
1467 and we've had PID wraparound since we last tried to stop all threads,
1468 this assumption might be wrong; fortunately, this is very unlikely
1470 if (lwpid
!= GET_PID (ptid
) && lp
== NULL
)
1472 int status
, cloned
= 0, signalled
= 0;
1474 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) < 0)
1476 if (linux_supports_tracefork_flag
)
1478 /* If we haven't stopped all threads when we get here,
1479 we may have seen a thread listed in thread_db's list,
1480 but not processed the PTRACE_EVENT_CLONE yet. If
1481 that's the case, ignore this new thread, and let
1482 normal event handling discover it later. */
1483 if (in_pid_list_p (stopped_pids
, lwpid
))
1485 /* We've already seen this thread stop, but we
1486 haven't seen the PTRACE_EVENT_CLONE extended
1488 restore_child_signals_mask (&prev_mask
);
1496 /* See if we've got a stop for this new child
1497 pending. If so, we're already attached. */
1498 new_pid
= my_waitpid (lwpid
, &status
, WNOHANG
);
1499 if (new_pid
== -1 && errno
== ECHILD
)
1500 new_pid
= my_waitpid (lwpid
, &status
, __WCLONE
| WNOHANG
);
1503 if (WIFSTOPPED (status
))
1504 add_to_pid_list (&stopped_pids
, lwpid
, status
);
1506 restore_child_signals_mask (&prev_mask
);
1512 /* If we fail to attach to the thread, issue a warning,
1513 but continue. One way this can happen is if thread
1514 creation is interrupted; as of Linux kernel 2.6.19, a
1515 bug may place threads in the thread list and then fail
1517 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1518 safe_strerror (errno
));
1519 restore_child_signals_mask (&prev_mask
);
1523 if (debug_linux_nat
)
1524 fprintf_unfiltered (gdb_stdlog
,
1525 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1526 target_pid_to_str (ptid
));
1528 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1529 if (!WIFSTOPPED (status
))
1531 restore_child_signals_mask (&prev_mask
);
1535 lp
= add_lwp (ptid
);
1537 lp
->cloned
= cloned
;
1538 lp
->signalled
= signalled
;
1539 if (WSTOPSIG (status
) != SIGSTOP
)
1542 lp
->status
= status
;
1545 target_post_attach (GET_LWP (lp
->ptid
));
1547 if (debug_linux_nat
)
1549 fprintf_unfiltered (gdb_stdlog
,
1550 "LLAL: waitpid %s received %s\n",
1551 target_pid_to_str (ptid
),
1552 status_to_str (status
));
1557 /* We assume that the LWP representing the original process is
1558 already stopped. Mark it as stopped in the data structure
1559 that the GNU/linux ptrace layer uses to keep track of
1560 threads. Note that this won't have already been done since
1561 the main thread will have, we assume, been stopped by an
1562 attach from a different layer. */
1564 lp
= add_lwp (ptid
);
1568 lp
->last_resume_kind
= resume_stop
;
1569 restore_child_signals_mask (&prev_mask
);
1574 linux_nat_create_inferior (struct target_ops
*ops
,
1575 char *exec_file
, char *allargs
, char **env
,
1578 #ifdef HAVE_PERSONALITY
1579 int personality_orig
= 0, personality_set
= 0;
1580 #endif /* HAVE_PERSONALITY */
1582 /* The fork_child mechanism is synchronous and calls target_wait, so
1583 we have to mask the async mode. */
1585 #ifdef HAVE_PERSONALITY
1586 if (disable_randomization
)
1589 personality_orig
= personality (0xffffffff);
1590 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
1592 personality_set
= 1;
1593 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
1595 if (errno
!= 0 || (personality_set
1596 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
1597 warning (_("Error disabling address space randomization: %s"),
1598 safe_strerror (errno
));
1600 #endif /* HAVE_PERSONALITY */
1602 /* Make sure we report all signals during startup. */
1603 linux_nat_pass_signals (0, NULL
);
1605 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1607 #ifdef HAVE_PERSONALITY
1608 if (personality_set
)
1611 personality (personality_orig
);
1613 warning (_("Error restoring address space randomization: %s"),
1614 safe_strerror (errno
));
1616 #endif /* HAVE_PERSONALITY */
1620 linux_nat_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1622 struct lwp_info
*lp
;
1625 volatile struct gdb_exception ex
;
1627 /* Make sure we report all signals during attach. */
1628 linux_nat_pass_signals (0, NULL
);
1630 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
1632 linux_ops
->to_attach (ops
, args
, from_tty
);
1636 pid_t pid
= parse_pid_to_attach (args
);
1637 struct buffer buffer
;
1638 char *message
, *buffer_s
;
1640 message
= xstrdup (ex
.message
);
1641 make_cleanup (xfree
, message
);
1643 buffer_init (&buffer
);
1644 linux_ptrace_attach_warnings (pid
, &buffer
);
1646 buffer_grow_str0 (&buffer
, "");
1647 buffer_s
= buffer_finish (&buffer
);
1648 make_cleanup (xfree
, buffer_s
);
1650 throw_error (ex
.error
, "%s%s", buffer_s
, message
);
1653 /* The ptrace base target adds the main thread with (pid,0,0)
1654 format. Decorate it with lwp info. */
1655 ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1656 thread_change_ptid (inferior_ptid
, ptid
);
1658 /* Add the initial process as the first LWP to the list. */
1659 lp
= add_lwp (ptid
);
1661 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1663 if (!WIFSTOPPED (status
))
1665 if (WIFEXITED (status
))
1667 int exit_code
= WEXITSTATUS (status
);
1669 target_terminal_ours ();
1670 target_mourn_inferior ();
1672 error (_("Unable to attach: program exited normally."));
1674 error (_("Unable to attach: program exited with code %d."),
1677 else if (WIFSIGNALED (status
))
1679 enum gdb_signal signo
;
1681 target_terminal_ours ();
1682 target_mourn_inferior ();
1684 signo
= gdb_signal_from_host (WTERMSIG (status
));
1685 error (_("Unable to attach: program terminated with signal "
1687 gdb_signal_to_name (signo
),
1688 gdb_signal_to_string (signo
));
1691 internal_error (__FILE__
, __LINE__
,
1692 _("unexpected status %d for PID %ld"),
1693 status
, (long) GET_LWP (ptid
));
1698 /* Save the wait status to report later. */
1700 if (debug_linux_nat
)
1701 fprintf_unfiltered (gdb_stdlog
,
1702 "LNA: waitpid %ld, saving status %s\n",
1703 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1705 lp
->status
= status
;
1707 if (target_can_async_p ())
1708 target_async (inferior_event_handler
, 0);
1711 /* Get pending status of LP. */
1713 get_pending_status (struct lwp_info
*lp
, int *status
)
1715 enum gdb_signal signo
= GDB_SIGNAL_0
;
1717 /* If we paused threads momentarily, we may have stored pending
1718 events in lp->status or lp->waitstatus (see stop_wait_callback),
1719 and GDB core hasn't seen any signal for those threads.
1720 Otherwise, the last signal reported to the core is found in the
1721 thread object's stop_signal.
1723 There's a corner case that isn't handled here at present. Only
1724 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1725 stop_signal make sense as a real signal to pass to the inferior.
1726 Some catchpoint related events, like
1727 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1728 to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1729 those traps are debug API (ptrace in our case) related and
1730 induced; the inferior wouldn't see them if it wasn't being
1731 traced. Hence, we should never pass them to the inferior, even
1732 when set to pass state. Since this corner case isn't handled by
1733 infrun.c when proceeding with a signal, for consistency, neither
1734 do we handle it here (or elsewhere in the file we check for
1735 signal pass state). Normally SIGTRAP isn't set to pass state, so
1736 this is really a corner case. */
1738 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
1739 signo
= GDB_SIGNAL_0
; /* a pending ptrace event, not a real signal. */
1740 else if (lp
->status
)
1741 signo
= gdb_signal_from_host (WSTOPSIG (lp
->status
));
1742 else if (non_stop
&& !is_executing (lp
->ptid
))
1744 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1746 signo
= tp
->suspend
.stop_signal
;
1750 struct target_waitstatus last
;
1753 get_last_target_status (&last_ptid
, &last
);
1755 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1757 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1759 signo
= tp
->suspend
.stop_signal
;
1765 if (signo
== GDB_SIGNAL_0
)
1767 if (debug_linux_nat
)
1768 fprintf_unfiltered (gdb_stdlog
,
1769 "GPT: lwp %s has no pending signal\n",
1770 target_pid_to_str (lp
->ptid
));
1772 else if (!signal_pass_state (signo
))
1774 if (debug_linux_nat
)
1775 fprintf_unfiltered (gdb_stdlog
,
1776 "GPT: lwp %s had signal %s, "
1777 "but it is in no pass state\n",
1778 target_pid_to_str (lp
->ptid
),
1779 gdb_signal_to_string (signo
));
1783 *status
= W_STOPCODE (gdb_signal_to_host (signo
));
1785 if (debug_linux_nat
)
1786 fprintf_unfiltered (gdb_stdlog
,
1787 "GPT: lwp %s has pending signal %s\n",
1788 target_pid_to_str (lp
->ptid
),
1789 gdb_signal_to_string (signo
));
1796 detach_callback (struct lwp_info
*lp
, void *data
)
1798 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1800 if (debug_linux_nat
&& lp
->status
)
1801 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1802 strsignal (WSTOPSIG (lp
->status
)),
1803 target_pid_to_str (lp
->ptid
));
1805 /* If there is a pending SIGSTOP, get rid of it. */
1808 if (debug_linux_nat
)
1809 fprintf_unfiltered (gdb_stdlog
,
1810 "DC: Sending SIGCONT to %s\n",
1811 target_pid_to_str (lp
->ptid
));
1813 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1817 /* We don't actually detach from the LWP that has an id equal to the
1818 overall process id just yet. */
1819 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1823 /* Pass on any pending signal for this LWP. */
1824 get_pending_status (lp
, &status
);
1826 if (linux_nat_prepare_to_resume
!= NULL
)
1827 linux_nat_prepare_to_resume (lp
);
1829 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1830 WSTOPSIG (status
)) < 0)
1831 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1832 safe_strerror (errno
));
1834 if (debug_linux_nat
)
1835 fprintf_unfiltered (gdb_stdlog
,
1836 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1837 target_pid_to_str (lp
->ptid
),
1838 strsignal (WSTOPSIG (status
)));
1840 delete_lwp (lp
->ptid
);
1847 linux_nat_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1851 struct lwp_info
*main_lwp
;
1853 pid
= GET_PID (inferior_ptid
);
1855 /* Don't unregister from the event loop, as there may be other
1856 inferiors running. */
1858 /* Stop all threads before detaching. ptrace requires that the
1859 thread is stopped to sucessfully detach. */
1860 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1861 /* ... and wait until all of them have reported back that
1862 they're no longer running. */
1863 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1865 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1867 /* Only the initial process should be left right now. */
1868 gdb_assert (num_lwps (GET_PID (inferior_ptid
)) == 1);
1870 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1872 /* Pass on any pending signal for the last LWP. */
1873 if ((args
== NULL
|| *args
== '\0')
1874 && get_pending_status (main_lwp
, &status
) != -1
1875 && WIFSTOPPED (status
))
1877 /* Put the signal number in ARGS so that inf_ptrace_detach will
1878 pass it along with PTRACE_DETACH. */
1880 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1881 if (debug_linux_nat
)
1882 fprintf_unfiltered (gdb_stdlog
,
1883 "LND: Sending signal %s to %s\n",
1885 target_pid_to_str (main_lwp
->ptid
));
1888 if (linux_nat_prepare_to_resume
!= NULL
)
1889 linux_nat_prepare_to_resume (main_lwp
);
1890 delete_lwp (main_lwp
->ptid
);
1892 if (forks_exist_p ())
1894 /* Multi-fork case. The current inferior_ptid is being detached
1895 from, but there are other viable forks to debug. Detach from
1896 the current fork, and context-switch to the first
1898 linux_fork_detach (args
, from_tty
);
1901 linux_ops
->to_detach (ops
, args
, from_tty
);
1907 resume_lwp (struct lwp_info
*lp
, int step
, enum gdb_signal signo
)
1911 struct inferior
*inf
= find_inferior_pid (GET_PID (lp
->ptid
));
1913 if (inf
->vfork_child
!= NULL
)
1915 if (debug_linux_nat
)
1916 fprintf_unfiltered (gdb_stdlog
,
1917 "RC: Not resuming %s (vfork parent)\n",
1918 target_pid_to_str (lp
->ptid
));
1920 else if (lp
->status
== 0
1921 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
1923 if (debug_linux_nat
)
1924 fprintf_unfiltered (gdb_stdlog
,
1925 "RC: Resuming sibling %s, %s, %s\n",
1926 target_pid_to_str (lp
->ptid
),
1927 (signo
!= GDB_SIGNAL_0
1928 ? strsignal (gdb_signal_to_host (signo
))
1930 step
? "step" : "resume");
1932 if (linux_nat_prepare_to_resume
!= NULL
)
1933 linux_nat_prepare_to_resume (lp
);
1934 linux_ops
->to_resume (linux_ops
,
1935 pid_to_ptid (GET_LWP (lp
->ptid
)),
1939 lp
->stopped_by_watchpoint
= 0;
1943 if (debug_linux_nat
)
1944 fprintf_unfiltered (gdb_stdlog
,
1945 "RC: Not resuming sibling %s (has pending)\n",
1946 target_pid_to_str (lp
->ptid
));
1951 if (debug_linux_nat
)
1952 fprintf_unfiltered (gdb_stdlog
,
1953 "RC: Not resuming sibling %s (not stopped)\n",
1954 target_pid_to_str (lp
->ptid
));
1958 /* Resume LWP, with the last stop signal, if it is in pass state. */
1961 linux_nat_resume_callback (struct lwp_info
*lp
, void *data
)
1963 enum gdb_signal signo
= GDB_SIGNAL_0
;
1967 struct thread_info
*thread
;
1969 thread
= find_thread_ptid (lp
->ptid
);
1972 if (signal_pass_state (thread
->suspend
.stop_signal
))
1973 signo
= thread
->suspend
.stop_signal
;
1974 thread
->suspend
.stop_signal
= GDB_SIGNAL_0
;
1978 resume_lwp (lp
, 0, signo
);
1983 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1986 lp
->last_resume_kind
= resume_stop
;
1991 resume_set_callback (struct lwp_info
*lp
, void *data
)
1994 lp
->last_resume_kind
= resume_continue
;
1999 linux_nat_resume (struct target_ops
*ops
,
2000 ptid_t ptid
, int step
, enum gdb_signal signo
)
2003 struct lwp_info
*lp
;
2006 if (debug_linux_nat
)
2007 fprintf_unfiltered (gdb_stdlog
,
2008 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
2009 step
? "step" : "resume",
2010 target_pid_to_str (ptid
),
2011 (signo
!= GDB_SIGNAL_0
2012 ? strsignal (gdb_signal_to_host (signo
)) : "0"),
2013 target_pid_to_str (inferior_ptid
));
2015 block_child_signals (&prev_mask
);
2017 /* A specific PTID means `step only this process id'. */
2018 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
2019 || ptid_is_pid (ptid
));
2021 /* Mark the lwps we're resuming as resumed. */
2022 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
2024 /* See if it's the current inferior that should be handled
2027 lp
= find_lwp_pid (inferior_ptid
);
2029 lp
= find_lwp_pid (ptid
);
2030 gdb_assert (lp
!= NULL
);
2032 /* Remember if we're stepping. */
2034 lp
->last_resume_kind
= step
? resume_step
: resume_continue
;
2036 /* If we have a pending wait status for this thread, there is no
2037 point in resuming the process. But first make sure that
2038 linux_nat_wait won't preemptively handle the event - we
2039 should never take this short-circuit if we are going to
2040 leave LP running, since we have skipped resuming all the
2041 other threads. This bit of code needs to be synchronized
2042 with linux_nat_wait. */
2044 if (lp
->status
&& WIFSTOPPED (lp
->status
))
2047 && WSTOPSIG (lp
->status
)
2048 && sigismember (&pass_mask
, WSTOPSIG (lp
->status
)))
2050 if (debug_linux_nat
)
2051 fprintf_unfiltered (gdb_stdlog
,
2052 "LLR: Not short circuiting for ignored "
2053 "status 0x%x\n", lp
->status
);
2055 /* FIXME: What should we do if we are supposed to continue
2056 this thread with a signal? */
2057 gdb_assert (signo
== GDB_SIGNAL_0
);
2058 signo
= gdb_signal_from_host (WSTOPSIG (lp
->status
));
2063 if (lp
->status
|| lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2065 /* FIXME: What should we do if we are supposed to continue
2066 this thread with a signal? */
2067 gdb_assert (signo
== GDB_SIGNAL_0
);
2069 if (debug_linux_nat
)
2070 fprintf_unfiltered (gdb_stdlog
,
2071 "LLR: Short circuiting for status 0x%x\n",
2074 restore_child_signals_mask (&prev_mask
);
2075 if (target_can_async_p ())
2077 target_async (inferior_event_handler
, 0);
2078 /* Tell the event loop we have something to process. */
2084 /* Mark LWP as not stopped to prevent it from being continued by
2085 linux_nat_resume_callback. */
2089 iterate_over_lwps (ptid
, linux_nat_resume_callback
, NULL
);
2091 /* Convert to something the lower layer understands. */
2092 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
2094 if (linux_nat_prepare_to_resume
!= NULL
)
2095 linux_nat_prepare_to_resume (lp
);
2096 linux_ops
->to_resume (linux_ops
, ptid
, step
, signo
);
2097 lp
->stopped_by_watchpoint
= 0;
2099 if (debug_linux_nat
)
2100 fprintf_unfiltered (gdb_stdlog
,
2101 "LLR: %s %s, %s (resume event thread)\n",
2102 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2103 target_pid_to_str (ptid
),
2104 (signo
!= GDB_SIGNAL_0
2105 ? strsignal (gdb_signal_to_host (signo
)) : "0"));
2107 restore_child_signals_mask (&prev_mask
);
2108 if (target_can_async_p ())
2109 target_async (inferior_event_handler
, 0);
2112 /* Send a signal to an LWP. */
2115 kill_lwp (int lwpid
, int signo
)
2117 /* Use tkill, if possible, in case we are using nptl threads. If tkill
2118 fails, then we are not using nptl threads and we should be using kill. */
2120 #ifdef HAVE_TKILL_SYSCALL
2122 static int tkill_failed
;
2129 ret
= syscall (__NR_tkill
, lwpid
, signo
);
2130 if (errno
!= ENOSYS
)
2137 return kill (lwpid
, signo
);
2140 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
2141 event, check if the core is interested in it: if not, ignore the
2142 event, and keep waiting; otherwise, we need to toggle the LWP's
2143 syscall entry/exit status, since the ptrace event itself doesn't
2144 indicate it, and report the trap to higher layers. */
2147 linux_handle_syscall_trap (struct lwp_info
*lp
, int stopping
)
2149 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
2150 struct gdbarch
*gdbarch
= target_thread_architecture (lp
->ptid
);
2151 int syscall_number
= (int) gdbarch_get_syscall_number (gdbarch
, lp
->ptid
);
2155 /* If we're stopping threads, there's a SIGSTOP pending, which
2156 makes it so that the LWP reports an immediate syscall return,
2157 followed by the SIGSTOP. Skip seeing that "return" using
2158 PTRACE_CONT directly, and let stop_wait_callback collect the
2159 SIGSTOP. Later when the thread is resumed, a new syscall
2160 entry event. If we didn't do this (and returned 0), we'd
2161 leave a syscall entry pending, and our caller, by using
2162 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
2163 itself. Later, when the user re-resumes this LWP, we'd see
2164 another syscall entry event and we'd mistake it for a return.
2166 If stop_wait_callback didn't force the SIGSTOP out of the LWP
2167 (leaving immediately with LWP->signalled set, without issuing
2168 a PTRACE_CONT), it would still be problematic to leave this
2169 syscall enter pending, as later when the thread is resumed,
2170 it would then see the same syscall exit mentioned above,
2171 followed by the delayed SIGSTOP, while the syscall didn't
2172 actually get to execute. It seems it would be even more
2173 confusing to the user. */
2175 if (debug_linux_nat
)
2176 fprintf_unfiltered (gdb_stdlog
,
2177 "LHST: ignoring syscall %d "
2178 "for LWP %ld (stopping threads), "
2179 "resuming with PTRACE_CONT for SIGSTOP\n",
2181 GET_LWP (lp
->ptid
));
2183 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2184 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2188 if (catch_syscall_enabled ())
2190 /* Always update the entry/return state, even if this particular
2191 syscall isn't interesting to the core now. In async mode,
2192 the user could install a new catchpoint for this syscall
2193 between syscall enter/return, and we'll need to know to
2194 report a syscall return if that happens. */
2195 lp
->syscall_state
= (lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2196 ? TARGET_WAITKIND_SYSCALL_RETURN
2197 : TARGET_WAITKIND_SYSCALL_ENTRY
);
2199 if (catching_syscall_number (syscall_number
))
2201 /* Alright, an event to report. */
2202 ourstatus
->kind
= lp
->syscall_state
;
2203 ourstatus
->value
.syscall_number
= syscall_number
;
2205 if (debug_linux_nat
)
2206 fprintf_unfiltered (gdb_stdlog
,
2207 "LHST: stopping for %s of syscall %d"
2210 == TARGET_WAITKIND_SYSCALL_ENTRY
2211 ? "entry" : "return",
2213 GET_LWP (lp
->ptid
));
2217 if (debug_linux_nat
)
2218 fprintf_unfiltered (gdb_stdlog
,
2219 "LHST: ignoring %s of syscall %d "
2221 lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2222 ? "entry" : "return",
2224 GET_LWP (lp
->ptid
));
2228 /* If we had been syscall tracing, and hence used PT_SYSCALL
2229 before on this LWP, it could happen that the user removes all
2230 syscall catchpoints before we get to process this event.
2231 There are two noteworthy issues here:
2233 - When stopped at a syscall entry event, resuming with
2234 PT_STEP still resumes executing the syscall and reports a
2237 - Only PT_SYSCALL catches syscall enters. If we last
2238 single-stepped this thread, then this event can't be a
2239 syscall enter. If we last single-stepped this thread, this
2240 has to be a syscall exit.
2242 The points above mean that the next resume, be it PT_STEP or
2243 PT_CONTINUE, can not trigger a syscall trace event. */
2244 if (debug_linux_nat
)
2245 fprintf_unfiltered (gdb_stdlog
,
2246 "LHST: caught syscall event "
2247 "with no syscall catchpoints."
2248 " %d for LWP %ld, ignoring\n",
2250 GET_LWP (lp
->ptid
));
2251 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2254 /* The core isn't interested in this event. For efficiency, avoid
2255 stopping all threads only to have the core resume them all again.
2256 Since we're not stopping threads, if we're still syscall tracing
2257 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
2258 subsequent syscall. Simply resume using the inf-ptrace layer,
2259 which knows when to use PT_SYSCALL or PT_CONTINUE. */
2261 /* Note that gdbarch_get_syscall_number may access registers, hence
2263 registers_changed ();
2264 if (linux_nat_prepare_to_resume
!= NULL
)
2265 linux_nat_prepare_to_resume (lp
);
2266 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2267 lp
->step
, GDB_SIGNAL_0
);
2271 /* Handle a GNU/Linux extended wait response. If we see a clone
2272 event, we need to add the new LWP to our list (and not report the
2273 trap to higher layers). This function returns non-zero if the
2274 event should be ignored and we should wait again. If STOPPING is
2275 true, the new LWP remains stopped, otherwise it is continued. */
2278 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
2281 int pid
= GET_LWP (lp
->ptid
);
2282 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
2283 int event
= status
>> 16;
2285 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
2286 || event
== PTRACE_EVENT_CLONE
)
2288 unsigned long new_pid
;
2291 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
2293 /* If we haven't already seen the new PID stop, wait for it now. */
2294 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
2296 /* The new child has a pending SIGSTOP. We can't affect it until it
2297 hits the SIGSTOP, but we're already attached. */
2298 ret
= my_waitpid (new_pid
, &status
,
2299 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
2301 perror_with_name (_("waiting for new child"));
2302 else if (ret
!= new_pid
)
2303 internal_error (__FILE__
, __LINE__
,
2304 _("wait returned unexpected PID %d"), ret
);
2305 else if (!WIFSTOPPED (status
))
2306 internal_error (__FILE__
, __LINE__
,
2307 _("wait returned unexpected status 0x%x"), status
);
2310 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
2312 if (event
== PTRACE_EVENT_FORK
2313 && linux_fork_checkpointing_p (GET_PID (lp
->ptid
)))
2315 /* Handle checkpointing by linux-fork.c here as a special
2316 case. We don't want the follow-fork-mode or 'catch fork'
2317 to interfere with this. */
2319 /* This won't actually modify the breakpoint list, but will
2320 physically remove the breakpoints from the child. */
2321 detach_breakpoints (ptid_build (new_pid
, new_pid
, 0));
2323 /* Retain child fork in ptrace (stopped) state. */
2324 if (!find_fork_pid (new_pid
))
2327 /* Report as spurious, so that infrun doesn't want to follow
2328 this fork. We're actually doing an infcall in
2330 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
2331 linux_enable_event_reporting (pid_to_ptid (new_pid
));
2333 /* Report the stop to the core. */
2337 if (event
== PTRACE_EVENT_FORK
)
2338 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
2339 else if (event
== PTRACE_EVENT_VFORK
)
2340 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
2343 struct lwp_info
*new_lp
;
2345 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2347 if (debug_linux_nat
)
2348 fprintf_unfiltered (gdb_stdlog
,
2349 "LHEW: Got clone event "
2350 "from LWP %d, new child is LWP %ld\n",
2353 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (lp
->ptid
)));
2355 new_lp
->stopped
= 1;
2357 if (WSTOPSIG (status
) != SIGSTOP
)
2359 /* This can happen if someone starts sending signals to
2360 the new thread before it gets a chance to run, which
2361 have a lower number than SIGSTOP (e.g. SIGUSR1).
2362 This is an unlikely case, and harder to handle for
2363 fork / vfork than for clone, so we do not try - but
2364 we handle it for clone events here. We'll send
2365 the other signal on to the thread below. */
2367 new_lp
->signalled
= 1;
2371 struct thread_info
*tp
;
2373 /* When we stop for an event in some other thread, and
2374 pull the thread list just as this thread has cloned,
2375 we'll have seen the new thread in the thread_db list
2376 before handling the CLONE event (glibc's
2377 pthread_create adds the new thread to the thread list
2378 before clone'ing, and has the kernel fill in the
2379 thread's tid on the clone call with
2380 CLONE_PARENT_SETTID). If that happened, and the core
2381 had requested the new thread to stop, we'll have
2382 killed it with SIGSTOP. But since SIGSTOP is not an
2383 RT signal, it can only be queued once. We need to be
2384 careful to not resume the LWP if we wanted it to
2385 stop. In that case, we'll leave the SIGSTOP pending.
2386 It will later be reported as GDB_SIGNAL_0. */
2387 tp
= find_thread_ptid (new_lp
->ptid
);
2388 if (tp
!= NULL
&& tp
->stop_requested
)
2389 new_lp
->last_resume_kind
= resume_stop
;
2396 /* Add the new thread to GDB's lists as soon as possible
2399 1) the frontend doesn't have to wait for a stop to
2402 2) we tag it with the correct running state. */
2404 /* If the thread_db layer is active, let it know about
2405 this new thread, and add it to GDB's list. */
2406 if (!thread_db_attach_lwp (new_lp
->ptid
))
2408 /* We're not using thread_db. Add it to GDB's
2410 target_post_attach (GET_LWP (new_lp
->ptid
));
2411 add_thread (new_lp
->ptid
);
2416 set_running (new_lp
->ptid
, 1);
2417 set_executing (new_lp
->ptid
, 1);
2418 /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
2420 new_lp
->last_resume_kind
= resume_continue
;
2426 /* We created NEW_LP so it cannot yet contain STATUS. */
2427 gdb_assert (new_lp
->status
== 0);
2429 /* Save the wait status to report later. */
2430 if (debug_linux_nat
)
2431 fprintf_unfiltered (gdb_stdlog
,
2432 "LHEW: waitpid of new LWP %ld, "
2433 "saving status %s\n",
2434 (long) GET_LWP (new_lp
->ptid
),
2435 status_to_str (status
));
2436 new_lp
->status
= status
;
2439 /* Note the need to use the low target ops to resume, to
2440 handle resuming with PT_SYSCALL if we have syscall
2444 new_lp
->resumed
= 1;
2448 gdb_assert (new_lp
->last_resume_kind
== resume_continue
);
2449 if (debug_linux_nat
)
2450 fprintf_unfiltered (gdb_stdlog
,
2451 "LHEW: resuming new LWP %ld\n",
2452 GET_LWP (new_lp
->ptid
));
2453 if (linux_nat_prepare_to_resume
!= NULL
)
2454 linux_nat_prepare_to_resume (new_lp
);
2455 linux_ops
->to_resume (linux_ops
, pid_to_ptid (new_pid
),
2457 new_lp
->stopped
= 0;
2461 if (debug_linux_nat
)
2462 fprintf_unfiltered (gdb_stdlog
,
2463 "LHEW: resuming parent LWP %d\n", pid
);
2464 if (linux_nat_prepare_to_resume
!= NULL
)
2465 linux_nat_prepare_to_resume (lp
);
2466 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2475 if (event
== PTRACE_EVENT_EXEC
)
2477 if (debug_linux_nat
)
2478 fprintf_unfiltered (gdb_stdlog
,
2479 "LHEW: Got exec event from LWP %ld\n",
2480 GET_LWP (lp
->ptid
));
2482 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
2483 ourstatus
->value
.execd_pathname
2484 = xstrdup (linux_child_pid_to_exec_file (pid
));
2489 if (event
== PTRACE_EVENT_VFORK_DONE
)
2491 if (current_inferior ()->waiting_for_vfork_done
)
2493 if (debug_linux_nat
)
2494 fprintf_unfiltered (gdb_stdlog
,
2495 "LHEW: Got expected PTRACE_EVENT_"
2496 "VFORK_DONE from LWP %ld: stopping\n",
2497 GET_LWP (lp
->ptid
));
2499 ourstatus
->kind
= TARGET_WAITKIND_VFORK_DONE
;
2503 if (debug_linux_nat
)
2504 fprintf_unfiltered (gdb_stdlog
,
2505 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2506 "from LWP %ld: resuming\n",
2507 GET_LWP (lp
->ptid
));
2508 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2512 internal_error (__FILE__
, __LINE__
,
2513 _("unknown ptrace event %d"), event
);
2516 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2520 wait_lwp (struct lwp_info
*lp
)
2524 int thread_dead
= 0;
2527 gdb_assert (!lp
->stopped
);
2528 gdb_assert (lp
->status
== 0);
2530 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2531 block_child_signals (&prev_mask
);
2535 /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2536 was right and we should just call sigsuspend. */
2538 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, WNOHANG
);
2539 if (pid
== -1 && errno
== ECHILD
)
2540 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
| WNOHANG
);
2541 if (pid
== -1 && errno
== ECHILD
)
2543 /* The thread has previously exited. We need to delete it
2544 now because, for some vendor 2.4 kernels with NPTL
2545 support backported, there won't be an exit event unless
2546 it is the main thread. 2.6 kernels will report an exit
2547 event for each thread that exits, as expected. */
2549 if (debug_linux_nat
)
2550 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2551 target_pid_to_str (lp
->ptid
));
2556 /* Bugs 10970, 12702.
2557 Thread group leader may have exited in which case we'll lock up in
2558 waitpid if there are other threads, even if they are all zombies too.
2559 Basically, we're not supposed to use waitpid this way.
2560 __WCLONE is not applicable for the leader so we can't use that.
2561 LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2562 process; it gets ESRCH both for the zombie and for running processes.
2564 As a workaround, check if we're waiting for the thread group leader and
2565 if it's a zombie, and avoid calling waitpid if it is.
2567 This is racy, what if the tgl becomes a zombie right after we check?
2568 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2569 waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
2571 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
)
2572 && linux_proc_pid_is_zombie (GET_LWP (lp
->ptid
)))
2575 if (debug_linux_nat
)
2576 fprintf_unfiltered (gdb_stdlog
,
2577 "WL: Thread group leader %s vanished.\n",
2578 target_pid_to_str (lp
->ptid
));
2582 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2583 get invoked despite our caller had them intentionally blocked by
2584 block_child_signals. This is sensitive only to the loop of
2585 linux_nat_wait_1 and there if we get called my_waitpid gets called
2586 again before it gets to sigsuspend so we can safely let the handlers
2587 get executed here. */
2589 sigsuspend (&suspend_mask
);
2592 restore_child_signals_mask (&prev_mask
);
2596 gdb_assert (pid
== GET_LWP (lp
->ptid
));
2598 if (debug_linux_nat
)
2600 fprintf_unfiltered (gdb_stdlog
,
2601 "WL: waitpid %s received %s\n",
2602 target_pid_to_str (lp
->ptid
),
2603 status_to_str (status
));
2606 /* Check if the thread has exited. */
2607 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2610 if (debug_linux_nat
)
2611 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2612 target_pid_to_str (lp
->ptid
));
2622 gdb_assert (WIFSTOPPED (status
));
2624 /* Handle GNU/Linux's syscall SIGTRAPs. */
2625 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2627 /* No longer need the sysgood bit. The ptrace event ends up
2628 recorded in lp->waitstatus if we care for it. We can carry
2629 on handling the event like a regular SIGTRAP from here
2631 status
= W_STOPCODE (SIGTRAP
);
2632 if (linux_handle_syscall_trap (lp
, 1))
2633 return wait_lwp (lp
);
2636 /* Handle GNU/Linux's extended waitstatus for trace events. */
2637 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2639 if (debug_linux_nat
)
2640 fprintf_unfiltered (gdb_stdlog
,
2641 "WL: Handling extended status 0x%06x\n",
2643 if (linux_handle_extended_wait (lp
, status
, 1))
2644 return wait_lwp (lp
);
2650 /* Send a SIGSTOP to LP. */
2653 stop_callback (struct lwp_info
*lp
, void *data
)
2655 if (!lp
->stopped
&& !lp
->signalled
)
2659 if (debug_linux_nat
)
2661 fprintf_unfiltered (gdb_stdlog
,
2662 "SC: kill %s **<SIGSTOP>**\n",
2663 target_pid_to_str (lp
->ptid
));
2666 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
2667 if (debug_linux_nat
)
2669 fprintf_unfiltered (gdb_stdlog
,
2670 "SC: lwp kill %d %s\n",
2672 errno
? safe_strerror (errno
) : "ERRNO-OK");
2676 gdb_assert (lp
->status
== 0);
2682 /* Request a stop on LWP. */
2685 linux_stop_lwp (struct lwp_info
*lwp
)
2687 stop_callback (lwp
, NULL
);
2690 /* Return non-zero if LWP PID has a pending SIGINT. */
2693 linux_nat_has_pending_sigint (int pid
)
2695 sigset_t pending
, blocked
, ignored
;
2697 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2699 if (sigismember (&pending
, SIGINT
)
2700 && !sigismember (&ignored
, SIGINT
))
2706 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2709 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2711 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2712 flag to consume the next one. */
2713 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2714 && WSTOPSIG (lp
->status
) == SIGINT
)
2717 lp
->ignore_sigint
= 1;
2722 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2723 This function is called after we know the LWP has stopped; if the LWP
2724 stopped before the expected SIGINT was delivered, then it will never have
2725 arrived. Also, if the signal was delivered to a shared queue and consumed
2726 by a different thread, it will never be delivered to this LWP. */
2729 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2731 if (!lp
->ignore_sigint
)
2734 if (!linux_nat_has_pending_sigint (GET_LWP (lp
->ptid
)))
2736 if (debug_linux_nat
)
2737 fprintf_unfiltered (gdb_stdlog
,
2738 "MCIS: Clearing bogus flag for %s\n",
2739 target_pid_to_str (lp
->ptid
));
2740 lp
->ignore_sigint
= 0;
2744 /* Fetch the possible triggered data watchpoint info and store it in
2747 On some archs, like x86, that use debug registers to set
2748 watchpoints, it's possible that the way to know which watched
2749 address trapped, is to check the register that is used to select
2750 which address to watch. Problem is, between setting the watchpoint
2751 and reading back which data address trapped, the user may change
2752 the set of watchpoints, and, as a consequence, GDB changes the
2753 debug registers in the inferior. To avoid reading back a stale
2754 stopped-data-address when that happens, we cache in LP the fact
2755 that a watchpoint trapped, and the corresponding data address, as
2756 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2757 registers meanwhile, we have the cached data we can rely on. */
2760 save_sigtrap (struct lwp_info
*lp
)
2762 struct cleanup
*old_chain
;
2764 if (linux_ops
->to_stopped_by_watchpoint
== NULL
)
2766 lp
->stopped_by_watchpoint
= 0;
2770 old_chain
= save_inferior_ptid ();
2771 inferior_ptid
= lp
->ptid
;
2773 lp
->stopped_by_watchpoint
= linux_ops
->to_stopped_by_watchpoint ();
2775 if (lp
->stopped_by_watchpoint
)
2777 if (linux_ops
->to_stopped_data_address
!= NULL
)
2778 lp
->stopped_data_address_p
=
2779 linux_ops
->to_stopped_data_address (¤t_target
,
2780 &lp
->stopped_data_address
);
2782 lp
->stopped_data_address_p
= 0;
2785 do_cleanups (old_chain
);
2788 /* See save_sigtrap. */
2791 linux_nat_stopped_by_watchpoint (void)
2793 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2795 gdb_assert (lp
!= NULL
);
2797 return lp
->stopped_by_watchpoint
;
2801 linux_nat_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
2803 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2805 gdb_assert (lp
!= NULL
);
2807 *addr_p
= lp
->stopped_data_address
;
2809 return lp
->stopped_data_address_p
;
2812 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2815 sigtrap_is_event (int status
)
2817 return WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
;
2820 /* SIGTRAP-like events recognizer. */
2822 static int (*linux_nat_status_is_event
) (int status
) = sigtrap_is_event
;
2824 /* Check for SIGTRAP-like events in LP. */
2827 linux_nat_lp_status_is_event (struct lwp_info
*lp
)
2829 /* We check for lp->waitstatus in addition to lp->status, because we can
2830 have pending process exits recorded in lp->status
2831 and W_EXITCODE(0,0) == 0. We should probably have an additional
2832 lp->status_p flag. */
2834 return (lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
2835 && linux_nat_status_is_event (lp
->status
));
2838 /* Set alternative SIGTRAP-like events recognizer. If
2839 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2843 linux_nat_set_status_is_event (struct target_ops
*t
,
2844 int (*status_is_event
) (int status
))
2846 linux_nat_status_is_event
= status_is_event
;
2849 /* Wait until LP is stopped. */
2852 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2854 struct inferior
*inf
= find_inferior_pid (GET_PID (lp
->ptid
));
2856 /* If this is a vfork parent, bail out, it is not going to report
2857 any SIGSTOP until the vfork is done with. */
2858 if (inf
->vfork_child
!= NULL
)
2865 status
= wait_lwp (lp
);
2869 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2870 && WSTOPSIG (status
) == SIGINT
)
2872 lp
->ignore_sigint
= 0;
2875 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2876 if (debug_linux_nat
)
2877 fprintf_unfiltered (gdb_stdlog
,
2878 "PTRACE_CONT %s, 0, 0 (%s) "
2879 "(discarding SIGINT)\n",
2880 target_pid_to_str (lp
->ptid
),
2881 errno
? safe_strerror (errno
) : "OK");
2883 return stop_wait_callback (lp
, NULL
);
2886 maybe_clear_ignore_sigint (lp
);
2888 if (WSTOPSIG (status
) != SIGSTOP
)
2890 /* The thread was stopped with a signal other than SIGSTOP. */
2894 if (debug_linux_nat
)
2895 fprintf_unfiltered (gdb_stdlog
,
2896 "SWC: Pending event %s in %s\n",
2897 status_to_str ((int) status
),
2898 target_pid_to_str (lp
->ptid
));
2900 /* Save the sigtrap event. */
2901 lp
->status
= status
;
2902 gdb_assert (!lp
->stopped
);
2903 gdb_assert (lp
->signalled
);
2908 /* We caught the SIGSTOP that we intended to catch, so
2909 there's no SIGSTOP pending. */
2911 if (debug_linux_nat
)
2912 fprintf_unfiltered (gdb_stdlog
,
2913 "SWC: Delayed SIGSTOP caught for %s.\n",
2914 target_pid_to_str (lp
->ptid
));
2918 /* Reset SIGNALLED only after the stop_wait_callback call
2919 above as it does gdb_assert on SIGNALLED. */
2927 /* Return non-zero if LP has a wait status pending. */
2930 status_callback (struct lwp_info
*lp
, void *data
)
2932 /* Only report a pending wait status if we pretend that this has
2933 indeed been resumed. */
2937 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2939 /* A ptrace event, like PTRACE_FORK|VFORK|EXEC, syscall event,
2940 or a pending process exit. Note that `W_EXITCODE(0,0) ==
2941 0', so a clean process exit can not be stored pending in
2942 lp->status, it is indistinguishable from
2943 no-pending-status. */
2947 if (lp
->status
!= 0)
2953 /* Return non-zero if LP isn't stopped. */
2956 running_callback (struct lwp_info
*lp
, void *data
)
2958 return (!lp
->stopped
2959 || ((lp
->status
!= 0
2960 || lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2964 /* Count the LWP's that have had events. */
2967 count_events_callback (struct lwp_info
*lp
, void *data
)
2971 gdb_assert (count
!= NULL
);
2973 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2974 if (lp
->resumed
&& linux_nat_lp_status_is_event (lp
))
2980 /* Select the LWP (if any) that is currently being single-stepped. */
2983 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2985 if (lp
->last_resume_kind
== resume_step
2992 /* Select the Nth LWP that has had a SIGTRAP event. */
2995 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2997 int *selector
= data
;
2999 gdb_assert (selector
!= NULL
);
3001 /* Select only resumed LWPs that have a SIGTRAP event pending. */
3002 if (lp
->resumed
&& linux_nat_lp_status_is_event (lp
))
3003 if ((*selector
)-- == 0)
3010 cancel_breakpoint (struct lwp_info
*lp
)
3012 /* Arrange for a breakpoint to be hit again later. We don't keep
3013 the SIGTRAP status and don't forward the SIGTRAP signal to the
3014 LWP. We will handle the current event, eventually we will resume
3015 this LWP, and this breakpoint will trap again.
3017 If we do not do this, then we run the risk that the user will
3018 delete or disable the breakpoint, but the LWP will have already
3021 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3022 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3025 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
3026 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
3028 if (debug_linux_nat
)
3029 fprintf_unfiltered (gdb_stdlog
,
3030 "CB: Push back breakpoint for %s\n",
3031 target_pid_to_str (lp
->ptid
));
3033 /* Back up the PC if necessary. */
3034 if (gdbarch_decr_pc_after_break (gdbarch
))
3035 regcache_write_pc (regcache
, pc
);
3043 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
3045 struct lwp_info
*event_lp
= data
;
3047 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
3051 /* If a LWP other than the LWP that we're reporting an event for has
3052 hit a GDB breakpoint (as opposed to some random trap signal),
3053 then just arrange for it to hit it again later. We don't keep
3054 the SIGTRAP status and don't forward the SIGTRAP signal to the
3055 LWP. We will handle the current event, eventually we will resume
3056 all LWPs, and this one will get its breakpoint trap again.
3058 If we do not do this, then we run the risk that the user will
3059 delete or disable the breakpoint, but the LWP will have already
3062 if (linux_nat_lp_status_is_event (lp
)
3063 && cancel_breakpoint (lp
))
3064 /* Throw away the SIGTRAP. */
3070 /* Select one LWP out of those that have events pending. */
3073 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
3076 int random_selector
;
3077 struct lwp_info
*event_lp
;
3079 /* Record the wait status for the original LWP. */
3080 (*orig_lp
)->status
= *status
;
3082 /* Give preference to any LWP that is being single-stepped. */
3083 event_lp
= iterate_over_lwps (filter
,
3084 select_singlestep_lwp_callback
, NULL
);
3085 if (event_lp
!= NULL
)
3087 if (debug_linux_nat
)
3088 fprintf_unfiltered (gdb_stdlog
,
3089 "SEL: Select single-step %s\n",
3090 target_pid_to_str (event_lp
->ptid
));
3094 /* No single-stepping LWP. Select one at random, out of those
3095 which have had SIGTRAP events. */
3097 /* First see how many SIGTRAP events we have. */
3098 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
3100 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
3101 random_selector
= (int)
3102 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
3104 if (debug_linux_nat
&& num_events
> 1)
3105 fprintf_unfiltered (gdb_stdlog
,
3106 "SEL: Found %d SIGTRAP events, selecting #%d\n",
3107 num_events
, random_selector
);
3109 event_lp
= iterate_over_lwps (filter
,
3110 select_event_lwp_callback
,
3114 if (event_lp
!= NULL
)
3116 /* Switch the event LWP. */
3117 *orig_lp
= event_lp
;
3118 *status
= event_lp
->status
;
3121 /* Flush the wait status for the event LWP. */
3122 (*orig_lp
)->status
= 0;
3125 /* Return non-zero if LP has been resumed. */
3128 resumed_callback (struct lwp_info
*lp
, void *data
)
3133 /* Stop an active thread, verify it still exists, then resume it. If
3134 the thread ends up with a pending status, then it is not resumed,
3135 and *DATA (really a pointer to int), is set. */
3138 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
3140 int *new_pending_p
= data
;
3144 ptid_t ptid
= lp
->ptid
;
3146 stop_callback (lp
, NULL
);
3147 stop_wait_callback (lp
, NULL
);
3149 /* Resume if the lwp still exists, and the core wanted it
3151 lp
= find_lwp_pid (ptid
);
3154 if (lp
->last_resume_kind
== resume_stop
3157 /* The core wanted the LWP to stop. Even if it stopped
3158 cleanly (with SIGSTOP), leave the event pending. */
3159 if (debug_linux_nat
)
3160 fprintf_unfiltered (gdb_stdlog
,
3161 "SARC: core wanted LWP %ld stopped "
3162 "(leaving SIGSTOP pending)\n",
3163 GET_LWP (lp
->ptid
));
3164 lp
->status
= W_STOPCODE (SIGSTOP
);
3167 if (lp
->status
== 0)
3169 if (debug_linux_nat
)
3170 fprintf_unfiltered (gdb_stdlog
,
3171 "SARC: re-resuming LWP %ld\n",
3172 GET_LWP (lp
->ptid
));
3173 resume_lwp (lp
, lp
->step
, GDB_SIGNAL_0
);
3177 if (debug_linux_nat
)
3178 fprintf_unfiltered (gdb_stdlog
,
3179 "SARC: not re-resuming LWP %ld "
3181 GET_LWP (lp
->ptid
));
3190 /* Check if we should go on and pass this event to common code.
3191 Return the affected lwp if we are, or NULL otherwise. If we stop
3192 all lwps temporarily, we may end up with new pending events in some
3193 other lwp. In that case set *NEW_PENDING_P to true. */
3195 static struct lwp_info
*
3196 linux_nat_filter_event (int lwpid
, int status
, int *new_pending_p
)
3198 struct lwp_info
*lp
;
3202 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
3204 /* Check for stop events reported by a process we didn't already
3205 know about - anything not already in our LWP list.
3207 If we're expecting to receive stopped processes after
3208 fork, vfork, and clone events, then we'll just add the
3209 new one to our list and go back to waiting for the event
3210 to be reported - the stopped process might be returned
3211 from waitpid before or after the event is.
3213 But note the case of a non-leader thread exec'ing after the
3214 leader having exited, and gone from our lists. The non-leader
3215 thread changes its tid to the tgid. */
3217 if (WIFSTOPPED (status
) && lp
== NULL
3218 && (WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 == PTRACE_EVENT_EXEC
))
3220 /* A multi-thread exec after we had seen the leader exiting. */
3221 if (debug_linux_nat
)
3222 fprintf_unfiltered (gdb_stdlog
,
3223 "LLW: Re-adding thread group leader LWP %d.\n",
3226 lp
= add_lwp (BUILD_LWP (lwpid
, lwpid
));
3229 add_thread (lp
->ptid
);
3232 if (WIFSTOPPED (status
) && !lp
)
3234 add_to_pid_list (&stopped_pids
, lwpid
, status
);
3238 /* Make sure we don't report an event for the exit of an LWP not in
3239 our list, i.e. not part of the current process. This can happen
3240 if we detach from a program we originally forked and then it
3242 if (!WIFSTOPPED (status
) && !lp
)
3245 /* Handle GNU/Linux's syscall SIGTRAPs. */
3246 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
3248 /* No longer need the sysgood bit. The ptrace event ends up
3249 recorded in lp->waitstatus if we care for it. We can carry
3250 on handling the event like a regular SIGTRAP from here
3252 status
= W_STOPCODE (SIGTRAP
);
3253 if (linux_handle_syscall_trap (lp
, 0))
3257 /* Handle GNU/Linux's extended waitstatus for trace events. */
3258 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
3260 if (debug_linux_nat
)
3261 fprintf_unfiltered (gdb_stdlog
,
3262 "LLW: Handling extended status 0x%06x\n",
3264 if (linux_handle_extended_wait (lp
, status
, 0))
3268 if (linux_nat_status_is_event (status
))
3271 /* Check if the thread has exited. */
3272 if ((WIFEXITED (status
) || WIFSIGNALED (status
))
3273 && num_lwps (GET_PID (lp
->ptid
)) > 1)
3275 /* If this is the main thread, we must stop all threads and verify
3276 if they are still alive. This is because in the nptl thread model
3277 on Linux 2.4, there is no signal issued for exiting LWPs
3278 other than the main thread. We only get the main thread exit
3279 signal once all child threads have already exited. If we
3280 stop all the threads and use the stop_wait_callback to check
3281 if they have exited we can determine whether this signal
3282 should be ignored or whether it means the end of the debugged
3283 application, regardless of which threading model is being
3285 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
3288 iterate_over_lwps (pid_to_ptid (GET_PID (lp
->ptid
)),
3289 stop_and_resume_callback
, new_pending_p
);
3292 if (debug_linux_nat
)
3293 fprintf_unfiltered (gdb_stdlog
,
3294 "LLW: %s exited.\n",
3295 target_pid_to_str (lp
->ptid
));
3297 if (num_lwps (GET_PID (lp
->ptid
)) > 1)
3299 /* If there is at least one more LWP, then the exit signal
3300 was not the end of the debugged application and should be
3307 /* Check if the current LWP has previously exited. In the nptl
3308 thread model, LWPs other than the main thread do not issue
3309 signals when they exit so we must check whenever the thread has
3310 stopped. A similar check is made in stop_wait_callback(). */
3311 if (num_lwps (GET_PID (lp
->ptid
)) > 1 && !linux_thread_alive (lp
->ptid
))
3313 ptid_t ptid
= pid_to_ptid (GET_PID (lp
->ptid
));
3315 if (debug_linux_nat
)
3316 fprintf_unfiltered (gdb_stdlog
,
3317 "LLW: %s exited.\n",
3318 target_pid_to_str (lp
->ptid
));
3322 /* Make sure there is at least one thread running. */
3323 gdb_assert (iterate_over_lwps (ptid
, running_callback
, NULL
));
3325 /* Discard the event. */
3329 /* Make sure we don't report a SIGSTOP that we sent ourselves in
3330 an attempt to stop an LWP. */
3332 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
3334 if (debug_linux_nat
)
3335 fprintf_unfiltered (gdb_stdlog
,
3336 "LLW: Delayed SIGSTOP caught for %s.\n",
3337 target_pid_to_str (lp
->ptid
));
3341 if (lp
->last_resume_kind
!= resume_stop
)
3343 /* This is a delayed SIGSTOP. */
3345 registers_changed ();
3347 if (linux_nat_prepare_to_resume
!= NULL
)
3348 linux_nat_prepare_to_resume (lp
);
3349 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3350 lp
->step
, GDB_SIGNAL_0
);
3351 if (debug_linux_nat
)
3352 fprintf_unfiltered (gdb_stdlog
,
3353 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
3355 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3356 target_pid_to_str (lp
->ptid
));
3359 gdb_assert (lp
->resumed
);
3361 /* Discard the event. */
3366 /* Make sure we don't report a SIGINT that we have already displayed
3367 for another thread. */
3368 if (lp
->ignore_sigint
3369 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
3371 if (debug_linux_nat
)
3372 fprintf_unfiltered (gdb_stdlog
,
3373 "LLW: Delayed SIGINT caught for %s.\n",
3374 target_pid_to_str (lp
->ptid
));
3376 /* This is a delayed SIGINT. */
3377 lp
->ignore_sigint
= 0;
3379 registers_changed ();
3380 if (linux_nat_prepare_to_resume
!= NULL
)
3381 linux_nat_prepare_to_resume (lp
);
3382 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3383 lp
->step
, GDB_SIGNAL_0
);
3384 if (debug_linux_nat
)
3385 fprintf_unfiltered (gdb_stdlog
,
3386 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
3388 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3389 target_pid_to_str (lp
->ptid
));
3392 gdb_assert (lp
->resumed
);
3394 /* Discard the event. */
3398 /* An interesting event. */
3400 lp
->status
= status
;
3404 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3405 their exits until all other threads in the group have exited. */
3408 check_zombie_leaders (void)
3410 struct inferior
*inf
;
3414 struct lwp_info
*leader_lp
;
3419 leader_lp
= find_lwp_pid (pid_to_ptid (inf
->pid
));
3420 if (leader_lp
!= NULL
3421 /* Check if there are other threads in the group, as we may
3422 have raced with the inferior simply exiting. */
3423 && num_lwps (inf
->pid
) > 1
3424 && linux_proc_pid_is_zombie (inf
->pid
))
3426 if (debug_linux_nat
)
3427 fprintf_unfiltered (gdb_stdlog
,
3428 "CZL: Thread group leader %d zombie "
3429 "(it exited, or another thread execd).\n",
3432 /* A leader zombie can mean one of two things:
3434 - It exited, and there's an exit status pending
3435 available, or only the leader exited (not the whole
3436 program). In the latter case, we can't waitpid the
3437 leader's exit status until all other threads are gone.
3439 - There are 3 or more threads in the group, and a thread
3440 other than the leader exec'd. On an exec, the Linux
3441 kernel destroys all other threads (except the execing
3442 one) in the thread group, and resets the execing thread's
3443 tid to the tgid. No exit notification is sent for the
3444 execing thread -- from the ptracer's perspective, it
3445 appears as though the execing thread just vanishes.
3446 Until we reap all other threads except the leader and the
3447 execing thread, the leader will be zombie, and the
3448 execing thread will be in `D (disc sleep)'. As soon as
3449 all other threads are reaped, the execing thread changes
3450 it's tid to the tgid, and the previous (zombie) leader
3451 vanishes, giving place to the "new" leader. We could try
3452 distinguishing the exit and exec cases, by waiting once
3453 more, and seeing if something comes out, but it doesn't
3454 sound useful. The previous leader _does_ go away, and
3455 we'll re-add the new one once we see the exec event
3456 (which is just the same as what would happen if the
3457 previous leader did exit voluntarily before some other
3460 if (debug_linux_nat
)
3461 fprintf_unfiltered (gdb_stdlog
,
3462 "CZL: Thread group leader %d vanished.\n",
3464 exit_lwp (leader_lp
);
3470 linux_nat_wait_1 (struct target_ops
*ops
,
3471 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3474 static sigset_t prev_mask
;
3475 enum resume_kind last_resume_kind
;
3476 struct lwp_info
*lp
;
3479 if (debug_linux_nat
)
3480 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
3482 /* The first time we get here after starting a new inferior, we may
3483 not have added it to the LWP list yet - this is the earliest
3484 moment at which we know its PID. */
3485 if (ptid_is_pid (inferior_ptid
))
3487 /* Upgrade the main thread's ptid. */
3488 thread_change_ptid (inferior_ptid
,
3489 BUILD_LWP (GET_PID (inferior_ptid
),
3490 GET_PID (inferior_ptid
)));
3492 lp
= add_lwp (inferior_ptid
);
3496 /* Make sure SIGCHLD is blocked. */
3497 block_child_signals (&prev_mask
);
3503 /* First check if there is a LWP with a wait status pending. */
3504 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3506 /* Any LWP in the PTID group that's been resumed will do. */
3507 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3510 if (debug_linux_nat
&& lp
->status
)
3511 fprintf_unfiltered (gdb_stdlog
,
3512 "LLW: Using pending wait status %s for %s.\n",
3513 status_to_str (lp
->status
),
3514 target_pid_to_str (lp
->ptid
));
3517 else if (is_lwp (ptid
))
3519 if (debug_linux_nat
)
3520 fprintf_unfiltered (gdb_stdlog
,
3521 "LLW: Waiting for specific LWP %s.\n",
3522 target_pid_to_str (ptid
));
3524 /* We have a specific LWP to check. */
3525 lp
= find_lwp_pid (ptid
);
3528 if (debug_linux_nat
&& lp
->status
)
3529 fprintf_unfiltered (gdb_stdlog
,
3530 "LLW: Using pending wait status %s for %s.\n",
3531 status_to_str (lp
->status
),
3532 target_pid_to_str (lp
->ptid
));
3534 /* We check for lp->waitstatus in addition to lp->status,
3535 because we can have pending process exits recorded in
3536 lp->status and W_EXITCODE(0,0) == 0. We should probably have
3537 an additional lp->status_p flag. */
3538 if (lp
->status
== 0 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
3542 if (!target_can_async_p ())
3544 /* Causes SIGINT to be passed on to the attached process. */
3548 /* But if we don't find a pending event, we'll have to wait. */
3554 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3557 - If the thread group leader exits while other threads in the
3558 thread group still exist, waitpid(TGID, ...) hangs. That
3559 waitpid won't return an exit status until the other threads
3560 in the group are reapped.
3562 - When a non-leader thread execs, that thread just vanishes
3563 without reporting an exit (so we'd hang if we waited for it
3564 explicitly in that case). The exec event is reported to
3568 lwpid
= my_waitpid (-1, &status
, __WCLONE
| WNOHANG
);
3569 if (lwpid
== 0 || (lwpid
== -1 && errno
== ECHILD
))
3570 lwpid
= my_waitpid (-1, &status
, WNOHANG
);
3572 if (debug_linux_nat
)
3573 fprintf_unfiltered (gdb_stdlog
,
3574 "LNW: waitpid(-1, ...) returned %d, %s\n",
3575 lwpid
, errno
? safe_strerror (errno
) : "ERRNO-OK");
3579 /* If this is true, then we paused LWPs momentarily, and may
3580 now have pending events to handle. */
3583 if (debug_linux_nat
)
3585 fprintf_unfiltered (gdb_stdlog
,
3586 "LLW: waitpid %ld received %s\n",
3587 (long) lwpid
, status_to_str (status
));
3590 lp
= linux_nat_filter_event (lwpid
, status
, &new_pending
);
3592 /* STATUS is now no longer valid, use LP->STATUS instead. */
3595 if (lp
&& !ptid_match (lp
->ptid
, ptid
))
3597 gdb_assert (lp
->resumed
);
3599 if (debug_linux_nat
)
3601 "LWP %ld got an event %06x, leaving pending.\n",
3602 ptid_get_lwp (lp
->ptid
), lp
->status
);
3604 if (WIFSTOPPED (lp
->status
))
3606 if (WSTOPSIG (lp
->status
) != SIGSTOP
)
3608 /* Cancel breakpoint hits. The breakpoint may
3609 be removed before we fetch events from this
3610 process to report to the core. It is best
3611 not to assume the moribund breakpoints
3612 heuristic always handles these cases --- it
3613 could be too many events go through to the
3614 core before this one is handled. All-stop
3615 always cancels breakpoint hits in all
3618 && linux_nat_lp_status_is_event (lp
)
3619 && cancel_breakpoint (lp
))
3621 /* Throw away the SIGTRAP. */
3624 if (debug_linux_nat
)
3626 "LLW: LWP %ld hit a breakpoint while"
3627 " waiting for another process;"
3629 ptid_get_lwp (lp
->ptid
));
3639 else if (WIFEXITED (lp
->status
) || WIFSIGNALED (lp
->status
))
3641 if (debug_linux_nat
)
3643 "Process %ld exited while stopping LWPs\n",
3644 ptid_get_lwp (lp
->ptid
));
3646 /* This was the last lwp in the process. Since
3647 events are serialized to GDB core, and we can't
3648 report this one right now, but GDB core and the
3649 other target layers will want to be notified
3650 about the exit code/signal, leave the status
3651 pending for the next time we're able to report
3654 /* Prevent trying to stop this thread again. We'll
3655 never try to resume it because it has a pending
3659 /* Dead LWP's aren't expected to reported a pending
3663 /* Store the pending event in the waitstatus as
3664 well, because W_EXITCODE(0,0) == 0. */
3665 store_waitstatus (&lp
->waitstatus
, lp
->status
);
3674 /* Some LWP now has a pending event. Go all the way
3675 back to check it. */
3681 /* We got an event to report to the core. */
3685 /* Retry until nothing comes out of waitpid. A single
3686 SIGCHLD can indicate more than one child stopped. */
3690 /* Check for zombie thread group leaders. Those can't be reaped
3691 until all other threads in the thread group are. */
3692 check_zombie_leaders ();
3694 /* If there are no resumed children left, bail. We'd be stuck
3695 forever in the sigsuspend call below otherwise. */
3696 if (iterate_over_lwps (ptid
, resumed_callback
, NULL
) == NULL
)
3698 if (debug_linux_nat
)
3699 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (no resumed LWP)\n");
3701 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3703 if (!target_can_async_p ())
3704 clear_sigint_trap ();
3706 restore_child_signals_mask (&prev_mask
);
3707 return minus_one_ptid
;
3710 /* No interesting event to report to the core. */
3712 if (target_options
& TARGET_WNOHANG
)
3714 if (debug_linux_nat
)
3715 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
3717 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3718 restore_child_signals_mask (&prev_mask
);
3719 return minus_one_ptid
;
3722 /* We shouldn't end up here unless we want to try again. */
3723 gdb_assert (lp
== NULL
);
3725 /* Block until we get an event reported with SIGCHLD. */
3726 sigsuspend (&suspend_mask
);
3729 if (!target_can_async_p ())
3730 clear_sigint_trap ();
3734 status
= lp
->status
;
3737 /* Don't report signals that GDB isn't interested in, such as
3738 signals that are neither printed nor stopped upon. Stopping all
3739 threads can be a bit time-consuming so if we want decent
3740 performance with heavily multi-threaded programs, especially when
3741 they're using a high frequency timer, we'd better avoid it if we
3744 if (WIFSTOPPED (status
))
3746 enum gdb_signal signo
= gdb_signal_from_host (WSTOPSIG (status
));
3748 /* When using hardware single-step, we need to report every signal.
3749 Otherwise, signals in pass_mask may be short-circuited. */
3751 && WSTOPSIG (status
) && sigismember (&pass_mask
, WSTOPSIG (status
)))
3753 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3754 here? It is not clear we should. GDB may not expect
3755 other threads to run. On the other hand, not resuming
3756 newly attached threads may cause an unwanted delay in
3757 getting them running. */
3758 registers_changed ();
3759 if (linux_nat_prepare_to_resume
!= NULL
)
3760 linux_nat_prepare_to_resume (lp
);
3761 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3763 if (debug_linux_nat
)
3764 fprintf_unfiltered (gdb_stdlog
,
3765 "LLW: %s %s, %s (preempt 'handle')\n",
3767 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3768 target_pid_to_str (lp
->ptid
),
3769 (signo
!= GDB_SIGNAL_0
3770 ? strsignal (gdb_signal_to_host (signo
))
3778 /* Only do the below in all-stop, as we currently use SIGINT
3779 to implement target_stop (see linux_nat_stop) in
3781 if (signo
== GDB_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3783 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3784 forwarded to the entire process group, that is, all LWPs
3785 will receive it - unless they're using CLONE_THREAD to
3786 share signals. Since we only want to report it once, we
3787 mark it as ignored for all LWPs except this one. */
3788 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid
)),
3789 set_ignore_sigint
, NULL
);
3790 lp
->ignore_sigint
= 0;
3793 maybe_clear_ignore_sigint (lp
);
3797 /* This LWP is stopped now. */
3800 if (debug_linux_nat
)
3801 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
3802 status_to_str (status
), target_pid_to_str (lp
->ptid
));
3806 /* Now stop all other LWP's ... */
3807 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3809 /* ... and wait until all of them have reported back that
3810 they're no longer running. */
3811 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3813 /* If we're not waiting for a specific LWP, choose an event LWP
3814 from among those that have had events. Giving equal priority
3815 to all LWPs that have had events helps prevent
3817 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3818 select_event_lwp (ptid
, &lp
, &status
);
3820 /* Now that we've selected our final event LWP, cancel any
3821 breakpoints in other LWPs that have hit a GDB breakpoint.
3822 See the comment in cancel_breakpoints_callback to find out
3824 iterate_over_lwps (minus_one_ptid
, cancel_breakpoints_callback
, lp
);
3826 /* We'll need this to determine whether to report a SIGSTOP as
3827 TARGET_WAITKIND_0. Need to take a copy because
3828 resume_clear_callback clears it. */
3829 last_resume_kind
= lp
->last_resume_kind
;
3831 /* In all-stop, from the core's perspective, all LWPs are now
3832 stopped until a new resume action is sent over. */
3833 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
3838 last_resume_kind
= lp
->last_resume_kind
;
3839 resume_clear_callback (lp
, NULL
);
3842 if (linux_nat_status_is_event (status
))
3844 if (debug_linux_nat
)
3845 fprintf_unfiltered (gdb_stdlog
,
3846 "LLW: trap ptid is %s.\n",
3847 target_pid_to_str (lp
->ptid
));
3850 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3852 *ourstatus
= lp
->waitstatus
;
3853 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3856 store_waitstatus (ourstatus
, status
);
3858 if (debug_linux_nat
)
3859 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3861 restore_child_signals_mask (&prev_mask
);
3863 if (last_resume_kind
== resume_stop
3864 && ourstatus
->kind
== TARGET_WAITKIND_STOPPED
3865 && WSTOPSIG (status
) == SIGSTOP
)
3867 /* A thread that has been requested to stop by GDB with
3868 target_stop, and it stopped cleanly, so report as SIG0. The
3869 use of SIGSTOP is an implementation detail. */
3870 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3873 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
3874 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
3877 lp
->core
= linux_common_core_of_thread (lp
->ptid
);
3882 /* Resume LWPs that are currently stopped without any pending status
3883 to report, but are resumed from the core's perspective. */
3886 resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
)
3888 ptid_t
*wait_ptid_p
= data
;
3893 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
3895 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3896 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3897 CORE_ADDR pc
= regcache_read_pc (regcache
);
3899 gdb_assert (is_executing (lp
->ptid
));
3901 /* Don't bother if there's a breakpoint at PC that we'd hit
3902 immediately, and we're not waiting for this LWP. */
3903 if (!ptid_match (lp
->ptid
, *wait_ptid_p
))
3905 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
3909 if (debug_linux_nat
)
3910 fprintf_unfiltered (gdb_stdlog
,
3911 "RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
3912 target_pid_to_str (lp
->ptid
),
3913 paddress (gdbarch
, pc
),
3916 registers_changed ();
3917 if (linux_nat_prepare_to_resume
!= NULL
)
3918 linux_nat_prepare_to_resume (lp
);
3919 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3920 lp
->step
, GDB_SIGNAL_0
);
3922 lp
->stopped_by_watchpoint
= 0;
3929 linux_nat_wait (struct target_ops
*ops
,
3930 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3935 if (debug_linux_nat
)
3937 char *options_string
;
3939 options_string
= target_options_to_string (target_options
);
3940 fprintf_unfiltered (gdb_stdlog
,
3941 "linux_nat_wait: [%s], [%s]\n",
3942 target_pid_to_str (ptid
),
3944 xfree (options_string
);
3947 /* Flush the async file first. */
3948 if (target_can_async_p ())
3949 async_file_flush ();
3951 /* Resume LWPs that are currently stopped without any pending status
3952 to report, but are resumed from the core's perspective. LWPs get
3953 in this state if we find them stopping at a time we're not
3954 interested in reporting the event (target_wait on a
3955 specific_process, for example, see linux_nat_wait_1), and
3956 meanwhile the event became uninteresting. Don't bother resuming
3957 LWPs we're not going to wait for if they'd stop immediately. */
3959 iterate_over_lwps (minus_one_ptid
, resume_stopped_resumed_lwps
, &ptid
);
3961 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
, target_options
);
3963 /* If we requested any event, and something came out, assume there
3964 may be more. If we requested a specific lwp or process, also
3965 assume there may be more. */
3966 if (target_can_async_p ()
3967 && ((ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
3968 && ourstatus
->kind
!= TARGET_WAITKIND_NO_RESUMED
)
3969 || !ptid_equal (ptid
, minus_one_ptid
)))
3972 /* Get ready for the next event. */
3973 if (target_can_async_p ())
3974 target_async (inferior_event_handler
, 0);
3980 kill_callback (struct lwp_info
*lp
, void *data
)
3982 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
3985 kill (GET_LWP (lp
->ptid
), SIGKILL
);
3986 if (debug_linux_nat
)
3987 fprintf_unfiltered (gdb_stdlog
,
3988 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
3989 target_pid_to_str (lp
->ptid
),
3990 errno
? safe_strerror (errno
) : "OK");
3992 /* Some kernels ignore even SIGKILL for processes under ptrace. */
3995 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
3996 if (debug_linux_nat
)
3997 fprintf_unfiltered (gdb_stdlog
,
3998 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3999 target_pid_to_str (lp
->ptid
),
4000 errno
? safe_strerror (errno
) : "OK");
4006 kill_wait_callback (struct lwp_info
*lp
, void *data
)
4010 /* We must make sure that there are no pending events (delayed
4011 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
4012 program doesn't interfere with any following debugging session. */
4014 /* For cloned processes we must check both with __WCLONE and
4015 without, since the exit status of a cloned process isn't reported
4021 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
4022 if (pid
!= (pid_t
) -1)
4024 if (debug_linux_nat
)
4025 fprintf_unfiltered (gdb_stdlog
,
4026 "KWC: wait %s received unknown.\n",
4027 target_pid_to_str (lp
->ptid
));
4028 /* The Linux kernel sometimes fails to kill a thread
4029 completely after PTRACE_KILL; that goes from the stop
4030 point in do_fork out to the one in
4031 get_signal_to_deliever and waits again. So kill it
4033 kill_callback (lp
, NULL
);
4036 while (pid
== GET_LWP (lp
->ptid
));
4038 gdb_assert (pid
== -1 && errno
== ECHILD
);
4043 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
4044 if (pid
!= (pid_t
) -1)
4046 if (debug_linux_nat
)
4047 fprintf_unfiltered (gdb_stdlog
,
4048 "KWC: wait %s received unk.\n",
4049 target_pid_to_str (lp
->ptid
));
4050 /* See the call to kill_callback above. */
4051 kill_callback (lp
, NULL
);
4054 while (pid
== GET_LWP (lp
->ptid
));
4056 gdb_assert (pid
== -1 && errno
== ECHILD
);
4061 linux_nat_kill (struct target_ops
*ops
)
4063 struct target_waitstatus last
;
4067 /* If we're stopped while forking and we haven't followed yet,
4068 kill the other task. We need to do this first because the
4069 parent will be sleeping if this is a vfork. */
4071 get_last_target_status (&last_ptid
, &last
);
4073 if (last
.kind
== TARGET_WAITKIND_FORKED
4074 || last
.kind
== TARGET_WAITKIND_VFORKED
)
4076 ptrace (PT_KILL
, PIDGET (last
.value
.related_pid
), 0, 0);
4080 if (forks_exist_p ())
4081 linux_fork_killall ();
4084 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
4086 /* Stop all threads before killing them, since ptrace requires
4087 that the thread is stopped to sucessfully PTRACE_KILL. */
4088 iterate_over_lwps (ptid
, stop_callback
, NULL
);
4089 /* ... and wait until all of them have reported back that
4090 they're no longer running. */
4091 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
4093 /* Kill all LWP's ... */
4094 iterate_over_lwps (ptid
, kill_callback
, NULL
);
4096 /* ... and wait until we've flushed all events. */
4097 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
4100 target_mourn_inferior ();
4104 linux_nat_mourn_inferior (struct target_ops
*ops
)
4106 purge_lwp_list (ptid_get_pid (inferior_ptid
));
4108 if (! forks_exist_p ())
4109 /* Normal case, no other forks available. */
4110 linux_ops
->to_mourn_inferior (ops
);
4112 /* Multi-fork case. The current inferior_ptid has exited, but
4113 there are other viable forks to debug. Delete the exiting
4114 one and context-switch to the first available. */
4115 linux_fork_mourn_inferior ();
4118 /* Convert a native/host siginfo object, into/from the siginfo in the
4119 layout of the inferiors' architecture. */
4122 siginfo_fixup (siginfo_t
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
4126 if (linux_nat_siginfo_fixup
!= NULL
)
4127 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
4129 /* If there was no callback, or the callback didn't do anything,
4130 then just do a straight memcpy. */
4134 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
4136 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
4141 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
4142 const char *annex
, gdb_byte
*readbuf
,
4143 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4147 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
4149 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
4150 gdb_assert (readbuf
|| writebuf
);
4152 pid
= GET_LWP (inferior_ptid
);
4154 pid
= GET_PID (inferior_ptid
);
4156 if (offset
> sizeof (siginfo
))
4160 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
4164 /* When GDB is built as a 64-bit application, ptrace writes into
4165 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
4166 inferior with a 64-bit GDB should look the same as debugging it
4167 with a 32-bit GDB, we need to convert it. GDB core always sees
4168 the converted layout, so any read/write will have to be done
4170 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
4172 if (offset
+ len
> sizeof (siginfo
))
4173 len
= sizeof (siginfo
) - offset
;
4175 if (readbuf
!= NULL
)
4176 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
4179 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
4181 /* Convert back to ptrace layout before flushing it out. */
4182 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
4185 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
4194 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4195 const char *annex
, gdb_byte
*readbuf
,
4196 const gdb_byte
*writebuf
,
4197 ULONGEST offset
, LONGEST len
)
4199 struct cleanup
*old_chain
;
4202 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
4203 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
4206 /* The target is connected but no live inferior is selected. Pass
4207 this request down to a lower stratum (e.g., the executable
4209 if (object
== TARGET_OBJECT_MEMORY
&& ptid_equal (inferior_ptid
, null_ptid
))
4212 old_chain
= save_inferior_ptid ();
4214 if (is_lwp (inferior_ptid
))
4215 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
4217 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4220 do_cleanups (old_chain
);
4225 linux_thread_alive (ptid_t ptid
)
4229 gdb_assert (is_lwp (ptid
));
4231 /* Send signal 0 instead of anything ptrace, because ptracing a
4232 running thread errors out claiming that the thread doesn't
4234 err
= kill_lwp (GET_LWP (ptid
), 0);
4236 if (debug_linux_nat
)
4237 fprintf_unfiltered (gdb_stdlog
,
4238 "LLTA: KILL(SIG0) %s (%s)\n",
4239 target_pid_to_str (ptid
),
4240 err
? safe_strerror (tmp_errno
) : "OK");
4249 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
4251 return linux_thread_alive (ptid
);
4255 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
4257 static char buf
[64];
4260 && (GET_PID (ptid
) != GET_LWP (ptid
)
4261 || num_lwps (GET_PID (ptid
)) > 1))
4263 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
4267 return normal_pid_to_str (ptid
);
4271 linux_nat_thread_name (struct thread_info
*thr
)
4273 int pid
= ptid_get_pid (thr
->ptid
);
4274 long lwp
= ptid_get_lwp (thr
->ptid
);
4275 #define FORMAT "/proc/%d/task/%ld/comm"
4276 char buf
[sizeof (FORMAT
) + 30];
4278 char *result
= NULL
;
4280 snprintf (buf
, sizeof (buf
), FORMAT
, pid
, lwp
);
4281 comm_file
= fopen (buf
, "r");
4284 /* Not exported by the kernel, so we define it here. */
4286 static char line
[COMM_LEN
+ 1];
4288 if (fgets (line
, sizeof (line
), comm_file
))
4290 char *nl
= strchr (line
, '\n');
4307 /* Accepts an integer PID; Returns a string representing a file that
4308 can be opened to get the symbols for the child process. */
4311 linux_child_pid_to_exec_file (int pid
)
4313 char *name1
, *name2
;
4315 name1
= xmalloc (MAXPATHLEN
);
4316 name2
= xmalloc (MAXPATHLEN
);
4317 make_cleanup (xfree
, name1
);
4318 make_cleanup (xfree
, name2
);
4319 memset (name2
, 0, MAXPATHLEN
);
4321 sprintf (name1
, "/proc/%d/exe", pid
);
4322 if (readlink (name1
, name2
, MAXPATHLEN
- 1) > 0)
4328 /* Records the thread's register state for the corefile note
4332 linux_nat_collect_thread_registers (const struct regcache
*regcache
,
4333 ptid_t ptid
, bfd
*obfd
,
4334 char *note_data
, int *note_size
,
4335 enum gdb_signal stop_signal
)
4337 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
4338 const struct regset
*regset
;
4340 gdb_gregset_t gregs
;
4341 gdb_fpregset_t fpregs
;
4343 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
4346 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
4348 != NULL
&& regset
->collect_regset
!= NULL
)
4349 regset
->collect_regset (regset
, regcache
, -1, &gregs
, sizeof (gregs
));
4351 fill_gregset (regcache
, &gregs
, -1);
4353 note_data
= (char *) elfcore_write_prstatus
4354 (obfd
, note_data
, note_size
, ptid_get_lwp (ptid
),
4355 gdb_signal_to_host (stop_signal
), &gregs
);
4358 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
4360 != NULL
&& regset
->collect_regset
!= NULL
)
4361 regset
->collect_regset (regset
, regcache
, -1, &fpregs
, sizeof (fpregs
));
4363 fill_fpregset (regcache
, &fpregs
, -1);
4365 note_data
= (char *) elfcore_write_prfpreg (obfd
, note_data
, note_size
,
4366 &fpregs
, sizeof (fpregs
));
4371 /* Fills the "to_make_corefile_note" target vector. Builds the note
4372 section for a corefile, and returns it in a malloc buffer. */
4375 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
4377 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
4378 converted to gdbarch_core_regset_sections, this function can go away. */
4379 return linux_make_corefile_notes (target_gdbarch (), obfd
, note_size
,
4380 linux_nat_collect_thread_registers
);
4383 /* Implement the to_xfer_partial interface for memory reads using the /proc
4384 filesystem. Because we can use a single read() call for /proc, this
4385 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4386 but it doesn't support writes. */
4389 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4390 const char *annex
, gdb_byte
*readbuf
,
4391 const gdb_byte
*writebuf
,
4392 ULONGEST offset
, LONGEST len
)
4398 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
4401 /* Don't bother for one word. */
4402 if (len
< 3 * sizeof (long))
4405 /* We could keep this file open and cache it - possibly one per
4406 thread. That requires some juggling, but is even faster. */
4407 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
4408 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4412 /* If pread64 is available, use it. It's faster if the kernel
4413 supports it (only one syscall), and it's 64-bit safe even on
4414 32-bit platforms (for instance, SPARC debugging a SPARC64
4417 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
4419 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
4430 /* Enumerate spufs IDs for process PID. */
4432 spu_enumerate_spu_ids (int pid
, gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
4434 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
4436 LONGEST written
= 0;
4439 struct dirent
*entry
;
4441 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
4442 dir
= opendir (path
);
4447 while ((entry
= readdir (dir
)) != NULL
)
4453 fd
= atoi (entry
->d_name
);
4457 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
4458 if (stat (path
, &st
) != 0)
4460 if (!S_ISDIR (st
.st_mode
))
4463 if (statfs (path
, &stfs
) != 0)
4465 if (stfs
.f_type
!= SPUFS_MAGIC
)
4468 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
4470 store_unsigned_integer (buf
+ pos
- offset
, 4, byte_order
, fd
);
4480 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4481 object type, using the /proc file system. */
4483 linux_proc_xfer_spu (struct target_ops
*ops
, enum target_object object
,
4484 const char *annex
, gdb_byte
*readbuf
,
4485 const gdb_byte
*writebuf
,
4486 ULONGEST offset
, LONGEST len
)
4491 int pid
= PIDGET (inferior_ptid
);
4498 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
4501 xsnprintf (buf
, sizeof buf
, "/proc/%d/fd/%s", pid
, annex
);
4502 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
4507 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4514 ret
= write (fd
, writebuf
, (size_t) len
);
4516 ret
= read (fd
, readbuf
, (size_t) len
);
4523 /* Parse LINE as a signal set and add its set bits to SIGS. */
4526 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4528 int len
= strlen (line
) - 1;
4532 if (line
[len
] != '\n')
4533 error (_("Could not parse signal set: %s"), line
);
4541 if (*p
>= '0' && *p
<= '9')
4543 else if (*p
>= 'a' && *p
<= 'f')
4544 digit
= *p
- 'a' + 10;
4546 error (_("Could not parse signal set: %s"), line
);
4551 sigaddset (sigs
, signum
+ 1);
4553 sigaddset (sigs
, signum
+ 2);
4555 sigaddset (sigs
, signum
+ 3);
4557 sigaddset (sigs
, signum
+ 4);
4563 /* Find process PID's pending signals from /proc/pid/status and set
4567 linux_proc_pending_signals (int pid
, sigset_t
*pending
,
4568 sigset_t
*blocked
, sigset_t
*ignored
)
4571 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
4572 struct cleanup
*cleanup
;
4574 sigemptyset (pending
);
4575 sigemptyset (blocked
);
4576 sigemptyset (ignored
);
4577 sprintf (fname
, "/proc/%d/status", pid
);
4578 procfile
= fopen (fname
, "r");
4579 if (procfile
== NULL
)
4580 error (_("Could not open %s"), fname
);
4581 cleanup
= make_cleanup_fclose (procfile
);
4583 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
4585 /* Normal queued signals are on the SigPnd line in the status
4586 file. However, 2.6 kernels also have a "shared" pending
4587 queue for delivering signals to a thread group, so check for
4590 Unfortunately some Red Hat kernels include the shared pending
4591 queue but not the ShdPnd status field. */
4593 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
4594 add_line_to_sigset (buffer
+ 8, pending
);
4595 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
4596 add_line_to_sigset (buffer
+ 8, pending
);
4597 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
4598 add_line_to_sigset (buffer
+ 8, blocked
);
4599 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
4600 add_line_to_sigset (buffer
+ 8, ignored
);
4603 do_cleanups (cleanup
);
4607 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4608 const char *annex
, gdb_byte
*readbuf
,
4609 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4611 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4613 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4617 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4618 const char *annex
, gdb_byte
*readbuf
,
4619 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4623 if (object
== TARGET_OBJECT_AUXV
)
4624 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4627 if (object
== TARGET_OBJECT_OSDATA
)
4628 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4631 if (object
== TARGET_OBJECT_SPU
)
4632 return linux_proc_xfer_spu (ops
, object
, annex
, readbuf
, writebuf
,
4635 /* GDB calculates all the addresses in possibly larget width of the address.
4636 Address width needs to be masked before its final use - either by
4637 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4639 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4641 if (object
== TARGET_OBJECT_MEMORY
)
4643 int addr_bit
= gdbarch_addr_bit (target_gdbarch ());
4645 if (addr_bit
< (sizeof (ULONGEST
) * HOST_CHAR_BIT
))
4646 offset
&= ((ULONGEST
) 1 << addr_bit
) - 1;
4649 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4654 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4659 cleanup_target_stop (void *arg
)
4661 ptid_t
*ptid
= (ptid_t
*) arg
;
4663 gdb_assert (arg
!= NULL
);
4666 target_resume (*ptid
, 0, GDB_SIGNAL_0
);
4669 static VEC(static_tracepoint_marker_p
) *
4670 linux_child_static_tracepoint_markers_by_strid (const char *strid
)
4672 char s
[IPA_CMD_BUF_SIZE
];
4673 struct cleanup
*old_chain
;
4674 int pid
= ptid_get_pid (inferior_ptid
);
4675 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
4676 struct static_tracepoint_marker
*marker
= NULL
;
4678 ptid_t ptid
= ptid_build (pid
, 0, 0);
4683 memcpy (s
, "qTfSTM", sizeof ("qTfSTM"));
4684 s
[sizeof ("qTfSTM")] = 0;
4686 agent_run_command (pid
, s
, strlen (s
) + 1);
4688 old_chain
= make_cleanup (free_current_marker
, &marker
);
4689 make_cleanup (cleanup_target_stop
, &ptid
);
4694 marker
= XCNEW (struct static_tracepoint_marker
);
4698 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
4700 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
4702 VEC_safe_push (static_tracepoint_marker_p
,
4708 release_static_tracepoint_marker (marker
);
4709 memset (marker
, 0, sizeof (*marker
));
4712 while (*p
++ == ','); /* comma-separated list */
4714 memcpy (s
, "qTsSTM", sizeof ("qTsSTM"));
4715 s
[sizeof ("qTsSTM")] = 0;
4716 agent_run_command (pid
, s
, strlen (s
) + 1);
4720 do_cleanups (old_chain
);
4725 /* Create a prototype generic GNU/Linux target. The client can override
4726 it with local methods. */
4729 linux_target_install_ops (struct target_ops
*t
)
4731 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4732 t
->to_remove_fork_catchpoint
= linux_child_remove_fork_catchpoint
;
4733 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4734 t
->to_remove_vfork_catchpoint
= linux_child_remove_vfork_catchpoint
;
4735 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4736 t
->to_remove_exec_catchpoint
= linux_child_remove_exec_catchpoint
;
4737 t
->to_set_syscall_catchpoint
= linux_child_set_syscall_catchpoint
;
4738 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4739 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4740 t
->to_post_attach
= linux_child_post_attach
;
4741 t
->to_follow_fork
= linux_child_follow_fork
;
4742 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
4744 super_xfer_partial
= t
->to_xfer_partial
;
4745 t
->to_xfer_partial
= linux_xfer_partial
;
4747 t
->to_static_tracepoint_markers_by_strid
4748 = linux_child_static_tracepoint_markers_by_strid
;
4754 struct target_ops
*t
;
4756 t
= inf_ptrace_target ();
4757 linux_target_install_ops (t
);
4763 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4765 struct target_ops
*t
;
4767 t
= inf_ptrace_trad_target (register_u_offset
);
4768 linux_target_install_ops (t
);
4773 /* target_is_async_p implementation. */
4776 linux_nat_is_async_p (void)
4778 /* NOTE: palves 2008-03-21: We're only async when the user requests
4779 it explicitly with the "set target-async" command.
4780 Someday, linux will always be async. */
4781 return target_async_permitted
;
4784 /* target_can_async_p implementation. */
4787 linux_nat_can_async_p (void)
4789 /* NOTE: palves 2008-03-21: We're only async when the user requests
4790 it explicitly with the "set target-async" command.
4791 Someday, linux will always be async. */
4792 return target_async_permitted
;
4796 linux_nat_supports_non_stop (void)
4801 /* True if we want to support multi-process. To be removed when GDB
4802 supports multi-exec. */
4804 int linux_multi_process
= 1;
4807 linux_nat_supports_multi_process (void)
4809 return linux_multi_process
;
4813 linux_nat_supports_disable_randomization (void)
4815 #ifdef HAVE_PERSONALITY
4822 static int async_terminal_is_ours
= 1;
4824 /* target_terminal_inferior implementation. */
4827 linux_nat_terminal_inferior (void)
4829 if (!target_is_async_p ())
4831 /* Async mode is disabled. */
4832 terminal_inferior ();
4836 terminal_inferior ();
4838 /* Calls to target_terminal_*() are meant to be idempotent. */
4839 if (!async_terminal_is_ours
)
4842 delete_file_handler (input_fd
);
4843 async_terminal_is_ours
= 0;
4847 /* target_terminal_ours implementation. */
4850 linux_nat_terminal_ours (void)
4852 if (!target_is_async_p ())
4854 /* Async mode is disabled. */
4859 /* GDB should never give the terminal to the inferior if the
4860 inferior is running in the background (run&, continue&, etc.),
4861 but claiming it sure should. */
4864 if (async_terminal_is_ours
)
4867 clear_sigint_trap ();
4868 add_file_handler (input_fd
, stdin_event_handler
, 0);
4869 async_terminal_is_ours
= 1;
4872 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4874 static void *async_client_context
;
4876 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4877 so we notice when any child changes state, and notify the
4878 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4879 above to wait for the arrival of a SIGCHLD. */
4882 sigchld_handler (int signo
)
4884 int old_errno
= errno
;
4886 if (debug_linux_nat
)
4887 ui_file_write_async_safe (gdb_stdlog
,
4888 "sigchld\n", sizeof ("sigchld\n") - 1);
4890 if (signo
== SIGCHLD
4891 && linux_nat_event_pipe
[0] != -1)
4892 async_file_mark (); /* Let the event loop know that there are
4893 events to handle. */
4898 /* Callback registered with the target events file descriptor. */
4901 handle_target_event (int error
, gdb_client_data client_data
)
4903 (*async_client_callback
) (INF_REG_EVENT
, async_client_context
);
4906 /* Create/destroy the target events pipe. Returns previous state. */
4909 linux_async_pipe (int enable
)
4911 int previous
= (linux_nat_event_pipe
[0] != -1);
4913 if (previous
!= enable
)
4917 block_child_signals (&prev_mask
);
4921 if (pipe (linux_nat_event_pipe
) == -1)
4922 internal_error (__FILE__
, __LINE__
,
4923 "creating event pipe failed.");
4925 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4926 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4930 close (linux_nat_event_pipe
[0]);
4931 close (linux_nat_event_pipe
[1]);
4932 linux_nat_event_pipe
[0] = -1;
4933 linux_nat_event_pipe
[1] = -1;
4936 restore_child_signals_mask (&prev_mask
);
4942 /* target_async implementation. */
4945 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
4946 void *context
), void *context
)
4948 if (callback
!= NULL
)
4950 async_client_callback
= callback
;
4951 async_client_context
= context
;
4952 if (!linux_async_pipe (1))
4954 add_file_handler (linux_nat_event_pipe
[0],
4955 handle_target_event
, NULL
);
4956 /* There may be pending events to handle. Tell the event loop
4963 async_client_callback
= callback
;
4964 async_client_context
= context
;
4965 delete_file_handler (linux_nat_event_pipe
[0]);
4966 linux_async_pipe (0);
4971 /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
4975 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4979 ptid_t ptid
= lwp
->ptid
;
4981 if (debug_linux_nat
)
4982 fprintf_unfiltered (gdb_stdlog
,
4983 "LNSL: running -> suspending %s\n",
4984 target_pid_to_str (lwp
->ptid
));
4987 if (lwp
->last_resume_kind
== resume_stop
)
4989 if (debug_linux_nat
)
4990 fprintf_unfiltered (gdb_stdlog
,
4991 "linux-nat: already stopping LWP %ld at "
4993 ptid_get_lwp (lwp
->ptid
));
4997 stop_callback (lwp
, NULL
);
4998 lwp
->last_resume_kind
= resume_stop
;
5002 /* Already known to be stopped; do nothing. */
5004 if (debug_linux_nat
)
5006 if (find_thread_ptid (lwp
->ptid
)->stop_requested
)
5007 fprintf_unfiltered (gdb_stdlog
,
5008 "LNSL: already stopped/stop_requested %s\n",
5009 target_pid_to_str (lwp
->ptid
));
5011 fprintf_unfiltered (gdb_stdlog
,
5012 "LNSL: already stopped/no "
5013 "stop_requested yet %s\n",
5014 target_pid_to_str (lwp
->ptid
));
5021 linux_nat_stop (ptid_t ptid
)
5024 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
5026 linux_ops
->to_stop (ptid
);
5030 linux_nat_close (int quitting
)
5032 /* Unregister from the event loop. */
5033 if (linux_nat_is_async_p ())
5034 linux_nat_async (NULL
, 0);
5036 if (linux_ops
->to_close
)
5037 linux_ops
->to_close (quitting
);
5040 /* When requests are passed down from the linux-nat layer to the
5041 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
5042 used. The address space pointer is stored in the inferior object,
5043 but the common code that is passed such ptid can't tell whether
5044 lwpid is a "main" process id or not (it assumes so). We reverse
5045 look up the "main" process id from the lwp here. */
5047 static struct address_space
*
5048 linux_nat_thread_address_space (struct target_ops
*t
, ptid_t ptid
)
5050 struct lwp_info
*lwp
;
5051 struct inferior
*inf
;
5054 pid
= GET_LWP (ptid
);
5055 if (GET_LWP (ptid
) == 0)
5057 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
5059 lwp
= find_lwp_pid (ptid
);
5060 pid
= GET_PID (lwp
->ptid
);
5064 /* A (pid,lwpid,0) ptid. */
5065 pid
= GET_PID (ptid
);
5068 inf
= find_inferior_pid (pid
);
5069 gdb_assert (inf
!= NULL
);
5073 /* Return the cached value of the processor core for thread PTID. */
5076 linux_nat_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
5078 struct lwp_info
*info
= find_lwp_pid (ptid
);
5086 linux_nat_add_target (struct target_ops
*t
)
5088 /* Save the provided single-threaded target. We save this in a separate
5089 variable because another target we've inherited from (e.g. inf-ptrace)
5090 may have saved a pointer to T; we want to use it for the final
5091 process stratum target. */
5092 linux_ops_saved
= *t
;
5093 linux_ops
= &linux_ops_saved
;
5095 /* Override some methods for multithreading. */
5096 t
->to_create_inferior
= linux_nat_create_inferior
;
5097 t
->to_attach
= linux_nat_attach
;
5098 t
->to_detach
= linux_nat_detach
;
5099 t
->to_resume
= linux_nat_resume
;
5100 t
->to_wait
= linux_nat_wait
;
5101 t
->to_pass_signals
= linux_nat_pass_signals
;
5102 t
->to_xfer_partial
= linux_nat_xfer_partial
;
5103 t
->to_kill
= linux_nat_kill
;
5104 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
5105 t
->to_thread_alive
= linux_nat_thread_alive
;
5106 t
->to_pid_to_str
= linux_nat_pid_to_str
;
5107 t
->to_thread_name
= linux_nat_thread_name
;
5108 t
->to_has_thread_control
= tc_schedlock
;
5109 t
->to_thread_address_space
= linux_nat_thread_address_space
;
5110 t
->to_stopped_by_watchpoint
= linux_nat_stopped_by_watchpoint
;
5111 t
->to_stopped_data_address
= linux_nat_stopped_data_address
;
5113 t
->to_can_async_p
= linux_nat_can_async_p
;
5114 t
->to_is_async_p
= linux_nat_is_async_p
;
5115 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
5116 t
->to_async
= linux_nat_async
;
5117 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
5118 t
->to_terminal_ours
= linux_nat_terminal_ours
;
5119 t
->to_close
= linux_nat_close
;
5121 /* Methods for non-stop support. */
5122 t
->to_stop
= linux_nat_stop
;
5124 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
5126 t
->to_supports_disable_randomization
5127 = linux_nat_supports_disable_randomization
;
5129 t
->to_core_of_thread
= linux_nat_core_of_thread
;
5131 /* We don't change the stratum; this target will sit at
5132 process_stratum and thread_db will set at thread_stratum. This
5133 is a little strange, since this is a multi-threaded-capable
5134 target, but we want to be on the stack below thread_db, and we
5135 also want to be used for single-threaded processes. */
5140 /* Register a method to call whenever a new thread is attached. */
5142 linux_nat_set_new_thread (struct target_ops
*t
,
5143 void (*new_thread
) (struct lwp_info
*))
5145 /* Save the pointer. We only support a single registered instance
5146 of the GNU/Linux native target, so we do not need to map this to
5148 linux_nat_new_thread
= new_thread
;
5151 /* Register a method that converts a siginfo object between the layout
5152 that ptrace returns, and the layout in the architecture of the
5155 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
5156 int (*siginfo_fixup
) (siginfo_t
*,
5160 /* Save the pointer. */
5161 linux_nat_siginfo_fixup
= siginfo_fixup
;
5164 /* Register a method to call prior to resuming a thread. */
5167 linux_nat_set_prepare_to_resume (struct target_ops
*t
,
5168 void (*prepare_to_resume
) (struct lwp_info
*))
5170 /* Save the pointer. */
5171 linux_nat_prepare_to_resume
= prepare_to_resume
;
5174 /* See linux-nat.h. */
5177 linux_nat_get_siginfo (ptid_t ptid
, siginfo_t
*siginfo
)
5181 pid
= GET_LWP (ptid
);
5183 pid
= GET_PID (ptid
);
5186 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, siginfo
);
5189 memset (siginfo
, 0, sizeof (*siginfo
));
5195 /* Provide a prototype to silence -Wmissing-prototypes. */
5196 extern initialize_file_ftype _initialize_linux_nat
;
5199 _initialize_linux_nat (void)
5201 add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance
,
5202 &debug_linux_nat
, _("\
5203 Set debugging of GNU/Linux lwp module."), _("\
5204 Show debugging of GNU/Linux lwp module."), _("\
5205 Enables printf debugging output."),
5207 show_debug_linux_nat
,
5208 &setdebuglist
, &showdebuglist
);
5210 /* Save this mask as the default. */
5211 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
5213 /* Install a SIGCHLD handler. */
5214 sigchld_action
.sa_handler
= sigchld_handler
;
5215 sigemptyset (&sigchld_action
.sa_mask
);
5216 sigchld_action
.sa_flags
= SA_RESTART
;
5218 /* Make it the default. */
5219 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
5221 /* Make sure we don't block SIGCHLD during a sigsuspend. */
5222 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
5223 sigdelset (&suspend_mask
, SIGCHLD
);
5225 sigemptyset (&blocked_mask
);
5229 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
5230 the GNU/Linux Threads library and therefore doesn't really belong
5233 /* Read variable NAME in the target and return its value if found.
5234 Otherwise return zero. It is assumed that the type of the variable
5238 get_signo (const char *name
)
5240 struct minimal_symbol
*ms
;
5243 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
5247 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
5248 sizeof (signo
)) != 0)
5254 /* Return the set of signals used by the threads library in *SET. */
5257 lin_thread_get_thread_signals (sigset_t
*set
)
5259 struct sigaction action
;
5260 int restart
, cancel
;
5262 sigemptyset (&blocked_mask
);
5265 restart
= get_signo ("__pthread_sig_restart");
5266 cancel
= get_signo ("__pthread_sig_cancel");
5268 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5269 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5270 not provide any way for the debugger to query the signal numbers -
5271 fortunately they don't change! */
5274 restart
= __SIGRTMIN
;
5277 cancel
= __SIGRTMIN
+ 1;
5279 sigaddset (set
, restart
);
5280 sigaddset (set
, cancel
);
5282 /* The GNU/Linux Threads library makes terminating threads send a
5283 special "cancel" signal instead of SIGCHLD. Make sure we catch
5284 those (to prevent them from terminating GDB itself, which is
5285 likely to be their default action) and treat them the same way as
5288 action
.sa_handler
= sigchld_handler
;
5289 sigemptyset (&action
.sa_mask
);
5290 action
.sa_flags
= SA_RESTART
;
5291 sigaction (cancel
, &action
, NULL
);
5293 /* We block the "cancel" signal throughout this code ... */
5294 sigaddset (&blocked_mask
, cancel
);
5295 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
5297 /* ... except during a sigsuspend. */
5298 sigdelset (&suspend_mask
, cancel
);