wgl: Fix a render_texture regression. Only enable GLX_ATI_render_texture when the...
[wine.git] / server / ptrace.c
blobf1a9afb7c3dc2f3e08910f2102c7b97ec00f2f7b
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 #ifdef USE_PTRACE
50 #ifndef PTRACE_CONT
51 #define PTRACE_CONT PT_CONTINUE
52 #endif
53 #ifndef PTRACE_SINGLESTEP
54 #define PTRACE_SINGLESTEP PT_STEP
55 #endif
56 #ifndef PTRACE_ATTACH
57 #define PTRACE_ATTACH PT_ATTACH
58 #endif
59 #ifndef PTRACE_DETACH
60 #define PTRACE_DETACH PT_DETACH
61 #endif
62 #ifndef PTRACE_PEEKDATA
63 #define PTRACE_PEEKDATA PT_READ_D
64 #endif
65 #ifndef PTRACE_POKEDATA
66 #define PTRACE_POKEDATA PT_WRITE_D
67 #endif
68 #ifndef PTRACE_PEEKUSER
69 #define PTRACE_PEEKUSER PT_READ_U
70 #endif
71 #ifndef PTRACE_POKEUSER
72 #define PTRACE_POKEUSER PT_WRITE_U
73 #endif
75 #ifdef PT_GETDBREGS
76 #define PTRACE_GETDBREGS PT_GETDBREGS
77 #endif
78 #ifdef PT_SETDBREGS
79 #define PTRACE_SETDBREGS PT_SETDBREGS
80 #endif
82 #ifndef HAVE_SYS_PTRACE_H
83 #define PT_CONTINUE 0
84 #define PT_ATTACH 1
85 #define PT_DETACH 2
86 #define PT_READ_D 3
87 #define PT_WRITE_D 4
88 #define PT_STEP 5
89 static inline int ptrace(int req, ...) { errno = EPERM; return -1; /*FAIL*/ }
90 #endif /* HAVE_SYS_PTRACE_H */
92 /* handle a status returned by wait4 */
93 static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
95 if (WIFSTOPPED(status))
97 int sig = WSTOPSIG(status);
98 if (debug_level && thread)
99 fprintf( stderr, "%04x: *signal* signal=%d\n", thread->id, sig );
100 if (sig != want_sig)
102 /* ignore other signals for now */
103 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
105 return sig;
107 if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
109 thread->unix_pid = -1;
110 thread->unix_tid = -1;
111 if (debug_level)
113 if (WIFSIGNALED(status))
114 fprintf( stderr, "%04x: *exited* signal=%d\n",
115 thread->id, WTERMSIG(status) );
116 else
117 fprintf( stderr, "%04x: *exited* status=%d\n",
118 thread->id, WEXITSTATUS(status) );
121 return 0;
124 /* wait4 wrapper to handle missing __WALL flag in older kernels */
125 static inline pid_t wait4_wrapper( pid_t pid, int *status, int options, struct rusage *usage )
127 #ifdef __WALL
128 static int wall_flag = __WALL;
130 for (;;)
132 pid_t ret = wait4( pid, status, options | wall_flag, usage );
133 if (ret != -1 || !wall_flag || errno != EINVAL) return ret;
134 wall_flag = 0;
136 #else
137 return wait4( pid, status, options, usage );
138 #endif
141 /* handle a SIGCHLD signal */
142 void sigchld_callback(void)
144 int pid, status;
146 for (;;)
148 if (!(pid = wait4_wrapper( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
149 if (pid != -1)
151 struct thread *thread = get_thread_from_tid( pid );
152 if (!thread) thread = get_thread_from_pid( pid );
153 handle_child_status( thread, pid, status, -1 );
155 else break;
159 /* return the Unix pid to use in ptrace calls for a given process */
160 static int get_ptrace_pid( struct thread *thread )
162 #ifdef linux /* linux always uses thread id */
163 if (thread->unix_tid != -1) return thread->unix_tid;
164 #endif
165 return thread->unix_pid;
168 /* return the Unix tid to use in ptrace calls for a given thread */
169 static int get_ptrace_tid( struct thread *thread )
171 if (thread->unix_tid != -1) return thread->unix_tid;
172 return thread->unix_pid;
175 /* wait for a ptraced child to get a certain signal */
176 static int wait4_thread( struct thread *thread, int signal )
178 int res, status;
180 start_watchdog();
181 for (;;)
183 if ((res = wait4_wrapper( get_ptrace_pid(thread), &status, WUNTRACED, NULL )) == -1)
185 if (errno == EINTR)
187 if (!watchdog_triggered()) continue;
188 if (debug_level) fprintf( stderr, "%04x: *watchdog* wait4 aborted\n", thread->id );
190 else if (errno == ECHILD) /* must have died */
192 thread->unix_pid = -1;
193 thread->unix_tid = -1;
195 else perror( "wait4" );
196 stop_watchdog();
197 return 0;
199 res = handle_child_status( thread, res, status, signal );
200 if (!res || res == signal) break;
202 stop_watchdog();
203 return (thread->unix_pid != -1);
206 /* send a signal to a specific thread */
207 static inline int tkill( int tgid, int pid, int sig )
209 int ret = -ENOSYS;
211 #ifdef __linux__
212 # ifdef __i386__
213 __asm__( "pushl %%ebx\n\t"
214 "movl %2,%%ebx\n\t"
215 "int $0x80\n\t"
216 "popl %%ebx\n\t"
217 : "=a" (ret)
218 : "0" (270) /*SYS_tgkill*/, "r" (tgid), "c" (pid), "d" (sig) );
219 if (ret == -ENOSYS)
220 __asm__( "pushl %%ebx\n\t"
221 "movl %2,%%ebx\n\t"
222 "int $0x80\n\t"
223 "popl %%ebx\n\t"
224 : "=a" (ret)
225 : "0" (238) /*SYS_tkill*/, "r" (pid), "c" (sig) );
226 # elif defined(__x86_64__)
227 __asm__( "syscall" : "=a" (ret)
228 : "0" (200) /*SYS_tkill*/, "D" (pid), "S" (sig) );
229 # endif
230 #endif /* __linux__ */
232 if (ret >= 0) return ret;
233 errno = -ret;
234 return -1;
237 /* initialize the process tracing mechanism */
238 void init_tracing_mechanism(void)
240 /* no initialization needed for ptrace */
243 /* initialize the per-process tracing mechanism */
244 void init_process_tracing( struct process *process )
246 /* ptrace setup is done on-demand */
249 /* terminate the per-process tracing mechanism */
250 void finish_process_tracing( struct process *process )
254 /* send a Unix signal to a specific thread */
255 int send_thread_signal( struct thread *thread, int sig )
257 int ret = -1;
259 if (thread->unix_pid != -1)
261 if (thread->unix_tid != -1)
263 ret = tkill( thread->unix_pid, thread->unix_tid, sig );
264 if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
266 else ret = kill( thread->unix_pid, sig );
268 if (ret == -1 && errno == ESRCH) /* thread got killed */
270 thread->unix_pid = -1;
271 thread->unix_tid = -1;
274 if (debug_level && ret != -1)
275 fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
276 return (ret != -1);
279 /* resume a thread after we have used ptrace on it */
280 static void resume_after_ptrace( struct thread *thread )
282 if (thread->unix_pid == -1) return;
283 if (ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, 0 ) == -1)
285 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
289 /* suspend a thread to allow using ptrace on it */
290 /* you must do a resume_after_ptrace when finished with the thread */
291 static int suspend_for_ptrace( struct thread *thread )
293 /* can't stop a thread while initialisation is in progress */
294 if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
296 /* this may fail if the client is already being debugged */
297 if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
299 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
300 goto error;
302 if (wait4_thread( thread, SIGSTOP )) return 1;
303 resume_after_ptrace( thread );
304 error:
305 set_error( STATUS_ACCESS_DENIED );
306 return 0;
309 /* read an int from a thread address space */
310 static int read_thread_int( struct thread *thread, int *addr, int *data )
312 errno = 0;
313 *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
314 if ( *data == -1 && errno)
316 file_set_error();
317 return -1;
319 return 0;
322 /* write an int to a thread address space */
323 static int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
325 int res;
326 if (mask != ~0u)
328 if (read_thread_int( thread, addr, &res ) == -1) return -1;
329 data = (data & mask) | (res & ~mask);
331 if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
332 file_set_error();
333 return res;
336 /* return a thread of the process suitable for ptracing */
337 static struct thread *get_ptrace_thread( struct process *process )
339 struct thread *thread;
341 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
343 if (thread->unix_pid != -1) return thread;
345 set_error( STATUS_ACCESS_DENIED ); /* process is dead */
346 return NULL;
349 /* read data from a process memory space */
350 int read_process_memory( struct process *process, const void *ptr, data_size_t size, char *dest )
352 struct thread *thread = get_ptrace_thread( process );
353 unsigned int first_offset, last_offset, len;
354 int data, *addr;
356 if (!thread) return 0;
358 first_offset = (unsigned long)ptr % sizeof(int);
359 last_offset = (size + first_offset) % sizeof(int);
360 if (!last_offset) last_offset = sizeof(int);
362 addr = (int *)((char *)ptr - first_offset);
363 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
365 if (suspend_for_ptrace( thread ))
367 if (len > 1)
369 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
370 memcpy( dest, (char *)&data + first_offset, sizeof(int) - first_offset );
371 dest += sizeof(int) - first_offset;
372 first_offset = 0;
373 len--;
376 while (len > 1)
378 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
379 memcpy( dest, &data, sizeof(int) );
380 dest += sizeof(int);
381 len--;
384 if (read_thread_int( thread, addr++, &data ) == -1) goto done;
385 memcpy( dest, (char *)&data + first_offset, last_offset - first_offset );
386 len--;
388 done:
389 resume_after_ptrace( thread );
391 return !len;
394 /* make sure we can write to the whole address range */
395 /* len is the total size (in ints) */
396 static int check_process_write_access( struct thread *thread, int *addr, data_size_t len )
398 int page = get_page_size() / sizeof(int);
400 for (;;)
402 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
403 if (len <= page) break;
404 addr += page;
405 len -= page;
407 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
410 /* write data to a process memory space */
411 int write_process_memory( struct process *process, void *ptr, data_size_t size, const char *src )
413 struct thread *thread = get_ptrace_thread( process );
414 int ret = 0, data = 0;
415 data_size_t len;
416 int *addr;
417 unsigned int first_mask, first_offset, last_mask, last_offset;
419 if (!thread) return 0;
421 /* compute the mask for the first int */
422 first_mask = ~0;
423 first_offset = (unsigned long)ptr % sizeof(int);
424 memset( &first_mask, 0, first_offset );
426 /* compute the mask for the last int */
427 last_offset = (size + first_offset) % sizeof(int);
428 if (!last_offset) last_offset = sizeof(int);
429 last_mask = 0;
430 memset( &last_mask, 0xff, last_offset );
432 addr = (int *)((char *)ptr - first_offset);
433 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
435 if (suspend_for_ptrace( thread ))
437 if (!check_process_write_access( thread, addr, len ))
439 set_error( STATUS_ACCESS_DENIED );
440 goto done;
442 /* first word is special */
443 if (len > 1)
445 memcpy( (char *)&data + first_offset, src, sizeof(int) - first_offset );
446 src += sizeof(int) - first_offset;
447 if (write_thread_int( thread, addr++, data, first_mask ) == -1) goto done;
448 first_offset = 0;
449 len--;
451 else last_mask &= first_mask;
453 while (len > 1)
455 memcpy( &data, src, sizeof(int) );
456 src += sizeof(int);
457 if (write_thread_int( thread, addr++, data, ~0 ) == -1) goto done;
458 len--;
461 /* last word is special too */
462 memcpy( (char *)&data + first_offset, src, last_offset - first_offset );
463 if (write_thread_int( thread, addr, data, last_mask ) == -1) goto done;
464 ret = 1;
466 done:
467 resume_after_ptrace( thread );
469 return ret;
472 /* retrieve an LDT selector entry */
473 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
474 unsigned int *limit, unsigned char *flags )
476 if (!thread->process->ldt_copy)
478 set_error( STATUS_ACCESS_DENIED );
479 return;
481 if (entry >= 8192)
483 set_error( STATUS_ACCESS_VIOLATION );
484 return;
486 if (suspend_for_ptrace( thread ))
488 unsigned char flags_buf[4];
489 int *addr = (int *)thread->process->ldt_copy + entry;
490 if (read_thread_int( thread, addr, (int *)base ) == -1) goto done;
491 if (read_thread_int( thread, addr + 8192, (int *)limit ) == -1) goto done;
492 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
493 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
494 *flags = flags_buf[entry & 3];
495 done:
496 resume_after_ptrace( thread );
501 #if defined(linux) && (defined(__i386__) || defined(__x86_64__))
503 #ifdef HAVE_SYS_USER_H
504 # include <sys/user.h>
505 #endif
507 /* debug register offset in struct user */
508 #define DR_OFFSET(dr) ((((struct user *)0)->u_debugreg) + (dr))
510 /* retrieve the thread x86 registers */
511 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
513 int i, pid = get_ptrace_tid(thread);
514 long data[8];
516 /* all other regs are handled on the client side */
517 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
519 if (!suspend_for_ptrace( thread )) return;
521 for (i = 0; i < 8; i++)
523 if (i == 4 || i == 5) continue;
524 errno = 0;
525 data[i] = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(i), 0 );
526 if ((data[i] == -1) && errno)
528 file_set_error();
529 goto done;
532 context->Dr0 = data[0];
533 context->Dr1 = data[1];
534 context->Dr2 = data[2];
535 context->Dr3 = data[3];
536 context->Dr6 = data[6];
537 context->Dr7 = data[7];
538 context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
539 done:
540 resume_after_ptrace( thread );
543 /* set the thread x86 registers */
544 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
546 int pid = get_ptrace_tid( thread );
548 /* all other regs are handled on the client side */
549 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
551 if (!suspend_for_ptrace( thread )) return;
553 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
554 if (thread->context) thread->context->Dr0 = context->Dr0;
555 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
556 if (thread->context) thread->context->Dr1 = context->Dr1;
557 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
558 if (thread->context) thread->context->Dr2 = context->Dr2;
559 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
560 if (thread->context) thread->context->Dr3 = context->Dr3;
561 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
562 if (thread->context) thread->context->Dr6 = context->Dr6;
563 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
564 if (thread->context) thread->context->Dr7 = context->Dr7;
565 resume_after_ptrace( thread );
566 return;
567 error:
568 file_set_error();
569 resume_after_ptrace( thread );
572 #elif defined(__i386__) && defined(PTRACE_GETDBREGS) && defined(PTRACE_SETDBREGS) && \
573 (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__))
575 #include <machine/reg.h>
577 /* retrieve the thread x86 registers */
578 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
580 int pid = get_ptrace_tid(thread);
581 struct dbreg dbregs;
583 /* all other regs are handled on the client side */
584 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
586 if (!suspend_for_ptrace( thread )) return;
588 if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) file_set_error();
589 else
591 #ifdef DBREG_DRX
592 /* needed for FreeBSD, the structure fields have changed under 5.x */
593 context->Dr0 = DBREG_DRX((&dbregs), 0);
594 context->Dr1 = DBREG_DRX((&dbregs), 1);
595 context->Dr2 = DBREG_DRX((&dbregs), 2);
596 context->Dr3 = DBREG_DRX((&dbregs), 3);
597 context->Dr6 = DBREG_DRX((&dbregs), 6);
598 context->Dr7 = DBREG_DRX((&dbregs), 7);
599 #else
600 context->Dr0 = dbregs.dr0;
601 context->Dr1 = dbregs.dr1;
602 context->Dr2 = dbregs.dr2;
603 context->Dr3 = dbregs.dr3;
604 context->Dr6 = dbregs.dr6;
605 context->Dr7 = dbregs.dr7;
606 #endif
607 context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
609 resume_after_ptrace( thread );
612 /* set the thread x86 registers */
613 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
615 int pid = get_ptrace_tid(thread);
616 struct dbreg dbregs;
618 /* all other regs are handled on the client side */
619 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
621 if (!suspend_for_ptrace( thread )) return;
623 #ifdef DBREG_DRX
624 /* needed for FreeBSD, the structure fields have changed under 5.x */
625 DBREG_DRX((&dbregs), 0) = context->Dr0;
626 DBREG_DRX((&dbregs), 1) = context->Dr1;
627 DBREG_DRX((&dbregs), 2) = context->Dr2;
628 DBREG_DRX((&dbregs), 3) = context->Dr3;
629 DBREG_DRX((&dbregs), 4) = 0;
630 DBREG_DRX((&dbregs), 5) = 0;
631 DBREG_DRX((&dbregs), 6) = context->Dr6;
632 DBREG_DRX((&dbregs), 7) = context->Dr7;
633 #else
634 dbregs.dr0 = context->Dr0;
635 dbregs.dr1 = context->Dr1;
636 dbregs.dr2 = context->Dr2;
637 dbregs.dr3 = context->Dr3;
638 dbregs.dr4 = 0;
639 dbregs.dr5 = 0;
640 dbregs.dr6 = context->Dr6;
641 dbregs.dr7 = context->Dr7;
642 #endif
643 if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) file_set_error();
644 else if (thread->context) /* update the cached values */
646 thread->context->Dr0 = context->Dr0;
647 thread->context->Dr1 = context->Dr1;
648 thread->context->Dr2 = context->Dr2;
649 thread->context->Dr3 = context->Dr3;
650 thread->context->Dr6 = context->Dr6;
651 thread->context->Dr7 = context->Dr7;
653 resume_after_ptrace( thread );
656 #else /* linux || __FreeBSD__ */
658 /* retrieve the thread x86 registers */
659 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
663 /* set the thread x86 debug registers */
664 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
668 #endif /* linux || __FreeBSD__ */
670 #endif /* USE_PTRACE */