Added generic signal handling mechanism based on pipes to synchronize
[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 #ifndef __WALL
70 #define __WALL 0
71 #endif
72
73 static const int use_ptrace = 1;  /* set to 0 to disable ptrace */
74
75 /* handle a status returned by wait4 */
76 static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
77 {
78     if (WIFSTOPPED(status))
79     {
80         int sig = WSTOPSIG(status);
81         if (debug_level && thread)
82             fprintf( stderr, "%04x: *signal* signal=%d\n", thread->id, sig );
83         if (sig != want_sig)
84         {
85             /* ignore other signals for now */
86             if (thread && get_thread_single_step( thread ))
87                 ptrace( PTRACE_SINGLESTEP, pid, (caddr_t)1, sig );
88             else
89                 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
90         }
91         return sig;
92     }
93     if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
94     {
95         thread->attached = 0;
96         thread->unix_pid = -1;
97         thread->unix_tid = -1;
98         if (debug_level)
99         {
100             if (WIFSIGNALED(status))
101                 fprintf( stderr, "%04x: *exited* signal=%d\n",
102                          thread->id, WTERMSIG(status) );
103             else
104                 fprintf( stderr, "%04x: *exited* status=%d\n",
105                          thread->id, WEXITSTATUS(status) );
106         }
107     }
108     return 0;
109 }
110
111 /* handle a SIGCHLD signal */
112 void sigchld_callback(void)
113 {
114     int pid, status;
115
116     for (;;)
117     {
118         if (!(pid = wait4( -1, &status, WUNTRACED | WNOHANG | __WALL, NULL ))) break;
119         if (pid != -1) handle_child_status( get_thread_from_pid(pid), pid, status, -1 );
120         else break;
121     }
122 }
123
124 /* wait for a ptraced child to get a certain signal */
125 static void wait4_thread( struct thread *thread, int signal )
126 {
127     int res, status;
128
129     do
130     {
131         if ((res = wait4( get_ptrace_pid(thread), &status, WUNTRACED | __WALL, NULL )) == -1)
132         {
133             if (errno == ECHILD)  /* must have died */
134             {
135                 thread->unix_pid = -1;
136                 thread->unix_tid = -1;
137                 thread->attached = 0;
138             }
139             else perror( "wait4" );
140             return;
141         }
142         res = handle_child_status( thread, res, status, signal );
143     } while (res && res != signal);
144 }
145
146 /* return the Unix pid to use in ptrace calls for a given thread */
147 int get_ptrace_pid( struct thread *thread )
148 {
149     if (thread->unix_tid != -1) return thread->unix_tid;
150     return thread->unix_pid;
151 }
152
153 /* send a Unix signal to a specific thread */
154 int send_thread_signal( struct thread *thread, int sig )
155 {
156     int ret = -1;
157
158     if (thread->unix_pid != -1)
159     {
160         if (thread->unix_tid != -1) ret = tkill( thread->unix_tid, sig );
161         else ret = kill( thread->unix_pid, sig );
162
163         if (ret == -1 && errno == ESRCH) /* thread got killed */
164         {
165             thread->unix_pid = -1;
166             thread->unix_tid = -1;
167             thread->attached = 0;
168         }
169     }
170     return (ret != -1);
171 }
172
173 /* attach to a Unix thread */
174 static int attach_thread( struct thread *thread )
175 {
176     /* this may fail if the client is already being debugged */
177     if (!use_ptrace) return 0;
178     if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
179     {
180         if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1;  /* thread got killed */
181         return 0;
182     }
183     if (debug_level) fprintf( stderr, "%04x: *attached*\n", thread->id );
184     thread->attached = 1;
185     wait4_thread( thread, SIGSTOP );
186     return 1;
187 }
188
189 /* detach from a Unix thread and kill it */
190 void detach_thread( struct thread *thread, int sig )
191 {
192     if (thread->unix_pid == -1) return;
193     if (thread->attached)
194     {
195         /* make sure it is stopped */
196         suspend_for_ptrace( thread );
197         if (sig) send_thread_signal( thread, sig );
198         if (thread->unix_pid == -1) return;
199         if (debug_level) fprintf( stderr, "%04x: *detached*\n", thread->id );
200         ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, sig );
201         thread->attached = 0;
202     }
203     else if (sig) send_thread_signal( thread, sig );
204 }
205
206 /* attach to a Unix process with ptrace */
207 int attach_process( struct process *process )
208 {
209     struct thread *thread;
210     int ret = 1;
211
212     if (!process->thread_list)  /* need at least one running thread */
213     {
214         set_error( STATUS_ACCESS_DENIED );
215         return 0;
216     }
217     for (thread = process->thread_list; thread; thread = thread->proc_next)
218     {
219         if (thread->attached) continue;
220         if (suspend_for_ptrace( thread )) resume_after_ptrace( thread );
221         else ret = 0;
222     }
223     return ret;
224 }
225
226 /* detach from a ptraced Unix process */
227 void detach_process( struct process *process )
228 {
229     struct thread *thread;
230
231     for (thread = process->thread_list; thread; thread = thread->proc_next)
232     {
233         if (thread->attached) detach_thread( thread, 0 );
234     }
235 }
236
237 /* suspend a thread to allow using ptrace on it */
238 /* you must do a resume_after_ptrace when finished with the thread */
239 int suspend_for_ptrace( struct thread *thread )
240 {
241     /* can't stop a thread while initialisation is in progress */
242     if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
243
244     if (thread->attached)
245     {
246         send_thread_signal( thread, SIGSTOP );
247         wait4_thread( thread, SIGSTOP );
248         return 1;
249     }
250     if (attach_thread( thread )) return 1;
251  error:
252     set_error( STATUS_ACCESS_DENIED );
253     return 0;
254 }
255
256 /* resume a thread after we have used ptrace on it */
257 void resume_after_ptrace( struct thread *thread )
258 {
259     if (thread->unix_pid == -1) return;
260     assert( thread->attached );
261     ptrace( get_thread_single_step(thread) ? PTRACE_SINGLESTEP : PTRACE_CONT,
262             get_ptrace_pid(thread), (caddr_t)1, 0 /* cancel the SIGSTOP */ );
263 }
264
265 /* read an int from a thread address space */
266 int read_thread_int( struct thread *thread, const int *addr, int *data )
267 {
268     errno = 0;
269     *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
270     if ( *data == -1 && errno)
271     {
272         file_set_error();
273         return -1;
274     }
275     return 0;
276 }
277
278 /* write an int to a thread address space */
279 int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
280 {
281     int res;
282     if (mask != ~0)
283     {
284         if (read_thread_int( thread, addr, &res ) == -1) return -1;
285         data = (data & mask) | (res & ~mask);
286     }
287     if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
288         file_set_error();
289     return res;
290 }