Commit | Line | Data |
---|---|---|
3993f6b1 | 1 | /* GNU/Linux native-dependent code common to multiple platforms. |
a2f23071 | 2 | Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
3993f6b1 DJ |
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 | ||
0274a8ce MS |
28 | #include "linux-nat.h" |
29 | ||
3993f6b1 DJ |
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 | |
9016a515 DJ |
43 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 |
44 | #define PTRACE_O_TRACEEXIT 0x00000040 | |
3993f6b1 DJ |
45 | |
46 | /* Wait extended result codes for the above trace options. */ | |
47 | #define PTRACE_EVENT_FORK 1 | |
48 | #define PTRACE_EVENT_VFORK 2 | |
49 | #define PTRACE_EVENT_CLONE 3 | |
50 | #define PTRACE_EVENT_EXEC 4 | |
9016a515 DJ |
51 | #define PTRACE_EVENT_VFORKDONE 5 |
52 | #define PTRACE_EVENT_EXIT 6 | |
3993f6b1 DJ |
53 | |
54 | #endif /* PTRACE_EVENT_FORK */ | |
55 | ||
56 | /* We can't always assume that this flag is available, but all systems | |
57 | with the ptrace event handlers also have __WALL, so it's safe to use | |
58 | here. */ | |
59 | #ifndef __WALL | |
60 | #define __WALL 0x40000000 /* Wait for any child. */ | |
61 | #endif | |
62 | ||
4de4c07c DJ |
63 | extern struct target_ops child_ops; |
64 | ||
9016a515 DJ |
65 | static int linux_parent_pid; |
66 | ||
ae087d01 DJ |
67 | struct simple_pid_list |
68 | { | |
69 | int pid; | |
70 | struct simple_pid_list *next; | |
71 | }; | |
72 | struct simple_pid_list *stopped_pids; | |
73 | ||
3993f6b1 DJ |
74 | /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK |
75 | can not be used, 1 if it can. */ | |
76 | ||
77 | static int linux_supports_tracefork_flag = -1; | |
78 | ||
9016a515 DJ |
79 | /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have |
80 | PTRACE_O_TRACEVFORKDONE. */ | |
81 | ||
82 | static int linux_supports_tracevforkdone_flag = -1; | |
83 | ||
ae087d01 DJ |
84 | \f |
85 | /* Trivial list manipulation functions to keep track of a list of | |
86 | new stopped processes. */ | |
87 | static void | |
88 | add_to_pid_list (struct simple_pid_list **listp, int pid) | |
89 | { | |
90 | struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list)); | |
91 | new_pid->pid = pid; | |
92 | new_pid->next = *listp; | |
93 | *listp = new_pid; | |
94 | } | |
95 | ||
96 | static int | |
97 | pull_pid_from_list (struct simple_pid_list **listp, int pid) | |
98 | { | |
99 | struct simple_pid_list **p; | |
100 | ||
101 | for (p = listp; *p != NULL; p = &(*p)->next) | |
102 | if ((*p)->pid == pid) | |
103 | { | |
104 | struct simple_pid_list *next = (*p)->next; | |
105 | xfree (*p); | |
106 | *p = next; | |
107 | return 1; | |
108 | } | |
109 | return 0; | |
110 | } | |
111 | ||
112 | void | |
113 | linux_record_stopped_pid (int pid) | |
114 | { | |
115 | add_to_pid_list (&stopped_pids, pid); | |
116 | } | |
117 | ||
3993f6b1 DJ |
118 | \f |
119 | /* A helper function for linux_test_for_tracefork, called after fork (). */ | |
120 | ||
121 | static void | |
122 | linux_tracefork_child (void) | |
123 | { | |
124 | int ret; | |
125 | ||
126 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
127 | kill (getpid (), SIGSTOP); | |
128 | fork (); | |
129 | exit (0); | |
130 | } | |
131 | ||
132 | /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We | |
133 | create a child process, attach to it, use PTRACE_SETOPTIONS to enable | |
134 | fork tracing, and let it fork. If the process exits, we assume that | |
135 | we can't use TRACEFORK; if we get the fork notification, and we can | |
136 | extract the new child's PID, then we assume that we can. */ | |
137 | ||
138 | static void | |
139 | linux_test_for_tracefork (void) | |
140 | { | |
141 | int child_pid, ret, status; | |
142 | long second_pid; | |
143 | ||
144 | child_pid = fork (); | |
145 | if (child_pid == -1) | |
146 | perror_with_name ("linux_test_for_tracefork: fork"); | |
147 | ||
148 | if (child_pid == 0) | |
149 | linux_tracefork_child (); | |
150 | ||
151 | ret = waitpid (child_pid, &status, 0); | |
152 | if (ret == -1) | |
153 | perror_with_name ("linux_test_for_tracefork: waitpid"); | |
154 | else if (ret != child_pid) | |
155 | error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret); | |
156 | if (! WIFSTOPPED (status)) | |
157 | error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status); | |
158 | ||
159 | linux_supports_tracefork_flag = 0; | |
160 | ||
161 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK); | |
162 | if (ret != 0) | |
163 | { | |
164 | ptrace (PTRACE_KILL, child_pid, 0, 0); | |
165 | waitpid (child_pid, &status, 0); | |
166 | return; | |
167 | } | |
168 | ||
9016a515 DJ |
169 | /* Check whether PTRACE_O_TRACEVFORKDONE is available. */ |
170 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, | |
171 | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE); | |
172 | linux_supports_tracevforkdone_flag = (ret == 0); | |
173 | ||
3993f6b1 DJ |
174 | ptrace (PTRACE_CONT, child_pid, 0, 0); |
175 | ret = waitpid (child_pid, &status, 0); | |
176 | if (ret == child_pid && WIFSTOPPED (status) | |
177 | && status >> 16 == PTRACE_EVENT_FORK) | |
178 | { | |
179 | second_pid = 0; | |
180 | ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid); | |
181 | if (ret == 0 && second_pid != 0) | |
182 | { | |
183 | int second_status; | |
184 | ||
185 | linux_supports_tracefork_flag = 1; | |
186 | waitpid (second_pid, &second_status, 0); | |
187 | ptrace (PTRACE_DETACH, second_pid, 0, 0); | |
188 | } | |
189 | } | |
190 | ||
191 | if (WIFSTOPPED (status)) | |
192 | { | |
193 | ptrace (PTRACE_DETACH, child_pid, 0, 0); | |
194 | waitpid (child_pid, &status, 0); | |
195 | } | |
196 | } | |
197 | ||
198 | /* Return non-zero iff we have tracefork functionality available. | |
199 | This function also sets linux_supports_tracefork_flag. */ | |
200 | ||
201 | static int | |
202 | linux_supports_tracefork (void) | |
203 | { | |
204 | if (linux_supports_tracefork_flag == -1) | |
205 | linux_test_for_tracefork (); | |
206 | return linux_supports_tracefork_flag; | |
207 | } | |
208 | ||
9016a515 DJ |
209 | static int |
210 | linux_supports_tracevforkdone (void) | |
211 | { | |
212 | if (linux_supports_tracefork_flag == -1) | |
213 | linux_test_for_tracefork (); | |
214 | return linux_supports_tracevforkdone_flag; | |
215 | } | |
216 | ||
3993f6b1 | 217 | \f |
4de4c07c DJ |
218 | void |
219 | linux_enable_event_reporting (ptid_t ptid) | |
220 | { | |
221 | int pid = ptid_get_pid (ptid); | |
222 | int options; | |
223 | ||
224 | if (! linux_supports_tracefork ()) | |
225 | return; | |
226 | ||
a2f23071 DJ |
227 | options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC |
228 | | PTRACE_O_TRACECLONE; | |
9016a515 DJ |
229 | if (linux_supports_tracevforkdone ()) |
230 | options |= PTRACE_O_TRACEVFORKDONE; | |
231 | ||
232 | /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support | |
233 | read-only process state. */ | |
4de4c07c DJ |
234 | |
235 | ptrace (PTRACE_SETOPTIONS, pid, 0, options); | |
236 | } | |
237 | ||
238 | void | |
239 | child_post_attach (int pid) | |
240 | { | |
241 | linux_enable_event_reporting (pid_to_ptid (pid)); | |
242 | } | |
243 | ||
244 | void | |
245 | linux_child_post_startup_inferior (ptid_t ptid) | |
246 | { | |
247 | linux_enable_event_reporting (ptid); | |
248 | } | |
249 | ||
250 | #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR | |
251 | void | |
252 | child_post_startup_inferior (ptid_t ptid) | |
253 | { | |
254 | linux_child_post_startup_inferior (ptid); | |
255 | } | |
256 | #endif | |
257 | ||
3993f6b1 | 258 | int |
4de4c07c | 259 | child_follow_fork (int follow_child) |
3993f6b1 | 260 | { |
4de4c07c DJ |
261 | ptid_t last_ptid; |
262 | struct target_waitstatus last_status; | |
9016a515 | 263 | int has_vforked; |
4de4c07c DJ |
264 | int parent_pid, child_pid; |
265 | ||
266 | get_last_target_status (&last_ptid, &last_status); | |
9016a515 | 267 | has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED); |
4de4c07c DJ |
268 | parent_pid = ptid_get_pid (last_ptid); |
269 | child_pid = last_status.value.related_pid; | |
270 | ||
271 | if (! follow_child) | |
272 | { | |
273 | /* We're already attached to the parent, by default. */ | |
274 | ||
275 | /* Before detaching from the child, remove all breakpoints from | |
276 | it. (This won't actually modify the breakpoint list, but will | |
277 | physically remove the breakpoints from the child.) */ | |
9016a515 DJ |
278 | /* If we vforked this will remove the breakpoints from the parent |
279 | also, but they'll be reinserted below. */ | |
4de4c07c DJ |
280 | detach_breakpoints (child_pid); |
281 | ||
282 | fprintf_filtered (gdb_stdout, | |
283 | "Detaching after fork from child process %d.\n", | |
284 | child_pid); | |
285 | ||
286 | ptrace (PTRACE_DETACH, child_pid, 0, 0); | |
9016a515 DJ |
287 | |
288 | if (has_vforked) | |
289 | { | |
290 | if (linux_supports_tracevforkdone ()) | |
291 | { | |
292 | int status; | |
293 | ||
294 | ptrace (PTRACE_CONT, parent_pid, 0, 0); | |
295 | waitpid (parent_pid, &status, __WALL); | |
296 | if ((status >> 16) != PTRACE_EVENT_VFORKDONE) | |
297 | warning ("Unexpected waitpid result %06x when waiting for " | |
298 | "vfork-done", status); | |
299 | } | |
300 | else | |
301 | { | |
302 | /* We can't insert breakpoints until the child has | |
303 | finished with the shared memory region. We need to | |
304 | wait until that happens. Ideal would be to just | |
305 | call: | |
306 | - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0); | |
307 | - waitpid (parent_pid, &status, __WALL); | |
308 | However, most architectures can't handle a syscall | |
309 | being traced on the way out if it wasn't traced on | |
310 | the way in. | |
311 | ||
312 | We might also think to loop, continuing the child | |
313 | until it exits or gets a SIGTRAP. One problem is | |
314 | that the child might call ptrace with PTRACE_TRACEME. | |
315 | ||
316 | There's no simple and reliable way to figure out when | |
317 | the vforked child will be done with its copy of the | |
318 | shared memory. We could step it out of the syscall, | |
319 | two instructions, let it go, and then single-step the | |
320 | parent once. When we have hardware single-step, this | |
321 | would work; with software single-step it could still | |
322 | be made to work but we'd have to be able to insert | |
323 | single-step breakpoints in the child, and we'd have | |
324 | to insert -just- the single-step breakpoint in the | |
325 | parent. Very awkward. | |
326 | ||
327 | In the end, the best we can do is to make sure it | |
328 | runs for a little while. Hopefully it will be out of | |
329 | range of any breakpoints we reinsert. Usually this | |
330 | is only the single-step breakpoint at vfork's return | |
331 | point. */ | |
332 | ||
333 | usleep (10000); | |
334 | } | |
335 | ||
336 | /* Since we vforked, breakpoints were removed in the parent | |
337 | too. Put them back. */ | |
338 | reattach_breakpoints (parent_pid); | |
339 | } | |
4de4c07c | 340 | } |
3993f6b1 | 341 | else |
4de4c07c DJ |
342 | { |
343 | char child_pid_spelling[40]; | |
344 | ||
345 | /* Needed to keep the breakpoint lists in sync. */ | |
9016a515 DJ |
346 | if (! has_vforked) |
347 | detach_breakpoints (child_pid); | |
4de4c07c DJ |
348 | |
349 | /* Before detaching from the parent, remove all breakpoints from it. */ | |
350 | remove_breakpoints (); | |
351 | ||
352 | fprintf_filtered (gdb_stdout, | |
353 | "Attaching after fork to child process %d.\n", | |
354 | child_pid); | |
355 | ||
9016a515 DJ |
356 | /* If we're vforking, we may want to hold on to the parent until |
357 | the child exits or execs. At exec time we can remove the old | |
358 | breakpoints from the parent and detach it; at exit time we | |
359 | could do the same (or even, sneakily, resume debugging it - the | |
360 | child's exec has failed, or something similar). | |
361 | ||
362 | This doesn't clean up "properly", because we can't call | |
363 | target_detach, but that's OK; if the current target is "child", | |
364 | then it doesn't need any further cleanups, and lin_lwp will | |
365 | generally not encounter vfork (vfork is defined to fork | |
366 | in libpthread.so). | |
367 | ||
368 | The holding part is very easy if we have VFORKDONE events; | |
369 | but keeping track of both processes is beyond GDB at the | |
370 | moment. So we don't expose the parent to the rest of GDB. | |
371 | Instead we quietly hold onto it until such time as we can | |
372 | safely resume it. */ | |
373 | ||
374 | if (has_vforked) | |
375 | linux_parent_pid = parent_pid; | |
376 | else | |
377 | target_detach (NULL, 0); | |
4de4c07c DJ |
378 | |
379 | inferior_ptid = pid_to_ptid (child_pid); | |
380 | push_target (&child_ops); | |
381 | ||
382 | /* Reset breakpoints in the child as appropriate. */ | |
383 | follow_inferior_reset_breakpoints (); | |
384 | } | |
385 | ||
386 | return 0; | |
387 | } | |
388 | ||
389 | ptid_t | |
390 | linux_handle_extended_wait (int pid, int status, | |
391 | struct target_waitstatus *ourstatus) | |
392 | { | |
393 | int event = status >> 16; | |
394 | ||
a2f23071 DJ |
395 | if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK |
396 | || event == PTRACE_EVENT_CLONE) | |
4de4c07c DJ |
397 | { |
398 | unsigned long new_pid; | |
399 | int ret; | |
400 | ||
401 | ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid); | |
402 | ||
403 | /* If we haven't already seen the new PID stop, wait for it now. */ | |
404 | if (! pull_pid_from_list (&stopped_pids, new_pid)) | |
405 | { | |
406 | /* The new child has a pending SIGSTOP. We can't affect it until it | |
a2f23071 | 407 | hits the SIGSTOP, but we're already attached. */ |
4de4c07c | 408 | do { |
a2f23071 DJ |
409 | ret = waitpid (new_pid, &status, |
410 | (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0); | |
4de4c07c DJ |
411 | } while (ret == -1 && errno == EINTR); |
412 | if (ret == -1) | |
413 | perror_with_name ("waiting for new child"); | |
414 | else if (ret != new_pid) | |
415 | internal_error (__FILE__, __LINE__, | |
416 | "wait returned unexpected PID %d", ret); | |
417 | else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP) | |
418 | internal_error (__FILE__, __LINE__, | |
419 | "wait returned unexpected status 0x%x", status); | |
420 | } | |
421 | ||
a2f23071 DJ |
422 | if (event == PTRACE_EVENT_FORK) |
423 | ourstatus->kind = TARGET_WAITKIND_FORKED; | |
424 | else if (event == PTRACE_EVENT_VFORK) | |
425 | ourstatus->kind = TARGET_WAITKIND_VFORKED; | |
426 | else | |
427 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
428 | ||
4de4c07c DJ |
429 | ourstatus->value.related_pid = new_pid; |
430 | return inferior_ptid; | |
431 | } | |
432 | ||
9016a515 DJ |
433 | if (event == PTRACE_EVENT_EXEC) |
434 | { | |
435 | ourstatus->kind = TARGET_WAITKIND_EXECD; | |
436 | ourstatus->value.execd_pathname | |
437 | = xstrdup (child_pid_to_exec_file (pid)); | |
438 | ||
439 | if (linux_parent_pid) | |
440 | { | |
441 | detach_breakpoints (linux_parent_pid); | |
442 | ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0); | |
443 | ||
444 | linux_parent_pid = 0; | |
445 | } | |
446 | ||
447 | return inferior_ptid; | |
448 | } | |
449 | ||
4de4c07c DJ |
450 | internal_error (__FILE__, __LINE__, |
451 | "unknown ptrace event %d", event); | |
452 | } | |
453 | ||
454 | \f | |
455 | int | |
456 | child_insert_fork_catchpoint (int pid) | |
457 | { | |
458 | if (! linux_supports_tracefork ()) | |
3993f6b1 | 459 | error ("Your system does not support fork catchpoints."); |
4de4c07c DJ |
460 | |
461 | return 0; | |
3993f6b1 DJ |
462 | } |
463 | ||
464 | int | |
465 | child_insert_vfork_catchpoint (int pid) | |
466 | { | |
9016a515 | 467 | if (!linux_supports_tracefork ()) |
3993f6b1 | 468 | error ("Your system does not support vfork catchpoints."); |
9016a515 DJ |
469 | |
470 | return 0; | |
3993f6b1 DJ |
471 | } |
472 | ||
473 | int | |
474 | child_insert_exec_catchpoint (int pid) | |
475 | { | |
9016a515 | 476 | if (!linux_supports_tracefork ()) |
3993f6b1 | 477 | error ("Your system does not support exec catchpoints."); |
9016a515 DJ |
478 | |
479 | return 0; | |
3993f6b1 DJ |
480 | } |
481 | ||
4de4c07c DJ |
482 | void |
483 | kill_inferior (void) | |
484 | { | |
485 | int status; | |
486 | int pid = PIDGET (inferior_ptid); | |
487 | struct target_waitstatus last; | |
488 | ptid_t last_ptid; | |
489 | int ret; | |
490 | ||
491 | if (pid == 0) | |
492 | return; | |
493 | ||
494 | /* If we're stopped while forking and we haven't followed yet, kill the | |
495 | other task. We need to do this first because the parent will be | |
496 | sleeping if this is a vfork. */ | |
497 | ||
498 | get_last_target_status (&last_ptid, &last); | |
3993f6b1 | 499 | |
4de4c07c DJ |
500 | if (last.kind == TARGET_WAITKIND_FORKED |
501 | || last.kind == TARGET_WAITKIND_VFORKED) | |
502 | { | |
503 | ptrace (PT_KILL, last.value.related_pid); | |
504 | ptrace_wait (null_ptid, &status); | |
505 | } | |
506 | ||
507 | /* Kill the current process. */ | |
508 | ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0); | |
509 | ret = ptrace_wait (null_ptid, &status); | |
510 | ||
511 | /* We might get a SIGCHLD instead of an exit status. This is | |
512 | aggravated by the first kill above - a child has just died. */ | |
513 | ||
514 | while (ret == pid && WIFSTOPPED (status)) | |
515 | { | |
516 | ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0); | |
517 | ret = ptrace_wait (null_ptid, &status); | |
518 | } | |
519 | ||
520 | target_mourn_inferior (); | |
521 | } |