1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
26 #include "gdb_string.h"
28 #include "gdb_assert.h"
29 #ifdef HAVE_TKILL_SYSCALL
31 #include <sys/syscall.h>
33 #include <sys/ptrace.h>
34 #include "linux-nat.h"
35 #include "linux-fork.h"
36 #include "gdbthread.h"
39 #include "inf-ptrace.h"
41 #include <sys/param.h> /* for MAXPATHLEN */
42 #include <sys/procfs.h> /* for elf_gregset etc. */
43 #include "elf-bfd.h" /* for elfcore_write_* */
44 #include "gregset.h" /* for gregset */
45 #include "gdbcore.h" /* for get_exec_file */
46 #include <ctype.h> /* for isdigit */
47 #include "gdbthread.h" /* for struct thread_info etc. */
48 #include "gdb_stat.h" /* for struct stat */
49 #include <fcntl.h> /* for O_RDONLY */
55 /* If the system headers did not provide the constants, hard-code the normal
57 #ifndef PTRACE_EVENT_FORK
59 #define PTRACE_SETOPTIONS 0x4200
60 #define PTRACE_GETEVENTMSG 0x4201
62 /* options set using PTRACE_SETOPTIONS */
63 #define PTRACE_O_TRACESYSGOOD 0x00000001
64 #define PTRACE_O_TRACEFORK 0x00000002
65 #define PTRACE_O_TRACEVFORK 0x00000004
66 #define PTRACE_O_TRACECLONE 0x00000008
67 #define PTRACE_O_TRACEEXEC 0x00000010
68 #define PTRACE_O_TRACEVFORKDONE 0x00000020
69 #define PTRACE_O_TRACEEXIT 0x00000040
71 /* Wait extended result codes for the above trace options. */
72 #define PTRACE_EVENT_FORK 1
73 #define PTRACE_EVENT_VFORK 2
74 #define PTRACE_EVENT_CLONE 3
75 #define PTRACE_EVENT_EXEC 4
76 #define PTRACE_EVENT_VFORK_DONE 5
77 #define PTRACE_EVENT_EXIT 6
79 #endif /* PTRACE_EVENT_FORK */
81 /* We can't always assume that this flag is available, but all systems
82 with the ptrace event handlers also have __WALL, so it's safe to use
85 #define __WALL 0x40000000 /* Wait for any child. */
88 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
89 the use of the multi-threaded target. */
90 static struct target_ops
*linux_ops
;
92 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
93 Called by our to_xfer_partial. */
94 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
96 const char *, gdb_byte
*,
100 /* The saved to_mourn_inferior method, inherited from inf-ptrace.c.
101 Called by our to_mourn_inferior. */
102 static void (*super_mourn_inferior
) (void);
104 static int debug_linux_nat
;
106 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
107 struct cmd_list_element
*c
, const char *value
)
109 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
113 static int linux_parent_pid
;
115 struct simple_pid_list
118 struct simple_pid_list
*next
;
120 struct simple_pid_list
*stopped_pids
;
122 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
123 can not be used, 1 if it can. */
125 static int linux_supports_tracefork_flag
= -1;
127 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
128 PTRACE_O_TRACEVFORKDONE. */
130 static int linux_supports_tracevforkdone_flag
= -1;
133 /* Trivial list manipulation functions to keep track of a list of
134 new stopped processes. */
136 add_to_pid_list (struct simple_pid_list
**listp
, int pid
)
138 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
140 new_pid
->next
= *listp
;
145 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
)
147 struct simple_pid_list
**p
;
149 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
150 if ((*p
)->pid
== pid
)
152 struct simple_pid_list
*next
= (*p
)->next
;
161 linux_record_stopped_pid (int pid
)
163 add_to_pid_list (&stopped_pids
, pid
);
167 /* A helper function for linux_test_for_tracefork, called after fork (). */
170 linux_tracefork_child (void)
174 ptrace (PTRACE_TRACEME
, 0, 0, 0);
175 kill (getpid (), SIGSTOP
);
180 /* Wrapper function for waitpid which handles EINTR. */
183 my_waitpid (int pid
, int *status
, int flags
)
188 ret
= waitpid (pid
, status
, flags
);
190 while (ret
== -1 && errno
== EINTR
);
195 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
197 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
198 we know that the feature is not available. This may change the tracing
199 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
201 However, if it succeeds, we don't know for sure that the feature is
202 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
203 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
204 fork tracing, and let it fork. If the process exits, we assume that we
205 can't use TRACEFORK; if we get the fork notification, and we can extract
206 the new child's PID, then we assume that we can. */
209 linux_test_for_tracefork (int original_pid
)
211 int child_pid
, ret
, status
;
214 linux_supports_tracefork_flag
= 0;
215 linux_supports_tracevforkdone_flag
= 0;
217 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
223 perror_with_name (("fork"));
226 linux_tracefork_child ();
228 ret
= my_waitpid (child_pid
, &status
, 0);
230 perror_with_name (("waitpid"));
231 else if (ret
!= child_pid
)
232 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
233 if (! WIFSTOPPED (status
))
234 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
236 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
239 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
242 warning (_("linux_test_for_tracefork: failed to kill child"));
246 ret
= my_waitpid (child_pid
, &status
, 0);
247 if (ret
!= child_pid
)
248 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
249 else if (!WIFSIGNALED (status
))
250 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
251 "killed child"), status
);
256 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
257 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
258 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
259 linux_supports_tracevforkdone_flag
= (ret
== 0);
261 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
263 warning (_("linux_test_for_tracefork: failed to resume child"));
265 ret
= my_waitpid (child_pid
, &status
, 0);
267 if (ret
== child_pid
&& WIFSTOPPED (status
)
268 && status
>> 16 == PTRACE_EVENT_FORK
)
271 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
272 if (ret
== 0 && second_pid
!= 0)
276 linux_supports_tracefork_flag
= 1;
277 my_waitpid (second_pid
, &second_status
, 0);
278 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
280 warning (_("linux_test_for_tracefork: failed to kill second child"));
284 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
285 "(%d, status 0x%x)"), ret
, status
);
287 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
289 warning (_("linux_test_for_tracefork: failed to kill child"));
290 my_waitpid (child_pid
, &status
, 0);
293 /* Return non-zero iff we have tracefork functionality available.
294 This function also sets linux_supports_tracefork_flag. */
297 linux_supports_tracefork (int pid
)
299 if (linux_supports_tracefork_flag
== -1)
300 linux_test_for_tracefork (pid
);
301 return linux_supports_tracefork_flag
;
305 linux_supports_tracevforkdone (int pid
)
307 if (linux_supports_tracefork_flag
== -1)
308 linux_test_for_tracefork (pid
);
309 return linux_supports_tracevforkdone_flag
;
314 linux_enable_event_reporting (ptid_t ptid
)
316 int pid
= ptid_get_lwp (ptid
);
320 pid
= ptid_get_pid (ptid
);
322 if (! linux_supports_tracefork (pid
))
325 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
326 | PTRACE_O_TRACECLONE
;
327 if (linux_supports_tracevforkdone (pid
))
328 options
|= PTRACE_O_TRACEVFORKDONE
;
330 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
331 read-only process state. */
333 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
337 child_post_attach (int pid
)
339 linux_enable_event_reporting (pid_to_ptid (pid
));
343 linux_child_post_startup_inferior (ptid_t ptid
)
345 linux_enable_event_reporting (ptid
);
349 child_follow_fork (struct target_ops
*ops
, int follow_child
)
352 struct target_waitstatus last_status
;
354 int parent_pid
, child_pid
;
356 get_last_target_status (&last_ptid
, &last_status
);
357 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
358 parent_pid
= ptid_get_lwp (last_ptid
);
360 parent_pid
= ptid_get_pid (last_ptid
);
361 child_pid
= last_status
.value
.related_pid
;
365 /* We're already attached to the parent, by default. */
367 /* Before detaching from the child, remove all breakpoints from
368 it. (This won't actually modify the breakpoint list, but will
369 physically remove the breakpoints from the child.) */
370 /* If we vforked this will remove the breakpoints from the parent
371 also, but they'll be reinserted below. */
372 detach_breakpoints (child_pid
);
374 /* Detach new forked process? */
379 target_terminal_ours ();
380 fprintf_filtered (gdb_stdlog
,
381 "Detaching after fork from child process %d.\n",
385 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
389 struct fork_info
*fp
;
390 /* Retain child fork in ptrace (stopped) state. */
391 fp
= find_fork_pid (child_pid
);
393 fp
= add_fork (child_pid
);
394 fork_save_infrun_state (fp
, 0);
399 gdb_assert (linux_supports_tracefork_flag
>= 0);
400 if (linux_supports_tracevforkdone (0))
404 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
405 my_waitpid (parent_pid
, &status
, __WALL
);
406 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
407 warning (_("Unexpected waitpid result %06x when waiting for "
408 "vfork-done"), status
);
412 /* We can't insert breakpoints until the child has
413 finished with the shared memory region. We need to
414 wait until that happens. Ideal would be to just
416 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
417 - waitpid (parent_pid, &status, __WALL);
418 However, most architectures can't handle a syscall
419 being traced on the way out if it wasn't traced on
422 We might also think to loop, continuing the child
423 until it exits or gets a SIGTRAP. One problem is
424 that the child might call ptrace with PTRACE_TRACEME.
426 There's no simple and reliable way to figure out when
427 the vforked child will be done with its copy of the
428 shared memory. We could step it out of the syscall,
429 two instructions, let it go, and then single-step the
430 parent once. When we have hardware single-step, this
431 would work; with software single-step it could still
432 be made to work but we'd have to be able to insert
433 single-step breakpoints in the child, and we'd have
434 to insert -just- the single-step breakpoint in the
435 parent. Very awkward.
437 In the end, the best we can do is to make sure it
438 runs for a little while. Hopefully it will be out of
439 range of any breakpoints we reinsert. Usually this
440 is only the single-step breakpoint at vfork's return
446 /* Since we vforked, breakpoints were removed in the parent
447 too. Put them back. */
448 reattach_breakpoints (parent_pid
);
453 char child_pid_spelling
[40];
455 /* Needed to keep the breakpoint lists in sync. */
457 detach_breakpoints (child_pid
);
459 /* Before detaching from the parent, remove all breakpoints from it. */
460 remove_breakpoints ();
464 target_terminal_ours ();
465 fprintf_filtered (gdb_stdlog
,
466 "Attaching after fork to child process %d.\n",
470 /* If we're vforking, we may want to hold on to the parent until
471 the child exits or execs. At exec time we can remove the old
472 breakpoints from the parent and detach it; at exit time we
473 could do the same (or even, sneakily, resume debugging it - the
474 child's exec has failed, or something similar).
476 This doesn't clean up "properly", because we can't call
477 target_detach, but that's OK; if the current target is "child",
478 then it doesn't need any further cleanups, and lin_lwp will
479 generally not encounter vfork (vfork is defined to fork
482 The holding part is very easy if we have VFORKDONE events;
483 but keeping track of both processes is beyond GDB at the
484 moment. So we don't expose the parent to the rest of GDB.
485 Instead we quietly hold onto it until such time as we can
489 linux_parent_pid
= parent_pid
;
490 else if (!detach_fork
)
492 struct fork_info
*fp
;
493 /* Retain parent fork in ptrace (stopped) state. */
494 fp
= find_fork_pid (parent_pid
);
496 fp
= add_fork (parent_pid
);
497 fork_save_infrun_state (fp
, 0);
501 target_detach (NULL
, 0);
504 inferior_ptid
= pid_to_ptid (child_pid
);
506 /* Reinstall ourselves, since we might have been removed in
507 target_detach (which does other necessary cleanup). */
511 /* Reset breakpoints in the child as appropriate. */
512 follow_inferior_reset_breakpoints ();
519 linux_handle_extended_wait (int pid
, int status
,
520 struct target_waitstatus
*ourstatus
)
522 int event
= status
>> 16;
524 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
525 || event
== PTRACE_EVENT_CLONE
)
527 unsigned long new_pid
;
530 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
532 /* If we haven't already seen the new PID stop, wait for it now. */
533 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
535 /* The new child has a pending SIGSTOP. We can't affect it until it
536 hits the SIGSTOP, but we're already attached. */
537 ret
= my_waitpid (new_pid
, &status
,
538 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
540 perror_with_name (_("waiting for new child"));
541 else if (ret
!= new_pid
)
542 internal_error (__FILE__
, __LINE__
,
543 _("wait returned unexpected PID %d"), ret
);
544 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
545 internal_error (__FILE__
, __LINE__
,
546 _("wait returned unexpected status 0x%x"), status
);
549 if (event
== PTRACE_EVENT_FORK
)
550 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
551 else if (event
== PTRACE_EVENT_VFORK
)
552 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
554 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
556 ourstatus
->value
.related_pid
= new_pid
;
557 return inferior_ptid
;
560 if (event
== PTRACE_EVENT_EXEC
)
562 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
563 ourstatus
->value
.execd_pathname
564 = xstrdup (child_pid_to_exec_file (pid
));
566 if (linux_parent_pid
)
568 detach_breakpoints (linux_parent_pid
);
569 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
571 linux_parent_pid
= 0;
574 return inferior_ptid
;
577 internal_error (__FILE__
, __LINE__
,
578 _("unknown ptrace event %d"), event
);
583 child_insert_fork_catchpoint (int pid
)
585 if (! linux_supports_tracefork (pid
))
586 error (_("Your system does not support fork catchpoints."));
590 child_insert_vfork_catchpoint (int pid
)
592 if (!linux_supports_tracefork (pid
))
593 error (_("Your system does not support vfork catchpoints."));
597 child_insert_exec_catchpoint (int pid
)
599 if (!linux_supports_tracefork (pid
))
600 error (_("Your system does not support exec catchpoints."));
607 int pid
= PIDGET (inferior_ptid
);
608 struct target_waitstatus last
;
615 /* First cut -- let's crudely do everything inline. */
616 if (forks_exist_p ())
618 linux_fork_killall ();
622 /* If we're stopped while forking and we haven't followed yet,
623 kill the other task. We need to do this first because the
624 parent will be sleeping if this is a vfork. */
626 get_last_target_status (&last_ptid
, &last
);
628 if (last
.kind
== TARGET_WAITKIND_FORKED
629 || last
.kind
== TARGET_WAITKIND_VFORKED
)
631 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
635 /* Kill the current process. */
636 ptrace (PT_KILL
, pid
, 0, 0);
637 ret
= wait (&status
);
639 /* We might get a SIGCHLD instead of an exit status. This is
640 aggravated by the first kill above - a child has just died. */
642 while (ret
== pid
&& WIFSTOPPED (status
))
644 ptrace (PT_KILL
, pid
, 0, 0);
645 ret
= wait (&status
);
648 target_mourn_inferior ();
651 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
652 are processes sharing the same VM space. A multi-threaded process
653 is basically a group of such processes. However, such a grouping
654 is almost entirely a user-space issue; the kernel doesn't enforce
655 such a grouping at all (this might change in the future). In
656 general, we'll rely on the threads library (i.e. the GNU/Linux
657 Threads library) to provide such a grouping.
659 It is perfectly well possible to write a multi-threaded application
660 without the assistance of a threads library, by using the clone
661 system call directly. This module should be able to give some
662 rudimentary support for debugging such applications if developers
663 specify the CLONE_PTRACE flag in the clone system call, and are
664 using the Linux kernel 2.4 or above.
666 Note that there are some peculiarities in GNU/Linux that affect
669 - In general one should specify the __WCLONE flag to waitpid in
670 order to make it report events for any of the cloned processes
671 (and leave it out for the initial process). However, if a cloned
672 process has exited the exit status is only reported if the
673 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
674 we cannot use it since GDB must work on older systems too.
676 - When a traced, cloned process exits and is waited for by the
677 debugger, the kernel reassigns it to the original parent and
678 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
679 library doesn't notice this, which leads to the "zombie problem":
680 When debugged a multi-threaded process that spawns a lot of
681 threads will run out of processes, even if the threads exit,
682 because the "zombies" stay around. */
684 /* List of known LWPs. */
685 static struct lwp_info
*lwp_list
;
687 /* Number of LWPs in the list. */
690 /* Non-zero if we're running in "threaded" mode. */
694 #define GET_LWP(ptid) ptid_get_lwp (ptid)
695 #define GET_PID(ptid) ptid_get_pid (ptid)
696 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
697 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
699 /* If the last reported event was a SIGTRAP, this variable is set to
700 the process id of the LWP/thread that got it. */
704 /* This module's target-specific operations. */
705 static struct target_ops linux_nat_ops
;
707 /* Since we cannot wait (in linux_nat_wait) for the initial process and
708 any cloned processes with a single call to waitpid, we have to use
709 the WNOHANG flag and call waitpid in a loop. To optimize
710 things a bit we use `sigsuspend' to wake us up when a process has
711 something to report (it will send us a SIGCHLD if it has). To make
712 this work we have to juggle with the signal mask. We save the
713 original signal mask such that we can restore it before creating a
714 new process in order to avoid blocking certain signals in the
715 inferior. We then block SIGCHLD during the waitpid/sigsuspend
718 /* Original signal mask. */
719 static sigset_t normal_mask
;
721 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
722 _initialize_linux_nat. */
723 static sigset_t suspend_mask
;
725 /* Signals to block to make that sigsuspend work. */
726 static sigset_t blocked_mask
;
729 /* Prototypes for local functions. */
730 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
731 static int linux_nat_thread_alive (ptid_t ptid
);
733 /* Convert wait status STATUS to a string. Used for printing debug
737 status_to_str (int status
)
741 if (WIFSTOPPED (status
))
742 snprintf (buf
, sizeof (buf
), "%s (stopped)",
743 strsignal (WSTOPSIG (status
)));
744 else if (WIFSIGNALED (status
))
745 snprintf (buf
, sizeof (buf
), "%s (terminated)",
746 strsignal (WSTOPSIG (status
)));
748 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
753 /* Initialize the list of LWPs. Note that this module, contrary to
754 what GDB's generic threads layer does for its thread list,
755 re-initializes the LWP lists whenever we mourn or detach (which
756 doesn't involve mourning) the inferior. */
761 struct lwp_info
*lp
, *lpnext
;
763 for (lp
= lwp_list
; lp
; lp
= lpnext
)
774 /* Add the LWP specified by PID to the list. If this causes the
775 number of LWPs to become larger than one, go into "threaded" mode.
776 Return a pointer to the structure describing the new LWP. */
778 static struct lwp_info
*
779 add_lwp (ptid_t ptid
)
783 gdb_assert (is_lwp (ptid
));
785 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
787 memset (lp
, 0, sizeof (struct lwp_info
));
789 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
801 /* Remove the LWP specified by PID from the list. */
804 delete_lwp (ptid_t ptid
)
806 struct lwp_info
*lp
, *lpprev
;
810 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
811 if (ptid_equal (lp
->ptid
, ptid
))
817 /* We don't go back to "non-threaded" mode if the number of threads
818 becomes less than two. */
822 lpprev
->next
= lp
->next
;
829 /* Return a pointer to the structure describing the LWP corresponding
830 to PID. If no corresponding LWP could be found, return NULL. */
832 static struct lwp_info
*
833 find_lwp_pid (ptid_t ptid
)
839 lwp
= GET_LWP (ptid
);
841 lwp
= GET_PID (ptid
);
843 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
844 if (lwp
== GET_LWP (lp
->ptid
))
850 /* Call CALLBACK with its second argument set to DATA for every LWP in
851 the list. If CALLBACK returns 1 for a particular LWP, return a
852 pointer to the structure describing that LWP immediately.
853 Otherwise return NULL. */
856 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
858 struct lwp_info
*lp
, *lpnext
;
860 for (lp
= lwp_list
; lp
; lp
= lpnext
)
863 if ((*callback
) (lp
, data
))
870 /* Record a PTID for later deletion. */
875 struct saved_ptids
*next
;
877 static struct saved_ptids
*threads_to_delete
;
880 record_dead_thread (ptid_t ptid
)
882 struct saved_ptids
*p
= xmalloc (sizeof (struct saved_ptids
));
884 p
->next
= threads_to_delete
;
885 threads_to_delete
= p
;
888 /* Delete any dead threads which are not the current thread. */
893 struct saved_ptids
**p
= &threads_to_delete
;
896 if (! ptid_equal ((*p
)->ptid
, inferior_ptid
))
898 struct saved_ptids
*tmp
= *p
;
899 delete_thread (tmp
->ptid
);
907 /* Callback for iterate_over_threads that finds a thread corresponding
911 find_thread_from_lwp (struct thread_info
*thr
, void *dummy
)
913 ptid_t
*ptid_p
= dummy
;
915 if (GET_LWP (thr
->ptid
) && GET_LWP (thr
->ptid
) == GET_LWP (*ptid_p
))
921 /* Handle the exit of a single thread LP. */
924 exit_lwp (struct lwp_info
*lp
)
926 if (in_thread_list (lp
->ptid
))
928 /* Core GDB cannot deal with us deleting the current thread. */
929 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
930 delete_thread (lp
->ptid
);
932 record_dead_thread (lp
->ptid
);
933 printf_unfiltered (_("[%s exited]\n"),
934 target_pid_to_str (lp
->ptid
));
938 /* Even if LP->PTID is not in the global GDB thread list, the
939 LWP may be - with an additional thread ID. We don't need
940 to print anything in this case; thread_db is in use and
941 already took care of that. But it didn't delete the thread
942 in order to handle zombies correctly. */
944 struct thread_info
*thr
;
946 thr
= iterate_over_threads (find_thread_from_lwp
, &lp
->ptid
);
947 if (thr
&& !ptid_equal (thr
->ptid
, inferior_ptid
))
948 delete_thread (thr
->ptid
);
950 record_dead_thread (thr
->ptid
);
953 delete_lwp (lp
->ptid
);
956 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
957 a message telling the user that a new LWP has been added to the
961 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
963 struct lwp_info
*lp
, *found_lp
;
965 gdb_assert (is_lwp (ptid
));
967 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
968 to interrupt either the ptrace() or waitpid() calls below. */
969 if (!sigismember (&blocked_mask
, SIGCHLD
))
971 sigaddset (&blocked_mask
, SIGCHLD
);
972 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
976 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid
));
978 found_lp
= lp
= find_lwp_pid (ptid
);
982 /* We assume that we're already attached to any LWP that has an id
983 equal to the overall process id, and to any LWP that is already
984 in our list of LWPs. If we're not seeing exit events from threads
985 and we've had PID wraparound since we last tried to stop all threads,
986 this assumption might be wrong; fortunately, this is very unlikely
988 if (GET_LWP (ptid
) != GET_PID (ptid
) && found_lp
== NULL
)
993 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
994 error (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
995 safe_strerror (errno
));
998 fprintf_unfiltered (gdb_stdlog
,
999 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1000 target_pid_to_str (ptid
));
1002 pid
= my_waitpid (GET_LWP (ptid
), &status
, 0);
1003 if (pid
== -1 && errno
== ECHILD
)
1005 /* Try again with __WCLONE to check cloned processes. */
1006 pid
= my_waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
1010 gdb_assert (pid
== GET_LWP (ptid
)
1011 && WIFSTOPPED (status
) && WSTOPSIG (status
));
1013 child_post_attach (pid
);
1017 if (debug_linux_nat
)
1019 fprintf_unfiltered (gdb_stdlog
,
1020 "LLAL: waitpid %s received %s\n",
1021 target_pid_to_str (ptid
),
1022 status_to_str (status
));
1027 /* We assume that the LWP representing the original process is
1028 already stopped. Mark it as stopped in the data structure
1029 that the linux ptrace layer uses to keep track of threads.
1030 Note that this won't have already been done since the main
1031 thread will have, we assume, been stopped by an attach from a
1038 linux_nat_attach (char *args
, int from_tty
)
1040 struct lwp_info
*lp
;
1044 /* FIXME: We should probably accept a list of process id's, and
1045 attach all of them. */
1046 linux_ops
->to_attach (args
, from_tty
);
1048 /* Add the initial process as the first LWP to the list. */
1049 lp
= add_lwp (BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
)));
1051 /* Make sure the initial process is stopped. The user-level threads
1052 layer might want to poke around in the inferior, and that won't
1053 work if things haven't stabilized yet. */
1054 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, 0);
1055 if (pid
== -1 && errno
== ECHILD
)
1057 warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid
));
1059 /* Try again with __WCLONE to check cloned processes. */
1060 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
1064 gdb_assert (pid
== GET_PID (inferior_ptid
)
1065 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
1069 /* Fake the SIGSTOP that core GDB expects. */
1070 lp
->status
= W_STOPCODE (SIGSTOP
);
1072 if (debug_linux_nat
)
1074 fprintf_unfiltered (gdb_stdlog
,
1075 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid
);
1080 detach_callback (struct lwp_info
*lp
, void *data
)
1082 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1084 if (debug_linux_nat
&& lp
->status
)
1085 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1086 strsignal (WSTOPSIG (lp
->status
)),
1087 target_pid_to_str (lp
->ptid
));
1089 while (lp
->signalled
&& lp
->stopped
)
1092 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
1093 WSTOPSIG (lp
->status
)) < 0)
1094 error (_("Can't continue %s: %s"), target_pid_to_str (lp
->ptid
),
1095 safe_strerror (errno
));
1097 if (debug_linux_nat
)
1098 fprintf_unfiltered (gdb_stdlog
,
1099 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
1100 target_pid_to_str (lp
->ptid
),
1101 status_to_str (lp
->status
));
1106 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
1107 here. But since lp->signalled was cleared above,
1108 stop_wait_callback didn't do anything; the process was left
1109 running. Shouldn't we be waiting for it to stop?
1110 I've removed the call, since stop_wait_callback now does do
1111 something when called with lp->signalled == 0. */
1113 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1116 /* We don't actually detach from the LWP that has an id equal to the
1117 overall process id just yet. */
1118 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1121 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1122 WSTOPSIG (lp
->status
)) < 0)
1123 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1124 safe_strerror (errno
));
1126 if (debug_linux_nat
)
1127 fprintf_unfiltered (gdb_stdlog
,
1128 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1129 target_pid_to_str (lp
->ptid
),
1130 strsignal (WSTOPSIG (lp
->status
)));
1132 delete_lwp (lp
->ptid
);
1139 linux_nat_detach (char *args
, int from_tty
)
1141 iterate_over_lwps (detach_callback
, NULL
);
1143 /* Only the initial process should be left right now. */
1144 gdb_assert (num_lwps
== 1);
1146 trap_ptid
= null_ptid
;
1148 /* Destroy LWP info; it's no longer valid. */
1151 /* Restore the original signal mask. */
1152 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1153 sigemptyset (&blocked_mask
);
1155 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
1156 linux_ops
->to_detach (args
, from_tty
);
1162 resume_callback (struct lwp_info
*lp
, void *data
)
1164 if (lp
->stopped
&& lp
->status
== 0)
1166 struct thread_info
*tp
;
1168 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
1169 0, TARGET_SIGNAL_0
);
1170 if (debug_linux_nat
)
1171 fprintf_unfiltered (gdb_stdlog
,
1172 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1173 target_pid_to_str (lp
->ptid
));
1182 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1189 resume_set_callback (struct lwp_info
*lp
, void *data
)
1196 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1198 struct lwp_info
*lp
;
1201 if (debug_linux_nat
)
1202 fprintf_unfiltered (gdb_stdlog
,
1203 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1204 step
? "step" : "resume",
1205 target_pid_to_str (ptid
),
1206 signo
? strsignal (signo
) : "0",
1207 target_pid_to_str (inferior_ptid
));
1211 /* A specific PTID means `step only this process id'. */
1212 resume_all
= (PIDGET (ptid
) == -1);
1215 iterate_over_lwps (resume_set_callback
, NULL
);
1217 iterate_over_lwps (resume_clear_callback
, NULL
);
1219 /* If PID is -1, it's the current inferior that should be
1220 handled specially. */
1221 if (PIDGET (ptid
) == -1)
1222 ptid
= inferior_ptid
;
1224 lp
= find_lwp_pid (ptid
);
1227 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1229 /* Remember if we're stepping. */
1232 /* Mark this LWP as resumed. */
1235 /* If we have a pending wait status for this thread, there is no
1236 point in resuming the process. But first make sure that
1237 linux_nat_wait won't preemptively handle the event - we
1238 should never take this short-circuit if we are going to
1239 leave LP running, since we have skipped resuming all the
1240 other threads. This bit of code needs to be synchronized
1241 with linux_nat_wait. */
1243 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1245 int saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1247 if (signal_stop_state (saved_signo
) == 0
1248 && signal_print_state (saved_signo
) == 0
1249 && signal_pass_state (saved_signo
) == 1)
1251 if (debug_linux_nat
)
1252 fprintf_unfiltered (gdb_stdlog
,
1253 "LLR: Not short circuiting for ignored "
1254 "status 0x%x\n", lp
->status
);
1256 /* FIXME: What should we do if we are supposed to continue
1257 this thread with a signal? */
1258 gdb_assert (signo
== TARGET_SIGNAL_0
);
1259 signo
= saved_signo
;
1266 /* FIXME: What should we do if we are supposed to continue
1267 this thread with a signal? */
1268 gdb_assert (signo
== TARGET_SIGNAL_0
);
1270 if (debug_linux_nat
)
1271 fprintf_unfiltered (gdb_stdlog
,
1272 "LLR: Short circuiting for status 0x%x\n",
1278 /* Mark LWP as not stopped to prevent it from being continued by
1284 iterate_over_lwps (resume_callback
, NULL
);
1286 linux_ops
->to_resume (ptid
, step
, signo
);
1287 if (debug_linux_nat
)
1288 fprintf_unfiltered (gdb_stdlog
,
1289 "LLR: %s %s, %s (resume event thread)\n",
1290 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1291 target_pid_to_str (ptid
),
1292 signo
? strsignal (signo
) : "0");
1295 /* Issue kill to specified lwp. */
1297 static int tkill_failed
;
1300 kill_lwp (int lwpid
, int signo
)
1304 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1305 fails, then we are not using nptl threads and we should be using kill. */
1307 #ifdef HAVE_TKILL_SYSCALL
1310 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1311 if (errno
!= ENOSYS
)
1318 return kill (lwpid
, signo
);
1321 /* Handle a GNU/Linux extended wait response. Most of the work we
1322 just pass off to linux_handle_extended_wait, but if it reports a
1323 clone event we need to add the new LWP to our list (and not report
1324 the trap to higher layers). This function returns non-zero if
1325 the event should be ignored and we should wait again. */
1328 linux_nat_handle_extended (struct lwp_info
*lp
, int status
)
1330 linux_handle_extended_wait (GET_LWP (lp
->ptid
), status
,
1333 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1334 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_SPURIOUS
)
1336 struct lwp_info
*new_lp
;
1337 new_lp
= add_lwp (BUILD_LWP (lp
->waitstatus
.value
.related_pid
,
1338 GET_PID (inferior_ptid
)));
1340 new_lp
->stopped
= 1;
1342 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1344 if (debug_linux_nat
)
1345 fprintf_unfiltered (gdb_stdlog
,
1346 "LLHE: Got clone event from LWP %ld, resuming\n",
1347 GET_LWP (lp
->ptid
));
1348 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1356 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1360 wait_lwp (struct lwp_info
*lp
)
1364 int thread_dead
= 0;
1366 gdb_assert (!lp
->stopped
);
1367 gdb_assert (lp
->status
== 0);
1369 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1370 if (pid
== -1 && errno
== ECHILD
)
1372 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1373 if (pid
== -1 && errno
== ECHILD
)
1375 /* The thread has previously exited. We need to delete it
1376 now because, for some vendor 2.4 kernels with NPTL
1377 support backported, there won't be an exit event unless
1378 it is the main thread. 2.6 kernels will report an exit
1379 event for each thread that exits, as expected. */
1381 if (debug_linux_nat
)
1382 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1383 target_pid_to_str (lp
->ptid
));
1389 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1391 if (debug_linux_nat
)
1393 fprintf_unfiltered (gdb_stdlog
,
1394 "WL: waitpid %s received %s\n",
1395 target_pid_to_str (lp
->ptid
),
1396 status_to_str (status
));
1400 /* Check if the thread has exited. */
1401 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1404 if (debug_linux_nat
)
1405 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1406 target_pid_to_str (lp
->ptid
));
1415 gdb_assert (WIFSTOPPED (status
));
1417 /* Handle GNU/Linux's extended waitstatus for trace events. */
1418 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1420 if (debug_linux_nat
)
1421 fprintf_unfiltered (gdb_stdlog
,
1422 "WL: Handling extended status 0x%06x\n",
1424 if (linux_nat_handle_extended (lp
, status
))
1425 return wait_lwp (lp
);
1431 /* Send a SIGSTOP to LP. */
1434 stop_callback (struct lwp_info
*lp
, void *data
)
1436 if (!lp
->stopped
&& !lp
->signalled
)
1440 if (debug_linux_nat
)
1442 fprintf_unfiltered (gdb_stdlog
,
1443 "SC: kill %s **<SIGSTOP>**\n",
1444 target_pid_to_str (lp
->ptid
));
1447 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1448 if (debug_linux_nat
)
1450 fprintf_unfiltered (gdb_stdlog
,
1451 "SC: lwp kill %d %s\n",
1453 errno
? safe_strerror (errno
) : "ERRNO-OK");
1457 gdb_assert (lp
->status
== 0);
1463 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1464 a pointer to a set of signals to be flushed immediately. */
1467 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1469 sigset_t
*flush_mask
= data
;
1475 status
= wait_lwp (lp
);
1479 /* Ignore any signals in FLUSH_MASK. */
1480 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1489 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1490 if (debug_linux_nat
)
1491 fprintf_unfiltered (gdb_stdlog
,
1492 "PTRACE_CONT %s, 0, 0 (%s)\n",
1493 target_pid_to_str (lp
->ptid
),
1494 errno
? safe_strerror (errno
) : "OK");
1496 return stop_wait_callback (lp
, flush_mask
);
1499 if (WSTOPSIG (status
) != SIGSTOP
)
1501 if (WSTOPSIG (status
) == SIGTRAP
)
1503 /* If a LWP other than the LWP that we're reporting an
1504 event for has hit a GDB breakpoint (as opposed to
1505 some random trap signal), then just arrange for it to
1506 hit it again later. We don't keep the SIGTRAP status
1507 and don't forward the SIGTRAP signal to the LWP. We
1508 will handle the current event, eventually we will
1509 resume all LWPs, and this one will get its breakpoint
1512 If we do not do this, then we run the risk that the
1513 user will delete or disable the breakpoint, but the
1514 thread will have already tripped on it. */
1516 /* Now resume this LWP and get the SIGSTOP event. */
1518 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1519 if (debug_linux_nat
)
1521 fprintf_unfiltered (gdb_stdlog
,
1522 "PTRACE_CONT %s, 0, 0 (%s)\n",
1523 target_pid_to_str (lp
->ptid
),
1524 errno
? safe_strerror (errno
) : "OK");
1526 fprintf_unfiltered (gdb_stdlog
,
1527 "SWC: Candidate SIGTRAP event in %s\n",
1528 target_pid_to_str (lp
->ptid
));
1530 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1531 stop_wait_callback (lp
, data
);
1532 /* If there's another event, throw it back into the queue. */
1535 if (debug_linux_nat
)
1537 fprintf_unfiltered (gdb_stdlog
,
1538 "SWC: kill %s, %s\n",
1539 target_pid_to_str (lp
->ptid
),
1540 status_to_str ((int) status
));
1542 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1544 /* Save the sigtrap event. */
1545 lp
->status
= status
;
1550 /* The thread was stopped with a signal other than
1551 SIGSTOP, and didn't accidentally trip a breakpoint. */
1553 if (debug_linux_nat
)
1555 fprintf_unfiltered (gdb_stdlog
,
1556 "SWC: Pending event %s in %s\n",
1557 status_to_str ((int) status
),
1558 target_pid_to_str (lp
->ptid
));
1560 /* Now resume this LWP and get the SIGSTOP event. */
1562 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1563 if (debug_linux_nat
)
1564 fprintf_unfiltered (gdb_stdlog
,
1565 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1566 target_pid_to_str (lp
->ptid
),
1567 errno
? safe_strerror (errno
) : "OK");
1569 /* Hold this event/waitstatus while we check to see if
1570 there are any more (we still want to get that SIGSTOP). */
1571 stop_wait_callback (lp
, data
);
1572 /* If the lp->status field is still empty, use it to hold
1573 this event. If not, then this event must be returned
1574 to the event queue of the LWP. */
1575 if (lp
->status
== 0)
1576 lp
->status
= status
;
1579 if (debug_linux_nat
)
1581 fprintf_unfiltered (gdb_stdlog
,
1582 "SWC: kill %s, %s\n",
1583 target_pid_to_str (lp
->ptid
),
1584 status_to_str ((int) status
));
1586 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1593 /* We caught the SIGSTOP that we intended to catch, so
1594 there's no SIGSTOP pending. */
1603 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1604 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1607 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
1609 sigset_t blocked
, ignored
;
1612 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
1617 for (i
= 1; i
< NSIG
; i
++)
1618 if (sigismember (pending
, i
))
1619 if (!sigismember (flush_mask
, i
)
1620 || sigismember (&blocked
, i
)
1621 || sigismember (&ignored
, i
))
1622 sigdelset (pending
, i
);
1624 if (sigisemptyset (pending
))
1630 /* DATA is interpreted as a mask of signals to flush. If LP has
1631 signals pending, and they are all in the flush mask, then arrange
1632 to flush them. LP should be stopped, as should all other threads
1633 it might share a signal queue with. */
1636 flush_callback (struct lwp_info
*lp
, void *data
)
1638 sigset_t
*flush_mask
= data
;
1639 sigset_t pending
, intersection
, blocked
, ignored
;
1642 /* Normally, when an LWP exits, it is removed from the LWP list. The
1643 last LWP isn't removed till later, however. So if there is only
1644 one LWP on the list, make sure it's alive. */
1645 if (lwp_list
== lp
&& lp
->next
== NULL
)
1646 if (!linux_nat_thread_alive (lp
->ptid
))
1649 /* Just because the LWP is stopped doesn't mean that new signals
1650 can't arrive from outside, so this function must be careful of
1651 race conditions. However, because all threads are stopped, we
1652 can assume that the pending mask will not shrink unless we resume
1653 the LWP, and that it will then get another signal. We can't
1654 control which one, however. */
1658 if (debug_linux_nat
)
1659 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp
->status
);
1660 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
1664 while (linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
1669 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1670 if (debug_linux_nat
)
1671 fprintf_unfiltered (gdb_stderr
,
1672 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
1675 stop_wait_callback (lp
, flush_mask
);
1676 if (debug_linux_nat
)
1677 fprintf_unfiltered (gdb_stderr
,
1678 "FC: Wait finished; saved status is %d\n",
1685 /* Return non-zero if LP has a wait status pending. */
1688 status_callback (struct lwp_info
*lp
, void *data
)
1690 /* Only report a pending wait status if we pretend that this has
1691 indeed been resumed. */
1692 return (lp
->status
!= 0 && lp
->resumed
);
1695 /* Return non-zero if LP isn't stopped. */
1698 running_callback (struct lwp_info
*lp
, void *data
)
1700 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
1703 /* Count the LWP's that have had events. */
1706 count_events_callback (struct lwp_info
*lp
, void *data
)
1710 gdb_assert (count
!= NULL
);
1712 /* Count only LWPs that have a SIGTRAP event pending. */
1714 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1720 /* Select the LWP (if any) that is currently being single-stepped. */
1723 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
1725 if (lp
->step
&& lp
->status
!= 0)
1731 /* Select the Nth LWP that has had a SIGTRAP event. */
1734 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
1736 int *selector
= data
;
1738 gdb_assert (selector
!= NULL
);
1740 /* Select only LWPs that have a SIGTRAP event pending. */
1742 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1743 if ((*selector
)-- == 0)
1750 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
1752 struct lwp_info
*event_lp
= data
;
1754 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1758 /* If a LWP other than the LWP that we're reporting an event for has
1759 hit a GDB breakpoint (as opposed to some random trap signal),
1760 then just arrange for it to hit it again later. We don't keep
1761 the SIGTRAP status and don't forward the SIGTRAP signal to the
1762 LWP. We will handle the current event, eventually we will resume
1763 all LWPs, and this one will get its breakpoint trap again.
1765 If we do not do this, then we run the risk that the user will
1766 delete or disable the breakpoint, but the LWP will have already
1770 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
1771 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
1772 DECR_PC_AFTER_BREAK
))
1774 if (debug_linux_nat
)
1775 fprintf_unfiltered (gdb_stdlog
,
1776 "CBC: Push back breakpoint for %s\n",
1777 target_pid_to_str (lp
->ptid
));
1779 /* Back up the PC if necessary. */
1780 if (DECR_PC_AFTER_BREAK
)
1781 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
1783 /* Throw away the SIGTRAP. */
1790 /* Select one LWP out of those that have events pending. */
1793 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
1796 int random_selector
;
1797 struct lwp_info
*event_lp
;
1799 /* Record the wait status for the original LWP. */
1800 (*orig_lp
)->status
= *status
;
1802 /* Give preference to any LWP that is being single-stepped. */
1803 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
1804 if (event_lp
!= NULL
)
1806 if (debug_linux_nat
)
1807 fprintf_unfiltered (gdb_stdlog
,
1808 "SEL: Select single-step %s\n",
1809 target_pid_to_str (event_lp
->ptid
));
1813 /* No single-stepping LWP. Select one at random, out of those
1814 which have had SIGTRAP events. */
1816 /* First see how many SIGTRAP events we have. */
1817 iterate_over_lwps (count_events_callback
, &num_events
);
1819 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1820 random_selector
= (int)
1821 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1823 if (debug_linux_nat
&& num_events
> 1)
1824 fprintf_unfiltered (gdb_stdlog
,
1825 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1826 num_events
, random_selector
);
1828 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
1832 if (event_lp
!= NULL
)
1834 /* Switch the event LWP. */
1835 *orig_lp
= event_lp
;
1836 *status
= event_lp
->status
;
1839 /* Flush the wait status for the event LWP. */
1840 (*orig_lp
)->status
= 0;
1843 /* Return non-zero if LP has been resumed. */
1846 resumed_callback (struct lwp_info
*lp
, void *data
)
1851 /* Local mourn_inferior -- we need to override mourn_inferior
1852 so that we can do something clever if one of several forks
1856 child_mourn_inferior (void)
1860 if (! forks_exist_p ())
1862 /* Normal case, no other forks available. */
1863 super_mourn_inferior ();
1868 /* Multi-fork case. The current inferior_ptid has exited, but
1869 there are other viable forks to debug. Delete the exiting
1870 one and context-switch to the first available. */
1871 linux_fork_mourn_inferior ();
1875 /* We need to override child_wait to support attaching to cloned
1876 processes, since a normal wait (as done by the default version)
1877 ignores those processes. */
1879 /* Wait for child PTID to do something. Return id of the child,
1880 minus_one_ptid in case of error; store status into *OURSTATUS. */
1883 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1889 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1893 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1894 attached process. */
1897 pid
= my_waitpid (GET_PID (ptid
), &status
, 0);
1898 if (pid
== -1 && errno
== ECHILD
)
1899 /* Try again with __WCLONE to check cloned processes. */
1900 pid
= my_waitpid (GET_PID (ptid
), &status
, __WCLONE
);
1902 if (debug_linux_nat
)
1904 fprintf_unfiltered (gdb_stdlog
,
1905 "CW: waitpid %ld received %s\n",
1906 (long) pid
, status_to_str (status
));
1911 /* Make sure we don't report an event for the exit of the
1912 original program, if we've detached from it. */
1913 if (pid
!= -1 && !WIFSTOPPED (status
) && pid
!= GET_PID (inferior_ptid
))
1919 /* Check for stop events reported by a process we didn't already
1920 know about - in this case, anything other than inferior_ptid.
1922 If we're expecting to receive stopped processes after fork,
1923 vfork, and clone events, then we'll just add the new one to
1924 our list and go back to waiting for the event to be reported
1925 - the stopped process might be returned from waitpid before
1926 or after the event is. If we want to handle debugging of
1927 CLONE_PTRACE processes we need to do more here, i.e. switch
1928 to multi-threaded mode. */
1929 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
1930 && pid
!= GET_PID (inferior_ptid
))
1932 linux_record_stopped_pid (pid
);
1937 /* Handle GNU/Linux's extended waitstatus for trace events. */
1938 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
1939 && status
>> 16 != 0)
1941 linux_handle_extended_wait (pid
, status
, ourstatus
);
1943 /* If we see a clone event, detach the child, and don't
1944 report the event. It would be nice to offer some way to
1945 switch into a non-thread-db based threaded mode at this
1947 if (ourstatus
->kind
== TARGET_WAITKIND_SPURIOUS
)
1949 ptrace (PTRACE_DETACH
, ourstatus
->value
.related_pid
, 0, 0);
1950 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1951 ptrace (PTRACE_CONT
, pid
, 0, 0);
1957 clear_sigio_trap ();
1958 clear_sigint_trap ();
1960 while (pid
== -1 && save_errno
== EINTR
);
1964 warning (_("Child process unexpectedly missing: %s"),
1965 safe_strerror (errno
));
1967 /* Claim it exited with unknown signal. */
1968 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1969 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1970 return minus_one_ptid
;
1973 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1974 store_waitstatus (ourstatus
, status
);
1976 return pid_to_ptid (pid
);
1979 /* Stop an active thread, verify it still exists, then resume it. */
1982 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
1984 struct lwp_info
*ptr
;
1986 if (!lp
->stopped
&& !lp
->signalled
)
1988 stop_callback (lp
, NULL
);
1989 stop_wait_callback (lp
, NULL
);
1990 /* Resume if the lwp still exists. */
1991 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
1994 resume_callback (lp
, NULL
);
1995 resume_set_callback (lp
, NULL
);
2002 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
2004 struct lwp_info
*lp
= NULL
;
2007 pid_t pid
= PIDGET (ptid
);
2008 sigset_t flush_mask
;
2010 sigemptyset (&flush_mask
);
2012 /* Make sure SIGCHLD is blocked. */
2013 if (!sigismember (&blocked_mask
, SIGCHLD
))
2015 sigaddset (&blocked_mask
, SIGCHLD
);
2016 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
2021 /* Make sure there is at least one LWP that has been resumed, at
2022 least if there are any LWPs at all. */
2023 gdb_assert (num_lwps
== 0 || iterate_over_lwps (resumed_callback
, NULL
));
2025 /* First check if there is a LWP with a wait status pending. */
2028 /* Any LWP that's been resumed will do. */
2029 lp
= iterate_over_lwps (status_callback
, NULL
);
2032 status
= lp
->status
;
2035 if (debug_linux_nat
&& status
)
2036 fprintf_unfiltered (gdb_stdlog
,
2037 "LLW: Using pending wait status %s for %s.\n",
2038 status_to_str (status
),
2039 target_pid_to_str (lp
->ptid
));
2042 /* But if we don't fine one, we'll have to wait, and check both
2043 cloned and uncloned processes. We start with the cloned
2045 options
= __WCLONE
| WNOHANG
;
2047 else if (is_lwp (ptid
))
2049 if (debug_linux_nat
)
2050 fprintf_unfiltered (gdb_stdlog
,
2051 "LLW: Waiting for specific LWP %s.\n",
2052 target_pid_to_str (ptid
));
2054 /* We have a specific LWP to check. */
2055 lp
= find_lwp_pid (ptid
);
2057 status
= lp
->status
;
2060 if (debug_linux_nat
&& status
)
2061 fprintf_unfiltered (gdb_stdlog
,
2062 "LLW: Using pending wait status %s for %s.\n",
2063 status_to_str (status
),
2064 target_pid_to_str (lp
->ptid
));
2066 /* If we have to wait, take into account whether PID is a cloned
2067 process or not. And we have to convert it to something that
2068 the layer beneath us can understand. */
2069 options
= lp
->cloned
? __WCLONE
: 0;
2070 pid
= GET_LWP (ptid
);
2073 if (status
&& lp
->signalled
)
2075 /* A pending SIGSTOP may interfere with the normal stream of
2076 events. In a typical case where interference is a problem,
2077 we have a SIGSTOP signal pending for LWP A while
2078 single-stepping it, encounter an event in LWP B, and take the
2079 pending SIGSTOP while trying to stop LWP A. After processing
2080 the event in LWP B, LWP A is continued, and we'll never see
2081 the SIGTRAP associated with the last time we were
2082 single-stepping LWP A. */
2084 /* Resume the thread. It should halt immediately returning the
2086 registers_changed ();
2087 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2088 lp
->step
, TARGET_SIGNAL_0
);
2089 if (debug_linux_nat
)
2090 fprintf_unfiltered (gdb_stdlog
,
2091 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2092 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2093 target_pid_to_str (lp
->ptid
));
2095 gdb_assert (lp
->resumed
);
2097 /* This should catch the pending SIGSTOP. */
2098 stop_wait_callback (lp
, NULL
);
2101 set_sigint_trap (); /* Causes SIGINT to be passed on to the
2102 attached process. */
2109 lwpid
= my_waitpid (pid
, &status
, options
);
2112 gdb_assert (pid
== -1 || lwpid
== pid
);
2114 if (debug_linux_nat
)
2116 fprintf_unfiltered (gdb_stdlog
,
2117 "LLW: waitpid %ld received %s\n",
2118 (long) lwpid
, status_to_str (status
));
2121 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2123 /* Check for stop events reported by a process we didn't
2124 already know about - anything not already in our LWP
2127 If we're expecting to receive stopped processes after
2128 fork, vfork, and clone events, then we'll just add the
2129 new one to our list and go back to waiting for the event
2130 to be reported - the stopped process might be returned
2131 from waitpid before or after the event is. */
2132 if (WIFSTOPPED (status
) && !lp
)
2134 linux_record_stopped_pid (lwpid
);
2139 /* Make sure we don't report an event for the exit of an LWP not in
2140 our list, i.e. not part of the current process. This can happen
2141 if we detach from a program we original forked and then it
2143 if (!WIFSTOPPED (status
) && !lp
)
2149 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2150 CLONE_PTRACE processes which do not use the thread library -
2151 otherwise we wouldn't find the new LWP this way. That doesn't
2152 currently work, and the following code is currently unreachable
2153 due to the two blocks above. If it's fixed some day, this code
2154 should be broken out into a function so that we can also pick up
2155 LWPs from the new interface. */
2158 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
2159 if (options
& __WCLONE
)
2164 gdb_assert (WIFSTOPPED (status
)
2165 && WSTOPSIG (status
) == SIGSTOP
);
2168 if (!in_thread_list (inferior_ptid
))
2170 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2171 GET_PID (inferior_ptid
));
2172 add_thread (inferior_ptid
);
2175 add_thread (lp
->ptid
);
2176 printf_unfiltered (_("[New %s]\n"),
2177 target_pid_to_str (lp
->ptid
));
2181 /* Handle GNU/Linux's extended waitstatus for trace events. */
2182 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2184 if (debug_linux_nat
)
2185 fprintf_unfiltered (gdb_stdlog
,
2186 "LLW: Handling extended status 0x%06x\n",
2188 if (linux_nat_handle_extended (lp
, status
))
2195 /* Check if the thread has exited. */
2196 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
2198 /* If this is the main thread, we must stop all threads and
2199 verify if they are still alive. This is because in the nptl
2200 thread model, there is no signal issued for exiting LWPs
2201 other than the main thread. We only get the main thread
2202 exit signal once all child threads have already exited.
2203 If we stop all the threads and use the stop_wait_callback
2204 to check if they have exited we can determine whether this
2205 signal should be ignored or whether it means the end of the
2206 debugged application, regardless of which threading model
2208 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2211 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2214 if (debug_linux_nat
)
2215 fprintf_unfiltered (gdb_stdlog
,
2216 "LLW: %s exited.\n",
2217 target_pid_to_str (lp
->ptid
));
2221 /* If there is at least one more LWP, then the exit signal
2222 was not the end of the debugged application and should be
2226 /* Make sure there is at least one thread running. */
2227 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2229 /* Discard the event. */
2235 /* Check if the current LWP has previously exited. In the nptl
2236 thread model, LWPs other than the main thread do not issue
2237 signals when they exit so we must check whenever the thread
2238 has stopped. A similar check is made in stop_wait_callback(). */
2239 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2241 if (debug_linux_nat
)
2242 fprintf_unfiltered (gdb_stdlog
,
2243 "LLW: %s exited.\n",
2244 target_pid_to_str (lp
->ptid
));
2248 /* Make sure there is at least one thread running. */
2249 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2251 /* Discard the event. */
2256 /* Make sure we don't report a SIGSTOP that we sent
2257 ourselves in an attempt to stop an LWP. */
2259 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2261 if (debug_linux_nat
)
2262 fprintf_unfiltered (gdb_stdlog
,
2263 "LLW: Delayed SIGSTOP caught for %s.\n",
2264 target_pid_to_str (lp
->ptid
));
2266 /* This is a delayed SIGSTOP. */
2269 registers_changed ();
2270 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2271 lp
->step
, TARGET_SIGNAL_0
);
2272 if (debug_linux_nat
)
2273 fprintf_unfiltered (gdb_stdlog
,
2274 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2276 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2277 target_pid_to_str (lp
->ptid
));
2280 gdb_assert (lp
->resumed
);
2282 /* Discard the event. */
2292 /* Alternate between checking cloned and uncloned processes. */
2293 options
^= __WCLONE
;
2295 /* And suspend every time we have checked both. */
2296 if (options
& __WCLONE
)
2297 sigsuspend (&suspend_mask
);
2300 /* We shouldn't end up here unless we want to try again. */
2301 gdb_assert (status
== 0);
2304 clear_sigio_trap ();
2305 clear_sigint_trap ();
2309 /* Don't report signals that GDB isn't interested in, such as
2310 signals that are neither printed nor stopped upon. Stopping all
2311 threads can be a bit time-consuming so if we want decent
2312 performance with heavily multi-threaded programs, especially when
2313 they're using a high frequency timer, we'd better avoid it if we
2316 if (WIFSTOPPED (status
))
2318 int signo
= target_signal_from_host (WSTOPSIG (status
));
2320 if (signal_stop_state (signo
) == 0
2321 && signal_print_state (signo
) == 0
2322 && signal_pass_state (signo
) == 1)
2324 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2325 here? It is not clear we should. GDB may not expect
2326 other threads to run. On the other hand, not resuming
2327 newly attached threads may cause an unwanted delay in
2328 getting them running. */
2329 registers_changed ();
2330 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2332 if (debug_linux_nat
)
2333 fprintf_unfiltered (gdb_stdlog
,
2334 "LLW: %s %s, %s (preempt 'handle')\n",
2336 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2337 target_pid_to_str (lp
->ptid
),
2338 signo
? strsignal (signo
) : "0");
2344 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2346 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2347 forwarded to the entire process group, that is, all LWP's
2348 will receive it. Since we only want to report it once,
2349 we try to flush it from all LWPs except this one. */
2350 sigaddset (&flush_mask
, SIGINT
);
2354 /* This LWP is stopped now. */
2357 if (debug_linux_nat
)
2358 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2359 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2361 /* Now stop all other LWP's ... */
2362 iterate_over_lwps (stop_callback
, NULL
);
2364 /* ... and wait until all of them have reported back that they're no
2366 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2367 iterate_over_lwps (flush_callback
, &flush_mask
);
2369 /* If we're not waiting for a specific LWP, choose an event LWP from
2370 among those that have had events. Giving equal priority to all
2371 LWPs that have had events helps prevent starvation. */
2373 select_event_lwp (&lp
, &status
);
2375 /* Now that we've selected our final event LWP, cancel any
2376 breakpoints in other LWPs that have hit a GDB breakpoint. See
2377 the comment in cancel_breakpoints_callback to find out why. */
2378 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2380 /* If we're not running in "threaded" mode, we'll report the bare
2383 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2385 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2386 if (debug_linux_nat
)
2387 fprintf_unfiltered (gdb_stdlog
,
2388 "LLW: trap_ptid is %s.\n",
2389 target_pid_to_str (trap_ptid
));
2392 trap_ptid
= null_ptid
;
2394 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2396 *ourstatus
= lp
->waitstatus
;
2397 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2400 store_waitstatus (ourstatus
, status
);
2402 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2406 kill_callback (struct lwp_info
*lp
, void *data
)
2409 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2410 if (debug_linux_nat
)
2411 fprintf_unfiltered (gdb_stdlog
,
2412 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2413 target_pid_to_str (lp
->ptid
),
2414 errno
? safe_strerror (errno
) : "OK");
2420 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2424 /* We must make sure that there are no pending events (delayed
2425 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2426 program doesn't interfere with any following debugging session. */
2428 /* For cloned processes we must check both with __WCLONE and
2429 without, since the exit status of a cloned process isn't reported
2435 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2436 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2438 fprintf_unfiltered (gdb_stdlog
,
2439 "KWC: wait %s received unknown.\n",
2440 target_pid_to_str (lp
->ptid
));
2443 while (pid
== GET_LWP (lp
->ptid
));
2445 gdb_assert (pid
== -1 && errno
== ECHILD
);
2450 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2451 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2453 fprintf_unfiltered (gdb_stdlog
,
2454 "KWC: wait %s received unk.\n",
2455 target_pid_to_str (lp
->ptid
));
2458 while (pid
== GET_LWP (lp
->ptid
));
2460 gdb_assert (pid
== -1 && errno
== ECHILD
);
2465 linux_nat_kill (void)
2467 /* Kill all LWP's ... */
2468 iterate_over_lwps (kill_callback
, NULL
);
2470 /* ... and wait until we've flushed all events. */
2471 iterate_over_lwps (kill_wait_callback
, NULL
);
2473 target_mourn_inferior ();
2477 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
2480 linux_ops
->to_create_inferior (exec_file
, allargs
, env
, from_tty
);
2484 linux_nat_mourn_inferior (void)
2486 trap_ptid
= null_ptid
;
2488 /* Destroy LWP info; it's no longer valid. */
2491 /* Restore the original signal mask. */
2492 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
2493 sigemptyset (&blocked_mask
);
2495 linux_ops
->to_mourn_inferior ();
2499 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2500 const char *annex
, gdb_byte
*readbuf
,
2501 const gdb_byte
*writebuf
,
2502 ULONGEST offset
, LONGEST len
)
2504 struct cleanup
*old_chain
= save_inferior_ptid ();
2507 if (is_lwp (inferior_ptid
))
2508 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2510 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
2513 do_cleanups (old_chain
);
2518 linux_nat_thread_alive (ptid_t ptid
)
2520 gdb_assert (is_lwp (ptid
));
2523 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2524 if (debug_linux_nat
)
2525 fprintf_unfiltered (gdb_stdlog
,
2526 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2527 target_pid_to_str (ptid
),
2528 errno
? safe_strerror (errno
) : "OK");
2536 linux_nat_pid_to_str (ptid_t ptid
)
2538 static char buf
[64];
2542 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2546 return normal_pid_to_str (ptid
);
2550 linux_nat_fetch_registers (int regnum
)
2552 /* to_fetch_registers will honor the LWP ID, so we can use it directly. */
2553 linux_ops
->to_fetch_registers (regnum
);
2557 linux_nat_store_registers (int regnum
)
2559 /* to_store_registers will honor the LWP ID, so we can use it directly. */
2560 linux_ops
->to_store_registers (regnum
);
2564 linux_nat_child_post_startup_inferior (ptid_t ptid
)
2566 linux_ops
->to_post_startup_inferior (ptid
);
2570 init_linux_nat_ops (void)
2573 linux_nat_ops
.to_open
= linux_nat_open
;
2575 linux_nat_ops
.to_shortname
= "lwp-layer";
2576 linux_nat_ops
.to_longname
= "lwp-layer";
2577 linux_nat_ops
.to_doc
= "Low level threads support (LWP layer)";
2578 linux_nat_ops
.to_attach
= linux_nat_attach
;
2579 linux_nat_ops
.to_detach
= linux_nat_detach
;
2580 linux_nat_ops
.to_resume
= linux_nat_resume
;
2581 linux_nat_ops
.to_wait
= linux_nat_wait
;
2582 linux_nat_ops
.to_fetch_registers
= linux_nat_fetch_registers
;
2583 linux_nat_ops
.to_store_registers
= linux_nat_store_registers
;
2584 linux_nat_ops
.to_xfer_partial
= linux_nat_xfer_partial
;
2585 linux_nat_ops
.to_kill
= linux_nat_kill
;
2586 linux_nat_ops
.to_create_inferior
= linux_nat_create_inferior
;
2587 linux_nat_ops
.to_mourn_inferior
= linux_nat_mourn_inferior
;
2588 linux_nat_ops
.to_thread_alive
= linux_nat_thread_alive
;
2589 linux_nat_ops
.to_pid_to_str
= linux_nat_pid_to_str
;
2590 linux_nat_ops
.to_post_startup_inferior
2591 = linux_nat_child_post_startup_inferior
;
2592 linux_nat_ops
.to_post_attach
= child_post_attach
;
2593 linux_nat_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
2594 linux_nat_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
2595 linux_nat_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
2597 linux_nat_ops
.to_stratum
= thread_stratum
;
2598 linux_nat_ops
.to_has_thread_control
= tc_schedlock
;
2599 linux_nat_ops
.to_magic
= OPS_MAGIC
;
2603 sigchld_handler (int signo
)
2605 /* Do nothing. The only reason for this handler is that it allows
2606 us to use sigsuspend in linux_nat_wait above to wait for the
2607 arrival of a SIGCHLD. */
2610 /* Accepts an integer PID; Returns a string representing a file that
2611 can be opened to get the symbols for the child process. */
2614 child_pid_to_exec_file (int pid
)
2616 char *name1
, *name2
;
2618 name1
= xmalloc (MAXPATHLEN
);
2619 name2
= xmalloc (MAXPATHLEN
);
2620 make_cleanup (xfree
, name1
);
2621 make_cleanup (xfree
, name2
);
2622 memset (name2
, 0, MAXPATHLEN
);
2624 sprintf (name1
, "/proc/%d/exe", pid
);
2625 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
2631 /* Service function for corefiles and info proc. */
2634 read_mapping (FILE *mapfile
,
2639 char *device
, long long *inode
, char *filename
)
2641 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
2642 addr
, endaddr
, permissions
, offset
, device
, inode
);
2645 if (ret
> 0 && ret
!= EOF
)
2647 /* Eat everything up to EOL for the filename. This will prevent
2648 weird filenames (such as one with embedded whitespace) from
2649 confusing this code. It also makes this code more robust in
2650 respect to annotations the kernel may add after the filename.
2652 Note the filename is used for informational purposes
2654 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
2657 return (ret
!= 0 && ret
!= EOF
);
2660 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2661 regions in the inferior for a corefile. */
2664 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
2666 int, int, int, void *), void *obfd
)
2668 long long pid
= PIDGET (inferior_ptid
);
2669 char mapsfilename
[MAXPATHLEN
];
2671 long long addr
, endaddr
, size
, offset
, inode
;
2672 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2673 int read
, write
, exec
;
2676 /* Compose the filename for the /proc memory map, and open it. */
2677 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
2678 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
2679 error (_("Could not open %s."), mapsfilename
);
2682 fprintf_filtered (gdb_stdout
,
2683 "Reading memory regions from %s\n", mapsfilename
);
2685 /* Now iterate until end-of-file. */
2686 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
2687 &offset
, &device
[0], &inode
, &filename
[0]))
2689 size
= endaddr
- addr
;
2691 /* Get the segment's permissions. */
2692 read
= (strchr (permissions
, 'r') != 0);
2693 write
= (strchr (permissions
, 'w') != 0);
2694 exec
= (strchr (permissions
, 'x') != 0);
2698 fprintf_filtered (gdb_stdout
,
2699 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2700 size
, paddr_nz (addr
),
2702 write
? 'w' : ' ', exec
? 'x' : ' ');
2703 if (filename
&& filename
[0])
2704 fprintf_filtered (gdb_stdout
, " for %s", filename
);
2705 fprintf_filtered (gdb_stdout
, "\n");
2708 /* Invoke the callback function to create the corefile
2710 func (addr
, size
, read
, write
, exec
, obfd
);
2716 /* Records the thread's register state for the corefile note
2720 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
2721 char *note_data
, int *note_size
)
2723 gdb_gregset_t gregs
;
2724 gdb_fpregset_t fpregs
;
2725 #ifdef FILL_FPXREGSET
2726 gdb_fpxregset_t fpxregs
;
2728 unsigned long lwp
= ptid_get_lwp (ptid
);
2730 fill_gregset (&gregs
, -1);
2731 note_data
= (char *) elfcore_write_prstatus (obfd
,
2735 stop_signal
, &gregs
);
2737 fill_fpregset (&fpregs
, -1);
2738 note_data
= (char *) elfcore_write_prfpreg (obfd
,
2741 &fpregs
, sizeof (fpregs
));
2742 #ifdef FILL_FPXREGSET
2743 fill_fpxregset (&fpxregs
, -1);
2744 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
2747 &fpxregs
, sizeof (fpxregs
));
2752 struct linux_nat_corefile_thread_data
2760 /* Called by gdbthread.c once per thread. Records the thread's
2761 register state for the corefile note section. */
2764 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
2766 struct linux_nat_corefile_thread_data
*args
= data
;
2767 ptid_t saved_ptid
= inferior_ptid
;
2769 inferior_ptid
= ti
->ptid
;
2770 registers_changed ();
2771 target_fetch_registers (-1); /* FIXME should not be necessary;
2772 fill_gregset should do it automatically. */
2773 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
2778 inferior_ptid
= saved_ptid
;
2779 registers_changed ();
2780 target_fetch_registers (-1); /* FIXME should not be necessary;
2781 fill_gregset should do it automatically. */
2785 /* Records the register state for the corefile note section. */
2788 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
2789 char *note_data
, int *note_size
)
2791 registers_changed ();
2792 target_fetch_registers (-1); /* FIXME should not be necessary;
2793 fill_gregset should do it automatically. */
2794 return linux_nat_do_thread_registers (obfd
,
2795 ptid_build (ptid_get_pid (inferior_ptid
),
2796 ptid_get_pid (inferior_ptid
),
2798 note_data
, note_size
);
2802 /* Fills the "to_make_corefile_note" target vector. Builds the note
2803 section for a corefile, and returns it in a malloc buffer. */
2806 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
2808 struct linux_nat_corefile_thread_data thread_args
;
2809 struct cleanup
*old_chain
;
2810 char fname
[16] = { '\0' };
2811 char psargs
[80] = { '\0' };
2812 char *note_data
= NULL
;
2813 ptid_t current_ptid
= inferior_ptid
;
2817 if (get_exec_file (0))
2819 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
2820 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
2821 if (get_inferior_args ())
2823 strncat (psargs
, " ", sizeof (psargs
) - strlen (psargs
));
2824 strncat (psargs
, get_inferior_args (),
2825 sizeof (psargs
) - strlen (psargs
));
2827 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
2829 note_size
, fname
, psargs
);
2832 /* Dump information for threads. */
2833 thread_args
.obfd
= obfd
;
2834 thread_args
.note_data
= note_data
;
2835 thread_args
.note_size
= note_size
;
2836 thread_args
.num_notes
= 0;
2837 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
2838 if (thread_args
.num_notes
== 0)
2840 /* iterate_over_threads didn't come up with any threads; just
2841 use inferior_ptid. */
2842 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
2843 note_data
, note_size
);
2847 note_data
= thread_args
.note_data
;
2850 auxv_len
= target_auxv_read (¤t_target
, &auxv
);
2853 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
2854 "CORE", NT_AUXV
, auxv
, auxv_len
);
2858 make_cleanup (xfree
, note_data
);
2862 /* Implement the "info proc" command. */
2865 linux_nat_info_proc_cmd (char *args
, int from_tty
)
2867 long long pid
= PIDGET (inferior_ptid
);
2870 char buffer
[MAXPATHLEN
];
2871 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
2884 /* Break up 'args' into an argv array. */
2885 if ((argv
= buildargv (args
)) == NULL
)
2888 make_cleanup_freeargv (argv
);
2890 while (argv
!= NULL
&& *argv
!= NULL
)
2892 if (isdigit (argv
[0][0]))
2894 pid
= strtoul (argv
[0], NULL
, 10);
2896 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
2900 else if (strcmp (argv
[0], "status") == 0)
2904 else if (strcmp (argv
[0], "stat") == 0)
2908 else if (strcmp (argv
[0], "cmd") == 0)
2912 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
2916 else if (strcmp (argv
[0], "cwd") == 0)
2920 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
2926 /* [...] (future options here) */
2931 error (_("No current process: you must name one."));
2933 sprintf (fname1
, "/proc/%lld", pid
);
2934 if (stat (fname1
, &dummy
) != 0)
2935 error (_("No /proc directory: '%s'"), fname1
);
2937 printf_filtered (_("process %lld\n"), pid
);
2938 if (cmdline_f
|| all
)
2940 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
2941 if ((procfile
= fopen (fname1
, "r")) > 0)
2943 fgets (buffer
, sizeof (buffer
), procfile
);
2944 printf_filtered ("cmdline = '%s'\n", buffer
);
2948 warning (_("unable to open /proc file '%s'"), fname1
);
2952 sprintf (fname1
, "/proc/%lld/cwd", pid
);
2953 memset (fname2
, 0, sizeof (fname2
));
2954 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2955 printf_filtered ("cwd = '%s'\n", fname2
);
2957 warning (_("unable to read link '%s'"), fname1
);
2961 sprintf (fname1
, "/proc/%lld/exe", pid
);
2962 memset (fname2
, 0, sizeof (fname2
));
2963 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2964 printf_filtered ("exe = '%s'\n", fname2
);
2966 warning (_("unable to read link '%s'"), fname1
);
2968 if (mappings_f
|| all
)
2970 sprintf (fname1
, "/proc/%lld/maps", pid
);
2971 if ((procfile
= fopen (fname1
, "r")) > 0)
2973 long long addr
, endaddr
, size
, offset
, inode
;
2974 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2976 printf_filtered (_("Mapped address spaces:\n\n"));
2977 if (TARGET_ADDR_BIT
== 32)
2979 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2982 " Size", " Offset", "objfile");
2986 printf_filtered (" %18s %18s %10s %10s %7s\n",
2989 " Size", " Offset", "objfile");
2992 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
2993 &offset
, &device
[0], &inode
, &filename
[0]))
2995 size
= endaddr
- addr
;
2997 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2998 calls here (and possibly above) should be abstracted
2999 out into their own functions? Andrew suggests using
3000 a generic local_address_string instead to print out
3001 the addresses; that makes sense to me, too. */
3003 if (TARGET_ADDR_BIT
== 32)
3005 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3006 (unsigned long) addr
, /* FIXME: pr_addr */
3007 (unsigned long) endaddr
,
3009 (unsigned int) offset
,
3010 filename
[0] ? filename
: "");
3014 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3015 (unsigned long) addr
, /* FIXME: pr_addr */
3016 (unsigned long) endaddr
,
3018 (unsigned int) offset
,
3019 filename
[0] ? filename
: "");
3026 warning (_("unable to open /proc file '%s'"), fname1
);
3028 if (status_f
|| all
)
3030 sprintf (fname1
, "/proc/%lld/status", pid
);
3031 if ((procfile
= fopen (fname1
, "r")) > 0)
3033 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
3034 puts_filtered (buffer
);
3038 warning (_("unable to open /proc file '%s'"), fname1
);
3042 sprintf (fname1
, "/proc/%lld/stat", pid
);
3043 if ((procfile
= fopen (fname1
, "r")) > 0)
3048 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3049 printf_filtered (_("Process: %d\n"), itmp
);
3050 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
3051 printf_filtered (_("Exec file: %s\n"), buffer
);
3052 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
3053 printf_filtered (_("State: %c\n"), ctmp
);
3054 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3055 printf_filtered (_("Parent process: %d\n"), itmp
);
3056 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3057 printf_filtered (_("Process group: %d\n"), itmp
);
3058 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3059 printf_filtered (_("Session id: %d\n"), itmp
);
3060 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3061 printf_filtered (_("TTY: %d\n"), itmp
);
3062 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3063 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
3064 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3065 printf_filtered (_("Flags: 0x%x\n"), itmp
);
3066 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3067 printf_filtered (_("Minor faults (no memory page): %u\n"),
3068 (unsigned int) itmp
);
3069 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3070 printf_filtered (_("Minor faults, children: %u\n"),
3071 (unsigned int) itmp
);
3072 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3073 printf_filtered (_("Major faults (memory page faults): %u\n"),
3074 (unsigned int) itmp
);
3075 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3076 printf_filtered (_("Major faults, children: %u\n"),
3077 (unsigned int) itmp
);
3078 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3079 printf_filtered ("utime: %d\n", itmp
);
3080 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3081 printf_filtered ("stime: %d\n", itmp
);
3082 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3083 printf_filtered ("utime, children: %d\n", itmp
);
3084 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3085 printf_filtered ("stime, children: %d\n", itmp
);
3086 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3087 printf_filtered (_("jiffies remaining in current time slice: %d\n"),
3089 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3090 printf_filtered ("'nice' value: %d\n", itmp
);
3091 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3092 printf_filtered (_("jiffies until next timeout: %u\n"),
3093 (unsigned int) itmp
);
3094 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3095 printf_filtered ("jiffies until next SIGALRM: %u\n",
3096 (unsigned int) itmp
);
3097 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3098 printf_filtered (_("start time (jiffies since system boot): %d\n"),
3100 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3101 printf_filtered (_("Virtual memory size: %u\n"),
3102 (unsigned int) itmp
);
3103 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3104 printf_filtered (_("Resident set size: %u\n"), (unsigned int) itmp
);
3105 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3106 printf_filtered ("rlim: %u\n", (unsigned int) itmp
);
3107 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3108 printf_filtered (_("Start of text: 0x%x\n"), itmp
);
3109 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3110 printf_filtered (_("End of text: 0x%x\n"), itmp
);
3111 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3112 printf_filtered (_("Start of stack: 0x%x\n"), itmp
);
3113 #if 0 /* Don't know how architecture-dependent the rest is...
3114 Anyway the signal bitmap info is available from "status". */
3115 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
3116 printf_filtered (_("Kernel stack pointer: 0x%x\n"), itmp
);
3117 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
3118 printf_filtered (_("Kernel instr pointer: 0x%x\n"), itmp
);
3119 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3120 printf_filtered (_("Pending signals bitmap: 0x%x\n"), itmp
);
3121 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3122 printf_filtered (_("Blocked signals bitmap: 0x%x\n"), itmp
);
3123 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3124 printf_filtered (_("Ignored signals bitmap: 0x%x\n"), itmp
);
3125 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3126 printf_filtered (_("Catched signals bitmap: 0x%x\n"), itmp
);
3127 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
3128 printf_filtered (_("wchan (system call): 0x%x\n"), itmp
);
3133 warning (_("unable to open /proc file '%s'"), fname1
);
3137 /* Implement the to_xfer_partial interface for memory reads using the /proc
3138 filesystem. Because we can use a single read() call for /proc, this
3139 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3140 but it doesn't support writes. */
3143 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3144 const char *annex
, gdb_byte
*readbuf
,
3145 const gdb_byte
*writebuf
,
3146 ULONGEST offset
, LONGEST len
)
3152 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
3155 /* Don't bother for one word. */
3156 if (len
< 3 * sizeof (long))
3159 /* We could keep this file open and cache it - possibly one per
3160 thread. That requires some juggling, but is even faster. */
3161 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
3162 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
3166 /* If pread64 is available, use it. It's faster if the kernel
3167 supports it (only one syscall), and it's 64-bit safe even on
3168 32-bit platforms (for instance, SPARC debugging a SPARC64
3171 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
3173 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
3183 /* Parse LINE as a signal set and add its set bits to SIGS. */
3186 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
3188 int len
= strlen (line
) - 1;
3192 if (line
[len
] != '\n')
3193 error (_("Could not parse signal set: %s"), line
);
3201 if (*p
>= '0' && *p
<= '9')
3203 else if (*p
>= 'a' && *p
<= 'f')
3204 digit
= *p
- 'a' + 10;
3206 error (_("Could not parse signal set: %s"), line
);
3211 sigaddset (sigs
, signum
+ 1);
3213 sigaddset (sigs
, signum
+ 2);
3215 sigaddset (sigs
, signum
+ 3);
3217 sigaddset (sigs
, signum
+ 4);
3223 /* Find process PID's pending signals from /proc/pid/status and set
3227 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3230 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3233 sigemptyset (pending
);
3234 sigemptyset (blocked
);
3235 sigemptyset (ignored
);
3236 sprintf (fname
, "/proc/%d/status", pid
);
3237 procfile
= fopen (fname
, "r");
3238 if (procfile
== NULL
)
3239 error (_("Could not open %s"), fname
);
3241 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3243 /* Normal queued signals are on the SigPnd line in the status
3244 file. However, 2.6 kernels also have a "shared" pending
3245 queue for delivering signals to a thread group, so check for
3248 Unfortunately some Red Hat kernels include the shared pending
3249 queue but not the ShdPnd status field. */
3251 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3252 add_line_to_sigset (buffer
+ 8, pending
);
3253 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3254 add_line_to_sigset (buffer
+ 8, pending
);
3255 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3256 add_line_to_sigset (buffer
+ 8, blocked
);
3257 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3258 add_line_to_sigset (buffer
+ 8, ignored
);
3265 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3266 const char *annex
, gdb_byte
*readbuf
,
3267 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3271 if (object
== TARGET_OBJECT_AUXV
)
3272 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
3275 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3280 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3284 #ifndef FETCH_INFERIOR_REGISTERS
3286 /* Return the address in the core dump or inferior of register
3290 linux_register_u_offset (int regno
)
3292 /* FIXME drow/2005-09-04: The hardcoded use of register_addr should go
3293 away. This requires disentangling the various definitions of it
3294 (particularly alpha-nat.c's). */
3295 return register_addr (regno
, 0);
3300 /* Create a prototype generic Linux target. The client can override
3301 it with local methods. */
3306 struct target_ops
*t
;
3308 #ifdef FETCH_INFERIOR_REGISTERS
3309 t
= inf_ptrace_target ();
3311 t
= inf_ptrace_trad_target (linux_register_u_offset
);
3313 t
->to_wait
= child_wait
;
3314 t
->to_kill
= kill_inferior
;
3315 t
->to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
3316 t
->to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
3317 t
->to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
3318 t
->to_pid_to_exec_file
= child_pid_to_exec_file
;
3319 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
3320 t
->to_post_attach
= child_post_attach
;
3321 t
->to_follow_fork
= child_follow_fork
;
3322 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
3323 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3325 super_xfer_partial
= t
->to_xfer_partial
;
3326 t
->to_xfer_partial
= linux_xfer_partial
;
3328 super_mourn_inferior
= t
->to_mourn_inferior
;
3329 t
->to_mourn_inferior
= child_mourn_inferior
;
3336 _initialize_linux_nat (void)
3338 struct sigaction action
;
3339 extern void thread_db_init (struct target_ops
*);
3341 add_info ("proc", linux_nat_info_proc_cmd
, _("\
3342 Show /proc process information about any running process.\n\
3343 Specify any process id, or use the program being debugged by default.\n\
3344 Specify any of the following keywords for detailed info:\n\
3345 mappings -- list of mapped memory regions.\n\
3346 stat -- list a bunch of random process info.\n\
3347 status -- list a different bunch of random process info.\n\
3348 all -- list all available /proc info."));
3350 init_linux_nat_ops ();
3351 add_target (&linux_nat_ops
);
3352 thread_db_init (&linux_nat_ops
);
3354 /* Save the original signal mask. */
3355 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
3357 action
.sa_handler
= sigchld_handler
;
3358 sigemptyset (&action
.sa_mask
);
3359 action
.sa_flags
= SA_RESTART
;
3360 sigaction (SIGCHLD
, &action
, NULL
);
3362 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3363 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
3364 sigdelset (&suspend_mask
, SIGCHLD
);
3366 sigemptyset (&blocked_mask
);
3368 add_setshow_zinteger_cmd ("lin-lwp", no_class
, &debug_linux_nat
, _("\
3369 Set debugging of GNU/Linux lwp module."), _("\
3370 Show debugging of GNU/Linux lwp module."), _("\
3371 Enables printf debugging output."),
3373 show_debug_linux_nat
,
3374 &setdebuglist
, &showdebuglist
);
3378 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3379 the GNU/Linux Threads library and therefore doesn't really belong
3382 /* Read variable NAME in the target and return its value if found.
3383 Otherwise return zero. It is assumed that the type of the variable
3387 get_signo (const char *name
)
3389 struct minimal_symbol
*ms
;
3392 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
3396 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
3397 sizeof (signo
)) != 0)
3403 /* Return the set of signals used by the threads library in *SET. */
3406 lin_thread_get_thread_signals (sigset_t
*set
)
3408 struct sigaction action
;
3409 int restart
, cancel
;
3413 restart
= get_signo ("__pthread_sig_restart");
3414 cancel
= get_signo ("__pthread_sig_cancel");
3416 /* LinuxThreads normally uses the first two RT signals, but in some legacy
3417 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
3418 not provide any way for the debugger to query the signal numbers -
3419 fortunately they don't change! */
3422 restart
= __SIGRTMIN
;
3425 cancel
= __SIGRTMIN
+ 1;
3427 sigaddset (set
, restart
);
3428 sigaddset (set
, cancel
);
3430 /* The GNU/Linux Threads library makes terminating threads send a
3431 special "cancel" signal instead of SIGCHLD. Make sure we catch
3432 those (to prevent them from terminating GDB itself, which is
3433 likely to be their default action) and treat them the same way as
3436 action
.sa_handler
= sigchld_handler
;
3437 sigemptyset (&action
.sa_mask
);
3438 action
.sa_flags
= SA_RESTART
;
3439 sigaction (cancel
, &action
, NULL
);
3441 /* We block the "cancel" signal throughout this code ... */
3442 sigaddset (&blocked_mask
, cancel
);
3443 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
3445 /* ... except during a sigsuspend. */
3446 sigdelset (&suspend_mask
, cancel
);