Removed the get_file_info request.
[wine] / server / ptrace.c
1 /*
2  * Server-side ptrace support
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <assert.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <signal.h>
27 #include <sys/types.h>
28 #ifdef HAVE_SYS_PTRACE_H
29 # include <sys/ptrace.h>
30 #endif
31 #ifdef HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
33 #endif
34 #include <unistd.h>
35
36 #include "file.h"
37 #include "process.h"
38 #include "thread.h"
39
40 #ifndef PTRACE_CONT
41 #define PTRACE_CONT PT_CONTINUE
42 #endif
43 #ifndef PTRACE_SINGLESTEP
44 #define PTRACE_SINGLESTEP PT_STEP
45 #endif
46 #ifndef PTRACE_ATTACH
47 #define PTRACE_ATTACH PT_ATTACH
48 #endif
49 #ifndef PTRACE_DETACH
50 #define PTRACE_DETACH PT_DETACH
51 #endif
52 #ifndef PTRACE_PEEKDATA
53 #define PTRACE_PEEKDATA PT_READ_D
54 #endif
55 #ifndef PTRACE_POKEDATA
56 #define PTRACE_POKEDATA PT_WRITE_D
57 #endif
58
59 #ifndef HAVE_SYS_PTRACE_H
60 #define PT_CONTINUE 0
61 #define PT_ATTACH   1
62 #define PT_DETACH   2
63 #define PT_READ_D   3
64 #define PT_WRITE_D  4
65 #define PT_STEP     5
66 inline static int ptrace(int req, ...) { errno = EPERM; return -1; /*FAIL*/ }
67 #endif  /* HAVE_SYS_PTRACE_H */
68
69 static const int use_ptrace = 1;  /* set to 0 to disable ptrace */
70
71 /* handle a status returned by wait4 */
72 static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
73 {
74     if (WIFSTOPPED(status))
75     {
76         int sig = WSTOPSIG(status);
77         if (debug_level && thread)
78             fprintf( stderr, "%04x: *signal* signal=%d\n", thread->id, sig );
79         if (sig != want_sig)
80         {
81             /* ignore other signals for now */
82             if (thread && get_thread_single_step( thread ))
83                 ptrace( PTRACE_SINGLESTEP, pid, (caddr_t)1, sig );
84             else
85                 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
86         }
87         return sig;
88     }
89     if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
90     {
91         thread->attached = 0;
92         thread->unix_pid = -1;
93         thread->unix_tid = -1;
94         if (debug_level)
95         {
96             if (WIFSIGNALED(status))
97                 fprintf( stderr, "%04x: *exited* signal=%d\n",
98                          thread->id, WTERMSIG(status) );
99             else
100                 fprintf( stderr, "%04x: *exited* status=%d\n",
101                          thread->id, WEXITSTATUS(status) );
102         }
103     }
104     return 0;
105 }
106
107 /* wait4 wrapper to handle missing __WALL flag in older kernels */
108 static inline pid_t wait4_wrapper( pid_t pid, int *status, int options, struct rusage *usage )
109 {
110 #ifdef __WALL
111     static int wall_flag = __WALL;
112
113     for (;;)
114     {
115         pid_t ret = wait4( pid, status, options | wall_flag, usage );
116         if (ret != -1 || !wall_flag || errno != EINVAL) return ret;
117         wall_flag = 0;
118     }
119 #else
120     return wait4( pid, status, options, usage );
121 #endif
122 }
123
124 /* handle a SIGCHLD signal */
125 void sigchld_callback(void)
126 {
127     int pid, status;
128
129     for (;;)
130     {
131         if (!(pid = wait4_wrapper( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
132         if (pid != -1) handle_child_status( get_thread_from_pid(pid), pid, status, -1 );
133         else break;
134     }
135 }
136
137 /* wait for a ptraced child to get a certain signal */
138 static int wait4_thread( struct thread *thread, int signal )
139 {
140     int res, status;
141
142     for (;;)
143     {
144         if ((res = wait4_wrapper( get_ptrace_pid(thread), &status, WUNTRACED, NULL )) == -1)
145         {
146             if (errno == EINTR) continue;
147             if (errno == ECHILD)  /* must have died */
148             {
149                 thread->unix_pid = -1;
150                 thread->unix_tid = -1;
151                 thread->attached = 0;
152             }
153             else perror( "wait4" );
154             return 0;
155         }
156         res = handle_child_status( thread, res, status, signal );
157         if (!res || res == signal) break;
158     }
159     return (thread->unix_pid != -1);
160 }
161
162 /* return the Unix pid to use in ptrace calls for a given thread */
163 int get_ptrace_pid( struct thread *thread )
164 {
165     if (thread->unix_tid != -1) return thread->unix_tid;
166     return thread->unix_pid;
167 }
168
169 /* send a Unix signal to a specific thread */
170 int send_thread_signal( struct thread *thread, int sig )
171 {
172     int ret = -1;
173
174     if (thread->unix_pid != -1)
175     {
176         if (thread->unix_tid != -1)
177         {
178             ret = tkill( thread->unix_tid, sig );
179             if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
180         }
181         else ret = kill( thread->unix_pid, sig );
182
183         if (ret == -1 && errno == ESRCH) /* thread got killed */
184         {
185             thread->unix_pid = -1;
186             thread->unix_tid = -1;
187             thread->attached = 0;
188         }
189     }
190     return (ret != -1);
191 }
192
193 /* attach to a Unix thread */
194 static int attach_thread( struct thread *thread )
195 {
196     /* this may fail if the client is already being debugged */
197     if (!use_ptrace) return 0;
198     if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
199     {
200         if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1;  /* thread got killed */
201         return 0;
202     }
203     if (debug_level) fprintf( stderr, "%04x: *attached*\n", thread->id );
204     thread->attached = 1;
205     return wait4_thread( thread, SIGSTOP );
206 }
207
208 /* detach from a Unix thread and kill it */
209 void detach_thread( struct thread *thread, int sig )
210 {
211     if (thread->unix_pid == -1) return;
212     if (thread->attached)
213     {
214         /* make sure it is stopped */
215         suspend_for_ptrace( thread );
216         if (sig) send_thread_signal( thread, sig );
217         if (thread->unix_pid == -1) return;
218         if (debug_level) fprintf( stderr, "%04x: *detached*\n", thread->id );
219         ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, sig );
220         thread->attached = 0;
221     }
222     else if (sig) send_thread_signal( thread, sig );
223 }
224
225 /* attach to a Unix process with ptrace */
226 int attach_process( struct process *process )
227 {
228     struct thread *thread;
229     int ret = 1;
230
231     if (!process->thread_list)  /* need at least one running thread */
232     {
233         set_error( STATUS_ACCESS_DENIED );
234         return 0;
235     }
236     for (thread = process->thread_list; thread; thread = thread->proc_next)
237     {
238         if (thread->attached) continue;
239         if (suspend_for_ptrace( thread )) resume_after_ptrace( thread );
240         else ret = 0;
241     }
242     return ret;
243 }
244
245 /* detach from a ptraced Unix process */
246 void detach_process( struct process *process )
247 {
248     struct thread *thread;
249
250     for (thread = process->thread_list; thread; thread = thread->proc_next)
251     {
252         if (thread->attached) detach_thread( thread, 0 );
253     }
254 }
255
256 /* suspend a thread to allow using ptrace on it */
257 /* you must do a resume_after_ptrace when finished with the thread */
258 int suspend_for_ptrace( struct thread *thread )
259 {
260     /* can't stop a thread while initialisation is in progress */
261     if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
262
263     if (thread->attached)
264     {
265         if (!send_thread_signal( thread, SIGSTOP )) goto error;
266         if (!wait4_thread( thread, SIGSTOP )) goto error;
267         return 1;
268     }
269     if (attach_thread( thread )) return 1;
270  error:
271     set_error( STATUS_ACCESS_DENIED );
272     return 0;
273 }
274
275 /* resume a thread after we have used ptrace on it */
276 void resume_after_ptrace( struct thread *thread )
277 {
278     if (thread->unix_pid == -1) return;
279     assert( thread->attached );
280     ptrace( get_thread_single_step(thread) ? PTRACE_SINGLESTEP : PTRACE_CONT,
281             get_ptrace_pid(thread), (caddr_t)1, 0 /* cancel the SIGSTOP */ );
282 }
283
284 /* read an int from a thread address space */
285 int read_thread_int( struct thread *thread, const int *addr, int *data )
286 {
287     errno = 0;
288     *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
289     if ( *data == -1 && errno)
290     {
291         file_set_error();
292         return -1;
293     }
294     return 0;
295 }
296
297 /* write an int to a thread address space */
298 int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
299 {
300     int res;
301     if (mask != ~0)
302     {
303         if (read_thread_int( thread, addr, &res ) == -1) return -1;
304         data = (data & mask) | (res & ~mask);
305     }
306     if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
307         file_set_error();
308     return res;
309 }