server: Add hooks to support process tracing mechanisms other than ptrace.
[wine/hacks.git] / server / ptrace.c
blob7b0bfa0754f8fe7e4da3ce24a9894f36d4cbc703
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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_PARAM_H
33 # include <sys/param.h>
34 #endif
35 #ifdef HAVE_SYS_WAIT_H
36 # include <sys/wait.h>
37 #endif
38 #include <unistd.h>
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "winternl.h"
44 #include "file.h"
45 #include "process.h"
46 #include "thread.h"
48 #ifndef PTRACE_CONT
49 #define PTRACE_CONT PT_CONTINUE
50 #endif
51 #ifndef PTRACE_SINGLESTEP
52 #define PTRACE_SINGLESTEP PT_STEP
53 #endif
54 #ifndef PTRACE_ATTACH
55 #define PTRACE_ATTACH PT_ATTACH
56 #endif
57 #ifndef PTRACE_DETACH
58 #define PTRACE_DETACH PT_DETACH
59 #endif
60 #ifndef PTRACE_PEEKDATA
61 #define PTRACE_PEEKDATA PT_READ_D
62 #endif
63 #ifndef PTRACE_POKEDATA
64 #define PTRACE_POKEDATA PT_WRITE_D
65 #endif
66 #ifndef PTRACE_PEEKUSER
67 #define PTRACE_PEEKUSER PT_READ_U
68 #endif
69 #ifndef PTRACE_POKEUSER
70 #define PTRACE_POKEUSER PT_WRITE_U
71 #endif
73 #ifdef PT_GETDBREGS
74 #define PTRACE_GETDBREGS PT_GETDBREGS
75 #endif
76 #ifdef PT_SETDBREGS
77 #define PTRACE_SETDBREGS PT_SETDBREGS
78 #endif
80 #ifndef HAVE_SYS_PTRACE_H
81 #define PT_CONTINUE 0
82 #define PT_ATTACH 1
83 #define PT_DETACH 2
84 #define PT_READ_D 3
85 #define PT_WRITE_D 4
86 #define PT_STEP 5
87 inline static int ptrace(int req, ...) { errno = EPERM; return -1; /*FAIL*/ }
88 #endif /* HAVE_SYS_PTRACE_H */
90 /* handle a status returned by wait4 */
91 static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
93 if (WIFSTOPPED(status))
95 int sig = WSTOPSIG(status);
96 if (debug_level && thread)
97 fprintf( stderr, "%04x: *signal* signal=%d\n", thread->id, sig );
98 if (sig != want_sig)
100 /* ignore other signals for now */
101 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
103 return sig;
105 if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
107 thread->unix_pid = -1;
108 thread->unix_tid = -1;
109 if (debug_level)
111 if (WIFSIGNALED(status))
112 fprintf( stderr, "%04x: *exited* signal=%d\n",
113 thread->id, WTERMSIG(status) );
114 else
115 fprintf( stderr, "%04x: *exited* status=%d\n",
116 thread->id, WEXITSTATUS(status) );
119 return 0;
122 /* wait4 wrapper to handle missing __WALL flag in older kernels */
123 static inline pid_t wait4_wrapper( pid_t pid, int *status, int options, struct rusage *usage )
125 #ifdef __WALL
126 static int wall_flag = __WALL;
128 for (;;)
130 pid_t ret = wait4( pid, status, options | wall_flag, usage );
131 if (ret != -1 || !wall_flag || errno != EINVAL) return ret;
132 wall_flag = 0;
134 #else
135 return wait4( pid, status, options, usage );
136 #endif
139 /* handle a SIGCHLD signal */
140 void sigchld_callback(void)
142 int pid, status;
144 for (;;)
146 if (!(pid = wait4_wrapper( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
147 if (pid != -1)
149 struct thread *thread = get_thread_from_tid( pid );
150 if (!thread) thread = get_thread_from_pid( pid );
151 handle_child_status( thread, pid, status, -1 );
153 else break;
157 /* return the Unix pid to use in ptrace calls for a given thread */
158 static int get_ptrace_pid( struct thread *thread )
160 if (thread->unix_tid != -1) return thread->unix_tid;
161 return thread->unix_pid;
164 /* wait for a ptraced child to get a certain signal */
165 static int wait4_thread( struct thread *thread, int signal )
167 int res, status;
169 start_watchdog();
170 for (;;)
172 if ((res = wait4_wrapper( get_ptrace_pid(thread), &status, WUNTRACED, NULL )) == -1)
174 if (errno == EINTR)
176 if (!watchdog_triggered()) continue;
177 if (debug_level) fprintf( stderr, "%04x: *watchdog* wait4 aborted\n", thread->id );
179 else if (errno == ECHILD) /* must have died */
181 thread->unix_pid = -1;
182 thread->unix_tid = -1;
184 else perror( "wait4" );
185 stop_watchdog();
186 return 0;
188 res = handle_child_status( thread, res, status, signal );
189 if (!res || res == signal) break;
191 stop_watchdog();
192 return (thread->unix_pid != -1);
195 /* send a signal to a specific thread */
196 static inline int tkill( int tgid, int pid, int sig )
198 int ret = -ENOSYS;
200 #ifdef __linux__
201 # ifdef __i386__
202 __asm__( "pushl %%ebx\n\t"
203 "movl %2,%%ebx\n\t"
204 "int $0x80\n\t"
205 "popl %%ebx\n\t"
206 : "=a" (ret)
207 : "0" (270) /*SYS_tgkill*/, "r" (tgid), "c" (pid), "d" (sig) );
208 if (ret == -ENOSYS)
209 __asm__( "pushl %%ebx\n\t"
210 "movl %2,%%ebx\n\t"
211 "int $0x80\n\t"
212 "popl %%ebx\n\t"
213 : "=a" (ret)
214 : "0" (238) /*SYS_tkill*/, "r" (pid), "c" (sig) );
215 # elif defined(__x86_64__)
216 __asm__( "syscall" : "=a" (ret)
217 : "0" (200) /*SYS_tkill*/, "D" (pid), "S" (sig) );
218 # endif
219 #endif /* __linux__ */
221 if (ret >= 0) return ret;
222 errno = -ret;
223 return -1;
226 /* initialize the process tracing mechanism */
227 void init_tracing_mechanism(void)
229 /* no initialization needed for ptrace */
232 /* initialize the per-process tracing mechanism */
233 void init_process_tracing( struct process *process )
235 /* ptrace setup is done on-demand */
238 /* terminate the per-process tracing mechanism */
239 void finish_process_tracing( struct process *process )
243 /* send a Unix signal to a specific thread */
244 int send_thread_signal( struct thread *thread, int sig )
246 int ret = -1;
248 if (thread->unix_pid != -1)
250 if (thread->unix_tid != -1)
252 ret = tkill( thread->unix_pid, thread->unix_tid, sig );
253 if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
255 else ret = kill( thread->unix_pid, sig );
257 if (ret == -1 && errno == ESRCH) /* thread got killed */
259 thread->unix_pid = -1;
260 thread->unix_tid = -1;
263 return (ret != -1);
266 /* resume a thread after we have used ptrace on it */
267 static void resume_after_ptrace( struct thread *thread )
269 if (thread->unix_pid == -1) return;
270 if (ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, 0 ) == -1)
272 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
276 /* suspend a thread to allow using ptrace on it */
277 /* you must do a resume_after_ptrace when finished with the thread */
278 static int suspend_for_ptrace( struct thread *thread )
280 /* can't stop a thread while initialisation is in progress */
281 if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
283 /* this may fail if the client is already being debugged */
284 if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
286 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
287 goto error;
289 if (wait4_thread( thread, SIGSTOP )) return 1;
290 resume_after_ptrace( thread );
291 error:
292 set_error( STATUS_ACCESS_DENIED );
293 return 0;
296 /* read an int from a thread address space */
297 static int read_thread_int( struct thread *thread, int *addr, int *data )
299 errno = 0;
300 *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
301 if ( *data == -1 && errno)
303 file_set_error();
304 return -1;
306 return 0;
309 /* write an int to a thread address space */
310 static int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
312 int res;
313 if (mask != ~0u)
315 if (read_thread_int( thread, addr, &res ) == -1) return -1;
316 data = (data & mask) | (res & ~mask);
318 if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
319 file_set_error();
320 return res;
323 /* return a thread of the process suitable for ptracing */
324 static struct thread *get_ptrace_thread( struct process *process )
326 struct thread *thread;
328 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
330 if (thread->unix_pid != -1) return thread;
332 set_error( STATUS_ACCESS_DENIED ); /* process is dead */
333 return NULL;
336 /* read data from a process memory space */
337 int read_process_memory( struct process *process, const void *ptr, data_size_t size, char *dest )
339 struct thread *thread = get_ptrace_thread( process );
340 unsigned int first_offset, last_offset, len;
341 int data, *addr;
343 if (!thread) return 0;
345 first_offset = (unsigned long)ptr % sizeof(int);
346 last_offset = (size + first_offset) % sizeof(int);
347 if (!last_offset) last_offset = sizeof(int);
349 addr = (int *)((char *)ptr - first_offset);
350 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
352 if (suspend_for_ptrace( thread ))
354 if (len > 1)
356 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
357 memcpy( dest, (char *)&data + first_offset, sizeof(int) - first_offset );
358 dest += sizeof(int) - first_offset;
359 first_offset = 0;
360 len--;
363 while (len > 1)
365 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
366 memcpy( dest, &data, sizeof(int) );
367 dest += sizeof(int);
368 len--;
371 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
372 memcpy( dest, (char *)&data + first_offset, last_offset - first_offset );
373 len--;
375 done:
376 resume_after_ptrace( thread );
378 return !len;
381 /* make sure we can write to the whole address range */
382 /* len is the total size (in ints) */
383 static int check_process_write_access( struct thread *thread, int *addr, data_size_t len )
385 int page = get_page_size() / sizeof(int);
387 for (;;)
389 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
390 if (len <= page) break;
391 addr += page;
392 len -= page;
394 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
397 /* write data to a process memory space */
398 int write_process_memory( struct process *process, void *ptr, data_size_t size, const char *src )
400 struct thread *thread = get_ptrace_thread( process );
401 int ret = 0, data = 0;
402 data_size_t len;
403 int *addr;
404 unsigned int first_mask, first_offset, last_mask, last_offset;
406 if (!thread) return 0;
408 /* compute the mask for the first int */
409 first_mask = ~0;
410 first_offset = (unsigned long)ptr % sizeof(int);
411 memset( &first_mask, 0, first_offset );
413 /* compute the mask for the last int */
414 last_offset = (size + first_offset) % sizeof(int);
415 if (!last_offset) last_offset = sizeof(int);
416 last_mask = 0;
417 memset( &last_mask, 0xff, last_offset );
419 addr = (int *)((char *)ptr - first_offset);
420 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
422 if (suspend_for_ptrace( thread ))
424 if (!check_process_write_access( thread, addr, len ))
426 set_error( STATUS_ACCESS_DENIED );
427 goto done;
429 /* first word is special */
430 if (len > 1)
432 memcpy( (char *)&data + first_offset, src, sizeof(int) - first_offset );
433 src += sizeof(int) - first_offset;
434 if (write_thread_int( thread, addr++, data, first_mask ) == -1) goto done;
435 first_offset = 0;
436 len--;
438 else last_mask &= first_mask;
440 while (len > 1)
442 memcpy( &data, src, sizeof(int) );
443 src += sizeof(int);
444 if (write_thread_int( thread, addr++, data, ~0 ) == -1) goto done;
445 len--;
448 /* last word is special too */
449 memcpy( (char *)&data + first_offset, src, last_offset - first_offset );
450 if (write_thread_int( thread, addr, data, last_mask ) == -1) goto done;
451 ret = 1;
453 done:
454 resume_after_ptrace( thread );
456 return ret;
459 /* retrieve an LDT selector entry */
460 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
461 unsigned int *limit, unsigned char *flags )
463 if (!thread->process->ldt_copy)
465 set_error( STATUS_ACCESS_DENIED );
466 return;
468 if (entry >= 8192)
470 set_error( STATUS_ACCESS_VIOLATION );
471 return;
473 if (suspend_for_ptrace( thread ))
475 unsigned char flags_buf[4];
476 int *addr = (int *)thread->process->ldt_copy + entry;
477 if (read_thread_int( thread, addr, (int *)base ) == -1) goto done;
478 if (read_thread_int( thread, addr + 8192, (int *)limit ) == -1) goto done;
479 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
480 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
481 *flags = flags_buf[entry & 3];
482 done:
483 resume_after_ptrace( thread );
488 #if defined(linux) && (defined(__i386__) || defined(__x86_64__))
490 #ifdef HAVE_SYS_USER_H
491 # include <sys/user.h>
492 #endif
494 /* debug register offset in struct user */
495 #define DR_OFFSET(dr) ((((struct user *)0)->u_debugreg) + (dr))
497 /* retrieve the thread x86 registers */
498 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
500 int i, pid = get_ptrace_pid(thread);
501 long data[8];
503 /* all other regs are handled on the client side */
504 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
506 if (!suspend_for_ptrace( thread )) return;
508 for (i = 0; i < 8; i++)
510 if (i == 4 || i == 5) continue;
511 errno = 0;
512 data[i] = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(i), 0 );
513 if ((data[i] == -1) && errno)
515 file_set_error();
516 goto done;
519 context->Dr0 = data[0];
520 context->Dr1 = data[1];
521 context->Dr2 = data[2];
522 context->Dr3 = data[3];
523 context->Dr6 = data[6];
524 context->Dr7 = data[7];
525 context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
526 done:
527 resume_after_ptrace( thread );
530 /* set the thread x86 registers */
531 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
533 int pid = get_ptrace_pid( thread );
535 /* all other regs are handled on the client side */
536 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
538 if (!suspend_for_ptrace( thread )) return;
540 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
541 if (thread->context) thread->context->Dr0 = context->Dr0;
542 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
543 if (thread->context) thread->context->Dr1 = context->Dr1;
544 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
545 if (thread->context) thread->context->Dr2 = context->Dr2;
546 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
547 if (thread->context) thread->context->Dr3 = context->Dr3;
548 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
549 if (thread->context) thread->context->Dr6 = context->Dr6;
550 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
551 if (thread->context) thread->context->Dr7 = context->Dr7;
552 resume_after_ptrace( thread );
553 return;
554 error:
555 file_set_error();
556 resume_after_ptrace( thread );
559 #elif defined(__i386__) && defined(PTRACE_GETDBREGS) && defined(PTRACE_SETDBREGS) && \
560 (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__))
562 #include <machine/reg.h>
564 /* retrieve the thread x86 registers */
565 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
567 int pid = get_ptrace_pid(thread);
568 struct dbreg dbregs;
570 /* all other regs are handled on the client side */
571 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
573 if (!suspend_for_ptrace( thread )) return;
575 if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) file_set_error();
576 else
578 #ifdef DBREG_DRX
579 /* needed for FreeBSD, the structure fields have changed under 5.x */
580 context->Dr0 = DBREG_DRX((&dbregs), 0);
581 context->Dr1 = DBREG_DRX((&dbregs), 1);
582 context->Dr2 = DBREG_DRX((&dbregs), 2);
583 context->Dr3 = DBREG_DRX((&dbregs), 3);
584 context->Dr6 = DBREG_DRX((&dbregs), 6);
585 context->Dr7 = DBREG_DRX((&dbregs), 7);
586 #else
587 context->Dr0 = dbregs.dr0;
588 context->Dr1 = dbregs.dr1;
589 context->Dr2 = dbregs.dr2;
590 context->Dr3 = dbregs.dr3;
591 context->Dr6 = dbregs.dr6;
592 context->Dr7 = dbregs.dr7;
593 #endif
594 context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
596 resume_after_ptrace( thread );
599 /* set the thread x86 registers */
600 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
602 int pid = get_ptrace_pid(thread);
603 struct dbreg dbregs;
605 /* all other regs are handled on the client side */
606 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
608 if (!suspend_for_ptrace( thread )) return;
610 #ifdef DBREG_DRX
611 /* needed for FreeBSD, the structure fields have changed under 5.x */
612 DBREG_DRX((&dbregs), 0) = context->Dr0;
613 DBREG_DRX((&dbregs), 1) = context->Dr1;
614 DBREG_DRX((&dbregs), 2) = context->Dr2;
615 DBREG_DRX((&dbregs), 3) = context->Dr3;
616 DBREG_DRX((&dbregs), 4) = 0;
617 DBREG_DRX((&dbregs), 5) = 0;
618 DBREG_DRX((&dbregs), 6) = context->Dr6;
619 DBREG_DRX((&dbregs), 7) = context->Dr7;
620 #else
621 dbregs.dr0 = context->Dr0;
622 dbregs.dr1 = context->Dr1;
623 dbregs.dr2 = context->Dr2;
624 dbregs.dr3 = context->Dr3;
625 dbregs.dr4 = 0;
626 dbregs.dr5 = 0;
627 dbregs.dr6 = context->Dr6;
628 dbregs.dr7 = context->Dr7;
629 #endif
630 if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) file_set_error();
631 else if (thread->context) /* update the cached values */
633 thread->context->Dr0 = context->Dr0;
634 thread->context->Dr1 = context->Dr1;
635 thread->context->Dr2 = context->Dr2;
636 thread->context->Dr3 = context->Dr3;
637 thread->context->Dr6 = context->Dr6;
638 thread->context->Dr7 = context->Dr7;
640 resume_after_ptrace( thread );
643 #else /* linux || __FreeBSD__ */
645 /* retrieve the thread x86 registers */
646 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
650 /* set the thread x86 debug registers */
651 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
655 #endif /* linux || __FreeBSD__ */