kernel32: Added some tests for toolhelp functions.
[wine.git] / server / ptrace.c
blob9f8441fdeb69a5e35a5d0f0a5619a080ee3041e3
1 /*
2 * Server-side ptrace support
4 * Copyright (C) 1999 Alexandre Julliard
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.
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.
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
21 #include "config.h"
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>
37 #include "ntstatus.h"
38 #define WIN32_NO_STATUS
39 #include "winternl.h"
41 #include "file.h"
42 #include "process.h"
43 #include "thread.h"
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
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 */
74 static const int use_ptrace = 1; /* set to 0 to disable ptrace */
76 /* handle a status returned by wait4 */
77 static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
79 if (WIFSTOPPED(status))
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)
86 /* ignore other signals for now */
87 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
89 return sig;
91 if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
93 thread->unix_pid = -1;
94 thread->unix_tid = -1;
95 if (debug_level)
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) );
105 return 0;
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 )
111 #ifdef __WALL
112 static int wall_flag = __WALL;
114 for (;;)
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;
120 #else
121 return wait4( pid, status, options, usage );
122 #endif
125 /* handle a SIGCHLD signal */
126 void sigchld_callback(void)
128 int pid, status;
130 for (;;)
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;
138 /* wait for a ptraced child to get a certain signal */
139 static int wait4_thread( struct thread *thread, int signal )
141 int res, status;
143 start_watchdog();
144 for (;;)
146 if ((res = wait4_wrapper( get_ptrace_pid(thread), &status, WUNTRACED, NULL )) == -1)
148 if (errno == EINTR)
150 if (!watchdog_triggered()) continue;
151 if (debug_level) fprintf( stderr, "%04x: *watchdog* wait4 aborted\n", thread->id );
153 else if (errno == ECHILD) /* must have died */
155 thread->unix_pid = -1;
156 thread->unix_tid = -1;
158 else perror( "wait4" );
159 stop_watchdog();
160 return 0;
162 res = handle_child_status( thread, res, status, signal );
163 if (!res || res == signal) break;
165 stop_watchdog();
166 return (thread->unix_pid != -1);
169 /* return the Unix pid to use in ptrace calls for a given thread */
170 int get_ptrace_pid( struct thread *thread )
172 if (thread->unix_tid != -1) return thread->unix_tid;
173 return thread->unix_pid;
176 /* send a signal to a specific thread */
177 static inline int tkill( int tgid, int pid, int sig )
179 int ret = -ENOSYS;
181 #ifdef __linux__
182 # ifdef __i386__
183 __asm__( "pushl %%ebx\n\t"
184 "movl %2,%%ebx\n\t"
185 "int $0x80\n\t"
186 "popl %%ebx\n\t"
187 : "=a" (ret)
188 : "0" (270) /*SYS_tgkill*/, "r" (tgid), "c" (pid), "d" (sig) );
189 if (ret == -ENOSYS)
190 __asm__( "pushl %%ebx\n\t"
191 "movl %2,%%ebx\n\t"
192 "int $0x80\n\t"
193 "popl %%ebx\n\t"
194 : "=a" (ret)
195 : "0" (238) /*SYS_tkill*/, "r" (pid), "c" (sig) );
196 # elif defined(__x86_64__)
197 __asm__( "syscall" : "=a" (ret)
198 : "0" (200) /*SYS_tkill*/, "D" (pid), "S" (sig) );
199 # endif
200 #endif /* __linux__ */
202 if (ret >= 0) return ret;
203 errno = -ret;
204 return -1;
207 /* send a Unix signal to a specific thread */
208 int send_thread_signal( struct thread *thread, int sig )
210 int ret = -1;
212 if (thread->unix_pid != -1)
214 if (thread->unix_tid != -1)
216 ret = tkill( thread->unix_pid, thread->unix_tid, sig );
217 if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
219 else ret = kill( thread->unix_pid, sig );
221 if (ret == -1 && errno == ESRCH) /* thread got killed */
223 thread->unix_pid = -1;
224 thread->unix_tid = -1;
227 return (ret != -1);
230 /* suspend a thread to allow using ptrace on it */
231 /* you must do a resume_after_ptrace when finished with the thread */
232 int suspend_for_ptrace( struct thread *thread )
234 /* can't stop a thread while initialisation is in progress */
235 if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
237 /* this may fail if the client is already being debugged */
238 if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
240 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
241 goto error;
243 if (wait4_thread( thread, SIGSTOP )) return 1;
244 resume_after_ptrace( thread );
245 error:
246 set_error( STATUS_ACCESS_DENIED );
247 return 0;
250 /* resume a thread after we have used ptrace on it */
251 void resume_after_ptrace( struct thread *thread )
253 if (thread->unix_pid == -1) return;
254 if (ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, 0 ) == -1)
256 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
260 /* read an int from a thread address space */
261 static int read_thread_int( struct thread *thread, const int *addr, int *data )
263 errno = 0;
264 *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
265 if ( *data == -1 && errno)
267 file_set_error();
268 return -1;
270 return 0;
273 /* write an int to a thread address space */
274 static int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
276 int res;
277 if (mask != ~0)
279 if (read_thread_int( thread, addr, &res ) == -1) return -1;
280 data = (data & mask) | (res & ~mask);
282 if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
283 file_set_error();
284 return res;
287 /* read data from a process memory space */
288 int read_process_memory( struct process *process, const void *ptr, size_t size, char *dest )
290 struct thread *thread = get_process_first_thread( process );
291 unsigned int first_offset, last_offset, len;
292 int data, *addr;
294 if (!thread) /* process is dead */
296 set_error( STATUS_ACCESS_DENIED );
297 return 0;
300 first_offset = (unsigned long)ptr % sizeof(int);
301 last_offset = (size + first_offset) % sizeof(int);
302 if (!last_offset) last_offset = sizeof(int);
304 addr = (int *)((char *)ptr - first_offset);
305 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
307 if (suspend_for_ptrace( thread ))
309 if (len > 1)
311 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
312 memcpy( dest, (char *)&data + first_offset, sizeof(int) - first_offset );
313 dest += sizeof(int) - first_offset;
314 first_offset = 0;
315 len--;
318 while (len > 1)
320 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
321 memcpy( dest, &data, sizeof(int) );
322 dest += sizeof(int);
323 len--;
326 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
327 memcpy( dest, (char *)&data + first_offset, last_offset - first_offset );
328 len--;
330 done:
331 resume_after_ptrace( thread );
333 return !len;
336 /* make sure we can write to the whole address range */
337 /* len is the total size (in ints) */
338 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
340 int page = get_page_size() / sizeof(int);
342 for (;;)
344 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
345 if (len <= page) break;
346 addr += page;
347 len -= page;
349 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
352 /* write data to a process memory space */
353 int write_process_memory( struct process *process, void *ptr, size_t size, const char *src )
355 struct thread *thread = get_process_first_thread( process );
356 int ret = 0, data = 0;
357 size_t len;
358 int *addr;
359 unsigned int first_mask, first_offset, last_mask, last_offset;
361 if (!thread) /* process is dead */
363 set_error( STATUS_ACCESS_DENIED );
364 return 0;
367 /* compute the mask for the first int */
368 first_mask = ~0;
369 first_offset = (unsigned long)ptr % sizeof(int);
370 memset( &first_mask, 0, first_offset );
372 /* compute the mask for the last int */
373 last_offset = (size + first_offset) % sizeof(int);
374 if (!last_offset) last_offset = sizeof(int);
375 last_mask = 0;
376 memset( &last_mask, 0xff, last_offset );
378 addr = (int *)((char *)ptr - first_offset);
379 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
381 if (suspend_for_ptrace( thread ))
383 if (!check_process_write_access( thread, addr, len ))
385 set_error( STATUS_ACCESS_DENIED );
386 goto done;
388 /* first word is special */
389 if (len > 1)
391 memcpy( (char *)&data + first_offset, src, sizeof(int) - first_offset );
392 src += sizeof(int) - first_offset;
393 if (write_thread_int( thread, addr++, data, first_mask ) == -1) goto done;
394 first_offset = 0;
395 len--;
397 else last_mask &= first_mask;
399 while (len > 1)
401 memcpy( &data, src, sizeof(int) );
402 src += sizeof(int);
403 if (write_thread_int( thread, addr++, data, ~0 ) == -1) goto done;
404 len--;
407 /* last word is special too */
408 memcpy( (char *)&data + first_offset, src, last_offset - first_offset );
409 if (write_thread_int( thread, addr, data, last_mask ) == -1) goto done;
410 ret = 1;
412 done:
413 resume_after_ptrace( thread );
415 return ret;
418 /* retrieve an LDT selector entry */
419 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
420 unsigned int *limit, unsigned char *flags )
422 if (!thread->process->ldt_copy)
424 set_error( STATUS_ACCESS_DENIED );
425 return;
427 if (entry >= 8192)
429 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
430 return;
432 if (suspend_for_ptrace( thread ))
434 unsigned char flags_buf[4];
435 int *addr = (int *)thread->process->ldt_copy + entry;
436 if (read_thread_int( thread, addr, (int *)base ) == -1) goto done;
437 if (read_thread_int( thread, addr + 8192, (int *)limit ) == -1) goto done;
438 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
439 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
440 *flags = flags_buf[entry & 3];
441 done:
442 resume_after_ptrace( thread );