1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
25 #include "gdb_assert.h"
26 #ifdef HAVE_TKILL_SYSCALL
28 #include <sys/syscall.h>
30 #include <sys/ptrace.h>
31 #include "linux-nat.h"
32 #include "linux-ptrace.h"
33 #include "linux-procfs.h"
34 #include "linux-fork.h"
35 #include "gdbthread.h"
39 #include "inf-ptrace.h"
41 #include <sys/param.h> /* for MAXPATHLEN */
42 #include <sys/procfs.h> /* for elf_gregset etc. */
43 #include "elf-bfd.h" /* for elfcore_write_* */
44 #include "gregset.h" /* for gregset */
45 #include "gdbcore.h" /* for get_exec_file */
46 #include <ctype.h> /* for isdigit */
47 #include "gdbthread.h" /* for struct thread_info etc. */
48 #include "gdb_stat.h" /* for struct stat */
49 #include <fcntl.h> /* for O_RDONLY */
51 #include "event-loop.h"
52 #include "event-top.h"
54 #include <sys/types.h>
55 #include "gdb_dirent.h"
56 #include "xml-support.h"
60 #include "linux-osdata.h"
61 #include "linux-tdep.h"
64 #define SPUFS_MAGIC 0x23c9b64e
67 #ifdef HAVE_PERSONALITY
68 # include <sys/personality.h>
69 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
70 # define ADDR_NO_RANDOMIZE 0x0040000
72 #endif /* HAVE_PERSONALITY */
74 /* This comment documents high-level logic of this file.
76 Waiting for events in sync mode
77 ===============================
79 When waiting for an event in a specific thread, we just use waitpid, passing
80 the specific pid, and not passing WNOHANG.
82 When waiting for an event in all threads, waitpid is not quite good. Prior to
83 version 2.4, Linux can either wait for event in main thread, or in secondary
84 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
85 miss an event. The solution is to use non-blocking waitpid, together with
86 sigsuspend. First, we use non-blocking waitpid to get an event in the main
87 process, if any. Second, we use non-blocking waitpid with the __WCLONED
88 flag to check for events in cloned processes. If nothing is found, we use
89 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
90 happened to a child process -- and SIGCHLD will be delivered both for events
91 in main debugged process and in cloned processes. As soon as we know there's
92 an event, we get back to calling nonblocking waitpid with and without
95 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
96 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
97 blocked, the signal becomes pending and sigsuspend immediately
98 notices it and returns.
100 Waiting for events in async mode
101 ================================
103 In async mode, GDB should always be ready to handle both user input
104 and target events, so neither blocking waitpid nor sigsuspend are
105 viable options. Instead, we should asynchronously notify the GDB main
106 event loop whenever there's an unprocessed event from the target. We
107 detect asynchronous target events by handling SIGCHLD signals. To
108 notify the event loop about target events, the self-pipe trick is used
109 --- a pipe is registered as waitable event source in the event loop,
110 the event loop select/poll's on the read end of this pipe (as well on
111 other event sources, e.g., stdin), and the SIGCHLD handler writes a
112 byte to this pipe. This is more portable than relying on
113 pselect/ppoll, since on kernels that lack those syscalls, libc
114 emulates them with select/poll+sigprocmask, and that is racy
115 (a.k.a. plain broken).
117 Obviously, if we fail to notify the event loop if there's a target
118 event, it's bad. OTOH, if we notify the event loop when there's no
119 event from the target, linux_nat_wait will detect that there's no real
120 event to report, and return event of type TARGET_WAITKIND_IGNORE.
121 This is mostly harmless, but it will waste time and is better avoided.
123 The main design point is that every time GDB is outside linux-nat.c,
124 we have a SIGCHLD handler installed that is called when something
125 happens to the target and notifies the GDB event loop. Whenever GDB
126 core decides to handle the event, and calls into linux-nat.c, we
127 process things as in sync mode, except that the we never block in
130 While processing an event, we may end up momentarily blocked in
131 waitpid calls. Those waitpid calls, while blocking, are guarantied to
132 return quickly. E.g., in all-stop mode, before reporting to the core
133 that an LWP hit a breakpoint, all LWPs are stopped by sending them
134 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
135 Note that this is different from blocking indefinitely waiting for the
136 next event --- here, we're already handling an event.
141 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
142 signal is not entirely significant; we just need for a signal to be delivered,
143 so that we can intercept it. SIGSTOP's advantage is that it can not be
144 blocked. A disadvantage is that it is not a real-time signal, so it can only
145 be queued once; we do not keep track of other sources of SIGSTOP.
147 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
148 use them, because they have special behavior when the signal is generated -
149 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
150 kills the entire thread group.
152 A delivered SIGSTOP would stop the entire thread group, not just the thread we
153 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
154 cancel it (by PTRACE_CONT without passing SIGSTOP).
156 We could use a real-time signal instead. This would solve those problems; we
157 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
158 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
159 generates it, and there are races with trying to find a signal that is not
163 #define O_LARGEFILE 0
166 /* Unlike other extended result codes, WSTOPSIG (status) on
167 PTRACE_O_TRACESYSGOOD syscall events doesn't return SIGTRAP, but
168 instead SIGTRAP with bit 7 set. */
169 #define SYSCALL_SIGTRAP (SIGTRAP | 0x80)
171 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
172 the use of the multi-threaded target. */
173 static struct target_ops
*linux_ops
;
174 static struct target_ops linux_ops_saved
;
176 /* The method to call, if any, when a new thread is attached. */
177 static void (*linux_nat_new_thread
) (struct lwp_info
*);
179 /* Hook to call prior to resuming a thread. */
180 static void (*linux_nat_prepare_to_resume
) (struct lwp_info
*);
182 /* The method to call, if any, when the siginfo object needs to be
183 converted between the layout returned by ptrace, and the layout in
184 the architecture of the inferior. */
185 static int (*linux_nat_siginfo_fixup
) (struct siginfo
*,
189 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
190 Called by our to_xfer_partial. */
191 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
193 const char *, gdb_byte
*,
197 static int debug_linux_nat
;
199 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
200 struct cmd_list_element
*c
, const char *value
)
202 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
206 struct simple_pid_list
210 struct simple_pid_list
*next
;
212 struct simple_pid_list
*stopped_pids
;
214 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
215 can not be used, 1 if it can. */
217 static int linux_supports_tracefork_flag
= -1;
219 /* This variable is a tri-state flag: -1 for unknown, 0 if
220 PTRACE_O_TRACESYSGOOD can not be used, 1 if it can. */
222 static int linux_supports_tracesysgood_flag
= -1;
224 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
225 PTRACE_O_TRACEVFORKDONE. */
227 static int linux_supports_tracevforkdone_flag
= -1;
229 /* Stores the current used ptrace() options. */
230 static int current_ptrace_options
= 0;
232 /* Async mode support. */
234 /* The read/write ends of the pipe registered as waitable file in the
236 static int linux_nat_event_pipe
[2] = { -1, -1 };
238 /* Flush the event pipe. */
241 async_file_flush (void)
248 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
250 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
253 /* Put something (anything, doesn't matter what, or how much) in event
254 pipe, so that the select/poll in the event-loop realizes we have
255 something to process. */
258 async_file_mark (void)
262 /* It doesn't really matter what the pipe contains, as long we end
263 up with something in it. Might as well flush the previous
269 ret
= write (linux_nat_event_pipe
[1], "+", 1);
271 while (ret
== -1 && errno
== EINTR
);
273 /* Ignore EAGAIN. If the pipe is full, the event loop will already
274 be awakened anyway. */
277 static void linux_nat_async (void (*callback
)
278 (enum inferior_event_type event_type
,
281 static int kill_lwp (int lwpid
, int signo
);
283 static int stop_callback (struct lwp_info
*lp
, void *data
);
285 static void block_child_signals (sigset_t
*prev_mask
);
286 static void restore_child_signals_mask (sigset_t
*prev_mask
);
289 static struct lwp_info
*add_lwp (ptid_t ptid
);
290 static void purge_lwp_list (int pid
);
291 static struct lwp_info
*find_lwp_pid (ptid_t ptid
);
294 /* Trivial list manipulation functions to keep track of a list of
295 new stopped processes. */
297 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
299 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
302 new_pid
->status
= status
;
303 new_pid
->next
= *listp
;
308 in_pid_list_p (struct simple_pid_list
*list
, int pid
)
310 struct simple_pid_list
*p
;
312 for (p
= list
; p
!= NULL
; p
= p
->next
)
319 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
321 struct simple_pid_list
**p
;
323 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
324 if ((*p
)->pid
== pid
)
326 struct simple_pid_list
*next
= (*p
)->next
;
328 *statusp
= (*p
)->status
;
337 /* A helper function for linux_test_for_tracefork, called after fork (). */
340 linux_tracefork_child (void)
342 ptrace (PTRACE_TRACEME
, 0, 0, 0);
343 kill (getpid (), SIGSTOP
);
348 /* Wrapper function for waitpid which handles EINTR. */
351 my_waitpid (int pid
, int *statusp
, int flags
)
357 ret
= waitpid (pid
, statusp
, flags
);
359 while (ret
== -1 && errno
== EINTR
);
364 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
366 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
367 we know that the feature is not available. This may change the tracing
368 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
370 However, if it succeeds, we don't know for sure that the feature is
371 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
372 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
373 fork tracing, and let it fork. If the process exits, we assume that we
374 can't use TRACEFORK; if we get the fork notification, and we can extract
375 the new child's PID, then we assume that we can. */
378 linux_test_for_tracefork (int original_pid
)
380 int child_pid
, ret
, status
;
384 /* We don't want those ptrace calls to be interrupted. */
385 block_child_signals (&prev_mask
);
387 linux_supports_tracefork_flag
= 0;
388 linux_supports_tracevforkdone_flag
= 0;
390 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
393 restore_child_signals_mask (&prev_mask
);
399 perror_with_name (("fork"));
402 linux_tracefork_child ();
404 ret
= my_waitpid (child_pid
, &status
, 0);
406 perror_with_name (("waitpid"));
407 else if (ret
!= child_pid
)
408 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
409 if (! WIFSTOPPED (status
))
410 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."),
413 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
416 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
419 warning (_("linux_test_for_tracefork: failed to kill child"));
420 restore_child_signals_mask (&prev_mask
);
424 ret
= my_waitpid (child_pid
, &status
, 0);
425 if (ret
!= child_pid
)
426 warning (_("linux_test_for_tracefork: failed "
427 "to wait for killed child"));
428 else if (!WIFSIGNALED (status
))
429 warning (_("linux_test_for_tracefork: unexpected "
430 "wait status 0x%x from killed child"), status
);
432 restore_child_signals_mask (&prev_mask
);
436 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
437 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
438 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
439 linux_supports_tracevforkdone_flag
= (ret
== 0);
441 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
443 warning (_("linux_test_for_tracefork: failed to resume child"));
445 ret
= my_waitpid (child_pid
, &status
, 0);
447 if (ret
== child_pid
&& WIFSTOPPED (status
)
448 && status
>> 16 == PTRACE_EVENT_FORK
)
451 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
452 if (ret
== 0 && second_pid
!= 0)
456 linux_supports_tracefork_flag
= 1;
457 my_waitpid (second_pid
, &second_status
, 0);
458 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
460 warning (_("linux_test_for_tracefork: "
461 "failed to kill second child"));
462 my_waitpid (second_pid
, &status
, 0);
466 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
467 "(%d, status 0x%x)"), ret
, status
);
469 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
471 warning (_("linux_test_for_tracefork: failed to kill child"));
472 my_waitpid (child_pid
, &status
, 0);
474 restore_child_signals_mask (&prev_mask
);
477 /* Determine if PTRACE_O_TRACESYSGOOD can be used to follow syscalls.
479 We try to enable syscall tracing on ORIGINAL_PID. If this fails,
480 we know that the feature is not available. This may change the tracing
481 options for ORIGINAL_PID, but we'll be setting them shortly anyway. */
484 linux_test_for_tracesysgood (int original_pid
)
489 /* We don't want those ptrace calls to be interrupted. */
490 block_child_signals (&prev_mask
);
492 linux_supports_tracesysgood_flag
= 0;
494 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACESYSGOOD
);
498 linux_supports_tracesysgood_flag
= 1;
500 restore_child_signals_mask (&prev_mask
);
503 /* Determine wether we support PTRACE_O_TRACESYSGOOD option available.
504 This function also sets linux_supports_tracesysgood_flag. */
507 linux_supports_tracesysgood (int pid
)
509 if (linux_supports_tracesysgood_flag
== -1)
510 linux_test_for_tracesysgood (pid
);
511 return linux_supports_tracesysgood_flag
;
514 /* Return non-zero iff we have tracefork functionality available.
515 This function also sets linux_supports_tracefork_flag. */
518 linux_supports_tracefork (int pid
)
520 if (linux_supports_tracefork_flag
== -1)
521 linux_test_for_tracefork (pid
);
522 return linux_supports_tracefork_flag
;
526 linux_supports_tracevforkdone (int pid
)
528 if (linux_supports_tracefork_flag
== -1)
529 linux_test_for_tracefork (pid
);
530 return linux_supports_tracevforkdone_flag
;
534 linux_enable_tracesysgood (ptid_t ptid
)
536 int pid
= ptid_get_lwp (ptid
);
539 pid
= ptid_get_pid (ptid
);
541 if (linux_supports_tracesysgood (pid
) == 0)
544 current_ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
546 ptrace (PTRACE_SETOPTIONS
, pid
, 0, current_ptrace_options
);
551 linux_enable_event_reporting (ptid_t ptid
)
553 int pid
= ptid_get_lwp (ptid
);
556 pid
= ptid_get_pid (ptid
);
558 if (! linux_supports_tracefork (pid
))
561 current_ptrace_options
|= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
562 | PTRACE_O_TRACEEXEC
| PTRACE_O_TRACECLONE
;
564 if (linux_supports_tracevforkdone (pid
))
565 current_ptrace_options
|= PTRACE_O_TRACEVFORKDONE
;
567 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
568 read-only process state. */
570 ptrace (PTRACE_SETOPTIONS
, pid
, 0, current_ptrace_options
);
574 linux_child_post_attach (int pid
)
576 linux_enable_event_reporting (pid_to_ptid (pid
));
577 linux_enable_tracesysgood (pid_to_ptid (pid
));
581 linux_child_post_startup_inferior (ptid_t ptid
)
583 linux_enable_event_reporting (ptid
);
584 linux_enable_tracesysgood (ptid
);
588 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
592 int parent_pid
, child_pid
;
594 block_child_signals (&prev_mask
);
596 has_vforked
= (inferior_thread ()->pending_follow
.kind
597 == TARGET_WAITKIND_VFORKED
);
598 parent_pid
= ptid_get_lwp (inferior_ptid
);
600 parent_pid
= ptid_get_pid (inferior_ptid
);
601 child_pid
= PIDGET (inferior_thread ()->pending_follow
.value
.related_pid
);
604 linux_enable_event_reporting (pid_to_ptid (child_pid
));
607 && !non_stop
/* Non-stop always resumes both branches. */
608 && (!target_is_async_p () || sync_execution
)
609 && !(follow_child
|| detach_fork
|| sched_multi
))
611 /* The parent stays blocked inside the vfork syscall until the
612 child execs or exits. If we don't let the child run, then
613 the parent stays blocked. If we're telling the parent to run
614 in the foreground, the user will not be able to ctrl-c to get
615 back the terminal, effectively hanging the debug session. */
616 fprintf_filtered (gdb_stderr
, _("\
617 Can not resume the parent process over vfork in the foreground while\n\
618 holding the child stopped. Try \"set detach-on-fork\" or \
619 \"set schedule-multiple\".\n"));
620 /* FIXME output string > 80 columns. */
626 struct lwp_info
*child_lp
= NULL
;
628 /* We're already attached to the parent, by default. */
630 /* Detach new forked process? */
633 /* Before detaching from the child, remove all breakpoints
634 from it. If we forked, then this has already been taken
635 care of by infrun.c. If we vforked however, any
636 breakpoint inserted in the parent is visible in the
637 child, even those added while stopped in a vfork
638 catchpoint. This will remove the breakpoints from the
639 parent also, but they'll be reinserted below. */
642 /* keep breakpoints list in sync. */
643 remove_breakpoints_pid (GET_PID (inferior_ptid
));
646 if (info_verbose
|| debug_linux_nat
)
648 target_terminal_ours ();
649 fprintf_filtered (gdb_stdlog
,
650 "Detaching after fork from "
651 "child process %d.\n",
655 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
659 struct inferior
*parent_inf
, *child_inf
;
660 struct cleanup
*old_chain
;
662 /* Add process to GDB's tables. */
663 child_inf
= add_inferior (child_pid
);
665 parent_inf
= current_inferior ();
666 child_inf
->attach_flag
= parent_inf
->attach_flag
;
667 copy_terminal_info (child_inf
, parent_inf
);
669 old_chain
= save_inferior_ptid ();
670 save_current_program_space ();
672 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
673 add_thread (inferior_ptid
);
674 child_lp
= add_lwp (inferior_ptid
);
675 child_lp
->stopped
= 1;
676 child_lp
->last_resume_kind
= resume_stop
;
678 /* If this is a vfork child, then the address-space is
679 shared with the parent. */
682 child_inf
->pspace
= parent_inf
->pspace
;
683 child_inf
->aspace
= parent_inf
->aspace
;
685 /* The parent will be frozen until the child is done
686 with the shared region. Keep track of the
688 child_inf
->vfork_parent
= parent_inf
;
689 child_inf
->pending_detach
= 0;
690 parent_inf
->vfork_child
= child_inf
;
691 parent_inf
->pending_detach
= 0;
695 child_inf
->aspace
= new_address_space ();
696 child_inf
->pspace
= add_program_space (child_inf
->aspace
);
697 child_inf
->removable
= 1;
698 set_current_program_space (child_inf
->pspace
);
699 clone_program_space (child_inf
->pspace
, parent_inf
->pspace
);
701 /* Let the shared library layer (solib-svr4) learn about
702 this new process, relocate the cloned exec, pull in
703 shared libraries, and install the solib event
704 breakpoint. If a "cloned-VM" event was propagated
705 better throughout the core, this wouldn't be
707 solib_create_inferior_hook (0);
710 /* Let the thread_db layer learn about this new process. */
711 check_for_thread_db ();
713 do_cleanups (old_chain
);
718 struct lwp_info
*parent_lp
;
719 struct inferior
*parent_inf
;
721 parent_inf
= current_inferior ();
723 /* If we detached from the child, then we have to be careful
724 to not insert breakpoints in the parent until the child
725 is done with the shared memory region. However, if we're
726 staying attached to the child, then we can and should
727 insert breakpoints, so that we can debug it. A
728 subsequent child exec or exit is enough to know when does
729 the child stops using the parent's address space. */
730 parent_inf
->waiting_for_vfork_done
= detach_fork
;
731 parent_inf
->pspace
->breakpoints_not_allowed
= detach_fork
;
733 parent_lp
= find_lwp_pid (pid_to_ptid (parent_pid
));
734 gdb_assert (linux_supports_tracefork_flag
>= 0);
736 if (linux_supports_tracevforkdone (0))
739 fprintf_unfiltered (gdb_stdlog
,
740 "LCFF: waiting for VFORK_DONE on %d\n",
742 parent_lp
->stopped
= 1;
744 /* We'll handle the VFORK_DONE event like any other
745 event, in target_wait. */
749 /* We can't insert breakpoints until the child has
750 finished with the shared memory region. We need to
751 wait until that happens. Ideal would be to just
753 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
754 - waitpid (parent_pid, &status, __WALL);
755 However, most architectures can't handle a syscall
756 being traced on the way out if it wasn't traced on
759 We might also think to loop, continuing the child
760 until it exits or gets a SIGTRAP. One problem is
761 that the child might call ptrace with PTRACE_TRACEME.
763 There's no simple and reliable way to figure out when
764 the vforked child will be done with its copy of the
765 shared memory. We could step it out of the syscall,
766 two instructions, let it go, and then single-step the
767 parent once. When we have hardware single-step, this
768 would work; with software single-step it could still
769 be made to work but we'd have to be able to insert
770 single-step breakpoints in the child, and we'd have
771 to insert -just- the single-step breakpoint in the
772 parent. Very awkward.
774 In the end, the best we can do is to make sure it
775 runs for a little while. Hopefully it will be out of
776 range of any breakpoints we reinsert. Usually this
777 is only the single-step breakpoint at vfork's return
781 fprintf_unfiltered (gdb_stdlog
,
782 "LCFF: no VFORK_DONE "
783 "support, sleeping a bit\n");
787 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
788 and leave it pending. The next linux_nat_resume call
789 will notice a pending event, and bypasses actually
790 resuming the inferior. */
791 parent_lp
->status
= 0;
792 parent_lp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
793 parent_lp
->stopped
= 1;
795 /* If we're in async mode, need to tell the event loop
796 there's something here to process. */
797 if (target_can_async_p ())
804 struct inferior
*parent_inf
, *child_inf
;
805 struct lwp_info
*child_lp
;
806 struct program_space
*parent_pspace
;
808 if (info_verbose
|| debug_linux_nat
)
810 target_terminal_ours ();
812 fprintf_filtered (gdb_stdlog
,
813 _("Attaching after process %d "
814 "vfork to child process %d.\n"),
815 parent_pid
, child_pid
);
817 fprintf_filtered (gdb_stdlog
,
818 _("Attaching after process %d "
819 "fork to child process %d.\n"),
820 parent_pid
, child_pid
);
823 /* Add the new inferior first, so that the target_detach below
824 doesn't unpush the target. */
826 child_inf
= add_inferior (child_pid
);
828 parent_inf
= current_inferior ();
829 child_inf
->attach_flag
= parent_inf
->attach_flag
;
830 copy_terminal_info (child_inf
, parent_inf
);
832 parent_pspace
= parent_inf
->pspace
;
834 /* If we're vforking, we want to hold on to the parent until the
835 child exits or execs. At child exec or exit time we can
836 remove the old breakpoints from the parent and detach or
837 resume debugging it. Otherwise, detach the parent now; we'll
838 want to reuse it's program/address spaces, but we can't set
839 them to the child before removing breakpoints from the
840 parent, otherwise, the breakpoints module could decide to
841 remove breakpoints from the wrong process (since they'd be
842 assigned to the same address space). */
846 gdb_assert (child_inf
->vfork_parent
== NULL
);
847 gdb_assert (parent_inf
->vfork_child
== NULL
);
848 child_inf
->vfork_parent
= parent_inf
;
849 child_inf
->pending_detach
= 0;
850 parent_inf
->vfork_child
= child_inf
;
851 parent_inf
->pending_detach
= detach_fork
;
852 parent_inf
->waiting_for_vfork_done
= 0;
854 else if (detach_fork
)
855 target_detach (NULL
, 0);
857 /* Note that the detach above makes PARENT_INF dangling. */
859 /* Add the child thread to the appropriate lists, and switch to
860 this new thread, before cloning the program space, and
861 informing the solib layer about this new process. */
863 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
864 add_thread (inferior_ptid
);
865 child_lp
= add_lwp (inferior_ptid
);
866 child_lp
->stopped
= 1;
867 child_lp
->last_resume_kind
= resume_stop
;
869 /* If this is a vfork child, then the address-space is shared
870 with the parent. If we detached from the parent, then we can
871 reuse the parent's program/address spaces. */
872 if (has_vforked
|| detach_fork
)
874 child_inf
->pspace
= parent_pspace
;
875 child_inf
->aspace
= child_inf
->pspace
->aspace
;
879 child_inf
->aspace
= new_address_space ();
880 child_inf
->pspace
= add_program_space (child_inf
->aspace
);
881 child_inf
->removable
= 1;
882 set_current_program_space (child_inf
->pspace
);
883 clone_program_space (child_inf
->pspace
, parent_pspace
);
885 /* Let the shared library layer (solib-svr4) learn about
886 this new process, relocate the cloned exec, pull in
887 shared libraries, and install the solib event breakpoint.
888 If a "cloned-VM" event was propagated better throughout
889 the core, this wouldn't be required. */
890 solib_create_inferior_hook (0);
893 /* Let the thread_db layer learn about this new process. */
894 check_for_thread_db ();
897 restore_child_signals_mask (&prev_mask
);
903 linux_child_insert_fork_catchpoint (int pid
)
905 return !linux_supports_tracefork (pid
);
909 linux_child_remove_fork_catchpoint (int pid
)
915 linux_child_insert_vfork_catchpoint (int pid
)
917 return !linux_supports_tracefork (pid
);
921 linux_child_remove_vfork_catchpoint (int pid
)
927 linux_child_insert_exec_catchpoint (int pid
)
929 return !linux_supports_tracefork (pid
);
933 linux_child_remove_exec_catchpoint (int pid
)
939 linux_child_set_syscall_catchpoint (int pid
, int needed
, int any_count
,
940 int table_size
, int *table
)
942 if (!linux_supports_tracesysgood (pid
))
945 /* On GNU/Linux, we ignore the arguments. It means that we only
946 enable the syscall catchpoints, but do not disable them.
948 Also, we do not use the `table' information because we do not
949 filter system calls here. We let GDB do the logic for us. */
953 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
954 are processes sharing the same VM space. A multi-threaded process
955 is basically a group of such processes. However, such a grouping
956 is almost entirely a user-space issue; the kernel doesn't enforce
957 such a grouping at all (this might change in the future). In
958 general, we'll rely on the threads library (i.e. the GNU/Linux
959 Threads library) to provide such a grouping.
961 It is perfectly well possible to write a multi-threaded application
962 without the assistance of a threads library, by using the clone
963 system call directly. This module should be able to give some
964 rudimentary support for debugging such applications if developers
965 specify the CLONE_PTRACE flag in the clone system call, and are
966 using the Linux kernel 2.4 or above.
968 Note that there are some peculiarities in GNU/Linux that affect
971 - In general one should specify the __WCLONE flag to waitpid in
972 order to make it report events for any of the cloned processes
973 (and leave it out for the initial process). However, if a cloned
974 process has exited the exit status is only reported if the
975 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
976 we cannot use it since GDB must work on older systems too.
978 - When a traced, cloned process exits and is waited for by the
979 debugger, the kernel reassigns it to the original parent and
980 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
981 library doesn't notice this, which leads to the "zombie problem":
982 When debugged a multi-threaded process that spawns a lot of
983 threads will run out of processes, even if the threads exit,
984 because the "zombies" stay around. */
986 /* List of known LWPs. */
987 struct lwp_info
*lwp_list
;
990 /* Original signal mask. */
991 static sigset_t normal_mask
;
993 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
994 _initialize_linux_nat. */
995 static sigset_t suspend_mask
;
997 /* Signals to block to make that sigsuspend work. */
998 static sigset_t blocked_mask
;
1000 /* SIGCHLD action. */
1001 struct sigaction sigchld_action
;
1003 /* Block child signals (SIGCHLD and linux threads signals), and store
1004 the previous mask in PREV_MASK. */
1007 block_child_signals (sigset_t
*prev_mask
)
1009 /* Make sure SIGCHLD is blocked. */
1010 if (!sigismember (&blocked_mask
, SIGCHLD
))
1011 sigaddset (&blocked_mask
, SIGCHLD
);
1013 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
1016 /* Restore child signals mask, previously returned by
1017 block_child_signals. */
1020 restore_child_signals_mask (sigset_t
*prev_mask
)
1022 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
1025 /* Mask of signals to pass directly to the inferior. */
1026 static sigset_t pass_mask
;
1028 /* Update signals to pass to the inferior. */
1030 linux_nat_pass_signals (int numsigs
, unsigned char *pass_signals
)
1034 sigemptyset (&pass_mask
);
1036 for (signo
= 1; signo
< NSIG
; signo
++)
1038 int target_signo
= target_signal_from_host (signo
);
1039 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
1040 sigaddset (&pass_mask
, signo
);
1046 /* Prototypes for local functions. */
1047 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
1048 static int linux_thread_alive (ptid_t ptid
);
1049 static char *linux_child_pid_to_exec_file (int pid
);
1052 /* Convert wait status STATUS to a string. Used for printing debug
1056 status_to_str (int status
)
1058 static char buf
[64];
1060 if (WIFSTOPPED (status
))
1062 if (WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
1063 snprintf (buf
, sizeof (buf
), "%s (stopped at syscall)",
1064 strsignal (SIGTRAP
));
1066 snprintf (buf
, sizeof (buf
), "%s (stopped)",
1067 strsignal (WSTOPSIG (status
)));
1069 else if (WIFSIGNALED (status
))
1070 snprintf (buf
, sizeof (buf
), "%s (terminated)",
1071 strsignal (WTERMSIG (status
)));
1073 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
1078 /* Destroy and free LP. */
1081 lwp_free (struct lwp_info
*lp
)
1083 xfree (lp
->arch_private
);
1087 /* Remove all LWPs belong to PID from the lwp list. */
1090 purge_lwp_list (int pid
)
1092 struct lwp_info
*lp
, *lpprev
, *lpnext
;
1096 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1100 if (ptid_get_pid (lp
->ptid
) == pid
)
1103 lwp_list
= lp
->next
;
1105 lpprev
->next
= lp
->next
;
1114 /* Return the number of known LWPs in the tgid given by PID. */
1120 struct lwp_info
*lp
;
1122 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1123 if (ptid_get_pid (lp
->ptid
) == pid
)
1129 /* Add the LWP specified by PID to the list. Return a pointer to the
1130 structure describing the new LWP. The LWP should already be stopped
1131 (with an exception for the very first LWP). */
1133 static struct lwp_info
*
1134 add_lwp (ptid_t ptid
)
1136 struct lwp_info
*lp
;
1138 gdb_assert (is_lwp (ptid
));
1140 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
1142 memset (lp
, 0, sizeof (struct lwp_info
));
1144 lp
->last_resume_kind
= resume_continue
;
1145 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1150 lp
->next
= lwp_list
;
1153 /* Let the arch specific bits know about this new thread. Current
1154 clients of this callback take the opportunity to install
1155 watchpoints in the new thread. Don't do this for the first
1156 thread though. If we're spawning a child ("run"), the thread
1157 executes the shell wrapper first, and we shouldn't touch it until
1158 it execs the program we want to debug. For "attach", it'd be
1159 okay to call the callback, but it's not necessary, because
1160 watchpoints can't yet have been inserted into the inferior. */
1161 if (num_lwps (GET_PID (ptid
)) > 1 && linux_nat_new_thread
!= NULL
)
1162 linux_nat_new_thread (lp
);
1167 /* Remove the LWP specified by PID from the list. */
1170 delete_lwp (ptid_t ptid
)
1172 struct lwp_info
*lp
, *lpprev
;
1176 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
1177 if (ptid_equal (lp
->ptid
, ptid
))
1184 lpprev
->next
= lp
->next
;
1186 lwp_list
= lp
->next
;
1191 /* Return a pointer to the structure describing the LWP corresponding
1192 to PID. If no corresponding LWP could be found, return NULL. */
1194 static struct lwp_info
*
1195 find_lwp_pid (ptid_t ptid
)
1197 struct lwp_info
*lp
;
1201 lwp
= GET_LWP (ptid
);
1203 lwp
= GET_PID (ptid
);
1205 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1206 if (lwp
== GET_LWP (lp
->ptid
))
1212 /* Call CALLBACK with its second argument set to DATA for every LWP in
1213 the list. If CALLBACK returns 1 for a particular LWP, return a
1214 pointer to the structure describing that LWP immediately.
1215 Otherwise return NULL. */
1218 iterate_over_lwps (ptid_t filter
,
1219 int (*callback
) (struct lwp_info
*, void *),
1222 struct lwp_info
*lp
, *lpnext
;
1224 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1228 if (ptid_match (lp
->ptid
, filter
))
1230 if ((*callback
) (lp
, data
))
1238 /* Update our internal state when changing from one checkpoint to
1239 another indicated by NEW_PTID. We can only switch single-threaded
1240 applications, so we only create one new LWP, and the previous list
1244 linux_nat_switch_fork (ptid_t new_ptid
)
1246 struct lwp_info
*lp
;
1248 purge_lwp_list (GET_PID (inferior_ptid
));
1250 lp
= add_lwp (new_ptid
);
1253 /* This changes the thread's ptid while preserving the gdb thread
1254 num. Also changes the inferior pid, while preserving the
1256 thread_change_ptid (inferior_ptid
, new_ptid
);
1258 /* We've just told GDB core that the thread changed target id, but,
1259 in fact, it really is a different thread, with different register
1261 registers_changed ();
1264 /* Handle the exit of a single thread LP. */
1267 exit_lwp (struct lwp_info
*lp
)
1269 struct thread_info
*th
= find_thread_ptid (lp
->ptid
);
1273 if (print_thread_events
)
1274 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1276 delete_thread (lp
->ptid
);
1279 delete_lwp (lp
->ptid
);
1282 /* Detect `T (stopped)' in `/proc/PID/status'.
1283 Other states including `T (tracing stop)' are reported as false. */
1286 pid_is_stopped (pid_t pid
)
1292 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) pid
);
1293 status_file
= fopen (buf
, "r");
1294 if (status_file
!= NULL
)
1298 while (fgets (buf
, sizeof (buf
), status_file
))
1300 if (strncmp (buf
, "State:", 6) == 0)
1306 if (have_state
&& strstr (buf
, "T (stopped)") != NULL
)
1308 fclose (status_file
);
1313 /* Wait for the LWP specified by LP, which we have just attached to.
1314 Returns a wait status for that LWP, to cache. */
1317 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1320 pid_t new_pid
, pid
= GET_LWP (ptid
);
1323 if (pid_is_stopped (pid
))
1325 if (debug_linux_nat
)
1326 fprintf_unfiltered (gdb_stdlog
,
1327 "LNPAW: Attaching to a stopped process\n");
1329 /* The process is definitely stopped. It is in a job control
1330 stop, unless the kernel predates the TASK_STOPPED /
1331 TASK_TRACED distinction, in which case it might be in a
1332 ptrace stop. Make sure it is in a ptrace stop; from there we
1333 can kill it, signal it, et cetera.
1335 First make sure there is a pending SIGSTOP. Since we are
1336 already attached, the process can not transition from stopped
1337 to running without a PTRACE_CONT; so we know this signal will
1338 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1339 probably already in the queue (unless this kernel is old
1340 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1341 is not an RT signal, it can only be queued once. */
1342 kill_lwp (pid
, SIGSTOP
);
1344 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1345 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1346 ptrace (PTRACE_CONT
, pid
, 0, 0);
1349 /* Make sure the initial process is stopped. The user-level threads
1350 layer might want to poke around in the inferior, and that won't
1351 work if things haven't stabilized yet. */
1352 new_pid
= my_waitpid (pid
, &status
, 0);
1353 if (new_pid
== -1 && errno
== ECHILD
)
1356 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1358 /* Try again with __WCLONE to check cloned processes. */
1359 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1363 gdb_assert (pid
== new_pid
);
1365 if (!WIFSTOPPED (status
))
1367 /* The pid we tried to attach has apparently just exited. */
1368 if (debug_linux_nat
)
1369 fprintf_unfiltered (gdb_stdlog
, "LNPAW: Failed to stop %d: %s",
1370 pid
, status_to_str (status
));
1374 if (WSTOPSIG (status
) != SIGSTOP
)
1377 if (debug_linux_nat
)
1378 fprintf_unfiltered (gdb_stdlog
,
1379 "LNPAW: Received %s after attaching\n",
1380 status_to_str (status
));
1386 /* Attach to the LWP specified by PID. Return 0 if successful, -1 if
1387 the new LWP could not be attached, or 1 if we're already auto
1388 attached to this thread, but haven't processed the
1389 PTRACE_EVENT_CLONE event of its parent thread, so we just ignore
1390 its existance, without considering it an error. */
1393 lin_lwp_attach_lwp (ptid_t ptid
)
1395 struct lwp_info
*lp
;
1399 gdb_assert (is_lwp (ptid
));
1401 block_child_signals (&prev_mask
);
1403 lp
= find_lwp_pid (ptid
);
1404 lwpid
= GET_LWP (ptid
);
1406 /* We assume that we're already attached to any LWP that has an id
1407 equal to the overall process id, and to any LWP that is already
1408 in our list of LWPs. If we're not seeing exit events from threads
1409 and we've had PID wraparound since we last tried to stop all threads,
1410 this assumption might be wrong; fortunately, this is very unlikely
1412 if (lwpid
!= GET_PID (ptid
) && lp
== NULL
)
1414 int status
, cloned
= 0, signalled
= 0;
1416 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) < 0)
1418 if (linux_supports_tracefork_flag
)
1420 /* If we haven't stopped all threads when we get here,
1421 we may have seen a thread listed in thread_db's list,
1422 but not processed the PTRACE_EVENT_CLONE yet. If
1423 that's the case, ignore this new thread, and let
1424 normal event handling discover it later. */
1425 if (in_pid_list_p (stopped_pids
, lwpid
))
1427 /* We've already seen this thread stop, but we
1428 haven't seen the PTRACE_EVENT_CLONE extended
1430 restore_child_signals_mask (&prev_mask
);
1438 /* See if we've got a stop for this new child
1439 pending. If so, we're already attached. */
1440 new_pid
= my_waitpid (lwpid
, &status
, WNOHANG
);
1441 if (new_pid
== -1 && errno
== ECHILD
)
1442 new_pid
= my_waitpid (lwpid
, &status
, __WCLONE
| WNOHANG
);
1445 if (WIFSTOPPED (status
))
1446 add_to_pid_list (&stopped_pids
, lwpid
, status
);
1448 restore_child_signals_mask (&prev_mask
);
1454 /* If we fail to attach to the thread, issue a warning,
1455 but continue. One way this can happen is if thread
1456 creation is interrupted; as of Linux kernel 2.6.19, a
1457 bug may place threads in the thread list and then fail
1459 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1460 safe_strerror (errno
));
1461 restore_child_signals_mask (&prev_mask
);
1465 if (debug_linux_nat
)
1466 fprintf_unfiltered (gdb_stdlog
,
1467 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1468 target_pid_to_str (ptid
));
1470 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1471 if (!WIFSTOPPED (status
))
1473 restore_child_signals_mask (&prev_mask
);
1477 lp
= add_lwp (ptid
);
1479 lp
->cloned
= cloned
;
1480 lp
->signalled
= signalled
;
1481 if (WSTOPSIG (status
) != SIGSTOP
)
1484 lp
->status
= status
;
1487 target_post_attach (GET_LWP (lp
->ptid
));
1489 if (debug_linux_nat
)
1491 fprintf_unfiltered (gdb_stdlog
,
1492 "LLAL: waitpid %s received %s\n",
1493 target_pid_to_str (ptid
),
1494 status_to_str (status
));
1499 /* We assume that the LWP representing the original process is
1500 already stopped. Mark it as stopped in the data structure
1501 that the GNU/linux ptrace layer uses to keep track of
1502 threads. Note that this won't have already been done since
1503 the main thread will have, we assume, been stopped by an
1504 attach from a different layer. */
1506 lp
= add_lwp (ptid
);
1510 lp
->last_resume_kind
= resume_stop
;
1511 restore_child_signals_mask (&prev_mask
);
1516 linux_nat_create_inferior (struct target_ops
*ops
,
1517 char *exec_file
, char *allargs
, char **env
,
1520 #ifdef HAVE_PERSONALITY
1521 int personality_orig
= 0, personality_set
= 0;
1522 #endif /* HAVE_PERSONALITY */
1524 /* The fork_child mechanism is synchronous and calls target_wait, so
1525 we have to mask the async mode. */
1527 #ifdef HAVE_PERSONALITY
1528 if (disable_randomization
)
1531 personality_orig
= personality (0xffffffff);
1532 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
1534 personality_set
= 1;
1535 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
1537 if (errno
!= 0 || (personality_set
1538 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
1539 warning (_("Error disabling address space randomization: %s"),
1540 safe_strerror (errno
));
1542 #endif /* HAVE_PERSONALITY */
1544 /* Make sure we report all signals during startup. */
1545 linux_nat_pass_signals (0, NULL
);
1547 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1549 #ifdef HAVE_PERSONALITY
1550 if (personality_set
)
1553 personality (personality_orig
);
1555 warning (_("Error restoring address space randomization: %s"),
1556 safe_strerror (errno
));
1558 #endif /* HAVE_PERSONALITY */
1562 linux_nat_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1564 struct lwp_info
*lp
;
1568 /* Make sure we report all signals during attach. */
1569 linux_nat_pass_signals (0, NULL
);
1571 linux_ops
->to_attach (ops
, args
, from_tty
);
1573 /* The ptrace base target adds the main thread with (pid,0,0)
1574 format. Decorate it with lwp info. */
1575 ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1576 thread_change_ptid (inferior_ptid
, ptid
);
1578 /* Add the initial process as the first LWP to the list. */
1579 lp
= add_lwp (ptid
);
1581 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1583 if (!WIFSTOPPED (status
))
1585 if (WIFEXITED (status
))
1587 int exit_code
= WEXITSTATUS (status
);
1589 target_terminal_ours ();
1590 target_mourn_inferior ();
1592 error (_("Unable to attach: program exited normally."));
1594 error (_("Unable to attach: program exited with code %d."),
1597 else if (WIFSIGNALED (status
))
1599 enum target_signal signo
;
1601 target_terminal_ours ();
1602 target_mourn_inferior ();
1604 signo
= target_signal_from_host (WTERMSIG (status
));
1605 error (_("Unable to attach: program terminated with signal "
1607 target_signal_to_name (signo
),
1608 target_signal_to_string (signo
));
1611 internal_error (__FILE__
, __LINE__
,
1612 _("unexpected status %d for PID %ld"),
1613 status
, (long) GET_LWP (ptid
));
1618 /* Save the wait status to report later. */
1620 if (debug_linux_nat
)
1621 fprintf_unfiltered (gdb_stdlog
,
1622 "LNA: waitpid %ld, saving status %s\n",
1623 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1625 lp
->status
= status
;
1627 if (target_can_async_p ())
1628 target_async (inferior_event_handler
, 0);
1631 /* Get pending status of LP. */
1633 get_pending_status (struct lwp_info
*lp
, int *status
)
1635 enum target_signal signo
= TARGET_SIGNAL_0
;
1637 /* If we paused threads momentarily, we may have stored pending
1638 events in lp->status or lp->waitstatus (see stop_wait_callback),
1639 and GDB core hasn't seen any signal for those threads.
1640 Otherwise, the last signal reported to the core is found in the
1641 thread object's stop_signal.
1643 There's a corner case that isn't handled here at present. Only
1644 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1645 stop_signal make sense as a real signal to pass to the inferior.
1646 Some catchpoint related events, like
1647 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1648 to TARGET_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1649 those traps are debug API (ptrace in our case) related and
1650 induced; the inferior wouldn't see them if it wasn't being
1651 traced. Hence, we should never pass them to the inferior, even
1652 when set to pass state. Since this corner case isn't handled by
1653 infrun.c when proceeding with a signal, for consistency, neither
1654 do we handle it here (or elsewhere in the file we check for
1655 signal pass state). Normally SIGTRAP isn't set to pass state, so
1656 this is really a corner case. */
1658 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
1659 signo
= TARGET_SIGNAL_0
; /* a pending ptrace event, not a real signal. */
1660 else if (lp
->status
)
1661 signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1662 else if (non_stop
&& !is_executing (lp
->ptid
))
1664 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1666 signo
= tp
->suspend
.stop_signal
;
1670 struct target_waitstatus last
;
1673 get_last_target_status (&last_ptid
, &last
);
1675 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1677 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1679 signo
= tp
->suspend
.stop_signal
;
1685 if (signo
== TARGET_SIGNAL_0
)
1687 if (debug_linux_nat
)
1688 fprintf_unfiltered (gdb_stdlog
,
1689 "GPT: lwp %s has no pending signal\n",
1690 target_pid_to_str (lp
->ptid
));
1692 else if (!signal_pass_state (signo
))
1694 if (debug_linux_nat
)
1695 fprintf_unfiltered (gdb_stdlog
,
1696 "GPT: lwp %s had signal %s, "
1697 "but it is in no pass state\n",
1698 target_pid_to_str (lp
->ptid
),
1699 target_signal_to_string (signo
));
1703 *status
= W_STOPCODE (target_signal_to_host (signo
));
1705 if (debug_linux_nat
)
1706 fprintf_unfiltered (gdb_stdlog
,
1707 "GPT: lwp %s has pending signal %s\n",
1708 target_pid_to_str (lp
->ptid
),
1709 target_signal_to_string (signo
));
1716 detach_callback (struct lwp_info
*lp
, void *data
)
1718 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1720 if (debug_linux_nat
&& lp
->status
)
1721 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1722 strsignal (WSTOPSIG (lp
->status
)),
1723 target_pid_to_str (lp
->ptid
));
1725 /* If there is a pending SIGSTOP, get rid of it. */
1728 if (debug_linux_nat
)
1729 fprintf_unfiltered (gdb_stdlog
,
1730 "DC: Sending SIGCONT to %s\n",
1731 target_pid_to_str (lp
->ptid
));
1733 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1737 /* We don't actually detach from the LWP that has an id equal to the
1738 overall process id just yet. */
1739 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1743 /* Pass on any pending signal for this LWP. */
1744 get_pending_status (lp
, &status
);
1746 if (linux_nat_prepare_to_resume
!= NULL
)
1747 linux_nat_prepare_to_resume (lp
);
1749 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1750 WSTOPSIG (status
)) < 0)
1751 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1752 safe_strerror (errno
));
1754 if (debug_linux_nat
)
1755 fprintf_unfiltered (gdb_stdlog
,
1756 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1757 target_pid_to_str (lp
->ptid
),
1758 strsignal (WSTOPSIG (status
)));
1760 delete_lwp (lp
->ptid
);
1767 linux_nat_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1771 struct lwp_info
*main_lwp
;
1773 pid
= GET_PID (inferior_ptid
);
1775 if (target_can_async_p ())
1776 linux_nat_async (NULL
, 0);
1778 /* Stop all threads before detaching. ptrace requires that the
1779 thread is stopped to sucessfully detach. */
1780 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1781 /* ... and wait until all of them have reported back that
1782 they're no longer running. */
1783 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1785 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1787 /* Only the initial process should be left right now. */
1788 gdb_assert (num_lwps (GET_PID (inferior_ptid
)) == 1);
1790 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1792 /* Pass on any pending signal for the last LWP. */
1793 if ((args
== NULL
|| *args
== '\0')
1794 && get_pending_status (main_lwp
, &status
) != -1
1795 && WIFSTOPPED (status
))
1797 /* Put the signal number in ARGS so that inf_ptrace_detach will
1798 pass it along with PTRACE_DETACH. */
1800 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1801 if (debug_linux_nat
)
1802 fprintf_unfiltered (gdb_stdlog
,
1803 "LND: Sending signal %s to %s\n",
1805 target_pid_to_str (main_lwp
->ptid
));
1808 if (linux_nat_prepare_to_resume
!= NULL
)
1809 linux_nat_prepare_to_resume (main_lwp
);
1810 delete_lwp (main_lwp
->ptid
);
1812 if (forks_exist_p ())
1814 /* Multi-fork case. The current inferior_ptid is being detached
1815 from, but there are other viable forks to debug. Detach from
1816 the current fork, and context-switch to the first
1818 linux_fork_detach (args
, from_tty
);
1820 if (non_stop
&& target_can_async_p ())
1821 target_async (inferior_event_handler
, 0);
1824 linux_ops
->to_detach (ops
, args
, from_tty
);
1830 resume_lwp (struct lwp_info
*lp
, int step
)
1834 struct inferior
*inf
= find_inferior_pid (GET_PID (lp
->ptid
));
1836 if (inf
->vfork_child
!= NULL
)
1838 if (debug_linux_nat
)
1839 fprintf_unfiltered (gdb_stdlog
,
1840 "RC: Not resuming %s (vfork parent)\n",
1841 target_pid_to_str (lp
->ptid
));
1843 else if (lp
->status
== 0
1844 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
1846 if (debug_linux_nat
)
1847 fprintf_unfiltered (gdb_stdlog
,
1848 "RC: PTRACE_CONT %s, 0, 0 (resuming sibling)\n",
1849 target_pid_to_str (lp
->ptid
));
1851 if (linux_nat_prepare_to_resume
!= NULL
)
1852 linux_nat_prepare_to_resume (lp
);
1853 linux_ops
->to_resume (linux_ops
,
1854 pid_to_ptid (GET_LWP (lp
->ptid
)),
1855 step
, TARGET_SIGNAL_0
);
1858 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1859 lp
->stopped_by_watchpoint
= 0;
1863 if (debug_linux_nat
)
1864 fprintf_unfiltered (gdb_stdlog
,
1865 "RC: Not resuming sibling %s (has pending)\n",
1866 target_pid_to_str (lp
->ptid
));
1871 if (debug_linux_nat
)
1872 fprintf_unfiltered (gdb_stdlog
,
1873 "RC: Not resuming sibling %s (not stopped)\n",
1874 target_pid_to_str (lp
->ptid
));
1879 resume_callback (struct lwp_info
*lp
, void *data
)
1886 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1889 lp
->last_resume_kind
= resume_stop
;
1894 resume_set_callback (struct lwp_info
*lp
, void *data
)
1897 lp
->last_resume_kind
= resume_continue
;
1902 linux_nat_resume (struct target_ops
*ops
,
1903 ptid_t ptid
, int step
, enum target_signal signo
)
1906 struct lwp_info
*lp
;
1909 if (debug_linux_nat
)
1910 fprintf_unfiltered (gdb_stdlog
,
1911 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1912 step
? "step" : "resume",
1913 target_pid_to_str (ptid
),
1914 (signo
!= TARGET_SIGNAL_0
1915 ? strsignal (target_signal_to_host (signo
)) : "0"),
1916 target_pid_to_str (inferior_ptid
));
1918 block_child_signals (&prev_mask
);
1920 /* A specific PTID means `step only this process id'. */
1921 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
1922 || ptid_is_pid (ptid
));
1924 /* Mark the lwps we're resuming as resumed. */
1925 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
1927 /* See if it's the current inferior that should be handled
1930 lp
= find_lwp_pid (inferior_ptid
);
1932 lp
= find_lwp_pid (ptid
);
1933 gdb_assert (lp
!= NULL
);
1935 /* Remember if we're stepping. */
1937 lp
->last_resume_kind
= step
? resume_step
: resume_continue
;
1939 /* If we have a pending wait status for this thread, there is no
1940 point in resuming the process. But first make sure that
1941 linux_nat_wait won't preemptively handle the event - we
1942 should never take this short-circuit if we are going to
1943 leave LP running, since we have skipped resuming all the
1944 other threads. This bit of code needs to be synchronized
1945 with linux_nat_wait. */
1947 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1950 && WSTOPSIG (lp
->status
)
1951 && sigismember (&pass_mask
, WSTOPSIG (lp
->status
)))
1953 if (debug_linux_nat
)
1954 fprintf_unfiltered (gdb_stdlog
,
1955 "LLR: Not short circuiting for ignored "
1956 "status 0x%x\n", lp
->status
);
1958 /* FIXME: What should we do if we are supposed to continue
1959 this thread with a signal? */
1960 gdb_assert (signo
== TARGET_SIGNAL_0
);
1961 signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1966 if (lp
->status
|| lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
1968 /* FIXME: What should we do if we are supposed to continue
1969 this thread with a signal? */
1970 gdb_assert (signo
== TARGET_SIGNAL_0
);
1972 if (debug_linux_nat
)
1973 fprintf_unfiltered (gdb_stdlog
,
1974 "LLR: Short circuiting for status 0x%x\n",
1977 restore_child_signals_mask (&prev_mask
);
1978 if (target_can_async_p ())
1980 target_async (inferior_event_handler
, 0);
1981 /* Tell the event loop we have something to process. */
1987 /* Mark LWP as not stopped to prevent it from being continued by
1992 iterate_over_lwps (ptid
, resume_callback
, NULL
);
1994 /* Convert to something the lower layer understands. */
1995 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1997 if (linux_nat_prepare_to_resume
!= NULL
)
1998 linux_nat_prepare_to_resume (lp
);
1999 linux_ops
->to_resume (linux_ops
, ptid
, step
, signo
);
2000 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
2001 lp
->stopped_by_watchpoint
= 0;
2003 if (debug_linux_nat
)
2004 fprintf_unfiltered (gdb_stdlog
,
2005 "LLR: %s %s, %s (resume event thread)\n",
2006 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2007 target_pid_to_str (ptid
),
2008 (signo
!= TARGET_SIGNAL_0
2009 ? strsignal (target_signal_to_host (signo
)) : "0"));
2011 restore_child_signals_mask (&prev_mask
);
2012 if (target_can_async_p ())
2013 target_async (inferior_event_handler
, 0);
2016 /* Send a signal to an LWP. */
2019 kill_lwp (int lwpid
, int signo
)
2021 /* Use tkill, if possible, in case we are using nptl threads. If tkill
2022 fails, then we are not using nptl threads and we should be using kill. */
2024 #ifdef HAVE_TKILL_SYSCALL
2026 static int tkill_failed
;
2033 ret
= syscall (__NR_tkill
, lwpid
, signo
);
2034 if (errno
!= ENOSYS
)
2041 return kill (lwpid
, signo
);
2044 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
2045 event, check if the core is interested in it: if not, ignore the
2046 event, and keep waiting; otherwise, we need to toggle the LWP's
2047 syscall entry/exit status, since the ptrace event itself doesn't
2048 indicate it, and report the trap to higher layers. */
2051 linux_handle_syscall_trap (struct lwp_info
*lp
, int stopping
)
2053 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
2054 struct gdbarch
*gdbarch
= target_thread_architecture (lp
->ptid
);
2055 int syscall_number
= (int) gdbarch_get_syscall_number (gdbarch
, lp
->ptid
);
2059 /* If we're stopping threads, there's a SIGSTOP pending, which
2060 makes it so that the LWP reports an immediate syscall return,
2061 followed by the SIGSTOP. Skip seeing that "return" using
2062 PTRACE_CONT directly, and let stop_wait_callback collect the
2063 SIGSTOP. Later when the thread is resumed, a new syscall
2064 entry event. If we didn't do this (and returned 0), we'd
2065 leave a syscall entry pending, and our caller, by using
2066 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
2067 itself. Later, when the user re-resumes this LWP, we'd see
2068 another syscall entry event and we'd mistake it for a return.
2070 If stop_wait_callback didn't force the SIGSTOP out of the LWP
2071 (leaving immediately with LWP->signalled set, without issuing
2072 a PTRACE_CONT), it would still be problematic to leave this
2073 syscall enter pending, as later when the thread is resumed,
2074 it would then see the same syscall exit mentioned above,
2075 followed by the delayed SIGSTOP, while the syscall didn't
2076 actually get to execute. It seems it would be even more
2077 confusing to the user. */
2079 if (debug_linux_nat
)
2080 fprintf_unfiltered (gdb_stdlog
,
2081 "LHST: ignoring syscall %d "
2082 "for LWP %ld (stopping threads), "
2083 "resuming with PTRACE_CONT for SIGSTOP\n",
2085 GET_LWP (lp
->ptid
));
2087 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2088 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2092 if (catch_syscall_enabled ())
2094 /* Always update the entry/return state, even if this particular
2095 syscall isn't interesting to the core now. In async mode,
2096 the user could install a new catchpoint for this syscall
2097 between syscall enter/return, and we'll need to know to
2098 report a syscall return if that happens. */
2099 lp
->syscall_state
= (lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2100 ? TARGET_WAITKIND_SYSCALL_RETURN
2101 : TARGET_WAITKIND_SYSCALL_ENTRY
);
2103 if (catching_syscall_number (syscall_number
))
2105 /* Alright, an event to report. */
2106 ourstatus
->kind
= lp
->syscall_state
;
2107 ourstatus
->value
.syscall_number
= syscall_number
;
2109 if (debug_linux_nat
)
2110 fprintf_unfiltered (gdb_stdlog
,
2111 "LHST: stopping for %s of syscall %d"
2114 == TARGET_WAITKIND_SYSCALL_ENTRY
2115 ? "entry" : "return",
2117 GET_LWP (lp
->ptid
));
2121 if (debug_linux_nat
)
2122 fprintf_unfiltered (gdb_stdlog
,
2123 "LHST: ignoring %s of syscall %d "
2125 lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2126 ? "entry" : "return",
2128 GET_LWP (lp
->ptid
));
2132 /* If we had been syscall tracing, and hence used PT_SYSCALL
2133 before on this LWP, it could happen that the user removes all
2134 syscall catchpoints before we get to process this event.
2135 There are two noteworthy issues here:
2137 - When stopped at a syscall entry event, resuming with
2138 PT_STEP still resumes executing the syscall and reports a
2141 - Only PT_SYSCALL catches syscall enters. If we last
2142 single-stepped this thread, then this event can't be a
2143 syscall enter. If we last single-stepped this thread, this
2144 has to be a syscall exit.
2146 The points above mean that the next resume, be it PT_STEP or
2147 PT_CONTINUE, can not trigger a syscall trace event. */
2148 if (debug_linux_nat
)
2149 fprintf_unfiltered (gdb_stdlog
,
2150 "LHST: caught syscall event "
2151 "with no syscall catchpoints."
2152 " %d for LWP %ld, ignoring\n",
2154 GET_LWP (lp
->ptid
));
2155 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2158 /* The core isn't interested in this event. For efficiency, avoid
2159 stopping all threads only to have the core resume them all again.
2160 Since we're not stopping threads, if we're still syscall tracing
2161 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
2162 subsequent syscall. Simply resume using the inf-ptrace layer,
2163 which knows when to use PT_SYSCALL or PT_CONTINUE. */
2165 /* Note that gdbarch_get_syscall_number may access registers, hence
2167 registers_changed ();
2168 if (linux_nat_prepare_to_resume
!= NULL
)
2169 linux_nat_prepare_to_resume (lp
);
2170 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2171 lp
->step
, TARGET_SIGNAL_0
);
2175 /* Handle a GNU/Linux extended wait response. If we see a clone
2176 event, we need to add the new LWP to our list (and not report the
2177 trap to higher layers). This function returns non-zero if the
2178 event should be ignored and we should wait again. If STOPPING is
2179 true, the new LWP remains stopped, otherwise it is continued. */
2182 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
2185 int pid
= GET_LWP (lp
->ptid
);
2186 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
2187 int event
= status
>> 16;
2189 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
2190 || event
== PTRACE_EVENT_CLONE
)
2192 unsigned long new_pid
;
2195 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
2197 /* If we haven't already seen the new PID stop, wait for it now. */
2198 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
2200 /* The new child has a pending SIGSTOP. We can't affect it until it
2201 hits the SIGSTOP, but we're already attached. */
2202 ret
= my_waitpid (new_pid
, &status
,
2203 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
2205 perror_with_name (_("waiting for new child"));
2206 else if (ret
!= new_pid
)
2207 internal_error (__FILE__
, __LINE__
,
2208 _("wait returned unexpected PID %d"), ret
);
2209 else if (!WIFSTOPPED (status
))
2210 internal_error (__FILE__
, __LINE__
,
2211 _("wait returned unexpected status 0x%x"), status
);
2214 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
2216 if (event
== PTRACE_EVENT_FORK
2217 && linux_fork_checkpointing_p (GET_PID (lp
->ptid
)))
2219 /* Handle checkpointing by linux-fork.c here as a special
2220 case. We don't want the follow-fork-mode or 'catch fork'
2221 to interfere with this. */
2223 /* This won't actually modify the breakpoint list, but will
2224 physically remove the breakpoints from the child. */
2225 detach_breakpoints (new_pid
);
2227 /* Retain child fork in ptrace (stopped) state. */
2228 if (!find_fork_pid (new_pid
))
2231 /* Report as spurious, so that infrun doesn't want to follow
2232 this fork. We're actually doing an infcall in
2234 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
2235 linux_enable_event_reporting (pid_to_ptid (new_pid
));
2237 /* Report the stop to the core. */
2241 if (event
== PTRACE_EVENT_FORK
)
2242 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
2243 else if (event
== PTRACE_EVENT_VFORK
)
2244 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
2247 struct lwp_info
*new_lp
;
2249 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2251 if (debug_linux_nat
)
2252 fprintf_unfiltered (gdb_stdlog
,
2253 "LHEW: Got clone event "
2254 "from LWP %d, new child is LWP %ld\n",
2257 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (lp
->ptid
)));
2259 new_lp
->stopped
= 1;
2261 if (WSTOPSIG (status
) != SIGSTOP
)
2263 /* This can happen if someone starts sending signals to
2264 the new thread before it gets a chance to run, which
2265 have a lower number than SIGSTOP (e.g. SIGUSR1).
2266 This is an unlikely case, and harder to handle for
2267 fork / vfork than for clone, so we do not try - but
2268 we handle it for clone events here. We'll send
2269 the other signal on to the thread below. */
2271 new_lp
->signalled
= 1;
2275 struct thread_info
*tp
;
2277 /* When we stop for an event in some other thread, and
2278 pull the thread list just as this thread has cloned,
2279 we'll have seen the new thread in the thread_db list
2280 before handling the CLONE event (glibc's
2281 pthread_create adds the new thread to the thread list
2282 before clone'ing, and has the kernel fill in the
2283 thread's tid on the clone call with
2284 CLONE_PARENT_SETTID). If that happened, and the core
2285 had requested the new thread to stop, we'll have
2286 killed it with SIGSTOP. But since SIGSTOP is not an
2287 RT signal, it can only be queued once. We need to be
2288 careful to not resume the LWP if we wanted it to
2289 stop. In that case, we'll leave the SIGSTOP pending.
2290 It will later be reported as TARGET_SIGNAL_0. */
2291 tp
= find_thread_ptid (new_lp
->ptid
);
2292 if (tp
!= NULL
&& tp
->stop_requested
)
2293 new_lp
->last_resume_kind
= resume_stop
;
2300 /* Add the new thread to GDB's lists as soon as possible
2303 1) the frontend doesn't have to wait for a stop to
2306 2) we tag it with the correct running state. */
2308 /* If the thread_db layer is active, let it know about
2309 this new thread, and add it to GDB's list. */
2310 if (!thread_db_attach_lwp (new_lp
->ptid
))
2312 /* We're not using thread_db. Add it to GDB's
2314 target_post_attach (GET_LWP (new_lp
->ptid
));
2315 add_thread (new_lp
->ptid
);
2320 set_running (new_lp
->ptid
, 1);
2321 set_executing (new_lp
->ptid
, 1);
2322 /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
2324 new_lp
->last_resume_kind
= resume_continue
;
2330 /* We created NEW_LP so it cannot yet contain STATUS. */
2331 gdb_assert (new_lp
->status
== 0);
2333 /* Save the wait status to report later. */
2334 if (debug_linux_nat
)
2335 fprintf_unfiltered (gdb_stdlog
,
2336 "LHEW: waitpid of new LWP %ld, "
2337 "saving status %s\n",
2338 (long) GET_LWP (new_lp
->ptid
),
2339 status_to_str (status
));
2340 new_lp
->status
= status
;
2343 /* Note the need to use the low target ops to resume, to
2344 handle resuming with PT_SYSCALL if we have syscall
2348 new_lp
->resumed
= 1;
2352 gdb_assert (new_lp
->last_resume_kind
== resume_continue
);
2353 if (debug_linux_nat
)
2354 fprintf_unfiltered (gdb_stdlog
,
2355 "LHEW: resuming new LWP %ld\n",
2356 GET_LWP (new_lp
->ptid
));
2357 if (linux_nat_prepare_to_resume
!= NULL
)
2358 linux_nat_prepare_to_resume (new_lp
);
2359 linux_ops
->to_resume (linux_ops
, pid_to_ptid (new_pid
),
2360 0, TARGET_SIGNAL_0
);
2361 new_lp
->stopped
= 0;
2365 if (debug_linux_nat
)
2366 fprintf_unfiltered (gdb_stdlog
,
2367 "LHEW: resuming parent LWP %d\n", pid
);
2368 if (linux_nat_prepare_to_resume
!= NULL
)
2369 linux_nat_prepare_to_resume (lp
);
2370 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2371 0, TARGET_SIGNAL_0
);
2379 if (event
== PTRACE_EVENT_EXEC
)
2381 if (debug_linux_nat
)
2382 fprintf_unfiltered (gdb_stdlog
,
2383 "LHEW: Got exec event from LWP %ld\n",
2384 GET_LWP (lp
->ptid
));
2386 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
2387 ourstatus
->value
.execd_pathname
2388 = xstrdup (linux_child_pid_to_exec_file (pid
));
2393 if (event
== PTRACE_EVENT_VFORK_DONE
)
2395 if (current_inferior ()->waiting_for_vfork_done
)
2397 if (debug_linux_nat
)
2398 fprintf_unfiltered (gdb_stdlog
,
2399 "LHEW: Got expected PTRACE_EVENT_"
2400 "VFORK_DONE from LWP %ld: stopping\n",
2401 GET_LWP (lp
->ptid
));
2403 ourstatus
->kind
= TARGET_WAITKIND_VFORK_DONE
;
2407 if (debug_linux_nat
)
2408 fprintf_unfiltered (gdb_stdlog
,
2409 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2410 "from LWP %ld: resuming\n",
2411 GET_LWP (lp
->ptid
));
2412 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2416 internal_error (__FILE__
, __LINE__
,
2417 _("unknown ptrace event %d"), event
);
2420 /* Return non-zero if LWP is a zombie. */
2423 linux_lwp_is_zombie (long lwp
)
2425 char buffer
[MAXPATHLEN
];
2430 xsnprintf (buffer
, sizeof (buffer
), "/proc/%ld/status", lwp
);
2431 procfile
= fopen (buffer
, "r");
2432 if (procfile
== NULL
)
2434 warning (_("unable to open /proc file '%s'"), buffer
);
2439 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
2440 if (strncmp (buffer
, "State:", 6) == 0)
2445 retval
= (have_state
2446 && strcmp (buffer
, "State:\tZ (zombie)\n") == 0);
2451 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2455 wait_lwp (struct lwp_info
*lp
)
2459 int thread_dead
= 0;
2462 gdb_assert (!lp
->stopped
);
2463 gdb_assert (lp
->status
== 0);
2465 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2466 block_child_signals (&prev_mask
);
2470 /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2471 was right and we should just call sigsuspend. */
2473 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, WNOHANG
);
2474 if (pid
== -1 && errno
== ECHILD
)
2475 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
| WNOHANG
);
2476 if (pid
== -1 && errno
== ECHILD
)
2478 /* The thread has previously exited. We need to delete it
2479 now because, for some vendor 2.4 kernels with NPTL
2480 support backported, there won't be an exit event unless
2481 it is the main thread. 2.6 kernels will report an exit
2482 event for each thread that exits, as expected. */
2484 if (debug_linux_nat
)
2485 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2486 target_pid_to_str (lp
->ptid
));
2491 /* Bugs 10970, 12702.
2492 Thread group leader may have exited in which case we'll lock up in
2493 waitpid if there are other threads, even if they are all zombies too.
2494 Basically, we're not supposed to use waitpid this way.
2495 __WCLONE is not applicable for the leader so we can't use that.
2496 LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2497 process; it gets ESRCH both for the zombie and for running processes.
2499 As a workaround, check if we're waiting for the thread group leader and
2500 if it's a zombie, and avoid calling waitpid if it is.
2502 This is racy, what if the tgl becomes a zombie right after we check?
2503 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2504 waiting waitpid but the linux_lwp_is_zombie is safe this way. */
2506 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
)
2507 && linux_lwp_is_zombie (GET_LWP (lp
->ptid
)))
2510 if (debug_linux_nat
)
2511 fprintf_unfiltered (gdb_stdlog
,
2512 "WL: Thread group leader %s vanished.\n",
2513 target_pid_to_str (lp
->ptid
));
2517 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2518 get invoked despite our caller had them intentionally blocked by
2519 block_child_signals. This is sensitive only to the loop of
2520 linux_nat_wait_1 and there if we get called my_waitpid gets called
2521 again before it gets to sigsuspend so we can safely let the handlers
2522 get executed here. */
2524 sigsuspend (&suspend_mask
);
2527 restore_child_signals_mask (&prev_mask
);
2531 gdb_assert (pid
== GET_LWP (lp
->ptid
));
2533 if (debug_linux_nat
)
2535 fprintf_unfiltered (gdb_stdlog
,
2536 "WL: waitpid %s received %s\n",
2537 target_pid_to_str (lp
->ptid
),
2538 status_to_str (status
));
2541 /* Check if the thread has exited. */
2542 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2545 if (debug_linux_nat
)
2546 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2547 target_pid_to_str (lp
->ptid
));
2557 gdb_assert (WIFSTOPPED (status
));
2559 /* Handle GNU/Linux's syscall SIGTRAPs. */
2560 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2562 /* No longer need the sysgood bit. The ptrace event ends up
2563 recorded in lp->waitstatus if we care for it. We can carry
2564 on handling the event like a regular SIGTRAP from here
2566 status
= W_STOPCODE (SIGTRAP
);
2567 if (linux_handle_syscall_trap (lp
, 1))
2568 return wait_lwp (lp
);
2571 /* Handle GNU/Linux's extended waitstatus for trace events. */
2572 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2574 if (debug_linux_nat
)
2575 fprintf_unfiltered (gdb_stdlog
,
2576 "WL: Handling extended status 0x%06x\n",
2578 if (linux_handle_extended_wait (lp
, status
, 1))
2579 return wait_lwp (lp
);
2585 /* Save the most recent siginfo for LP. This is currently only called
2586 for SIGTRAP; some ports use the si_addr field for
2587 target_stopped_data_address. In the future, it may also be used to
2588 restore the siginfo of requeued signals. */
2591 save_siginfo (struct lwp_info
*lp
)
2594 ptrace (PTRACE_GETSIGINFO
, GET_LWP (lp
->ptid
),
2595 (PTRACE_TYPE_ARG3
) 0, &lp
->siginfo
);
2598 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
2601 /* Send a SIGSTOP to LP. */
2604 stop_callback (struct lwp_info
*lp
, void *data
)
2606 if (!lp
->stopped
&& !lp
->signalled
)
2610 if (debug_linux_nat
)
2612 fprintf_unfiltered (gdb_stdlog
,
2613 "SC: kill %s **<SIGSTOP>**\n",
2614 target_pid_to_str (lp
->ptid
));
2617 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
2618 if (debug_linux_nat
)
2620 fprintf_unfiltered (gdb_stdlog
,
2621 "SC: lwp kill %d %s\n",
2623 errno
? safe_strerror (errno
) : "ERRNO-OK");
2627 gdb_assert (lp
->status
== 0);
2633 /* Request a stop on LWP. */
2636 linux_stop_lwp (struct lwp_info
*lwp
)
2638 stop_callback (lwp
, NULL
);
2641 /* Return non-zero if LWP PID has a pending SIGINT. */
2644 linux_nat_has_pending_sigint (int pid
)
2646 sigset_t pending
, blocked
, ignored
;
2648 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2650 if (sigismember (&pending
, SIGINT
)
2651 && !sigismember (&ignored
, SIGINT
))
2657 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2660 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2662 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2663 flag to consume the next one. */
2664 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2665 && WSTOPSIG (lp
->status
) == SIGINT
)
2668 lp
->ignore_sigint
= 1;
2673 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2674 This function is called after we know the LWP has stopped; if the LWP
2675 stopped before the expected SIGINT was delivered, then it will never have
2676 arrived. Also, if the signal was delivered to a shared queue and consumed
2677 by a different thread, it will never be delivered to this LWP. */
2680 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2682 if (!lp
->ignore_sigint
)
2685 if (!linux_nat_has_pending_sigint (GET_LWP (lp
->ptid
)))
2687 if (debug_linux_nat
)
2688 fprintf_unfiltered (gdb_stdlog
,
2689 "MCIS: Clearing bogus flag for %s\n",
2690 target_pid_to_str (lp
->ptid
));
2691 lp
->ignore_sigint
= 0;
2695 /* Fetch the possible triggered data watchpoint info and store it in
2698 On some archs, like x86, that use debug registers to set
2699 watchpoints, it's possible that the way to know which watched
2700 address trapped, is to check the register that is used to select
2701 which address to watch. Problem is, between setting the watchpoint
2702 and reading back which data address trapped, the user may change
2703 the set of watchpoints, and, as a consequence, GDB changes the
2704 debug registers in the inferior. To avoid reading back a stale
2705 stopped-data-address when that happens, we cache in LP the fact
2706 that a watchpoint trapped, and the corresponding data address, as
2707 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2708 registers meanwhile, we have the cached data we can rely on. */
2711 save_sigtrap (struct lwp_info
*lp
)
2713 struct cleanup
*old_chain
;
2715 if (linux_ops
->to_stopped_by_watchpoint
== NULL
)
2717 lp
->stopped_by_watchpoint
= 0;
2721 old_chain
= save_inferior_ptid ();
2722 inferior_ptid
= lp
->ptid
;
2724 lp
->stopped_by_watchpoint
= linux_ops
->to_stopped_by_watchpoint ();
2726 if (lp
->stopped_by_watchpoint
)
2728 if (linux_ops
->to_stopped_data_address
!= NULL
)
2729 lp
->stopped_data_address_p
=
2730 linux_ops
->to_stopped_data_address (¤t_target
,
2731 &lp
->stopped_data_address
);
2733 lp
->stopped_data_address_p
= 0;
2736 do_cleanups (old_chain
);
2739 /* See save_sigtrap. */
2742 linux_nat_stopped_by_watchpoint (void)
2744 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2746 gdb_assert (lp
!= NULL
);
2748 return lp
->stopped_by_watchpoint
;
2752 linux_nat_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
2754 struct lwp_info
*lp
= find_lwp_pid (inferior_ptid
);
2756 gdb_assert (lp
!= NULL
);
2758 *addr_p
= lp
->stopped_data_address
;
2760 return lp
->stopped_data_address_p
;
2763 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2766 sigtrap_is_event (int status
)
2768 return WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
;
2771 /* SIGTRAP-like events recognizer. */
2773 static int (*linux_nat_status_is_event
) (int status
) = sigtrap_is_event
;
2775 /* Check for SIGTRAP-like events in LP. */
2778 linux_nat_lp_status_is_event (struct lwp_info
*lp
)
2780 /* We check for lp->waitstatus in addition to lp->status, because we can
2781 have pending process exits recorded in lp->status
2782 and W_EXITCODE(0,0) == 0. We should probably have an additional
2783 lp->status_p flag. */
2785 return (lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
2786 && linux_nat_status_is_event (lp
->status
));
2789 /* Set alternative SIGTRAP-like events recognizer. If
2790 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2794 linux_nat_set_status_is_event (struct target_ops
*t
,
2795 int (*status_is_event
) (int status
))
2797 linux_nat_status_is_event
= status_is_event
;
2800 /* Wait until LP is stopped. */
2803 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2805 struct inferior
*inf
= find_inferior_pid (GET_PID (lp
->ptid
));
2807 /* If this is a vfork parent, bail out, it is not going to report
2808 any SIGSTOP until the vfork is done with. */
2809 if (inf
->vfork_child
!= NULL
)
2816 status
= wait_lwp (lp
);
2820 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2821 && WSTOPSIG (status
) == SIGINT
)
2823 lp
->ignore_sigint
= 0;
2826 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2827 if (debug_linux_nat
)
2828 fprintf_unfiltered (gdb_stdlog
,
2829 "PTRACE_CONT %s, 0, 0 (%s) "
2830 "(discarding SIGINT)\n",
2831 target_pid_to_str (lp
->ptid
),
2832 errno
? safe_strerror (errno
) : "OK");
2834 return stop_wait_callback (lp
, NULL
);
2837 maybe_clear_ignore_sigint (lp
);
2839 if (WSTOPSIG (status
) != SIGSTOP
)
2841 if (linux_nat_status_is_event (status
))
2843 /* If a LWP other than the LWP that we're reporting an
2844 event for has hit a GDB breakpoint (as opposed to
2845 some random trap signal), then just arrange for it to
2846 hit it again later. We don't keep the SIGTRAP status
2847 and don't forward the SIGTRAP signal to the LWP. We
2848 will handle the current event, eventually we will
2849 resume all LWPs, and this one will get its breakpoint
2852 If we do not do this, then we run the risk that the
2853 user will delete or disable the breakpoint, but the
2854 thread will have already tripped on it. */
2856 /* Save the trap's siginfo in case we need it later. */
2861 /* Now resume this LWP and get the SIGSTOP event. */
2863 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2864 if (debug_linux_nat
)
2866 fprintf_unfiltered (gdb_stdlog
,
2867 "PTRACE_CONT %s, 0, 0 (%s)\n",
2868 target_pid_to_str (lp
->ptid
),
2869 errno
? safe_strerror (errno
) : "OK");
2871 fprintf_unfiltered (gdb_stdlog
,
2872 "SWC: Candidate SIGTRAP event in %s\n",
2873 target_pid_to_str (lp
->ptid
));
2875 /* Hold this event/waitstatus while we check to see if
2876 there are any more (we still want to get that SIGSTOP). */
2877 stop_wait_callback (lp
, NULL
);
2879 /* Hold the SIGTRAP for handling by linux_nat_wait. If
2880 there's another event, throw it back into the
2884 if (debug_linux_nat
)
2885 fprintf_unfiltered (gdb_stdlog
,
2886 "SWC: kill %s, %s\n",
2887 target_pid_to_str (lp
->ptid
),
2888 status_to_str ((int) status
));
2889 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
2892 /* Save the sigtrap event. */
2893 lp
->status
= status
;
2898 /* The thread was stopped with a signal other than
2899 SIGSTOP, and didn't accidentally trip a breakpoint. */
2901 if (debug_linux_nat
)
2903 fprintf_unfiltered (gdb_stdlog
,
2904 "SWC: Pending event %s in %s\n",
2905 status_to_str ((int) status
),
2906 target_pid_to_str (lp
->ptid
));
2908 /* Now resume this LWP and get the SIGSTOP event. */
2910 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2911 if (debug_linux_nat
)
2912 fprintf_unfiltered (gdb_stdlog
,
2913 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2914 target_pid_to_str (lp
->ptid
),
2915 errno
? safe_strerror (errno
) : "OK");
2917 /* Hold this event/waitstatus while we check to see if
2918 there are any more (we still want to get that SIGSTOP). */
2919 stop_wait_callback (lp
, NULL
);
2921 /* If the lp->status field is still empty, use it to
2922 hold this event. If not, then this event must be
2923 returned to the event queue of the LWP. */
2926 if (debug_linux_nat
)
2928 fprintf_unfiltered (gdb_stdlog
,
2929 "SWC: kill %s, %s\n",
2930 target_pid_to_str (lp
->ptid
),
2931 status_to_str ((int) status
));
2933 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
2936 lp
->status
= status
;
2942 /* We caught the SIGSTOP that we intended to catch, so
2943 there's no SIGSTOP pending. */
2952 /* Return non-zero if LP has a wait status pending. */
2955 status_callback (struct lwp_info
*lp
, void *data
)
2957 /* Only report a pending wait status if we pretend that this has
2958 indeed been resumed. */
2962 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2964 /* A ptrace event, like PTRACE_FORK|VFORK|EXEC, syscall event,
2965 or a pending process exit. Note that `W_EXITCODE(0,0) ==
2966 0', so a clean process exit can not be stored pending in
2967 lp->status, it is indistinguishable from
2968 no-pending-status. */
2972 if (lp
->status
!= 0)
2978 /* Return non-zero if LP isn't stopped. */
2981 running_callback (struct lwp_info
*lp
, void *data
)
2983 return (!lp
->stopped
2984 || ((lp
->status
!= 0
2985 || lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2989 /* Count the LWP's that have had events. */
2992 count_events_callback (struct lwp_info
*lp
, void *data
)
2996 gdb_assert (count
!= NULL
);
2998 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2999 if (lp
->resumed
&& linux_nat_lp_status_is_event (lp
))
3005 /* Select the LWP (if any) that is currently being single-stepped. */
3008 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
3010 if (lp
->last_resume_kind
== resume_step
3017 /* Select the Nth LWP that has had a SIGTRAP event. */
3020 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
3022 int *selector
= data
;
3024 gdb_assert (selector
!= NULL
);
3026 /* Select only resumed LWPs that have a SIGTRAP event pending. */
3027 if (lp
->resumed
&& linux_nat_lp_status_is_event (lp
))
3028 if ((*selector
)-- == 0)
3035 cancel_breakpoint (struct lwp_info
*lp
)
3037 /* Arrange for a breakpoint to be hit again later. We don't keep
3038 the SIGTRAP status and don't forward the SIGTRAP signal to the
3039 LWP. We will handle the current event, eventually we will resume
3040 this LWP, and this breakpoint will trap again.
3042 If we do not do this, then we run the risk that the user will
3043 delete or disable the breakpoint, but the LWP will have already
3046 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3047 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3050 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
3051 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
3053 if (debug_linux_nat
)
3054 fprintf_unfiltered (gdb_stdlog
,
3055 "CB: Push back breakpoint for %s\n",
3056 target_pid_to_str (lp
->ptid
));
3058 /* Back up the PC if necessary. */
3059 if (gdbarch_decr_pc_after_break (gdbarch
))
3060 regcache_write_pc (regcache
, pc
);
3068 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
3070 struct lwp_info
*event_lp
= data
;
3072 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
3076 /* If a LWP other than the LWP that we're reporting an event for has
3077 hit a GDB breakpoint (as opposed to some random trap signal),
3078 then just arrange for it to hit it again later. We don't keep
3079 the SIGTRAP status and don't forward the SIGTRAP signal to the
3080 LWP. We will handle the current event, eventually we will resume
3081 all LWPs, and this one will get its breakpoint trap again.
3083 If we do not do this, then we run the risk that the user will
3084 delete or disable the breakpoint, but the LWP will have already
3087 if (linux_nat_lp_status_is_event (lp
)
3088 && cancel_breakpoint (lp
))
3089 /* Throw away the SIGTRAP. */
3095 /* Select one LWP out of those that have events pending. */
3098 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
3101 int random_selector
;
3102 struct lwp_info
*event_lp
;
3104 /* Record the wait status for the original LWP. */
3105 (*orig_lp
)->status
= *status
;
3107 /* Give preference to any LWP that is being single-stepped. */
3108 event_lp
= iterate_over_lwps (filter
,
3109 select_singlestep_lwp_callback
, NULL
);
3110 if (event_lp
!= NULL
)
3112 if (debug_linux_nat
)
3113 fprintf_unfiltered (gdb_stdlog
,
3114 "SEL: Select single-step %s\n",
3115 target_pid_to_str (event_lp
->ptid
));
3119 /* No single-stepping LWP. Select one at random, out of those
3120 which have had SIGTRAP events. */
3122 /* First see how many SIGTRAP events we have. */
3123 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
3125 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
3126 random_selector
= (int)
3127 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
3129 if (debug_linux_nat
&& num_events
> 1)
3130 fprintf_unfiltered (gdb_stdlog
,
3131 "SEL: Found %d SIGTRAP events, selecting #%d\n",
3132 num_events
, random_selector
);
3134 event_lp
= iterate_over_lwps (filter
,
3135 select_event_lwp_callback
,
3139 if (event_lp
!= NULL
)
3141 /* Switch the event LWP. */
3142 *orig_lp
= event_lp
;
3143 *status
= event_lp
->status
;
3146 /* Flush the wait status for the event LWP. */
3147 (*orig_lp
)->status
= 0;
3150 /* Return non-zero if LP has been resumed. */
3153 resumed_callback (struct lwp_info
*lp
, void *data
)
3158 /* Stop an active thread, verify it still exists, then resume it. If
3159 the thread ends up with a pending status, then it is not resumed,
3160 and *DATA (really a pointer to int), is set. */
3163 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
3165 int *new_pending_p
= data
;
3169 ptid_t ptid
= lp
->ptid
;
3171 stop_callback (lp
, NULL
);
3172 stop_wait_callback (lp
, NULL
);
3174 /* Resume if the lwp still exists, and the core wanted it
3176 lp
= find_lwp_pid (ptid
);
3179 if (lp
->last_resume_kind
== resume_stop
3182 /* The core wanted the LWP to stop. Even if it stopped
3183 cleanly (with SIGSTOP), leave the event pending. */
3184 if (debug_linux_nat
)
3185 fprintf_unfiltered (gdb_stdlog
,
3186 "SARC: core wanted LWP %ld stopped "
3187 "(leaving SIGSTOP pending)\n",
3188 GET_LWP (lp
->ptid
));
3189 lp
->status
= W_STOPCODE (SIGSTOP
);
3192 if (lp
->status
== 0)
3194 if (debug_linux_nat
)
3195 fprintf_unfiltered (gdb_stdlog
,
3196 "SARC: re-resuming LWP %ld\n",
3197 GET_LWP (lp
->ptid
));
3198 resume_lwp (lp
, lp
->step
);
3202 if (debug_linux_nat
)
3203 fprintf_unfiltered (gdb_stdlog
,
3204 "SARC: not re-resuming LWP %ld "
3206 GET_LWP (lp
->ptid
));
3215 /* Check if we should go on and pass this event to common code.
3216 Return the affected lwp if we are, or NULL otherwise. If we stop
3217 all lwps temporarily, we may end up with new pending events in some
3218 other lwp. In that case set *NEW_PENDING_P to true. */
3220 static struct lwp_info
*
3221 linux_nat_filter_event (int lwpid
, int status
, int *new_pending_p
)
3223 struct lwp_info
*lp
;
3227 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
3229 /* Check for stop events reported by a process we didn't already
3230 know about - anything not already in our LWP list.
3232 If we're expecting to receive stopped processes after
3233 fork, vfork, and clone events, then we'll just add the
3234 new one to our list and go back to waiting for the event
3235 to be reported - the stopped process might be returned
3236 from waitpid before or after the event is.
3238 But note the case of a non-leader thread exec'ing after the
3239 leader having exited, and gone from our lists. The non-leader
3240 thread changes its tid to the tgid. */
3242 if (WIFSTOPPED (status
) && lp
== NULL
3243 && (WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 == PTRACE_EVENT_EXEC
))
3245 /* A multi-thread exec after we had seen the leader exiting. */
3246 if (debug_linux_nat
)
3247 fprintf_unfiltered (gdb_stdlog
,
3248 "LLW: Re-adding thread group leader LWP %d.\n",
3251 lp
= add_lwp (BUILD_LWP (lwpid
, lwpid
));
3254 add_thread (lp
->ptid
);
3257 if (WIFSTOPPED (status
) && !lp
)
3259 add_to_pid_list (&stopped_pids
, lwpid
, status
);
3263 /* Make sure we don't report an event for the exit of an LWP not in
3264 our list, i.e. not part of the current process. This can happen
3265 if we detach from a program we originally forked and then it
3267 if (!WIFSTOPPED (status
) && !lp
)
3270 /* Handle GNU/Linux's syscall SIGTRAPs. */
3271 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
3273 /* No longer need the sysgood bit. The ptrace event ends up
3274 recorded in lp->waitstatus if we care for it. We can carry
3275 on handling the event like a regular SIGTRAP from here
3277 status
= W_STOPCODE (SIGTRAP
);
3278 if (linux_handle_syscall_trap (lp
, 0))
3282 /* Handle GNU/Linux's extended waitstatus for trace events. */
3283 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
3285 if (debug_linux_nat
)
3286 fprintf_unfiltered (gdb_stdlog
,
3287 "LLW: Handling extended status 0x%06x\n",
3289 if (linux_handle_extended_wait (lp
, status
, 0))
3293 if (linux_nat_status_is_event (status
))
3295 /* Save the trap's siginfo in case we need it later. */
3301 /* Check if the thread has exited. */
3302 if ((WIFEXITED (status
) || WIFSIGNALED (status
))
3303 && num_lwps (GET_PID (lp
->ptid
)) > 1)
3305 /* If this is the main thread, we must stop all threads and verify
3306 if they are still alive. This is because in the nptl thread model
3307 on Linux 2.4, there is no signal issued for exiting LWPs
3308 other than the main thread. We only get the main thread exit
3309 signal once all child threads have already exited. If we
3310 stop all the threads and use the stop_wait_callback to check
3311 if they have exited we can determine whether this signal
3312 should be ignored or whether it means the end of the debugged
3313 application, regardless of which threading model is being
3315 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
3318 iterate_over_lwps (pid_to_ptid (GET_PID (lp
->ptid
)),
3319 stop_and_resume_callback
, new_pending_p
);
3322 if (debug_linux_nat
)
3323 fprintf_unfiltered (gdb_stdlog
,
3324 "LLW: %s exited.\n",
3325 target_pid_to_str (lp
->ptid
));
3327 if (num_lwps (GET_PID (lp
->ptid
)) > 1)
3329 /* If there is at least one more LWP, then the exit signal
3330 was not the end of the debugged application and should be
3337 /* Check if the current LWP has previously exited. In the nptl
3338 thread model, LWPs other than the main thread do not issue
3339 signals when they exit so we must check whenever the thread has
3340 stopped. A similar check is made in stop_wait_callback(). */
3341 if (num_lwps (GET_PID (lp
->ptid
)) > 1 && !linux_thread_alive (lp
->ptid
))
3343 ptid_t ptid
= pid_to_ptid (GET_PID (lp
->ptid
));
3345 if (debug_linux_nat
)
3346 fprintf_unfiltered (gdb_stdlog
,
3347 "LLW: %s exited.\n",
3348 target_pid_to_str (lp
->ptid
));
3352 /* Make sure there is at least one thread running. */
3353 gdb_assert (iterate_over_lwps (ptid
, running_callback
, NULL
));
3355 /* Discard the event. */
3359 /* Make sure we don't report a SIGSTOP that we sent ourselves in
3360 an attempt to stop an LWP. */
3362 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
3364 if (debug_linux_nat
)
3365 fprintf_unfiltered (gdb_stdlog
,
3366 "LLW: Delayed SIGSTOP caught for %s.\n",
3367 target_pid_to_str (lp
->ptid
));
3371 if (lp
->last_resume_kind
!= resume_stop
)
3373 /* This is a delayed SIGSTOP. */
3375 registers_changed ();
3377 if (linux_nat_prepare_to_resume
!= NULL
)
3378 linux_nat_prepare_to_resume (lp
);
3379 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3380 lp
->step
, TARGET_SIGNAL_0
);
3381 if (debug_linux_nat
)
3382 fprintf_unfiltered (gdb_stdlog
,
3383 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
3385 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3386 target_pid_to_str (lp
->ptid
));
3389 gdb_assert (lp
->resumed
);
3391 /* Discard the event. */
3396 /* Make sure we don't report a SIGINT that we have already displayed
3397 for another thread. */
3398 if (lp
->ignore_sigint
3399 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
3401 if (debug_linux_nat
)
3402 fprintf_unfiltered (gdb_stdlog
,
3403 "LLW: Delayed SIGINT caught for %s.\n",
3404 target_pid_to_str (lp
->ptid
));
3406 /* This is a delayed SIGINT. */
3407 lp
->ignore_sigint
= 0;
3409 registers_changed ();
3410 if (linux_nat_prepare_to_resume
!= NULL
)
3411 linux_nat_prepare_to_resume (lp
);
3412 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3413 lp
->step
, TARGET_SIGNAL_0
);
3414 if (debug_linux_nat
)
3415 fprintf_unfiltered (gdb_stdlog
,
3416 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
3418 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3419 target_pid_to_str (lp
->ptid
));
3422 gdb_assert (lp
->resumed
);
3424 /* Discard the event. */
3428 /* An interesting event. */
3430 lp
->status
= status
;
3434 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3435 their exits until all other threads in the group have exited. */
3438 check_zombie_leaders (void)
3440 struct inferior
*inf
;
3444 struct lwp_info
*leader_lp
;
3449 leader_lp
= find_lwp_pid (pid_to_ptid (inf
->pid
));
3450 if (leader_lp
!= NULL
3451 /* Check if there are other threads in the group, as we may
3452 have raced with the inferior simply exiting. */
3453 && num_lwps (inf
->pid
) > 1
3454 && linux_lwp_is_zombie (inf
->pid
))
3456 if (debug_linux_nat
)
3457 fprintf_unfiltered (gdb_stdlog
,
3458 "CZL: Thread group leader %d zombie "
3459 "(it exited, or another thread execd).\n",
3462 /* A leader zombie can mean one of two things:
3464 - It exited, and there's an exit status pending
3465 available, or only the leader exited (not the whole
3466 program). In the latter case, we can't waitpid the
3467 leader's exit status until all other threads are gone.
3469 - There are 3 or more threads in the group, and a thread
3470 other than the leader exec'd. On an exec, the Linux
3471 kernel destroys all other threads (except the execing
3472 one) in the thread group, and resets the execing thread's
3473 tid to the tgid. No exit notification is sent for the
3474 execing thread -- from the ptracer's perspective, it
3475 appears as though the execing thread just vanishes.
3476 Until we reap all other threads except the leader and the
3477 execing thread, the leader will be zombie, and the
3478 execing thread will be in `D (disc sleep)'. As soon as
3479 all other threads are reaped, the execing thread changes
3480 it's tid to the tgid, and the previous (zombie) leader
3481 vanishes, giving place to the "new" leader. We could try
3482 distinguishing the exit and exec cases, by waiting once
3483 more, and seeing if something comes out, but it doesn't
3484 sound useful. The previous leader _does_ go away, and
3485 we'll re-add the new one once we see the exec event
3486 (which is just the same as what would happen if the
3487 previous leader did exit voluntarily before some other
3490 if (debug_linux_nat
)
3491 fprintf_unfiltered (gdb_stdlog
,
3492 "CZL: Thread group leader %d vanished.\n",
3494 exit_lwp (leader_lp
);
3500 linux_nat_wait_1 (struct target_ops
*ops
,
3501 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3504 static sigset_t prev_mask
;
3505 enum resume_kind last_resume_kind
;
3506 struct lwp_info
*lp
;
3509 if (debug_linux_nat
)
3510 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
3512 /* The first time we get here after starting a new inferior, we may
3513 not have added it to the LWP list yet - this is the earliest
3514 moment at which we know its PID. */
3515 if (ptid_is_pid (inferior_ptid
))
3517 /* Upgrade the main thread's ptid. */
3518 thread_change_ptid (inferior_ptid
,
3519 BUILD_LWP (GET_PID (inferior_ptid
),
3520 GET_PID (inferior_ptid
)));
3522 lp
= add_lwp (inferior_ptid
);
3526 /* Make sure SIGCHLD is blocked. */
3527 block_child_signals (&prev_mask
);
3533 /* First check if there is a LWP with a wait status pending. */
3534 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3536 /* Any LWP in the PTID group that's been resumed will do. */
3537 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3540 if (debug_linux_nat
&& lp
->status
)
3541 fprintf_unfiltered (gdb_stdlog
,
3542 "LLW: Using pending wait status %s for %s.\n",
3543 status_to_str (lp
->status
),
3544 target_pid_to_str (lp
->ptid
));
3547 else if (is_lwp (ptid
))
3549 if (debug_linux_nat
)
3550 fprintf_unfiltered (gdb_stdlog
,
3551 "LLW: Waiting for specific LWP %s.\n",
3552 target_pid_to_str (ptid
));
3554 /* We have a specific LWP to check. */
3555 lp
= find_lwp_pid (ptid
);
3558 if (debug_linux_nat
&& lp
->status
)
3559 fprintf_unfiltered (gdb_stdlog
,
3560 "LLW: Using pending wait status %s for %s.\n",
3561 status_to_str (lp
->status
),
3562 target_pid_to_str (lp
->ptid
));
3564 /* We check for lp->waitstatus in addition to lp->status,
3565 because we can have pending process exits recorded in
3566 lp->status and W_EXITCODE(0,0) == 0. We should probably have
3567 an additional lp->status_p flag. */
3568 if (lp
->status
== 0 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
3572 if (lp
&& lp
->signalled
&& lp
->last_resume_kind
!= resume_stop
)
3574 /* A pending SIGSTOP may interfere with the normal stream of
3575 events. In a typical case where interference is a problem,
3576 we have a SIGSTOP signal pending for LWP A while
3577 single-stepping it, encounter an event in LWP B, and take the
3578 pending SIGSTOP while trying to stop LWP A. After processing
3579 the event in LWP B, LWP A is continued, and we'll never see
3580 the SIGTRAP associated with the last time we were
3581 single-stepping LWP A. */
3583 /* Resume the thread. It should halt immediately returning the
3585 registers_changed ();
3586 if (linux_nat_prepare_to_resume
!= NULL
)
3587 linux_nat_prepare_to_resume (lp
);
3588 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3589 lp
->step
, TARGET_SIGNAL_0
);
3590 if (debug_linux_nat
)
3591 fprintf_unfiltered (gdb_stdlog
,
3592 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
3593 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3594 target_pid_to_str (lp
->ptid
));
3596 gdb_assert (lp
->resumed
);
3598 /* Catch the pending SIGSTOP. */
3599 status
= lp
->status
;
3602 stop_wait_callback (lp
, NULL
);
3604 /* If the lp->status field isn't empty, we caught another signal
3605 while flushing the SIGSTOP. Return it back to the event
3606 queue of the LWP, as we already have an event to handle. */
3609 if (debug_linux_nat
)
3610 fprintf_unfiltered (gdb_stdlog
,
3611 "LLW: kill %s, %s\n",
3612 target_pid_to_str (lp
->ptid
),
3613 status_to_str (lp
->status
));
3614 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
3617 lp
->status
= status
;
3620 if (!target_can_async_p ())
3622 /* Causes SIGINT to be passed on to the attached process. */
3626 /* But if we don't find a pending event, we'll have to wait. */
3632 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3635 - If the thread group leader exits while other threads in the
3636 thread group still exist, waitpid(TGID, ...) hangs. That
3637 waitpid won't return an exit status until the other threads
3638 in the group are reapped.
3640 - When a non-leader thread execs, that thread just vanishes
3641 without reporting an exit (so we'd hang if we waited for it
3642 explicitly in that case). The exec event is reported to
3646 lwpid
= my_waitpid (-1, &status
, __WCLONE
| WNOHANG
);
3647 if (lwpid
== 0 || (lwpid
== -1 && errno
== ECHILD
))
3648 lwpid
= my_waitpid (-1, &status
, WNOHANG
);
3650 if (debug_linux_nat
)
3651 fprintf_unfiltered (gdb_stdlog
,
3652 "LNW: waitpid(-1, ...) returned %d, %s\n",
3653 lwpid
, errno
? safe_strerror (errno
) : "ERRNO-OK");
3657 /* If this is true, then we paused LWPs momentarily, and may
3658 now have pending events to handle. */
3661 if (debug_linux_nat
)
3663 fprintf_unfiltered (gdb_stdlog
,
3664 "LLW: waitpid %ld received %s\n",
3665 (long) lwpid
, status_to_str (status
));
3668 lp
= linux_nat_filter_event (lwpid
, status
, &new_pending
);
3670 /* STATUS is now no longer valid, use LP->STATUS instead. */
3673 if (lp
&& !ptid_match (lp
->ptid
, ptid
))
3675 gdb_assert (lp
->resumed
);
3677 if (debug_linux_nat
)
3679 "LWP %ld got an event %06x, leaving pending.\n",
3680 ptid_get_lwp (lp
->ptid
), lp
->status
);
3682 if (WIFSTOPPED (lp
->status
))
3684 if (WSTOPSIG (lp
->status
) != SIGSTOP
)
3686 /* Cancel breakpoint hits. The breakpoint may
3687 be removed before we fetch events from this
3688 process to report to the core. It is best
3689 not to assume the moribund breakpoints
3690 heuristic always handles these cases --- it
3691 could be too many events go through to the
3692 core before this one is handled. All-stop
3693 always cancels breakpoint hits in all
3696 && linux_nat_lp_status_is_event (lp
)
3697 && cancel_breakpoint (lp
))
3699 /* Throw away the SIGTRAP. */
3702 if (debug_linux_nat
)
3704 "LLW: LWP %ld hit a breakpoint while"
3705 " waiting for another process;"
3707 ptid_get_lwp (lp
->ptid
));
3717 else if (WIFEXITED (lp
->status
) || WIFSIGNALED (lp
->status
))
3719 if (debug_linux_nat
)
3721 "Process %ld exited while stopping LWPs\n",
3722 ptid_get_lwp (lp
->ptid
));
3724 /* This was the last lwp in the process. Since
3725 events are serialized to GDB core, and we can't
3726 report this one right now, but GDB core and the
3727 other target layers will want to be notified
3728 about the exit code/signal, leave the status
3729 pending for the next time we're able to report
3732 /* Prevent trying to stop this thread again. We'll
3733 never try to resume it because it has a pending
3737 /* Dead LWP's aren't expected to reported a pending
3741 /* Store the pending event in the waitstatus as
3742 well, because W_EXITCODE(0,0) == 0. */
3743 store_waitstatus (&lp
->waitstatus
, lp
->status
);
3752 /* Some LWP now has a pending event. Go all the way
3753 back to check it. */
3759 /* We got an event to report to the core. */
3763 /* Retry until nothing comes out of waitpid. A single
3764 SIGCHLD can indicate more than one child stopped. */
3768 /* Check for zombie thread group leaders. Those can't be reaped
3769 until all other threads in the thread group are. */
3770 check_zombie_leaders ();
3772 /* If there are no resumed children left, bail. We'd be stuck
3773 forever in the sigsuspend call below otherwise. */
3774 if (iterate_over_lwps (ptid
, resumed_callback
, NULL
) == NULL
)
3776 if (debug_linux_nat
)
3777 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (no resumed LWP)\n");
3779 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3781 if (!target_can_async_p ())
3782 clear_sigint_trap ();
3784 restore_child_signals_mask (&prev_mask
);
3785 return minus_one_ptid
;
3788 /* No interesting event to report to the core. */
3790 if (target_options
& TARGET_WNOHANG
)
3792 if (debug_linux_nat
)
3793 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
3795 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3796 restore_child_signals_mask (&prev_mask
);
3797 return minus_one_ptid
;
3800 /* We shouldn't end up here unless we want to try again. */
3801 gdb_assert (lp
== NULL
);
3803 /* Block until we get an event reported with SIGCHLD. */
3804 sigsuspend (&suspend_mask
);
3807 if (!target_can_async_p ())
3808 clear_sigint_trap ();
3812 status
= lp
->status
;
3815 /* Don't report signals that GDB isn't interested in, such as
3816 signals that are neither printed nor stopped upon. Stopping all
3817 threads can be a bit time-consuming so if we want decent
3818 performance with heavily multi-threaded programs, especially when
3819 they're using a high frequency timer, we'd better avoid it if we
3822 if (WIFSTOPPED (status
))
3824 enum target_signal signo
= target_signal_from_host (WSTOPSIG (status
));
3826 /* When using hardware single-step, we need to report every signal.
3827 Otherwise, signals in pass_mask may be short-circuited. */
3829 && WSTOPSIG (status
) && sigismember (&pass_mask
, WSTOPSIG (status
)))
3831 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3832 here? It is not clear we should. GDB may not expect
3833 other threads to run. On the other hand, not resuming
3834 newly attached threads may cause an unwanted delay in
3835 getting them running. */
3836 registers_changed ();
3837 if (linux_nat_prepare_to_resume
!= NULL
)
3838 linux_nat_prepare_to_resume (lp
);
3839 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3841 if (debug_linux_nat
)
3842 fprintf_unfiltered (gdb_stdlog
,
3843 "LLW: %s %s, %s (preempt 'handle')\n",
3845 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3846 target_pid_to_str (lp
->ptid
),
3847 (signo
!= TARGET_SIGNAL_0
3848 ? strsignal (target_signal_to_host (signo
))
3856 /* Only do the below in all-stop, as we currently use SIGINT
3857 to implement target_stop (see linux_nat_stop) in
3859 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3861 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3862 forwarded to the entire process group, that is, all LWPs
3863 will receive it - unless they're using CLONE_THREAD to
3864 share signals. Since we only want to report it once, we
3865 mark it as ignored for all LWPs except this one. */
3866 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid
)),
3867 set_ignore_sigint
, NULL
);
3868 lp
->ignore_sigint
= 0;
3871 maybe_clear_ignore_sigint (lp
);
3875 /* This LWP is stopped now. */
3878 if (debug_linux_nat
)
3879 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
3880 status_to_str (status
), target_pid_to_str (lp
->ptid
));
3884 /* Now stop all other LWP's ... */
3885 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3887 /* ... and wait until all of them have reported back that
3888 they're no longer running. */
3889 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3891 /* If we're not waiting for a specific LWP, choose an event LWP
3892 from among those that have had events. Giving equal priority
3893 to all LWPs that have had events helps prevent
3895 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
3896 select_event_lwp (ptid
, &lp
, &status
);
3898 /* Now that we've selected our final event LWP, cancel any
3899 breakpoints in other LWPs that have hit a GDB breakpoint.
3900 See the comment in cancel_breakpoints_callback to find out
3902 iterate_over_lwps (minus_one_ptid
, cancel_breakpoints_callback
, lp
);
3904 /* We'll need this to determine whether to report a SIGSTOP as
3905 TARGET_WAITKIND_0. Need to take a copy because
3906 resume_clear_callback clears it. */
3907 last_resume_kind
= lp
->last_resume_kind
;
3909 /* In all-stop, from the core's perspective, all LWPs are now
3910 stopped until a new resume action is sent over. */
3911 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
3916 last_resume_kind
= lp
->last_resume_kind
;
3917 resume_clear_callback (lp
, NULL
);
3920 if (linux_nat_status_is_event (status
))
3922 if (debug_linux_nat
)
3923 fprintf_unfiltered (gdb_stdlog
,
3924 "LLW: trap ptid is %s.\n",
3925 target_pid_to_str (lp
->ptid
));
3928 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3930 *ourstatus
= lp
->waitstatus
;
3931 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3934 store_waitstatus (ourstatus
, status
);
3936 if (debug_linux_nat
)
3937 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3939 restore_child_signals_mask (&prev_mask
);
3941 if (last_resume_kind
== resume_stop
3942 && ourstatus
->kind
== TARGET_WAITKIND_STOPPED
3943 && WSTOPSIG (status
) == SIGSTOP
)
3945 /* A thread that has been requested to stop by GDB with
3946 target_stop, and it stopped cleanly, so report as SIG0. The
3947 use of SIGSTOP is an implementation detail. */
3948 ourstatus
->value
.sig
= TARGET_SIGNAL_0
;
3951 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
3952 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
3955 lp
->core
= linux_nat_core_of_thread_1 (lp
->ptid
);
3960 /* Resume LWPs that are currently stopped without any pending status
3961 to report, but are resumed from the core's perspective. */
3964 resume_stopped_resumed_lwps (struct lwp_info
*lp
, void *data
)
3966 ptid_t
*wait_ptid_p
= data
;
3971 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
3973 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
3974 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3975 CORE_ADDR pc
= regcache_read_pc (regcache
);
3977 gdb_assert (is_executing (lp
->ptid
));
3979 /* Don't bother if there's a breakpoint at PC that we'd hit
3980 immediately, and we're not waiting for this LWP. */
3981 if (!ptid_match (lp
->ptid
, *wait_ptid_p
))
3983 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
3987 if (debug_linux_nat
)
3988 fprintf_unfiltered (gdb_stdlog
,
3989 "RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
3990 target_pid_to_str (lp
->ptid
),
3991 paddress (gdbarch
, pc
),
3994 registers_changed ();
3995 if (linux_nat_prepare_to_resume
!= NULL
)
3996 linux_nat_prepare_to_resume (lp
);
3997 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3998 lp
->step
, TARGET_SIGNAL_0
);
4000 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
4001 lp
->stopped_by_watchpoint
= 0;
4008 linux_nat_wait (struct target_ops
*ops
,
4009 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
4014 if (debug_linux_nat
)
4015 fprintf_unfiltered (gdb_stdlog
,
4016 "linux_nat_wait: [%s]\n", target_pid_to_str (ptid
));
4018 /* Flush the async file first. */
4019 if (target_can_async_p ())
4020 async_file_flush ();
4022 /* Resume LWPs that are currently stopped without any pending status
4023 to report, but are resumed from the core's perspective. LWPs get
4024 in this state if we find them stopping at a time we're not
4025 interested in reporting the event (target_wait on a
4026 specific_process, for example, see linux_nat_wait_1), and
4027 meanwhile the event became uninteresting. Don't bother resuming
4028 LWPs we're not going to wait for if they'd stop immediately. */
4030 iterate_over_lwps (minus_one_ptid
, resume_stopped_resumed_lwps
, &ptid
);
4032 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
, target_options
);
4034 /* If we requested any event, and something came out, assume there
4035 may be more. If we requested a specific lwp or process, also
4036 assume there may be more. */
4037 if (target_can_async_p ()
4038 && ((ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
4039 && ourstatus
->kind
!= TARGET_WAITKIND_NO_RESUMED
)
4040 || !ptid_equal (ptid
, minus_one_ptid
)))
4043 /* Get ready for the next event. */
4044 if (target_can_async_p ())
4045 target_async (inferior_event_handler
, 0);
4051 kill_callback (struct lwp_info
*lp
, void *data
)
4053 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
4056 kill (GET_LWP (lp
->ptid
), SIGKILL
);
4057 if (debug_linux_nat
)
4058 fprintf_unfiltered (gdb_stdlog
,
4059 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
4060 target_pid_to_str (lp
->ptid
),
4061 errno
? safe_strerror (errno
) : "OK");
4063 /* Some kernels ignore even SIGKILL for processes under ptrace. */
4066 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
4067 if (debug_linux_nat
)
4068 fprintf_unfiltered (gdb_stdlog
,
4069 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
4070 target_pid_to_str (lp
->ptid
),
4071 errno
? safe_strerror (errno
) : "OK");
4077 kill_wait_callback (struct lwp_info
*lp
, void *data
)
4081 /* We must make sure that there are no pending events (delayed
4082 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
4083 program doesn't interfere with any following debugging session. */
4085 /* For cloned processes we must check both with __WCLONE and
4086 without, since the exit status of a cloned process isn't reported
4092 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
4093 if (pid
!= (pid_t
) -1)
4095 if (debug_linux_nat
)
4096 fprintf_unfiltered (gdb_stdlog
,
4097 "KWC: wait %s received unknown.\n",
4098 target_pid_to_str (lp
->ptid
));
4099 /* The Linux kernel sometimes fails to kill a thread
4100 completely after PTRACE_KILL; that goes from the stop
4101 point in do_fork out to the one in
4102 get_signal_to_deliever and waits again. So kill it
4104 kill_callback (lp
, NULL
);
4107 while (pid
== GET_LWP (lp
->ptid
));
4109 gdb_assert (pid
== -1 && errno
== ECHILD
);
4114 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
4115 if (pid
!= (pid_t
) -1)
4117 if (debug_linux_nat
)
4118 fprintf_unfiltered (gdb_stdlog
,
4119 "KWC: wait %s received unk.\n",
4120 target_pid_to_str (lp
->ptid
));
4121 /* See the call to kill_callback above. */
4122 kill_callback (lp
, NULL
);
4125 while (pid
== GET_LWP (lp
->ptid
));
4127 gdb_assert (pid
== -1 && errno
== ECHILD
);
4132 linux_nat_kill (struct target_ops
*ops
)
4134 struct target_waitstatus last
;
4138 /* If we're stopped while forking and we haven't followed yet,
4139 kill the other task. We need to do this first because the
4140 parent will be sleeping if this is a vfork. */
4142 get_last_target_status (&last_ptid
, &last
);
4144 if (last
.kind
== TARGET_WAITKIND_FORKED
4145 || last
.kind
== TARGET_WAITKIND_VFORKED
)
4147 ptrace (PT_KILL
, PIDGET (last
.value
.related_pid
), 0, 0);
4151 if (forks_exist_p ())
4152 linux_fork_killall ();
4155 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
4157 /* Stop all threads before killing them, since ptrace requires
4158 that the thread is stopped to sucessfully PTRACE_KILL. */
4159 iterate_over_lwps (ptid
, stop_callback
, NULL
);
4160 /* ... and wait until all of them have reported back that
4161 they're no longer running. */
4162 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
4164 /* Kill all LWP's ... */
4165 iterate_over_lwps (ptid
, kill_callback
, NULL
);
4167 /* ... and wait until we've flushed all events. */
4168 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
4171 target_mourn_inferior ();
4175 linux_nat_mourn_inferior (struct target_ops
*ops
)
4177 purge_lwp_list (ptid_get_pid (inferior_ptid
));
4179 if (! forks_exist_p ())
4180 /* Normal case, no other forks available. */
4181 linux_ops
->to_mourn_inferior (ops
);
4183 /* Multi-fork case. The current inferior_ptid has exited, but
4184 there are other viable forks to debug. Delete the exiting
4185 one and context-switch to the first available. */
4186 linux_fork_mourn_inferior ();
4189 /* Convert a native/host siginfo object, into/from the siginfo in the
4190 layout of the inferiors' architecture. */
4193 siginfo_fixup (struct siginfo
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
4197 if (linux_nat_siginfo_fixup
!= NULL
)
4198 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
4200 /* If there was no callback, or the callback didn't do anything,
4201 then just do a straight memcpy. */
4205 memcpy (siginfo
, inf_siginfo
, sizeof (struct siginfo
));
4207 memcpy (inf_siginfo
, siginfo
, sizeof (struct siginfo
));
4212 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
4213 const char *annex
, gdb_byte
*readbuf
,
4214 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4217 struct siginfo siginfo
;
4218 gdb_byte inf_siginfo
[sizeof (struct siginfo
)];
4220 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
4221 gdb_assert (readbuf
|| writebuf
);
4223 pid
= GET_LWP (inferior_ptid
);
4225 pid
= GET_PID (inferior_ptid
);
4227 if (offset
> sizeof (siginfo
))
4231 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
4235 /* When GDB is built as a 64-bit application, ptrace writes into
4236 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
4237 inferior with a 64-bit GDB should look the same as debugging it
4238 with a 32-bit GDB, we need to convert it. GDB core always sees
4239 the converted layout, so any read/write will have to be done
4241 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
4243 if (offset
+ len
> sizeof (siginfo
))
4244 len
= sizeof (siginfo
) - offset
;
4246 if (readbuf
!= NULL
)
4247 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
4250 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
4252 /* Convert back to ptrace layout before flushing it out. */
4253 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
4256 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
4265 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4266 const char *annex
, gdb_byte
*readbuf
,
4267 const gdb_byte
*writebuf
,
4268 ULONGEST offset
, LONGEST len
)
4270 struct cleanup
*old_chain
;
4273 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
4274 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
4277 /* The target is connected but no live inferior is selected. Pass
4278 this request down to a lower stratum (e.g., the executable
4280 if (object
== TARGET_OBJECT_MEMORY
&& ptid_equal (inferior_ptid
, null_ptid
))
4283 old_chain
= save_inferior_ptid ();
4285 if (is_lwp (inferior_ptid
))
4286 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
4288 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4291 do_cleanups (old_chain
);
4296 linux_thread_alive (ptid_t ptid
)
4300 gdb_assert (is_lwp (ptid
));
4302 /* Send signal 0 instead of anything ptrace, because ptracing a
4303 running thread errors out claiming that the thread doesn't
4305 err
= kill_lwp (GET_LWP (ptid
), 0);
4307 if (debug_linux_nat
)
4308 fprintf_unfiltered (gdb_stdlog
,
4309 "LLTA: KILL(SIG0) %s (%s)\n",
4310 target_pid_to_str (ptid
),
4311 err
? safe_strerror (tmp_errno
) : "OK");
4320 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
4322 return linux_thread_alive (ptid
);
4326 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
4328 static char buf
[64];
4331 && (GET_PID (ptid
) != GET_LWP (ptid
)
4332 || num_lwps (GET_PID (ptid
)) > 1))
4334 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
4338 return normal_pid_to_str (ptid
);
4342 linux_nat_thread_name (struct thread_info
*thr
)
4344 int pid
= ptid_get_pid (thr
->ptid
);
4345 long lwp
= ptid_get_lwp (thr
->ptid
);
4346 #define FORMAT "/proc/%d/task/%ld/comm"
4347 char buf
[sizeof (FORMAT
) + 30];
4349 char *result
= NULL
;
4351 snprintf (buf
, sizeof (buf
), FORMAT
, pid
, lwp
);
4352 comm_file
= fopen (buf
, "r");
4355 /* Not exported by the kernel, so we define it here. */
4357 static char line
[COMM_LEN
+ 1];
4359 if (fgets (line
, sizeof (line
), comm_file
))
4361 char *nl
= strchr (line
, '\n');
4378 /* Accepts an integer PID; Returns a string representing a file that
4379 can be opened to get the symbols for the child process. */
4382 linux_child_pid_to_exec_file (int pid
)
4384 char *name1
, *name2
;
4386 name1
= xmalloc (MAXPATHLEN
);
4387 name2
= xmalloc (MAXPATHLEN
);
4388 make_cleanup (xfree
, name1
);
4389 make_cleanup (xfree
, name2
);
4390 memset (name2
, 0, MAXPATHLEN
);
4392 sprintf (name1
, "/proc/%d/exe", pid
);
4393 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
4399 /* Records the thread's register state for the corefile note
4403 linux_nat_collect_thread_registers (const struct regcache
*regcache
,
4404 ptid_t ptid
, bfd
*obfd
,
4405 char *note_data
, int *note_size
,
4406 enum target_signal stop_signal
)
4408 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
4409 const struct regset
*regset
;
4411 gdb_gregset_t gregs
;
4412 gdb_fpregset_t fpregs
;
4414 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
4417 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
4419 != NULL
&& regset
->collect_regset
!= NULL
)
4420 regset
->collect_regset (regset
, regcache
, -1, &gregs
, sizeof (gregs
));
4422 fill_gregset (regcache
, &gregs
, -1);
4424 note_data
= (char *) elfcore_write_prstatus
4425 (obfd
, note_data
, note_size
, ptid_get_lwp (ptid
),
4426 target_signal_to_host (stop_signal
), &gregs
);
4429 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
4431 != NULL
&& regset
->collect_regset
!= NULL
)
4432 regset
->collect_regset (regset
, regcache
, -1, &fpregs
, sizeof (fpregs
));
4434 fill_fpregset (regcache
, &fpregs
, -1);
4436 note_data
= (char *) elfcore_write_prfpreg (obfd
, note_data
, note_size
,
4437 &fpregs
, sizeof (fpregs
));
4442 /* Fills the "to_make_corefile_note" target vector. Builds the note
4443 section for a corefile, and returns it in a malloc buffer. */
4446 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
4448 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
4449 converted to gdbarch_core_regset_sections, this function can go away. */
4450 return linux_make_corefile_notes (target_gdbarch
, obfd
, note_size
,
4451 linux_nat_collect_thread_registers
);
4454 /* Implement the to_xfer_partial interface for memory reads using the /proc
4455 filesystem. Because we can use a single read() call for /proc, this
4456 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4457 but it doesn't support writes. */
4460 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4461 const char *annex
, gdb_byte
*readbuf
,
4462 const gdb_byte
*writebuf
,
4463 ULONGEST offset
, LONGEST len
)
4469 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
4472 /* Don't bother for one word. */
4473 if (len
< 3 * sizeof (long))
4476 /* We could keep this file open and cache it - possibly one per
4477 thread. That requires some juggling, but is even faster. */
4478 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
4479 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4483 /* If pread64 is available, use it. It's faster if the kernel
4484 supports it (only one syscall), and it's 64-bit safe even on
4485 32-bit platforms (for instance, SPARC debugging a SPARC64
4488 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
4490 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
4501 /* Enumerate spufs IDs for process PID. */
4503 spu_enumerate_spu_ids (int pid
, gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
4505 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
4507 LONGEST written
= 0;
4510 struct dirent
*entry
;
4512 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
4513 dir
= opendir (path
);
4518 while ((entry
= readdir (dir
)) != NULL
)
4524 fd
= atoi (entry
->d_name
);
4528 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
4529 if (stat (path
, &st
) != 0)
4531 if (!S_ISDIR (st
.st_mode
))
4534 if (statfs (path
, &stfs
) != 0)
4536 if (stfs
.f_type
!= SPUFS_MAGIC
)
4539 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
4541 store_unsigned_integer (buf
+ pos
- offset
, 4, byte_order
, fd
);
4551 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4552 object type, using the /proc file system. */
4554 linux_proc_xfer_spu (struct target_ops
*ops
, enum target_object object
,
4555 const char *annex
, gdb_byte
*readbuf
,
4556 const gdb_byte
*writebuf
,
4557 ULONGEST offset
, LONGEST len
)
4562 int pid
= PIDGET (inferior_ptid
);
4569 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
4572 xsnprintf (buf
, sizeof buf
, "/proc/%d/fd/%s", pid
, annex
);
4573 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
4578 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4585 ret
= write (fd
, writebuf
, (size_t) len
);
4587 ret
= read (fd
, readbuf
, (size_t) len
);
4594 /* Parse LINE as a signal set and add its set bits to SIGS. */
4597 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4599 int len
= strlen (line
) - 1;
4603 if (line
[len
] != '\n')
4604 error (_("Could not parse signal set: %s"), line
);
4612 if (*p
>= '0' && *p
<= '9')
4614 else if (*p
>= 'a' && *p
<= 'f')
4615 digit
= *p
- 'a' + 10;
4617 error (_("Could not parse signal set: %s"), line
);
4622 sigaddset (sigs
, signum
+ 1);
4624 sigaddset (sigs
, signum
+ 2);
4626 sigaddset (sigs
, signum
+ 3);
4628 sigaddset (sigs
, signum
+ 4);
4634 /* Find process PID's pending signals from /proc/pid/status and set
4638 linux_proc_pending_signals (int pid
, sigset_t
*pending
,
4639 sigset_t
*blocked
, sigset_t
*ignored
)
4642 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
4643 struct cleanup
*cleanup
;
4645 sigemptyset (pending
);
4646 sigemptyset (blocked
);
4647 sigemptyset (ignored
);
4648 sprintf (fname
, "/proc/%d/status", pid
);
4649 procfile
= fopen (fname
, "r");
4650 if (procfile
== NULL
)
4651 error (_("Could not open %s"), fname
);
4652 cleanup
= make_cleanup_fclose (procfile
);
4654 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
4656 /* Normal queued signals are on the SigPnd line in the status
4657 file. However, 2.6 kernels also have a "shared" pending
4658 queue for delivering signals to a thread group, so check for
4661 Unfortunately some Red Hat kernels include the shared pending
4662 queue but not the ShdPnd status field. */
4664 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
4665 add_line_to_sigset (buffer
+ 8, pending
);
4666 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
4667 add_line_to_sigset (buffer
+ 8, pending
);
4668 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
4669 add_line_to_sigset (buffer
+ 8, blocked
);
4670 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
4671 add_line_to_sigset (buffer
+ 8, ignored
);
4674 do_cleanups (cleanup
);
4678 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4679 const char *annex
, gdb_byte
*readbuf
,
4680 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4682 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4684 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4688 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4689 const char *annex
, gdb_byte
*readbuf
,
4690 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4694 if (object
== TARGET_OBJECT_AUXV
)
4695 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4698 if (object
== TARGET_OBJECT_OSDATA
)
4699 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4702 if (object
== TARGET_OBJECT_SPU
)
4703 return linux_proc_xfer_spu (ops
, object
, annex
, readbuf
, writebuf
,
4706 /* GDB calculates all the addresses in possibly larget width of the address.
4707 Address width needs to be masked before its final use - either by
4708 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4710 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4712 if (object
== TARGET_OBJECT_MEMORY
)
4714 int addr_bit
= gdbarch_addr_bit (target_gdbarch
);
4716 if (addr_bit
< (sizeof (ULONGEST
) * HOST_CHAR_BIT
))
4717 offset
&= ((ULONGEST
) 1 << addr_bit
) - 1;
4720 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4725 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4729 /* Create a prototype generic GNU/Linux target. The client can override
4730 it with local methods. */
4733 linux_target_install_ops (struct target_ops
*t
)
4735 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4736 t
->to_remove_fork_catchpoint
= linux_child_remove_fork_catchpoint
;
4737 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4738 t
->to_remove_vfork_catchpoint
= linux_child_remove_vfork_catchpoint
;
4739 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4740 t
->to_remove_exec_catchpoint
= linux_child_remove_exec_catchpoint
;
4741 t
->to_set_syscall_catchpoint
= linux_child_set_syscall_catchpoint
;
4742 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4743 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4744 t
->to_post_attach
= linux_child_post_attach
;
4745 t
->to_follow_fork
= linux_child_follow_fork
;
4746 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
4748 super_xfer_partial
= t
->to_xfer_partial
;
4749 t
->to_xfer_partial
= linux_xfer_partial
;
4755 struct target_ops
*t
;
4757 t
= inf_ptrace_target ();
4758 linux_target_install_ops (t
);
4764 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4766 struct target_ops
*t
;
4768 t
= inf_ptrace_trad_target (register_u_offset
);
4769 linux_target_install_ops (t
);
4774 /* target_is_async_p implementation. */
4777 linux_nat_is_async_p (void)
4779 /* NOTE: palves 2008-03-21: We're only async when the user requests
4780 it explicitly with the "set target-async" command.
4781 Someday, linux will always be async. */
4782 return target_async_permitted
;
4785 /* target_can_async_p implementation. */
4788 linux_nat_can_async_p (void)
4790 /* NOTE: palves 2008-03-21: We're only async when the user requests
4791 it explicitly with the "set target-async" command.
4792 Someday, linux will always be async. */
4793 return target_async_permitted
;
4797 linux_nat_supports_non_stop (void)
4802 /* True if we want to support multi-process. To be removed when GDB
4803 supports multi-exec. */
4805 int linux_multi_process
= 1;
4808 linux_nat_supports_multi_process (void)
4810 return linux_multi_process
;
4814 linux_nat_supports_disable_randomization (void)
4816 #ifdef HAVE_PERSONALITY
4823 static int async_terminal_is_ours
= 1;
4825 /* target_terminal_inferior implementation. */
4828 linux_nat_terminal_inferior (void)
4830 if (!target_is_async_p ())
4832 /* Async mode is disabled. */
4833 terminal_inferior ();
4837 terminal_inferior ();
4839 /* Calls to target_terminal_*() are meant to be idempotent. */
4840 if (!async_terminal_is_ours
)
4843 delete_file_handler (input_fd
);
4844 async_terminal_is_ours
= 0;
4848 /* target_terminal_ours implementation. */
4851 linux_nat_terminal_ours (void)
4853 if (!target_is_async_p ())
4855 /* Async mode is disabled. */
4860 /* GDB should never give the terminal to the inferior if the
4861 inferior is running in the background (run&, continue&, etc.),
4862 but claiming it sure should. */
4865 if (async_terminal_is_ours
)
4868 clear_sigint_trap ();
4869 add_file_handler (input_fd
, stdin_event_handler
, 0);
4870 async_terminal_is_ours
= 1;
4873 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4875 static void *async_client_context
;
4877 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4878 so we notice when any child changes state, and notify the
4879 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4880 above to wait for the arrival of a SIGCHLD. */
4883 sigchld_handler (int signo
)
4885 int old_errno
= errno
;
4887 if (debug_linux_nat
)
4888 ui_file_write_async_safe (gdb_stdlog
,
4889 "sigchld\n", sizeof ("sigchld\n") - 1);
4891 if (signo
== SIGCHLD
4892 && linux_nat_event_pipe
[0] != -1)
4893 async_file_mark (); /* Let the event loop know that there are
4894 events to handle. */
4899 /* Callback registered with the target events file descriptor. */
4902 handle_target_event (int error
, gdb_client_data client_data
)
4904 (*async_client_callback
) (INF_REG_EVENT
, async_client_context
);
4907 /* Create/destroy the target events pipe. Returns previous state. */
4910 linux_async_pipe (int enable
)
4912 int previous
= (linux_nat_event_pipe
[0] != -1);
4914 if (previous
!= enable
)
4918 block_child_signals (&prev_mask
);
4922 if (pipe (linux_nat_event_pipe
) == -1)
4923 internal_error (__FILE__
, __LINE__
,
4924 "creating event pipe failed.");
4926 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4927 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4931 close (linux_nat_event_pipe
[0]);
4932 close (linux_nat_event_pipe
[1]);
4933 linux_nat_event_pipe
[0] = -1;
4934 linux_nat_event_pipe
[1] = -1;
4937 restore_child_signals_mask (&prev_mask
);
4943 /* target_async implementation. */
4946 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
4947 void *context
), void *context
)
4949 if (callback
!= NULL
)
4951 async_client_callback
= callback
;
4952 async_client_context
= context
;
4953 if (!linux_async_pipe (1))
4955 add_file_handler (linux_nat_event_pipe
[0],
4956 handle_target_event
, NULL
);
4957 /* There may be pending events to handle. Tell the event loop
4964 async_client_callback
= callback
;
4965 async_client_context
= context
;
4966 delete_file_handler (linux_nat_event_pipe
[0]);
4967 linux_async_pipe (0);
4972 /* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
4976 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4980 ptid_t ptid
= lwp
->ptid
;
4982 if (debug_linux_nat
)
4983 fprintf_unfiltered (gdb_stdlog
,
4984 "LNSL: running -> suspending %s\n",
4985 target_pid_to_str (lwp
->ptid
));
4988 if (lwp
->last_resume_kind
== resume_stop
)
4990 if (debug_linux_nat
)
4991 fprintf_unfiltered (gdb_stdlog
,
4992 "linux-nat: already stopping LWP %ld at "
4994 ptid_get_lwp (lwp
->ptid
));
4998 stop_callback (lwp
, NULL
);
4999 lwp
->last_resume_kind
= resume_stop
;
5003 /* Already known to be stopped; do nothing. */
5005 if (debug_linux_nat
)
5007 if (find_thread_ptid (lwp
->ptid
)->stop_requested
)
5008 fprintf_unfiltered (gdb_stdlog
,
5009 "LNSL: already stopped/stop_requested %s\n",
5010 target_pid_to_str (lwp
->ptid
));
5012 fprintf_unfiltered (gdb_stdlog
,
5013 "LNSL: already stopped/no "
5014 "stop_requested yet %s\n",
5015 target_pid_to_str (lwp
->ptid
));
5022 linux_nat_stop (ptid_t ptid
)
5025 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
5027 linux_ops
->to_stop (ptid
);
5031 linux_nat_close (int quitting
)
5033 /* Unregister from the event loop. */
5034 if (linux_nat_is_async_p ())
5035 linux_nat_async (NULL
, 0);
5037 if (linux_ops
->to_close
)
5038 linux_ops
->to_close (quitting
);
5041 /* When requests are passed down from the linux-nat layer to the
5042 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
5043 used. The address space pointer is stored in the inferior object,
5044 but the common code that is passed such ptid can't tell whether
5045 lwpid is a "main" process id or not (it assumes so). We reverse
5046 look up the "main" process id from the lwp here. */
5048 struct address_space
*
5049 linux_nat_thread_address_space (struct target_ops
*t
, ptid_t ptid
)
5051 struct lwp_info
*lwp
;
5052 struct inferior
*inf
;
5055 pid
= GET_LWP (ptid
);
5056 if (GET_LWP (ptid
) == 0)
5058 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
5060 lwp
= find_lwp_pid (ptid
);
5061 pid
= GET_PID (lwp
->ptid
);
5065 /* A (pid,lwpid,0) ptid. */
5066 pid
= GET_PID (ptid
);
5069 inf
= find_inferior_pid (pid
);
5070 gdb_assert (inf
!= NULL
);
5075 linux_nat_core_of_thread_1 (ptid_t ptid
)
5077 struct cleanup
*back_to
;
5080 char *content
= NULL
;
5083 int content_read
= 0;
5087 filename
= xstrprintf ("/proc/%d/task/%ld/stat",
5088 GET_PID (ptid
), GET_LWP (ptid
));
5089 back_to
= make_cleanup (xfree
, filename
);
5091 f
= fopen (filename
, "r");
5094 do_cleanups (back_to
);
5098 make_cleanup_fclose (f
);
5104 content
= xrealloc (content
, content_read
+ 1024);
5105 n
= fread (content
+ content_read
, 1, 1024, f
);
5109 content
[content_read
] = '\0';
5114 make_cleanup (xfree
, content
);
5116 p
= strchr (content
, '(');
5120 p
= strchr (p
, ')');
5124 /* If the first field after program name has index 0, then core number is
5125 the field with index 36. There's no constant for that anywhere. */
5127 p
= strtok_r (p
, " ", &ts
);
5128 for (i
= 0; p
!= NULL
&& i
!= 36; ++i
)
5129 p
= strtok_r (NULL
, " ", &ts
);
5131 if (p
== NULL
|| sscanf (p
, "%d", &core
) == 0)
5134 do_cleanups (back_to
);
5139 /* Return the cached value of the processor core for thread PTID. */
5142 linux_nat_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
5144 struct lwp_info
*info
= find_lwp_pid (ptid
);
5152 linux_nat_add_target (struct target_ops
*t
)
5154 /* Save the provided single-threaded target. We save this in a separate
5155 variable because another target we've inherited from (e.g. inf-ptrace)
5156 may have saved a pointer to T; we want to use it for the final
5157 process stratum target. */
5158 linux_ops_saved
= *t
;
5159 linux_ops
= &linux_ops_saved
;
5161 /* Override some methods for multithreading. */
5162 t
->to_create_inferior
= linux_nat_create_inferior
;
5163 t
->to_attach
= linux_nat_attach
;
5164 t
->to_detach
= linux_nat_detach
;
5165 t
->to_resume
= linux_nat_resume
;
5166 t
->to_wait
= linux_nat_wait
;
5167 t
->to_pass_signals
= linux_nat_pass_signals
;
5168 t
->to_xfer_partial
= linux_nat_xfer_partial
;
5169 t
->to_kill
= linux_nat_kill
;
5170 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
5171 t
->to_thread_alive
= linux_nat_thread_alive
;
5172 t
->to_pid_to_str
= linux_nat_pid_to_str
;
5173 t
->to_thread_name
= linux_nat_thread_name
;
5174 t
->to_has_thread_control
= tc_schedlock
;
5175 t
->to_thread_address_space
= linux_nat_thread_address_space
;
5176 t
->to_stopped_by_watchpoint
= linux_nat_stopped_by_watchpoint
;
5177 t
->to_stopped_data_address
= linux_nat_stopped_data_address
;
5179 t
->to_can_async_p
= linux_nat_can_async_p
;
5180 t
->to_is_async_p
= linux_nat_is_async_p
;
5181 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
5182 t
->to_async
= linux_nat_async
;
5183 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
5184 t
->to_terminal_ours
= linux_nat_terminal_ours
;
5185 t
->to_close
= linux_nat_close
;
5187 /* Methods for non-stop support. */
5188 t
->to_stop
= linux_nat_stop
;
5190 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
5192 t
->to_supports_disable_randomization
5193 = linux_nat_supports_disable_randomization
;
5195 t
->to_core_of_thread
= linux_nat_core_of_thread
;
5197 /* We don't change the stratum; this target will sit at
5198 process_stratum and thread_db will set at thread_stratum. This
5199 is a little strange, since this is a multi-threaded-capable
5200 target, but we want to be on the stack below thread_db, and we
5201 also want to be used for single-threaded processes. */
5206 /* Register a method to call whenever a new thread is attached. */
5208 linux_nat_set_new_thread (struct target_ops
*t
,
5209 void (*new_thread
) (struct lwp_info
*))
5211 /* Save the pointer. We only support a single registered instance
5212 of the GNU/Linux native target, so we do not need to map this to
5214 linux_nat_new_thread
= new_thread
;
5217 /* Register a method that converts a siginfo object between the layout
5218 that ptrace returns, and the layout in the architecture of the
5221 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
5222 int (*siginfo_fixup
) (struct siginfo
*,
5226 /* Save the pointer. */
5227 linux_nat_siginfo_fixup
= siginfo_fixup
;
5230 /* Register a method to call prior to resuming a thread. */
5233 linux_nat_set_prepare_to_resume (struct target_ops
*t
,
5234 void (*prepare_to_resume
) (struct lwp_info
*))
5236 /* Save the pointer. */
5237 linux_nat_prepare_to_resume
= prepare_to_resume
;
5240 /* Return the saved siginfo associated with PTID. */
5242 linux_nat_get_siginfo (ptid_t ptid
)
5244 struct lwp_info
*lp
= find_lwp_pid (ptid
);
5246 gdb_assert (lp
!= NULL
);
5248 return &lp
->siginfo
;
5251 /* Provide a prototype to silence -Wmissing-prototypes. */
5252 extern initialize_file_ftype _initialize_linux_nat
;
5255 _initialize_linux_nat (void)
5257 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance
,
5258 &debug_linux_nat
, _("\
5259 Set debugging of GNU/Linux lwp module."), _("\
5260 Show debugging of GNU/Linux lwp module."), _("\
5261 Enables printf debugging output."),
5263 show_debug_linux_nat
,
5264 &setdebuglist
, &showdebuglist
);
5266 /* Save this mask as the default. */
5267 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
5269 /* Install a SIGCHLD handler. */
5270 sigchld_action
.sa_handler
= sigchld_handler
;
5271 sigemptyset (&sigchld_action
.sa_mask
);
5272 sigchld_action
.sa_flags
= SA_RESTART
;
5274 /* Make it the default. */
5275 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
5277 /* Make sure we don't block SIGCHLD during a sigsuspend. */
5278 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
5279 sigdelset (&suspend_mask
, SIGCHLD
);
5281 sigemptyset (&blocked_mask
);
5285 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
5286 the GNU/Linux Threads library and therefore doesn't really belong
5289 /* Read variable NAME in the target and return its value if found.
5290 Otherwise return zero. It is assumed that the type of the variable
5294 get_signo (const char *name
)
5296 struct minimal_symbol
*ms
;
5299 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
5303 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
5304 sizeof (signo
)) != 0)
5310 /* Return the set of signals used by the threads library in *SET. */
5313 lin_thread_get_thread_signals (sigset_t
*set
)
5315 struct sigaction action
;
5316 int restart
, cancel
;
5318 sigemptyset (&blocked_mask
);
5321 restart
= get_signo ("__pthread_sig_restart");
5322 cancel
= get_signo ("__pthread_sig_cancel");
5324 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5325 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5326 not provide any way for the debugger to query the signal numbers -
5327 fortunately they don't change! */
5330 restart
= __SIGRTMIN
;
5333 cancel
= __SIGRTMIN
+ 1;
5335 sigaddset (set
, restart
);
5336 sigaddset (set
, cancel
);
5338 /* The GNU/Linux Threads library makes terminating threads send a
5339 special "cancel" signal instead of SIGCHLD. Make sure we catch
5340 those (to prevent them from terminating GDB itself, which is
5341 likely to be their default action) and treat them the same way as
5344 action
.sa_handler
= sigchld_handler
;
5345 sigemptyset (&action
.sa_mask
);
5346 action
.sa_flags
= SA_RESTART
;
5347 sigaction (cancel
, &action
, NULL
);
5349 /* We block the "cancel" signal throughout this code ... */
5350 sigaddset (&blocked_mask
, cancel
);
5351 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
5353 /* ... except during a sigsuspend. */
5354 sigdelset (&suspend_mask
, cancel
);