1 /* GNU/Linux native-dependent code common to multiple platforms.
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include <sys/ptrace.h>
28 /* If the system headers did not provide the constants, hard-code the normal
30 #ifndef PTRACE_EVENT_FORK
32 #define PTRACE_SETOPTIONS 0x4200
33 #define PTRACE_GETEVENTMSG 0x4201
35 /* options set using PTRACE_SETOPTIONS */
36 #define PTRACE_O_TRACESYSGOOD 0x00000001
37 #define PTRACE_O_TRACEFORK 0x00000002
38 #define PTRACE_O_TRACEVFORK 0x00000004
39 #define PTRACE_O_TRACECLONE 0x00000008
40 #define PTRACE_O_TRACEEXEC 0x00000010
42 /* Wait extended result codes for the above trace options. */
43 #define PTRACE_EVENT_FORK 1
44 #define PTRACE_EVENT_VFORK 2
45 #define PTRACE_EVENT_CLONE 3
46 #define PTRACE_EVENT_EXEC 4
48 #endif /* PTRACE_EVENT_FORK */
50 /* We can't always assume that this flag is available, but all systems
51 with the ptrace event handlers also have __WALL, so it's safe to use
54 #define __WALL 0x40000000 /* Wait for any child. */
57 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
58 can not be used, 1 if it can. */
60 static int linux_supports_tracefork_flag
= -1;
63 /* A helper function for linux_test_for_tracefork, called after fork (). */
66 linux_tracefork_child (void)
70 ptrace (PTRACE_TRACEME
, 0, 0, 0);
71 kill (getpid (), SIGSTOP
);
76 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
77 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
78 fork tracing, and let it fork. If the process exits, we assume that
79 we can't use TRACEFORK; if we get the fork notification, and we can
80 extract the new child's PID, then we assume that we can. */
83 linux_test_for_tracefork (void)
85 int child_pid
, ret
, status
;
90 perror_with_name ("linux_test_for_tracefork: fork");
93 linux_tracefork_child ();
95 ret
= waitpid (child_pid
, &status
, 0);
97 perror_with_name ("linux_test_for_tracefork: waitpid");
98 else if (ret
!= child_pid
)
99 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
100 if (! WIFSTOPPED (status
))
101 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
103 linux_supports_tracefork_flag
= 0;
105 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
108 ptrace (PTRACE_KILL
, child_pid
, 0, 0);
109 waitpid (child_pid
, &status
, 0);
113 ptrace (PTRACE_CONT
, child_pid
, 0, 0);
114 ret
= waitpid (child_pid
, &status
, 0);
115 if (ret
== child_pid
&& WIFSTOPPED (status
)
116 && status
>> 16 == PTRACE_EVENT_FORK
)
119 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
120 if (ret
== 0 && second_pid
!= 0)
124 linux_supports_tracefork_flag
= 1;
125 waitpid (second_pid
, &second_status
, 0);
126 ptrace (PTRACE_DETACH
, second_pid
, 0, 0);
130 if (WIFSTOPPED (status
))
132 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
133 waitpid (child_pid
, &status
, 0);
137 /* Return non-zero iff we have tracefork functionality available.
138 This function also sets linux_supports_tracefork_flag. */
141 linux_supports_tracefork (void)
143 if (linux_supports_tracefork_flag
== -1)
144 linux_test_for_tracefork ();
145 return linux_supports_tracefork_flag
;
150 child_insert_fork_catchpoint (int pid
)
152 if (linux_supports_tracefork ())
153 error ("Fork catchpoints have not been implemented yet.");
155 error ("Your system does not support fork catchpoints.");
159 child_insert_vfork_catchpoint (int pid
)
161 if (linux_supports_tracefork ())
162 error ("Vfork catchpoints have not been implemented yet.");
164 error ("Your system does not support vfork catchpoints.");
168 child_insert_exec_catchpoint (int pid
)
170 if (linux_supports_tracefork ())
171 error ("Exec catchpoints have not been implemented yet.");
173 error ("Your system does not support exec catchpoints.");