* Makefile.in (linux-nat.o): Add rule.
[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 /* If the system headers did not provide the constants, hard-code the normal
29 values. */
30 #ifndef PTRACE_EVENT_FORK
31
32 #define PTRACE_SETOPTIONS 0x4200
33 #define PTRACE_GETEVENTMSG 0x4201
34
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
41
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
47
48 #endif /* PTRACE_EVENT_FORK */
49
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
52 here. */
53 #ifndef __WALL
54 #define __WALL 0x40000000 /* Wait for any child. */
55 #endif
56
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. */
59
60 static int linux_supports_tracefork_flag = -1;
61
62 \f
63 /* A helper function for linux_test_for_tracefork, called after fork (). */
64
65 static void
66 linux_tracefork_child (void)
67 {
68 int ret;
69
70 ptrace (PTRACE_TRACEME, 0, 0, 0);
71 kill (getpid (), SIGSTOP);
72 fork ();
73 exit (0);
74 }
75
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. */
81
82 static void
83 linux_test_for_tracefork (void)
84 {
85 int child_pid, ret, status;
86 long second_pid;
87
88 child_pid = fork ();
89 if (child_pid == -1)
90 perror_with_name ("linux_test_for_tracefork: fork");
91
92 if (child_pid == 0)
93 linux_tracefork_child ();
94
95 ret = waitpid (child_pid, &status, 0);
96 if (ret == -1)
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);
102
103 linux_supports_tracefork_flag = 0;
104
105 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
106 if (ret != 0)
107 {
108 ptrace (PTRACE_KILL, child_pid, 0, 0);
109 waitpid (child_pid, &status, 0);
110 return;
111 }
112
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)
117 {
118 second_pid = 0;
119 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
120 if (ret == 0 && second_pid != 0)
121 {
122 int second_status;
123
124 linux_supports_tracefork_flag = 1;
125 waitpid (second_pid, &second_status, 0);
126 ptrace (PTRACE_DETACH, second_pid, 0, 0);
127 }
128 }
129
130 if (WIFSTOPPED (status))
131 {
132 ptrace (PTRACE_DETACH, child_pid, 0, 0);
133 waitpid (child_pid, &status, 0);
134 }
135 }
136
137 /* Return non-zero iff we have tracefork functionality available.
138 This function also sets linux_supports_tracefork_flag. */
139
140 static int
141 linux_supports_tracefork (void)
142 {
143 if (linux_supports_tracefork_flag == -1)
144 linux_test_for_tracefork ();
145 return linux_supports_tracefork_flag;
146 }
147
148 \f
149 int
150 child_insert_fork_catchpoint (int pid)
151 {
152 if (linux_supports_tracefork ())
153 error ("Fork catchpoints have not been implemented yet.");
154 else
155 error ("Your system does not support fork catchpoints.");
156 }
157
158 int
159 child_insert_vfork_catchpoint (int pid)
160 {
161 if (linux_supports_tracefork ())
162 error ("Vfork catchpoints have not been implemented yet.");
163 else
164 error ("Your system does not support vfork catchpoints.");
165 }
166
167 int
168 child_insert_exec_catchpoint (int pid)
169 {
170 if (linux_supports_tracefork ())
171 error ("Exec catchpoints have not been implemented yet.");
172 else
173 error ("Your system does not support exec catchpoints.");
174 }
175
176