winspool: Avoid reading from NULL (GetPrinterDriver).
[wine/hacks.git] / server / ptrace.c
blob4280b1c6b9e0321a4b8c783641efd0722553a6a7
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->attached = 0;
94 thread->unix_pid = -1;
95 thread->unix_tid = -1;
96 if (debug_level)
98 if (WIFSIGNALED(status))
99 fprintf( stderr, "%04x: *exited* signal=%d\n",
100 thread->id, WTERMSIG(status) );
101 else
102 fprintf( stderr, "%04x: *exited* status=%d\n",
103 thread->id, WEXITSTATUS(status) );
106 return 0;
109 /* wait4 wrapper to handle missing __WALL flag in older kernels */
110 static inline pid_t wait4_wrapper( pid_t pid, int *status, int options, struct rusage *usage )
112 #ifdef __WALL
113 static int wall_flag = __WALL;
115 for (;;)
117 pid_t ret = wait4( pid, status, options | wall_flag, usage );
118 if (ret != -1 || !wall_flag || errno != EINVAL) return ret;
119 wall_flag = 0;
121 #else
122 return wait4( pid, status, options, usage );
123 #endif
126 /* handle a SIGCHLD signal */
127 void sigchld_callback(void)
129 int pid, status;
131 for (;;)
133 if (!(pid = wait4_wrapper( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
134 if (pid != -1) handle_child_status( get_thread_from_pid(pid), pid, status, -1 );
135 else break;
139 /* wait for a ptraced child to get a certain signal */
140 static int wait4_thread( struct thread *thread, int signal )
142 int res, status;
144 start_watchdog();
145 for (;;)
147 if ((res = wait4_wrapper( get_ptrace_pid(thread), &status, WUNTRACED, NULL )) == -1)
149 if (errno == EINTR)
151 if (!watchdog_triggered()) continue;
152 if (debug_level) fprintf( stderr, "%04x: *watchdog* wait4 aborted\n", thread->id );
154 else if (errno == ECHILD) /* must have died */
156 thread->unix_pid = -1;
157 thread->unix_tid = -1;
158 thread->attached = 0;
160 else perror( "wait4" );
161 stop_watchdog();
162 return 0;
164 res = handle_child_status( thread, res, status, signal );
165 if (!res || res == signal) break;
167 stop_watchdog();
168 return (thread->unix_pid != -1);
171 /* return the Unix pid to use in ptrace calls for a given thread */
172 int get_ptrace_pid( struct thread *thread )
174 if (thread->unix_tid != -1) return thread->unix_tid;
175 return thread->unix_pid;
178 /* send a Unix signal to a specific thread */
179 int send_thread_signal( struct thread *thread, int sig )
181 int ret = -1;
183 if (thread->unix_pid != -1)
185 if (thread->unix_tid != -1)
187 ret = tkill( thread->unix_pid, thread->unix_tid, sig );
188 if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
190 else ret = kill( thread->unix_pid, sig );
192 if (ret == -1 && errno == ESRCH) /* thread got killed */
194 thread->unix_pid = -1;
195 thread->unix_tid = -1;
196 thread->attached = 0;
199 return (ret != -1);
202 /* attach to a Unix process with ptrace */
203 int attach_process( struct process *process )
205 struct thread *thread;
206 int ret = 1;
208 if (list_empty( &process->thread_list )) /* need at least one running thread */
210 set_error( STATUS_ACCESS_DENIED );
211 return 0;
214 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
216 if (thread->attached) continue;
217 if (suspend_for_ptrace( thread )) resume_after_ptrace( thread );
218 else ret = 0;
220 return ret;
223 /* detach from a ptraced Unix process */
224 void detach_process( struct process *process )
226 struct thread *thread;
228 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
230 if (thread->attached)
232 /* make sure it is stopped */
233 suspend_for_ptrace( thread );
234 if (thread->unix_pid == -1) return;
235 if (debug_level) fprintf( stderr, "%04x: *detached*\n", thread->id );
236 ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, 0 );
237 thread->attached = 0;
242 /* suspend a thread to allow using ptrace on it */
243 /* you must do a resume_after_ptrace when finished with the thread */
244 int suspend_for_ptrace( struct thread *thread )
246 /* can't stop a thread while initialisation is in progress */
247 if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
249 if (thread->attached)
251 if (!send_thread_signal( thread, SIGSTOP )) goto error;
253 else
255 /* this may fail if the client is already being debugged */
256 if (!use_ptrace) goto error;
257 if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
259 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
260 goto error;
262 if (debug_level) fprintf( stderr, "%04x: *attached*\n", thread->id );
263 thread->attached = 1;
265 if (wait4_thread( thread, SIGSTOP )) return 1;
266 resume_after_ptrace( thread );
267 error:
268 set_error( STATUS_ACCESS_DENIED );
269 return 0;
272 /* resume a thread after we have used ptrace on it */
273 void resume_after_ptrace( struct thread *thread )
275 if (thread->unix_pid == -1) return;
276 assert( thread->attached );
277 ptrace( get_thread_single_step(thread) ? PTRACE_SINGLESTEP : PTRACE_CONT,
278 get_ptrace_pid(thread), (caddr_t)1, 0 /* cancel the SIGSTOP */ );
281 /* read an int from a thread address space */
282 static int read_thread_int( struct thread *thread, const int *addr, int *data )
284 errno = 0;
285 *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
286 if ( *data == -1 && errno)
288 file_set_error();
289 return -1;
291 return 0;
294 /* write an int to a thread address space */
295 static int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
297 int res;
298 if (mask != ~0)
300 if (read_thread_int( thread, addr, &res ) == -1) return -1;
301 data = (data & mask) | (res & ~mask);
303 if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
304 file_set_error();
305 return res;
308 /* read data from a process memory space */
309 int read_process_memory( struct process *process, const void *ptr, size_t size, char *dest )
311 struct thread *thread = get_process_first_thread( process );
312 unsigned int first_offset, last_offset, len;
313 int data, *addr;
315 if (!thread) /* process is dead */
317 set_error( STATUS_ACCESS_DENIED );
318 return 0;
321 first_offset = (unsigned long)ptr % sizeof(int);
322 last_offset = (size + first_offset) % sizeof(int);
323 if (!last_offset) last_offset = sizeof(int);
325 addr = (int *)((char *)ptr - first_offset);
326 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
328 if (suspend_for_ptrace( thread ))
330 if (len > 1)
332 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
333 memcpy( dest, (char *)&data + first_offset, sizeof(int) - first_offset );
334 dest += sizeof(int) - first_offset;
335 first_offset = 0;
336 len--;
339 while (len > 1)
341 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
342 memcpy( dest, &data, sizeof(int) );
343 dest += sizeof(int);
344 len--;
347 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
348 memcpy( dest, (char *)&data + first_offset, last_offset - first_offset );
349 len--;
351 done:
352 resume_after_ptrace( thread );
354 return !len;
357 /* make sure we can write to the whole address range */
358 /* len is the total size (in ints) */
359 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
361 int page = get_page_size() / sizeof(int);
363 for (;;)
365 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
366 if (len <= page) break;
367 addr += page;
368 len -= page;
370 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
373 /* write data to a process memory space */
374 int write_process_memory( struct process *process, void *ptr, size_t size, const char *src )
376 struct thread *thread = get_process_first_thread( process );
377 int ret = 0, data = 0;
378 size_t len;
379 int *addr;
380 unsigned int first_mask, first_offset, last_mask, last_offset;
382 if (!thread) /* process is dead */
384 set_error( STATUS_ACCESS_DENIED );
385 return 0;
388 /* compute the mask for the first int */
389 first_mask = ~0;
390 first_offset = (unsigned long)ptr % sizeof(int);
391 memset( &first_mask, 0, first_offset );
393 /* compute the mask for the last int */
394 last_offset = (size + first_offset) % sizeof(int);
395 if (!last_offset) last_offset = sizeof(int);
396 last_mask = 0;
397 memset( &last_mask, 0xff, last_offset );
399 addr = (int *)((char *)ptr - first_offset);
400 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
402 if (suspend_for_ptrace( thread ))
404 if (!check_process_write_access( thread, addr, len ))
406 set_error( STATUS_ACCESS_DENIED );
407 goto done;
409 /* first word is special */
410 if (len > 1)
412 memcpy( (char *)&data + first_offset, src, sizeof(int) - first_offset );
413 src += sizeof(int) - first_offset;
414 if (write_thread_int( thread, addr++, data, first_mask ) == -1) goto done;
415 first_offset = 0;
416 len--;
418 else last_mask &= first_mask;
420 while (len > 1)
422 memcpy( &data, src, sizeof(int) );
423 src += sizeof(int);
424 if (write_thread_int( thread, addr++, data, ~0 ) == -1) goto done;
425 len--;
428 /* last word is special too */
429 memcpy( (char *)&data + first_offset, src, last_offset - first_offset );
430 if (write_thread_int( thread, addr, data, last_mask ) == -1) goto done;
431 ret = 1;
433 done:
434 resume_after_ptrace( thread );
436 return ret;
439 /* retrieve an LDT selector entry */
440 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
441 unsigned int *limit, unsigned char *flags )
443 if (!thread->process->ldt_copy)
445 set_error( STATUS_ACCESS_DENIED );
446 return;
448 if (entry >= 8192)
450 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
451 return;
453 if (suspend_for_ptrace( thread ))
455 unsigned char flags_buf[4];
456 int *addr = (int *)thread->process->ldt_copy + entry;
457 if (read_thread_int( thread, addr, (int *)base ) == -1) goto done;
458 if (read_thread_int( thread, addr + 8192, (int *)limit ) == -1) goto done;
459 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
460 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
461 *flags = flags_buf[entry & 3];
462 done:
463 resume_after_ptrace( thread );