1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_string.h"
26 #include "gdb_assert.h"
27 #ifdef HAVE_TKILL_SYSCALL
29 #include <sys/syscall.h>
31 #include <sys/ptrace.h>
32 #include "linux-nat.h"
33 #include "linux-fork.h"
34 #include "gdbthread.h"
38 #include "inf-ptrace.h"
40 #include <sys/param.h> /* for MAXPATHLEN */
41 #include <sys/procfs.h> /* for elf_gregset etc. */
42 #include "elf-bfd.h" /* for elfcore_write_* */
43 #include "gregset.h" /* for gregset */
44 #include "gdbcore.h" /* for get_exec_file */
45 #include <ctype.h> /* for isdigit */
46 #include "gdbthread.h" /* for struct thread_info etc. */
47 #include "gdb_stat.h" /* for struct stat */
48 #include <fcntl.h> /* for O_RDONLY */
50 #include "event-loop.h"
51 #include "event-top.h"
53 /* Note on this file's use of signals:
55 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead
56 of another signal is not entirely significant; we just need for a
57 signal to be delivered, so that we can intercept it. SIGSTOP's
58 advantage is that it can not be blocked. A disadvantage is that it
59 is not a real-time signal, so it can only be queued once; we do not
60 keep track of other sources of SIGSTOP.
62 Two other signals that can't be blocked are SIGCONT and SIGKILL.
63 But we can't use them, because they have special behavior when the
64 signal is generated - not when it is delivered. SIGCONT resumes
65 the entire thread group and SIGKILL kills the entire thread group.
67 A delivered SIGSTOP would stop the entire thread group, not just the
68 thread we tkill'd. But we never let the SIGSTOP deliver; we always
69 intercept and cancel it (by PTRACE_CONT without passing SIGSTOP).
71 We could use a real-time signal instead. This would solve those
72 problems; we could use PTRACE_GETSIGINFO to locate the specific
73 stop signals sent by GDB. But we would still have to have some
74 support for SIGSTOP, since PTRACE_ATTACH generates it, and there
75 are races with trying to find a signal that is not blocked. */
81 /* If the system headers did not provide the constants, hard-code the normal
83 #ifndef PTRACE_EVENT_FORK
85 #define PTRACE_SETOPTIONS 0x4200
86 #define PTRACE_GETEVENTMSG 0x4201
88 /* options set using PTRACE_SETOPTIONS */
89 #define PTRACE_O_TRACESYSGOOD 0x00000001
90 #define PTRACE_O_TRACEFORK 0x00000002
91 #define PTRACE_O_TRACEVFORK 0x00000004
92 #define PTRACE_O_TRACECLONE 0x00000008
93 #define PTRACE_O_TRACEEXEC 0x00000010
94 #define PTRACE_O_TRACEVFORKDONE 0x00000020
95 #define PTRACE_O_TRACEEXIT 0x00000040
97 /* Wait extended result codes for the above trace options. */
98 #define PTRACE_EVENT_FORK 1
99 #define PTRACE_EVENT_VFORK 2
100 #define PTRACE_EVENT_CLONE 3
101 #define PTRACE_EVENT_EXEC 4
102 #define PTRACE_EVENT_VFORK_DONE 5
103 #define PTRACE_EVENT_EXIT 6
105 #endif /* PTRACE_EVENT_FORK */
107 /* We can't always assume that this flag is available, but all systems
108 with the ptrace event handlers also have __WALL, so it's safe to use
111 #define __WALL 0x40000000 /* Wait for any child. */
114 #ifndef PTRACE_GETSIGINFO
115 #define PTRACE_GETSIGINFO 0x4202
118 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
119 the use of the multi-threaded target. */
120 static struct target_ops
*linux_ops
;
121 static struct target_ops linux_ops_saved
;
123 /* The method to call, if any, when a new thread is attached. */
124 static void (*linux_nat_new_thread
) (ptid_t
);
126 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
127 Called by our to_xfer_partial. */
128 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
130 const char *, gdb_byte
*,
134 static int debug_linux_nat
;
136 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
137 struct cmd_list_element
*c
, const char *value
)
139 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
143 static int debug_linux_nat_async
= 0;
145 show_debug_linux_nat_async (struct ui_file
*file
, int from_tty
,
146 struct cmd_list_element
*c
, const char *value
)
148 fprintf_filtered (file
, _("Debugging of GNU/Linux async lwp module is %s.\n"),
152 static int linux_parent_pid
;
154 struct simple_pid_list
158 struct simple_pid_list
*next
;
160 struct simple_pid_list
*stopped_pids
;
162 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
163 can not be used, 1 if it can. */
165 static int linux_supports_tracefork_flag
= -1;
167 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
168 PTRACE_O_TRACEVFORKDONE. */
170 static int linux_supports_tracevforkdone_flag
= -1;
172 /* Async mode support */
174 /* To listen to target events asynchronously, we install a SIGCHLD
175 handler whose duty is to call waitpid (-1, ..., WNOHANG) to get all
176 the pending events into a pipe. Whenever we're ready to handle
177 events asynchronously, this pipe is registered as the waitable file
178 handle in the event loop. When we get to entry target points
179 coming out of the common code (target_wait, target_resume, ...),
180 that are going to call waitpid, we block SIGCHLD signals, and
181 remove all the events placed in the pipe into a local queue. All
182 the subsequent calls to my_waitpid (a waitpid wrapper) check this
183 local queue first. */
185 /* True if async mode is currently on. */
186 static int linux_nat_async_enabled
;
188 /* Zero if the async mode, although enabled, is masked, which means
189 linux_nat_wait should behave as if async mode was off. */
190 static int linux_nat_async_mask_value
= 1;
192 /* The read/write ends of the pipe registered as waitable file in the
194 static int linux_nat_event_pipe
[2] = { -1, -1 };
196 /* Number of queued events in the pipe. */
197 static volatile int linux_nat_num_queued_events
;
199 /* If async mode is on, true if we're listening for events; false if
200 target events are blocked. */
201 static int linux_nat_async_events_enabled
;
203 static int linux_nat_async_events (int enable
);
204 static void pipe_to_local_event_queue (void);
205 static void local_event_queue_to_pipe (void);
206 static void linux_nat_event_pipe_push (int pid
, int status
, int options
);
207 static int linux_nat_event_pipe_pop (int* ptr_status
, int* ptr_options
);
208 static void linux_nat_set_async_mode (int on
);
209 static void linux_nat_async (void (*callback
)
210 (enum inferior_event_type event_type
, void *context
),
212 static int linux_nat_async_mask (int mask
);
213 static int kill_lwp (int lwpid
, int signo
);
215 /* Captures the result of a successful waitpid call, along with the
216 options used in that call. */
217 struct waitpid_result
222 struct waitpid_result
*next
;
225 /* A singly-linked list of the results of the waitpid calls performed
226 in the async SIGCHLD handler. */
227 static struct waitpid_result
*waitpid_queue
= NULL
;
230 queued_waitpid (int pid
, int *status
, int flags
)
232 struct waitpid_result
*msg
= waitpid_queue
, *prev
= NULL
;
234 if (debug_linux_nat_async
)
235 fprintf_unfiltered (gdb_stdlog
,
237 QWPID: linux_nat_async_events_enabled(%d), linux_nat_num_queued_events(%d)\n",
238 linux_nat_async_events_enabled
,
239 linux_nat_num_queued_events
);
243 for (; msg
; prev
= msg
, msg
= msg
->next
)
244 if (pid
== -1 || pid
== msg
->pid
)
247 else if (flags
& __WCLONE
)
249 for (; msg
; prev
= msg
, msg
= msg
->next
)
250 if (msg
->options
& __WCLONE
251 && (pid
== -1 || pid
== msg
->pid
))
256 for (; msg
; prev
= msg
, msg
= msg
->next
)
257 if ((msg
->options
& __WCLONE
) == 0
258 && (pid
== -1 || pid
== msg
->pid
))
267 prev
->next
= msg
->next
;
269 waitpid_queue
= msg
->next
;
273 *status
= msg
->status
;
276 if (debug_linux_nat_async
)
277 fprintf_unfiltered (gdb_stdlog
, "QWPID: pid(%d), status(%x)\n",
284 if (debug_linux_nat_async
)
285 fprintf_unfiltered (gdb_stdlog
, "QWPID: miss\n");
293 push_waitpid (int pid
, int status
, int options
)
295 struct waitpid_result
*event
, *new_event
;
297 new_event
= xmalloc (sizeof (*new_event
));
298 new_event
->pid
= pid
;
299 new_event
->status
= status
;
300 new_event
->options
= options
;
301 new_event
->next
= NULL
;
305 for (event
= waitpid_queue
;
306 event
&& event
->next
;
310 event
->next
= new_event
;
313 waitpid_queue
= new_event
;
316 /* Drain all queued events of PID. If PID is -1, the effect is of
317 draining all events. */
319 drain_queued_events (int pid
)
321 while (queued_waitpid (pid
, NULL
, __WALL
) != -1)
326 /* Trivial list manipulation functions to keep track of a list of
327 new stopped processes. */
329 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
331 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
333 new_pid
->status
= status
;
334 new_pid
->next
= *listp
;
339 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *status
)
341 struct simple_pid_list
**p
;
343 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
344 if ((*p
)->pid
== pid
)
346 struct simple_pid_list
*next
= (*p
)->next
;
347 *status
= (*p
)->status
;
356 linux_record_stopped_pid (int pid
, int status
)
358 add_to_pid_list (&stopped_pids
, pid
, status
);
362 /* A helper function for linux_test_for_tracefork, called after fork (). */
365 linux_tracefork_child (void)
369 ptrace (PTRACE_TRACEME
, 0, 0, 0);
370 kill (getpid (), SIGSTOP
);
375 /* Wrapper function for waitpid which handles EINTR, and checks for
376 locally queued events. */
379 my_waitpid (int pid
, int *status
, int flags
)
383 /* There should be no concurrent calls to waitpid. */
384 gdb_assert (!linux_nat_async_events_enabled
);
386 ret
= queued_waitpid (pid
, status
, flags
);
392 ret
= waitpid (pid
, status
, flags
);
394 while (ret
== -1 && errno
== EINTR
);
399 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
401 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
402 we know that the feature is not available. This may change the tracing
403 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
405 However, if it succeeds, we don't know for sure that the feature is
406 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
407 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
408 fork tracing, and let it fork. If the process exits, we assume that we
409 can't use TRACEFORK; if we get the fork notification, and we can extract
410 the new child's PID, then we assume that we can. */
413 linux_test_for_tracefork (int original_pid
)
415 int child_pid
, ret
, status
;
418 linux_supports_tracefork_flag
= 0;
419 linux_supports_tracevforkdone_flag
= 0;
421 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
427 perror_with_name (("fork"));
430 linux_tracefork_child ();
432 ret
= my_waitpid (child_pid
, &status
, 0);
434 perror_with_name (("waitpid"));
435 else if (ret
!= child_pid
)
436 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
437 if (! WIFSTOPPED (status
))
438 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
440 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
443 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
446 warning (_("linux_test_for_tracefork: failed to kill child"));
450 ret
= my_waitpid (child_pid
, &status
, 0);
451 if (ret
!= child_pid
)
452 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
453 else if (!WIFSIGNALED (status
))
454 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
455 "killed child"), status
);
460 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
461 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
462 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
463 linux_supports_tracevforkdone_flag
= (ret
== 0);
465 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
467 warning (_("linux_test_for_tracefork: failed to resume child"));
469 ret
= my_waitpid (child_pid
, &status
, 0);
471 if (ret
== child_pid
&& WIFSTOPPED (status
)
472 && status
>> 16 == PTRACE_EVENT_FORK
)
475 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
476 if (ret
== 0 && second_pid
!= 0)
480 linux_supports_tracefork_flag
= 1;
481 my_waitpid (second_pid
, &second_status
, 0);
482 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
484 warning (_("linux_test_for_tracefork: failed to kill second child"));
485 my_waitpid (second_pid
, &status
, 0);
489 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
490 "(%d, status 0x%x)"), ret
, status
);
492 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
494 warning (_("linux_test_for_tracefork: failed to kill child"));
495 my_waitpid (child_pid
, &status
, 0);
498 /* Return non-zero iff we have tracefork functionality available.
499 This function also sets linux_supports_tracefork_flag. */
502 linux_supports_tracefork (int pid
)
504 if (linux_supports_tracefork_flag
== -1)
505 linux_test_for_tracefork (pid
);
506 return linux_supports_tracefork_flag
;
510 linux_supports_tracevforkdone (int pid
)
512 if (linux_supports_tracefork_flag
== -1)
513 linux_test_for_tracefork (pid
);
514 return linux_supports_tracevforkdone_flag
;
519 linux_enable_event_reporting (ptid_t ptid
)
521 int pid
= ptid_get_lwp (ptid
);
525 pid
= ptid_get_pid (ptid
);
527 if (! linux_supports_tracefork (pid
))
530 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
531 | PTRACE_O_TRACECLONE
;
532 if (linux_supports_tracevforkdone (pid
))
533 options
|= PTRACE_O_TRACEVFORKDONE
;
535 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
536 read-only process state. */
538 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
542 linux_child_post_attach (int pid
)
544 linux_enable_event_reporting (pid_to_ptid (pid
));
545 check_for_thread_db ();
549 linux_child_post_startup_inferior (ptid_t ptid
)
551 linux_enable_event_reporting (ptid
);
552 check_for_thread_db ();
556 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
559 struct target_waitstatus last_status
;
561 int parent_pid
, child_pid
;
563 if (target_can_async_p ())
564 target_async (NULL
, 0);
566 get_last_target_status (&last_ptid
, &last_status
);
567 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
568 parent_pid
= ptid_get_lwp (last_ptid
);
570 parent_pid
= ptid_get_pid (last_ptid
);
571 child_pid
= last_status
.value
.related_pid
;
575 /* We're already attached to the parent, by default. */
577 /* Before detaching from the child, remove all breakpoints from
578 it. (This won't actually modify the breakpoint list, but will
579 physically remove the breakpoints from the child.) */
580 /* If we vforked this will remove the breakpoints from the parent
581 also, but they'll be reinserted below. */
582 detach_breakpoints (child_pid
);
584 /* Detach new forked process? */
587 if (info_verbose
|| debug_linux_nat
)
589 target_terminal_ours ();
590 fprintf_filtered (gdb_stdlog
,
591 "Detaching after fork from child process %d.\n",
595 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
599 struct fork_info
*fp
;
600 /* Retain child fork in ptrace (stopped) state. */
601 fp
= find_fork_pid (child_pid
);
603 fp
= add_fork (child_pid
);
604 fork_save_infrun_state (fp
, 0);
609 gdb_assert (linux_supports_tracefork_flag
>= 0);
610 if (linux_supports_tracevforkdone (0))
614 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
615 my_waitpid (parent_pid
, &status
, __WALL
);
616 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
617 warning (_("Unexpected waitpid result %06x when waiting for "
618 "vfork-done"), status
);
622 /* We can't insert breakpoints until the child has
623 finished with the shared memory region. We need to
624 wait until that happens. Ideal would be to just
626 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
627 - waitpid (parent_pid, &status, __WALL);
628 However, most architectures can't handle a syscall
629 being traced on the way out if it wasn't traced on
632 We might also think to loop, continuing the child
633 until it exits or gets a SIGTRAP. One problem is
634 that the child might call ptrace with PTRACE_TRACEME.
636 There's no simple and reliable way to figure out when
637 the vforked child will be done with its copy of the
638 shared memory. We could step it out of the syscall,
639 two instructions, let it go, and then single-step the
640 parent once. When we have hardware single-step, this
641 would work; with software single-step it could still
642 be made to work but we'd have to be able to insert
643 single-step breakpoints in the child, and we'd have
644 to insert -just- the single-step breakpoint in the
645 parent. Very awkward.
647 In the end, the best we can do is to make sure it
648 runs for a little while. Hopefully it will be out of
649 range of any breakpoints we reinsert. Usually this
650 is only the single-step breakpoint at vfork's return
656 /* Since we vforked, breakpoints were removed in the parent
657 too. Put them back. */
658 reattach_breakpoints (parent_pid
);
663 char child_pid_spelling
[40];
665 /* Needed to keep the breakpoint lists in sync. */
667 detach_breakpoints (child_pid
);
669 /* Before detaching from the parent, remove all breakpoints from it. */
670 remove_breakpoints ();
672 if (info_verbose
|| debug_linux_nat
)
674 target_terminal_ours ();
675 fprintf_filtered (gdb_stdlog
,
676 "Attaching after fork to child process %d.\n",
680 /* If we're vforking, we may want to hold on to the parent until
681 the child exits or execs. At exec time we can remove the old
682 breakpoints from the parent and detach it; at exit time we
683 could do the same (or even, sneakily, resume debugging it - the
684 child's exec has failed, or something similar).
686 This doesn't clean up "properly", because we can't call
687 target_detach, but that's OK; if the current target is "child",
688 then it doesn't need any further cleanups, and lin_lwp will
689 generally not encounter vfork (vfork is defined to fork
692 The holding part is very easy if we have VFORKDONE events;
693 but keeping track of both processes is beyond GDB at the
694 moment. So we don't expose the parent to the rest of GDB.
695 Instead we quietly hold onto it until such time as we can
699 linux_parent_pid
= parent_pid
;
700 else if (!detach_fork
)
702 struct fork_info
*fp
;
703 /* Retain parent fork in ptrace (stopped) state. */
704 fp
= find_fork_pid (parent_pid
);
706 fp
= add_fork (parent_pid
);
707 fork_save_infrun_state (fp
, 0);
710 target_detach (NULL
, 0);
712 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
714 /* Reinstall ourselves, since we might have been removed in
715 target_detach (which does other necessary cleanup). */
718 linux_nat_switch_fork (inferior_ptid
);
719 check_for_thread_db ();
721 /* Reset breakpoints in the child as appropriate. */
722 follow_inferior_reset_breakpoints ();
725 if (target_can_async_p ())
726 target_async (inferior_event_handler
, 0);
733 linux_child_insert_fork_catchpoint (int pid
)
735 if (! linux_supports_tracefork (pid
))
736 error (_("Your system does not support fork catchpoints."));
740 linux_child_insert_vfork_catchpoint (int pid
)
742 if (!linux_supports_tracefork (pid
))
743 error (_("Your system does not support vfork catchpoints."));
747 linux_child_insert_exec_catchpoint (int pid
)
749 if (!linux_supports_tracefork (pid
))
750 error (_("Your system does not support exec catchpoints."));
753 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
754 are processes sharing the same VM space. A multi-threaded process
755 is basically a group of such processes. However, such a grouping
756 is almost entirely a user-space issue; the kernel doesn't enforce
757 such a grouping at all (this might change in the future). In
758 general, we'll rely on the threads library (i.e. the GNU/Linux
759 Threads library) to provide such a grouping.
761 It is perfectly well possible to write a multi-threaded application
762 without the assistance of a threads library, by using the clone
763 system call directly. This module should be able to give some
764 rudimentary support for debugging such applications if developers
765 specify the CLONE_PTRACE flag in the clone system call, and are
766 using the Linux kernel 2.4 or above.
768 Note that there are some peculiarities in GNU/Linux that affect
771 - In general one should specify the __WCLONE flag to waitpid in
772 order to make it report events for any of the cloned processes
773 (and leave it out for the initial process). However, if a cloned
774 process has exited the exit status is only reported if the
775 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
776 we cannot use it since GDB must work on older systems too.
778 - When a traced, cloned process exits and is waited for by the
779 debugger, the kernel reassigns it to the original parent and
780 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
781 library doesn't notice this, which leads to the "zombie problem":
782 When debugged a multi-threaded process that spawns a lot of
783 threads will run out of processes, even if the threads exit,
784 because the "zombies" stay around. */
786 /* List of known LWPs. */
787 struct lwp_info
*lwp_list
;
789 /* Number of LWPs in the list. */
793 /* If the last reported event was a SIGTRAP, this variable is set to
794 the process id of the LWP/thread that got it. */
798 /* Since we cannot wait (in linux_nat_wait) for the initial process and
799 any cloned processes with a single call to waitpid, we have to use
800 the WNOHANG flag and call waitpid in a loop. To optimize
801 things a bit we use `sigsuspend' to wake us up when a process has
802 something to report (it will send us a SIGCHLD if it has). To make
803 this work we have to juggle with the signal mask. We save the
804 original signal mask such that we can restore it before creating a
805 new process in order to avoid blocking certain signals in the
806 inferior. We then block SIGCHLD during the waitpid/sigsuspend
809 /* Original signal mask. */
810 static sigset_t normal_mask
;
812 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
813 _initialize_linux_nat. */
814 static sigset_t suspend_mask
;
816 /* SIGCHLD action for synchronous mode. */
817 struct sigaction sync_sigchld_action
;
819 /* SIGCHLD action for asynchronous mode. */
820 static struct sigaction async_sigchld_action
;
823 /* Prototypes for local functions. */
824 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
825 static int linux_nat_thread_alive (ptid_t ptid
);
826 static char *linux_child_pid_to_exec_file (int pid
);
827 static int cancel_breakpoint (struct lwp_info
*lp
);
830 /* Convert wait status STATUS to a string. Used for printing debug
834 status_to_str (int status
)
838 if (WIFSTOPPED (status
))
839 snprintf (buf
, sizeof (buf
), "%s (stopped)",
840 strsignal (WSTOPSIG (status
)));
841 else if (WIFSIGNALED (status
))
842 snprintf (buf
, sizeof (buf
), "%s (terminated)",
843 strsignal (WSTOPSIG (status
)));
845 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
850 /* Initialize the list of LWPs. Note that this module, contrary to
851 what GDB's generic threads layer does for its thread list,
852 re-initializes the LWP lists whenever we mourn or detach (which
853 doesn't involve mourning) the inferior. */
858 struct lwp_info
*lp
, *lpnext
;
860 for (lp
= lwp_list
; lp
; lp
= lpnext
)
870 /* Add the LWP specified by PID to the list. Return a pointer to the
871 structure describing the new LWP. The LWP should already be stopped
872 (with an exception for the very first LWP). */
874 static struct lwp_info
*
875 add_lwp (ptid_t ptid
)
879 gdb_assert (is_lwp (ptid
));
881 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
883 memset (lp
, 0, sizeof (struct lwp_info
));
885 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
893 if (num_lwps
> 1 && linux_nat_new_thread
!= NULL
)
894 linux_nat_new_thread (ptid
);
899 /* Remove the LWP specified by PID from the list. */
902 delete_lwp (ptid_t ptid
)
904 struct lwp_info
*lp
, *lpprev
;
908 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
909 if (ptid_equal (lp
->ptid
, ptid
))
918 lpprev
->next
= lp
->next
;
925 /* Return a pointer to the structure describing the LWP corresponding
926 to PID. If no corresponding LWP could be found, return NULL. */
928 static struct lwp_info
*
929 find_lwp_pid (ptid_t ptid
)
935 lwp
= GET_LWP (ptid
);
937 lwp
= GET_PID (ptid
);
939 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
940 if (lwp
== GET_LWP (lp
->ptid
))
946 /* Call CALLBACK with its second argument set to DATA for every LWP in
947 the list. If CALLBACK returns 1 for a particular LWP, return a
948 pointer to the structure describing that LWP immediately.
949 Otherwise return NULL. */
952 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
954 struct lwp_info
*lp
, *lpnext
;
956 for (lp
= lwp_list
; lp
; lp
= lpnext
)
959 if ((*callback
) (lp
, data
))
966 /* Update our internal state when changing from one fork (checkpoint,
967 et cetera) to another indicated by NEW_PTID. We can only switch
968 single-threaded applications, so we only create one new LWP, and
969 the previous list is discarded. */
972 linux_nat_switch_fork (ptid_t new_ptid
)
978 lp
= add_lwp (new_ptid
);
979 add_thread_silent (new_ptid
);
983 /* Record a PTID for later deletion. */
988 struct saved_ptids
*next
;
990 static struct saved_ptids
*threads_to_delete
;
993 record_dead_thread (ptid_t ptid
)
995 struct saved_ptids
*p
= xmalloc (sizeof (struct saved_ptids
));
997 p
->next
= threads_to_delete
;
998 threads_to_delete
= p
;
1001 /* Delete any dead threads which are not the current thread. */
1006 struct saved_ptids
**p
= &threads_to_delete
;
1009 if (! ptid_equal ((*p
)->ptid
, inferior_ptid
))
1011 struct saved_ptids
*tmp
= *p
;
1012 delete_thread (tmp
->ptid
);
1020 /* Handle the exit of a single thread LP. */
1023 exit_lwp (struct lwp_info
*lp
)
1025 struct thread_info
*th
= find_thread_pid (lp
->ptid
);
1029 if (print_thread_events
)
1030 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1032 /* Core GDB cannot deal with us deleting the current thread. */
1033 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1034 delete_thread (lp
->ptid
);
1036 record_dead_thread (lp
->ptid
);
1039 delete_lwp (lp
->ptid
);
1042 /* Detect `T (stopped)' in `/proc/PID/status'.
1043 Other states including `T (tracing stop)' are reported as false. */
1046 pid_is_stopped (pid_t pid
)
1052 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) pid
);
1053 status_file
= fopen (buf
, "r");
1054 if (status_file
!= NULL
)
1058 while (fgets (buf
, sizeof (buf
), status_file
))
1060 if (strncmp (buf
, "State:", 6) == 0)
1066 if (have_state
&& strstr (buf
, "T (stopped)") != NULL
)
1068 fclose (status_file
);
1073 /* Wait for the LWP specified by LP, which we have just attached to.
1074 Returns a wait status for that LWP, to cache. */
1077 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1080 pid_t new_pid
, pid
= GET_LWP (ptid
);
1083 if (pid_is_stopped (pid
))
1085 if (debug_linux_nat
)
1086 fprintf_unfiltered (gdb_stdlog
,
1087 "LNPAW: Attaching to a stopped process\n");
1089 /* The process is definitely stopped. It is in a job control
1090 stop, unless the kernel predates the TASK_STOPPED /
1091 TASK_TRACED distinction, in which case it might be in a
1092 ptrace stop. Make sure it is in a ptrace stop; from there we
1093 can kill it, signal it, et cetera.
1095 First make sure there is a pending SIGSTOP. Since we are
1096 already attached, the process can not transition from stopped
1097 to running without a PTRACE_CONT; so we know this signal will
1098 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1099 probably already in the queue (unless this kernel is old
1100 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1101 is not an RT signal, it can only be queued once. */
1102 kill_lwp (pid
, SIGSTOP
);
1104 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1105 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1106 ptrace (PTRACE_CONT
, pid
, 0, 0);
1109 /* Make sure the initial process is stopped. The user-level threads
1110 layer might want to poke around in the inferior, and that won't
1111 work if things haven't stabilized yet. */
1112 new_pid
= my_waitpid (pid
, &status
, 0);
1113 if (new_pid
== -1 && errno
== ECHILD
)
1116 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1118 /* Try again with __WCLONE to check cloned processes. */
1119 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1123 gdb_assert (pid
== new_pid
&& WIFSTOPPED (status
));
1125 if (WSTOPSIG (status
) != SIGSTOP
)
1128 if (debug_linux_nat
)
1129 fprintf_unfiltered (gdb_stdlog
,
1130 "LNPAW: Received %s after attaching\n",
1131 status_to_str (status
));
1137 /* Attach to the LWP specified by PID. Return 0 if successful or -1
1138 if the new LWP could not be attached. */
1141 lin_lwp_attach_lwp (ptid_t ptid
)
1143 struct lwp_info
*lp
;
1144 int async_events_were_enabled
= 0;
1146 gdb_assert (is_lwp (ptid
));
1148 if (target_can_async_p ())
1149 async_events_were_enabled
= linux_nat_async_events (0);
1151 lp
= find_lwp_pid (ptid
);
1153 /* We assume that we're already attached to any LWP that has an id
1154 equal to the overall process id, and to any LWP that is already
1155 in our list of LWPs. If we're not seeing exit events from threads
1156 and we've had PID wraparound since we last tried to stop all threads,
1157 this assumption might be wrong; fortunately, this is very unlikely
1159 if (GET_LWP (ptid
) != GET_PID (ptid
) && lp
== NULL
)
1161 int status
, cloned
= 0, signalled
= 0;
1163 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
1165 /* If we fail to attach to the thread, issue a warning,
1166 but continue. One way this can happen is if thread
1167 creation is interrupted; as of Linux kernel 2.6.19, a
1168 bug may place threads in the thread list and then fail
1170 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1171 safe_strerror (errno
));
1175 if (debug_linux_nat
)
1176 fprintf_unfiltered (gdb_stdlog
,
1177 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1178 target_pid_to_str (ptid
));
1180 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1181 lp
= add_lwp (ptid
);
1183 lp
->cloned
= cloned
;
1184 lp
->signalled
= signalled
;
1185 if (WSTOPSIG (status
) != SIGSTOP
)
1188 lp
->status
= status
;
1191 target_post_attach (GET_LWP (lp
->ptid
));
1193 if (debug_linux_nat
)
1195 fprintf_unfiltered (gdb_stdlog
,
1196 "LLAL: waitpid %s received %s\n",
1197 target_pid_to_str (ptid
),
1198 status_to_str (status
));
1203 /* We assume that the LWP representing the original process is
1204 already stopped. Mark it as stopped in the data structure
1205 that the GNU/linux ptrace layer uses to keep track of
1206 threads. Note that this won't have already been done since
1207 the main thread will have, we assume, been stopped by an
1208 attach from a different layer. */
1210 lp
= add_lwp (ptid
);
1214 if (async_events_were_enabled
)
1215 linux_nat_async_events (1);
1221 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
1224 int saved_async
= 0;
1226 /* The fork_child mechanism is synchronous and calls target_wait, so
1227 we have to mask the async mode. */
1229 if (target_can_async_p ())
1230 saved_async
= linux_nat_async_mask (0);
1233 /* Restore the original signal mask. */
1234 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1235 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1236 suspend_mask
= normal_mask
;
1237 sigdelset (&suspend_mask
, SIGCHLD
);
1240 linux_ops
->to_create_inferior (exec_file
, allargs
, env
, from_tty
);
1243 linux_nat_async_mask (saved_async
);
1247 linux_nat_attach (char *args
, int from_tty
)
1249 struct lwp_info
*lp
;
1252 /* FIXME: We should probably accept a list of process id's, and
1253 attach all of them. */
1254 linux_ops
->to_attach (args
, from_tty
);
1256 if (!target_can_async_p ())
1258 /* Restore the original signal mask. */
1259 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1260 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1261 suspend_mask
= normal_mask
;
1262 sigdelset (&suspend_mask
, SIGCHLD
);
1265 /* Add the initial process as the first LWP to the list. */
1266 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1267 lp
= add_lwp (inferior_ptid
);
1269 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1273 /* If this process is not using thread_db, then we still don't
1274 detect any other threads, but add at least this one. */
1275 add_thread_silent (lp
->ptid
);
1277 /* Save the wait status to report later. */
1279 if (debug_linux_nat
)
1280 fprintf_unfiltered (gdb_stdlog
,
1281 "LNA: waitpid %ld, saving status %s\n",
1282 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1284 if (!target_can_async_p ())
1285 lp
->status
= status
;
1288 /* We already waited for this LWP, so put the wait result on the
1289 pipe. The event loop will wake up and gets us to handling
1291 linux_nat_event_pipe_push (GET_PID (lp
->ptid
), status
,
1292 lp
->cloned
? __WCLONE
: 0);
1293 /* Register in the event loop. */
1294 target_async (inferior_event_handler
, 0);
1298 /* Get pending status of LP. */
1300 get_pending_status (struct lwp_info
*lp
, int *status
)
1302 struct target_waitstatus last
;
1305 get_last_target_status (&last_ptid
, &last
);
1307 /* If this lwp is the ptid that GDB is processing an event from, the
1308 signal will be in stop_signal. Otherwise, in all-stop + sync
1309 mode, we may cache pending events in lp->status while trying to
1310 stop all threads (see stop_wait_callback). In async mode, the
1311 events are always cached in waitpid_queue. */
1314 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1316 if (stop_signal
!= TARGET_SIGNAL_0
1317 && signal_pass_state (stop_signal
))
1318 *status
= W_STOPCODE (target_signal_to_host (stop_signal
));
1320 else if (target_can_async_p ())
1321 queued_waitpid (GET_LWP (lp
->ptid
), status
, __WALL
);
1323 *status
= lp
->status
;
1329 detach_callback (struct lwp_info
*lp
, void *data
)
1331 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1333 if (debug_linux_nat
&& lp
->status
)
1334 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1335 strsignal (WSTOPSIG (lp
->status
)),
1336 target_pid_to_str (lp
->ptid
));
1338 /* If there is a pending SIGSTOP, get rid of it. */
1341 if (debug_linux_nat
)
1342 fprintf_unfiltered (gdb_stdlog
,
1343 "DC: Sending SIGCONT to %s\n",
1344 target_pid_to_str (lp
->ptid
));
1346 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1350 /* We don't actually detach from the LWP that has an id equal to the
1351 overall process id just yet. */
1352 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1356 /* Pass on any pending signal for this LWP. */
1357 get_pending_status (lp
, &status
);
1360 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1361 WSTOPSIG (status
)) < 0)
1362 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1363 safe_strerror (errno
));
1365 if (debug_linux_nat
)
1366 fprintf_unfiltered (gdb_stdlog
,
1367 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1368 target_pid_to_str (lp
->ptid
),
1369 strsignal (WSTOPSIG (lp
->status
)));
1371 delete_lwp (lp
->ptid
);
1378 linux_nat_detach (char *args
, int from_tty
)
1382 enum target_signal sig
;
1384 if (target_can_async_p ())
1385 linux_nat_async (NULL
, 0);
1387 iterate_over_lwps (detach_callback
, NULL
);
1389 /* Only the initial process should be left right now. */
1390 gdb_assert (num_lwps
== 1);
1392 /* Pass on any pending signal for the last LWP. */
1393 if ((args
== NULL
|| *args
== '\0')
1394 && get_pending_status (lwp_list
, &status
) != -1
1395 && WIFSTOPPED (status
))
1397 /* Put the signal number in ARGS so that inf_ptrace_detach will
1398 pass it along with PTRACE_DETACH. */
1400 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1401 fprintf_unfiltered (gdb_stdlog
,
1402 "LND: Sending signal %s to %s\n",
1404 target_pid_to_str (lwp_list
->ptid
));
1407 trap_ptid
= null_ptid
;
1409 /* Destroy LWP info; it's no longer valid. */
1412 pid
= GET_PID (inferior_ptid
);
1413 inferior_ptid
= pid_to_ptid (pid
);
1414 linux_ops
->to_detach (args
, from_tty
);
1416 if (target_can_async_p ())
1417 drain_queued_events (pid
);
1423 resume_callback (struct lwp_info
*lp
, void *data
)
1425 if (lp
->stopped
&& lp
->status
== 0)
1427 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
1428 0, TARGET_SIGNAL_0
);
1429 if (debug_linux_nat
)
1430 fprintf_unfiltered (gdb_stdlog
,
1431 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1432 target_pid_to_str (lp
->ptid
));
1435 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1442 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1449 resume_set_callback (struct lwp_info
*lp
, void *data
)
1456 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1458 struct lwp_info
*lp
;
1461 if (debug_linux_nat
)
1462 fprintf_unfiltered (gdb_stdlog
,
1463 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1464 step
? "step" : "resume",
1465 target_pid_to_str (ptid
),
1466 signo
? strsignal (signo
) : "0",
1467 target_pid_to_str (inferior_ptid
));
1471 if (target_can_async_p ())
1472 /* Block events while we're here. */
1473 linux_nat_async_events (0);
1475 /* A specific PTID means `step only this process id'. */
1476 resume_all
= (PIDGET (ptid
) == -1);
1479 iterate_over_lwps (resume_set_callback
, NULL
);
1481 iterate_over_lwps (resume_clear_callback
, NULL
);
1483 /* If PID is -1, it's the current inferior that should be
1484 handled specially. */
1485 if (PIDGET (ptid
) == -1)
1486 ptid
= inferior_ptid
;
1488 lp
= find_lwp_pid (ptid
);
1489 gdb_assert (lp
!= NULL
);
1491 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1493 /* Remember if we're stepping. */
1496 /* Mark this LWP as resumed. */
1499 /* If we have a pending wait status for this thread, there is no
1500 point in resuming the process. But first make sure that
1501 linux_nat_wait won't preemptively handle the event - we
1502 should never take this short-circuit if we are going to
1503 leave LP running, since we have skipped resuming all the
1504 other threads. This bit of code needs to be synchronized
1505 with linux_nat_wait. */
1507 /* In async mode, we never have pending wait status. */
1508 if (target_can_async_p () && lp
->status
)
1509 internal_error (__FILE__
, __LINE__
, "Pending status in async mode");
1511 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1513 int saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1515 if (signal_stop_state (saved_signo
) == 0
1516 && signal_print_state (saved_signo
) == 0
1517 && signal_pass_state (saved_signo
) == 1)
1519 if (debug_linux_nat
)
1520 fprintf_unfiltered (gdb_stdlog
,
1521 "LLR: Not short circuiting for ignored "
1522 "status 0x%x\n", lp
->status
);
1524 /* FIXME: What should we do if we are supposed to continue
1525 this thread with a signal? */
1526 gdb_assert (signo
== TARGET_SIGNAL_0
);
1527 signo
= saved_signo
;
1534 /* FIXME: What should we do if we are supposed to continue
1535 this thread with a signal? */
1536 gdb_assert (signo
== TARGET_SIGNAL_0
);
1538 if (debug_linux_nat
)
1539 fprintf_unfiltered (gdb_stdlog
,
1540 "LLR: Short circuiting for status 0x%x\n",
1546 /* Mark LWP as not stopped to prevent it from being continued by
1551 iterate_over_lwps (resume_callback
, NULL
);
1553 linux_ops
->to_resume (ptid
, step
, signo
);
1554 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1556 if (debug_linux_nat
)
1557 fprintf_unfiltered (gdb_stdlog
,
1558 "LLR: %s %s, %s (resume event thread)\n",
1559 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1560 target_pid_to_str (ptid
),
1561 signo
? strsignal (signo
) : "0");
1563 if (target_can_async_p ())
1565 target_executing
= 1;
1566 target_async (inferior_event_handler
, 0);
1570 /* Issue kill to specified lwp. */
1572 static int tkill_failed
;
1575 kill_lwp (int lwpid
, int signo
)
1579 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1580 fails, then we are not using nptl threads and we should be using kill. */
1582 #ifdef HAVE_TKILL_SYSCALL
1585 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1586 if (errno
!= ENOSYS
)
1593 return kill (lwpid
, signo
);
1596 /* Handle a GNU/Linux extended wait response. If we see a clone
1597 event, we need to add the new LWP to our list (and not report the
1598 trap to higher layers). This function returns non-zero if the
1599 event should be ignored and we should wait again. If STOPPING is
1600 true, the new LWP remains stopped, otherwise it is continued. */
1603 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
1606 int pid
= GET_LWP (lp
->ptid
);
1607 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1608 struct lwp_info
*new_lp
= NULL
;
1609 int event
= status
>> 16;
1611 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1612 || event
== PTRACE_EVENT_CLONE
)
1614 unsigned long new_pid
;
1617 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1619 /* If we haven't already seen the new PID stop, wait for it now. */
1620 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1622 /* The new child has a pending SIGSTOP. We can't affect it until it
1623 hits the SIGSTOP, but we're already attached. */
1624 ret
= my_waitpid (new_pid
, &status
,
1625 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
1627 perror_with_name (_("waiting for new child"));
1628 else if (ret
!= new_pid
)
1629 internal_error (__FILE__
, __LINE__
,
1630 _("wait returned unexpected PID %d"), ret
);
1631 else if (!WIFSTOPPED (status
))
1632 internal_error (__FILE__
, __LINE__
,
1633 _("wait returned unexpected status 0x%x"), status
);
1636 ourstatus
->value
.related_pid
= new_pid
;
1638 if (event
== PTRACE_EVENT_FORK
)
1639 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1640 else if (event
== PTRACE_EVENT_VFORK
)
1641 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1644 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1645 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (inferior_ptid
)));
1648 if (WSTOPSIG (status
) != SIGSTOP
)
1650 /* This can happen if someone starts sending signals to
1651 the new thread before it gets a chance to run, which
1652 have a lower number than SIGSTOP (e.g. SIGUSR1).
1653 This is an unlikely case, and harder to handle for
1654 fork / vfork than for clone, so we do not try - but
1655 we handle it for clone events here. We'll send
1656 the other signal on to the thread below. */
1658 new_lp
->signalled
= 1;
1664 new_lp
->stopped
= 1;
1667 new_lp
->resumed
= 1;
1668 ptrace (PTRACE_CONT
, lp
->waitstatus
.value
.related_pid
, 0,
1669 status
? WSTOPSIG (status
) : 0);
1672 if (debug_linux_nat
)
1673 fprintf_unfiltered (gdb_stdlog
,
1674 "LHEW: Got clone event from LWP %ld, resuming\n",
1675 GET_LWP (lp
->ptid
));
1676 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1684 if (event
== PTRACE_EVENT_EXEC
)
1686 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1687 ourstatus
->value
.execd_pathname
1688 = xstrdup (linux_child_pid_to_exec_file (pid
));
1690 if (linux_parent_pid
)
1692 detach_breakpoints (linux_parent_pid
);
1693 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
1695 linux_parent_pid
= 0;
1701 internal_error (__FILE__
, __LINE__
,
1702 _("unknown ptrace event %d"), event
);
1705 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1709 wait_lwp (struct lwp_info
*lp
)
1713 int thread_dead
= 0;
1715 gdb_assert (!lp
->stopped
);
1716 gdb_assert (lp
->status
== 0);
1718 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1719 if (pid
== -1 && errno
== ECHILD
)
1721 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1722 if (pid
== -1 && errno
== ECHILD
)
1724 /* The thread has previously exited. We need to delete it
1725 now because, for some vendor 2.4 kernels with NPTL
1726 support backported, there won't be an exit event unless
1727 it is the main thread. 2.6 kernels will report an exit
1728 event for each thread that exits, as expected. */
1730 if (debug_linux_nat
)
1731 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1732 target_pid_to_str (lp
->ptid
));
1738 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1740 if (debug_linux_nat
)
1742 fprintf_unfiltered (gdb_stdlog
,
1743 "WL: waitpid %s received %s\n",
1744 target_pid_to_str (lp
->ptid
),
1745 status_to_str (status
));
1749 /* Check if the thread has exited. */
1750 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1753 if (debug_linux_nat
)
1754 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1755 target_pid_to_str (lp
->ptid
));
1764 gdb_assert (WIFSTOPPED (status
));
1766 /* Handle GNU/Linux's extended waitstatus for trace events. */
1767 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1769 if (debug_linux_nat
)
1770 fprintf_unfiltered (gdb_stdlog
,
1771 "WL: Handling extended status 0x%06x\n",
1773 if (linux_handle_extended_wait (lp
, status
, 1))
1774 return wait_lwp (lp
);
1780 /* Save the most recent siginfo for LP. This is currently only called
1781 for SIGTRAP; some ports use the si_addr field for
1782 target_stopped_data_address. In the future, it may also be used to
1783 restore the siginfo of requeued signals. */
1786 save_siginfo (struct lwp_info
*lp
)
1789 ptrace (PTRACE_GETSIGINFO
, GET_LWP (lp
->ptid
),
1790 (PTRACE_TYPE_ARG3
) 0, &lp
->siginfo
);
1793 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1796 /* Send a SIGSTOP to LP. */
1799 stop_callback (struct lwp_info
*lp
, void *data
)
1801 if (!lp
->stopped
&& !lp
->signalled
)
1805 if (debug_linux_nat
)
1807 fprintf_unfiltered (gdb_stdlog
,
1808 "SC: kill %s **<SIGSTOP>**\n",
1809 target_pid_to_str (lp
->ptid
));
1812 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1813 if (debug_linux_nat
)
1815 fprintf_unfiltered (gdb_stdlog
,
1816 "SC: lwp kill %d %s\n",
1818 errno
? safe_strerror (errno
) : "ERRNO-OK");
1822 gdb_assert (lp
->status
== 0);
1828 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1829 a pointer to a set of signals to be flushed immediately. */
1832 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1834 sigset_t
*flush_mask
= data
;
1840 status
= wait_lwp (lp
);
1844 /* Ignore any signals in FLUSH_MASK. */
1845 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1854 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1855 if (debug_linux_nat
)
1856 fprintf_unfiltered (gdb_stdlog
,
1857 "PTRACE_CONT %s, 0, 0 (%s)\n",
1858 target_pid_to_str (lp
->ptid
),
1859 errno
? safe_strerror (errno
) : "OK");
1861 return stop_wait_callback (lp
, flush_mask
);
1864 if (WSTOPSIG (status
) != SIGSTOP
)
1866 if (WSTOPSIG (status
) == SIGTRAP
)
1868 /* If a LWP other than the LWP that we're reporting an
1869 event for has hit a GDB breakpoint (as opposed to
1870 some random trap signal), then just arrange for it to
1871 hit it again later. We don't keep the SIGTRAP status
1872 and don't forward the SIGTRAP signal to the LWP. We
1873 will handle the current event, eventually we will
1874 resume all LWPs, and this one will get its breakpoint
1877 If we do not do this, then we run the risk that the
1878 user will delete or disable the breakpoint, but the
1879 thread will have already tripped on it. */
1881 /* Save the trap's siginfo in case we need it later. */
1884 /* Now resume this LWP and get the SIGSTOP event. */
1886 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1887 if (debug_linux_nat
)
1889 fprintf_unfiltered (gdb_stdlog
,
1890 "PTRACE_CONT %s, 0, 0 (%s)\n",
1891 target_pid_to_str (lp
->ptid
),
1892 errno
? safe_strerror (errno
) : "OK");
1894 fprintf_unfiltered (gdb_stdlog
,
1895 "SWC: Candidate SIGTRAP event in %s\n",
1896 target_pid_to_str (lp
->ptid
));
1898 /* Hold this event/waitstatus while we check to see if
1899 there are any more (we still want to get that SIGSTOP). */
1900 stop_wait_callback (lp
, data
);
1902 if (target_can_async_p ())
1904 /* Don't leave a pending wait status in async mode.
1905 Retrigger the breakpoint. */
1906 if (!cancel_breakpoint (lp
))
1908 /* There was no gdb breakpoint set at pc. Put
1909 the event back in the queue. */
1910 if (debug_linux_nat
)
1911 fprintf_unfiltered (gdb_stdlog
,
1912 "SWC: kill %s, %s\n",
1913 target_pid_to_str (lp
->ptid
),
1914 status_to_str ((int) status
));
1915 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1920 /* Hold the SIGTRAP for handling by
1922 /* If there's another event, throw it back into the
1926 if (debug_linux_nat
)
1927 fprintf_unfiltered (gdb_stdlog
,
1928 "SWC: kill %s, %s\n",
1929 target_pid_to_str (lp
->ptid
),
1930 status_to_str ((int) status
));
1931 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1933 /* Save the sigtrap event. */
1934 lp
->status
= status
;
1940 /* The thread was stopped with a signal other than
1941 SIGSTOP, and didn't accidentally trip a breakpoint. */
1943 if (debug_linux_nat
)
1945 fprintf_unfiltered (gdb_stdlog
,
1946 "SWC: Pending event %s in %s\n",
1947 status_to_str ((int) status
),
1948 target_pid_to_str (lp
->ptid
));
1950 /* Now resume this LWP and get the SIGSTOP event. */
1952 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1953 if (debug_linux_nat
)
1954 fprintf_unfiltered (gdb_stdlog
,
1955 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1956 target_pid_to_str (lp
->ptid
),
1957 errno
? safe_strerror (errno
) : "OK");
1959 /* Hold this event/waitstatus while we check to see if
1960 there are any more (we still want to get that SIGSTOP). */
1961 stop_wait_callback (lp
, data
);
1963 /* If the lp->status field is still empty, use it to
1964 hold this event. If not, then this event must be
1965 returned to the event queue of the LWP. */
1966 if (lp
->status
|| target_can_async_p ())
1968 if (debug_linux_nat
)
1970 fprintf_unfiltered (gdb_stdlog
,
1971 "SWC: kill %s, %s\n",
1972 target_pid_to_str (lp
->ptid
),
1973 status_to_str ((int) status
));
1975 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1978 lp
->status
= status
;
1984 /* We caught the SIGSTOP that we intended to catch, so
1985 there's no SIGSTOP pending. */
1994 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1995 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1998 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
2000 sigset_t blocked
, ignored
;
2003 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
2008 for (i
= 1; i
< NSIG
; i
++)
2009 if (sigismember (pending
, i
))
2010 if (!sigismember (flush_mask
, i
)
2011 || sigismember (&blocked
, i
)
2012 || sigismember (&ignored
, i
))
2013 sigdelset (pending
, i
);
2015 if (sigisemptyset (pending
))
2021 /* DATA is interpreted as a mask of signals to flush. If LP has
2022 signals pending, and they are all in the flush mask, then arrange
2023 to flush them. LP should be stopped, as should all other threads
2024 it might share a signal queue with. */
2027 flush_callback (struct lwp_info
*lp
, void *data
)
2029 sigset_t
*flush_mask
= data
;
2030 sigset_t pending
, intersection
, blocked
, ignored
;
2033 /* Normally, when an LWP exits, it is removed from the LWP list. The
2034 last LWP isn't removed till later, however. So if there is only
2035 one LWP on the list, make sure it's alive. */
2036 if (lwp_list
== lp
&& lp
->next
== NULL
)
2037 if (!linux_nat_thread_alive (lp
->ptid
))
2040 /* Just because the LWP is stopped doesn't mean that new signals
2041 can't arrive from outside, so this function must be careful of
2042 race conditions. However, because all threads are stopped, we
2043 can assume that the pending mask will not shrink unless we resume
2044 the LWP, and that it will then get another signal. We can't
2045 control which one, however. */
2049 if (debug_linux_nat
)
2050 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp
->status
);
2051 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
2055 /* While there is a pending signal we would like to flush, continue
2056 the inferior and collect another signal. But if there's already
2057 a saved status that we don't want to flush, we can't resume the
2058 inferior - if it stopped for some other reason we wouldn't have
2059 anywhere to save the new status. In that case, we must leave the
2060 signal unflushed (and possibly generate an extra SIGINT stop).
2061 That's much less bad than losing a signal. */
2062 while (lp
->status
== 0
2063 && linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
2068 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2069 if (debug_linux_nat
)
2070 fprintf_unfiltered (gdb_stderr
,
2071 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
2074 stop_wait_callback (lp
, flush_mask
);
2075 if (debug_linux_nat
)
2076 fprintf_unfiltered (gdb_stderr
,
2077 "FC: Wait finished; saved status is %d\n",
2084 /* Return non-zero if LP has a wait status pending. */
2087 status_callback (struct lwp_info
*lp
, void *data
)
2089 /* Only report a pending wait status if we pretend that this has
2090 indeed been resumed. */
2091 return (lp
->status
!= 0 && lp
->resumed
);
2094 /* Return non-zero if LP isn't stopped. */
2097 running_callback (struct lwp_info
*lp
, void *data
)
2099 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
2102 /* Count the LWP's that have had events. */
2105 count_events_callback (struct lwp_info
*lp
, void *data
)
2109 gdb_assert (count
!= NULL
);
2111 /* Count only LWPs that have a SIGTRAP event pending. */
2113 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2119 /* Select the LWP (if any) that is currently being single-stepped. */
2122 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2124 if (lp
->step
&& lp
->status
!= 0)
2130 /* Select the Nth LWP that has had a SIGTRAP event. */
2133 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2135 int *selector
= data
;
2137 gdb_assert (selector
!= NULL
);
2139 /* Select only LWPs that have a SIGTRAP event pending. */
2141 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2142 if ((*selector
)-- == 0)
2149 cancel_breakpoint (struct lwp_info
*lp
)
2151 /* Arrange for a breakpoint to be hit again later. We don't keep
2152 the SIGTRAP status and don't forward the SIGTRAP signal to the
2153 LWP. We will handle the current event, eventually we will resume
2154 this LWP, and this breakpoint will trap again.
2156 If we do not do this, then we run the risk that the user will
2157 delete or disable the breakpoint, but the LWP will have already
2160 if (breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
2161 gdbarch_decr_pc_after_break
2164 if (debug_linux_nat
)
2165 fprintf_unfiltered (gdb_stdlog
,
2166 "CB: Push back breakpoint for %s\n",
2167 target_pid_to_str (lp
->ptid
));
2169 /* Back up the PC if necessary. */
2170 if (gdbarch_decr_pc_after_break (current_gdbarch
))
2171 write_pc_pid (read_pc_pid (lp
->ptid
) - gdbarch_decr_pc_after_break
2180 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
2182 struct lwp_info
*event_lp
= data
;
2184 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2188 /* If a LWP other than the LWP that we're reporting an event for has
2189 hit a GDB breakpoint (as opposed to some random trap signal),
2190 then just arrange for it to hit it again later. We don't keep
2191 the SIGTRAP status and don't forward the SIGTRAP signal to the
2192 LWP. We will handle the current event, eventually we will resume
2193 all LWPs, and this one will get its breakpoint trap again.
2195 If we do not do this, then we run the risk that the user will
2196 delete or disable the breakpoint, but the LWP will have already
2200 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
2201 && cancel_breakpoint (lp
))
2202 /* Throw away the SIGTRAP. */
2208 /* Select one LWP out of those that have events pending. */
2211 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
2214 int random_selector
;
2215 struct lwp_info
*event_lp
;
2217 /* Record the wait status for the original LWP. */
2218 (*orig_lp
)->status
= *status
;
2220 /* Give preference to any LWP that is being single-stepped. */
2221 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
2222 if (event_lp
!= NULL
)
2224 if (debug_linux_nat
)
2225 fprintf_unfiltered (gdb_stdlog
,
2226 "SEL: Select single-step %s\n",
2227 target_pid_to_str (event_lp
->ptid
));
2231 /* No single-stepping LWP. Select one at random, out of those
2232 which have had SIGTRAP events. */
2234 /* First see how many SIGTRAP events we have. */
2235 iterate_over_lwps (count_events_callback
, &num_events
);
2237 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2238 random_selector
= (int)
2239 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2241 if (debug_linux_nat
&& num_events
> 1)
2242 fprintf_unfiltered (gdb_stdlog
,
2243 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2244 num_events
, random_selector
);
2246 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
2250 if (event_lp
!= NULL
)
2252 /* Switch the event LWP. */
2253 *orig_lp
= event_lp
;
2254 *status
= event_lp
->status
;
2257 /* Flush the wait status for the event LWP. */
2258 (*orig_lp
)->status
= 0;
2261 /* Return non-zero if LP has been resumed. */
2264 resumed_callback (struct lwp_info
*lp
, void *data
)
2269 /* Stop an active thread, verify it still exists, then resume it. */
2272 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2274 struct lwp_info
*ptr
;
2276 if (!lp
->stopped
&& !lp
->signalled
)
2278 stop_callback (lp
, NULL
);
2279 stop_wait_callback (lp
, NULL
);
2280 /* Resume if the lwp still exists. */
2281 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
2284 resume_callback (lp
, NULL
);
2285 resume_set_callback (lp
, NULL
);
2291 /* Check if we should go on and pass this event to common code.
2292 Return the affected lwp if we are, or NULL otherwise. */
2293 static struct lwp_info
*
2294 linux_nat_filter_event (int lwpid
, int status
, int options
)
2296 struct lwp_info
*lp
;
2298 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2300 /* Check for stop events reported by a process we didn't already
2301 know about - anything not already in our LWP list.
2303 If we're expecting to receive stopped processes after
2304 fork, vfork, and clone events, then we'll just add the
2305 new one to our list and go back to waiting for the event
2306 to be reported - the stopped process might be returned
2307 from waitpid before or after the event is. */
2308 if (WIFSTOPPED (status
) && !lp
)
2310 linux_record_stopped_pid (lwpid
, status
);
2314 /* Make sure we don't report an event for the exit of an LWP not in
2315 our list, i.e. not part of the current process. This can happen
2316 if we detach from a program we original forked and then it
2318 if (!WIFSTOPPED (status
) && !lp
)
2321 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2322 CLONE_PTRACE processes which do not use the thread library -
2323 otherwise we wouldn't find the new LWP this way. That doesn't
2324 currently work, and the following code is currently unreachable
2325 due to the two blocks above. If it's fixed some day, this code
2326 should be broken out into a function so that we can also pick up
2327 LWPs from the new interface. */
2330 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
2331 if (options
& __WCLONE
)
2334 gdb_assert (WIFSTOPPED (status
)
2335 && WSTOPSIG (status
) == SIGSTOP
);
2338 if (!in_thread_list (inferior_ptid
))
2340 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2341 GET_PID (inferior_ptid
));
2342 add_thread (inferior_ptid
);
2345 add_thread (lp
->ptid
);
2348 /* Save the trap's siginfo in case we need it later. */
2349 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2352 /* Handle GNU/Linux's extended waitstatus for trace events. */
2353 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2355 if (debug_linux_nat
)
2356 fprintf_unfiltered (gdb_stdlog
,
2357 "LLW: Handling extended status 0x%06x\n",
2359 if (linux_handle_extended_wait (lp
, status
, 0))
2363 /* Check if the thread has exited. */
2364 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
2366 /* If this is the main thread, we must stop all threads and
2367 verify if they are still alive. This is because in the nptl
2368 thread model, there is no signal issued for exiting LWPs
2369 other than the main thread. We only get the main thread exit
2370 signal once all child threads have already exited. If we
2371 stop all the threads and use the stop_wait_callback to check
2372 if they have exited we can determine whether this signal
2373 should be ignored or whether it means the end of the debugged
2374 application, regardless of which threading model is being
2376 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2379 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2382 if (debug_linux_nat
)
2383 fprintf_unfiltered (gdb_stdlog
,
2384 "LLW: %s exited.\n",
2385 target_pid_to_str (lp
->ptid
));
2389 /* If there is at least one more LWP, then the exit signal was
2390 not the end of the debugged application and should be
2394 /* Make sure there is at least one thread running. */
2395 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2397 /* Discard the event. */
2402 /* Check if the current LWP has previously exited. In the nptl
2403 thread model, LWPs other than the main thread do not issue
2404 signals when they exit so we must check whenever the thread has
2405 stopped. A similar check is made in stop_wait_callback(). */
2406 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2408 if (debug_linux_nat
)
2409 fprintf_unfiltered (gdb_stdlog
,
2410 "LLW: %s exited.\n",
2411 target_pid_to_str (lp
->ptid
));
2415 /* Make sure there is at least one thread running. */
2416 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2418 /* Discard the event. */
2422 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2423 an attempt to stop an LWP. */
2425 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2427 if (debug_linux_nat
)
2428 fprintf_unfiltered (gdb_stdlog
,
2429 "LLW: Delayed SIGSTOP caught for %s.\n",
2430 target_pid_to_str (lp
->ptid
));
2432 /* This is a delayed SIGSTOP. */
2435 registers_changed ();
2437 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2438 lp
->step
, TARGET_SIGNAL_0
);
2439 if (debug_linux_nat
)
2440 fprintf_unfiltered (gdb_stdlog
,
2441 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2443 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2444 target_pid_to_str (lp
->ptid
));
2447 gdb_assert (lp
->resumed
);
2449 /* Discard the event. */
2453 /* An interesting event. */
2458 /* Get the events stored in the pipe into the local queue, so they are
2459 accessible to queued_waitpid. We need to do this, since it is not
2460 always the case that the event at the head of the pipe is the event
2464 pipe_to_local_event_queue (void)
2466 if (debug_linux_nat_async
)
2467 fprintf_unfiltered (gdb_stdlog
,
2468 "PTLEQ: linux_nat_num_queued_events(%d)\n",
2469 linux_nat_num_queued_events
);
2470 while (linux_nat_num_queued_events
)
2472 int lwpid
, status
, options
;
2473 lwpid
= linux_nat_event_pipe_pop (&status
, &options
);
2474 gdb_assert (lwpid
> 0);
2475 push_waitpid (lwpid
, status
, options
);
2479 /* Get the unprocessed events stored in the local queue back into the
2480 pipe, so the event loop realizes there's something else to
2484 local_event_queue_to_pipe (void)
2486 struct waitpid_result
*w
= waitpid_queue
;
2489 struct waitpid_result
*next
= w
->next
;
2490 linux_nat_event_pipe_push (w
->pid
,
2496 waitpid_queue
= NULL
;
2498 if (debug_linux_nat_async
)
2499 fprintf_unfiltered (gdb_stdlog
,
2500 "LEQTP: linux_nat_num_queued_events(%d)\n",
2501 linux_nat_num_queued_events
);
2505 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
2507 struct lwp_info
*lp
= NULL
;
2510 pid_t pid
= PIDGET (ptid
);
2511 sigset_t flush_mask
;
2513 if (debug_linux_nat_async
)
2514 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
2516 /* The first time we get here after starting a new inferior, we may
2517 not have added it to the LWP list yet - this is the earliest
2518 moment at which we know its PID. */
2521 gdb_assert (!is_lwp (inferior_ptid
));
2523 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2524 GET_PID (inferior_ptid
));
2525 lp
= add_lwp (inferior_ptid
);
2527 /* Add the main thread to GDB's thread list. */
2528 add_thread_silent (lp
->ptid
);
2531 sigemptyset (&flush_mask
);
2533 if (target_can_async_p ())
2534 /* Block events while we're here. */
2535 target_async (NULL
, 0);
2539 /* Make sure there is at least one LWP that has been resumed. */
2540 gdb_assert (iterate_over_lwps (resumed_callback
, NULL
));
2542 /* First check if there is a LWP with a wait status pending. */
2545 /* Any LWP that's been resumed will do. */
2546 lp
= iterate_over_lwps (status_callback
, NULL
);
2549 if (target_can_async_p ())
2550 internal_error (__FILE__
, __LINE__
,
2551 "Found an LWP with a pending status in async mode.");
2553 status
= lp
->status
;
2556 if (debug_linux_nat
&& status
)
2557 fprintf_unfiltered (gdb_stdlog
,
2558 "LLW: Using pending wait status %s for %s.\n",
2559 status_to_str (status
),
2560 target_pid_to_str (lp
->ptid
));
2563 /* But if we don't find one, we'll have to wait, and check both
2564 cloned and uncloned processes. We start with the cloned
2566 options
= __WCLONE
| WNOHANG
;
2568 else if (is_lwp (ptid
))
2570 if (debug_linux_nat
)
2571 fprintf_unfiltered (gdb_stdlog
,
2572 "LLW: Waiting for specific LWP %s.\n",
2573 target_pid_to_str (ptid
));
2575 /* We have a specific LWP to check. */
2576 lp
= find_lwp_pid (ptid
);
2578 status
= lp
->status
;
2581 if (debug_linux_nat
&& status
)
2582 fprintf_unfiltered (gdb_stdlog
,
2583 "LLW: Using pending wait status %s for %s.\n",
2584 status_to_str (status
),
2585 target_pid_to_str (lp
->ptid
));
2587 /* If we have to wait, take into account whether PID is a cloned
2588 process or not. And we have to convert it to something that
2589 the layer beneath us can understand. */
2590 options
= lp
->cloned
? __WCLONE
: 0;
2591 pid
= GET_LWP (ptid
);
2594 if (status
&& lp
->signalled
)
2596 /* A pending SIGSTOP may interfere with the normal stream of
2597 events. In a typical case where interference is a problem,
2598 we have a SIGSTOP signal pending for LWP A while
2599 single-stepping it, encounter an event in LWP B, and take the
2600 pending SIGSTOP while trying to stop LWP A. After processing
2601 the event in LWP B, LWP A is continued, and we'll never see
2602 the SIGTRAP associated with the last time we were
2603 single-stepping LWP A. */
2605 /* Resume the thread. It should halt immediately returning the
2607 registers_changed ();
2608 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2609 lp
->step
, TARGET_SIGNAL_0
);
2610 if (debug_linux_nat
)
2611 fprintf_unfiltered (gdb_stdlog
,
2612 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2613 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2614 target_pid_to_str (lp
->ptid
));
2616 gdb_assert (lp
->resumed
);
2618 /* This should catch the pending SIGSTOP. */
2619 stop_wait_callback (lp
, NULL
);
2622 if (!target_can_async_p ())
2624 /* Causes SIGINT to be passed on to the attached process. */
2633 if (target_can_async_p ())
2634 /* In async mode, don't ever block. Only look at the locally
2636 lwpid
= queued_waitpid (pid
, &status
, options
);
2638 lwpid
= my_waitpid (pid
, &status
, options
);
2642 gdb_assert (pid
== -1 || lwpid
== pid
);
2644 if (debug_linux_nat
)
2646 fprintf_unfiltered (gdb_stdlog
,
2647 "LLW: waitpid %ld received %s\n",
2648 (long) lwpid
, status_to_str (status
));
2651 lp
= linux_nat_filter_event (lwpid
, status
, options
);
2654 /* A discarded event. */
2664 /* Alternate between checking cloned and uncloned processes. */
2665 options
^= __WCLONE
;
2667 /* And every time we have checked both:
2668 In async mode, return to event loop;
2669 In sync mode, suspend waiting for a SIGCHLD signal. */
2670 if (options
& __WCLONE
)
2672 if (target_can_async_p ())
2674 /* No interesting event. */
2675 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2677 /* Get ready for the next event. */
2678 target_async (inferior_event_handler
, 0);
2680 if (debug_linux_nat_async
)
2681 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
2683 return minus_one_ptid
;
2686 sigsuspend (&suspend_mask
);
2690 /* We shouldn't end up here unless we want to try again. */
2691 gdb_assert (status
== 0);
2694 if (!target_can_async_p ())
2696 clear_sigio_trap ();
2697 clear_sigint_trap ();
2702 /* Don't report signals that GDB isn't interested in, such as
2703 signals that are neither printed nor stopped upon. Stopping all
2704 threads can be a bit time-consuming so if we want decent
2705 performance with heavily multi-threaded programs, especially when
2706 they're using a high frequency timer, we'd better avoid it if we
2709 if (WIFSTOPPED (status
))
2711 int signo
= target_signal_from_host (WSTOPSIG (status
));
2713 /* If we get a signal while single-stepping, we may need special
2714 care, e.g. to skip the signal handler. Defer to common code. */
2716 && signal_stop_state (signo
) == 0
2717 && signal_print_state (signo
) == 0
2718 && signal_pass_state (signo
) == 1)
2720 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2721 here? It is not clear we should. GDB may not expect
2722 other threads to run. On the other hand, not resuming
2723 newly attached threads may cause an unwanted delay in
2724 getting them running. */
2725 registers_changed ();
2726 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2728 if (debug_linux_nat
)
2729 fprintf_unfiltered (gdb_stdlog
,
2730 "LLW: %s %s, %s (preempt 'handle')\n",
2732 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2733 target_pid_to_str (lp
->ptid
),
2734 signo
? strsignal (signo
) : "0");
2740 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2742 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2743 forwarded to the entire process group, that is, all LWP's
2744 will receive it. Since we only want to report it once,
2745 we try to flush it from all LWPs except this one. */
2746 sigaddset (&flush_mask
, SIGINT
);
2750 /* This LWP is stopped now. */
2753 if (debug_linux_nat
)
2754 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2755 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2757 /* Now stop all other LWP's ... */
2758 iterate_over_lwps (stop_callback
, NULL
);
2760 /* ... and wait until all of them have reported back that they're no
2762 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2763 iterate_over_lwps (flush_callback
, &flush_mask
);
2765 /* If we're not waiting for a specific LWP, choose an event LWP from
2766 among those that have had events. Giving equal priority to all
2767 LWPs that have had events helps prevent starvation. */
2769 select_event_lwp (&lp
, &status
);
2771 /* Now that we've selected our final event LWP, cancel any
2772 breakpoints in other LWPs that have hit a GDB breakpoint. See
2773 the comment in cancel_breakpoints_callback to find out why. */
2774 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2776 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2778 trap_ptid
= lp
->ptid
;
2779 if (debug_linux_nat
)
2780 fprintf_unfiltered (gdb_stdlog
,
2781 "LLW: trap_ptid is %s.\n",
2782 target_pid_to_str (trap_ptid
));
2785 trap_ptid
= null_ptid
;
2787 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2789 *ourstatus
= lp
->waitstatus
;
2790 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2793 store_waitstatus (ourstatus
, status
);
2795 /* Get ready for the next event. */
2796 if (target_can_async_p ())
2797 target_async (inferior_event_handler
, 0);
2799 if (debug_linux_nat_async
)
2800 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
2806 kill_callback (struct lwp_info
*lp
, void *data
)
2809 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2810 if (debug_linux_nat
)
2811 fprintf_unfiltered (gdb_stdlog
,
2812 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2813 target_pid_to_str (lp
->ptid
),
2814 errno
? safe_strerror (errno
) : "OK");
2820 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2824 /* We must make sure that there are no pending events (delayed
2825 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2826 program doesn't interfere with any following debugging session. */
2828 /* For cloned processes we must check both with __WCLONE and
2829 without, since the exit status of a cloned process isn't reported
2835 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2836 if (pid
!= (pid_t
) -1)
2838 if (debug_linux_nat
)
2839 fprintf_unfiltered (gdb_stdlog
,
2840 "KWC: wait %s received unknown.\n",
2841 target_pid_to_str (lp
->ptid
));
2842 /* The Linux kernel sometimes fails to kill a thread
2843 completely after PTRACE_KILL; that goes from the stop
2844 point in do_fork out to the one in
2845 get_signal_to_deliever and waits again. So kill it
2847 kill_callback (lp
, NULL
);
2850 while (pid
== GET_LWP (lp
->ptid
));
2852 gdb_assert (pid
== -1 && errno
== ECHILD
);
2857 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2858 if (pid
!= (pid_t
) -1)
2860 if (debug_linux_nat
)
2861 fprintf_unfiltered (gdb_stdlog
,
2862 "KWC: wait %s received unk.\n",
2863 target_pid_to_str (lp
->ptid
));
2864 /* See the call to kill_callback above. */
2865 kill_callback (lp
, NULL
);
2868 while (pid
== GET_LWP (lp
->ptid
));
2870 gdb_assert (pid
== -1 && errno
== ECHILD
);
2875 linux_nat_kill (void)
2877 struct target_waitstatus last
;
2881 if (target_can_async_p ())
2882 target_async (NULL
, 0);
2884 /* If we're stopped while forking and we haven't followed yet,
2885 kill the other task. We need to do this first because the
2886 parent will be sleeping if this is a vfork. */
2888 get_last_target_status (&last_ptid
, &last
);
2890 if (last
.kind
== TARGET_WAITKIND_FORKED
2891 || last
.kind
== TARGET_WAITKIND_VFORKED
)
2893 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
2897 if (forks_exist_p ())
2899 linux_fork_killall ();
2900 drain_queued_events (-1);
2904 /* Kill all LWP's ... */
2905 iterate_over_lwps (kill_callback
, NULL
);
2907 /* ... and wait until we've flushed all events. */
2908 iterate_over_lwps (kill_wait_callback
, NULL
);
2911 target_mourn_inferior ();
2915 linux_nat_mourn_inferior (void)
2917 trap_ptid
= null_ptid
;
2919 /* Destroy LWP info; it's no longer valid. */
2922 if (! forks_exist_p ())
2924 /* Normal case, no other forks available. */
2925 if (target_can_async_p ())
2926 linux_nat_async (NULL
, 0);
2927 linux_ops
->to_mourn_inferior ();
2930 /* Multi-fork case. The current inferior_ptid has exited, but
2931 there are other viable forks to debug. Delete the exiting
2932 one and context-switch to the first available. */
2933 linux_fork_mourn_inferior ();
2937 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2938 const char *annex
, gdb_byte
*readbuf
,
2939 const gdb_byte
*writebuf
,
2940 ULONGEST offset
, LONGEST len
)
2942 struct cleanup
*old_chain
= save_inferior_ptid ();
2945 if (is_lwp (inferior_ptid
))
2946 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2948 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
2951 do_cleanups (old_chain
);
2956 linux_nat_thread_alive (ptid_t ptid
)
2958 gdb_assert (is_lwp (ptid
));
2961 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2962 if (debug_linux_nat
)
2963 fprintf_unfiltered (gdb_stdlog
,
2964 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2965 target_pid_to_str (ptid
),
2966 errno
? safe_strerror (errno
) : "OK");
2968 /* Not every Linux kernel implements PTRACE_PEEKUSER. But we can
2969 handle that case gracefully since ptrace will first do a lookup
2970 for the process based upon the passed-in pid. If that fails we
2971 will get either -ESRCH or -EPERM, otherwise the child exists and
2973 if (errno
== ESRCH
|| errno
== EPERM
)
2980 linux_nat_pid_to_str (ptid_t ptid
)
2982 static char buf
[64];
2985 && ((lwp_list
&& lwp_list
->next
)
2986 || GET_PID (ptid
) != GET_LWP (ptid
)))
2988 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2992 return normal_pid_to_str (ptid
);
2996 sigchld_handler (int signo
)
2998 if (linux_nat_async_enabled
2999 && linux_nat_async_events_enabled
3000 && signo
== SIGCHLD
)
3001 /* It is *always* a bug to hit this. */
3002 internal_error (__FILE__
, __LINE__
,
3003 "sigchld_handler called when async events are enabled");
3005 /* Do nothing. The only reason for this handler is that it allows
3006 us to use sigsuspend in linux_nat_wait above to wait for the
3007 arrival of a SIGCHLD. */
3010 /* Accepts an integer PID; Returns a string representing a file that
3011 can be opened to get the symbols for the child process. */
3014 linux_child_pid_to_exec_file (int pid
)
3016 char *name1
, *name2
;
3018 name1
= xmalloc (MAXPATHLEN
);
3019 name2
= xmalloc (MAXPATHLEN
);
3020 make_cleanup (xfree
, name1
);
3021 make_cleanup (xfree
, name2
);
3022 memset (name2
, 0, MAXPATHLEN
);
3024 sprintf (name1
, "/proc/%d/exe", pid
);
3025 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
3031 /* Service function for corefiles and info proc. */
3034 read_mapping (FILE *mapfile
,
3039 char *device
, long long *inode
, char *filename
)
3041 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
3042 addr
, endaddr
, permissions
, offset
, device
, inode
);
3045 if (ret
> 0 && ret
!= EOF
)
3047 /* Eat everything up to EOL for the filename. This will prevent
3048 weird filenames (such as one with embedded whitespace) from
3049 confusing this code. It also makes this code more robust in
3050 respect to annotations the kernel may add after the filename.
3052 Note the filename is used for informational purposes
3054 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
3057 return (ret
!= 0 && ret
!= EOF
);
3060 /* Fills the "to_find_memory_regions" target vector. Lists the memory
3061 regions in the inferior for a corefile. */
3064 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
3066 int, int, int, void *), void *obfd
)
3068 long long pid
= PIDGET (inferior_ptid
);
3069 char mapsfilename
[MAXPATHLEN
];
3071 long long addr
, endaddr
, size
, offset
, inode
;
3072 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3073 int read
, write
, exec
;
3076 /* Compose the filename for the /proc memory map, and open it. */
3077 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
3078 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
3079 error (_("Could not open %s."), mapsfilename
);
3082 fprintf_filtered (gdb_stdout
,
3083 "Reading memory regions from %s\n", mapsfilename
);
3085 /* Now iterate until end-of-file. */
3086 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
3087 &offset
, &device
[0], &inode
, &filename
[0]))
3089 size
= endaddr
- addr
;
3091 /* Get the segment's permissions. */
3092 read
= (strchr (permissions
, 'r') != 0);
3093 write
= (strchr (permissions
, 'w') != 0);
3094 exec
= (strchr (permissions
, 'x') != 0);
3098 fprintf_filtered (gdb_stdout
,
3099 "Save segment, %lld bytes at 0x%s (%c%c%c)",
3100 size
, paddr_nz (addr
),
3102 write
? 'w' : ' ', exec
? 'x' : ' ');
3104 fprintf_filtered (gdb_stdout
, " for %s", filename
);
3105 fprintf_filtered (gdb_stdout
, "\n");
3108 /* Invoke the callback function to create the corefile
3110 func (addr
, size
, read
, write
, exec
, obfd
);
3116 /* Records the thread's register state for the corefile note
3120 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3121 char *note_data
, int *note_size
)
3123 gdb_gregset_t gregs
;
3124 gdb_fpregset_t fpregs
;
3125 #ifdef FILL_FPXREGSET
3126 gdb_fpxregset_t fpxregs
;
3128 unsigned long lwp
= ptid_get_lwp (ptid
);
3129 struct regcache
*regcache
= get_thread_regcache (ptid
);
3130 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3131 const struct regset
*regset
;
3133 struct cleanup
*old_chain
;
3135 old_chain
= save_inferior_ptid ();
3136 inferior_ptid
= ptid
;
3137 target_fetch_registers (regcache
, -1);
3138 do_cleanups (old_chain
);
3140 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
3142 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
3143 sizeof (gregs
))) != NULL
3144 && regset
->collect_regset
!= NULL
)
3145 regset
->collect_regset (regset
, regcache
, -1,
3146 &gregs
, sizeof (gregs
));
3148 fill_gregset (regcache
, &gregs
, -1);
3150 note_data
= (char *) elfcore_write_prstatus (obfd
,
3154 stop_signal
, &gregs
);
3157 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
3158 sizeof (fpregs
))) != NULL
3159 && regset
->collect_regset
!= NULL
)
3160 regset
->collect_regset (regset
, regcache
, -1,
3161 &fpregs
, sizeof (fpregs
));
3163 fill_fpregset (regcache
, &fpregs
, -1);
3165 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3168 &fpregs
, sizeof (fpregs
));
3170 #ifdef FILL_FPXREGSET
3172 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg-xfp",
3173 sizeof (fpxregs
))) != NULL
3174 && regset
->collect_regset
!= NULL
)
3175 regset
->collect_regset (regset
, regcache
, -1,
3176 &fpxregs
, sizeof (fpxregs
));
3178 fill_fpxregset (regcache
, &fpxregs
, -1);
3180 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
3183 &fpxregs
, sizeof (fpxregs
));
3188 struct linux_nat_corefile_thread_data
3196 /* Called by gdbthread.c once per thread. Records the thread's
3197 register state for the corefile note section. */
3200 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
3202 struct linux_nat_corefile_thread_data
*args
= data
;
3204 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
3213 /* Records the register state for the corefile note section. */
3216 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
3217 char *note_data
, int *note_size
)
3219 return linux_nat_do_thread_registers (obfd
,
3220 ptid_build (ptid_get_pid (inferior_ptid
),
3221 ptid_get_pid (inferior_ptid
),
3223 note_data
, note_size
);
3226 /* Fills the "to_make_corefile_note" target vector. Builds the note
3227 section for a corefile, and returns it in a malloc buffer. */
3230 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
3232 struct linux_nat_corefile_thread_data thread_args
;
3233 struct cleanup
*old_chain
;
3234 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
3235 char fname
[16] = { '\0' };
3236 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
3237 char psargs
[80] = { '\0' };
3238 char *note_data
= NULL
;
3239 ptid_t current_ptid
= inferior_ptid
;
3243 if (get_exec_file (0))
3245 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
3246 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3247 if (get_inferior_args ())
3250 char *psargs_end
= psargs
+ sizeof (psargs
);
3252 /* linux_elfcore_write_prpsinfo () handles zero unterminated
3254 string_end
= memchr (psargs
, 0, sizeof (psargs
));
3255 if (string_end
!= NULL
)
3257 *string_end
++ = ' ';
3258 strncpy (string_end
, get_inferior_args (),
3259 psargs_end
- string_end
);
3262 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3264 note_size
, fname
, psargs
);
3267 /* Dump information for threads. */
3268 thread_args
.obfd
= obfd
;
3269 thread_args
.note_data
= note_data
;
3270 thread_args
.note_size
= note_size
;
3271 thread_args
.num_notes
= 0;
3272 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
3273 if (thread_args
.num_notes
== 0)
3275 /* iterate_over_threads didn't come up with any threads; just
3276 use inferior_ptid. */
3277 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
3278 note_data
, note_size
);
3282 note_data
= thread_args
.note_data
;
3285 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
3289 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3290 "CORE", NT_AUXV
, auxv
, auxv_len
);
3294 make_cleanup (xfree
, note_data
);
3298 /* Implement the "info proc" command. */
3301 linux_nat_info_proc_cmd (char *args
, int from_tty
)
3303 long long pid
= PIDGET (inferior_ptid
);
3306 char buffer
[MAXPATHLEN
];
3307 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
3320 /* Break up 'args' into an argv array. */
3321 if ((argv
= buildargv (args
)) == NULL
)
3324 make_cleanup_freeargv (argv
);
3326 while (argv
!= NULL
&& *argv
!= NULL
)
3328 if (isdigit (argv
[0][0]))
3330 pid
= strtoul (argv
[0], NULL
, 10);
3332 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
3336 else if (strcmp (argv
[0], "status") == 0)
3340 else if (strcmp (argv
[0], "stat") == 0)
3344 else if (strcmp (argv
[0], "cmd") == 0)
3348 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
3352 else if (strcmp (argv
[0], "cwd") == 0)
3356 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
3362 /* [...] (future options here) */
3367 error (_("No current process: you must name one."));
3369 sprintf (fname1
, "/proc/%lld", pid
);
3370 if (stat (fname1
, &dummy
) != 0)
3371 error (_("No /proc directory: '%s'"), fname1
);
3373 printf_filtered (_("process %lld\n"), pid
);
3374 if (cmdline_f
|| all
)
3376 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
3377 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3379 fgets (buffer
, sizeof (buffer
), procfile
);
3380 printf_filtered ("cmdline = '%s'\n", buffer
);
3384 warning (_("unable to open /proc file '%s'"), fname1
);
3388 sprintf (fname1
, "/proc/%lld/cwd", pid
);
3389 memset (fname2
, 0, sizeof (fname2
));
3390 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3391 printf_filtered ("cwd = '%s'\n", fname2
);
3393 warning (_("unable to read link '%s'"), fname1
);
3397 sprintf (fname1
, "/proc/%lld/exe", pid
);
3398 memset (fname2
, 0, sizeof (fname2
));
3399 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3400 printf_filtered ("exe = '%s'\n", fname2
);
3402 warning (_("unable to read link '%s'"), fname1
);
3404 if (mappings_f
|| all
)
3406 sprintf (fname1
, "/proc/%lld/maps", pid
);
3407 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3409 long long addr
, endaddr
, size
, offset
, inode
;
3410 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3412 printf_filtered (_("Mapped address spaces:\n\n"));
3413 if (gdbarch_addr_bit (current_gdbarch
) == 32)
3415 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3418 " Size", " Offset", "objfile");
3422 printf_filtered (" %18s %18s %10s %10s %7s\n",
3425 " Size", " Offset", "objfile");
3428 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
3429 &offset
, &device
[0], &inode
, &filename
[0]))
3431 size
= endaddr
- addr
;
3433 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
3434 calls here (and possibly above) should be abstracted
3435 out into their own functions? Andrew suggests using
3436 a generic local_address_string instead to print out
3437 the addresses; that makes sense to me, too. */
3439 if (gdbarch_addr_bit (current_gdbarch
) == 32)
3441 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3442 (unsigned long) addr
, /* FIXME: pr_addr */
3443 (unsigned long) endaddr
,
3445 (unsigned int) offset
,
3446 filename
[0] ? filename
: "");
3450 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3451 (unsigned long) addr
, /* FIXME: pr_addr */
3452 (unsigned long) endaddr
,
3454 (unsigned int) offset
,
3455 filename
[0] ? filename
: "");
3462 warning (_("unable to open /proc file '%s'"), fname1
);
3464 if (status_f
|| all
)
3466 sprintf (fname1
, "/proc/%lld/status", pid
);
3467 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3469 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
3470 puts_filtered (buffer
);
3474 warning (_("unable to open /proc file '%s'"), fname1
);
3478 sprintf (fname1
, "/proc/%lld/stat", pid
);
3479 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3485 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3486 printf_filtered (_("Process: %d\n"), itmp
);
3487 if (fscanf (procfile
, "(%[^)]) ", &buffer
[0]) > 0)
3488 printf_filtered (_("Exec file: %s\n"), buffer
);
3489 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
3490 printf_filtered (_("State: %c\n"), ctmp
);
3491 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3492 printf_filtered (_("Parent process: %d\n"), itmp
);
3493 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3494 printf_filtered (_("Process group: %d\n"), itmp
);
3495 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3496 printf_filtered (_("Session id: %d\n"), itmp
);
3497 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3498 printf_filtered (_("TTY: %d\n"), itmp
);
3499 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3500 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
3501 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3502 printf_filtered (_("Flags: 0x%lx\n"), ltmp
);
3503 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3504 printf_filtered (_("Minor faults (no memory page): %lu\n"),
3505 (unsigned long) ltmp
);
3506 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3507 printf_filtered (_("Minor faults, children: %lu\n"),
3508 (unsigned long) ltmp
);
3509 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3510 printf_filtered (_("Major faults (memory page faults): %lu\n"),
3511 (unsigned long) ltmp
);
3512 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3513 printf_filtered (_("Major faults, children: %lu\n"),
3514 (unsigned long) ltmp
);
3515 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3516 printf_filtered (_("utime: %ld\n"), ltmp
);
3517 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3518 printf_filtered (_("stime: %ld\n"), ltmp
);
3519 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3520 printf_filtered (_("utime, children: %ld\n"), ltmp
);
3521 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3522 printf_filtered (_("stime, children: %ld\n"), ltmp
);
3523 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3524 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
3526 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3527 printf_filtered (_("'nice' value: %ld\n"), ltmp
);
3528 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3529 printf_filtered (_("jiffies until next timeout: %lu\n"),
3530 (unsigned long) ltmp
);
3531 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3532 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
3533 (unsigned long) ltmp
);
3534 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3535 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
3537 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3538 printf_filtered (_("Virtual memory size: %lu\n"),
3539 (unsigned long) ltmp
);
3540 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3541 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp
);
3542 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3543 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp
);
3544 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3545 printf_filtered (_("Start of text: 0x%lx\n"), ltmp
);
3546 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3547 printf_filtered (_("End of text: 0x%lx\n"), ltmp
);
3548 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3549 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp
);
3550 #if 0 /* Don't know how architecture-dependent the rest is...
3551 Anyway the signal bitmap info is available from "status". */
3552 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3553 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp
);
3554 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3555 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp
);
3556 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3557 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp
);
3558 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3559 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp
);
3560 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3561 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp
);
3562 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3563 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp
);
3564 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3565 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp
);
3570 warning (_("unable to open /proc file '%s'"), fname1
);
3574 /* Implement the to_xfer_partial interface for memory reads using the /proc
3575 filesystem. Because we can use a single read() call for /proc, this
3576 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3577 but it doesn't support writes. */
3580 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3581 const char *annex
, gdb_byte
*readbuf
,
3582 const gdb_byte
*writebuf
,
3583 ULONGEST offset
, LONGEST len
)
3589 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
3592 /* Don't bother for one word. */
3593 if (len
< 3 * sizeof (long))
3596 /* We could keep this file open and cache it - possibly one per
3597 thread. That requires some juggling, but is even faster. */
3598 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
3599 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
3603 /* If pread64 is available, use it. It's faster if the kernel
3604 supports it (only one syscall), and it's 64-bit safe even on
3605 32-bit platforms (for instance, SPARC debugging a SPARC64
3608 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
3610 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
3620 /* Parse LINE as a signal set and add its set bits to SIGS. */
3623 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
3625 int len
= strlen (line
) - 1;
3629 if (line
[len
] != '\n')
3630 error (_("Could not parse signal set: %s"), line
);
3638 if (*p
>= '0' && *p
<= '9')
3640 else if (*p
>= 'a' && *p
<= 'f')
3641 digit
= *p
- 'a' + 10;
3643 error (_("Could not parse signal set: %s"), line
);
3648 sigaddset (sigs
, signum
+ 1);
3650 sigaddset (sigs
, signum
+ 2);
3652 sigaddset (sigs
, signum
+ 3);
3654 sigaddset (sigs
, signum
+ 4);
3660 /* Find process PID's pending signals from /proc/pid/status and set
3664 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3667 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3670 sigemptyset (pending
);
3671 sigemptyset (blocked
);
3672 sigemptyset (ignored
);
3673 sprintf (fname
, "/proc/%d/status", pid
);
3674 procfile
= fopen (fname
, "r");
3675 if (procfile
== NULL
)
3676 error (_("Could not open %s"), fname
);
3678 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3680 /* Normal queued signals are on the SigPnd line in the status
3681 file. However, 2.6 kernels also have a "shared" pending
3682 queue for delivering signals to a thread group, so check for
3685 Unfortunately some Red Hat kernels include the shared pending
3686 queue but not the ShdPnd status field. */
3688 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3689 add_line_to_sigset (buffer
+ 8, pending
);
3690 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3691 add_line_to_sigset (buffer
+ 8, pending
);
3692 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3693 add_line_to_sigset (buffer
+ 8, blocked
);
3694 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3695 add_line_to_sigset (buffer
+ 8, ignored
);
3702 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3703 const char *annex
, gdb_byte
*readbuf
,
3704 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3708 if (object
== TARGET_OBJECT_AUXV
)
3709 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
3712 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3717 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3721 /* Create a prototype generic GNU/Linux target. The client can override
3722 it with local methods. */
3725 linux_target_install_ops (struct target_ops
*t
)
3727 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
3728 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
3729 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
3730 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
3731 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
3732 t
->to_post_attach
= linux_child_post_attach
;
3733 t
->to_follow_fork
= linux_child_follow_fork
;
3734 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
3735 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3737 super_xfer_partial
= t
->to_xfer_partial
;
3738 t
->to_xfer_partial
= linux_xfer_partial
;
3744 struct target_ops
*t
;
3746 t
= inf_ptrace_target ();
3747 linux_target_install_ops (t
);
3753 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
3755 struct target_ops
*t
;
3757 t
= inf_ptrace_trad_target (register_u_offset
);
3758 linux_target_install_ops (t
);
3763 /* Controls if async mode is permitted. */
3764 static int linux_async_permitted
= 0;
3766 /* The set command writes to this variable. If the inferior is
3767 executing, linux_nat_async_permitted is *not* updated. */
3768 static int linux_async_permitted_1
= 0;
3771 set_maintenance_linux_async_permitted (char *args
, int from_tty
,
3772 struct cmd_list_element
*c
)
3774 if (target_has_execution
)
3776 linux_async_permitted_1
= linux_async_permitted
;
3777 error (_("Cannot change this setting while the inferior is running."));
3780 linux_async_permitted
= linux_async_permitted_1
;
3781 linux_nat_set_async_mode (linux_async_permitted
);
3785 show_maintenance_linux_async_permitted (struct ui_file
*file
, int from_tty
,
3786 struct cmd_list_element
*c
, const char *value
)
3788 fprintf_filtered (file
, _("\
3789 Controlling the GNU/Linux inferior in asynchronous mode is %s.\n"),
3793 /* target_is_async_p implementation. */
3796 linux_nat_is_async_p (void)
3798 /* NOTE: palves 2008-03-21: We're only async when the user requests
3799 it explicitly with the "maintenance set linux-async" command.
3800 Someday, linux will always be async. */
3801 if (!linux_async_permitted
)
3807 /* target_can_async_p implementation. */
3810 linux_nat_can_async_p (void)
3812 /* NOTE: palves 2008-03-21: We're only async when the user requests
3813 it explicitly with the "maintenance set linux-async" command.
3814 Someday, linux will always be async. */
3815 if (!linux_async_permitted
)
3818 /* See target.h/target_async_mask. */
3819 return linux_nat_async_mask_value
;
3822 /* target_async_mask implementation. */
3825 linux_nat_async_mask (int mask
)
3828 current_state
= linux_nat_async_mask_value
;
3830 if (current_state
!= mask
)
3834 linux_nat_async (NULL
, 0);
3835 linux_nat_async_mask_value
= mask
;
3836 /* We're in sync mode. Make sure SIGCHLD isn't handled by
3837 async_sigchld_handler when we come out of sigsuspend in
3839 sigaction (SIGCHLD
, &sync_sigchld_action
, NULL
);
3843 /* Restore the async handler. */
3844 sigaction (SIGCHLD
, &async_sigchld_action
, NULL
);
3845 linux_nat_async_mask_value
= mask
;
3846 linux_nat_async (inferior_event_handler
, 0);
3850 return current_state
;
3853 /* Pop an event from the event pipe. */
3856 linux_nat_event_pipe_pop (int* ptr_status
, int* ptr_options
)
3858 struct waitpid_result event
= {0};
3863 ret
= read (linux_nat_event_pipe
[0], &event
, sizeof (event
));
3865 while (ret
== -1 && errno
== EINTR
);
3867 gdb_assert (ret
== sizeof (event
));
3869 *ptr_status
= event
.status
;
3870 *ptr_options
= event
.options
;
3872 linux_nat_num_queued_events
--;
3877 /* Push an event into the event pipe. */
3880 linux_nat_event_pipe_push (int pid
, int status
, int options
)
3883 struct waitpid_result event
= {0};
3885 event
.status
= status
;
3886 event
.options
= options
;
3890 ret
= write (linux_nat_event_pipe
[1], &event
, sizeof (event
));
3891 gdb_assert ((ret
== -1 && errno
== EINTR
) || ret
== sizeof (event
));
3892 } while (ret
== -1 && errno
== EINTR
);
3894 linux_nat_num_queued_events
++;
3898 get_pending_events (void)
3900 int status
, options
, pid
;
3902 if (!linux_nat_async_enabled
|| !linux_nat_async_events_enabled
)
3903 internal_error (__FILE__
, __LINE__
,
3904 "get_pending_events called with async masked");
3909 options
= __WCLONE
| WNOHANG
;
3913 pid
= waitpid (-1, &status
, options
);
3915 while (pid
== -1 && errno
== EINTR
);
3922 pid
= waitpid (-1, &status
, options
);
3924 while (pid
== -1 && errno
== EINTR
);
3928 /* No more children reporting events. */
3931 if (debug_linux_nat_async
)
3932 fprintf_unfiltered (gdb_stdlog
, "\
3933 get_pending_events: pid(%d), status(%x), options (%x)\n",
3934 pid
, status
, options
);
3936 linux_nat_event_pipe_push (pid
, status
, options
);
3939 if (debug_linux_nat_async
)
3940 fprintf_unfiltered (gdb_stdlog
, "\
3941 get_pending_events: linux_nat_num_queued_events(%d)\n",
3942 linux_nat_num_queued_events
);
3945 /* SIGCHLD handler for async mode. */
3948 async_sigchld_handler (int signo
)
3950 if (debug_linux_nat_async
)
3951 fprintf_unfiltered (gdb_stdlog
, "async_sigchld_handler\n");
3953 get_pending_events ();
3956 /* Enable or disable async SIGCHLD handling. */
3959 linux_nat_async_events (int enable
)
3961 int current_state
= linux_nat_async_events_enabled
;
3963 if (debug_linux_nat_async
)
3964 fprintf_unfiltered (gdb_stdlog
,
3965 "LNAE: enable(%d): linux_nat_async_events_enabled(%d), "
3966 "linux_nat_num_queued_events(%d)\n",
3967 enable
, linux_nat_async_events_enabled
,
3968 linux_nat_num_queued_events
);
3970 if (current_state
!= enable
)
3973 sigemptyset (&mask
);
3974 sigaddset (&mask
, SIGCHLD
);
3977 /* Unblock target events. */
3978 linux_nat_async_events_enabled
= 1;
3980 local_event_queue_to_pipe ();
3981 /* While in masked async, we may have not collected all the
3982 pending events. Get them out now. */
3983 get_pending_events ();
3984 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
3988 /* Block target events. */
3989 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
3990 linux_nat_async_events_enabled
= 0;
3991 /* Get events out of queue, and make them available to
3992 queued_waitpid / my_waitpid. */
3993 pipe_to_local_event_queue ();
3997 return current_state
;
4000 static int async_terminal_is_ours
= 1;
4002 /* target_terminal_inferior implementation. */
4005 linux_nat_terminal_inferior (void)
4007 if (!target_is_async_p ())
4009 /* Async mode is disabled. */
4010 terminal_inferior ();
4014 /* GDB should never give the terminal to the inferior, if the
4015 inferior is running in the background (run&, continue&, etc.).
4016 This check can be removed when the common code is fixed. */
4017 if (!sync_execution
)
4020 terminal_inferior ();
4022 if (!async_terminal_is_ours
)
4025 delete_file_handler (input_fd
);
4026 async_terminal_is_ours
= 0;
4030 /* target_terminal_ours implementation. */
4033 linux_nat_terminal_ours (void)
4035 if (!target_is_async_p ())
4037 /* Async mode is disabled. */
4042 /* GDB should never give the terminal to the inferior if the
4043 inferior is running in the background (run&, continue&, etc.),
4044 but claiming it sure should. */
4047 if (!sync_execution
)
4050 if (async_terminal_is_ours
)
4053 clear_sigint_trap ();
4054 add_file_handler (input_fd
, stdin_event_handler
, 0);
4055 async_terminal_is_ours
= 1;
4058 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4060 static void *async_client_context
;
4063 linux_nat_async_file_handler (int error
, gdb_client_data client_data
)
4065 async_client_callback (INF_REG_EVENT
, async_client_context
);
4068 /* target_async implementation. */
4071 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
4072 void *context
), void *context
)
4074 if (linux_nat_async_mask_value
== 0 || !linux_nat_async_enabled
)
4075 internal_error (__FILE__
, __LINE__
,
4076 "Calling target_async when async is masked");
4078 if (callback
!= NULL
)
4080 async_client_callback
= callback
;
4081 async_client_context
= context
;
4082 add_file_handler (linux_nat_event_pipe
[0],
4083 linux_nat_async_file_handler
, NULL
);
4085 linux_nat_async_events (1);
4089 async_client_callback
= callback
;
4090 async_client_context
= context
;
4092 linux_nat_async_events (0);
4093 delete_file_handler (linux_nat_event_pipe
[0]);
4098 /* Enable/Disable async mode. */
4101 linux_nat_set_async_mode (int on
)
4103 if (linux_nat_async_enabled
!= on
)
4107 gdb_assert (waitpid_queue
== NULL
);
4108 sigaction (SIGCHLD
, &async_sigchld_action
, NULL
);
4110 if (pipe (linux_nat_event_pipe
) == -1)
4111 internal_error (__FILE__
, __LINE__
,
4112 "creating event pipe failed.");
4114 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4115 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4119 sigaction (SIGCHLD
, &sync_sigchld_action
, NULL
);
4121 drain_queued_events (-1);
4123 linux_nat_num_queued_events
= 0;
4124 close (linux_nat_event_pipe
[0]);
4125 close (linux_nat_event_pipe
[1]);
4126 linux_nat_event_pipe
[0] = linux_nat_event_pipe
[1] = -1;
4130 linux_nat_async_enabled
= on
;
4134 linux_nat_add_target (struct target_ops
*t
)
4136 /* Save the provided single-threaded target. We save this in a separate
4137 variable because another target we've inherited from (e.g. inf-ptrace)
4138 may have saved a pointer to T; we want to use it for the final
4139 process stratum target. */
4140 linux_ops_saved
= *t
;
4141 linux_ops
= &linux_ops_saved
;
4143 /* Override some methods for multithreading. */
4144 t
->to_create_inferior
= linux_nat_create_inferior
;
4145 t
->to_attach
= linux_nat_attach
;
4146 t
->to_detach
= linux_nat_detach
;
4147 t
->to_resume
= linux_nat_resume
;
4148 t
->to_wait
= linux_nat_wait
;
4149 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4150 t
->to_kill
= linux_nat_kill
;
4151 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4152 t
->to_thread_alive
= linux_nat_thread_alive
;
4153 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4154 t
->to_has_thread_control
= tc_schedlock
;
4156 t
->to_can_async_p
= linux_nat_can_async_p
;
4157 t
->to_is_async_p
= linux_nat_is_async_p
;
4158 t
->to_async
= linux_nat_async
;
4159 t
->to_async_mask
= linux_nat_async_mask
;
4160 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4161 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4163 /* We don't change the stratum; this target will sit at
4164 process_stratum and thread_db will set at thread_stratum. This
4165 is a little strange, since this is a multi-threaded-capable
4166 target, but we want to be on the stack below thread_db, and we
4167 also want to be used for single-threaded processes. */
4171 /* TODO: Eliminate this and have libthread_db use
4172 find_target_beneath. */
4176 /* Register a method to call whenever a new thread is attached. */
4178 linux_nat_set_new_thread (struct target_ops
*t
, void (*new_thread
) (ptid_t
))
4180 /* Save the pointer. We only support a single registered instance
4181 of the GNU/Linux native target, so we do not need to map this to
4183 linux_nat_new_thread
= new_thread
;
4186 /* Return the saved siginfo associated with PTID. */
4188 linux_nat_get_siginfo (ptid_t ptid
)
4190 struct lwp_info
*lp
= find_lwp_pid (ptid
);
4192 gdb_assert (lp
!= NULL
);
4194 return &lp
->siginfo
;
4198 _initialize_linux_nat (void)
4202 add_info ("proc", linux_nat_info_proc_cmd
, _("\
4203 Show /proc process information about any running process.\n\
4204 Specify any process id, or use the program being debugged by default.\n\
4205 Specify any of the following keywords for detailed info:\n\
4206 mappings -- list of mapped memory regions.\n\
4207 stat -- list a bunch of random process info.\n\
4208 status -- list a different bunch of random process info.\n\
4209 all -- list all available /proc info."));
4211 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance
,
4212 &debug_linux_nat
, _("\
4213 Set debugging of GNU/Linux lwp module."), _("\
4214 Show debugging of GNU/Linux lwp module."), _("\
4215 Enables printf debugging output."),
4217 show_debug_linux_nat
,
4218 &setdebuglist
, &showdebuglist
);
4220 add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance
,
4221 &debug_linux_nat_async
, _("\
4222 Set debugging of GNU/Linux async lwp module."), _("\
4223 Show debugging of GNU/Linux async lwp module."), _("\
4224 Enables printf debugging output."),
4226 show_debug_linux_nat_async
,
4227 &setdebuglist
, &showdebuglist
);
4229 add_setshow_boolean_cmd ("linux-async", class_maintenance
,
4230 &linux_async_permitted_1
, _("\
4231 Set whether gdb controls the GNU/Linux inferior in asynchronous mode."), _("\
4232 Show whether gdb controls the GNU/Linux inferior in asynchronous mode."), _("\
4233 Tells gdb whether to control the GNU/Linux inferior in asynchronous mode."),
4234 set_maintenance_linux_async_permitted
,
4235 show_maintenance_linux_async_permitted
,
4236 &maintenance_set_cmdlist
,
4237 &maintenance_show_cmdlist
);
4239 /* Block SIGCHLD by default. Doing this early prevents it getting
4240 unblocked if an exception is thrown due to an error while the
4241 inferior is starting (sigsetjmp/siglongjmp). */
4242 sigemptyset (&mask
);
4243 sigaddset (&mask
, SIGCHLD
);
4244 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4246 /* Save this mask as the default. */
4247 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4249 /* The synchronous SIGCHLD handler. */
4250 sync_sigchld_action
.sa_handler
= sigchld_handler
;
4251 sigemptyset (&sync_sigchld_action
.sa_mask
);
4252 sync_sigchld_action
.sa_flags
= SA_RESTART
;
4254 /* Make it the default. */
4255 sigaction (SIGCHLD
, &sync_sigchld_action
, NULL
);
4257 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4258 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
4259 sigdelset (&suspend_mask
, SIGCHLD
);
4261 /* SIGCHLD handler for async mode. */
4262 async_sigchld_action
.sa_handler
= async_sigchld_handler
;
4263 sigemptyset (&async_sigchld_action
.sa_mask
);
4264 async_sigchld_action
.sa_flags
= SA_RESTART
;
4266 /* Install the default mode. */
4267 linux_nat_set_async_mode (linux_async_permitted
);
4271 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4272 the GNU/Linux Threads library and therefore doesn't really belong
4275 /* Read variable NAME in the target and return its value if found.
4276 Otherwise return zero. It is assumed that the type of the variable
4280 get_signo (const char *name
)
4282 struct minimal_symbol
*ms
;
4285 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
4289 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
4290 sizeof (signo
)) != 0)
4296 /* Return the set of signals used by the threads library in *SET. */
4299 lin_thread_get_thread_signals (sigset_t
*set
)
4301 struct sigaction action
;
4302 int restart
, cancel
;
4303 sigset_t blocked_mask
;
4305 sigemptyset (&blocked_mask
);
4308 restart
= get_signo ("__pthread_sig_restart");
4309 cancel
= get_signo ("__pthread_sig_cancel");
4311 /* LinuxThreads normally uses the first two RT signals, but in some legacy
4312 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
4313 not provide any way for the debugger to query the signal numbers -
4314 fortunately they don't change! */
4317 restart
= __SIGRTMIN
;
4320 cancel
= __SIGRTMIN
+ 1;
4322 sigaddset (set
, restart
);
4323 sigaddset (set
, cancel
);
4325 /* The GNU/Linux Threads library makes terminating threads send a
4326 special "cancel" signal instead of SIGCHLD. Make sure we catch
4327 those (to prevent them from terminating GDB itself, which is
4328 likely to be their default action) and treat them the same way as
4331 action
.sa_handler
= sigchld_handler
;
4332 sigemptyset (&action
.sa_mask
);
4333 action
.sa_flags
= SA_RESTART
;
4334 sigaction (cancel
, &action
, NULL
);
4336 /* We block the "cancel" signal throughout this code ... */
4337 sigaddset (&blocked_mask
, cancel
);
4338 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
4340 /* ... except during a sigsuspend. */
4341 sigdelset (&suspend_mask
, cancel
);