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 struct simple_pid_list
60 struct simple_pid_list
*next
;
62 struct simple_pid_list
*stopped_pids
;
64 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
65 can not be used, 1 if it can. */
67 static int linux_supports_tracefork_flag
= -1;
70 /* Trivial list manipulation functions to keep track of a list of
71 new stopped processes. */
73 add_to_pid_list (struct simple_pid_list
**listp
, int pid
)
75 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
77 new_pid
->next
= *listp
;
82 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
)
84 struct simple_pid_list
**p
;
86 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
89 struct simple_pid_list
*next
= (*p
)->next
;
98 linux_record_stopped_pid (int pid
)
100 add_to_pid_list (&stopped_pids
, pid
);
104 /* A helper function for linux_test_for_tracefork, called after fork (). */
107 linux_tracefork_child (void)
111 ptrace (PTRACE_TRACEME
, 0, 0, 0);
112 kill (getpid (), SIGSTOP
);
117 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
118 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
119 fork tracing, and let it fork. If the process exits, we assume that
120 we can't use TRACEFORK; if we get the fork notification, and we can
121 extract the new child's PID, then we assume that we can. */
124 linux_test_for_tracefork (void)
126 int child_pid
, ret
, status
;
131 perror_with_name ("linux_test_for_tracefork: fork");
134 linux_tracefork_child ();
136 ret
= waitpid (child_pid
, &status
, 0);
138 perror_with_name ("linux_test_for_tracefork: waitpid");
139 else if (ret
!= child_pid
)
140 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
141 if (! WIFSTOPPED (status
))
142 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
144 linux_supports_tracefork_flag
= 0;
146 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
149 ptrace (PTRACE_KILL
, child_pid
, 0, 0);
150 waitpid (child_pid
, &status
, 0);
154 ptrace (PTRACE_CONT
, child_pid
, 0, 0);
155 ret
= waitpid (child_pid
, &status
, 0);
156 if (ret
== child_pid
&& WIFSTOPPED (status
)
157 && status
>> 16 == PTRACE_EVENT_FORK
)
160 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
161 if (ret
== 0 && second_pid
!= 0)
165 linux_supports_tracefork_flag
= 1;
166 waitpid (second_pid
, &second_status
, 0);
167 ptrace (PTRACE_DETACH
, second_pid
, 0, 0);
171 if (WIFSTOPPED (status
))
173 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
174 waitpid (child_pid
, &status
, 0);
178 /* Return non-zero iff we have tracefork functionality available.
179 This function also sets linux_supports_tracefork_flag. */
182 linux_supports_tracefork (void)
184 if (linux_supports_tracefork_flag
== -1)
185 linux_test_for_tracefork ();
186 return linux_supports_tracefork_flag
;
191 child_insert_fork_catchpoint (int pid
)
193 if (linux_supports_tracefork ())
194 error ("Fork catchpoints have not been implemented yet.");
196 error ("Your system does not support fork catchpoints.");
200 child_insert_vfork_catchpoint (int pid
)
202 if (linux_supports_tracefork ())
203 error ("Vfork catchpoints have not been implemented yet.");
205 error ("Your system does not support vfork catchpoints.");
209 child_insert_exec_catchpoint (int pid
)
211 if (linux_supports_tracefork ())
212 error ("Exec catchpoints have not been implemented yet.");
214 error ("Your system does not support exec catchpoints.");