server: Removed the thread attached flag, since we always detach now.
[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 <stdarg.h>
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_PTRACE_H
30 # include <sys/ptrace.h>
31 #endif
32 #ifdef HAVE_SYS_WAIT_H
33 # include <sys/wait.h>
34 #endif
35 #include <unistd.h>
36
37 #include "ntstatus.h"
38 #define WIN32_NO_STATUS
39 #include "winternl.h"
40
41 #include "file.h"
42 #include "process.h"
43 #include "thread.h"
44
45 #ifndef PTRACE_CONT
46 #define PTRACE_CONT PT_CONTINUE
47 #endif
48 #ifndef PTRACE_SINGLESTEP
49 #define PTRACE_SINGLESTEP PT_STEP
50 #endif
51 #ifndef PTRACE_ATTACH
52 #define PTRACE_ATTACH PT_ATTACH
53 #endif
54 #ifndef PTRACE_DETACH
55 #define PTRACE_DETACH PT_DETACH
56 #endif
57 #ifndef PTRACE_PEEKDATA
58 #define PTRACE_PEEKDATA PT_READ_D
59 #endif
60 #ifndef PTRACE_POKEDATA
61 #define PTRACE_POKEDATA PT_WRITE_D
62 #endif
63
64 #ifndef HAVE_SYS_PTRACE_H
65 #define PT_CONTINUE 0
66 #define PT_ATTACH   1
67 #define PT_DETACH   2
68 #define PT_READ_D   3
69 #define PT_WRITE_D  4
70 #define PT_STEP     5
71 inline static int ptrace(int req, ...) { errno = EPERM; return -1; /*FAIL*/ }
72 #endif  /* HAVE_SYS_PTRACE_H */
73
74 static const int use_ptrace = 1;  /* set to 0 to disable ptrace */
75
76 /* handle a status returned by wait4 */
77 static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
78 {
79     if (WIFSTOPPED(status))
80     {
81         int sig = WSTOPSIG(status);
82         if (debug_level && thread)
83             fprintf( stderr, "%04x: *signal* signal=%d\n", thread->id, sig );
84         if (sig != want_sig)
85         {
86             /* ignore other signals for now */
87             ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
88         }
89         return sig;
90     }
91     if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
92     {
93         thread->unix_pid = -1;
94         thread->unix_tid = -1;
95         if (debug_level)
96         {
97             if (WIFSIGNALED(status))
98                 fprintf( stderr, "%04x: *exited* signal=%d\n",
99                          thread->id, WTERMSIG(status) );
100             else
101                 fprintf( stderr, "%04x: *exited* status=%d\n",
102                          thread->id, WEXITSTATUS(status) );
103         }
104     }
105     return 0;
106 }
107
108 /* wait4 wrapper to handle missing __WALL flag in older kernels */
109 static inline pid_t wait4_wrapper( pid_t pid, int *status, int options, struct rusage *usage )
110 {
111 #ifdef __WALL
112     static int wall_flag = __WALL;
113
114     for (;;)
115     {
116         pid_t ret = wait4( pid, status, options | wall_flag, usage );
117         if (ret != -1 || !wall_flag || errno != EINVAL) return ret;
118         wall_flag = 0;
119     }
120 #else
121     return wait4( pid, status, options, usage );
122 #endif
123 }
124
125 /* handle a SIGCHLD signal */
126 void sigchld_callback(void)
127 {
128     int pid, status;
129
130     for (;;)
131     {
132         if (!(pid = wait4_wrapper( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
133         if (pid != -1) handle_child_status( get_thread_from_pid(pid), pid, status, -1 );
134         else break;
135     }
136 }
137
138 /* wait for a ptraced child to get a certain signal */
139 static int wait4_thread( struct thread *thread, int signal )
140 {
141     int res, status;
142
143     start_watchdog();
144     for (;;)
145     {
146         if ((res = wait4_wrapper( get_ptrace_pid(thread), &status, WUNTRACED, NULL )) == -1)
147         {
148             if (errno == EINTR)
149             {
150                 if (!watchdog_triggered()) continue;
151                 if (debug_level) fprintf( stderr, "%04x: *watchdog* wait4 aborted\n", thread->id );
152             }
153             else if (errno == ECHILD)  /* must have died */
154             {
155                 thread->unix_pid = -1;
156                 thread->unix_tid = -1;
157             }
158             else perror( "wait4" );
159             stop_watchdog();
160             return 0;
161         }
162         res = handle_child_status( thread, res, status, signal );
163         if (!res || res == signal) break;
164     }
165     stop_watchdog();
166     return (thread->unix_pid != -1);
167 }
168
169 /* return the Unix pid to use in ptrace calls for a given thread */
170 int get_ptrace_pid( struct thread *thread )
171 {
172     if (thread->unix_tid != -1) return thread->unix_tid;
173     return thread->unix_pid;
174 }
175
176 /* send a Unix signal to a specific thread */
177 int send_thread_signal( struct thread *thread, int sig )
178 {
179     int ret = -1;
180
181     if (thread->unix_pid != -1)
182     {
183         if (thread->unix_tid != -1)
184         {
185             ret = tkill( thread->unix_pid, thread->unix_tid, sig );
186             if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
187         }
188         else ret = kill( thread->unix_pid, sig );
189
190         if (ret == -1 && errno == ESRCH) /* thread got killed */
191         {
192             thread->unix_pid = -1;
193             thread->unix_tid = -1;
194         }
195     }
196     return (ret != -1);
197 }
198
199 /* suspend a thread to allow using ptrace on it */
200 /* you must do a resume_after_ptrace when finished with the thread */
201 int suspend_for_ptrace( struct thread *thread )
202 {
203     /* can't stop a thread while initialisation is in progress */
204     if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
205
206     /* this may fail if the client is already being debugged */
207     if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
208     {
209         if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1;  /* thread got killed */
210         goto error;
211     }
212     if (wait4_thread( thread, SIGSTOP )) return 1;
213     resume_after_ptrace( thread );
214  error:
215     set_error( STATUS_ACCESS_DENIED );
216     return 0;
217 }
218
219 /* resume a thread after we have used ptrace on it */
220 void resume_after_ptrace( struct thread *thread )
221 {
222     if (thread->unix_pid == -1) return;
223     if (ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, 0 ) == -1)
224     {
225         if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1;  /* thread got killed */
226     }
227 }
228
229 /* read an int from a thread address space */
230 static int read_thread_int( struct thread *thread, const int *addr, int *data )
231 {
232     errno = 0;
233     *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
234     if ( *data == -1 && errno)
235     {
236         file_set_error();
237         return -1;
238     }
239     return 0;
240 }
241
242 /* write an int to a thread address space */
243 static int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
244 {
245     int res;
246     if (mask != ~0)
247     {
248         if (read_thread_int( thread, addr, &res ) == -1) return -1;
249         data = (data & mask) | (res & ~mask);
250     }
251     if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
252         file_set_error();
253     return res;
254 }
255
256 /* read data from a process memory space */
257 int read_process_memory( struct process *process, const void *ptr, size_t size, char *dest )
258 {
259     struct thread *thread = get_process_first_thread( process );
260     unsigned int first_offset, last_offset, len;
261     int data, *addr;
262
263     if (!thread)  /* process is dead */
264     {
265         set_error( STATUS_ACCESS_DENIED );
266         return 0;
267     }
268
269     first_offset = (unsigned long)ptr % sizeof(int);
270     last_offset = (size + first_offset) % sizeof(int);
271     if (!last_offset) last_offset = sizeof(int);
272
273     addr = (int *)((char *)ptr - first_offset);
274     len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
275
276     if (suspend_for_ptrace( thread ))
277     {
278         if (len > 1)
279         {
280             if (read_thread_int( thread, addr++, &data ) == -1) goto done;
281             memcpy( dest, (char *)&data + first_offset, sizeof(int) - first_offset );
282             dest += sizeof(int) - first_offset;
283             first_offset = 0;
284             len--;
285         }
286
287         while (len > 1)
288         {
289             if (read_thread_int( thread, addr++, &data ) == -1) goto done;
290             memcpy( dest, &data, sizeof(int) );
291             dest += sizeof(int);
292             len--;
293         }
294
295         if (read_thread_int( thread, addr++, &data ) == -1) goto done;
296         memcpy( dest, (char *)&data + first_offset, last_offset - first_offset );
297         len--;
298
299     done:
300         resume_after_ptrace( thread );
301     }
302     return !len;
303 }
304
305 /* make sure we can write to the whole address range */
306 /* len is the total size (in ints) */
307 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
308 {
309     int page = get_page_size() / sizeof(int);
310
311     for (;;)
312     {
313         if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
314         if (len <= page) break;
315         addr += page;
316         len -= page;
317     }
318     return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
319 }
320
321 /* write data to a process memory space */
322 int write_process_memory( struct process *process, void *ptr, size_t size, const char *src )
323 {
324     struct thread *thread = get_process_first_thread( process );
325     int ret = 0, data = 0;
326     size_t len;
327     int *addr;
328     unsigned int first_mask, first_offset, last_mask, last_offset;
329
330     if (!thread)  /* process is dead */
331     {
332         set_error( STATUS_ACCESS_DENIED );
333         return 0;
334     }
335
336     /* compute the mask for the first int */
337     first_mask = ~0;
338     first_offset = (unsigned long)ptr % sizeof(int);
339     memset( &first_mask, 0, first_offset );
340
341     /* compute the mask for the last int */
342     last_offset = (size + first_offset) % sizeof(int);
343     if (!last_offset) last_offset = sizeof(int);
344     last_mask = 0;
345     memset( &last_mask, 0xff, last_offset );
346
347     addr = (int *)((char *)ptr - first_offset);
348     len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
349
350     if (suspend_for_ptrace( thread ))
351     {
352         if (!check_process_write_access( thread, addr, len ))
353         {
354             set_error( STATUS_ACCESS_DENIED );
355             goto done;
356         }
357         /* first word is special */
358         if (len > 1)
359         {
360             memcpy( (char *)&data + first_offset, src, sizeof(int) - first_offset );
361             src += sizeof(int) - first_offset;
362             if (write_thread_int( thread, addr++, data, first_mask ) == -1) goto done;
363             first_offset = 0;
364             len--;
365         }
366         else last_mask &= first_mask;
367
368         while (len > 1)
369         {
370             memcpy( &data, src, sizeof(int) );
371             src += sizeof(int);
372             if (write_thread_int( thread, addr++, data, ~0 ) == -1) goto done;
373             len--;
374         }
375
376         /* last word is special too */
377         memcpy( (char *)&data + first_offset, src, last_offset - first_offset );
378         if (write_thread_int( thread, addr, data, last_mask ) == -1) goto done;
379         ret = 1;
380
381     done:
382         resume_after_ptrace( thread );
383     }
384     return ret;
385 }
386
387 /* retrieve an LDT selector entry */
388 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
389                          unsigned int *limit, unsigned char *flags )
390 {
391     if (!thread->process->ldt_copy)
392     {
393         set_error( STATUS_ACCESS_DENIED );
394         return;
395     }
396     if (entry >= 8192)
397     {
398         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
399         return;
400     }
401     if (suspend_for_ptrace( thread ))
402     {
403         unsigned char flags_buf[4];
404         int *addr = (int *)thread->process->ldt_copy + entry;
405         if (read_thread_int( thread, addr, (int *)base ) == -1) goto done;
406         if (read_thread_int( thread, addr + 8192, (int *)limit ) == -1) goto done;
407         addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
408         if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
409         *flags = flags_buf[entry & 3];
410     done:
411         resume_after_ptrace( thread );
412     }
413 }