1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "gdb_string.h"
27 #include "gdb_assert.h"
28 #ifdef HAVE_TKILL_SYSCALL
30 #include <sys/syscall.h>
32 #include <sys/ptrace.h>
33 #include "linux-nat.h"
34 #include "gdbthread.h"
37 #include "inf-ptrace.h"
39 #include <sys/param.h> /* for MAXPATHLEN */
40 #include <sys/procfs.h> /* for elf_gregset etc. */
41 #include "elf-bfd.h" /* for elfcore_write_* */
42 #include "gregset.h" /* for gregset */
43 #include "gdbcore.h" /* for get_exec_file */
44 #include <ctype.h> /* for isdigit */
45 #include "gdbthread.h" /* for struct thread_info etc. */
46 #include "gdb_stat.h" /* for struct stat */
47 #include <fcntl.h> /* for O_RDONLY */
53 /* If the system headers did not provide the constants, hard-code the normal
55 #ifndef PTRACE_EVENT_FORK
57 #define PTRACE_SETOPTIONS 0x4200
58 #define PTRACE_GETEVENTMSG 0x4201
60 /* options set using PTRACE_SETOPTIONS */
61 #define PTRACE_O_TRACESYSGOOD 0x00000001
62 #define PTRACE_O_TRACEFORK 0x00000002
63 #define PTRACE_O_TRACEVFORK 0x00000004
64 #define PTRACE_O_TRACECLONE 0x00000008
65 #define PTRACE_O_TRACEEXEC 0x00000010
66 #define PTRACE_O_TRACEVFORKDONE 0x00000020
67 #define PTRACE_O_TRACEEXIT 0x00000040
69 /* Wait extended result codes for the above trace options. */
70 #define PTRACE_EVENT_FORK 1
71 #define PTRACE_EVENT_VFORK 2
72 #define PTRACE_EVENT_CLONE 3
73 #define PTRACE_EVENT_EXEC 4
74 #define PTRACE_EVENT_VFORK_DONE 5
75 #define PTRACE_EVENT_EXIT 6
77 #endif /* PTRACE_EVENT_FORK */
79 /* We can't always assume that this flag is available, but all systems
80 with the ptrace event handlers also have __WALL, so it's safe to use
83 #define __WALL 0x40000000 /* Wait for any child. */
86 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
87 the use of the multi-threaded target. */
88 static struct target_ops
*linux_ops
;
90 /* The saved to_xfer_partial method, inherited from inf-ptrace.c. Called
91 by our to_xfer_partial. */
92 static LONGEST (*super_xfer_partial
) (struct target_ops
*, enum target_object
,
93 const char *, gdb_byte
*, const gdb_byte
*,
96 static int debug_linux_nat
;
98 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
99 struct cmd_list_element
*c
, const char *value
)
101 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
105 static int linux_parent_pid
;
107 struct simple_pid_list
110 struct simple_pid_list
*next
;
112 struct simple_pid_list
*stopped_pids
;
114 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
115 can not be used, 1 if it can. */
117 static int linux_supports_tracefork_flag
= -1;
119 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
120 PTRACE_O_TRACEVFORKDONE. */
122 static int linux_supports_tracevforkdone_flag
= -1;
125 /* Trivial list manipulation functions to keep track of a list of
126 new stopped processes. */
128 add_to_pid_list (struct simple_pid_list
**listp
, int pid
)
130 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
132 new_pid
->next
= *listp
;
137 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
)
139 struct simple_pid_list
**p
;
141 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
142 if ((*p
)->pid
== pid
)
144 struct simple_pid_list
*next
= (*p
)->next
;
153 linux_record_stopped_pid (int pid
)
155 add_to_pid_list (&stopped_pids
, pid
);
159 /* A helper function for linux_test_for_tracefork, called after fork (). */
162 linux_tracefork_child (void)
166 ptrace (PTRACE_TRACEME
, 0, 0, 0);
167 kill (getpid (), SIGSTOP
);
172 /* Wrapper function for waitpid which handles EINTR. */
175 my_waitpid (int pid
, int *status
, int flags
)
180 ret
= waitpid (pid
, status
, flags
);
182 while (ret
== -1 && errno
== EINTR
);
187 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
189 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
190 we know that the feature is not available. This may change the tracing
191 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
193 However, if it succeeds, we don't know for sure that the feature is
194 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
195 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
196 fork tracing, and let it fork. If the process exits, we assume that we
197 can't use TRACEFORK; if we get the fork notification, and we can extract
198 the new child's PID, then we assume that we can. */
201 linux_test_for_tracefork (int original_pid
)
203 int child_pid
, ret
, status
;
206 linux_supports_tracefork_flag
= 0;
207 linux_supports_tracevforkdone_flag
= 0;
209 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
215 perror_with_name (("fork"));
218 linux_tracefork_child ();
220 ret
= my_waitpid (child_pid
, &status
, 0);
222 perror_with_name (("waitpid"));
223 else if (ret
!= child_pid
)
224 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
225 if (! WIFSTOPPED (status
))
226 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
228 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
231 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
234 warning (_("linux_test_for_tracefork: failed to kill child"));
238 ret
= my_waitpid (child_pid
, &status
, 0);
239 if (ret
!= child_pid
)
240 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
241 else if (!WIFSIGNALED (status
))
242 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
243 "killed child"), status
);
248 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
249 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
250 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
251 linux_supports_tracevforkdone_flag
= (ret
== 0);
253 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
255 warning (_("linux_test_for_tracefork: failed to resume child"));
257 ret
= my_waitpid (child_pid
, &status
, 0);
259 if (ret
== child_pid
&& WIFSTOPPED (status
)
260 && status
>> 16 == PTRACE_EVENT_FORK
)
263 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
264 if (ret
== 0 && second_pid
!= 0)
268 linux_supports_tracefork_flag
= 1;
269 my_waitpid (second_pid
, &second_status
, 0);
270 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
272 warning (_("linux_test_for_tracefork: failed to kill second child"));
276 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
277 "(%d, status 0x%x)"), ret
, status
);
279 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
281 warning (_("linux_test_for_tracefork: failed to kill child"));
282 my_waitpid (child_pid
, &status
, 0);
285 /* Return non-zero iff we have tracefork functionality available.
286 This function also sets linux_supports_tracefork_flag. */
289 linux_supports_tracefork (int pid
)
291 if (linux_supports_tracefork_flag
== -1)
292 linux_test_for_tracefork (pid
);
293 return linux_supports_tracefork_flag
;
297 linux_supports_tracevforkdone (int pid
)
299 if (linux_supports_tracefork_flag
== -1)
300 linux_test_for_tracefork (pid
);
301 return linux_supports_tracevforkdone_flag
;
306 linux_enable_event_reporting (ptid_t ptid
)
308 int pid
= ptid_get_lwp (ptid
);
312 pid
= ptid_get_pid (ptid
);
314 if (! linux_supports_tracefork (pid
))
317 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
318 | PTRACE_O_TRACECLONE
;
319 if (linux_supports_tracevforkdone (pid
))
320 options
|= PTRACE_O_TRACEVFORKDONE
;
322 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
323 read-only process state. */
325 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
329 child_post_attach (int pid
)
331 linux_enable_event_reporting (pid_to_ptid (pid
));
335 linux_child_post_startup_inferior (ptid_t ptid
)
337 linux_enable_event_reporting (ptid
);
341 child_follow_fork (struct target_ops
*ops
, int follow_child
)
344 struct target_waitstatus last_status
;
346 int parent_pid
, child_pid
;
348 get_last_target_status (&last_ptid
, &last_status
);
349 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
350 parent_pid
= ptid_get_lwp (last_ptid
);
352 parent_pid
= ptid_get_pid (last_ptid
);
353 child_pid
= last_status
.value
.related_pid
;
357 /* We're already attached to the parent, by default. */
359 /* Before detaching from the child, remove all breakpoints from
360 it. (This won't actually modify the breakpoint list, but will
361 physically remove the breakpoints from the child.) */
362 /* If we vforked this will remove the breakpoints from the parent
363 also, but they'll be reinserted below. */
364 detach_breakpoints (child_pid
);
368 target_terminal_ours ();
369 fprintf_unfiltered (gdb_stdlog
,
370 "Detaching after fork from child process %d.\n",
374 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
378 gdb_assert (linux_supports_tracefork_flag
>= 0);
379 if (linux_supports_tracevforkdone (0))
383 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
384 my_waitpid (parent_pid
, &status
, __WALL
);
385 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
386 warning (_("Unexpected waitpid result %06x when waiting for "
387 "vfork-done"), status
);
391 /* We can't insert breakpoints until the child has
392 finished with the shared memory region. We need to
393 wait until that happens. Ideal would be to just
395 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
396 - waitpid (parent_pid, &status, __WALL);
397 However, most architectures can't handle a syscall
398 being traced on the way out if it wasn't traced on
401 We might also think to loop, continuing the child
402 until it exits or gets a SIGTRAP. One problem is
403 that the child might call ptrace with PTRACE_TRACEME.
405 There's no simple and reliable way to figure out when
406 the vforked child will be done with its copy of the
407 shared memory. We could step it out of the syscall,
408 two instructions, let it go, and then single-step the
409 parent once. When we have hardware single-step, this
410 would work; with software single-step it could still
411 be made to work but we'd have to be able to insert
412 single-step breakpoints in the child, and we'd have
413 to insert -just- the single-step breakpoint in the
414 parent. Very awkward.
416 In the end, the best we can do is to make sure it
417 runs for a little while. Hopefully it will be out of
418 range of any breakpoints we reinsert. Usually this
419 is only the single-step breakpoint at vfork's return
425 /* Since we vforked, breakpoints were removed in the parent
426 too. Put them back. */
427 reattach_breakpoints (parent_pid
);
432 char child_pid_spelling
[40];
434 /* Needed to keep the breakpoint lists in sync. */
436 detach_breakpoints (child_pid
);
438 /* Before detaching from the parent, remove all breakpoints from it. */
439 remove_breakpoints ();
443 target_terminal_ours ();
444 fprintf_unfiltered (gdb_stdlog
,
445 "Attaching after fork to child process %d.\n",
449 /* If we're vforking, we may want to hold on to the parent until
450 the child exits or execs. At exec time we can remove the old
451 breakpoints from the parent and detach it; at exit time we
452 could do the same (or even, sneakily, resume debugging it - the
453 child's exec has failed, or something similar).
455 This doesn't clean up "properly", because we can't call
456 target_detach, but that's OK; if the current target is "child",
457 then it doesn't need any further cleanups, and lin_lwp will
458 generally not encounter vfork (vfork is defined to fork
461 The holding part is very easy if we have VFORKDONE events;
462 but keeping track of both processes is beyond GDB at the
463 moment. So we don't expose the parent to the rest of GDB.
464 Instead we quietly hold onto it until such time as we can
468 linux_parent_pid
= parent_pid
;
470 target_detach (NULL
, 0);
472 inferior_ptid
= pid_to_ptid (child_pid
);
474 /* Reinstall ourselves, since we might have been removed in
475 target_detach (which does other necessary cleanup). */
478 /* Reset breakpoints in the child as appropriate. */
479 follow_inferior_reset_breakpoints ();
486 linux_handle_extended_wait (int pid
, int status
,
487 struct target_waitstatus
*ourstatus
)
489 int event
= status
>> 16;
491 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
492 || event
== PTRACE_EVENT_CLONE
)
494 unsigned long new_pid
;
497 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
499 /* If we haven't already seen the new PID stop, wait for it now. */
500 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
502 /* The new child has a pending SIGSTOP. We can't affect it until it
503 hits the SIGSTOP, but we're already attached. */
504 ret
= my_waitpid (new_pid
, &status
,
505 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
507 perror_with_name (_("waiting for new child"));
508 else if (ret
!= new_pid
)
509 internal_error (__FILE__
, __LINE__
,
510 _("wait returned unexpected PID %d"), ret
);
511 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
512 internal_error (__FILE__
, __LINE__
,
513 _("wait returned unexpected status 0x%x"), status
);
516 if (event
== PTRACE_EVENT_FORK
)
517 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
518 else if (event
== PTRACE_EVENT_VFORK
)
519 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
521 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
523 ourstatus
->value
.related_pid
= new_pid
;
524 return inferior_ptid
;
527 if (event
== PTRACE_EVENT_EXEC
)
529 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
530 ourstatus
->value
.execd_pathname
531 = xstrdup (child_pid_to_exec_file (pid
));
533 if (linux_parent_pid
)
535 detach_breakpoints (linux_parent_pid
);
536 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
538 linux_parent_pid
= 0;
541 return inferior_ptid
;
544 internal_error (__FILE__
, __LINE__
,
545 _("unknown ptrace event %d"), event
);
550 child_insert_fork_catchpoint (int pid
)
552 if (! linux_supports_tracefork (pid
))
553 error (_("Your system does not support fork catchpoints."));
557 child_insert_vfork_catchpoint (int pid
)
559 if (!linux_supports_tracefork (pid
))
560 error (_("Your system does not support vfork catchpoints."));
564 child_insert_exec_catchpoint (int pid
)
566 if (!linux_supports_tracefork (pid
))
567 error (_("Your system does not support exec catchpoints."));
574 int pid
= PIDGET (inferior_ptid
);
575 struct target_waitstatus last
;
582 /* If we're stopped while forking and we haven't followed yet, kill the
583 other task. We need to do this first because the parent will be
584 sleeping if this is a vfork. */
586 get_last_target_status (&last_ptid
, &last
);
588 if (last
.kind
== TARGET_WAITKIND_FORKED
589 || last
.kind
== TARGET_WAITKIND_VFORKED
)
591 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
595 /* Kill the current process. */
596 ptrace (PT_KILL
, pid
, 0, 0);
597 ret
= wait (&status
);
599 /* We might get a SIGCHLD instead of an exit status. This is
600 aggravated by the first kill above - a child has just died. */
602 while (ret
== pid
&& WIFSTOPPED (status
))
604 ptrace (PT_KILL
, pid
, 0, 0);
605 ret
= wait (&status
);
608 target_mourn_inferior ();
611 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
612 are processes sharing the same VM space. A multi-threaded process
613 is basically a group of such processes. However, such a grouping
614 is almost entirely a user-space issue; the kernel doesn't enforce
615 such a grouping at all (this might change in the future). In
616 general, we'll rely on the threads library (i.e. the GNU/Linux
617 Threads library) to provide such a grouping.
619 It is perfectly well possible to write a multi-threaded application
620 without the assistance of a threads library, by using the clone
621 system call directly. This module should be able to give some
622 rudimentary support for debugging such applications if developers
623 specify the CLONE_PTRACE flag in the clone system call, and are
624 using the Linux kernel 2.4 or above.
626 Note that there are some peculiarities in GNU/Linux that affect
629 - In general one should specify the __WCLONE flag to waitpid in
630 order to make it report events for any of the cloned processes
631 (and leave it out for the initial process). However, if a cloned
632 process has exited the exit status is only reported if the
633 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
634 we cannot use it since GDB must work on older systems too.
636 - When a traced, cloned process exits and is waited for by the
637 debugger, the kernel reassigns it to the original parent and
638 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
639 library doesn't notice this, which leads to the "zombie problem":
640 When debugged a multi-threaded process that spawns a lot of
641 threads will run out of processes, even if the threads exit,
642 because the "zombies" stay around. */
644 /* List of known LWPs. */
645 static struct lwp_info
*lwp_list
;
647 /* Number of LWPs in the list. */
650 /* Non-zero if we're running in "threaded" mode. */
654 #define GET_LWP(ptid) ptid_get_lwp (ptid)
655 #define GET_PID(ptid) ptid_get_pid (ptid)
656 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
657 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
659 /* If the last reported event was a SIGTRAP, this variable is set to
660 the process id of the LWP/thread that got it. */
664 /* This module's target-specific operations. */
665 static struct target_ops linux_nat_ops
;
667 /* Since we cannot wait (in linux_nat_wait) for the initial process and
668 any cloned processes with a single call to waitpid, we have to use
669 the WNOHANG flag and call waitpid in a loop. To optimize
670 things a bit we use `sigsuspend' to wake us up when a process has
671 something to report (it will send us a SIGCHLD if it has). To make
672 this work we have to juggle with the signal mask. We save the
673 original signal mask such that we can restore it before creating a
674 new process in order to avoid blocking certain signals in the
675 inferior. We then block SIGCHLD during the waitpid/sigsuspend
678 /* Original signal mask. */
679 static sigset_t normal_mask
;
681 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
682 _initialize_linux_nat. */
683 static sigset_t suspend_mask
;
685 /* Signals to block to make that sigsuspend work. */
686 static sigset_t blocked_mask
;
689 /* Prototypes for local functions. */
690 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
691 static int linux_nat_thread_alive (ptid_t ptid
);
693 /* Convert wait status STATUS to a string. Used for printing debug
697 status_to_str (int status
)
701 if (WIFSTOPPED (status
))
702 snprintf (buf
, sizeof (buf
), "%s (stopped)",
703 strsignal (WSTOPSIG (status
)));
704 else if (WIFSIGNALED (status
))
705 snprintf (buf
, sizeof (buf
), "%s (terminated)",
706 strsignal (WSTOPSIG (status
)));
708 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
713 /* Initialize the list of LWPs. Note that this module, contrary to
714 what GDB's generic threads layer does for its thread list,
715 re-initializes the LWP lists whenever we mourn or detach (which
716 doesn't involve mourning) the inferior. */
721 struct lwp_info
*lp
, *lpnext
;
723 for (lp
= lwp_list
; lp
; lp
= lpnext
)
734 /* Add the LWP specified by PID to the list. If this causes the
735 number of LWPs to become larger than one, go into "threaded" mode.
736 Return a pointer to the structure describing the new LWP. */
738 static struct lwp_info
*
739 add_lwp (ptid_t ptid
)
743 gdb_assert (is_lwp (ptid
));
745 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
747 memset (lp
, 0, sizeof (struct lwp_info
));
749 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
761 /* Remove the LWP specified by PID from the list. */
764 delete_lwp (ptid_t ptid
)
766 struct lwp_info
*lp
, *lpprev
;
770 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
771 if (ptid_equal (lp
->ptid
, ptid
))
777 /* We don't go back to "non-threaded" mode if the number of threads
778 becomes less than two. */
782 lpprev
->next
= lp
->next
;
789 /* Return a pointer to the structure describing the LWP corresponding
790 to PID. If no corresponding LWP could be found, return NULL. */
792 static struct lwp_info
*
793 find_lwp_pid (ptid_t ptid
)
799 lwp
= GET_LWP (ptid
);
801 lwp
= GET_PID (ptid
);
803 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
804 if (lwp
== GET_LWP (lp
->ptid
))
810 /* Call CALLBACK with its second argument set to DATA for every LWP in
811 the list. If CALLBACK returns 1 for a particular LWP, return a
812 pointer to the structure describing that LWP immediately.
813 Otherwise return NULL. */
816 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
818 struct lwp_info
*lp
, *lpnext
;
820 for (lp
= lwp_list
; lp
; lp
= lpnext
)
823 if ((*callback
) (lp
, data
))
830 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
831 a message telling the user that a new LWP has been added to the
835 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
837 struct lwp_info
*lp
, *found_lp
;
839 gdb_assert (is_lwp (ptid
));
841 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
842 to interrupt either the ptrace() or waitpid() calls below. */
843 if (!sigismember (&blocked_mask
, SIGCHLD
))
845 sigaddset (&blocked_mask
, SIGCHLD
);
846 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
850 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid
));
852 found_lp
= lp
= find_lwp_pid (ptid
);
856 /* We assume that we're already attached to any LWP that has an id
857 equal to the overall process id, and to any LWP that is already
858 in our list of LWPs. If we're not seeing exit events from threads
859 and we've had PID wraparound since we last tried to stop all threads,
860 this assumption might be wrong; fortunately, this is very unlikely
862 if (GET_LWP (ptid
) != GET_PID (ptid
) && found_lp
== NULL
)
867 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
868 error (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
869 safe_strerror (errno
));
872 fprintf_unfiltered (gdb_stdlog
,
873 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
874 target_pid_to_str (ptid
));
876 pid
= my_waitpid (GET_LWP (ptid
), &status
, 0);
877 if (pid
== -1 && errno
== ECHILD
)
879 /* Try again with __WCLONE to check cloned processes. */
880 pid
= my_waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
884 gdb_assert (pid
== GET_LWP (ptid
)
885 && WIFSTOPPED (status
) && WSTOPSIG (status
));
887 child_post_attach (pid
);
893 fprintf_unfiltered (gdb_stdlog
,
894 "LLAL: waitpid %s received %s\n",
895 target_pid_to_str (ptid
),
896 status_to_str (status
));
901 /* We assume that the LWP representing the original process is
902 already stopped. Mark it as stopped in the data structure
903 that the linux ptrace layer uses to keep track of threads.
904 Note that this won't have already been done since the main
905 thread will have, we assume, been stopped by an attach from a
912 linux_nat_attach (char *args
, int from_tty
)
918 /* FIXME: We should probably accept a list of process id's, and
919 attach all of them. */
920 linux_ops
->to_attach (args
, from_tty
);
922 /* Add the initial process as the first LWP to the list. */
923 lp
= add_lwp (BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
)));
925 /* Make sure the initial process is stopped. The user-level threads
926 layer might want to poke around in the inferior, and that won't
927 work if things haven't stabilized yet. */
928 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, 0);
929 if (pid
== -1 && errno
== ECHILD
)
931 warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid
));
933 /* Try again with __WCLONE to check cloned processes. */
934 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
938 gdb_assert (pid
== GET_PID (inferior_ptid
)
939 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
943 /* Fake the SIGSTOP that core GDB expects. */
944 lp
->status
= W_STOPCODE (SIGSTOP
);
948 fprintf_unfiltered (gdb_stdlog
,
949 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid
);
954 detach_callback (struct lwp_info
*lp
, void *data
)
956 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
958 if (debug_linux_nat
&& lp
->status
)
959 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
960 strsignal (WSTOPSIG (lp
->status
)),
961 target_pid_to_str (lp
->ptid
));
963 while (lp
->signalled
&& lp
->stopped
)
966 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
967 WSTOPSIG (lp
->status
)) < 0)
968 error (_("Can't continue %s: %s"), target_pid_to_str (lp
->ptid
),
969 safe_strerror (errno
));
972 fprintf_unfiltered (gdb_stdlog
,
973 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
974 target_pid_to_str (lp
->ptid
),
975 status_to_str (lp
->status
));
980 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
981 here. But since lp->signalled was cleared above,
982 stop_wait_callback didn't do anything; the process was left
983 running. Shouldn't we be waiting for it to stop?
984 I've removed the call, since stop_wait_callback now does do
985 something when called with lp->signalled == 0. */
987 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
990 /* We don't actually detach from the LWP that has an id equal to the
991 overall process id just yet. */
992 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
995 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
996 WSTOPSIG (lp
->status
)) < 0)
997 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
998 safe_strerror (errno
));
1000 if (debug_linux_nat
)
1001 fprintf_unfiltered (gdb_stdlog
,
1002 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1003 target_pid_to_str (lp
->ptid
),
1004 strsignal (WSTOPSIG (lp
->status
)));
1006 delete_lwp (lp
->ptid
);
1013 linux_nat_detach (char *args
, int from_tty
)
1015 iterate_over_lwps (detach_callback
, NULL
);
1017 /* Only the initial process should be left right now. */
1018 gdb_assert (num_lwps
== 1);
1020 trap_ptid
= null_ptid
;
1022 /* Destroy LWP info; it's no longer valid. */
1025 /* Restore the original signal mask. */
1026 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1027 sigemptyset (&blocked_mask
);
1029 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
1030 linux_ops
->to_detach (args
, from_tty
);
1036 resume_callback (struct lwp_info
*lp
, void *data
)
1038 if (lp
->stopped
&& lp
->status
== 0)
1040 struct thread_info
*tp
;
1042 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
1043 0, TARGET_SIGNAL_0
);
1044 if (debug_linux_nat
)
1045 fprintf_unfiltered (gdb_stdlog
,
1046 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1047 target_pid_to_str (lp
->ptid
));
1056 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1063 resume_set_callback (struct lwp_info
*lp
, void *data
)
1070 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1072 struct lwp_info
*lp
;
1075 /* A specific PTID means `step only this process id'. */
1076 resume_all
= (PIDGET (ptid
) == -1);
1079 iterate_over_lwps (resume_set_callback
, NULL
);
1081 iterate_over_lwps (resume_clear_callback
, NULL
);
1083 /* If PID is -1, it's the current inferior that should be
1084 handled specially. */
1085 if (PIDGET (ptid
) == -1)
1086 ptid
= inferior_ptid
;
1088 lp
= find_lwp_pid (ptid
);
1091 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1093 /* Remember if we're stepping. */
1096 /* Mark this LWP as resumed. */
1099 /* If we have a pending wait status for this thread, there is no
1100 point in resuming the process. */
1103 /* FIXME: What should we do if we are supposed to continue
1104 this thread with a signal? */
1105 gdb_assert (signo
== TARGET_SIGNAL_0
);
1109 /* Mark LWP as not stopped to prevent it from being continued by
1115 iterate_over_lwps (resume_callback
, NULL
);
1117 linux_ops
->to_resume (ptid
, step
, signo
);
1118 if (debug_linux_nat
)
1119 fprintf_unfiltered (gdb_stdlog
,
1120 "LLR: %s %s, %s (resume event thread)\n",
1121 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1122 target_pid_to_str (ptid
),
1123 signo
? strsignal (signo
) : "0");
1126 /* Issue kill to specified lwp. */
1128 static int tkill_failed
;
1131 kill_lwp (int lwpid
, int signo
)
1135 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1136 fails, then we are not using nptl threads and we should be using kill. */
1138 #ifdef HAVE_TKILL_SYSCALL
1141 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1142 if (errno
!= ENOSYS
)
1149 return kill (lwpid
, signo
);
1152 /* Handle a GNU/Linux extended wait response. Most of the work we
1153 just pass off to linux_handle_extended_wait, but if it reports a
1154 clone event we need to add the new LWP to our list (and not report
1155 the trap to higher layers). This function returns non-zero if
1156 the event should be ignored and we should wait again. */
1159 linux_nat_handle_extended (struct lwp_info
*lp
, int status
)
1161 linux_handle_extended_wait (GET_LWP (lp
->ptid
), status
,
1164 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1165 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_SPURIOUS
)
1167 struct lwp_info
*new_lp
;
1168 new_lp
= add_lwp (BUILD_LWP (lp
->waitstatus
.value
.related_pid
,
1169 GET_PID (inferior_ptid
)));
1171 new_lp
->stopped
= 1;
1173 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1175 if (debug_linux_nat
)
1176 fprintf_unfiltered (gdb_stdlog
,
1177 "LLHE: Got clone event from LWP %ld, resuming\n",
1178 GET_LWP (lp
->ptid
));
1179 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1187 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1191 wait_lwp (struct lwp_info
*lp
)
1195 int thread_dead
= 0;
1197 gdb_assert (!lp
->stopped
);
1198 gdb_assert (lp
->status
== 0);
1200 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1201 if (pid
== -1 && errno
== ECHILD
)
1203 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1204 if (pid
== -1 && errno
== ECHILD
)
1206 /* The thread has previously exited. We need to delete it
1207 now because, for some vendor 2.4 kernels with NPTL
1208 support backported, there won't be an exit event unless
1209 it is the main thread. 2.6 kernels will report an exit
1210 event for each thread that exits, as expected. */
1212 if (debug_linux_nat
)
1213 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1214 target_pid_to_str (lp
->ptid
));
1220 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1222 if (debug_linux_nat
)
1224 fprintf_unfiltered (gdb_stdlog
,
1225 "WL: waitpid %s received %s\n",
1226 target_pid_to_str (lp
->ptid
),
1227 status_to_str (status
));
1231 /* Check if the thread has exited. */
1232 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1235 if (debug_linux_nat
)
1236 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1237 target_pid_to_str (lp
->ptid
));
1242 if (in_thread_list (lp
->ptid
))
1244 /* Core GDB cannot deal with us deleting the current thread. */
1245 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1246 delete_thread (lp
->ptid
);
1247 printf_unfiltered (_("[%s exited]\n"),
1248 target_pid_to_str (lp
->ptid
));
1251 delete_lwp (lp
->ptid
);
1255 gdb_assert (WIFSTOPPED (status
));
1257 /* Handle GNU/Linux's extended waitstatus for trace events. */
1258 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1260 if (debug_linux_nat
)
1261 fprintf_unfiltered (gdb_stdlog
,
1262 "WL: Handling extended status 0x%06x\n",
1264 if (linux_nat_handle_extended (lp
, status
))
1265 return wait_lwp (lp
);
1271 /* Send a SIGSTOP to LP. */
1274 stop_callback (struct lwp_info
*lp
, void *data
)
1276 if (!lp
->stopped
&& !lp
->signalled
)
1280 if (debug_linux_nat
)
1282 fprintf_unfiltered (gdb_stdlog
,
1283 "SC: kill %s **<SIGSTOP>**\n",
1284 target_pid_to_str (lp
->ptid
));
1287 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1288 if (debug_linux_nat
)
1290 fprintf_unfiltered (gdb_stdlog
,
1291 "SC: lwp kill %d %s\n",
1293 errno
? safe_strerror (errno
) : "ERRNO-OK");
1297 gdb_assert (lp
->status
== 0);
1303 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1304 a pointer to a set of signals to be flushed immediately. */
1307 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1309 sigset_t
*flush_mask
= data
;
1315 status
= wait_lwp (lp
);
1319 /* Ignore any signals in FLUSH_MASK. */
1320 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1329 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1330 if (debug_linux_nat
)
1331 fprintf_unfiltered (gdb_stdlog
,
1332 "PTRACE_CONT %s, 0, 0 (%s)\n",
1333 target_pid_to_str (lp
->ptid
),
1334 errno
? safe_strerror (errno
) : "OK");
1336 return stop_wait_callback (lp
, flush_mask
);
1339 if (WSTOPSIG (status
) != SIGSTOP
)
1341 if (WSTOPSIG (status
) == SIGTRAP
)
1343 /* If a LWP other than the LWP that we're reporting an
1344 event for has hit a GDB breakpoint (as opposed to
1345 some random trap signal), then just arrange for it to
1346 hit it again later. We don't keep the SIGTRAP status
1347 and don't forward the SIGTRAP signal to the LWP. We
1348 will handle the current event, eventually we will
1349 resume all LWPs, and this one will get its breakpoint
1352 If we do not do this, then we run the risk that the
1353 user will delete or disable the breakpoint, but the
1354 thread will have already tripped on it. */
1356 /* Now resume this LWP and get the SIGSTOP event. */
1358 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1359 if (debug_linux_nat
)
1361 fprintf_unfiltered (gdb_stdlog
,
1362 "PTRACE_CONT %s, 0, 0 (%s)\n",
1363 target_pid_to_str (lp
->ptid
),
1364 errno
? safe_strerror (errno
) : "OK");
1366 fprintf_unfiltered (gdb_stdlog
,
1367 "SWC: Candidate SIGTRAP event in %s\n",
1368 target_pid_to_str (lp
->ptid
));
1370 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1371 stop_wait_callback (lp
, data
);
1372 /* If there's another event, throw it back into the queue. */
1375 if (debug_linux_nat
)
1377 fprintf_unfiltered (gdb_stdlog
,
1378 "SWC: kill %s, %s\n",
1379 target_pid_to_str (lp
->ptid
),
1380 status_to_str ((int) status
));
1382 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1384 /* Save the sigtrap event. */
1385 lp
->status
= status
;
1390 /* The thread was stopped with a signal other than
1391 SIGSTOP, and didn't accidentally trip a breakpoint. */
1393 if (debug_linux_nat
)
1395 fprintf_unfiltered (gdb_stdlog
,
1396 "SWC: Pending event %s in %s\n",
1397 status_to_str ((int) status
),
1398 target_pid_to_str (lp
->ptid
));
1400 /* Now resume this LWP and get the SIGSTOP event. */
1402 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1403 if (debug_linux_nat
)
1404 fprintf_unfiltered (gdb_stdlog
,
1405 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1406 target_pid_to_str (lp
->ptid
),
1407 errno
? safe_strerror (errno
) : "OK");
1409 /* Hold this event/waitstatus while we check to see if
1410 there are any more (we still want to get that SIGSTOP). */
1411 stop_wait_callback (lp
, data
);
1412 /* If the lp->status field is still empty, use it to hold
1413 this event. If not, then this event must be returned
1414 to the event queue of the LWP. */
1415 if (lp
->status
== 0)
1416 lp
->status
= status
;
1419 if (debug_linux_nat
)
1421 fprintf_unfiltered (gdb_stdlog
,
1422 "SWC: kill %s, %s\n",
1423 target_pid_to_str (lp
->ptid
),
1424 status_to_str ((int) status
));
1426 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1433 /* We caught the SIGSTOP that we intended to catch, so
1434 there's no SIGSTOP pending. */
1443 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1444 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1447 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
1449 sigset_t blocked
, ignored
;
1452 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
1457 for (i
= 1; i
< NSIG
; i
++)
1458 if (sigismember (pending
, i
))
1459 if (!sigismember (flush_mask
, i
)
1460 || sigismember (&blocked
, i
)
1461 || sigismember (&ignored
, i
))
1462 sigdelset (pending
, i
);
1464 if (sigisemptyset (pending
))
1470 /* DATA is interpreted as a mask of signals to flush. If LP has
1471 signals pending, and they are all in the flush mask, then arrange
1472 to flush them. LP should be stopped, as should all other threads
1473 it might share a signal queue with. */
1476 flush_callback (struct lwp_info
*lp
, void *data
)
1478 sigset_t
*flush_mask
= data
;
1479 sigset_t pending
, intersection
, blocked
, ignored
;
1482 /* Normally, when an LWP exits, it is removed from the LWP list. The
1483 last LWP isn't removed till later, however. So if there is only
1484 one LWP on the list, make sure it's alive. */
1485 if (lwp_list
== lp
&& lp
->next
== NULL
)
1486 if (!linux_nat_thread_alive (lp
->ptid
))
1489 /* Just because the LWP is stopped doesn't mean that new signals
1490 can't arrive from outside, so this function must be careful of
1491 race conditions. However, because all threads are stopped, we
1492 can assume that the pending mask will not shrink unless we resume
1493 the LWP, and that it will then get another signal. We can't
1494 control which one, however. */
1498 if (debug_linux_nat
)
1499 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp
->status
);
1500 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
1504 while (linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
1509 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1510 if (debug_linux_nat
)
1511 fprintf_unfiltered (gdb_stderr
,
1512 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
1515 stop_wait_callback (lp
, flush_mask
);
1516 if (debug_linux_nat
)
1517 fprintf_unfiltered (gdb_stderr
,
1518 "FC: Wait finished; saved status is %d\n",
1525 /* Return non-zero if LP has a wait status pending. */
1528 status_callback (struct lwp_info
*lp
, void *data
)
1530 /* Only report a pending wait status if we pretend that this has
1531 indeed been resumed. */
1532 return (lp
->status
!= 0 && lp
->resumed
);
1535 /* Return non-zero if LP isn't stopped. */
1538 running_callback (struct lwp_info
*lp
, void *data
)
1540 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
1543 /* Count the LWP's that have had events. */
1546 count_events_callback (struct lwp_info
*lp
, void *data
)
1550 gdb_assert (count
!= NULL
);
1552 /* Count only LWPs that have a SIGTRAP event pending. */
1554 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1560 /* Select the LWP (if any) that is currently being single-stepped. */
1563 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
1565 if (lp
->step
&& lp
->status
!= 0)
1571 /* Select the Nth LWP that has had a SIGTRAP event. */
1574 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
1576 int *selector
= data
;
1578 gdb_assert (selector
!= NULL
);
1580 /* Select only LWPs that have a SIGTRAP event pending. */
1582 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1583 if ((*selector
)-- == 0)
1590 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
1592 struct lwp_info
*event_lp
= data
;
1594 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1598 /* If a LWP other than the LWP that we're reporting an event for has
1599 hit a GDB breakpoint (as opposed to some random trap signal),
1600 then just arrange for it to hit it again later. We don't keep
1601 the SIGTRAP status and don't forward the SIGTRAP signal to the
1602 LWP. We will handle the current event, eventually we will resume
1603 all LWPs, and this one will get its breakpoint trap again.
1605 If we do not do this, then we run the risk that the user will
1606 delete or disable the breakpoint, but the LWP will have already
1610 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
1611 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
1612 DECR_PC_AFTER_BREAK
))
1614 if (debug_linux_nat
)
1615 fprintf_unfiltered (gdb_stdlog
,
1616 "CBC: Push back breakpoint for %s\n",
1617 target_pid_to_str (lp
->ptid
));
1619 /* Back up the PC if necessary. */
1620 if (DECR_PC_AFTER_BREAK
)
1621 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
1623 /* Throw away the SIGTRAP. */
1630 /* Select one LWP out of those that have events pending. */
1633 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
1636 int random_selector
;
1637 struct lwp_info
*event_lp
;
1639 /* Record the wait status for the origional LWP. */
1640 (*orig_lp
)->status
= *status
;
1642 /* Give preference to any LWP that is being single-stepped. */
1643 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
1644 if (event_lp
!= NULL
)
1646 if (debug_linux_nat
)
1647 fprintf_unfiltered (gdb_stdlog
,
1648 "SEL: Select single-step %s\n",
1649 target_pid_to_str (event_lp
->ptid
));
1653 /* No single-stepping LWP. Select one at random, out of those
1654 which have had SIGTRAP events. */
1656 /* First see how many SIGTRAP events we have. */
1657 iterate_over_lwps (count_events_callback
, &num_events
);
1659 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1660 random_selector
= (int)
1661 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1663 if (debug_linux_nat
&& num_events
> 1)
1664 fprintf_unfiltered (gdb_stdlog
,
1665 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1666 num_events
, random_selector
);
1668 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
1672 if (event_lp
!= NULL
)
1674 /* Switch the event LWP. */
1675 *orig_lp
= event_lp
;
1676 *status
= event_lp
->status
;
1679 /* Flush the wait status for the event LWP. */
1680 (*orig_lp
)->status
= 0;
1683 /* Return non-zero if LP has been resumed. */
1686 resumed_callback (struct lwp_info
*lp
, void *data
)
1691 /* We need to override child_wait to support attaching to cloned
1692 processes, since a normal wait (as done by the default version)
1693 ignores those processes. */
1695 /* Wait for child PTID to do something. Return id of the child,
1696 minus_one_ptid in case of error; store status into *OURSTATUS. */
1699 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1705 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1709 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1710 attached process. */
1713 pid
= my_waitpid (GET_PID (ptid
), &status
, 0);
1714 if (pid
== -1 && errno
== ECHILD
)
1715 /* Try again with __WCLONE to check cloned processes. */
1716 pid
= my_waitpid (GET_PID (ptid
), &status
, __WCLONE
);
1718 if (debug_linux_nat
)
1720 fprintf_unfiltered (gdb_stdlog
,
1721 "CW: waitpid %ld received %s\n",
1722 (long) pid
, status_to_str (status
));
1727 /* Make sure we don't report an event for the exit of the
1728 original program, if we've detached from it. */
1729 if (pid
!= -1 && !WIFSTOPPED (status
) && pid
!= GET_PID (inferior_ptid
))
1735 /* Check for stop events reported by a process we didn't already
1736 know about - in this case, anything other than inferior_ptid.
1738 If we're expecting to receive stopped processes after fork,
1739 vfork, and clone events, then we'll just add the new one to
1740 our list and go back to waiting for the event to be reported
1741 - the stopped process might be returned from waitpid before
1742 or after the event is. If we want to handle debugging of
1743 CLONE_PTRACE processes we need to do more here, i.e. switch
1744 to multi-threaded mode. */
1745 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
1746 && pid
!= GET_PID (inferior_ptid
))
1748 linux_record_stopped_pid (pid
);
1753 /* Handle GNU/Linux's extended waitstatus for trace events. */
1754 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
1755 && status
>> 16 != 0)
1757 linux_handle_extended_wait (pid
, status
, ourstatus
);
1759 /* If we see a clone event, detach the child, and don't
1760 report the event. It would be nice to offer some way to
1761 switch into a non-thread-db based threaded mode at this
1763 if (ourstatus
->kind
== TARGET_WAITKIND_SPURIOUS
)
1765 ptrace (PTRACE_DETACH
, ourstatus
->value
.related_pid
, 0, 0);
1766 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1767 ptrace (PTRACE_CONT
, pid
, 0, 0);
1773 clear_sigio_trap ();
1774 clear_sigint_trap ();
1776 while (pid
== -1 && save_errno
== EINTR
);
1780 warning (_("Child process unexpectedly missing: %s"),
1781 safe_strerror (errno
));
1783 /* Claim it exited with unknown signal. */
1784 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1785 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1786 return minus_one_ptid
;
1789 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1790 store_waitstatus (ourstatus
, status
);
1792 return pid_to_ptid (pid
);
1795 /* Stop an active thread, verify it still exists, then resume it. */
1798 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
1800 struct lwp_info
*ptr
;
1802 if (!lp
->stopped
&& !lp
->signalled
)
1804 stop_callback (lp
, NULL
);
1805 stop_wait_callback (lp
, NULL
);
1806 /* Resume if the lwp still exists. */
1807 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
1810 resume_callback (lp
, NULL
);
1811 resume_set_callback (lp
, NULL
);
1818 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1820 struct lwp_info
*lp
= NULL
;
1823 pid_t pid
= PIDGET (ptid
);
1824 sigset_t flush_mask
;
1826 sigemptyset (&flush_mask
);
1828 /* Make sure SIGCHLD is blocked. */
1829 if (!sigismember (&blocked_mask
, SIGCHLD
))
1831 sigaddset (&blocked_mask
, SIGCHLD
);
1832 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1837 /* Make sure there is at least one LWP that has been resumed, at
1838 least if there are any LWPs at all. */
1839 gdb_assert (num_lwps
== 0 || iterate_over_lwps (resumed_callback
, NULL
));
1841 /* First check if there is a LWP with a wait status pending. */
1844 /* Any LWP that's been resumed will do. */
1845 lp
= iterate_over_lwps (status_callback
, NULL
);
1848 status
= lp
->status
;
1851 if (debug_linux_nat
&& status
)
1852 fprintf_unfiltered (gdb_stdlog
,
1853 "LLW: Using pending wait status %s for %s.\n",
1854 status_to_str (status
),
1855 target_pid_to_str (lp
->ptid
));
1858 /* But if we don't fine one, we'll have to wait, and check both
1859 cloned and uncloned processes. We start with the cloned
1861 options
= __WCLONE
| WNOHANG
;
1863 else if (is_lwp (ptid
))
1865 if (debug_linux_nat
)
1866 fprintf_unfiltered (gdb_stdlog
,
1867 "LLW: Waiting for specific LWP %s.\n",
1868 target_pid_to_str (ptid
));
1870 /* We have a specific LWP to check. */
1871 lp
= find_lwp_pid (ptid
);
1873 status
= lp
->status
;
1876 if (debug_linux_nat
&& status
)
1877 fprintf_unfiltered (gdb_stdlog
,
1878 "LLW: Using pending wait status %s for %s.\n",
1879 status_to_str (status
),
1880 target_pid_to_str (lp
->ptid
));
1882 /* If we have to wait, take into account whether PID is a cloned
1883 process or not. And we have to convert it to something that
1884 the layer beneath us can understand. */
1885 options
= lp
->cloned
? __WCLONE
: 0;
1886 pid
= GET_LWP (ptid
);
1889 if (status
&& lp
->signalled
)
1891 /* A pending SIGSTOP may interfere with the normal stream of
1892 events. In a typical case where interference is a problem,
1893 we have a SIGSTOP signal pending for LWP A while
1894 single-stepping it, encounter an event in LWP B, and take the
1895 pending SIGSTOP while trying to stop LWP A. After processing
1896 the event in LWP B, LWP A is continued, and we'll never see
1897 the SIGTRAP associated with the last time we were
1898 single-stepping LWP A. */
1900 /* Resume the thread. It should halt immediately returning the
1902 registers_changed ();
1903 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
1904 lp
->step
, TARGET_SIGNAL_0
);
1905 if (debug_linux_nat
)
1906 fprintf_unfiltered (gdb_stdlog
,
1907 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1908 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1909 target_pid_to_str (lp
->ptid
));
1911 gdb_assert (lp
->resumed
);
1913 /* This should catch the pending SIGSTOP. */
1914 stop_wait_callback (lp
, NULL
);
1917 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1918 attached process. */
1925 lwpid
= my_waitpid (pid
, &status
, options
);
1928 gdb_assert (pid
== -1 || lwpid
== pid
);
1930 if (debug_linux_nat
)
1932 fprintf_unfiltered (gdb_stdlog
,
1933 "LLW: waitpid %ld received %s\n",
1934 (long) lwpid
, status_to_str (status
));
1937 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
1939 /* Check for stop events reported by a process we didn't
1940 already know about - anything not already in our LWP
1943 If we're expecting to receive stopped processes after
1944 fork, vfork, and clone events, then we'll just add the
1945 new one to our list and go back to waiting for the event
1946 to be reported - the stopped process might be returned
1947 from waitpid before or after the event is. */
1948 if (WIFSTOPPED (status
) && !lp
)
1950 linux_record_stopped_pid (lwpid
);
1955 /* Make sure we don't report an event for the exit of an LWP not in
1956 our list, i.e. not part of the current process. This can happen
1957 if we detach from a program we original forked and then it
1959 if (!WIFSTOPPED (status
) && !lp
)
1965 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1966 CLONE_PTRACE processes which do not use the thread library -
1967 otherwise we wouldn't find the new LWP this way. That doesn't
1968 currently work, and the following code is currently unreachable
1969 due to the two blocks above. If it's fixed some day, this code
1970 should be broken out into a function so that we can also pick up
1971 LWPs from the new interface. */
1974 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
1975 if (options
& __WCLONE
)
1980 gdb_assert (WIFSTOPPED (status
)
1981 && WSTOPSIG (status
) == SIGSTOP
);
1984 if (!in_thread_list (inferior_ptid
))
1986 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
1987 GET_PID (inferior_ptid
));
1988 add_thread (inferior_ptid
);
1991 add_thread (lp
->ptid
);
1992 printf_unfiltered (_("[New %s]\n"),
1993 target_pid_to_str (lp
->ptid
));
1997 /* Handle GNU/Linux's extended waitstatus for trace events. */
1998 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2000 if (debug_linux_nat
)
2001 fprintf_unfiltered (gdb_stdlog
,
2002 "LLW: Handling extended status 0x%06x\n",
2004 if (linux_nat_handle_extended (lp
, status
))
2011 /* Check if the thread has exited. */
2012 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
2014 if (in_thread_list (lp
->ptid
))
2016 /* Core GDB cannot deal with us deleting the current
2018 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2019 delete_thread (lp
->ptid
);
2020 printf_unfiltered (_("[%s exited]\n"),
2021 target_pid_to_str (lp
->ptid
));
2024 /* If this is the main thread, we must stop all threads and
2025 verify if they are still alive. This is because in the nptl
2026 thread model, there is no signal issued for exiting LWPs
2027 other than the main thread. We only get the main thread
2028 exit signal once all child threads have already exited.
2029 If we stop all the threads and use the stop_wait_callback
2030 to check if they have exited we can determine whether this
2031 signal should be ignored or whether it means the end of the
2032 debugged application, regardless of which threading model
2034 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2037 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2040 if (debug_linux_nat
)
2041 fprintf_unfiltered (gdb_stdlog
,
2042 "LLW: %s exited.\n",
2043 target_pid_to_str (lp
->ptid
));
2045 delete_lwp (lp
->ptid
);
2047 /* If there is at least one more LWP, then the exit signal
2048 was not the end of the debugged application and should be
2052 /* Make sure there is at least one thread running. */
2053 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2055 /* Discard the event. */
2061 /* Check if the current LWP has previously exited. In the nptl
2062 thread model, LWPs other than the main thread do not issue
2063 signals when they exit so we must check whenever the thread
2064 has stopped. A similar check is made in stop_wait_callback(). */
2065 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2067 if (in_thread_list (lp
->ptid
))
2069 /* Core GDB cannot deal with us deleting the current
2071 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2072 delete_thread (lp
->ptid
);
2073 printf_unfiltered (_("[%s exited]\n"),
2074 target_pid_to_str (lp
->ptid
));
2076 if (debug_linux_nat
)
2077 fprintf_unfiltered (gdb_stdlog
,
2078 "LLW: %s exited.\n",
2079 target_pid_to_str (lp
->ptid
));
2081 delete_lwp (lp
->ptid
);
2083 /* Make sure there is at least one thread running. */
2084 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2086 /* Discard the event. */
2091 /* Make sure we don't report a SIGSTOP that we sent
2092 ourselves in an attempt to stop an LWP. */
2094 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2096 if (debug_linux_nat
)
2097 fprintf_unfiltered (gdb_stdlog
,
2098 "LLW: Delayed SIGSTOP caught for %s.\n",
2099 target_pid_to_str (lp
->ptid
));
2101 /* This is a delayed SIGSTOP. */
2104 registers_changed ();
2105 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2106 lp
->step
, TARGET_SIGNAL_0
);
2107 if (debug_linux_nat
)
2108 fprintf_unfiltered (gdb_stdlog
,
2109 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2111 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2112 target_pid_to_str (lp
->ptid
));
2115 gdb_assert (lp
->resumed
);
2117 /* Discard the event. */
2127 /* Alternate between checking cloned and uncloned processes. */
2128 options
^= __WCLONE
;
2130 /* And suspend every time we have checked both. */
2131 if (options
& __WCLONE
)
2132 sigsuspend (&suspend_mask
);
2135 /* We shouldn't end up here unless we want to try again. */
2136 gdb_assert (status
== 0);
2139 clear_sigio_trap ();
2140 clear_sigint_trap ();
2144 /* Don't report signals that GDB isn't interested in, such as
2145 signals that are neither printed nor stopped upon. Stopping all
2146 threads can be a bit time-consuming so if we want decent
2147 performance with heavily multi-threaded programs, especially when
2148 they're using a high frequency timer, we'd better avoid it if we
2151 if (WIFSTOPPED (status
))
2153 int signo
= target_signal_from_host (WSTOPSIG (status
));
2155 if (signal_stop_state (signo
) == 0
2156 && signal_print_state (signo
) == 0
2157 && signal_pass_state (signo
) == 1)
2159 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2160 here? It is not clear we should. GDB may not expect
2161 other threads to run. On the other hand, not resuming
2162 newly attached threads may cause an unwanted delay in
2163 getting them running. */
2164 registers_changed ();
2165 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2167 if (debug_linux_nat
)
2168 fprintf_unfiltered (gdb_stdlog
,
2169 "LLW: %s %s, %s (preempt 'handle')\n",
2171 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2172 target_pid_to_str (lp
->ptid
),
2173 signo
? strsignal (signo
) : "0");
2179 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2181 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2182 forwarded to the entire process group, that is, all LWP's
2183 will receive it. Since we only want to report it once,
2184 we try to flush it from all LWPs except this one. */
2185 sigaddset (&flush_mask
, SIGINT
);
2189 /* This LWP is stopped now. */
2192 if (debug_linux_nat
)
2193 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2194 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2196 /* Now stop all other LWP's ... */
2197 iterate_over_lwps (stop_callback
, NULL
);
2199 /* ... and wait until all of them have reported back that they're no
2201 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2202 iterate_over_lwps (flush_callback
, &flush_mask
);
2204 /* If we're not waiting for a specific LWP, choose an event LWP from
2205 among those that have had events. Giving equal priority to all
2206 LWPs that have had events helps prevent starvation. */
2208 select_event_lwp (&lp
, &status
);
2210 /* Now that we've selected our final event LWP, cancel any
2211 breakpoints in other LWPs that have hit a GDB breakpoint. See
2212 the comment in cancel_breakpoints_callback to find out why. */
2213 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2215 /* If we're not running in "threaded" mode, we'll report the bare
2218 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2220 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2221 if (debug_linux_nat
)
2222 fprintf_unfiltered (gdb_stdlog
,
2223 "LLW: trap_ptid is %s.\n",
2224 target_pid_to_str (trap_ptid
));
2227 trap_ptid
= null_ptid
;
2229 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2231 *ourstatus
= lp
->waitstatus
;
2232 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2235 store_waitstatus (ourstatus
, status
);
2237 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2241 kill_callback (struct lwp_info
*lp
, void *data
)
2244 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2245 if (debug_linux_nat
)
2246 fprintf_unfiltered (gdb_stdlog
,
2247 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2248 target_pid_to_str (lp
->ptid
),
2249 errno
? safe_strerror (errno
) : "OK");
2255 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2259 /* We must make sure that there are no pending events (delayed
2260 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2261 program doesn't interfere with any following debugging session. */
2263 /* For cloned processes we must check both with __WCLONE and
2264 without, since the exit status of a cloned process isn't reported
2270 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2271 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2273 fprintf_unfiltered (gdb_stdlog
,
2274 "KWC: wait %s received unknown.\n",
2275 target_pid_to_str (lp
->ptid
));
2278 while (pid
== GET_LWP (lp
->ptid
));
2280 gdb_assert (pid
== -1 && errno
== ECHILD
);
2285 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2286 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2288 fprintf_unfiltered (gdb_stdlog
,
2289 "KWC: wait %s received unk.\n",
2290 target_pid_to_str (lp
->ptid
));
2293 while (pid
== GET_LWP (lp
->ptid
));
2295 gdb_assert (pid
== -1 && errno
== ECHILD
);
2300 linux_nat_kill (void)
2302 /* Kill all LWP's ... */
2303 iterate_over_lwps (kill_callback
, NULL
);
2305 /* ... and wait until we've flushed all events. */
2306 iterate_over_lwps (kill_wait_callback
, NULL
);
2308 target_mourn_inferior ();
2312 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
2315 linux_ops
->to_create_inferior (exec_file
, allargs
, env
, from_tty
);
2319 linux_nat_mourn_inferior (void)
2321 trap_ptid
= null_ptid
;
2323 /* Destroy LWP info; it's no longer valid. */
2326 /* Restore the original signal mask. */
2327 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
2328 sigemptyset (&blocked_mask
);
2330 linux_ops
->to_mourn_inferior ();
2334 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2335 const char *annex
, gdb_byte
*readbuf
,
2336 const gdb_byte
*writebuf
,
2337 ULONGEST offset
, LONGEST len
)
2339 struct cleanup
*old_chain
= save_inferior_ptid ();
2342 if (is_lwp (inferior_ptid
))
2343 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2345 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
2348 do_cleanups (old_chain
);
2353 linux_nat_thread_alive (ptid_t ptid
)
2355 gdb_assert (is_lwp (ptid
));
2358 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2359 if (debug_linux_nat
)
2360 fprintf_unfiltered (gdb_stdlog
,
2361 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2362 target_pid_to_str (ptid
),
2363 errno
? safe_strerror (errno
) : "OK");
2371 linux_nat_pid_to_str (ptid_t ptid
)
2373 static char buf
[64];
2377 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2381 return normal_pid_to_str (ptid
);
2385 linux_nat_fetch_registers (int regnum
)
2387 /* to_fetch_registers will honor the LWP ID, so we can use it directly. */
2388 linux_ops
->to_fetch_registers (regnum
);
2392 linux_nat_store_registers (int regnum
)
2394 /* to_store_registers will honor the LWP ID, so we can use it directly. */
2395 linux_ops
->to_store_registers (regnum
);
2399 linux_nat_child_post_startup_inferior (ptid_t ptid
)
2401 linux_ops
->to_post_startup_inferior (ptid
);
2405 init_linux_nat_ops (void)
2408 linux_nat_ops
.to_open
= linux_nat_open
;
2410 linux_nat_ops
.to_shortname
= "lwp-layer";
2411 linux_nat_ops
.to_longname
= "lwp-layer";
2412 linux_nat_ops
.to_doc
= "Low level threads support (LWP layer)";
2413 linux_nat_ops
.to_attach
= linux_nat_attach
;
2414 linux_nat_ops
.to_detach
= linux_nat_detach
;
2415 linux_nat_ops
.to_resume
= linux_nat_resume
;
2416 linux_nat_ops
.to_wait
= linux_nat_wait
;
2417 linux_nat_ops
.to_fetch_registers
= linux_nat_fetch_registers
;
2418 linux_nat_ops
.to_store_registers
= linux_nat_store_registers
;
2419 linux_nat_ops
.to_xfer_partial
= linux_nat_xfer_partial
;
2420 linux_nat_ops
.to_kill
= linux_nat_kill
;
2421 linux_nat_ops
.to_create_inferior
= linux_nat_create_inferior
;
2422 linux_nat_ops
.to_mourn_inferior
= linux_nat_mourn_inferior
;
2423 linux_nat_ops
.to_thread_alive
= linux_nat_thread_alive
;
2424 linux_nat_ops
.to_pid_to_str
= linux_nat_pid_to_str
;
2425 linux_nat_ops
.to_post_startup_inferior
2426 = linux_nat_child_post_startup_inferior
;
2427 linux_nat_ops
.to_post_attach
= child_post_attach
;
2428 linux_nat_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
2429 linux_nat_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
2430 linux_nat_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
2432 linux_nat_ops
.to_stratum
= thread_stratum
;
2433 linux_nat_ops
.to_has_thread_control
= tc_schedlock
;
2434 linux_nat_ops
.to_magic
= OPS_MAGIC
;
2438 sigchld_handler (int signo
)
2440 /* Do nothing. The only reason for this handler is that it allows
2441 us to use sigsuspend in linux_nat_wait above to wait for the
2442 arrival of a SIGCHLD. */
2445 /* Accepts an integer PID; Returns a string representing a file that
2446 can be opened to get the symbols for the child process. */
2449 child_pid_to_exec_file (int pid
)
2451 char *name1
, *name2
;
2453 name1
= xmalloc (MAXPATHLEN
);
2454 name2
= xmalloc (MAXPATHLEN
);
2455 make_cleanup (xfree
, name1
);
2456 make_cleanup (xfree
, name2
);
2457 memset (name2
, 0, MAXPATHLEN
);
2459 sprintf (name1
, "/proc/%d/exe", pid
);
2460 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
2466 /* Service function for corefiles and info proc. */
2469 read_mapping (FILE *mapfile
,
2474 char *device
, long long *inode
, char *filename
)
2476 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
2477 addr
, endaddr
, permissions
, offset
, device
, inode
);
2480 if (ret
> 0 && ret
!= EOF
)
2482 /* Eat everything up to EOL for the filename. This will prevent
2483 weird filenames (such as one with embedded whitespace) from
2484 confusing this code. It also makes this code more robust in
2485 respect to annotations the kernel may add after the filename.
2487 Note the filename is used for informational purposes
2489 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
2492 return (ret
!= 0 && ret
!= EOF
);
2495 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2496 regions in the inferior for a corefile. */
2499 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
2501 int, int, int, void *), void *obfd
)
2503 long long pid
= PIDGET (inferior_ptid
);
2504 char mapsfilename
[MAXPATHLEN
];
2506 long long addr
, endaddr
, size
, offset
, inode
;
2507 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2508 int read
, write
, exec
;
2511 /* Compose the filename for the /proc memory map, and open it. */
2512 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
2513 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
2514 error (_("Could not open %s."), mapsfilename
);
2517 fprintf_filtered (gdb_stdout
,
2518 "Reading memory regions from %s\n", mapsfilename
);
2520 /* Now iterate until end-of-file. */
2521 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
2522 &offset
, &device
[0], &inode
, &filename
[0]))
2524 size
= endaddr
- addr
;
2526 /* Get the segment's permissions. */
2527 read
= (strchr (permissions
, 'r') != 0);
2528 write
= (strchr (permissions
, 'w') != 0);
2529 exec
= (strchr (permissions
, 'x') != 0);
2533 fprintf_filtered (gdb_stdout
,
2534 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2535 size
, paddr_nz (addr
),
2537 write
? 'w' : ' ', exec
? 'x' : ' ');
2538 if (filename
&& filename
[0])
2539 fprintf_filtered (gdb_stdout
, " for %s", filename
);
2540 fprintf_filtered (gdb_stdout
, "\n");
2543 /* Invoke the callback function to create the corefile
2545 func (addr
, size
, read
, write
, exec
, obfd
);
2551 /* Records the thread's register state for the corefile note
2555 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
2556 char *note_data
, int *note_size
)
2558 gdb_gregset_t gregs
;
2559 gdb_fpregset_t fpregs
;
2560 #ifdef FILL_FPXREGSET
2561 gdb_fpxregset_t fpxregs
;
2563 unsigned long lwp
= ptid_get_lwp (ptid
);
2565 fill_gregset (&gregs
, -1);
2566 note_data
= (char *) elfcore_write_prstatus (obfd
,
2570 stop_signal
, &gregs
);
2572 fill_fpregset (&fpregs
, -1);
2573 note_data
= (char *) elfcore_write_prfpreg (obfd
,
2576 &fpregs
, sizeof (fpregs
));
2577 #ifdef FILL_FPXREGSET
2578 fill_fpxregset (&fpxregs
, -1);
2579 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
2582 &fpxregs
, sizeof (fpxregs
));
2587 struct linux_nat_corefile_thread_data
2595 /* Called by gdbthread.c once per thread. Records the thread's
2596 register state for the corefile note section. */
2599 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
2601 struct linux_nat_corefile_thread_data
*args
= data
;
2602 ptid_t saved_ptid
= inferior_ptid
;
2604 inferior_ptid
= ti
->ptid
;
2605 registers_changed ();
2606 target_fetch_registers (-1); /* FIXME should not be necessary;
2607 fill_gregset should do it automatically. */
2608 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
2613 inferior_ptid
= saved_ptid
;
2614 registers_changed ();
2615 target_fetch_registers (-1); /* FIXME should not be necessary;
2616 fill_gregset should do it automatically. */
2620 /* Records the register state for the corefile note section. */
2623 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
2624 char *note_data
, int *note_size
)
2626 registers_changed ();
2627 target_fetch_registers (-1); /* FIXME should not be necessary;
2628 fill_gregset should do it automatically. */
2629 return linux_nat_do_thread_registers (obfd
,
2630 ptid_build (ptid_get_pid (inferior_ptid
),
2631 ptid_get_pid (inferior_ptid
),
2633 note_data
, note_size
);
2637 /* Fills the "to_make_corefile_note" target vector. Builds the note
2638 section for a corefile, and returns it in a malloc buffer. */
2641 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
2643 struct linux_nat_corefile_thread_data thread_args
;
2644 struct cleanup
*old_chain
;
2645 char fname
[16] = { '\0' };
2646 char psargs
[80] = { '\0' };
2647 char *note_data
= NULL
;
2648 ptid_t current_ptid
= inferior_ptid
;
2652 if (get_exec_file (0))
2654 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
2655 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
2656 if (get_inferior_args ())
2658 strncat (psargs
, " ", sizeof (psargs
) - strlen (psargs
));
2659 strncat (psargs
, get_inferior_args (),
2660 sizeof (psargs
) - strlen (psargs
));
2662 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
2664 note_size
, fname
, psargs
);
2667 /* Dump information for threads. */
2668 thread_args
.obfd
= obfd
;
2669 thread_args
.note_data
= note_data
;
2670 thread_args
.note_size
= note_size
;
2671 thread_args
.num_notes
= 0;
2672 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
2673 if (thread_args
.num_notes
== 0)
2675 /* iterate_over_threads didn't come up with any threads; just
2676 use inferior_ptid. */
2677 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
2678 note_data
, note_size
);
2682 note_data
= thread_args
.note_data
;
2685 auxv_len
= target_auxv_read (¤t_target
, &auxv
);
2688 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
2689 "CORE", NT_AUXV
, auxv
, auxv_len
);
2693 make_cleanup (xfree
, note_data
);
2697 /* Implement the "info proc" command. */
2700 linux_nat_info_proc_cmd (char *args
, int from_tty
)
2702 long long pid
= PIDGET (inferior_ptid
);
2705 char buffer
[MAXPATHLEN
];
2706 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
2719 /* Break up 'args' into an argv array. */
2720 if ((argv
= buildargv (args
)) == NULL
)
2723 make_cleanup_freeargv (argv
);
2725 while (argv
!= NULL
&& *argv
!= NULL
)
2727 if (isdigit (argv
[0][0]))
2729 pid
= strtoul (argv
[0], NULL
, 10);
2731 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
2735 else if (strcmp (argv
[0], "status") == 0)
2739 else if (strcmp (argv
[0], "stat") == 0)
2743 else if (strcmp (argv
[0], "cmd") == 0)
2747 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
2751 else if (strcmp (argv
[0], "cwd") == 0)
2755 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
2761 /* [...] (future options here) */
2766 error (_("No current process: you must name one."));
2768 sprintf (fname1
, "/proc/%lld", pid
);
2769 if (stat (fname1
, &dummy
) != 0)
2770 error (_("No /proc directory: '%s'"), fname1
);
2772 printf_filtered (_("process %lld\n"), pid
);
2773 if (cmdline_f
|| all
)
2775 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
2776 if ((procfile
= fopen (fname1
, "r")) > 0)
2778 fgets (buffer
, sizeof (buffer
), procfile
);
2779 printf_filtered ("cmdline = '%s'\n", buffer
);
2783 warning (_("unable to open /proc file '%s'"), fname1
);
2787 sprintf (fname1
, "/proc/%lld/cwd", pid
);
2788 memset (fname2
, 0, sizeof (fname2
));
2789 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2790 printf_filtered ("cwd = '%s'\n", fname2
);
2792 warning (_("unable to read link '%s'"), fname1
);
2796 sprintf (fname1
, "/proc/%lld/exe", pid
);
2797 memset (fname2
, 0, sizeof (fname2
));
2798 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2799 printf_filtered ("exe = '%s'\n", fname2
);
2801 warning (_("unable to read link '%s'"), fname1
);
2803 if (mappings_f
|| all
)
2805 sprintf (fname1
, "/proc/%lld/maps", pid
);
2806 if ((procfile
= fopen (fname1
, "r")) > 0)
2808 long long addr
, endaddr
, size
, offset
, inode
;
2809 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2811 printf_filtered (_("Mapped address spaces:\n\n"));
2812 if (TARGET_ADDR_BIT
== 32)
2814 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2817 " Size", " Offset", "objfile");
2821 printf_filtered (" %18s %18s %10s %10s %7s\n",
2824 " Size", " Offset", "objfile");
2827 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
2828 &offset
, &device
[0], &inode
, &filename
[0]))
2830 size
= endaddr
- addr
;
2832 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2833 calls here (and possibly above) should be abstracted
2834 out into their own functions? Andrew suggests using
2835 a generic local_address_string instead to print out
2836 the addresses; that makes sense to me, too. */
2838 if (TARGET_ADDR_BIT
== 32)
2840 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2841 (unsigned long) addr
, /* FIXME: pr_addr */
2842 (unsigned long) endaddr
,
2844 (unsigned int) offset
,
2845 filename
[0] ? filename
: "");
2849 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2850 (unsigned long) addr
, /* FIXME: pr_addr */
2851 (unsigned long) endaddr
,
2853 (unsigned int) offset
,
2854 filename
[0] ? filename
: "");
2861 warning (_("unable to open /proc file '%s'"), fname1
);
2863 if (status_f
|| all
)
2865 sprintf (fname1
, "/proc/%lld/status", pid
);
2866 if ((procfile
= fopen (fname1
, "r")) > 0)
2868 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
2869 puts_filtered (buffer
);
2873 warning (_("unable to open /proc file '%s'"), fname1
);
2877 sprintf (fname1
, "/proc/%lld/stat", pid
);
2878 if ((procfile
= fopen (fname1
, "r")) > 0)
2883 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2884 printf_filtered (_("Process: %d\n"), itmp
);
2885 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
2886 printf_filtered (_("Exec file: %s\n"), buffer
);
2887 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
2888 printf_filtered (_("State: %c\n"), ctmp
);
2889 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2890 printf_filtered (_("Parent process: %d\n"), itmp
);
2891 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2892 printf_filtered (_("Process group: %d\n"), itmp
);
2893 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2894 printf_filtered (_("Session id: %d\n"), itmp
);
2895 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2896 printf_filtered (_("TTY: %d\n"), itmp
);
2897 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2898 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
2899 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2900 printf_filtered (_("Flags: 0x%x\n"), itmp
);
2901 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2902 printf_filtered (_("Minor faults (no memory page): %u\n"),
2903 (unsigned int) itmp
);
2904 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2905 printf_filtered (_("Minor faults, children: %u\n"),
2906 (unsigned int) itmp
);
2907 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2908 printf_filtered (_("Major faults (memory page faults): %u\n"),
2909 (unsigned int) itmp
);
2910 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2911 printf_filtered (_("Major faults, children: %u\n"),
2912 (unsigned int) itmp
);
2913 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2914 printf_filtered ("utime: %d\n", itmp
);
2915 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2916 printf_filtered ("stime: %d\n", itmp
);
2917 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2918 printf_filtered ("utime, children: %d\n", itmp
);
2919 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2920 printf_filtered ("stime, children: %d\n", itmp
);
2921 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2922 printf_filtered (_("jiffies remaining in current time slice: %d\n"),
2924 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2925 printf_filtered ("'nice' value: %d\n", itmp
);
2926 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2927 printf_filtered (_("jiffies until next timeout: %u\n"),
2928 (unsigned int) itmp
);
2929 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2930 printf_filtered ("jiffies until next SIGALRM: %u\n",
2931 (unsigned int) itmp
);
2932 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2933 printf_filtered (_("start time (jiffies since system boot): %d\n"),
2935 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2936 printf_filtered (_("Virtual memory size: %u\n"),
2937 (unsigned int) itmp
);
2938 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2939 printf_filtered (_("Resident set size: %u\n"), (unsigned int) itmp
);
2940 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2941 printf_filtered ("rlim: %u\n", (unsigned int) itmp
);
2942 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2943 printf_filtered (_("Start of text: 0x%x\n"), itmp
);
2944 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2945 printf_filtered (_("End of text: 0x%x\n"), itmp
);
2946 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2947 printf_filtered (_("Start of stack: 0x%x\n"), itmp
);
2948 #if 0 /* Don't know how architecture-dependent the rest is...
2949 Anyway the signal bitmap info is available from "status". */
2950 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2951 printf_filtered (_("Kernel stack pointer: 0x%x\n"), itmp
);
2952 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2953 printf_filtered (_("Kernel instr pointer: 0x%x\n"), itmp
);
2954 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2955 printf_filtered (_("Pending signals bitmap: 0x%x\n"), itmp
);
2956 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2957 printf_filtered (_("Blocked signals bitmap: 0x%x\n"), itmp
);
2958 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2959 printf_filtered (_("Ignored signals bitmap: 0x%x\n"), itmp
);
2960 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2961 printf_filtered (_("Catched signals bitmap: 0x%x\n"), itmp
);
2962 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2963 printf_filtered (_("wchan (system call): 0x%x\n"), itmp
);
2968 warning (_("unable to open /proc file '%s'"), fname1
);
2972 /* Implement the to_xfer_partial interface for memory reads using the /proc
2973 filesystem. Because we can use a single read() call for /proc, this
2974 can be much more efficient than banging away at PTRACE_PEEKTEXT,
2975 but it doesn't support writes. */
2978 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2979 const char *annex
, gdb_byte
*readbuf
,
2980 const gdb_byte
*writebuf
,
2981 ULONGEST offset
, LONGEST len
)
2987 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
2990 /* Don't bother for one word. */
2991 if (len
< 3 * sizeof (long))
2994 /* We could keep this file open and cache it - possibly one per
2995 thread. That requires some juggling, but is even faster. */
2996 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
2997 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
3001 /* If pread64 is available, use it. It's faster if the kernel
3002 supports it (only one syscall), and it's 64-bit safe even on
3003 32-bit platforms (for instance, SPARC debugging a SPARC64
3006 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
3008 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
3018 /* Parse LINE as a signal set and add its set bits to SIGS. */
3021 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
3023 int len
= strlen (line
) - 1;
3027 if (line
[len
] != '\n')
3028 error (_("Could not parse signal set: %s"), line
);
3036 if (*p
>= '0' && *p
<= '9')
3038 else if (*p
>= 'a' && *p
<= 'f')
3039 digit
= *p
- 'a' + 10;
3041 error (_("Could not parse signal set: %s"), line
);
3046 sigaddset (sigs
, signum
+ 1);
3048 sigaddset (sigs
, signum
+ 2);
3050 sigaddset (sigs
, signum
+ 3);
3052 sigaddset (sigs
, signum
+ 4);
3058 /* Find process PID's pending signals from /proc/pid/status and set
3062 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3065 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3068 sigemptyset (pending
);
3069 sigemptyset (blocked
);
3070 sigemptyset (ignored
);
3071 sprintf (fname
, "/proc/%d/status", pid
);
3072 procfile
= fopen (fname
, "r");
3073 if (procfile
== NULL
)
3074 error (_("Could not open %s"), fname
);
3076 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3078 /* Normal queued signals are on the SigPnd line in the status
3079 file. However, 2.6 kernels also have a "shared" pending
3080 queue for delivering signals to a thread group, so check for
3083 Unfortunately some Red Hat kernels include the shared pending
3084 queue but not the ShdPnd status field. */
3086 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3087 add_line_to_sigset (buffer
+ 8, pending
);
3088 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3089 add_line_to_sigset (buffer
+ 8, pending
);
3090 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3091 add_line_to_sigset (buffer
+ 8, blocked
);
3092 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3093 add_line_to_sigset (buffer
+ 8, ignored
);
3100 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3101 const char *annex
, gdb_byte
*readbuf
,
3102 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3106 if (object
== TARGET_OBJECT_AUXV
)
3107 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
3110 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3115 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3119 #ifndef FETCH_INFERIOR_REGISTERS
3121 /* Return the address in the core dump or inferior of register
3125 linux_register_u_offset (int regno
)
3127 /* FIXME drow/2005-09-04: The hardcoded use of register_addr should go
3128 away. This requires disentangling the various definitions of it
3129 (particularly alpha-nat.c's). */
3130 return register_addr (regno
, 0);
3135 /* Create a prototype generic Linux target. The client can override
3136 it with local methods. */
3141 struct target_ops
*t
;
3143 #ifdef FETCH_INFERIOR_REGISTERS
3144 t
= inf_ptrace_target ();
3146 t
= inf_ptrace_trad_target (linux_register_u_offset
);
3148 t
->to_wait
= child_wait
;
3149 t
->to_kill
= kill_inferior
;
3150 t
->to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
3151 t
->to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
3152 t
->to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
3153 t
->to_pid_to_exec_file
= child_pid_to_exec_file
;
3154 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
3155 t
->to_post_attach
= child_post_attach
;
3156 t
->to_follow_fork
= child_follow_fork
;
3157 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
3158 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3160 super_xfer_partial
= t
->to_xfer_partial
;
3161 t
->to_xfer_partial
= linux_xfer_partial
;
3168 _initialize_linux_nat (void)
3170 struct sigaction action
;
3171 extern void thread_db_init (struct target_ops
*);
3173 add_info ("proc", linux_nat_info_proc_cmd
, _("\
3174 Show /proc process information about any running process.\n\
3175 Specify any process id, or use the program being debugged by default.\n\
3176 Specify any of the following keywords for detailed info:\n\
3177 mappings -- list of mapped memory regions.\n\
3178 stat -- list a bunch of random process info.\n\
3179 status -- list a different bunch of random process info.\n\
3180 all -- list all available /proc info."));
3182 init_linux_nat_ops ();
3183 add_target (&linux_nat_ops
);
3184 thread_db_init (&linux_nat_ops
);
3186 /* Save the original signal mask. */
3187 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
3189 action
.sa_handler
= sigchld_handler
;
3190 sigemptyset (&action
.sa_mask
);
3191 action
.sa_flags
= SA_RESTART
;
3192 sigaction (SIGCHLD
, &action
, NULL
);
3194 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3195 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
3196 sigdelset (&suspend_mask
, SIGCHLD
);
3198 sigemptyset (&blocked_mask
);
3200 add_setshow_zinteger_cmd ("lin-lwp", no_class
, &debug_linux_nat
, _("\
3201 Set debugging of GNU/Linux lwp module."), _("\
3202 Show debugging of GNU/Linux lwp module."), _("\
3203 Enables printf debugging output."),
3205 show_debug_linux_nat
,
3206 &setdebuglist
, &showdebuglist
);
3210 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3211 the GNU/Linux Threads library and therefore doesn't really belong
3214 /* Read variable NAME in the target and return its value if found.
3215 Otherwise return zero. It is assumed that the type of the variable
3219 get_signo (const char *name
)
3221 struct minimal_symbol
*ms
;
3224 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
3228 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
3229 sizeof (signo
)) != 0)
3235 /* Return the set of signals used by the threads library in *SET. */
3238 lin_thread_get_thread_signals (sigset_t
*set
)
3240 struct sigaction action
;
3241 int restart
, cancel
;
3245 restart
= get_signo ("__pthread_sig_restart");
3249 cancel
= get_signo ("__pthread_sig_cancel");
3253 sigaddset (set
, restart
);
3254 sigaddset (set
, cancel
);
3256 /* The GNU/Linux Threads library makes terminating threads send a
3257 special "cancel" signal instead of SIGCHLD. Make sure we catch
3258 those (to prevent them from terminating GDB itself, which is
3259 likely to be their default action) and treat them the same way as
3262 action
.sa_handler
= sigchld_handler
;
3263 sigemptyset (&action
.sa_mask
);
3264 action
.sa_flags
= SA_RESTART
;
3265 sigaction (cancel
, &action
, NULL
);
3267 /* We block the "cancel" signal throughout this code ... */
3268 sigaddset (&blocked_mask
, cancel
);
3269 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
3271 /* ... except during a sigsuspend. */
3272 sigdelset (&suspend_mask
, cancel
);