1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 #include <sys/types.h>
54 #include "gdb_dirent.h"
55 #include "xml-support.h"
60 #define SPUFS_MAGIC 0x23c9b64e
63 #ifdef HAVE_PERSONALITY
64 # include <sys/personality.h>
65 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
66 # define ADDR_NO_RANDOMIZE 0x0040000
68 #endif /* HAVE_PERSONALITY */
70 /* To be used when one needs to know wether a
71 WSTOPSIG (status) is a syscall */
72 #define TRAP_IS_SYSCALL (SIGTRAP | 0x80)
74 /* This comment documents high-level logic of this file.
76 Waiting for events in sync mode
77 ===============================
79 When waiting for an event in a specific thread, we just use waitpid, passing
80 the specific pid, and not passing WNOHANG.
82 When waiting for an event in all threads, waitpid is not quite good. Prior to
83 version 2.4, Linux can either wait for event in main thread, or in secondary
84 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
85 miss an event. The solution is to use non-blocking waitpid, together with
86 sigsuspend. First, we use non-blocking waitpid to get an event in the main
87 process, if any. Second, we use non-blocking waitpid with the __WCLONED
88 flag to check for events in cloned processes. If nothing is found, we use
89 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
90 happened to a child process -- and SIGCHLD will be delivered both for events
91 in main debugged process and in cloned processes. As soon as we know there's
92 an event, we get back to calling nonblocking waitpid with and without __WCLONED.
94 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
95 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
96 blocked, the signal becomes pending and sigsuspend immediately
97 notices it and returns.
99 Waiting for events in async mode
100 ================================
102 In async mode, GDB should always be ready to handle both user input
103 and target events, so neither blocking waitpid nor sigsuspend are
104 viable options. Instead, we should asynchronously notify the GDB main
105 event loop whenever there's an unprocessed event from the target. We
106 detect asynchronous target events by handling SIGCHLD signals. To
107 notify the event loop about target events, the self-pipe trick is used
108 --- a pipe is registered as waitable event source in the event loop,
109 the event loop select/poll's on the read end of this pipe (as well on
110 other event sources, e.g., stdin), and the SIGCHLD handler writes a
111 byte to this pipe. This is more portable than relying on
112 pselect/ppoll, since on kernels that lack those syscalls, libc
113 emulates them with select/poll+sigprocmask, and that is racy
114 (a.k.a. plain broken).
116 Obviously, if we fail to notify the event loop if there's a target
117 event, it's bad. OTOH, if we notify the event loop when there's no
118 event from the target, linux_nat_wait will detect that there's no real
119 event to report, and return event of type TARGET_WAITKIND_IGNORE.
120 This is mostly harmless, but it will waste time and is better avoided.
122 The main design point is that every time GDB is outside linux-nat.c,
123 we have a SIGCHLD handler installed that is called when something
124 happens to the target and notifies the GDB event loop. Whenever GDB
125 core decides to handle the event, and calls into linux-nat.c, we
126 process things as in sync mode, except that the we never block in
129 While processing an event, we may end up momentarily blocked in
130 waitpid calls. Those waitpid calls, while blocking, are guarantied to
131 return quickly. E.g., in all-stop mode, before reporting to the core
132 that an LWP hit a breakpoint, all LWPs are stopped by sending them
133 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
134 Note that this is different from blocking indefinitely waiting for the
135 next event --- here, we're already handling an event.
140 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
141 signal is not entirely significant; we just need for a signal to be delivered,
142 so that we can intercept it. SIGSTOP's advantage is that it can not be
143 blocked. A disadvantage is that it is not a real-time signal, so it can only
144 be queued once; we do not keep track of other sources of SIGSTOP.
146 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
147 use them, because they have special behavior when the signal is generated -
148 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
149 kills the entire thread group.
151 A delivered SIGSTOP would stop the entire thread group, not just the thread we
152 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
153 cancel it (by PTRACE_CONT without passing SIGSTOP).
155 We could use a real-time signal instead. This would solve those problems; we
156 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
157 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
158 generates it, and there are races with trying to find a signal that is not
162 #define O_LARGEFILE 0
165 /* If the system headers did not provide the constants, hard-code the normal
167 #ifndef PTRACE_EVENT_FORK
169 #define PTRACE_SETOPTIONS 0x4200
170 #define PTRACE_GETEVENTMSG 0x4201
172 /* options set using PTRACE_SETOPTIONS */
173 #define PTRACE_O_TRACESYSGOOD 0x00000001
174 #define PTRACE_O_TRACEFORK 0x00000002
175 #define PTRACE_O_TRACEVFORK 0x00000004
176 #define PTRACE_O_TRACECLONE 0x00000008
177 #define PTRACE_O_TRACEEXEC 0x00000010
178 #define PTRACE_O_TRACEVFORKDONE 0x00000020
179 #define PTRACE_O_TRACEEXIT 0x00000040
181 /* Wait extended result codes for the above trace options. */
182 #define PTRACE_EVENT_FORK 1
183 #define PTRACE_EVENT_VFORK 2
184 #define PTRACE_EVENT_CLONE 3
185 #define PTRACE_EVENT_EXEC 4
186 #define PTRACE_EVENT_VFORK_DONE 5
187 #define PTRACE_EVENT_EXIT 6
189 #endif /* PTRACE_EVENT_FORK */
191 /* We can't always assume that this flag is available, but all systems
192 with the ptrace event handlers also have __WALL, so it's safe to use
195 #define __WALL 0x40000000 /* Wait for any child. */
198 #ifndef PTRACE_GETSIGINFO
199 # define PTRACE_GETSIGINFO 0x4202
200 # define PTRACE_SETSIGINFO 0x4203
203 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
204 the use of the multi-threaded target. */
205 static struct target_ops *linux_ops;
206 static struct target_ops linux_ops_saved;
208 /* The method to call, if any, when a new thread is attached. */
209 static void (*linux_nat_new_thread) (ptid_t);
211 /* The method to call, if any, when the siginfo object needs to be
212 converted between the layout returned by ptrace, and the layout in
213 the architecture of the inferior. */
214 static int (*linux_nat_siginfo_fixup) (struct siginfo *,
218 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
219 Called by our to_xfer_partial. */
220 static LONGEST (*super_xfer_partial) (struct target_ops *,
222 const char *, gdb_byte *,
226 static int debug_linux_nat;
228 show_debug_linux_nat (struct ui_file *file, int from_tty,
229 struct cmd_list_element *c, const char *value)
231 fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
235 static int debug_linux_nat_async = 0;
237 show_debug_linux_nat_async (struct ui_file *file, int from_tty,
238 struct cmd_list_element *c, const char *value)
240 fprintf_filtered (file, _("Debugging of GNU/Linux async lwp module is %s.\n"),
244 static int disable_randomization = 1;
247 show_disable_randomization (struct ui_file *file, int from_tty,
248 struct cmd_list_element *c, const char *value)
250 #ifdef HAVE_PERSONALITY
251 fprintf_filtered (file, _("\
252 Disabling randomization of debuggee's virtual address space is %s.\n"),
254 #else /* !HAVE_PERSONALITY */
256 Disabling randomization of debuggee's virtual address space is unsupported on\n\
257 this platform.\n"), file);
258 #endif /* !HAVE_PERSONALITY */
262 set_disable_randomization (char *args, int from_tty, struct cmd_list_element *c)
264 #ifndef HAVE_PERSONALITY
266 Disabling randomization of debuggee's virtual address space is unsupported on\n\
268 #endif /* !HAVE_PERSONALITY */
271 static int linux_parent_pid;
273 struct simple_pid_list
277 struct simple_pid_list *next;
279 struct simple_pid_list *stopped_pids;
281 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
282 can not be used, 1 if it can. */
284 static int linux_supports_tracefork_flag = -1;
286 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACESYSGOOD
287 can not be used, 1 if it can. */
289 static int linux_supports_tracesysgood_flag = -1;
291 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
292 PTRACE_O_TRACEVFORKDONE. */
294 static int linux_supports_tracevforkdone_flag = -1;
296 /* Async mode support */
298 /* Zero if the async mode, although enabled, is masked, which means
299 linux_nat_wait should behave as if async mode was off. */
300 static int linux_nat_async_mask_value = 1;
302 /* Stores the current used ptrace() options. */
303 static int current_ptrace_options = 0;
305 /* The read/write ends of the pipe registered as waitable file in the
307 static int linux_nat_event_pipe[2] = { -1, -1 };
309 /* Flush the event pipe. */
312 async_file_flush (void)
319 ret = read (linux_nat_event_pipe[0], &buf, 1);
321 while (ret >= 0 || (ret == -1 && errno == EINTR));
324 /* Put something (anything, doesn't matter what, or how much) in event
325 pipe, so that the select/poll in the event-loop realizes we have
326 something to process. */
329 async_file_mark (void)
333 /* It doesn't really matter what the pipe contains, as long we end
334 up with something in it. Might as well flush the previous
340 ret = write (linux_nat_event_pipe[1], "+", 1);
342 while (ret == -1 && errno == EINTR);
344 /* Ignore EAGAIN. If the pipe is full, the event loop will already
345 be awakened anyway. */
348 static void linux_nat_async (void (*callback)
349 (enum inferior_event_type event_type, void *context),
351 static int linux_nat_async_mask (int mask);
352 static int kill_lwp (int lwpid, int signo);
354 static int stop_callback (struct lwp_info *lp, void *data);
356 static void block_child_signals (sigset_t *prev_mask);
357 static void restore_child_signals_mask (sigset_t *prev_mask);
360 static struct lwp_info *add_lwp (ptid_t ptid);
361 static void purge_lwp_list (int pid);
362 static struct lwp_info *find_lwp_pid (ptid_t ptid);
365 /* Trivial list manipulation functions to keep track of a list of
366 new stopped processes. */
368 add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
370 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
372 new_pid->status = status;
373 new_pid->next = *listp;
378 pull_pid_from_list (struct simple_pid_list **listp, int pid, int *status)
380 struct simple_pid_list **p;
382 for (p = listp; *p != NULL; p = &(*p)->next)
383 if ((*p)->pid == pid)
385 struct simple_pid_list *next = (*p)->next;
386 *status = (*p)->status;
395 linux_record_stopped_pid (int pid, int status)
397 add_to_pid_list (&stopped_pids, pid, status);
401 /* A helper function for linux_test_for_tracefork, called after fork (). */
404 linux_tracefork_child (void)
408 ptrace (PTRACE_TRACEME, 0, 0, 0);
409 kill (getpid (), SIGSTOP);
414 /* Wrapper function for waitpid which handles EINTR. */
417 my_waitpid (int pid, int *status, int flags)
423 ret = waitpid (pid, status, flags);
425 while (ret == -1 && errno == EINTR);
430 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
432 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
433 we know that the feature is not available. This may change the tracing
434 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
436 However, if it succeeds, we don't know for sure that the feature is
437 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
438 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
439 fork tracing, and let it fork. If the process exits, we assume that we
440 can't use TRACEFORK; if we get the fork notification, and we can extract
441 the new child's PID, then we assume that we can. */
444 linux_test_for_tracefork (int original_pid)
446 int child_pid, ret, status;
450 /* We don't want those ptrace calls to be interrupted. */
451 block_child_signals (&prev_mask);
453 linux_supports_tracefork_flag = 0;
454 linux_supports_tracevforkdone_flag = 0;
456 ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK);
459 restore_child_signals_mask (&prev_mask);
465 perror_with_name (("fork"));
468 linux_tracefork_child ();
470 ret = my_waitpid (child_pid, &status, 0);
472 perror_with_name (("waitpid"));
473 else if (ret != child_pid)
474 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret);
475 if (! WIFSTOPPED (status))
476 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status);
478 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
481 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
484 warning (_("linux_test_for_tracefork: failed to kill child"));
485 restore_child_signals_mask (&prev_mask);
489 ret = my_waitpid (child_pid, &status, 0);
490 if (ret != child_pid)
491 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
492 else if (!WIFSIGNALED (status))
493 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
494 "killed child"), status);
496 restore_child_signals_mask (&prev_mask);
500 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
501 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
502 PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
503 linux_supports_tracevforkdone_flag = (ret == 0);
505 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
507 warning (_("linux_test_for_tracefork: failed to resume child"));
509 ret = my_waitpid (child_pid, &status, 0);
511 if (ret == child_pid && WIFSTOPPED (status)
512 && status >> 16 == PTRACE_EVENT_FORK)
515 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
516 if (ret == 0 && second_pid != 0)
520 linux_supports_tracefork_flag = 1;
521 my_waitpid (second_pid, &second_status, 0);
522 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
524 warning (_("linux_test_for_tracefork: failed to kill second child"));
525 my_waitpid (second_pid, &status, 0);
529 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
530 "(%d, status 0x%x)"), ret, status);
532 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
534 warning (_("linux_test_for_tracefork: failed to kill child"));
535 my_waitpid (child_pid, &status, 0);
537 restore_child_signals_mask (&prev_mask);
540 /* Determine if PTRACE_O_TRACESYSGOOD can be used to follow syscalls.
542 We try to enable syscall tracing on ORIGINAL_PID. If this fails,
543 we know that the feature is not available. This may change the tracing
544 options for ORIGINAL_PID, but we'll be setting them shortly anyway. */
547 linux_test_for_tracesysgood (int original_pid)
552 /* We don't want those ptrace calls to be interrupted. */
553 block_child_signals (&prev_mask);
555 linux_supports_tracesysgood_flag = 0;
557 ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACESYSGOOD);
561 linux_supports_tracesysgood_flag = 1;
563 restore_child_signals_mask (&prev_mask);
566 /* Determine wether we support PTRACE_O_TRACESYSGOOD option available.
567 This function also sets linux_supports_tracesysgood_flag. */
570 linux_supports_tracesysgood (int pid)
572 if (linux_supports_tracesysgood_flag == -1)
573 linux_test_for_tracesysgood (pid);
574 return linux_supports_tracesysgood_flag;
577 /* Return non-zero iff we have tracefork functionality available.
578 This function also sets linux_supports_tracefork_flag. */
581 linux_supports_tracefork (int pid)
583 if (linux_supports_tracefork_flag == -1)
584 linux_test_for_tracefork (pid);
585 return linux_supports_tracefork_flag;
589 linux_supports_tracevforkdone (int pid)
591 if (linux_supports_tracefork_flag == -1)
592 linux_test_for_tracefork (pid);
593 return linux_supports_tracevforkdone_flag;
597 linux_enable_tracesysgood (ptid_t ptid)
599 int pid = ptid_get_lwp (ptid);
602 pid = ptid_get_pid (ptid);
604 if (linux_supports_tracesysgood (pid) == 0)
607 current_ptrace_options |= PTRACE_O_TRACESYSGOOD;
609 ptrace (PTRACE_SETOPTIONS, pid, 0, current_ptrace_options);
614 linux_enable_event_reporting (ptid_t ptid)
616 int pid = ptid_get_lwp (ptid);
619 pid = ptid_get_pid (ptid);
621 if (! linux_supports_tracefork (pid))
624 current_ptrace_options |= PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK
625 | PTRACE_O_TRACEEXEC | PTRACE_O_TRACECLONE;
627 if (linux_supports_tracevforkdone (pid))
628 current_ptrace_options |= PTRACE_O_TRACEVFORKDONE;
630 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
631 read-only process state. */
633 ptrace (PTRACE_SETOPTIONS, pid, 0, current_ptrace_options);
637 linux_child_post_attach (int pid)
639 linux_enable_event_reporting (pid_to_ptid (pid));
640 check_for_thread_db ();
641 linux_enable_tracesysgood (pid_to_ptid (pid));
645 linux_child_post_startup_inferior (ptid_t ptid)
647 linux_enable_event_reporting (ptid);
648 check_for_thread_db ();
649 linux_enable_tracesysgood (ptid);
653 linux_child_follow_fork (struct target_ops *ops, int follow_child)
657 int parent_pid, child_pid;
659 block_child_signals (&prev_mask);
661 has_vforked = (inferior_thread ()->pending_follow.kind
662 == TARGET_WAITKIND_VFORKED);
663 parent_pid = ptid_get_lwp (inferior_ptid);
665 parent_pid = ptid_get_pid (inferior_ptid);
666 child_pid = PIDGET (inferior_thread ()->pending_follow.value.related_pid);
669 linux_enable_event_reporting (pid_to_ptid (child_pid));
673 /* We're already attached to the parent, by default. */
675 /* Before detaching from the child, remove all breakpoints from
676 it. If we forked, then this has already been taken care of
677 by infrun.c. If we vforked however, any breakpoint inserted
678 in the parent is visible in the child, even those added while
679 stopped in a vfork catchpoint. This won't actually modify
680 the breakpoint list, but will physically remove the
681 breakpoints from the child. This will remove the breakpoints
682 from the parent also, but they'll be reinserted below. */
684 detach_breakpoints (child_pid);
686 /* Detach new forked process? */
689 if (info_verbose || debug_linux_nat)
691 target_terminal_ours ();
692 fprintf_filtered (gdb_stdlog,
693 "Detaching after fork from child process %d.\n",
697 ptrace (PTRACE_DETACH, child_pid, 0, 0);
701 struct inferior *parent_inf, *child_inf;
703 struct cleanup *old_chain;
705 /* Add process to GDB's tables. */
706 child_inf = add_inferior (child_pid);
708 parent_inf = current_inferior ();
709 child_inf->attach_flag = parent_inf->attach_flag;
710 copy_terminal_info (child_inf, parent_inf);
712 old_chain = save_inferior_ptid ();
714 inferior_ptid = ptid_build (child_pid, child_pid, 0);
715 add_thread (inferior_ptid);
716 lp = add_lwp (inferior_ptid);
719 check_for_thread_db ();
721 do_cleanups (old_chain);
726 gdb_assert (linux_supports_tracefork_flag >= 0);
727 if (linux_supports_tracevforkdone (0))
731 ptrace (PTRACE_CONT, parent_pid, 0, 0);
732 my_waitpid (parent_pid, &status, __WALL);
733 if ((status >> 16) != PTRACE_EVENT_VFORK_DONE)
734 warning (_("Unexpected waitpid result %06x when waiting for "
735 "vfork-done"), status);
739 /* We can't insert breakpoints until the child has
740 finished with the shared memory region. We need to
741 wait until that happens. Ideal would be to just
743 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
744 - waitpid (parent_pid, &status, __WALL);
745 However, most architectures can't handle a syscall
746 being traced on the way out if it wasn't traced on
749 We might also think to loop, continuing the child
750 until it exits or gets a SIGTRAP. One problem is
751 that the child might call ptrace with PTRACE_TRACEME.
753 There's no simple and reliable way to figure out when
754 the vforked child will be done with its copy of the
755 shared memory. We could step it out of the syscall,
756 two instructions, let it go, and then single-step the
757 parent once. When we have hardware single-step, this
758 would work; with software single-step it could still
759 be made to work but we'd have to be able to insert
760 single-step breakpoints in the child, and we'd have
761 to insert -just- the single-step breakpoint in the
762 parent. Very awkward.
764 In the end, the best we can do is to make sure it
765 runs for a little while. Hopefully it will be out of
766 range of any breakpoints we reinsert. Usually this
767 is only the single-step breakpoint at vfork's return
773 /* Since we vforked, breakpoints were removed in the parent
774 too. Put them back. */
775 reattach_breakpoints (parent_pid);
780 struct thread_info *tp;
781 struct inferior *parent_inf, *child_inf;
784 /* Before detaching from the parent, remove all breakpoints from it. */
785 remove_breakpoints ();
787 if (info_verbose || debug_linux_nat)
789 target_terminal_ours ();
790 fprintf_filtered (gdb_stdlog,
791 "Attaching after fork to child process %d.\n",
795 /* Add the new inferior first, so that the target_detach below
796 doesn't unpush the target. */
798 child_inf = add_inferior (child_pid);
800 parent_inf = current_inferior ();
801 child_inf->attach_flag = parent_inf->attach_flag;
802 copy_terminal_info (child_inf, parent_inf);
804 /* If we're vforking, we may want to hold on to the parent until
805 the child exits or execs. At exec time we can remove the old
806 breakpoints from the parent and detach it; at exit time we
807 could do the same (or even, sneakily, resume debugging it - the
808 child's exec has failed, or something similar).
810 This doesn't clean up "properly", because we can't call
811 target_detach, but that's OK; if the current target is "child",
812 then it doesn't need any further cleanups, and lin_lwp will
813 generally not encounter vfork (vfork is defined to fork
816 The holding part is very easy if we have VFORKDONE events;
817 but keeping track of both processes is beyond GDB at the
818 moment. So we don't expose the parent to the rest of GDB.
819 Instead we quietly hold onto it until such time as we can
824 struct lwp_info *parent_lwp;
826 linux_parent_pid = parent_pid;
828 /* Get rid of the inferior on the core side as well. */
829 inferior_ptid = null_ptid;
830 detach_inferior (parent_pid);
832 /* Also get rid of all its lwps. We will detach from this
833 inferior soon-ish, but, we will still get an exit event
834 reported through waitpid when it exits. If we didn't get
835 rid of the lwps from our list, we would end up reporting
836 the inferior exit to the core, which would then try to
837 mourn a non-existing (from the core's perspective)
839 parent_lwp = find_lwp_pid (pid_to_ptid (parent_pid));
840 purge_lwp_list (GET_PID (parent_lwp->ptid));
841 linux_parent_pid = parent_pid;
843 else if (detach_fork)
844 target_detach (NULL, 0);
846 inferior_ptid = ptid_build (child_pid, child_pid, 0);
847 add_thread (inferior_ptid);
848 lp = add_lwp (inferior_ptid);
851 check_for_thread_db ();
854 restore_child_signals_mask (&prev_mask);
860 linux_child_insert_fork_catchpoint (int pid)
862 if (! linux_supports_tracefork (pid))
863 error (_("Your system does not support fork catchpoints."));
867 linux_child_insert_vfork_catchpoint (int pid)
869 if (!linux_supports_tracefork (pid))
870 error (_("Your system does not support vfork catchpoints."));
874 linux_child_insert_exec_catchpoint (int pid)
876 if (!linux_supports_tracefork (pid))
877 error (_("Your system does not support exec catchpoints."));
881 linux_child_set_syscall_catchpoint (int pid, int needed, int any_count,
882 int table_size, int *table)
884 if (! linux_supports_tracesysgood (pid))
885 error (_("Your system does not support syscall catchpoints."));
886 /* On GNU/Linux, we ignore the arguments. It means that we only
887 enable the syscall catchpoints, but do not disable them.
889 Also, we do not use the `table' information because we do not
890 filter system calls here. We let GDB do the logic for us. */
894 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
895 are processes sharing the same VM space. A multi-threaded process
896 is basically a group of such processes. However, such a grouping
897 is almost entirely a user-space issue; the kernel doesn't enforce
898 such a grouping at all (this might change in the future). In
899 general, we'll rely on the threads library (i.e. the GNU/Linux
900 Threads library) to provide such a grouping.
902 It is perfectly well possible to write a multi-threaded application
903 without the assistance of a threads library, by using the clone
904 system call directly. This module should be able to give some
905 rudimentary support for debugging such applications if developers
906 specify the CLONE_PTRACE flag in the clone system call, and are
907 using the Linux kernel 2.4 or above.
909 Note that there are some peculiarities in GNU/Linux that affect
912 - In general one should specify the __WCLONE flag to waitpid in
913 order to make it report events for any of the cloned processes
914 (and leave it out for the initial process). However, if a cloned
915 process has exited the exit status is only reported if the
916 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
917 we cannot use it since GDB must work on older systems too.
919 - When a traced, cloned process exits and is waited for by the
920 debugger, the kernel reassigns it to the original parent and
921 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
922 library doesn't notice this, which leads to the "zombie problem":
923 When debugged a multi-threaded process that spawns a lot of
924 threads will run out of processes, even if the threads exit,
925 because the "zombies" stay around. */
927 /* List of known LWPs. */
928 struct lwp_info *lwp_list;
931 /* Original signal mask. */
932 static sigset_t normal_mask;
934 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
935 _initialize_linux_nat. */
936 static sigset_t suspend_mask;
938 /* Signals to block to make that sigsuspend work. */
939 static sigset_t blocked_mask;
941 /* SIGCHLD action. */
942 struct sigaction sigchld_action;
944 /* Block child signals (SIGCHLD and linux threads signals), and store
945 the previous mask in PREV_MASK. */
948 block_child_signals (sigset_t *prev_mask)
950 /* Make sure SIGCHLD is blocked. */
951 if (!sigismember (&blocked_mask, SIGCHLD))
952 sigaddset (&blocked_mask, SIGCHLD);
954 sigprocmask (SIG_BLOCK, &blocked_mask, prev_mask);
957 /* Restore child signals mask, previously returned by
958 block_child_signals. */
961 restore_child_signals_mask (sigset_t *prev_mask)
963 sigprocmask (SIG_SETMASK, prev_mask, NULL);
967 /* Prototypes for local functions. */
968 static int stop_wait_callback (struct lwp_info *lp, void *data);
969 static int linux_thread_alive (ptid_t ptid);
970 static char *linux_child_pid_to_exec_file (int pid);
971 static int cancel_breakpoint (struct lwp_info *lp);
974 /* Convert wait status STATUS to a string. Used for printing debug
978 status_to_str (int status)
982 if (WIFSTOPPED (status))
984 if (WSTOPSIG (status) == TRAP_IS_SYSCALL)
985 snprintf (buf, sizeof (buf), "%s (stopped at syscall)",
986 strsignal (SIGTRAP));
988 snprintf (buf, sizeof (buf), "%s (stopped)",
989 strsignal (WSTOPSIG (status)));
991 else if (WIFSIGNALED (status))
992 snprintf (buf, sizeof (buf), "%s (terminated)",
993 strsignal (WSTOPSIG (status)));
995 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
1000 /* Initialize the list of LWPs. Note that this module, contrary to
1001 what GDB's generic threads layer does for its thread list,
1002 re-initializes the LWP lists whenever we mourn or detach (which
1003 doesn't involve mourning) the inferior. */
1006 init_lwp_list (void)
1008 struct lwp_info *lp, *lpnext;
1010 for (lp = lwp_list; lp; lp = lpnext)
1019 /* Remove all LWPs belong to PID from the lwp list. */
1022 purge_lwp_list (int pid)
1024 struct lwp_info *lp, *lpprev, *lpnext;
1028 for (lp = lwp_list; lp; lp = lpnext)
1032 if (ptid_get_pid (lp->ptid) == pid)
1035 lwp_list = lp->next;
1037 lpprev->next = lp->next;
1046 /* Return the number of known LWPs in the tgid given by PID. */
1052 struct lwp_info *lp;
1054 for (lp = lwp_list; lp; lp = lp->next)
1055 if (ptid_get_pid (lp->ptid) == pid)
1061 /* Add the LWP specified by PID to the list. Return a pointer to the
1062 structure describing the new LWP. The LWP should already be stopped
1063 (with an exception for the very first LWP). */
1065 static struct lwp_info *
1066 add_lwp (ptid_t ptid)
1068 struct lwp_info *lp;
1070 gdb_assert (is_lwp (ptid));
1072 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
1074 memset (lp, 0, sizeof (struct lwp_info));
1076 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1080 lp->next = lwp_list;
1083 if (num_lwps (GET_PID (ptid)) > 1 && linux_nat_new_thread != NULL)
1084 linux_nat_new_thread (ptid);
1089 /* Remove the LWP specified by PID from the list. */
1092 delete_lwp (ptid_t ptid)
1094 struct lwp_info *lp, *lpprev;
1098 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
1099 if (ptid_equal (lp->ptid, ptid))
1106 lpprev->next = lp->next;
1108 lwp_list = lp->next;
1113 /* Return a pointer to the structure describing the LWP corresponding
1114 to PID. If no corresponding LWP could be found, return NULL. */
1116 static struct lwp_info *
1117 find_lwp_pid (ptid_t ptid)
1119 struct lwp_info *lp;
1123 lwp = GET_LWP (ptid);
1125 lwp = GET_PID (ptid);
1127 for (lp = lwp_list; lp; lp = lp->next)
1128 if (lwp == GET_LWP (lp->ptid))
1134 /* Returns true if PTID matches filter FILTER. FILTER can be the wild
1135 card MINUS_ONE_PTID (all ptid match it); can be a ptid representing
1136 a process (ptid_is_pid returns true), in which case, all lwps of
1137 that give process match, lwps of other process do not; or, it can
1138 represent a specific thread, in which case, only that thread will
1139 match true. PTID must represent an LWP, it can never be a wild
1143 ptid_match (ptid_t ptid, ptid_t filter)
1145 /* Since both parameters have the same type, prevent easy mistakes
1147 gdb_assert (!ptid_equal (ptid, minus_one_ptid)
1148 && !ptid_equal (ptid, null_ptid));
1150 if (ptid_equal (filter, minus_one_ptid))
1152 if (ptid_is_pid (filter)
1153 && ptid_get_pid (ptid) == ptid_get_pid (filter))
1155 else if (ptid_equal (ptid, filter))
1161 /* Call CALLBACK with its second argument set to DATA for every LWP in
1162 the list. If CALLBACK returns 1 for a particular LWP, return a
1163 pointer to the structure describing that LWP immediately.
1164 Otherwise return NULL. */
1167 iterate_over_lwps (ptid_t filter,
1168 int (*callback) (struct lwp_info *, void *),
1171 struct lwp_info *lp, *lpnext;
1173 for (lp = lwp_list; lp; lp = lpnext)
1177 if (ptid_match (lp->ptid, filter))
1179 if ((*callback) (lp, data))
1187 /* Update our internal state when changing from one checkpoint to
1188 another indicated by NEW_PTID. We can only switch single-threaded
1189 applications, so we only create one new LWP, and the previous list
1193 linux_nat_switch_fork (ptid_t new_ptid)
1195 struct lwp_info *lp;
1197 purge_lwp_list (GET_PID (inferior_ptid));
1199 lp = add_lwp (new_ptid);
1202 /* This changes the thread's ptid while preserving the gdb thread
1203 num. Also changes the inferior pid, while preserving the
1205 thread_change_ptid (inferior_ptid, new_ptid);
1207 /* We've just told GDB core that the thread changed target id, but,
1208 in fact, it really is a different thread, with different register
1210 registers_changed ();
1213 /* Handle the exit of a single thread LP. */
1216 exit_lwp (struct lwp_info *lp)
1218 struct thread_info *th = find_thread_ptid (lp->ptid);
1222 if (print_thread_events)
1223 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid));
1225 delete_thread (lp->ptid);
1228 delete_lwp (lp->ptid);
1231 /* Return an lwp's tgid, found in `/proc/PID/status'. */
1234 linux_proc_get_tgid (int lwpid)
1240 snprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid);
1241 status_file = fopen (buf, "r");
1242 if (status_file != NULL)
1244 while (fgets (buf, sizeof (buf), status_file))
1246 if (strncmp (buf, "Tgid:", 5) == 0)
1248 tgid = strtoul (buf + strlen ("Tgid:"), NULL, 10);
1253 fclose (status_file);
1259 /* Detect `T (stopped)' in `/proc/PID/status'.
1260 Other states including `T (tracing stop)' are reported as false. */
1263 pid_is_stopped (pid_t pid)
1269 snprintf (buf, sizeof (buf), "/proc/%d/status", (int) pid);
1270 status_file = fopen (buf, "r");
1271 if (status_file != NULL)
1275 while (fgets (buf, sizeof (buf), status_file))
1277 if (strncmp (buf, "State:", 6) == 0)
1283 if (have_state && strstr (buf, "T (stopped)") != NULL)
1285 fclose (status_file);
1290 /* Wait for the LWP specified by LP, which we have just attached to.
1291 Returns a wait status for that LWP, to cache. */
1294 linux_nat_post_attach_wait (ptid_t ptid, int first, int *cloned,
1297 pid_t new_pid, pid = GET_LWP (ptid);
1300 if (pid_is_stopped (pid))
1302 if (debug_linux_nat)
1303 fprintf_unfiltered (gdb_stdlog,
1304 "LNPAW: Attaching to a stopped process\n");
1306 /* The process is definitely stopped. It is in a job control
1307 stop, unless the kernel predates the TASK_STOPPED /
1308 TASK_TRACED distinction, in which case it might be in a
1309 ptrace stop. Make sure it is in a ptrace stop; from there we
1310 can kill it, signal it, et cetera.
1312 First make sure there is a pending SIGSTOP. Since we are
1313 already attached, the process can not transition from stopped
1314 to running without a PTRACE_CONT; so we know this signal will
1315 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1316 probably already in the queue (unless this kernel is old
1317 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1318 is not an RT signal, it can only be queued once. */
1319 kill_lwp (pid, SIGSTOP);
1321 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1322 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1323 ptrace (PTRACE_CONT, pid, 0, 0);
1326 /* Make sure the initial process is stopped. The user-level threads
1327 layer might want to poke around in the inferior, and that won't
1328 work if things haven't stabilized yet. */
1329 new_pid = my_waitpid (pid, &status, 0);
1330 if (new_pid == -1 && errno == ECHILD)
1333 warning (_("%s is a cloned process"), target_pid_to_str (ptid));
1335 /* Try again with __WCLONE to check cloned processes. */
1336 new_pid = my_waitpid (pid, &status, __WCLONE);
1340 gdb_assert (pid == new_pid && WIFSTOPPED (status));
1342 if (WSTOPSIG (status) != SIGSTOP)
1345 if (debug_linux_nat)
1346 fprintf_unfiltered (gdb_stdlog,
1347 "LNPAW: Received %s after attaching\n",
1348 status_to_str (status));
1354 /* Attach to the LWP specified by PID. Return 0 if successful or -1
1355 if the new LWP could not be attached. */
1358 lin_lwp_attach_lwp (ptid_t ptid)
1360 struct lwp_info *lp;
1363 gdb_assert (is_lwp (ptid));
1365 block_child_signals (&prev_mask);
1367 lp = find_lwp_pid (ptid);
1369 /* We assume that we're already attached to any LWP that has an id
1370 equal to the overall process id, and to any LWP that is already
1371 in our list of LWPs. If we're not seeing exit events from threads
1372 and we've had PID wraparound since we last tried to stop all threads,
1373 this assumption might be wrong; fortunately, this is very unlikely
1375 if (GET_LWP (ptid) != GET_PID (ptid) && lp == NULL)
1377 int status, cloned = 0, signalled = 0;
1379 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
1381 /* If we fail to attach to the thread, issue a warning,
1382 but continue. One way this can happen is if thread
1383 creation is interrupted; as of Linux kernel 2.6.19, a
1384 bug may place threads in the thread list and then fail
1386 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid),
1387 safe_strerror (errno));
1388 restore_child_signals_mask (&prev_mask);
1392 if (debug_linux_nat)
1393 fprintf_unfiltered (gdb_stdlog,
1394 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1395 target_pid_to_str (ptid));
1397 status = linux_nat_post_attach_wait (ptid, 0, &cloned, &signalled);
1398 lp = add_lwp (ptid);
1400 lp->cloned = cloned;
1401 lp->signalled = signalled;
1402 if (WSTOPSIG (status) != SIGSTOP)
1405 lp->status = status;
1408 target_post_attach (GET_LWP (lp->ptid));
1410 if (debug_linux_nat)
1412 fprintf_unfiltered (gdb_stdlog,
1413 "LLAL: waitpid %s received %s\n",
1414 target_pid_to_str (ptid),
1415 status_to_str (status));
1420 /* We assume that the LWP representing the original process is
1421 already stopped. Mark it as stopped in the data structure
1422 that the GNU/linux ptrace layer uses to keep track of
1423 threads. Note that this won't have already been done since
1424 the main thread will have, we assume, been stopped by an
1425 attach from a different layer. */
1427 lp = add_lwp (ptid);
1431 restore_child_signals_mask (&prev_mask);
1436 linux_nat_create_inferior (struct target_ops *ops,
1437 char *exec_file, char *allargs, char **env,
1440 #ifdef HAVE_PERSONALITY
1441 int personality_orig = 0, personality_set = 0;
1442 #endif /* HAVE_PERSONALITY */
1444 /* The fork_child mechanism is synchronous and calls target_wait, so
1445 we have to mask the async mode. */
1447 #ifdef HAVE_PERSONALITY
1448 if (disable_randomization)
1451 personality_orig = personality (0xffffffff);
1452 if (errno == 0 && !(personality_orig & ADDR_NO_RANDOMIZE))
1454 personality_set = 1;
1455 personality (personality_orig | ADDR_NO_RANDOMIZE);
1457 if (errno != 0 || (personality_set
1458 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE)))
1459 warning (_("Error disabling address space randomization: %s"),
1460 safe_strerror (errno));
1462 #endif /* HAVE_PERSONALITY */
1464 linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
1466 #ifdef HAVE_PERSONALITY
1467 if (personality_set)
1470 personality (personality_orig);
1472 warning (_("Error restoring address space randomization: %s"),
1473 safe_strerror (errno));
1475 #endif /* HAVE_PERSONALITY */
1479 linux_nat_attach (struct target_ops *ops, char *args, int from_tty)
1481 struct lwp_info *lp;
1485 linux_ops->to_attach (ops, args, from_tty);
1487 /* The ptrace base target adds the main thread with (pid,0,0)
1488 format. Decorate it with lwp info. */
1489 ptid = BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid));
1490 thread_change_ptid (inferior_ptid, ptid);
1492 /* Add the initial process as the first LWP to the list. */
1493 lp = add_lwp (ptid);
1495 status = linux_nat_post_attach_wait (lp->ptid, 1, &lp->cloned,
1499 /* Save the wait status to report later. */
1501 if (debug_linux_nat)
1502 fprintf_unfiltered (gdb_stdlog,
1503 "LNA: waitpid %ld, saving status %s\n",
1504 (long) GET_PID (lp->ptid), status_to_str (status));
1506 lp->status = status;
1508 if (target_can_async_p ())
1509 target_async (inferior_event_handler, 0);
1512 /* Get pending status of LP. */
1514 get_pending_status (struct lwp_info *lp, int *status)
1516 struct target_waitstatus last;
1519 get_last_target_status (&last_ptid, &last);
1521 /* If this lwp is the ptid that GDB is processing an event from, the
1522 signal will be in stop_signal. Otherwise, we may cache pending
1523 events in lp->status while trying to stop all threads (see
1524 stop_wait_callback). */
1530 enum target_signal signo = TARGET_SIGNAL_0;
1532 if (is_executing (lp->ptid))
1534 /* If the core thought this lwp was executing --- e.g., the
1535 executing property hasn't been updated yet, but the
1536 thread has been stopped with a stop_callback /
1537 stop_wait_callback sequence (see linux_nat_detach for
1538 example) --- we can only have pending events in the local
1540 signo = target_signal_from_host (WSTOPSIG (lp->status));
1544 /* If the core knows the thread is not executing, then we
1545 have the last signal recorded in
1546 thread_info->stop_signal. */
1548 struct thread_info *tp = find_thread_ptid (lp->ptid);
1549 signo = tp->stop_signal;
1552 if (signo != TARGET_SIGNAL_0
1553 && !signal_pass_state (signo))
1555 if (debug_linux_nat)
1556 fprintf_unfiltered (gdb_stdlog, "\
1557 GPT: lwp %s had signal %s, but it is in no pass state\n",
1558 target_pid_to_str (lp->ptid),
1559 target_signal_to_string (signo));
1563 if (signo != TARGET_SIGNAL_0)
1564 *status = W_STOPCODE (target_signal_to_host (signo));
1566 if (debug_linux_nat)
1567 fprintf_unfiltered (gdb_stdlog,
1568 "GPT: lwp %s as pending signal %s\n",
1569 target_pid_to_str (lp->ptid),
1570 target_signal_to_string (signo));
1575 if (GET_LWP (lp->ptid) == GET_LWP (last_ptid))
1577 struct thread_info *tp = find_thread_ptid (lp->ptid);
1578 if (tp->stop_signal != TARGET_SIGNAL_0
1579 && signal_pass_state (tp->stop_signal))
1580 *status = W_STOPCODE (target_signal_to_host (tp->stop_signal));
1583 *status = lp->status;
1590 detach_callback (struct lwp_info *lp, void *data)
1592 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1594 if (debug_linux_nat && lp->status)
1595 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
1596 strsignal (WSTOPSIG (lp->status)),
1597 target_pid_to_str (lp->ptid));
1599 /* If there is a pending SIGSTOP, get rid of it. */
1602 if (debug_linux_nat)
1603 fprintf_unfiltered (gdb_stdlog,
1604 "DC: Sending SIGCONT to %s\n",
1605 target_pid_to_str (lp->ptid));
1607 kill_lwp (GET_LWP (lp->ptid), SIGCONT);
1611 /* We don't actually detach from the LWP that has an id equal to the
1612 overall process id just yet. */
1613 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
1617 /* Pass on any pending signal for this LWP. */
1618 get_pending_status (lp, &status);
1621 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
1622 WSTOPSIG (status)) < 0)
1623 error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
1624 safe_strerror (errno));
1626 if (debug_linux_nat)
1627 fprintf_unfiltered (gdb_stdlog,
1628 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1629 target_pid_to_str (lp->ptid),
1630 strsignal (WSTOPSIG (status)));
1632 delete_lwp (lp->ptid);
1639 linux_nat_detach (struct target_ops *ops, char *args, int from_tty)
1643 enum target_signal sig;
1644 struct lwp_info *main_lwp;
1646 pid = GET_PID (inferior_ptid);
1648 if (target_can_async_p ())
1649 linux_nat_async (NULL, 0);
1651 /* Stop all threads before detaching. ptrace requires that the
1652 thread is stopped to sucessfully detach. */
1653 iterate_over_lwps (pid_to_ptid (pid), stop_callback, NULL);
1654 /* ... and wait until all of them have reported back that
1655 they're no longer running. */
1656 iterate_over_lwps (pid_to_ptid (pid), stop_wait_callback, NULL);
1658 iterate_over_lwps (pid_to_ptid (pid), detach_callback, NULL);
1660 /* Only the initial process should be left right now. */
1661 gdb_assert (num_lwps (GET_PID (inferior_ptid)) == 1);
1663 main_lwp = find_lwp_pid (pid_to_ptid (pid));
1665 /* Pass on any pending signal for the last LWP. */
1666 if ((args == NULL || *args == '\0')
1667 && get_pending_status (main_lwp, &status) != -1
1668 && WIFSTOPPED (status))
1670 /* Put the signal number in ARGS so that inf_ptrace_detach will
1671 pass it along with PTRACE_DETACH. */
1673 sprintf (args, "%d", (int) WSTOPSIG (status));
1674 fprintf_unfiltered (gdb_stdlog,
1675 "LND: Sending signal %s to %s\n",
1677 target_pid_to_str (main_lwp->ptid));
1680 delete_lwp (main_lwp->ptid);
1682 if (forks_exist_p ())
1684 /* Multi-fork case. The current inferior_ptid is being detached
1685 from, but there are other viable forks to debug. Detach from
1686 the current fork, and context-switch to the first
1688 linux_fork_detach (args, from_tty);
1690 if (non_stop && target_can_async_p ())
1691 target_async (inferior_event_handler, 0);
1694 linux_ops->to_detach (ops, args, from_tty);
1700 resume_callback (struct lwp_info *lp, void *data)
1702 if (lp->stopped && lp->status == 0)
1704 if (debug_linux_nat)
1705 fprintf_unfiltered (gdb_stdlog,
1706 "RC: PTRACE_CONT %s, 0, 0 (resuming sibling)\n",
1707 target_pid_to_str (lp->ptid));
1709 linux_ops->to_resume (linux_ops,
1710 pid_to_ptid (GET_LWP (lp->ptid)),
1711 0, TARGET_SIGNAL_0);
1712 if (debug_linux_nat)
1713 fprintf_unfiltered (gdb_stdlog,
1714 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1715 target_pid_to_str (lp->ptid));
1718 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
1720 else if (lp->stopped && debug_linux_nat)
1721 fprintf_unfiltered (gdb_stdlog, "RC: Not resuming sibling %s (has pending)\n",
1722 target_pid_to_str (lp->ptid));
1723 else if (debug_linux_nat)
1724 fprintf_unfiltered (gdb_stdlog, "RC: Not resuming sibling %s (not stopped)\n",
1725 target_pid_to_str (lp->ptid));
1731 resume_clear_callback (struct lwp_info *lp, void *data)
1738 resume_set_callback (struct lwp_info *lp, void *data)
1745 linux_nat_resume (struct target_ops *ops,
1746 ptid_t ptid, int step, enum target_signal signo)
1749 struct lwp_info *lp;
1752 if (debug_linux_nat)
1753 fprintf_unfiltered (gdb_stdlog,
1754 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1755 step ? "step" : "resume",
1756 target_pid_to_str (ptid),
1757 signo ? strsignal (signo) : "0",
1758 target_pid_to_str (inferior_ptid));
1760 block_child_signals (&prev_mask);
1762 /* A specific PTID means `step only this process id'. */
1763 resume_many = (ptid_equal (minus_one_ptid, ptid)
1764 || ptid_is_pid (ptid));
1768 /* Mark the lwps we're resuming as resumed. */
1769 iterate_over_lwps (minus_one_ptid, resume_clear_callback, NULL);
1770 iterate_over_lwps (ptid, resume_set_callback, NULL);
1773 iterate_over_lwps (minus_one_ptid, resume_set_callback, NULL);
1775 /* See if it's the current inferior that should be handled
1778 lp = find_lwp_pid (inferior_ptid);
1780 lp = find_lwp_pid (ptid);
1781 gdb_assert (lp != NULL);
1783 /* Remember if we're stepping. */
1786 /* If we have a pending wait status for this thread, there is no
1787 point in resuming the process. But first make sure that
1788 linux_nat_wait won't preemptively handle the event - we
1789 should never take this short-circuit if we are going to
1790 leave LP running, since we have skipped resuming all the
1791 other threads. This bit of code needs to be synchronized
1792 with linux_nat_wait. */
1794 if (lp->status && WIFSTOPPED (lp->status))
1797 struct inferior *inf;
1799 inf = find_inferior_pid (ptid_get_pid (lp->ptid));
1801 saved_signo = target_signal_from_host (WSTOPSIG (lp->status));
1803 /* Defer to common code if we're gaining control of the
1805 if (inf->stop_soon == NO_STOP_QUIETLY
1806 && signal_stop_state (saved_signo) == 0
1807 && signal_print_state (saved_signo) == 0
1808 && signal_pass_state (saved_signo) == 1)
1810 if (debug_linux_nat)
1811 fprintf_unfiltered (gdb_stdlog,
1812 "LLR: Not short circuiting for ignored "
1813 "status 0x%x\n", lp->status);
1815 /* FIXME: What should we do if we are supposed to continue
1816 this thread with a signal? */
1817 gdb_assert (signo == TARGET_SIGNAL_0);
1818 signo = saved_signo;
1825 /* FIXME: What should we do if we are supposed to continue
1826 this thread with a signal? */
1827 gdb_assert (signo == TARGET_SIGNAL_0);
1829 if (debug_linux_nat)
1830 fprintf_unfiltered (gdb_stdlog,
1831 "LLR: Short circuiting for status 0x%x\n",
1834 restore_child_signals_mask (&prev_mask);
1835 if (target_can_async_p ())
1837 target_async (inferior_event_handler, 0);
1838 /* Tell the event loop we have something to process. */
1844 /* Mark LWP as not stopped to prevent it from being continued by
1849 iterate_over_lwps (ptid, resume_callback, NULL);
1851 /* Convert to something the lower layer understands. */
1852 ptid = pid_to_ptid (GET_LWP (lp->ptid));
1854 linux_ops->to_resume (linux_ops, ptid, step, signo);
1855 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
1857 if (debug_linux_nat)
1858 fprintf_unfiltered (gdb_stdlog,
1859 "LLR: %s %s, %s (resume event thread)\n",
1860 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1861 target_pid_to_str (ptid),
1862 signo ? strsignal (signo) : "0");
1864 restore_child_signals_mask (&prev_mask);
1865 if (target_can_async_p ())
1866 target_async (inferior_event_handler, 0);
1869 /* Issue kill to specified lwp. */
1871 static int tkill_failed;
1874 kill_lwp (int lwpid, int signo)
1878 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1879 fails, then we are not using nptl threads and we should be using kill. */
1881 #ifdef HAVE_TKILL_SYSCALL
1884 int ret = syscall (__NR_tkill, lwpid, signo);
1885 if (errno != ENOSYS)
1892 return kill (lwpid, signo);
1895 /* Handle a GNU/Linux extended wait response. If we see a clone
1896 event, we need to add the new LWP to our list (and not report the
1897 trap to higher layers). This function returns non-zero if the
1898 event should be ignored and we should wait again. If STOPPING is
1899 true, the new LWP remains stopped, otherwise it is continued. */
1902 linux_handle_extended_wait (struct lwp_info *lp, int status,
1905 int pid = GET_LWP (lp->ptid);
1906 struct target_waitstatus *ourstatus = &lp->waitstatus;
1907 struct lwp_info *new_lp = NULL;
1908 int event = status >> 16;
1910 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
1911 || event == PTRACE_EVENT_CLONE)
1913 unsigned long new_pid;
1916 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
1918 /* If we haven't already seen the new PID stop, wait for it now. */
1919 if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
1921 /* The new child has a pending SIGSTOP. We can't affect it until it
1922 hits the SIGSTOP, but we're already attached. */
1923 ret = my_waitpid (new_pid, &status,
1924 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
1926 perror_with_name (_("waiting for new child"));
1927 else if (ret != new_pid)
1928 internal_error (__FILE__, __LINE__,
1929 _("wait returned unexpected PID %d"), ret);
1930 else if (!WIFSTOPPED (status))
1931 internal_error (__FILE__, __LINE__,
1932 _("wait returned unexpected status 0x%x"), status);
1935 ourstatus->value.related_pid = ptid_build (new_pid, new_pid, 0);
1937 if (event == PTRACE_EVENT_FORK
1938 && linux_fork_checkpointing_p (GET_PID (lp->ptid)))
1940 struct fork_info *fp;
1942 /* Handle checkpointing by linux-fork.c here as a special
1943 case. We don't want the follow-fork-mode or 'catch fork'
1944 to interfere with this. */
1946 /* This won't actually modify the breakpoint list, but will
1947 physically remove the breakpoints from the child. */
1948 detach_breakpoints (new_pid);
1950 /* Retain child fork in ptrace (stopped) state. */
1951 fp = find_fork_pid (new_pid);
1953 fp = add_fork (new_pid);
1955 /* Report as spurious, so that infrun doesn't want to follow
1956 this fork. We're actually doing an infcall in
1958 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1959 linux_enable_event_reporting (pid_to_ptid (new_pid));
1961 /* Report the stop to the core. */
1965 if (event == PTRACE_EVENT_FORK)
1966 ourstatus->kind = TARGET_WAITKIND_FORKED;
1967 else if (event == PTRACE_EVENT_VFORK)
1968 ourstatus->kind = TARGET_WAITKIND_VFORKED;
1971 struct cleanup *old_chain;
1973 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1974 new_lp = add_lwp (BUILD_LWP (new_pid, GET_PID (lp->ptid)));
1976 new_lp->stopped = 1;
1978 if (WSTOPSIG (status) != SIGSTOP)
1980 /* This can happen if someone starts sending signals to
1981 the new thread before it gets a chance to run, which
1982 have a lower number than SIGSTOP (e.g. SIGUSR1).
1983 This is an unlikely case, and harder to handle for
1984 fork / vfork than for clone, so we do not try - but
1985 we handle it for clone events here. We'll send
1986 the other signal on to the thread below. */
1988 new_lp->signalled = 1;
1995 /* Add the new thread to GDB's lists as soon as possible
1998 1) the frontend doesn't have to wait for a stop to
2001 2) we tag it with the correct running state. */
2003 /* If the thread_db layer is active, let it know about
2004 this new thread, and add it to GDB's list. */
2005 if (!thread_db_attach_lwp (new_lp->ptid))
2007 /* We're not using thread_db. Add it to GDB's
2009 target_post_attach (GET_LWP (new_lp->ptid));
2010 add_thread (new_lp->ptid);
2015 set_running (new_lp->ptid, 1);
2016 set_executing (new_lp->ptid, 1);
2022 new_lp->stopped = 0;
2023 new_lp->resumed = 1;
2024 ptrace (PTRACE_CONT, new_pid, 0,
2025 status ? WSTOPSIG (status) : 0);
2028 if (debug_linux_nat)
2029 fprintf_unfiltered (gdb_stdlog,
2030 "LHEW: Got clone event from LWP %ld, resuming\n",
2031 GET_LWP (lp->ptid));
2032 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2040 if (event == PTRACE_EVENT_EXEC)
2042 if (debug_linux_nat)
2043 fprintf_unfiltered (gdb_stdlog,
2044 "LHEW: Got exec event from LWP %ld\n",
2045 GET_LWP (lp->ptid));
2047 ourstatus->kind = TARGET_WAITKIND_EXECD;
2048 ourstatus->value.execd_pathname
2049 = xstrdup (linux_child_pid_to_exec_file (pid));
2051 if (linux_parent_pid)
2053 detach_breakpoints (linux_parent_pid);
2054 ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0);
2056 linux_parent_pid = 0;
2059 /* At this point, all inserted breakpoints are gone. Doing this
2060 as soon as we detect an exec prevents the badness of deleting
2061 a breakpoint writing the current "shadow contents" to lift
2062 the bp. That shadow is NOT valid after an exec.
2064 Note that we have to do this after the detach_breakpoints
2065 call above, otherwise breakpoints wouldn't be lifted from the
2066 parent on a vfork, because detach_breakpoints would think
2067 that breakpoints are not inserted. */
2068 mark_breakpoints_out ();
2072 /* Used for 'catch syscall' feature. */
2073 if (WSTOPSIG (status) == TRAP_IS_SYSCALL)
2075 if (catch_syscall_enabled () == 0)
2076 ourstatus->kind = TARGET_WAITKIND_IGNORE;
2079 struct regcache *regcache = get_thread_regcache (lp->ptid);
2080 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2082 ourstatus->value.syscall_number =
2083 (int) gdbarch_get_syscall_number (gdbarch, lp->ptid);
2085 /* If we are catching this specific syscall number, then we
2086 should update the target_status to reflect which event
2087 has occurred. But if this syscall is not to be caught,
2088 then we can safely mark the event as a SYSCALL_RETURN.
2090 This is particularly needed if:
2092 - We are catching any syscalls, or
2093 - We are catching the syscall "exit"
2095 In this case, as the syscall "exit" *doesn't* return,
2096 then GDB would be confused because it would mark the last
2097 syscall event as a SYSCALL_ENTRY. After that, if we re-ran the
2098 inferior GDB will think that the first syscall event is
2099 the opposite of a SYSCALL_ENTRY, which is the SYSCALL_RETURN.
2100 Therefore, GDB would report inverted syscall events. */
2101 if (catching_syscall_number (ourstatus->value.syscall_number))
2103 (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY) ?
2104 TARGET_WAITKIND_SYSCALL_RETURN : TARGET_WAITKIND_SYSCALL_ENTRY;
2106 ourstatus->kind = TARGET_WAITKIND_SYSCALL_RETURN;
2108 lp->syscall_state = ourstatus->kind;
2113 internal_error (__FILE__, __LINE__,
2114 _("unknown ptrace event %d"), event);
2117 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2121 wait_lwp (struct lwp_info *lp)
2125 int thread_dead = 0;
2127 gdb_assert (!lp->stopped);
2128 gdb_assert (lp->status == 0);
2130 pid = my_waitpid (GET_LWP (lp->ptid), &status, 0);
2131 if (pid == -1 && errno == ECHILD)
2133 pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
2134 if (pid == -1 && errno == ECHILD)
2136 /* The thread has previously exited. We need to delete it
2137 now because, for some vendor 2.4 kernels with NPTL
2138 support backported, there won't be an exit event unless
2139 it is the main thread. 2.6 kernels will report an exit
2140 event for each thread that exits, as expected. */
2142 if (debug_linux_nat)
2143 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
2144 target_pid_to_str (lp->ptid));
2150 gdb_assert (pid == GET_LWP (lp->ptid));
2152 if (debug_linux_nat)
2154 fprintf_unfiltered (gdb_stdlog,
2155 "WL: waitpid %s received %s\n",
2156 target_pid_to_str (lp->ptid),
2157 status_to_str (status));
2161 /* Check if the thread has exited. */
2162 if (WIFEXITED (status) || WIFSIGNALED (status))
2165 if (debug_linux_nat)
2166 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
2167 target_pid_to_str (lp->ptid));
2176 gdb_assert (WIFSTOPPED (status));
2178 /* Handle GNU/Linux's extended waitstatus for trace events. */
2179 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
2181 if (debug_linux_nat)
2182 fprintf_unfiltered (gdb_stdlog,
2183 "WL: Handling extended status 0x%06x\n",
2185 if (linux_handle_extended_wait (lp, status, 1))
2186 return wait_lwp (lp);
2192 /* Save the most recent siginfo for LP. This is currently only called
2193 for SIGTRAP; some ports use the si_addr field for
2194 target_stopped_data_address. In the future, it may also be used to
2195 restore the siginfo of requeued signals. */
2198 save_siginfo (struct lwp_info *lp)
2201 ptrace (PTRACE_GETSIGINFO, GET_LWP (lp->ptid),
2202 (PTRACE_TYPE_ARG3) 0, &lp->siginfo);
2205 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
2208 /* Send a SIGSTOP to LP. */
2211 stop_callback (struct lwp_info *lp, void *data)
2213 if (!lp->stopped && !lp->signalled)
2217 if (debug_linux_nat)
2219 fprintf_unfiltered (gdb_stdlog,
2220 "SC: kill %s **<SIGSTOP>**\n",
2221 target_pid_to_str (lp->ptid));
2224 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
2225 if (debug_linux_nat)
2227 fprintf_unfiltered (gdb_stdlog,
2228 "SC: lwp kill %d %s\n",
2230 errno ? safe_strerror (errno) : "ERRNO-OK");
2234 gdb_assert (lp->status == 0);
2240 /* Return non-zero if LWP PID has a pending SIGINT. */
2243 linux_nat_has_pending_sigint (int pid)
2245 sigset_t pending, blocked, ignored;
2248 linux_proc_pending_signals (pid, &pending, &blocked, &ignored);
2250 if (sigismember (&pending, SIGINT)
2251 && !sigismember (&ignored, SIGINT))
2257 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2260 set_ignore_sigint (struct lwp_info *lp, void *data)
2262 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2263 flag to consume the next one. */
2264 if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status)
2265 && WSTOPSIG (lp->status) == SIGINT)
2268 lp->ignore_sigint = 1;
2273 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2274 This function is called after we know the LWP has stopped; if the LWP
2275 stopped before the expected SIGINT was delivered, then it will never have
2276 arrived. Also, if the signal was delivered to a shared queue and consumed
2277 by a different thread, it will never be delivered to this LWP. */
2280 maybe_clear_ignore_sigint (struct lwp_info *lp)
2282 if (!lp->ignore_sigint)
2285 if (!linux_nat_has_pending_sigint (GET_LWP (lp->ptid)))
2287 if (debug_linux_nat)
2288 fprintf_unfiltered (gdb_stdlog,
2289 "MCIS: Clearing bogus flag for %s\n",
2290 target_pid_to_str (lp->ptid));
2291 lp->ignore_sigint = 0;
2295 /* Wait until LP is stopped. */
2298 stop_wait_callback (struct lwp_info *lp, void *data)
2304 status = wait_lwp (lp);
2308 if (lp->ignore_sigint && WIFSTOPPED (status)
2309 && WSTOPSIG (status) == SIGINT)
2311 lp->ignore_sigint = 0;
2314 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2315 if (debug_linux_nat)
2316 fprintf_unfiltered (gdb_stdlog,
2317 "PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)\n",
2318 target_pid_to_str (lp->ptid),
2319 errno ? safe_strerror (errno) : "OK");
2321 return stop_wait_callback (lp, NULL);
2324 maybe_clear_ignore_sigint (lp);
2326 if (WSTOPSIG (status) != SIGSTOP)
2328 if (WSTOPSIG (status) == SIGTRAP)
2330 /* If a LWP other than the LWP that we're reporting an
2331 event for has hit a GDB breakpoint (as opposed to
2332 some random trap signal), then just arrange for it to
2333 hit it again later. We don't keep the SIGTRAP status
2334 and don't forward the SIGTRAP signal to the LWP. We
2335 will handle the current event, eventually we will
2336 resume all LWPs, and this one will get its breakpoint
2339 If we do not do this, then we run the risk that the
2340 user will delete or disable the breakpoint, but the
2341 thread will have already tripped on it. */
2343 /* Save the trap's siginfo in case we need it later. */
2346 /* Now resume this LWP and get the SIGSTOP event. */
2348 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2349 if (debug_linux_nat)
2351 fprintf_unfiltered (gdb_stdlog,
2352 "PTRACE_CONT %s, 0, 0 (%s)\n",
2353 target_pid_to_str (lp->ptid),
2354 errno ? safe_strerror (errno) : "OK");
2356 fprintf_unfiltered (gdb_stdlog,
2357 "SWC: Candidate SIGTRAP event in %s\n",
2358 target_pid_to_str (lp->ptid));
2360 /* Hold this event/waitstatus while we check to see if
2361 there are any more (we still want to get that SIGSTOP). */
2362 stop_wait_callback (lp, NULL);
2364 /* Hold the SIGTRAP for handling by linux_nat_wait. If
2365 there's another event, throw it back into the
2369 if (debug_linux_nat)
2370 fprintf_unfiltered (gdb_stdlog,
2371 "SWC: kill %s, %s\n",
2372 target_pid_to_str (lp->ptid),
2373 status_to_str ((int) status));
2374 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
2377 /* Save the sigtrap event. */
2378 lp->status = status;
2383 /* The thread was stopped with a signal other than
2384 SIGSTOP, and didn't accidentally trip a breakpoint. */
2386 if (debug_linux_nat)
2388 fprintf_unfiltered (gdb_stdlog,
2389 "SWC: Pending event %s in %s\n",
2390 status_to_str ((int) status),
2391 target_pid_to_str (lp->ptid));
2393 /* Now resume this LWP and get the SIGSTOP event. */
2395 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2396 if (debug_linux_nat)
2397 fprintf_unfiltered (gdb_stdlog,
2398 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2399 target_pid_to_str (lp->ptid),
2400 errno ? safe_strerror (errno) : "OK");
2402 /* Hold this event/waitstatus while we check to see if
2403 there are any more (we still want to get that SIGSTOP). */
2404 stop_wait_callback (lp, NULL);
2406 /* If the lp->status field is still empty, use it to
2407 hold this event. If not, then this event must be
2408 returned to the event queue of the LWP. */
2411 if (debug_linux_nat)
2413 fprintf_unfiltered (gdb_stdlog,
2414 "SWC: kill %s, %s\n",
2415 target_pid_to_str (lp->ptid),
2416 status_to_str ((int) status));
2418 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
2421 lp->status = status;
2427 /* We caught the SIGSTOP that we intended to catch, so
2428 there's no SIGSTOP pending. */
2437 /* Return non-zero if LP has a wait status pending. */
2440 status_callback (struct lwp_info *lp, void *data)
2442 /* Only report a pending wait status if we pretend that this has
2443 indeed been resumed. */
2444 /* We check for lp->waitstatus in addition to lp->status, because we
2445 can have pending process exits recorded in lp->waitstatus, and
2446 W_EXITCODE(0,0) == 0. */
2447 return ((lp->status != 0
2448 || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2452 /* Return non-zero if LP isn't stopped. */
2455 running_callback (struct lwp_info *lp, void *data)
2457 return (lp->stopped == 0 || (lp->status != 0 && lp->resumed));
2460 /* Count the LWP's that have had events. */
2463 count_events_callback (struct lwp_info *lp, void *data)
2467 gdb_assert (count != NULL);
2469 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2470 if (lp->status != 0 && lp->resumed
2471 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
2477 /* Select the LWP (if any) that is currently being single-stepped. */
2480 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
2482 if (lp->step && lp->status != 0)
2488 /* Select the Nth LWP that has had a SIGTRAP event. */
2491 select_event_lwp_callback (struct lwp_info *lp, void *data)
2493 int *selector = data;
2495 gdb_assert (selector != NULL);
2497 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2498 if (lp->status != 0 && lp->resumed
2499 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
2500 if ((*selector)-- == 0)
2507 cancel_breakpoint (struct lwp_info *lp)
2509 /* Arrange for a breakpoint to be hit again later. We don't keep
2510 the SIGTRAP status and don't forward the SIGTRAP signal to the
2511 LWP. We will handle the current event, eventually we will resume
2512 this LWP, and this breakpoint will trap again.
2514 If we do not do this, then we run the risk that the user will
2515 delete or disable the breakpoint, but the LWP will have already
2518 struct regcache *regcache = get_thread_regcache (lp->ptid);
2519 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2522 pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
2523 if (breakpoint_inserted_here_p (pc))
2525 if (debug_linux_nat)
2526 fprintf_unfiltered (gdb_stdlog,
2527 "CB: Push back breakpoint for %s\n",
2528 target_pid_to_str (lp->ptid));
2530 /* Back up the PC if necessary. */
2531 if (gdbarch_decr_pc_after_break (gdbarch))
2532 regcache_write_pc (regcache, pc);
2540 cancel_breakpoints_callback (struct lwp_info *lp, void *data)
2542 struct lwp_info *event_lp = data;
2544 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2548 /* If a LWP other than the LWP that we're reporting an event for has
2549 hit a GDB breakpoint (as opposed to some random trap signal),
2550 then just arrange for it to hit it again later. We don't keep
2551 the SIGTRAP status and don't forward the SIGTRAP signal to the
2552 LWP. We will handle the current event, eventually we will resume
2553 all LWPs, and this one will get its breakpoint trap again.
2555 If we do not do this, then we run the risk that the user will
2556 delete or disable the breakpoint, but the LWP will have already
2560 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
2561 && cancel_breakpoint (lp))
2562 /* Throw away the SIGTRAP. */
2568 /* Select one LWP out of those that have events pending. */
2571 select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status)
2574 int random_selector;
2575 struct lwp_info *event_lp;
2577 /* Record the wait status for the original LWP. */
2578 (*orig_lp)->status = *status;
2580 /* Give preference to any LWP that is being single-stepped. */
2581 event_lp = iterate_over_lwps (filter,
2582 select_singlestep_lwp_callback, NULL);
2583 if (event_lp != NULL)
2585 if (debug_linux_nat)
2586 fprintf_unfiltered (gdb_stdlog,
2587 "SEL: Select single-step %s\n",
2588 target_pid_to_str (event_lp->ptid));
2592 /* No single-stepping LWP. Select one at random, out of those
2593 which have had SIGTRAP events. */
2595 /* First see how many SIGTRAP events we have. */
2596 iterate_over_lwps (filter, count_events_callback, &num_events);
2598 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2599 random_selector = (int)
2600 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
2602 if (debug_linux_nat && num_events > 1)
2603 fprintf_unfiltered (gdb_stdlog,
2604 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2605 num_events, random_selector);
2607 event_lp = iterate_over_lwps (filter,
2608 select_event_lwp_callback,
2612 if (event_lp != NULL)
2614 /* Switch the event LWP. */
2615 *orig_lp = event_lp;
2616 *status = event_lp->status;
2619 /* Flush the wait status for the event LWP. */
2620 (*orig_lp)->status = 0;
2623 /* Return non-zero if LP has been resumed. */
2626 resumed_callback (struct lwp_info *lp, void *data)
2631 /* Stop an active thread, verify it still exists, then resume it. */
2634 stop_and_resume_callback (struct lwp_info *lp, void *data)
2636 struct lwp_info *ptr;
2638 if (!lp->stopped && !lp->signalled)
2640 stop_callback (lp, NULL);
2641 stop_wait_callback (lp, NULL);
2642 /* Resume if the lwp still exists. */
2643 for (ptr = lwp_list; ptr; ptr = ptr->next)
2646 resume_callback (lp, NULL);
2647 resume_set_callback (lp, NULL);
2653 /* Check if we should go on and pass this event to common code.
2654 Return the affected lwp if we are, or NULL otherwise. */
2655 static struct lwp_info *
2656 linux_nat_filter_event (int lwpid, int status, int options)
2658 struct lwp_info *lp;
2660 lp = find_lwp_pid (pid_to_ptid (lwpid));
2662 /* Check for stop events reported by a process we didn't already
2663 know about - anything not already in our LWP list.
2665 If we're expecting to receive stopped processes after
2666 fork, vfork, and clone events, then we'll just add the
2667 new one to our list and go back to waiting for the event
2668 to be reported - the stopped process might be returned
2669 from waitpid before or after the event is. */
2670 if (WIFSTOPPED (status) && !lp)
2672 linux_record_stopped_pid (lwpid, status);
2676 /* Make sure we don't report an event for the exit of an LWP not in
2677 our list, i.e. not part of the current process. This can happen
2678 if we detach from a program we original forked and then it
2680 if (!WIFSTOPPED (status) && !lp)
2683 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2684 CLONE_PTRACE processes which do not use the thread library -
2685 otherwise we wouldn't find the new LWP this way. That doesn't
2686 currently work, and the following code is currently unreachable
2687 due to the two blocks above. If it's fixed some day, this code
2688 should be broken out into a function so that we can also pick up
2689 LWPs from the new interface. */
2692 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
2693 if (options & __WCLONE)
2696 gdb_assert (WIFSTOPPED (status)
2697 && WSTOPSIG (status) == SIGSTOP);
2700 if (!in_thread_list (inferior_ptid))
2702 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
2703 GET_PID (inferior_ptid));
2704 add_thread (inferior_ptid);
2707 add_thread (lp->ptid);
2710 /* Save the trap's siginfo in case we need it later. */
2711 if (WIFSTOPPED (status)
2712 && (WSTOPSIG (status) == SIGTRAP || WSTOPSIG (status) == TRAP_IS_SYSCALL))
2715 /* Handle GNU/Linux's extended waitstatus for trace events.
2716 It is necessary to check if WSTOPSIG is signaling that
2717 the inferior is entering/exiting a system call. */
2718 if (WIFSTOPPED (status)
2719 && ((WSTOPSIG (status) == TRAP_IS_SYSCALL)
2720 || (WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)))
2722 if (debug_linux_nat)
2723 fprintf_unfiltered (gdb_stdlog,
2724 "LLW: Handling extended status 0x%06x\n",
2726 if (linux_handle_extended_wait (lp, status, 0))
2730 /* Check if the thread has exited. */
2731 if ((WIFEXITED (status) || WIFSIGNALED (status))
2732 && num_lwps (GET_PID (lp->ptid)) > 1)
2734 /* If this is the main thread, we must stop all threads and verify
2735 if they are still alive. This is because in the nptl thread model
2736 on Linux 2.4, there is no signal issued for exiting LWPs
2737 other than the main thread. We only get the main thread exit
2738 signal once all child threads have already exited. If we
2739 stop all the threads and use the stop_wait_callback to check
2740 if they have exited we can determine whether this signal
2741 should be ignored or whether it means the end of the debugged
2742 application, regardless of which threading model is being
2744 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
2747 iterate_over_lwps (pid_to_ptid (GET_PID (lp->ptid)),
2748 stop_and_resume_callback, NULL);
2751 if (debug_linux_nat)
2752 fprintf_unfiltered (gdb_stdlog,
2753 "LLW: %s exited.\n",
2754 target_pid_to_str (lp->ptid));
2756 if (num_lwps (GET_PID (lp->ptid)) > 1)
2758 /* If there is at least one more LWP, then the exit signal
2759 was not the end of the debugged application and should be
2766 /* Check if the current LWP has previously exited. In the nptl
2767 thread model, LWPs other than the main thread do not issue
2768 signals when they exit so we must check whenever the thread has
2769 stopped. A similar check is made in stop_wait_callback(). */
2770 if (num_lwps (GET_PID (lp->ptid)) > 1 && !linux_thread_alive (lp->ptid))
2772 ptid_t ptid = pid_to_ptid (GET_PID (lp->ptid));
2774 if (debug_linux_nat)
2775 fprintf_unfiltered (gdb_stdlog,
2776 "LLW: %s exited.\n",
2777 target_pid_to_str (lp->ptid));
2781 /* Make sure there is at least one thread running. */
2782 gdb_assert (iterate_over_lwps (ptid, running_callback, NULL));
2784 /* Discard the event. */
2788 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2789 an attempt to stop an LWP. */
2791 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
2793 if (debug_linux_nat)
2794 fprintf_unfiltered (gdb_stdlog,
2795 "LLW: Delayed SIGSTOP caught for %s.\n",
2796 target_pid_to_str (lp->ptid));
2798 /* This is a delayed SIGSTOP. */
2801 registers_changed ();
2803 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
2804 lp->step, TARGET_SIGNAL_0);
2805 if (debug_linux_nat)
2806 fprintf_unfiltered (gdb_stdlog,
2807 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2809 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2810 target_pid_to_str (lp->ptid));
2813 gdb_assert (lp->resumed);
2815 /* Discard the event. */
2819 /* Make sure we don't report a SIGINT that we have already displayed
2820 for another thread. */
2821 if (lp->ignore_sigint
2822 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT)
2824 if (debug_linux_nat)
2825 fprintf_unfiltered (gdb_stdlog,
2826 "LLW: Delayed SIGINT caught for %s.\n",
2827 target_pid_to_str (lp->ptid));
2829 /* This is a delayed SIGINT. */
2830 lp->ignore_sigint = 0;
2832 registers_changed ();
2833 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
2834 lp->step, TARGET_SIGNAL_0);
2835 if (debug_linux_nat)
2836 fprintf_unfiltered (gdb_stdlog,
2837 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2839 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2840 target_pid_to_str (lp->ptid));
2843 gdb_assert (lp->resumed);
2845 /* Discard the event. */
2849 /* An interesting event. */
2855 linux_nat_wait_1 (struct target_ops *ops,
2856 ptid_t ptid, struct target_waitstatus *ourstatus,
2859 static sigset_t prev_mask;
2860 struct lwp_info *lp = NULL;
2865 if (debug_linux_nat_async)
2866 fprintf_unfiltered (gdb_stdlog, "LLW: enter\n");
2868 /* The first time we get here after starting a new inferior, we may
2869 not have added it to the LWP list yet - this is the earliest
2870 moment at which we know its PID. */
2871 if (ptid_is_pid (inferior_ptid))
2873 /* Upgrade the main thread's ptid. */
2874 thread_change_ptid (inferior_ptid,
2875 BUILD_LWP (GET_PID (inferior_ptid),
2876 GET_PID (inferior_ptid)));
2878 lp = add_lwp (inferior_ptid);
2882 /* Make sure SIGCHLD is blocked. */
2883 block_child_signals (&prev_mask);
2885 if (ptid_equal (ptid, minus_one_ptid))
2887 else if (ptid_is_pid (ptid))
2888 /* A request to wait for a specific tgid. This is not possible
2889 with waitpid, so instead, we wait for any child, and leave
2890 children we're not interested in right now with a pending
2891 status to report later. */
2894 pid = GET_LWP (ptid);
2900 /* Make sure there is at least one LWP that has been resumed. */
2901 gdb_assert (iterate_over_lwps (ptid, resumed_callback, NULL));
2903 /* First check if there is a LWP with a wait status pending. */
2906 /* Any LWP that's been resumed will do. */
2907 lp = iterate_over_lwps (ptid, status_callback, NULL);
2910 status = lp->status;
2913 if (debug_linux_nat && status)
2914 fprintf_unfiltered (gdb_stdlog,
2915 "LLW: Using pending wait status %s for %s.\n",
2916 status_to_str (status),
2917 target_pid_to_str (lp->ptid));
2920 /* But if we don't find one, we'll have to wait, and check both
2921 cloned and uncloned processes. We start with the cloned
2923 options = __WCLONE | WNOHANG;
2925 else if (is_lwp (ptid))
2927 if (debug_linux_nat)
2928 fprintf_unfiltered (gdb_stdlog,
2929 "LLW: Waiting for specific LWP %s.\n",
2930 target_pid_to_str (ptid));
2932 /* We have a specific LWP to check. */
2933 lp = find_lwp_pid (ptid);
2935 status = lp->status;
2938 if (debug_linux_nat && status)
2939 fprintf_unfiltered (gdb_stdlog,
2940 "LLW: Using pending wait status %s for %s.\n",
2941 status_to_str (status),
2942 target_pid_to_str (lp->ptid));
2944 /* If we have to wait, take into account whether PID is a cloned
2945 process or not. And we have to convert it to something that
2946 the layer beneath us can understand. */
2947 options = lp->cloned ? __WCLONE : 0;
2948 pid = GET_LWP (ptid);
2950 /* We check for lp->waitstatus in addition to lp->status,
2951 because we can have pending process exits recorded in
2952 lp->status and W_EXITCODE(0,0) == 0. We should probably have
2953 an additional lp->status_p flag. */
2954 if (status == 0 && lp->waitstatus.kind == TARGET_WAITKIND_IGNORE)
2958 if (lp && lp->signalled)
2960 /* A pending SIGSTOP may interfere with the normal stream of
2961 events. In a typical case where interference is a problem,
2962 we have a SIGSTOP signal pending for LWP A while
2963 single-stepping it, encounter an event in LWP B, and take the
2964 pending SIGSTOP while trying to stop LWP A. After processing
2965 the event in LWP B, LWP A is continued, and we'll never see
2966 the SIGTRAP associated with the last time we were
2967 single-stepping LWP A. */
2969 /* Resume the thread. It should halt immediately returning the
2971 registers_changed ();
2972 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
2973 lp->step, TARGET_SIGNAL_0);
2974 if (debug_linux_nat)
2975 fprintf_unfiltered (gdb_stdlog,
2976 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2977 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2978 target_pid_to_str (lp->ptid));
2980 gdb_assert (lp->resumed);
2982 /* This should catch the pending SIGSTOP. */
2983 stop_wait_callback (lp, NULL);
2986 if (!target_can_async_p ())
2988 /* Causes SIGINT to be passed on to the attached process. */
2992 /* Translate generic target_wait options into waitpid options. */
2993 if (target_options & TARGET_WNOHANG)
3000 lwpid = my_waitpid (pid, &status, options);
3004 gdb_assert (pid == -1 || lwpid == pid);
3006 if (debug_linux_nat)
3008 fprintf_unfiltered (gdb_stdlog,
3009 "LLW: waitpid %ld received %s\n",
3010 (long) lwpid, status_to_str (status));
3013 lp = linux_nat_filter_event (lwpid, status, options);
3016 && ptid_is_pid (ptid)
3017 && ptid_get_pid (lp->ptid) != ptid_get_pid (ptid))
3019 if (debug_linux_nat)
3020 fprintf (stderr, "LWP %ld got an event %06x, leaving pending.\n",
3021 ptid_get_lwp (lp->ptid), status);
3023 if (WIFSTOPPED (status))
3025 if (WSTOPSIG (status) != SIGSTOP)
3027 lp->status = status;
3029 stop_callback (lp, NULL);
3031 /* Resume in order to collect the sigstop. */
3032 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
3034 stop_wait_callback (lp, NULL);
3042 else if (WIFEXITED (status) || WIFSIGNALED (status))
3044 if (debug_linux_nat)
3045 fprintf (stderr, "Process %ld exited while stopping LWPs\n",
3046 ptid_get_lwp (lp->ptid));
3048 /* This was the last lwp in the process. Since
3049 events are serialized to GDB core, and we can't
3050 report this one right now, but GDB core and the
3051 other target layers will want to be notified
3052 about the exit code/signal, leave the status
3053 pending for the next time we're able to report
3055 lp->status = status;
3057 /* Prevent trying to stop this thread again. We'll
3058 never try to resume it because it has a pending
3062 /* Dead LWP's aren't expected to reported a pending
3066 /* Store the pending event in the waitstatus as
3067 well, because W_EXITCODE(0,0) == 0. */
3068 store_waitstatus (&lp->waitstatus, status);
3082 /* waitpid did return something. Restart over. */
3083 options |= __WCLONE;
3091 /* Alternate between checking cloned and uncloned processes. */
3092 options ^= __WCLONE;
3094 /* And every time we have checked both:
3095 In async mode, return to event loop;
3096 In sync mode, suspend waiting for a SIGCHLD signal. */
3097 if (options & __WCLONE)
3099 if (target_options & TARGET_WNOHANG)
3101 /* No interesting event. */
3102 ourstatus->kind = TARGET_WAITKIND_IGNORE;
3104 if (debug_linux_nat_async)
3105 fprintf_unfiltered (gdb_stdlog, "LLW: exit (ignore)\n");
3107 restore_child_signals_mask (&prev_mask);
3108 return minus_one_ptid;
3111 sigsuspend (&suspend_mask);
3115 /* We shouldn't end up here unless we want to try again. */
3116 gdb_assert (lp == NULL);
3119 if (!target_can_async_p ())
3120 clear_sigint_trap ();
3124 /* Don't report signals that GDB isn't interested in, such as
3125 signals that are neither printed nor stopped upon. Stopping all
3126 threads can be a bit time-consuming so if we want decent
3127 performance with heavily multi-threaded programs, especially when
3128 they're using a high frequency timer, we'd better avoid it if we
3131 if (WIFSTOPPED (status))
3133 int signo = target_signal_from_host (WSTOPSIG (status));
3134 struct inferior *inf;
3136 inf = find_inferior_pid (ptid_get_pid (lp->ptid));
3139 /* Defer to common code if we get a signal while
3140 single-stepping, since that may need special care, e.g. to
3141 skip the signal handler, or, if we're gaining control of the
3144 && inf->stop_soon == NO_STOP_QUIETLY
3145 && signal_stop_state (signo) == 0
3146 && signal_print_state (signo) == 0
3147 && signal_pass_state (signo) == 1)
3149 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3150 here? It is not clear we should. GDB may not expect
3151 other threads to run. On the other hand, not resuming
3152 newly attached threads may cause an unwanted delay in
3153 getting them running. */
3154 registers_changed ();
3155 linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
3157 if (debug_linux_nat)
3158 fprintf_unfiltered (gdb_stdlog,
3159 "LLW: %s %s, %s (preempt 'handle')\n",
3161 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3162 target_pid_to_str (lp->ptid),
3163 signo ? strsignal (signo) : "0");
3170 /* Only do the below in all-stop, as we currently use SIGINT
3171 to implement target_stop (see linux_nat_stop) in
3173 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
3175 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3176 forwarded to the entire process group, that is, all LWPs
3177 will receive it - unless they're using CLONE_THREAD to
3178 share signals. Since we only want to report it once, we
3179 mark it as ignored for all LWPs except this one. */
3180 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid)),
3181 set_ignore_sigint, NULL);
3182 lp->ignore_sigint = 0;
3185 maybe_clear_ignore_sigint (lp);
3189 /* This LWP is stopped now. */
3192 if (debug_linux_nat)
3193 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
3194 status_to_str (status), target_pid_to_str (lp->ptid));
3198 /* Now stop all other LWP's ... */
3199 iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
3201 /* ... and wait until all of them have reported back that
3202 they're no longer running. */
3203 iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
3205 /* If we're not waiting for a specific LWP, choose an event LWP
3206 from among those that have had events. Giving equal priority
3207 to all LWPs that have had events helps prevent
3210 select_event_lwp (ptid, &lp, &status);
3213 /* Now that we've selected our final event LWP, cancel any
3214 breakpoints in other LWPs that have hit a GDB breakpoint. See
3215 the comment in cancel_breakpoints_callback to find out why. */
3216 iterate_over_lwps (minus_one_ptid, cancel_breakpoints_callback, lp);
3218 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
3220 if (debug_linux_nat)
3221 fprintf_unfiltered (gdb_stdlog,
3222 "LLW: trap ptid is %s.\n",
3223 target_pid_to_str (lp->ptid));
3226 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3228 *ourstatus = lp->waitstatus;
3229 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
3232 store_waitstatus (ourstatus, status);
3234 if (debug_linux_nat_async)
3235 fprintf_unfiltered (gdb_stdlog, "LLW: exit\n");
3237 restore_child_signals_mask (&prev_mask);
3242 linux_nat_wait (struct target_ops *ops,
3243 ptid_t ptid, struct target_waitstatus *ourstatus,
3248 if (debug_linux_nat)
3249 fprintf_unfiltered (gdb_stdlog, "linux_nat_wait: [%s]\n", target_pid_to_str (ptid));
3251 /* Flush the async file first. */
3252 if (target_can_async_p ())
3253 async_file_flush ();
3255 event_ptid = linux_nat_wait_1 (ops, ptid, ourstatus, target_options);
3257 /* If we requested any event, and something came out, assume there
3258 may be more. If we requested a specific lwp or process, also
3259 assume there may be more. */
3260 if (target_can_async_p ()
3261 && (ourstatus->kind != TARGET_WAITKIND_IGNORE
3262 || !ptid_equal (ptid, minus_one_ptid)))
3265 /* Get ready for the next event. */
3266 if (target_can_async_p ())
3267 target_async (inferior_event_handler, 0);
3273 kill_callback (struct lwp_info *lp, void *data)
3276 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
3277 if (debug_linux_nat)
3278 fprintf_unfiltered (gdb_stdlog,
3279 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3280 target_pid_to_str (lp->ptid),
3281 errno ? safe_strerror (errno) : "OK");
3287 kill_wait_callback (struct lwp_info *lp, void *data)
3291 /* We must make sure that there are no pending events (delayed
3292 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3293 program doesn't interfere with any following debugging session. */
3295 /* For cloned processes we must check both with __WCLONE and
3296 without, since the exit status of a cloned process isn't reported
3302 pid = my_waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
3303 if (pid != (pid_t) -1)
3305 if (debug_linux_nat)
3306 fprintf_unfiltered (gdb_stdlog,
3307 "KWC: wait %s received unknown.\n",
3308 target_pid_to_str (lp->ptid));
3309 /* The Linux kernel sometimes fails to kill a thread
3310 completely after PTRACE_KILL; that goes from the stop
3311 point in do_fork out to the one in
3312 get_signal_to_deliever and waits again. So kill it
3314 kill_callback (lp, NULL);
3317 while (pid == GET_LWP (lp->ptid));
3319 gdb_assert (pid == -1 && errno == ECHILD);
3324 pid = my_waitpid (GET_LWP (lp->ptid), NULL, 0);
3325 if (pid != (pid_t) -1)
3327 if (debug_linux_nat)
3328 fprintf_unfiltered (gdb_stdlog,
3329 "KWC: wait %s received unk.\n",
3330 target_pid_to_str (lp->ptid));
3331 /* See the call to kill_callback above. */
3332 kill_callback (lp, NULL);
3335 while (pid == GET_LWP (lp->ptid));
3337 gdb_assert (pid == -1 && errno == ECHILD);
3342 linux_nat_kill (struct target_ops *ops)
3344 struct target_waitstatus last;
3348 /* If we're stopped while forking and we haven't followed yet,
3349 kill the other task. We need to do this first because the
3350 parent will be sleeping if this is a vfork. */
3352 get_last_target_status (&last_ptid, &last);
3354 if (last.kind == TARGET_WAITKIND_FORKED
3355 || last.kind == TARGET_WAITKIND_VFORKED)
3357 ptrace (PT_KILL, PIDGET (last.value.related_pid), 0, 0);
3361 if (forks_exist_p ())
3362 linux_fork_killall ();
3365 ptid_t ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
3366 /* Stop all threads before killing them, since ptrace requires
3367 that the thread is stopped to sucessfully PTRACE_KILL. */
3368 iterate_over_lwps (ptid, stop_callback, NULL);
3369 /* ... and wait until all of them have reported back that
3370 they're no longer running. */
3371 iterate_over_lwps (ptid, stop_wait_callback, NULL);
3373 /* Kill all LWP's ... */
3374 iterate_over_lwps (ptid, kill_callback, NULL);
3376 /* ... and wait until we've flushed all events. */
3377 iterate_over_lwps (ptid, kill_wait_callback, NULL);
3380 target_mourn_inferior ();
3384 linux_nat_mourn_inferior (struct target_ops *ops)
3386 purge_lwp_list (ptid_get_pid (inferior_ptid));
3388 if (! forks_exist_p ())
3389 /* Normal case, no other forks available. */
3390 linux_ops->to_mourn_inferior (ops);
3392 /* Multi-fork case. The current inferior_ptid has exited, but
3393 there are other viable forks to debug. Delete the exiting
3394 one and context-switch to the first available. */
3395 linux_fork_mourn_inferior ();
3398 /* Convert a native/host siginfo object, into/from the siginfo in the
3399 layout of the inferiors' architecture. */
3402 siginfo_fixup (struct siginfo *siginfo, gdb_byte *inf_siginfo, int direction)
3406 if (linux_nat_siginfo_fixup != NULL)
3407 done = linux_nat_siginfo_fixup (siginfo, inf_siginfo, direction);
3409 /* If there was no callback, or the callback didn't do anything,
3410 then just do a straight memcpy. */
3414 memcpy (siginfo, inf_siginfo, sizeof (struct siginfo));
3416 memcpy (inf_siginfo, siginfo, sizeof (struct siginfo));
3421 linux_xfer_siginfo (struct target_ops *ops, enum target_object object,
3422 const char *annex, gdb_byte *readbuf,
3423 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
3426 struct siginfo siginfo;
3427 gdb_byte inf_siginfo[sizeof (struct siginfo)];
3429 gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO);
3430 gdb_assert (readbuf || writebuf);
3432 pid = GET_LWP (inferior_ptid);
3434 pid = GET_PID (inferior_ptid);
3436 if (offset > sizeof (siginfo))
3440 ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
3444 /* When GDB is built as a 64-bit application, ptrace writes into
3445 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3446 inferior with a 64-bit GDB should look the same as debugging it
3447 with a 32-bit GDB, we need to convert it. GDB core always sees
3448 the converted layout, so any read/write will have to be done
3450 siginfo_fixup (&siginfo, inf_siginfo, 0);
3452 if (offset + len > sizeof (siginfo))
3453 len = sizeof (siginfo) - offset;
3455 if (readbuf != NULL)
3456 memcpy (readbuf, inf_siginfo + offset, len);
3459 memcpy (inf_siginfo + offset, writebuf, len);
3461 /* Convert back to ptrace layout before flushing it out. */
3462 siginfo_fixup (&siginfo, inf_siginfo, 1);
3465 ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
3474 linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
3475 const char *annex, gdb_byte *readbuf,
3476 const gdb_byte *writebuf,
3477 ULONGEST offset, LONGEST len)
3479 struct cleanup *old_chain;
3482 if (object == TARGET_OBJECT_SIGNAL_INFO)
3483 return linux_xfer_siginfo (ops, object, annex, readbuf, writebuf,
3486 /* The target is connected but no live inferior is selected. Pass
3487 this request down to a lower stratum (e.g., the executable
3489 if (object == TARGET_OBJECT_MEMORY && ptid_equal (inferior_ptid, null_ptid))
3492 old_chain = save_inferior_ptid ();
3494 if (is_lwp (inferior_ptid))
3495 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
3497 xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
3500 do_cleanups (old_chain);
3505 linux_thread_alive (ptid_t ptid)
3509 gdb_assert (is_lwp (ptid));
3511 /* Send signal 0 instead of anything ptrace, because ptracing a
3512 running thread errors out claiming that the thread doesn't
3514 err = kill_lwp (GET_LWP (ptid), 0);
3516 if (debug_linux_nat)
3517 fprintf_unfiltered (gdb_stdlog,
3518 "LLTA: KILL(SIG0) %s (%s)\n",
3519 target_pid_to_str (ptid),
3520 err ? safe_strerror (err) : "OK");
3529 linux_nat_thread_alive (struct target_ops *ops, ptid_t ptid)
3531 return linux_thread_alive (ptid);
3535 linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
3537 static char buf[64];
3540 && (GET_PID (ptid) != GET_LWP (ptid)
3541 || num_lwps (GET_PID (ptid)) > 1))
3543 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
3547 return normal_pid_to_str (ptid);
3550 /* Accepts an integer PID; Returns a string representing a file that
3551 can be opened to get the symbols for the child process. */
3554 linux_child_pid_to_exec_file (int pid)
3556 char *name1, *name2;
3558 name1 = xmalloc (MAXPATHLEN);
3559 name2 = xmalloc (MAXPATHLEN);
3560 make_cleanup (xfree, name1);
3561 make_cleanup (xfree, name2);
3562 memset (name2, 0, MAXPATHLEN);
3564 sprintf (name1, "/proc/%d/exe", pid);
3565 if (readlink (name1, name2, MAXPATHLEN) > 0)
3571 /* Service function for corefiles and info proc. */
3574 read_mapping (FILE *mapfile,
3579 char *device, long long *inode, char *filename)
3581 int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx",
3582 addr, endaddr, permissions, offset, device, inode);
3585 if (ret > 0 && ret != EOF)
3587 /* Eat everything up to EOL for the filename. This will prevent
3588 weird filenames (such as one with embedded whitespace) from
3589 confusing this code. It also makes this code more robust in
3590 respect to annotations the kernel may add after the filename.
3592 Note the filename is used for informational purposes
3594 ret += fscanf (mapfile, "%[^\n]\n", filename);
3597 return (ret != 0 && ret != EOF);
3600 /* Fills the "to_find_memory_regions" target vector. Lists the memory
3601 regions in the inferior for a corefile. */
3604 linux_nat_find_memory_regions (int (*func) (CORE_ADDR,
3606 int, int, int, void *), void *obfd)
3608 int pid = PIDGET (inferior_ptid);
3609 char mapsfilename[MAXPATHLEN];
3611 long long addr, endaddr, size, offset, inode;
3612 char permissions[8], device[8], filename[MAXPATHLEN];
3613 int read, write, exec;
3615 struct cleanup *cleanup;
3617 /* Compose the filename for the /proc memory map, and open it. */
3618 sprintf (mapsfilename, "/proc/%d/maps", pid);
3619 if ((mapsfile = fopen (mapsfilename, "r")) == NULL)
3620 error (_("Could not open %s."), mapsfilename);
3621 cleanup = make_cleanup_fclose (mapsfile);
3624 fprintf_filtered (gdb_stdout,
3625 "Reading memory regions from %s\n", mapsfilename);
3627 /* Now iterate until end-of-file. */
3628 while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0],
3629 &offset, &device[0], &inode, &filename[0]))
3631 size = endaddr - addr;
3633 /* Get the segment's permissions. */
3634 read = (strchr (permissions, 'r') != 0);
3635 write = (strchr (permissions, 'w') != 0);
3636 exec = (strchr (permissions, 'x') != 0);
3640 fprintf_filtered (gdb_stdout,
3641 "Save segment, %lld bytes at %s (%c%c%c)",
3642 size, paddress (target_gdbarch, addr),
3644 write ? 'w' : ' ', exec ? 'x' : ' ');
3646 fprintf_filtered (gdb_stdout, " for %s", filename);
3647 fprintf_filtered (gdb_stdout, "\n");
3650 /* Invoke the callback function to create the corefile
3652 func (addr, size, read, write, exec, obfd);
3654 do_cleanups (cleanup);
3659 find_signalled_thread (struct thread_info *info, void *data)
3661 if (info->stop_signal != TARGET_SIGNAL_0
3662 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
3668 static enum target_signal
3669 find_stop_signal (void)
3671 struct thread_info *info =
3672 iterate_over_threads (find_signalled_thread, NULL);
3675 return info->stop_signal;
3677 return TARGET_SIGNAL_0;
3680 /* Records the thread's register state for the corefile note
3684 linux_nat_do_thread_registers (bfd *obfd, ptid_t ptid,
3685 char *note_data, int *note_size,
3686 enum target_signal stop_signal)
3688 gdb_gregset_t gregs;
3689 gdb_fpregset_t fpregs;
3690 unsigned long lwp = ptid_get_lwp (ptid);
3691 struct gdbarch *gdbarch = target_gdbarch;
3692 struct regcache *regcache = get_thread_arch_regcache (ptid, gdbarch);
3693 const struct regset *regset;
3695 struct cleanup *old_chain;
3696 struct core_regset_section *sect_list;
3699 old_chain = save_inferior_ptid ();
3700 inferior_ptid = ptid;
3701 target_fetch_registers (regcache, -1);
3702 do_cleanups (old_chain);
3704 core_regset_p = gdbarch_regset_from_core_section_p (gdbarch);
3705 sect_list = gdbarch_core_regset_sections (gdbarch);
3708 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg",
3709 sizeof (gregs))) != NULL
3710 && regset->collect_regset != NULL)
3711 regset->collect_regset (regset, regcache, -1,
3712 &gregs, sizeof (gregs));
3714 fill_gregset (regcache, &gregs, -1);
3716 note_data = (char *) elfcore_write_prstatus (obfd,
3720 stop_signal, &gregs);
3722 /* The loop below uses the new struct core_regset_section, which stores
3723 the supported section names and sizes for the core file. Note that
3724 note PRSTATUS needs to be treated specially. But the other notes are
3725 structurally the same, so they can benefit from the new struct. */
3726 if (core_regset_p && sect_list != NULL)
3727 while (sect_list->sect_name != NULL)
3729 /* .reg was already handled above. */
3730 if (strcmp (sect_list->sect_name, ".reg") == 0)
3735 regset = gdbarch_regset_from_core_section (gdbarch,
3736 sect_list->sect_name,
3738 gdb_assert (regset && regset->collect_regset);
3739 gdb_regset = xmalloc (sect_list->size);
3740 regset->collect_regset (regset, regcache, -1,
3741 gdb_regset, sect_list->size);
3742 note_data = (char *) elfcore_write_register_note (obfd,
3745 sect_list->sect_name,
3752 /* For architectures that does not have the struct core_regset_section
3753 implemented, we use the old method. When all the architectures have
3754 the new support, the code below should be deleted. */
3758 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg2",
3759 sizeof (fpregs))) != NULL
3760 && regset->collect_regset != NULL)
3761 regset->collect_regset (regset, regcache, -1,
3762 &fpregs, sizeof (fpregs));
3764 fill_fpregset (regcache, &fpregs, -1);
3766 note_data = (char *) elfcore_write_prfpreg (obfd,
3769 &fpregs, sizeof (fpregs));
3775 struct linux_nat_corefile_thread_data
3781 enum target_signal stop_signal;
3784 /* Called by gdbthread.c once per thread. Records the thread's
3785 register state for the corefile note section. */
3788 linux_nat_corefile_thread_callback (struct lwp_info *ti, void *data)
3790 struct linux_nat_corefile_thread_data *args = data;
3792 args->note_data = linux_nat_do_thread_registers (args->obfd,
3802 /* Enumerate spufs IDs for process PID. */
3805 iterate_over_spus (int pid, void (*callback) (void *, int), void *data)
3809 struct dirent *entry;
3811 xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
3812 dir = opendir (path);
3817 while ((entry = readdir (dir)) != NULL)
3823 fd = atoi (entry->d_name);
3827 xsnprintf (path, sizeof path, "/proc/%d/fd/%d", pid, fd);
3828 if (stat (path, &st) != 0)
3830 if (!S_ISDIR (st.st_mode))
3833 if (statfs (path, &stfs) != 0)
3835 if (stfs.f_type != SPUFS_MAGIC)
3838 callback (data, fd);
3844 /* Generate corefile notes for SPU contexts. */
3846 struct linux_spu_corefile_data
3854 linux_spu_corefile_callback (void *data, int fd)
3856 struct linux_spu_corefile_data *args = data;
3859 static const char *spu_files[] =
3881 for (i = 0; i < sizeof (spu_files) / sizeof (spu_files[0]); i++)
3883 char annex[32], note_name[32];
3887 xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[i]);
3888 spu_len = target_read_alloc (¤t_target, TARGET_OBJECT_SPU,
3892 xsnprintf (note_name, sizeof note_name, "SPU/%s", annex);
3893 args->note_data = elfcore_write_note (args->obfd, args->note_data,
3894 args->note_size, note_name,
3895 NT_SPU, spu_data, spu_len);
3902 linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size)
3904 struct linux_spu_corefile_data args;
3906 args.note_data = note_data;
3907 args.note_size = note_size;
3909 iterate_over_spus (PIDGET (inferior_ptid),
3910 linux_spu_corefile_callback, &args);
3912 return args.note_data;
3915 /* Fills the "to_make_corefile_note" target vector. Builds the note
3916 section for a corefile, and returns it in a malloc buffer. */
3919 linux_nat_make_corefile_notes (bfd *obfd, int *note_size)
3921 struct linux_nat_corefile_thread_data thread_args;
3922 struct cleanup *old_chain;
3923 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
3924 char fname[16] = { '\0' };
3925 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
3926 char psargs[80] = { '\0' };
3927 char *note_data = NULL;
3928 ptid_t current_ptid = inferior_ptid;
3929 ptid_t filter = pid_to_ptid (ptid_get_pid (inferior_ptid));
3933 if (get_exec_file (0))
3935 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
3936 strncpy (psargs, get_exec_file (0), sizeof (psargs));
3937 if (get_inferior_args ())
3940 char *psargs_end = psargs + sizeof (psargs);
3942 /* linux_elfcore_write_prpsinfo () handles zero unterminated
3944 string_end = memchr (psargs, 0, sizeof (psargs));
3945 if (string_end != NULL)
3947 *string_end++ = ' ';
3948 strncpy (string_end, get_inferior_args (),
3949 psargs_end - string_end);
3952 note_data = (char *) elfcore_write_prpsinfo (obfd,
3954 note_size, fname, psargs);
3957 /* Dump information for threads. */
3958 thread_args.obfd = obfd;
3959 thread_args.note_data = note_data;
3960 thread_args.note_size = note_size;
3961 thread_args.num_notes = 0;
3962 thread_args.stop_signal = find_stop_signal ();
3963 iterate_over_lwps (filter, linux_nat_corefile_thread_callback, &thread_args);
3964 gdb_assert (thread_args.num_notes != 0);
3965 note_data = thread_args.note_data;
3967 auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV,
3971 note_data = elfcore_write_note (obfd, note_data, note_size,
3972 "CORE", NT_AUXV, auxv, auxv_len);
3976 note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size);
3978 make_cleanup (xfree, note_data);
3982 /* Implement the "info proc" command. */
3985 linux_nat_info_proc_cmd (char *args, int from_tty)
3987 /* A long is used for pid instead of an int to avoid a loss of precision
3988 compiler warning from the output of strtoul. */
3989 long pid = PIDGET (inferior_ptid);
3992 char buffer[MAXPATHLEN];
3993 char fname1[MAXPATHLEN], fname2[MAXPATHLEN];
4006 /* Break up 'args' into an argv array. */
4007 argv = gdb_buildargv (args);
4008 make_cleanup_freeargv (argv);
4010 while (argv != NULL && *argv != NULL)
4012 if (isdigit (argv[0][0]))
4014 pid = strtoul (argv[0], NULL, 10);
4016 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
4020 else if (strcmp (argv[0], "status") == 0)
4024 else if (strcmp (argv[0], "stat") == 0)
4028 else if (strcmp (argv[0], "cmd") == 0)
4032 else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0)
4036 else if (strcmp (argv[0], "cwd") == 0)
4040 else if (strncmp (argv[0], "all", strlen (argv[0])) == 0)
4046 /* [...] (future options here) */
4051 error (_("No current process: you must name one."));
4053 sprintf (fname1, "/proc/%ld", pid);
4054 if (stat (fname1, &dummy) != 0)
4055 error (_("No /proc directory: '%s'"), fname1);
4057 printf_filtered (_("process %ld\n"), pid);
4058 if (cmdline_f || all)
4060 sprintf (fname1, "/proc/%ld/cmdline", pid);
4061 if ((procfile = fopen (fname1, "r")) != NULL)
4063 struct cleanup *cleanup = make_cleanup_fclose (procfile);
4064 if (fgets (buffer, sizeof (buffer), procfile))
4065 printf_filtered ("cmdline = '%s'\n", buffer);
4067 warning (_("unable to read '%s'"), fname1);
4068 do_cleanups (cleanup);
4071 warning (_("unable to open /proc file '%s'"), fname1);
4075 sprintf (fname1, "/proc/%ld/cwd", pid);
4076 memset (fname2, 0, sizeof (fname2));
4077 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
4078 printf_filtered ("cwd = '%s'\n", fname2);
4080 warning (_("unable to read link '%s'"), fname1);
4084 sprintf (fname1, "/proc/%ld/exe", pid);
4085 memset (fname2, 0, sizeof (fname2));
4086 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
4087 printf_filtered ("exe = '%s'\n", fname2);
4089 warning (_("unable to read link '%s'"), fname1);
4091 if (mappings_f || all)
4093 sprintf (fname1, "/proc/%ld/maps", pid);
4094 if ((procfile = fopen (fname1, "r")) != NULL)
4096 long long addr, endaddr, size, offset, inode;
4097 char permissions[8], device[8], filename[MAXPATHLEN];
4098 struct cleanup *cleanup;
4100 cleanup = make_cleanup_fclose (procfile);
4101 printf_filtered (_("Mapped address spaces:\n\n"));
4102 if (gdbarch_addr_bit (target_gdbarch) == 32)
4104 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
4107 " Size", " Offset", "objfile");
4111 printf_filtered (" %18s %18s %10s %10s %7s\n",
4114 " Size", " Offset", "objfile");
4117 while (read_mapping (procfile, &addr, &endaddr, &permissions[0],
4118 &offset, &device[0], &inode, &filename[0]))
4120 size = endaddr - addr;
4122 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
4123 calls here (and possibly above) should be abstracted
4124 out into their own functions? Andrew suggests using
4125 a generic local_address_string instead to print out
4126 the addresses; that makes sense to me, too. */
4128 if (gdbarch_addr_bit (target_gdbarch) == 32)
4130 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
4131 (unsigned long) addr, /* FIXME: pr_addr */
4132 (unsigned long) endaddr,
4134 (unsigned int) offset,
4135 filename[0] ? filename : "");
4139 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
4140 (unsigned long) addr, /* FIXME: pr_addr */
4141 (unsigned long) endaddr,
4143 (unsigned int) offset,
4144 filename[0] ? filename : "");
4148 do_cleanups (cleanup);
4151 warning (_("unable to open /proc file '%s'"), fname1);
4153 if (status_f || all)
4155 sprintf (fname1, "/proc/%ld/status", pid);
4156 if ((procfile = fopen (fname1, "r")) != NULL)
4158 struct cleanup *cleanup = make_cleanup_fclose (procfile);
4159 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
4160 puts_filtered (buffer);
4161 do_cleanups (cleanup);
4164 warning (_("unable to open /proc file '%s'"), fname1);
4168 sprintf (fname1, "/proc/%ld/stat", pid);
4169 if ((procfile = fopen (fname1, "r")) != NULL)
4174 struct cleanup *cleanup = make_cleanup_fclose (procfile);
4176 if (fscanf (procfile, "%d ", &itmp) > 0)
4177 printf_filtered (_("Process: %d\n"), itmp);
4178 if (fscanf (procfile, "(%[^)]) ", &buffer[0]) > 0)
4179 printf_filtered (_("Exec file: %s\n"), buffer);
4180 if (fscanf (procfile, "%c ", &ctmp) > 0)
4181 printf_filtered (_("State: %c\n"), ctmp);
4182 if (fscanf (procfile, "%d ", &itmp) > 0)
4183 printf_filtered (_("Parent process: %d\n"), itmp);
4184 if (fscanf (procfile, "%d ", &itmp) > 0)
4185 printf_filtered (_("Process group: %d\n"), itmp);
4186 if (fscanf (procfile, "%d ", &itmp) > 0)
4187 printf_filtered (_("Session id: %d\n"), itmp);
4188 if (fscanf (procfile, "%d ", &itmp) > 0)
4189 printf_filtered (_("TTY: %d\n"), itmp);
4190 if (fscanf (procfile, "%d ", &itmp) > 0)
4191 printf_filtered (_("TTY owner process group: %d\n"), itmp);
4192 if (fscanf (procfile, "%lu ", <mp) > 0)
4193 printf_filtered (_("Flags: 0x%lx\n"), ltmp);
4194 if (fscanf (procfile, "%lu ", <mp) > 0)
4195 printf_filtered (_("Minor faults (no memory page): %lu\n"),
4196 (unsigned long) ltmp);
4197 if (fscanf (procfile, "%lu ", <mp) > 0)
4198 printf_filtered (_("Minor faults, children: %lu\n"),
4199 (unsigned long) ltmp);
4200 if (fscanf (procfile, "%lu ", <mp) > 0)
4201 printf_filtered (_("Major faults (memory page faults): %lu\n"),
4202 (unsigned long) ltmp);
4203 if (fscanf (procfile, "%lu ", <mp) > 0)
4204 printf_filtered (_("Major faults, children: %lu\n"),
4205 (unsigned long) ltmp);
4206 if (fscanf (procfile, "%ld ", <mp) > 0)
4207 printf_filtered (_("utime: %ld\n"), ltmp);
4208 if (fscanf (procfile, "%ld ", <mp) > 0)
4209 printf_filtered (_("stime: %ld\n"), ltmp);
4210 if (fscanf (procfile, "%ld ", <mp) > 0)
4211 printf_filtered (_("utime, children: %ld\n"), ltmp);
4212 if (fscanf (procfile, "%ld ", <mp) > 0)
4213 printf_filtered (_("stime, children: %ld\n"), ltmp);
4214 if (fscanf (procfile, "%ld ", <mp) > 0)
4215 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
4217 if (fscanf (procfile, "%ld ", <mp) > 0)
4218 printf_filtered (_("'nice' value: %ld\n"), ltmp);
4219 if (fscanf (procfile, "%lu ", <mp) > 0)
4220 printf_filtered (_("jiffies until next timeout: %lu\n"),
4221 (unsigned long) ltmp);
4222 if (fscanf (procfile, "%lu ", <mp) > 0)
4223 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
4224 (unsigned long) ltmp);
4225 if (fscanf (procfile, "%ld ", <mp) > 0)
4226 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
4228 if (fscanf (procfile, "%lu ", <mp) > 0)
4229 printf_filtered (_("Virtual memory size: %lu\n"),
4230 (unsigned long) ltmp);
4231 if (fscanf (procfile, "%lu ", <mp) > 0)
4232 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp);
4233 if (fscanf (procfile, "%lu ", <mp) > 0)
4234 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp);
4235 if (fscanf (procfile, "%lu ", <mp) > 0)
4236 printf_filtered (_("Start of text: 0x%lx\n"), ltmp);
4237 if (fscanf (procfile, "%lu ", <mp) > 0)
4238 printf_filtered (_("End of text: 0x%lx\n"), ltmp);
4239 if (fscanf (procfile, "%lu ", <mp) > 0)
4240 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp);
4241 #if 0 /* Don't know how architecture-dependent the rest is...
4242 Anyway the signal bitmap info is available from "status". */
4243 if (fscanf (procfile, "%lu ", <mp) > 0) /* FIXME arch? */
4244 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp);
4245 if (fscanf (procfile, "%lu ", <mp) > 0) /* FIXME arch? */
4246 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp);
4247 if (fscanf (procfile, "%ld ", <mp) > 0)
4248 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp);
4249 if (fscanf (procfile, "%ld ", <mp) > 0)
4250 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp);
4251 if (fscanf (procfile, "%ld ", <mp) > 0)
4252 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp);
4253 if (fscanf (procfile, "%ld ", <mp) > 0)
4254 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp);
4255 if (fscanf (procfile, "%lu ", <mp) > 0) /* FIXME arch? */
4256 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp);
4258 do_cleanups (cleanup);
4261 warning (_("unable to open /proc file '%s'"), fname1);
4265 /* Implement the to_xfer_partial interface for memory reads using the /proc
4266 filesystem. Because we can use a single read() call for /proc, this
4267 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4268 but it doesn't support writes. */
4271 linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
4272 const char *annex, gdb_byte *readbuf,
4273 const gdb_byte *writebuf,
4274 ULONGEST offset, LONGEST len)
4280 if (object != TARGET_OBJECT_MEMORY || !readbuf)
4283 /* Don't bother for one word. */
4284 if (len < 3 * sizeof (long))
4287 /* We could keep this file open and cache it - possibly one per
4288 thread. That requires some juggling, but is even faster. */
4289 sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid));
4290 fd = open (filename, O_RDONLY | O_LARGEFILE);
4294 /* If pread64 is available, use it. It's faster if the kernel
4295 supports it (only one syscall), and it's 64-bit safe even on
4296 32-bit platforms (for instance, SPARC debugging a SPARC64
4299 if (pread64 (fd, readbuf, len, offset) != len)
4301 if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
4312 /* Enumerate spufs IDs for process PID. */
4314 spu_enumerate_spu_ids (int pid, gdb_byte *buf, ULONGEST offset, LONGEST len)
4316 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
4318 LONGEST written = 0;
4321 struct dirent *entry;
4323 xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
4324 dir = opendir (path);
4329 while ((entry = readdir (dir)) != NULL)
4335 fd = atoi (entry->d_name);
4339 xsnprintf (path, sizeof path, "/proc/%d/fd/%d", pid, fd);
4340 if (stat (path, &st) != 0)
4342 if (!S_ISDIR (st.st_mode))
4345 if (statfs (path, &stfs) != 0)
4347 if (stfs.f_type != SPUFS_MAGIC)
4350 if (pos >= offset && pos + 4 <= offset + len)
4352 store_unsigned_integer (buf + pos - offset, 4, byte_order, fd);
4362 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4363 object type, using the /proc file system. */
4365 linux_proc_xfer_spu (struct target_ops *ops, enum target_object object,
4366 const char *annex, gdb_byte *readbuf,
4367 const gdb_byte *writebuf,
4368 ULONGEST offset, LONGEST len)
4373 int pid = PIDGET (inferior_ptid);
4380 return spu_enumerate_spu_ids (pid, readbuf, offset, len);
4383 xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
4384 fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
4389 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
4396 ret = write (fd, writebuf, (size_t) len);
4398 ret = read (fd, readbuf, (size_t) len);
4405 /* Parse LINE as a signal set and add its set bits to SIGS. */
4408 add_line_to_sigset (const char *line, sigset_t *sigs)
4410 int len = strlen (line) - 1;
4414 if (line[len] != '\n')
4415 error (_("Could not parse signal set: %s"), line);
4423 if (*p >= '0' && *p <= '9')
4425 else if (*p >= 'a' && *p <= 'f')
4426 digit = *p - 'a' + 10;
4428 error (_("Could not parse signal set: %s"), line);
4433 sigaddset (sigs, signum + 1);
4435 sigaddset (sigs, signum + 2);
4437 sigaddset (sigs, signum + 3);
4439 sigaddset (sigs, signum + 4);
4445 /* Find process PID's pending signals from /proc/pid/status and set
4449 linux_proc_pending_signals (int pid, sigset_t *pending, sigset_t *blocked, sigset_t *ignored)
4452 char buffer[MAXPATHLEN], fname[MAXPATHLEN];
4454 struct cleanup *cleanup;
4456 sigemptyset (pending);
4457 sigemptyset (blocked);
4458 sigemptyset (ignored);
4459 sprintf (fname, "/proc/%d/status", pid);
4460 procfile = fopen (fname, "r");
4461 if (procfile == NULL)
4462 error (_("Could not open %s"), fname);
4463 cleanup = make_cleanup_fclose (procfile);
4465 while (fgets (buffer, MAXPATHLEN, procfile) != NULL)
4467 /* Normal queued signals are on the SigPnd line in the status
4468 file. However, 2.6 kernels also have a "shared" pending
4469 queue for delivering signals to a thread group, so check for
4472 Unfortunately some Red Hat kernels include the shared pending
4473 queue but not the ShdPnd status field. */
4475 if (strncmp (buffer, "SigPnd:\t", 8) == 0)
4476 add_line_to_sigset (buffer + 8, pending);
4477 else if (strncmp (buffer, "ShdPnd:\t", 8) == 0)
4478 add_line_to_sigset (buffer + 8, pending);
4479 else if (strncmp (buffer, "SigBlk:\t", 8) == 0)
4480 add_line_to_sigset (buffer + 8, blocked);
4481 else if (strncmp (buffer, "SigIgn:\t", 8) == 0)
4482 add_line_to_sigset (buffer + 8, ignored);
4485 do_cleanups (cleanup);
4489 linux_nat_xfer_osdata (struct target_ops *ops, enum target_object object,
4490 const char *annex, gdb_byte *readbuf,
4491 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4493 /* We make the process list snapshot when the object starts to be
4495 static const char *buf;
4496 static LONGEST len_avail = -1;
4497 static struct obstack obstack;
4501 gdb_assert (object == TARGET_OBJECT_OSDATA);
4503 if (strcmp (annex, "processes") != 0)
4506 gdb_assert (readbuf && !writebuf);
4510 if (len_avail != -1 && len_avail != 0)
4511 obstack_free (&obstack, NULL);
4514 obstack_init (&obstack);
4515 obstack_grow_str (&obstack, "<osdata type=\"processes\">\n");
4517 dirp = opendir ("/proc");
4521 while ((dp = readdir (dirp)) != NULL)
4523 struct stat statbuf;
4524 char procentry[sizeof ("/proc/4294967295")];
4526 if (!isdigit (dp->d_name[0])
4527 || NAMELEN (dp) > sizeof ("4294967295") - 1)
4530 sprintf (procentry, "/proc/%s", dp->d_name);
4531 if (stat (procentry, &statbuf) == 0
4532 && S_ISDIR (statbuf.st_mode))
4536 char cmd[MAXPATHLEN + 1];
4537 struct passwd *entry;
4539 pathname = xstrprintf ("/proc/%s/cmdline", dp->d_name);
4540 entry = getpwuid (statbuf.st_uid);
4542 if ((f = fopen (pathname, "r")) != NULL)
4544 size_t len = fread (cmd, 1, sizeof (cmd) - 1, f);
4548 for (i = 0; i < len; i++)
4553 obstack_xml_printf (
4556 "<column name=\"pid\">%s</column>"
4557 "<column name=\"user\">%s</column>"
4558 "<column name=\"command\">%s</column>"
4561 entry ? entry->pw_name : "?",
4574 obstack_grow_str0 (&obstack, "</osdata>\n");
4575 buf = obstack_finish (&obstack);
4576 len_avail = strlen (buf);
4579 if (offset >= len_avail)
4581 /* Done. Get rid of the obstack. */
4582 obstack_free (&obstack, NULL);
4588 if (len > len_avail - offset)
4589 len = len_avail - offset;
4590 memcpy (readbuf, buf + offset, len);
4596 linux_xfer_partial (struct target_ops *ops, enum target_object object,
4597 const char *annex, gdb_byte *readbuf,
4598 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4602 if (object == TARGET_OBJECT_AUXV)
4603 return procfs_xfer_auxv (ops, object, annex, readbuf, writebuf,
4606 if (object == TARGET_OBJECT_OSDATA)
4607 return linux_nat_xfer_osdata (ops, object, annex, readbuf, writebuf,
4610 if (object == TARGET_OBJECT_SPU)
4611 return linux_proc_xfer_spu (ops, object, annex, readbuf, writebuf,
4614 /* GDB calculates all the addresses in possibly larget width of the address.
4615 Address width needs to be masked before its final use - either by
4616 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4618 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4620 if (object == TARGET_OBJECT_MEMORY)
4622 int addr_bit = gdbarch_addr_bit (target_gdbarch);
4624 if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
4625 offset &= ((ULONGEST) 1 << addr_bit) - 1;
4628 xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
4633 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
4637 /* Create a prototype generic GNU/Linux target. The client can override
4638 it with local methods. */
4641 linux_target_install_ops (struct target_ops *t)
4643 t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
4644 t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
4645 t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
4646 t->to_set_syscall_catchpoint = linux_child_set_syscall_catchpoint;
4647 t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
4648 t->to_post_startup_inferior = linux_child_post_startup_inferior;
4649 t->to_post_attach = linux_child_post_attach;
4650 t->to_follow_fork = linux_child_follow_fork;
4651 t->to_find_memory_regions = linux_nat_find_memory_regions;
4652 t->to_make_corefile_notes = linux_nat_make_corefile_notes;
4654 super_xfer_partial = t->to_xfer_partial;
4655 t->to_xfer_partial = linux_xfer_partial;
4661 struct target_ops *t;
4663 t = inf_ptrace_target ();
4664 linux_target_install_ops (t);
4670 linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
4672 struct target_ops *t;
4674 t = inf_ptrace_trad_target (register_u_offset);
4675 linux_target_install_ops (t);
4680 /* target_is_async_p implementation. */
4683 linux_nat_is_async_p (void)
4685 /* NOTE: palves 2008-03-21: We're only async when the user requests
4686 it explicitly with the "set target-async" command.
4687 Someday, linux will always be async. */
4688 if (!target_async_permitted)
4691 /* See target.h/target_async_mask. */
4692 return linux_nat_async_mask_value;
4695 /* target_can_async_p implementation. */
4698 linux_nat_can_async_p (void)
4700 /* NOTE: palves 2008-03-21: We're only async when the user requests
4701 it explicitly with the "set target-async" command.
4702 Someday, linux will always be async. */
4703 if (!target_async_permitted)
4706 /* See target.h/target_async_mask. */
4707 return linux_nat_async_mask_value;
4711 linux_nat_supports_non_stop (void)
4716 /* True if we want to support multi-process. To be removed when GDB
4717 supports multi-exec. */
4719 int linux_multi_process = 1;
4722 linux_nat_supports_multi_process (void)
4724 return linux_multi_process;
4727 /* target_async_mask implementation. */
4730 linux_nat_async_mask (int new_mask)
4732 int curr_mask = linux_nat_async_mask_value;
4734 if (curr_mask != new_mask)
4738 linux_nat_async (NULL, 0);
4739 linux_nat_async_mask_value = new_mask;
4743 linux_nat_async_mask_value = new_mask;
4745 /* If we're going out of async-mask in all-stop, then the
4746 inferior is stopped. The next resume will call
4747 target_async. In non-stop, the target event source
4748 should be always registered in the event loop. Do so
4751 linux_nat_async (inferior_event_handler, 0);
4758 static int async_terminal_is_ours = 1;
4760 /* target_terminal_inferior implementation. */
4763 linux_nat_terminal_inferior (void)
4765 if (!target_is_async_p ())
4767 /* Async mode is disabled. */
4768 terminal_inferior ();
4772 terminal_inferior ();
4774 /* Calls to target_terminal_*() are meant to be idempotent. */
4775 if (!async_terminal_is_ours)
4778 delete_file_handler (input_fd);
4779 async_terminal_is_ours = 0;
4783 /* target_terminal_ours implementation. */
4786 linux_nat_terminal_ours (void)
4788 if (!target_is_async_p ())
4790 /* Async mode is disabled. */
4795 /* GDB should never give the terminal to the inferior if the
4796 inferior is running in the background (run&, continue&, etc.),
4797 but claiming it sure should. */
4800 if (async_terminal_is_ours)
4803 clear_sigint_trap ();
4804 add_file_handler (input_fd, stdin_event_handler, 0);
4805 async_terminal_is_ours = 1;
4808 static void (*async_client_callback) (enum inferior_event_type event_type,
4810 static void *async_client_context;
4812 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4813 so we notice when any child changes state, and notify the
4814 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4815 above to wait for the arrival of a SIGCHLD. */
4818 sigchld_handler (int signo)
4820 int old_errno = errno;
4822 if (debug_linux_nat_async)
4823 fprintf_unfiltered (gdb_stdlog, "sigchld\n");
4825 if (signo == SIGCHLD
4826 && linux_nat_event_pipe[0] != -1)
4827 async_file_mark (); /* Let the event loop know that there are
4828 events to handle. */
4833 /* Callback registered with the target events file descriptor. */
4836 handle_target_event (int error, gdb_client_data client_data)
4838 (*async_client_callback) (INF_REG_EVENT, async_client_context);
4841 /* Create/destroy the target events pipe. Returns previous state. */
4844 linux_async_pipe (int enable)
4846 int previous = (linux_nat_event_pipe[0] != -1);
4848 if (previous != enable)
4852 block_child_signals (&prev_mask);
4856 if (pipe (linux_nat_event_pipe) == -1)
4857 internal_error (__FILE__, __LINE__,
4858 "creating event pipe failed.");
4860 fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
4861 fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
4865 close (linux_nat_event_pipe[0]);
4866 close (linux_nat_event_pipe[1]);
4867 linux_nat_event_pipe[0] = -1;
4868 linux_nat_event_pipe[1] = -1;
4871 restore_child_signals_mask (&prev_mask);
4877 /* target_async implementation. */
4880 linux_nat_async (void (*callback) (enum inferior_event_type event_type,
4881 void *context), void *context)
4883 if (linux_nat_async_mask_value == 0 || !target_async_permitted)
4884 internal_error (__FILE__, __LINE__,
4885 "Calling target_async when async is masked");
4887 if (callback != NULL)
4889 async_client_callback = callback;
4890 async_client_context = context;
4891 if (!linux_async_pipe (1))
4893 add_file_handler (linux_nat_event_pipe[0],
4894 handle_target_event, NULL);
4895 /* There may be pending events to handle. Tell the event loop
4902 async_client_callback = callback;
4903 async_client_context = context;
4904 delete_file_handler (linux_nat_event_pipe[0]);
4905 linux_async_pipe (0);
4910 /* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
4914 linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
4919 ptid_t ptid = lwp->ptid;
4921 if (debug_linux_nat)
4922 fprintf_unfiltered (gdb_stdlog,
4923 "LNSL: running -> suspending %s\n",
4924 target_pid_to_str (lwp->ptid));
4927 stop_callback (lwp, NULL);
4928 stop_wait_callback (lwp, NULL);
4930 /* If the lwp exits while we try to stop it, there's nothing
4932 lwp = find_lwp_pid (ptid);
4936 /* If we didn't collect any signal other than SIGSTOP while
4937 stopping the LWP, push a SIGNAL_0 event. In either case, the
4938 event-loop will end up calling target_wait which will collect
4940 if (lwp->status == 0)
4941 lwp->status = W_STOPCODE (0);
4946 /* Already known to be stopped; do nothing. */
4948 if (debug_linux_nat)
4950 if (find_thread_ptid (lwp->ptid)->stop_requested)
4951 fprintf_unfiltered (gdb_stdlog, "\
4952 LNSL: already stopped/stop_requested %s\n",
4953 target_pid_to_str (lwp->ptid));
4955 fprintf_unfiltered (gdb_stdlog, "\
4956 LNSL: already stopped/no stop_requested yet %s\n",
4957 target_pid_to_str (lwp->ptid));
4964 linux_nat_stop (ptid_t ptid)
4967 iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
4969 linux_ops->to_stop (ptid);
4973 linux_nat_close (int quitting)
4975 /* Unregister from the event loop. */
4976 if (target_is_async_p ())
4977 target_async (NULL, 0);
4979 /* Reset the async_masking. */
4980 linux_nat_async_mask_value = 1;
4982 if (linux_ops->to_close)
4983 linux_ops->to_close (quitting);
4987 linux_nat_add_target (struct target_ops *t)
4989 /* Save the provided single-threaded target. We save this in a separate
4990 variable because another target we've inherited from (e.g. inf-ptrace)
4991 may have saved a pointer to T; we want to use it for the final
4992 process stratum target. */
4993 linux_ops_saved = *t;
4994 linux_ops = &linux_ops_saved;
4996 /* Override some methods for multithreading. */
4997 t->to_create_inferior = linux_nat_create_inferior;
4998 t->to_attach = linux_nat_attach;
4999 t->to_detach = linux_nat_detach;
5000 t->to_resume = linux_nat_resume;
5001 t->to_wait = linux_nat_wait;
5002 t->to_xfer_partial = linux_nat_xfer_partial;
5003 t->to_kill = linux_nat_kill;
5004 t->to_mourn_inferior = linux_nat_mourn_inferior;
5005 t->to_thread_alive = linux_nat_thread_alive;
5006 t->to_pid_to_str = linux_nat_pid_to_str;
5007 t->to_has_thread_control = tc_schedlock;
5009 t->to_can_async_p = linux_nat_can_async_p;
5010 t->to_is_async_p = linux_nat_is_async_p;
5011 t->to_supports_non_stop = linux_nat_supports_non_stop;
5012 t->to_async = linux_nat_async;
5013 t->to_async_mask = linux_nat_async_mask;
5014 t->to_terminal_inferior = linux_nat_terminal_inferior;
5015 t->to_terminal_ours = linux_nat_terminal_ours;
5016 t->to_close = linux_nat_close;
5018 /* Methods for non-stop support. */
5019 t->to_stop = linux_nat_stop;
5021 t->to_supports_multi_process = linux_nat_supports_multi_process;
5023 /* We don't change the stratum; this target will sit at
5024 process_stratum and thread_db will set at thread_stratum. This
5025 is a little strange, since this is a multi-threaded-capable
5026 target, but we want to be on the stack below thread_db, and we
5027 also want to be used for single-threaded processes. */
5032 /* Register a method to call whenever a new thread is attached. */
5034 linux_nat_set_new_thread (struct target_ops *t, void (*new_thread) (ptid_t))
5036 /* Save the pointer. We only support a single registered instance
5037 of the GNU/Linux native target, so we do not need to map this to
5039 linux_nat_new_thread = new_thread;
5042 /* Register a method that converts a siginfo object between the layout
5043 that ptrace returns, and the layout in the architecture of the
5046 linux_nat_set_siginfo_fixup (struct target_ops *t,
5047 int (*siginfo_fixup) (struct siginfo *,
5051 /* Save the pointer. */
5052 linux_nat_siginfo_fixup = siginfo_fixup;
5055 /* Return the saved siginfo associated with PTID. */
5057 linux_nat_get_siginfo (ptid_t ptid)
5059 struct lwp_info *lp = find_lwp_pid (ptid);
5061 gdb_assert (lp != NULL);
5063 return &lp->siginfo;
5066 /* Provide a prototype to silence -Wmissing-prototypes. */
5067 extern initialize_file_ftype _initialize_linux_nat;
5070 _initialize_linux_nat (void)
5074 add_info ("proc", linux_nat_info_proc_cmd, _("\
5075 Show /proc process information about any running process.\n\
5076 Specify any process id, or use the program being debugged by default.\n\
5077 Specify any of the following keywords for detailed info:\n\
5078 mappings -- list of mapped memory regions.\n\
5079 stat -- list a bunch of random process info.\n\
5080 status -- list a different bunch of random process info.\n\
5081 all -- list all available /proc info."));
5083 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance,
5084 &debug_linux_nat, _("\
5085 Set debugging of GNU/Linux lwp module."), _("\
5086 Show debugging of GNU/Linux lwp module."), _("\
5087 Enables printf debugging output."),
5089 show_debug_linux_nat,
5090 &setdebuglist, &showdebuglist);
5092 add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance,
5093 &debug_linux_nat_async, _("\
5094 Set debugging of GNU/Linux async lwp module."), _("\
5095 Show debugging of GNU/Linux async lwp module."), _("\
5096 Enables printf debugging output."),
5098 show_debug_linux_nat_async,
5099 &setdebuglist, &showdebuglist);
5101 /* Save this mask as the default. */
5102 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
5104 /* Install a SIGCHLD handler. */
5105 sigchld_action.sa_handler = sigchld_handler;
5106 sigemptyset (&sigchld_action.sa_mask);
5107 sigchld_action.sa_flags = SA_RESTART;
5109 /* Make it the default. */
5110 sigaction (SIGCHLD, &sigchld_action, NULL);
5112 /* Make sure we don't block SIGCHLD during a sigsuspend. */
5113 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
5114 sigdelset (&suspend_mask, SIGCHLD);
5116 sigemptyset (&blocked_mask);
5118 add_setshow_boolean_cmd ("disable-randomization", class_support,
5119 &disable_randomization, _("\
5120 Set disabling of debuggee's virtual address space randomization."), _("\
5121 Show disabling of debuggee's virtual address space randomization."), _("\
5122 When this mode is on (which is the default), randomization of the virtual\n\
5123 address space is disabled. Standalone programs run with the randomization\n\
5124 enabled by default on some platforms."),
5125 &set_disable_randomization,
5126 &show_disable_randomization,
5127 &setlist, &showlist);
5131 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
5132 the GNU/Linux Threads library and therefore doesn't really belong
5135 /* Read variable NAME in the target and return its value if found.
5136 Otherwise return zero. It is assumed that the type of the variable
5140 get_signo (const char *name)
5142 struct minimal_symbol *ms;
5145 ms = lookup_minimal_symbol (name, NULL, NULL);
5149 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (gdb_byte *) &signo,
5150 sizeof (signo)) != 0)
5156 /* Return the set of signals used by the threads library in *SET. */
5159 lin_thread_get_thread_signals (sigset_t *set)
5161 struct sigaction action;
5162 int restart, cancel;
5164 sigemptyset (&blocked_mask);
5167 restart = get_signo ("__pthread_sig_restart");
5168 cancel = get_signo ("__pthread_sig_cancel");
5170 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5171 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5172 not provide any way for the debugger to query the signal numbers -
5173 fortunately they don't change! */
5176 restart = __SIGRTMIN;
5179 cancel = __SIGRTMIN + 1;
5181 sigaddset (set, restart);
5182 sigaddset (set, cancel);
5184 /* The GNU/Linux Threads library makes terminating threads send a
5185 special "cancel" signal instead of SIGCHLD. Make sure we catch
5186 those (to prevent them from terminating GDB itself, which is
5187 likely to be their default action) and treat them the same way as
5190 action.sa_handler = sigchld_handler;
5191 sigemptyset (&action.sa_mask);
5192 action.sa_flags = SA_RESTART;
5193 sigaction (cancel, &action, NULL);
5195 /* We block the "cancel" signal throughout this code ... */
5196 sigaddset (&blocked_mask, cancel);
5197 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
5199 /* ... except during a sigsuspend. */
5200 sigdelset (&suspend_mask, cancel);