* elf64-sparc.c (sparc64_elf_relocate_section): Ignore R_SPARC_DISP32
[deliverable/binutils-gdb.git] / gdb / linux-nat.c
CommitLineData
3993f6b1
DJ
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
ae087d01
DJ
57struct simple_pid_list
58{
59 int pid;
60 struct simple_pid_list *next;
61};
62struct simple_pid_list *stopped_pids;
63
3993f6b1
DJ
64/* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
65 can not be used, 1 if it can. */
66
67static int linux_supports_tracefork_flag = -1;
68
ae087d01
DJ
69\f
70/* Trivial list manipulation functions to keep track of a list of
71 new stopped processes. */
72static void
73add_to_pid_list (struct simple_pid_list **listp, int pid)
74{
75 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
76 new_pid->pid = pid;
77 new_pid->next = *listp;
78 *listp = new_pid;
79}
80
81static int
82pull_pid_from_list (struct simple_pid_list **listp, int pid)
83{
84 struct simple_pid_list **p;
85
86 for (p = listp; *p != NULL; p = &(*p)->next)
87 if ((*p)->pid == pid)
88 {
89 struct simple_pid_list *next = (*p)->next;
90 xfree (*p);
91 *p = next;
92 return 1;
93 }
94 return 0;
95}
96
97void
98linux_record_stopped_pid (int pid)
99{
100 add_to_pid_list (&stopped_pids, pid);
101}
102
3993f6b1
DJ
103\f
104/* A helper function for linux_test_for_tracefork, called after fork (). */
105
106static void
107linux_tracefork_child (void)
108{
109 int ret;
110
111 ptrace (PTRACE_TRACEME, 0, 0, 0);
112 kill (getpid (), SIGSTOP);
113 fork ();
114 exit (0);
115}
116
117/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
118 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
119 fork tracing, and let it fork. If the process exits, we assume that
120 we can't use TRACEFORK; if we get the fork notification, and we can
121 extract the new child's PID, then we assume that we can. */
122
123static void
124linux_test_for_tracefork (void)
125{
126 int child_pid, ret, status;
127 long second_pid;
128
129 child_pid = fork ();
130 if (child_pid == -1)
131 perror_with_name ("linux_test_for_tracefork: fork");
132
133 if (child_pid == 0)
134 linux_tracefork_child ();
135
136 ret = waitpid (child_pid, &status, 0);
137 if (ret == -1)
138 perror_with_name ("linux_test_for_tracefork: waitpid");
139 else if (ret != child_pid)
140 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
141 if (! WIFSTOPPED (status))
142 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
143
144 linux_supports_tracefork_flag = 0;
145
146 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
147 if (ret != 0)
148 {
149 ptrace (PTRACE_KILL, child_pid, 0, 0);
150 waitpid (child_pid, &status, 0);
151 return;
152 }
153
154 ptrace (PTRACE_CONT, child_pid, 0, 0);
155 ret = waitpid (child_pid, &status, 0);
156 if (ret == child_pid && WIFSTOPPED (status)
157 && status >> 16 == PTRACE_EVENT_FORK)
158 {
159 second_pid = 0;
160 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
161 if (ret == 0 && second_pid != 0)
162 {
163 int second_status;
164
165 linux_supports_tracefork_flag = 1;
166 waitpid (second_pid, &second_status, 0);
167 ptrace (PTRACE_DETACH, second_pid, 0, 0);
168 }
169 }
170
171 if (WIFSTOPPED (status))
172 {
173 ptrace (PTRACE_DETACH, child_pid, 0, 0);
174 waitpid (child_pid, &status, 0);
175 }
176}
177
178/* Return non-zero iff we have tracefork functionality available.
179 This function also sets linux_supports_tracefork_flag. */
180
181static int
182linux_supports_tracefork (void)
183{
184 if (linux_supports_tracefork_flag == -1)
185 linux_test_for_tracefork ();
186 return linux_supports_tracefork_flag;
187}
188
189\f
190int
191child_insert_fork_catchpoint (int pid)
192{
193 if (linux_supports_tracefork ())
194 error ("Fork catchpoints have not been implemented yet.");
195 else
196 error ("Your system does not support fork catchpoints.");
197}
198
199int
200child_insert_vfork_catchpoint (int pid)
201{
202 if (linux_supports_tracefork ())
203 error ("Vfork catchpoints have not been implemented yet.");
204 else
205 error ("Your system does not support vfork catchpoints.");
206}
207
208int
209child_insert_exec_catchpoint (int pid)
210{
211 if (linux_supports_tracefork ())
212 error ("Exec catchpoints have not been implemented yet.");
213 else
214 error ("Your system does not support exec catchpoints.");
215}
216
217
This page took 0.032415 seconds and 4 git commands to generate.