* Makefile.in (i386-linux-nat.o): Update dependencies.
[deliverable/binutils-gdb.git] / gdb / linux-nat.c
1 /* GNU/Linux native-dependent code common to multiple platforms.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21 #include "defs.h"
22 #include "inferior.h"
23 #include "target.h"
24
25 #include "gdb_wait.h"
26 #include <sys/ptrace.h>
27
28 #include "linux-nat.h"
29
30 /* If the system headers did not provide the constants, hard-code the normal
31 values. */
32 #ifndef PTRACE_EVENT_FORK
33
34 #define PTRACE_SETOPTIONS 0x4200
35 #define PTRACE_GETEVENTMSG 0x4201
36
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
43
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
49
50 #endif /* PTRACE_EVENT_FORK */
51
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
54 here. */
55 #ifndef __WALL
56 #define __WALL 0x40000000 /* Wait for any child. */
57 #endif
58
59 extern struct target_ops child_ops;
60
61 struct simple_pid_list
62 {
63 int pid;
64 struct simple_pid_list *next;
65 };
66 struct simple_pid_list *stopped_pids;
67
68 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
69 can not be used, 1 if it can. */
70
71 static int linux_supports_tracefork_flag = -1;
72
73 \f
74 /* Trivial list manipulation functions to keep track of a list of
75 new stopped processes. */
76 static void
77 add_to_pid_list (struct simple_pid_list **listp, int pid)
78 {
79 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
80 new_pid->pid = pid;
81 new_pid->next = *listp;
82 *listp = new_pid;
83 }
84
85 static int
86 pull_pid_from_list (struct simple_pid_list **listp, int pid)
87 {
88 struct simple_pid_list **p;
89
90 for (p = listp; *p != NULL; p = &(*p)->next)
91 if ((*p)->pid == pid)
92 {
93 struct simple_pid_list *next = (*p)->next;
94 xfree (*p);
95 *p = next;
96 return 1;
97 }
98 return 0;
99 }
100
101 void
102 linux_record_stopped_pid (int pid)
103 {
104 add_to_pid_list (&stopped_pids, pid);
105 }
106
107 \f
108 /* A helper function for linux_test_for_tracefork, called after fork (). */
109
110 static void
111 linux_tracefork_child (void)
112 {
113 int ret;
114
115 ptrace (PTRACE_TRACEME, 0, 0, 0);
116 kill (getpid (), SIGSTOP);
117 fork ();
118 exit (0);
119 }
120
121 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
122 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
123 fork tracing, and let it fork. If the process exits, we assume that
124 we can't use TRACEFORK; if we get the fork notification, and we can
125 extract the new child's PID, then we assume that we can. */
126
127 static void
128 linux_test_for_tracefork (void)
129 {
130 int child_pid, ret, status;
131 long second_pid;
132
133 child_pid = fork ();
134 if (child_pid == -1)
135 perror_with_name ("linux_test_for_tracefork: fork");
136
137 if (child_pid == 0)
138 linux_tracefork_child ();
139
140 ret = waitpid (child_pid, &status, 0);
141 if (ret == -1)
142 perror_with_name ("linux_test_for_tracefork: waitpid");
143 else if (ret != child_pid)
144 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
145 if (! WIFSTOPPED (status))
146 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
147
148 linux_supports_tracefork_flag = 0;
149
150 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
151 if (ret != 0)
152 {
153 ptrace (PTRACE_KILL, child_pid, 0, 0);
154 waitpid (child_pid, &status, 0);
155 return;
156 }
157
158 ptrace (PTRACE_CONT, child_pid, 0, 0);
159 ret = waitpid (child_pid, &status, 0);
160 if (ret == child_pid && WIFSTOPPED (status)
161 && status >> 16 == PTRACE_EVENT_FORK)
162 {
163 second_pid = 0;
164 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
165 if (ret == 0 && second_pid != 0)
166 {
167 int second_status;
168
169 linux_supports_tracefork_flag = 1;
170 waitpid (second_pid, &second_status, 0);
171 ptrace (PTRACE_DETACH, second_pid, 0, 0);
172 }
173 }
174
175 if (WIFSTOPPED (status))
176 {
177 ptrace (PTRACE_DETACH, child_pid, 0, 0);
178 waitpid (child_pid, &status, 0);
179 }
180 }
181
182 /* Return non-zero iff we have tracefork functionality available.
183 This function also sets linux_supports_tracefork_flag. */
184
185 static int
186 linux_supports_tracefork (void)
187 {
188 if (linux_supports_tracefork_flag == -1)
189 linux_test_for_tracefork ();
190 return linux_supports_tracefork_flag;
191 }
192
193 \f
194 void
195 linux_enable_event_reporting (ptid_t ptid)
196 {
197 int pid = ptid_get_pid (ptid);
198 int options;
199
200 if (! linux_supports_tracefork ())
201 return;
202
203 options = PTRACE_O_TRACEFORK;
204
205 ptrace (PTRACE_SETOPTIONS, pid, 0, options);
206 }
207
208 void
209 child_post_attach (int pid)
210 {
211 linux_enable_event_reporting (pid_to_ptid (pid));
212 }
213
214 void
215 linux_child_post_startup_inferior (ptid_t ptid)
216 {
217 linux_enable_event_reporting (ptid);
218 }
219
220 #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
221 void
222 child_post_startup_inferior (ptid_t ptid)
223 {
224 linux_child_post_startup_inferior (ptid);
225 }
226 #endif
227
228 int
229 child_follow_fork (int follow_child)
230 {
231 ptid_t last_ptid;
232 struct target_waitstatus last_status;
233 int parent_pid, child_pid;
234
235 get_last_target_status (&last_ptid, &last_status);
236 parent_pid = ptid_get_pid (last_ptid);
237 child_pid = last_status.value.related_pid;
238
239 if (! follow_child)
240 {
241 /* We're already attached to the parent, by default. */
242
243 /* Before detaching from the child, remove all breakpoints from
244 it. (This won't actually modify the breakpoint list, but will
245 physically remove the breakpoints from the child.) */
246 detach_breakpoints (child_pid);
247
248 fprintf_filtered (gdb_stdout,
249 "Detaching after fork from child process %d.\n",
250 child_pid);
251
252 ptrace (PTRACE_DETACH, child_pid, 0, 0);
253 }
254 else
255 {
256 char child_pid_spelling[40];
257
258 /* Needed to keep the breakpoint lists in sync. */
259 detach_breakpoints (child_pid);
260
261 /* Before detaching from the parent, remove all breakpoints from it. */
262 remove_breakpoints ();
263
264 fprintf_filtered (gdb_stdout,
265 "Attaching after fork to child process %d.\n",
266 child_pid);
267
268 target_detach (NULL, 0);
269
270 inferior_ptid = pid_to_ptid (child_pid);
271 push_target (&child_ops);
272
273 /* Reset breakpoints in the child as appropriate. */
274 follow_inferior_reset_breakpoints ();
275 }
276
277 return 0;
278 }
279
280 ptid_t
281 linux_handle_extended_wait (int pid, int status,
282 struct target_waitstatus *ourstatus)
283 {
284 int event = status >> 16;
285
286 if (event == PTRACE_EVENT_CLONE)
287 internal_error (__FILE__, __LINE__,
288 "unexpected clone event");
289
290 if (event == PTRACE_EVENT_FORK)
291 {
292 unsigned long new_pid;
293 int ret;
294
295 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
296
297 /* If we haven't already seen the new PID stop, wait for it now. */
298 if (! pull_pid_from_list (&stopped_pids, new_pid))
299 {
300 /* The new child has a pending SIGSTOP. We can't affect it until it
301 hits the SIGSTOP, but we're already attached.
302
303 It won't be a clone (we didn't ask for clones in the event mask)
304 so we can just call waitpid and wait for the SIGSTOP. */
305 do {
306 ret = waitpid (new_pid, &status, 0);
307 } while (ret == -1 && errno == EINTR);
308 if (ret == -1)
309 perror_with_name ("waiting for new child");
310 else if (ret != new_pid)
311 internal_error (__FILE__, __LINE__,
312 "wait returned unexpected PID %d", ret);
313 else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP)
314 internal_error (__FILE__, __LINE__,
315 "wait returned unexpected status 0x%x", status);
316 }
317
318 ourstatus->kind = TARGET_WAITKIND_FORKED;
319 ourstatus->value.related_pid = new_pid;
320 return inferior_ptid;
321 }
322
323 internal_error (__FILE__, __LINE__,
324 "unknown ptrace event %d", event);
325 }
326
327 \f
328 int
329 child_insert_fork_catchpoint (int pid)
330 {
331 if (! linux_supports_tracefork ())
332 error ("Your system does not support fork catchpoints.");
333
334 return 0;
335 }
336
337 int
338 child_insert_vfork_catchpoint (int pid)
339 {
340 if (linux_supports_tracefork ())
341 error ("Vfork catchpoints have not been implemented yet.");
342 else
343 error ("Your system does not support vfork catchpoints.");
344 }
345
346 int
347 child_insert_exec_catchpoint (int pid)
348 {
349 if (linux_supports_tracefork ())
350 error ("Exec catchpoints have not been implemented yet.");
351 else
352 error ("Your system does not support exec catchpoints.");
353 }
354
355 void
356 kill_inferior (void)
357 {
358 int status;
359 int pid = PIDGET (inferior_ptid);
360 struct target_waitstatus last;
361 ptid_t last_ptid;
362 int ret;
363
364 if (pid == 0)
365 return;
366
367 /* If we're stopped while forking and we haven't followed yet, kill the
368 other task. We need to do this first because the parent will be
369 sleeping if this is a vfork. */
370
371 get_last_target_status (&last_ptid, &last);
372
373 if (last.kind == TARGET_WAITKIND_FORKED
374 || last.kind == TARGET_WAITKIND_VFORKED)
375 {
376 ptrace (PT_KILL, last.value.related_pid);
377 ptrace_wait (null_ptid, &status);
378 }
379
380 /* Kill the current process. */
381 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
382 ret = ptrace_wait (null_ptid, &status);
383
384 /* We might get a SIGCHLD instead of an exit status. This is
385 aggravated by the first kill above - a child has just died. */
386
387 while (ret == pid && WIFSTOPPED (status))
388 {
389 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
390 ret = ptrace_wait (null_ptid, &status);
391 }
392
393 target_mourn_inferior ();
394 }
This page took 0.049358 seconds and 5 git commands to generate.