This commit was manufactured by cvs2svn to create tag
[external/binutils.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 struct simple_pid_list
60 {
61   int pid;
62   struct simple_pid_list *next;
63 };
64 struct simple_pid_list *stopped_pids;
65
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.  */
68
69 static int linux_supports_tracefork_flag = -1;
70
71 \f
72 /* Trivial list manipulation functions to keep track of a list of
73    new stopped processes.  */
74 static void
75 add_to_pid_list (struct simple_pid_list **listp, int pid)
76 {
77   struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
78   new_pid->pid = pid;
79   new_pid->next = *listp;
80   *listp = new_pid;
81 }
82
83 static int
84 pull_pid_from_list (struct simple_pid_list **listp, int pid)
85 {
86   struct simple_pid_list **p;
87
88   for (p = listp; *p != NULL; p = &(*p)->next)
89     if ((*p)->pid == pid)
90       {
91         struct simple_pid_list *next = (*p)->next;
92         xfree (*p);
93         *p = next;
94         return 1;
95       }
96   return 0;
97 }
98
99 void
100 linux_record_stopped_pid (int pid)
101 {
102   add_to_pid_list (&stopped_pids, pid);
103 }
104
105 \f
106 /* A helper function for linux_test_for_tracefork, called after fork ().  */
107
108 static void
109 linux_tracefork_child (void)
110 {
111   int ret;
112
113   ptrace (PTRACE_TRACEME, 0, 0, 0);
114   kill (getpid (), SIGSTOP);
115   fork ();
116   exit (0);
117 }
118
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.  */
124
125 static void
126 linux_test_for_tracefork (void)
127 {
128   int child_pid, ret, status;
129   long second_pid;
130
131   child_pid = fork ();
132   if (child_pid == -1)
133     perror_with_name ("linux_test_for_tracefork: fork");
134
135   if (child_pid == 0)
136     linux_tracefork_child ();
137
138   ret = waitpid (child_pid, &status, 0);
139   if (ret == -1)
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);
145
146   linux_supports_tracefork_flag = 0;
147
148   ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
149   if (ret != 0)
150     {
151       ptrace (PTRACE_KILL, child_pid, 0, 0);
152       waitpid (child_pid, &status, 0);
153       return;
154     }
155
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)
160     {
161       second_pid = 0;
162       ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
163       if (ret == 0 && second_pid != 0)
164         {
165           int second_status;
166
167           linux_supports_tracefork_flag = 1;
168           waitpid (second_pid, &second_status, 0);
169           ptrace (PTRACE_DETACH, second_pid, 0, 0);
170         }
171     }
172
173   if (WIFSTOPPED (status))
174     {
175       ptrace (PTRACE_DETACH, child_pid, 0, 0);
176       waitpid (child_pid, &status, 0);
177     }
178 }
179
180 /* Return non-zero iff we have tracefork functionality available.
181    This function also sets linux_supports_tracefork_flag.  */
182
183 static int
184 linux_supports_tracefork (void)
185 {
186   if (linux_supports_tracefork_flag == -1)
187     linux_test_for_tracefork ();
188   return linux_supports_tracefork_flag;
189 }
190
191 \f
192 int
193 child_insert_fork_catchpoint (int pid)
194 {
195   if (linux_supports_tracefork ())
196     error ("Fork catchpoints have not been implemented yet.");
197   else
198     error ("Your system does not support fork catchpoints.");
199 }
200
201 int
202 child_insert_vfork_catchpoint (int pid)
203 {
204   if (linux_supports_tracefork ())
205     error ("Vfork catchpoints have not been implemented yet.");
206   else
207     error ("Your system does not support vfork catchpoints.");
208 }
209
210 int
211 child_insert_exec_catchpoint (int pid)
212 {
213   if (linux_supports_tracefork ())
214     error ("Exec catchpoints have not been implemented yet.");
215   else
216     error ("Your system does not support exec catchpoints.");
217 }
218
219