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 #include "linux-nat.h"
30 /* If the system headers did not provide the constants, hard-code the normal
32 #ifndef PTRACE_EVENT_FORK
34 #define PTRACE_SETOPTIONS 0x4200
35 #define PTRACE_GETEVENTMSG 0x4201
37 /* options set using PTRACE_SETOPTIONS */
38 #define PTRACE_O_TRACESYSGOOD 0x00000001
39 #define PTRACE_O_TRACEFORK 0x00000002
40 #define PTRACE_O_TRACEVFORK 0x00000004
41 #define PTRACE_O_TRACECLONE 0x00000008
42 #define PTRACE_O_TRACEEXEC 0x00000010
44 /* Wait extended result codes for the above trace options. */
45 #define PTRACE_EVENT_FORK 1
46 #define PTRACE_EVENT_VFORK 2
47 #define PTRACE_EVENT_CLONE 3
48 #define PTRACE_EVENT_EXEC 4
50 #endif /* PTRACE_EVENT_FORK */
52 /* We can't always assume that this flag is available, but all systems
53 with the ptrace event handlers also have __WALL, so it's safe to use
56 #define __WALL 0x40000000 /* Wait for any child. */
59 struct simple_pid_list
62 struct simple_pid_list
*next
;
64 struct simple_pid_list
*stopped_pids
;
66 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
67 can not be used, 1 if it can. */
69 static int linux_supports_tracefork_flag
= -1;
72 /* Trivial list manipulation functions to keep track of a list of
73 new stopped processes. */
75 add_to_pid_list (struct simple_pid_list
**listp
, int pid
)
77 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
79 new_pid
->next
= *listp
;
84 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
)
86 struct simple_pid_list
**p
;
88 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
91 struct simple_pid_list
*next
= (*p
)->next
;
100 linux_record_stopped_pid (int pid
)
102 add_to_pid_list (&stopped_pids
, pid
);
106 /* A helper function for linux_test_for_tracefork, called after fork (). */
109 linux_tracefork_child (void)
113 ptrace (PTRACE_TRACEME
, 0, 0, 0);
114 kill (getpid (), SIGSTOP
);
119 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
120 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
121 fork tracing, and let it fork. If the process exits, we assume that
122 we can't use TRACEFORK; if we get the fork notification, and we can
123 extract the new child's PID, then we assume that we can. */
126 linux_test_for_tracefork (void)
128 int child_pid
, ret
, status
;
133 perror_with_name ("linux_test_for_tracefork: fork");
136 linux_tracefork_child ();
138 ret
= waitpid (child_pid
, &status
, 0);
140 perror_with_name ("linux_test_for_tracefork: waitpid");
141 else if (ret
!= child_pid
)
142 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
143 if (! WIFSTOPPED (status
))
144 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
146 linux_supports_tracefork_flag
= 0;
148 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
151 ptrace (PTRACE_KILL
, child_pid
, 0, 0);
152 waitpid (child_pid
, &status
, 0);
156 ptrace (PTRACE_CONT
, child_pid
, 0, 0);
157 ret
= waitpid (child_pid
, &status
, 0);
158 if (ret
== child_pid
&& WIFSTOPPED (status
)
159 && status
>> 16 == PTRACE_EVENT_FORK
)
162 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
163 if (ret
== 0 && second_pid
!= 0)
167 linux_supports_tracefork_flag
= 1;
168 waitpid (second_pid
, &second_status
, 0);
169 ptrace (PTRACE_DETACH
, second_pid
, 0, 0);
173 if (WIFSTOPPED (status
))
175 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
176 waitpid (child_pid
, &status
, 0);
180 /* Return non-zero iff we have tracefork functionality available.
181 This function also sets linux_supports_tracefork_flag. */
184 linux_supports_tracefork (void)
186 if (linux_supports_tracefork_flag
== -1)
187 linux_test_for_tracefork ();
188 return linux_supports_tracefork_flag
;
193 child_insert_fork_catchpoint (int pid
)
195 if (linux_supports_tracefork ())
196 error ("Fork catchpoints have not been implemented yet.");
198 error ("Your system does not support fork catchpoints.");
202 child_insert_vfork_catchpoint (int pid
)
204 if (linux_supports_tracefork ())
205 error ("Vfork catchpoints have not been implemented yet.");
207 error ("Your system does not support vfork catchpoints.");
211 child_insert_exec_catchpoint (int pid
)
213 if (linux_supports_tracefork ())
214 error ("Exec catchpoints have not been implemented yet.");
216 error ("Your system does not support exec catchpoints.");