server: Moved get/set_thread_context implementation to ptrace.c.
[wine/multimedia.git] / server / ptrace.c
blob7aea8d5dfdfa7c04ea33a2d305b282f9ec492d40
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_REG_H
33 #include <sys/reg.h>
34 #endif
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
37 #endif
38 #ifdef HAVE_SYS_WAIT_H
39 # include <sys/wait.h>
40 #endif
41 #include <unistd.h>
43 #include "ntstatus.h"
44 #define WIN32_NO_STATUS
45 #include "winternl.h"
47 #include "file.h"
48 #include "process.h"
49 #include "thread.h"
51 #ifndef PTRACE_CONT
52 #define PTRACE_CONT PT_CONTINUE
53 #endif
54 #ifndef PTRACE_SINGLESTEP
55 #define PTRACE_SINGLESTEP PT_STEP
56 #endif
57 #ifndef PTRACE_ATTACH
58 #define PTRACE_ATTACH PT_ATTACH
59 #endif
60 #ifndef PTRACE_DETACH
61 #define PTRACE_DETACH PT_DETACH
62 #endif
63 #ifndef PTRACE_PEEKDATA
64 #define PTRACE_PEEKDATA PT_READ_D
65 #endif
66 #ifndef PTRACE_POKEDATA
67 #define PTRACE_POKEDATA PT_WRITE_D
68 #endif
69 #ifndef PTRACE_PEEKUSER
70 #define PTRACE_PEEKUSER PT_READ_U
71 #endif
72 #ifndef PTRACE_POKEUSER
73 #define PTRACE_POKEUSER PT_WRITE_U
74 #endif
76 #ifdef PT_GETDBREGS
77 #define PTRACE_GETDBREGS PT_GETDBREGS
78 #endif
79 #ifdef PT_SETDBREGS
80 #define PTRACE_SETDBREGS PT_SETDBREGS
81 #endif
83 #ifndef HAVE_SYS_PTRACE_H
84 #define PT_CONTINUE 0
85 #define PT_ATTACH 1
86 #define PT_DETACH 2
87 #define PT_READ_D 3
88 #define PT_WRITE_D 4
89 #define PT_STEP 5
90 inline static int ptrace(int req, ...) { errno = EPERM; return -1; /*FAIL*/ }
91 #endif /* HAVE_SYS_PTRACE_H */
93 /* handle a status returned by wait4 */
94 static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
96 if (WIFSTOPPED(status))
98 int sig = WSTOPSIG(status);
99 if (debug_level && thread)
100 fprintf( stderr, "%04x: *signal* signal=%d\n", thread->id, sig );
101 if (sig != want_sig)
103 /* ignore other signals for now */
104 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
106 return sig;
108 if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
110 thread->unix_pid = -1;
111 thread->unix_tid = -1;
112 if (debug_level)
114 if (WIFSIGNALED(status))
115 fprintf( stderr, "%04x: *exited* signal=%d\n",
116 thread->id, WTERMSIG(status) );
117 else
118 fprintf( stderr, "%04x: *exited* status=%d\n",
119 thread->id, WEXITSTATUS(status) );
122 return 0;
125 /* wait4 wrapper to handle missing __WALL flag in older kernels */
126 static inline pid_t wait4_wrapper( pid_t pid, int *status, int options, struct rusage *usage )
128 #ifdef __WALL
129 static int wall_flag = __WALL;
131 for (;;)
133 pid_t ret = wait4( pid, status, options | wall_flag, usage );
134 if (ret != -1 || !wall_flag || errno != EINVAL) return ret;
135 wall_flag = 0;
137 #else
138 return wait4( pid, status, options, usage );
139 #endif
142 /* handle a SIGCHLD signal */
143 void sigchld_callback(void)
145 int pid, status;
147 for (;;)
149 if (!(pid = wait4_wrapper( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
150 if (pid != -1) handle_child_status( get_thread_from_pid(pid), pid, status, -1 );
151 else break;
155 /* return the Unix pid to use in ptrace calls for a given thread */
156 static int get_ptrace_pid( struct thread *thread )
158 if (thread->unix_tid != -1) return thread->unix_tid;
159 return thread->unix_pid;
162 /* wait for a ptraced child to get a certain signal */
163 static int wait4_thread( struct thread *thread, int signal )
165 int res, status;
167 start_watchdog();
168 for (;;)
170 if ((res = wait4_wrapper( get_ptrace_pid(thread), &status, WUNTRACED, NULL )) == -1)
172 if (errno == EINTR)
174 if (!watchdog_triggered()) continue;
175 if (debug_level) fprintf( stderr, "%04x: *watchdog* wait4 aborted\n", thread->id );
177 else if (errno == ECHILD) /* must have died */
179 thread->unix_pid = -1;
180 thread->unix_tid = -1;
182 else perror( "wait4" );
183 stop_watchdog();
184 return 0;
186 res = handle_child_status( thread, res, status, signal );
187 if (!res || res == signal) break;
189 stop_watchdog();
190 return (thread->unix_pid != -1);
193 /* send a signal to a specific thread */
194 static inline int tkill( int tgid, int pid, int sig )
196 int ret = -ENOSYS;
198 #ifdef __linux__
199 # ifdef __i386__
200 __asm__( "pushl %%ebx\n\t"
201 "movl %2,%%ebx\n\t"
202 "int $0x80\n\t"
203 "popl %%ebx\n\t"
204 : "=a" (ret)
205 : "0" (270) /*SYS_tgkill*/, "r" (tgid), "c" (pid), "d" (sig) );
206 if (ret == -ENOSYS)
207 __asm__( "pushl %%ebx\n\t"
208 "movl %2,%%ebx\n\t"
209 "int $0x80\n\t"
210 "popl %%ebx\n\t"
211 : "=a" (ret)
212 : "0" (238) /*SYS_tkill*/, "r" (pid), "c" (sig) );
213 # elif defined(__x86_64__)
214 __asm__( "syscall" : "=a" (ret)
215 : "0" (200) /*SYS_tkill*/, "D" (pid), "S" (sig) );
216 # endif
217 #endif /* __linux__ */
219 if (ret >= 0) return ret;
220 errno = -ret;
221 return -1;
224 /* send a Unix signal to a specific thread */
225 int send_thread_signal( struct thread *thread, int sig )
227 int ret = -1;
229 if (thread->unix_pid != -1)
231 if (thread->unix_tid != -1)
233 ret = tkill( thread->unix_pid, thread->unix_tid, sig );
234 if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
236 else ret = kill( thread->unix_pid, sig );
238 if (ret == -1 && errno == ESRCH) /* thread got killed */
240 thread->unix_pid = -1;
241 thread->unix_tid = -1;
244 return (ret != -1);
247 /* resume a thread after we have used ptrace on it */
248 static void resume_after_ptrace( struct thread *thread )
250 if (thread->unix_pid == -1) return;
251 if (ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, 0 ) == -1)
253 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
257 /* suspend a thread to allow using ptrace on it */
258 /* you must do a resume_after_ptrace when finished with the thread */
259 static int suspend_for_ptrace( struct thread *thread )
261 /* can't stop a thread while initialisation is in progress */
262 if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
264 /* this may fail if the client is already being debugged */
265 if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
267 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
268 goto error;
270 if (wait4_thread( thread, SIGSTOP )) return 1;
271 resume_after_ptrace( thread );
272 error:
273 set_error( STATUS_ACCESS_DENIED );
274 return 0;
277 /* read an int from a thread address space */
278 static int read_thread_int( struct thread *thread, const int *addr, int *data )
280 errno = 0;
281 *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
282 if ( *data == -1 && errno)
284 file_set_error();
285 return -1;
287 return 0;
290 /* write an int to a thread address space */
291 static int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
293 int res;
294 if (mask != ~0)
296 if (read_thread_int( thread, addr, &res ) == -1) return -1;
297 data = (data & mask) | (res & ~mask);
299 if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
300 file_set_error();
301 return res;
304 /* read data from a process memory space */
305 int read_process_memory( struct process *process, const void *ptr, size_t size, char *dest )
307 struct thread *thread = get_process_first_thread( process );
308 unsigned int first_offset, last_offset, len;
309 int data, *addr;
311 if (!thread) /* process is dead */
313 set_error( STATUS_ACCESS_DENIED );
314 return 0;
317 first_offset = (unsigned long)ptr % sizeof(int);
318 last_offset = (size + first_offset) % sizeof(int);
319 if (!last_offset) last_offset = sizeof(int);
321 addr = (int *)((char *)ptr - first_offset);
322 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
324 if (suspend_for_ptrace( thread ))
326 if (len > 1)
328 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
329 memcpy( dest, (char *)&data + first_offset, sizeof(int) - first_offset );
330 dest += sizeof(int) - first_offset;
331 first_offset = 0;
332 len--;
335 while (len > 1)
337 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
338 memcpy( dest, &data, sizeof(int) );
339 dest += sizeof(int);
340 len--;
343 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
344 memcpy( dest, (char *)&data + first_offset, last_offset - first_offset );
345 len--;
347 done:
348 resume_after_ptrace( thread );
350 return !len;
353 /* make sure we can write to the whole address range */
354 /* len is the total size (in ints) */
355 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
357 int page = get_page_size() / sizeof(int);
359 for (;;)
361 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
362 if (len <= page) break;
363 addr += page;
364 len -= page;
366 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
369 /* write data to a process memory space */
370 int write_process_memory( struct process *process, void *ptr, size_t size, const char *src )
372 struct thread *thread = get_process_first_thread( process );
373 int ret = 0, data = 0;
374 size_t len;
375 int *addr;
376 unsigned int first_mask, first_offset, last_mask, last_offset;
378 if (!thread) /* process is dead */
380 set_error( STATUS_ACCESS_DENIED );
381 return 0;
384 /* compute the mask for the first int */
385 first_mask = ~0;
386 first_offset = (unsigned long)ptr % sizeof(int);
387 memset( &first_mask, 0, first_offset );
389 /* compute the mask for the last int */
390 last_offset = (size + first_offset) % sizeof(int);
391 if (!last_offset) last_offset = sizeof(int);
392 last_mask = 0;
393 memset( &last_mask, 0xff, last_offset );
395 addr = (int *)((char *)ptr - first_offset);
396 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
398 if (suspend_for_ptrace( thread ))
400 if (!check_process_write_access( thread, addr, len ))
402 set_error( STATUS_ACCESS_DENIED );
403 goto done;
405 /* first word is special */
406 if (len > 1)
408 memcpy( (char *)&data + first_offset, src, sizeof(int) - first_offset );
409 src += sizeof(int) - first_offset;
410 if (write_thread_int( thread, addr++, data, first_mask ) == -1) goto done;
411 first_offset = 0;
412 len--;
414 else last_mask &= first_mask;
416 while (len > 1)
418 memcpy( &data, src, sizeof(int) );
419 src += sizeof(int);
420 if (write_thread_int( thread, addr++, data, ~0 ) == -1) goto done;
421 len--;
424 /* last word is special too */
425 memcpy( (char *)&data + first_offset, src, last_offset - first_offset );
426 if (write_thread_int( thread, addr, data, last_mask ) == -1) goto done;
427 ret = 1;
429 done:
430 resume_after_ptrace( thread );
432 return ret;
435 /* retrieve an LDT selector entry */
436 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
437 unsigned int *limit, unsigned char *flags )
439 if (!thread->process->ldt_copy)
441 set_error( STATUS_ACCESS_DENIED );
442 return;
444 if (entry >= 8192)
446 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
447 return;
449 if (suspend_for_ptrace( thread ))
451 unsigned char flags_buf[4];
452 int *addr = (int *)thread->process->ldt_copy + entry;
453 if (read_thread_int( thread, addr, (int *)base ) == -1) goto done;
454 if (read_thread_int( thread, addr + 8192, (int *)limit ) == -1) goto done;
455 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
456 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
457 *flags = flags_buf[entry & 3];
458 done:
459 resume_after_ptrace( thread );
464 #if defined(linux) && (defined(__i386__) || defined(__x86_64__))
466 #ifdef HAVE_SYS_USER_H
467 # include <sys/user.h>
468 #endif
470 /* debug register offset in struct user */
471 #define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr)))
473 /* retrieve a debug register */
474 static inline int get_debug_reg( int pid, int num, DWORD *data )
476 int res;
477 errno = 0;
478 res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 );
479 if ((res == -1) && errno)
481 file_set_error();
482 return -1;
484 *data = res;
485 return 0;
488 /* retrieve the thread x86 registers */
489 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
491 int pid = get_ptrace_pid(thread);
493 /* all other regs are handled on the client side */
494 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
496 if (!suspend_for_ptrace( thread )) return;
498 if (get_debug_reg( pid, 0, &context->Dr0 ) == -1) goto error;
499 if (get_debug_reg( pid, 1, &context->Dr1 ) == -1) goto error;
500 if (get_debug_reg( pid, 2, &context->Dr2 ) == -1) goto error;
501 if (get_debug_reg( pid, 3, &context->Dr3 ) == -1) goto error;
502 if (get_debug_reg( pid, 6, &context->Dr6 ) == -1) goto error;
503 if (get_debug_reg( pid, 7, &context->Dr7 ) == -1) goto error;
504 context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
505 resume_after_ptrace( thread );
506 return;
507 error:
508 file_set_error();
509 resume_after_ptrace( thread );
512 /* set the thread x86 registers */
513 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
515 int pid = get_ptrace_pid( thread );
517 /* all other regs are handled on the client side */
518 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
520 if (!suspend_for_ptrace( thread )) return;
522 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
523 if (thread->context) thread->context->Dr0 = context->Dr0;
524 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
525 if (thread->context) thread->context->Dr1 = context->Dr1;
526 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
527 if (thread->context) thread->context->Dr2 = context->Dr2;
528 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
529 if (thread->context) thread->context->Dr3 = context->Dr3;
530 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
531 if (thread->context) thread->context->Dr6 = context->Dr6;
532 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
533 if (thread->context) thread->context->Dr7 = context->Dr7;
534 resume_after_ptrace( thread );
535 return;
536 error:
537 file_set_error();
538 resume_after_ptrace( thread );
541 #elif defined(__i386__) && defined(PTRACE_GETDBREGS) && defined(PTRACE_SETDBREGS) && \
542 (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__))
544 #include <machine/reg.h>
546 /* retrieve the thread x86 registers */
547 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
549 int pid = get_ptrace_pid(thread);
550 struct dbreg dbregs;
552 /* all other regs are handled on the client side */
553 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
555 if (!suspend_for_ptrace( thread )) return;
557 if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) file_set_error();
558 else
560 #ifdef DBREG_DRX
561 /* needed for FreeBSD, the structure fields have changed under 5.x */
562 context->Dr0 = DBREG_DRX((&dbregs), 0);
563 context->Dr1 = DBREG_DRX((&dbregs), 1);
564 context->Dr2 = DBREG_DRX((&dbregs), 2);
565 context->Dr3 = DBREG_DRX((&dbregs), 3);
566 context->Dr6 = DBREG_DRX((&dbregs), 6);
567 context->Dr7 = DBREG_DRX((&dbregs), 7);
568 #else
569 context->Dr0 = dbregs.dr0;
570 context->Dr1 = dbregs.dr1;
571 context->Dr2 = dbregs.dr2;
572 context->Dr3 = dbregs.dr3;
573 context->Dr6 = dbregs.dr6;
574 context->Dr7 = dbregs.dr7;
575 #endif
576 context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
578 resume_after_ptrace( thread );
581 /* set the thread x86 registers */
582 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
584 int pid = get_ptrace_pid(thread);
585 struct dbreg dbregs;
587 /* all other regs are handled on the client side */
588 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
590 if (!suspend_for_ptrace( thread )) return;
592 #ifdef DBREG_DRX
593 /* needed for FreeBSD, the structure fields have changed under 5.x */
594 DBREG_DRX((&dbregs), 0) = context->Dr0;
595 DBREG_DRX((&dbregs), 1) = context->Dr1;
596 DBREG_DRX((&dbregs), 2) = context->Dr2;
597 DBREG_DRX((&dbregs), 3) = context->Dr3;
598 DBREG_DRX((&dbregs), 4) = 0;
599 DBREG_DRX((&dbregs), 5) = 0;
600 DBREG_DRX((&dbregs), 6) = context->Dr6;
601 DBREG_DRX((&dbregs), 7) = context->Dr7;
602 #else
603 dbregs.dr0 = context->Dr0;
604 dbregs.dr1 = context->Dr1;
605 dbregs.dr2 = context->Dr2;
606 dbregs.dr3 = context->Dr3;
607 dbregs.dr4 = 0;
608 dbregs.dr5 = 0;
609 dbregs.dr6 = context->Dr6;
610 dbregs.dr7 = context->Dr7;
611 #endif
612 if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) file_set_error();
613 else if (thread->context) /* update the cached values */
615 thread->context->Dr0 = context->Dr0;
616 thread->context->Dr1 = context->Dr1;
617 thread->context->Dr2 = context->Dr2;
618 thread->context->Dr3 = context->Dr3;
619 thread->context->Dr6 = context->Dr6;
620 thread->context->Dr7 = context->Dr7;
622 resume_after_ptrace( thread );
625 #else /* linux || __FreeBSD__ */
627 /* retrieve the thread x86 registers */
628 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
632 /* set the thread x86 debug registers */
633 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
637 #endif /* linux || __FreeBSD__ */