oleaut32: Implement OleLoadPictureFile.
[wine.git] / server / ptrace.c
blob976d3a3778ea6cdde863730cdfa8743b05f22c97
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 <fcntl.h>
26 #include <stdio.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #ifdef HAVE_SYS_PTRACE_H
32 # include <sys/ptrace.h>
33 #endif
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
36 #endif
37 #ifdef HAVE_SYS_SYSCALL_H
38 # include <sys/syscall.h>
39 #endif
40 #ifdef HAVE_SYS_UCONTEXT_H
41 # include <sys/ucontext.h>
42 #endif
43 #ifdef HAVE_SYS_THR_H
44 # include <sys/thr.h>
45 #endif
46 #include <unistd.h>
48 #include "ntstatus.h"
49 #define WIN32_NO_STATUS
50 #include "winternl.h"
52 #include "file.h"
53 #include "process.h"
54 #include "thread.h"
56 #ifdef USE_PTRACE
58 #ifndef PTRACE_CONT
59 #define PTRACE_CONT PT_CONTINUE
60 #endif
61 #ifndef PTRACE_SINGLESTEP
62 #define PTRACE_SINGLESTEP PT_STEP
63 #endif
64 #ifndef PTRACE_ATTACH
65 #define PTRACE_ATTACH PT_ATTACH
66 #endif
67 #ifndef PTRACE_DETACH
68 #define PTRACE_DETACH PT_DETACH
69 #endif
70 #ifndef PTRACE_PEEKDATA
71 #define PTRACE_PEEKDATA PT_READ_D
72 #endif
73 #ifndef PTRACE_POKEDATA
74 #define PTRACE_POKEDATA PT_WRITE_D
75 #endif
76 #ifndef PTRACE_PEEKUSER
77 #define PTRACE_PEEKUSER PT_READ_U
78 #endif
79 #ifndef PTRACE_POKEUSER
80 #define PTRACE_POKEUSER PT_WRITE_U
81 #endif
83 #ifdef PT_GETDBREGS
84 #define PTRACE_GETDBREGS PT_GETDBREGS
85 #endif
86 #ifdef PT_SETDBREGS
87 #define PTRACE_SETDBREGS PT_SETDBREGS
88 #endif
90 #ifndef HAVE_SYS_PTRACE_H
91 #define PT_CONTINUE 0
92 #define PT_ATTACH 1
93 #define PT_DETACH 2
94 #define PT_READ_D 3
95 #define PT_WRITE_D 4
96 #define PT_STEP 5
97 static inline int ptrace(int req, ...) { errno = EPERM; return -1; /*FAIL*/ }
98 #endif /* HAVE_SYS_PTRACE_H */
100 #ifndef __WALL
101 #define __WALL 0
102 #endif
104 /* handle a status returned by waitpid */
105 static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
107 if (WIFSTOPPED(status))
109 int sig = WSTOPSIG(status);
110 if (debug_level && thread)
111 fprintf( stderr, "%04x: *signal* signal=%d\n", thread->id, sig );
112 if (sig != want_sig)
114 /* ignore other signals for now */
115 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
117 return sig;
119 if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
121 thread->unix_pid = -1;
122 thread->unix_tid = -1;
123 if (debug_level)
125 if (WIFSIGNALED(status))
126 fprintf( stderr, "%04x: *exited* signal=%d\n",
127 thread->id, WTERMSIG(status) );
128 else
129 fprintf( stderr, "%04x: *exited* status=%d\n",
130 thread->id, WEXITSTATUS(status) );
133 return 0;
136 /* handle a SIGCHLD signal */
137 void sigchld_callback(void)
139 int pid, status;
141 for (;;)
143 if (!(pid = waitpid( -1, &status, WUNTRACED | WNOHANG | __WALL ))) break;
144 if (pid != -1)
146 struct thread *thread = get_thread_from_tid( pid );
147 if (!thread) thread = get_thread_from_pid( pid );
148 handle_child_status( thread, pid, status, -1 );
150 else break;
154 /* return the Unix pid to use in ptrace calls for a given process */
155 static int get_ptrace_pid( struct thread *thread )
157 #ifdef linux /* linux always uses thread id */
158 if (thread->unix_tid != -1) return thread->unix_tid;
159 #endif
160 return thread->unix_pid;
163 /* return the Unix tid to use in ptrace calls for a given thread */
164 static int get_ptrace_tid( struct thread *thread )
166 if (thread->unix_tid != -1) return thread->unix_tid;
167 return thread->unix_pid;
170 /* wait for a ptraced child to get a certain signal */
171 static int waitpid_thread( struct thread *thread, int signal )
173 int res, status;
175 start_watchdog();
176 for (;;)
178 if ((res = waitpid( get_ptrace_pid(thread), &status, WUNTRACED | __WALL )) == -1)
180 if (errno == EINTR)
182 if (!watchdog_triggered()) continue;
183 if (debug_level) fprintf( stderr, "%04x: *watchdog* waitpid aborted\n", thread->id );
185 else if (errno == ECHILD) /* must have died */
187 thread->unix_pid = -1;
188 thread->unix_tid = -1;
190 else perror( "waitpid" );
191 stop_watchdog();
192 return 0;
194 res = handle_child_status( thread, res, status, signal );
195 if (!res || res == signal) break;
197 stop_watchdog();
198 return (thread->unix_pid != -1);
201 /* send a signal to a specific thread */
202 static inline int tkill( int tgid, int pid, int sig )
204 #ifdef __linux__
205 int ret = syscall( __NR_tgkill, tgid, pid, sig );
206 if (ret < 0 && errno == ENOSYS) ret = syscall( __NR_tkill, pid, sig );
207 return ret;
208 #elif (defined(__FreeBSD__) || defined (__FreeBSD_kernel__)) && defined(HAVE_THR_KILL2)
209 return thr_kill2( tgid, pid, sig );
210 #else
211 errno = ENOSYS;
212 return -1;
213 #endif
216 /* initialize the process tracing mechanism */
217 void init_tracing_mechanism(void)
219 /* no initialization needed for ptrace */
222 /* initialize the per-process tracing mechanism */
223 void init_process_tracing( struct process *process )
225 /* ptrace setup is done on-demand */
228 /* terminate the per-process tracing mechanism */
229 void finish_process_tracing( struct process *process )
233 /* send a Unix signal to a specific thread */
234 int send_thread_signal( struct thread *thread, int sig )
236 int ret = -1;
238 if (thread->unix_pid != -1)
240 if (thread->unix_tid != -1)
242 ret = tkill( thread->unix_pid, thread->unix_tid, sig );
243 if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
245 else ret = kill( thread->unix_pid, sig );
247 if (ret == -1 && errno == ESRCH) /* thread got killed */
249 thread->unix_pid = -1;
250 thread->unix_tid = -1;
253 if (debug_level && ret != -1)
254 fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
255 return (ret != -1);
258 /* resume a thread after we have used ptrace on it */
259 static void resume_after_ptrace( struct thread *thread )
261 if (thread->unix_pid == -1) return;
262 if (ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, 0 ) == -1)
264 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
268 /* suspend a thread to allow using ptrace on it */
269 /* you must do a resume_after_ptrace when finished with the thread */
270 static int suspend_for_ptrace( struct thread *thread )
272 /* can't stop a thread while initialisation is in progress */
273 if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
275 /* this may fail if the client is already being debugged */
276 if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
278 if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
279 goto error;
281 if (waitpid_thread( thread, SIGSTOP )) return 1;
282 resume_after_ptrace( thread );
283 error:
284 set_error( STATUS_ACCESS_DENIED );
285 return 0;
288 /* read a long from a thread address space */
289 static int read_thread_long( struct thread *thread, void *addr, unsigned long *data )
291 errno = 0;
292 *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
293 if ( *data == -1 && errno)
295 file_set_error();
296 return -1;
298 return 0;
301 static int read_thread_int( struct thread *thread, void *addr, unsigned int *data )
303 unsigned long long_data;
304 int ret;
306 ret = read_thread_long( thread, addr, &long_data );
307 *data = long_data;
308 return ret;
311 /* write a long to a thread address space */
312 static long write_thread_long( struct thread *thread, void *addr, unsigned long data, unsigned long mask )
314 unsigned long old_data;
315 int res;
317 if (mask != ~0ul)
319 if (read_thread_long( thread, addr, &old_data ) == -1) return -1;
320 data = (data & mask) | (old_data & ~mask);
322 if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
323 file_set_error();
324 return res;
327 /* return a thread of the process suitable for ptracing */
328 static struct thread *get_ptrace_thread( struct process *process )
330 struct thread *thread;
332 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
334 if (thread->unix_pid != -1) return thread;
336 set_error( STATUS_PROCESS_IS_TERMINATING ); /* process is dead */
337 return NULL;
340 /* read data from a process memory space */
341 int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
343 struct thread *thread = get_ptrace_thread( process );
344 unsigned int first_offset, last_offset, len;
345 unsigned long data, *addr;
347 if (!thread) return 0;
349 if ((unsigned long)ptr != ptr)
351 set_error( STATUS_ACCESS_DENIED );
352 return 0;
355 first_offset = ptr % sizeof(long);
356 last_offset = (size + first_offset) % sizeof(long);
357 if (!last_offset) last_offset = sizeof(long);
359 addr = (unsigned long *)(unsigned long)(ptr - first_offset);
360 len = (size + first_offset + sizeof(long) - 1) / sizeof(long);
362 if (suspend_for_ptrace( thread ))
364 if (len > 3) /* /proc/pid/mem should be faster for large sizes */
366 char procmem[24];
367 int fd;
369 sprintf( procmem, "/proc/%u/mem", process->unix_pid );
370 if ((fd = open( procmem, O_RDONLY )) != -1)
372 ssize_t ret = pread( fd, dest, size, ptr );
373 close( fd );
374 if (ret == size)
376 len = 0;
377 goto done;
382 if (len > 1)
384 if (read_thread_long( thread, addr++, &data ) == -1) goto done;
385 memcpy( dest, (char *)&data + first_offset, sizeof(long) - first_offset );
386 dest += sizeof(long) - first_offset;
387 first_offset = 0;
388 len--;
391 while (len > 1)
393 if (read_thread_long( thread, addr++, &data ) == -1) goto done;
394 memcpy( dest, &data, sizeof(long) );
395 dest += sizeof(long);
396 len--;
399 if (read_thread_long( thread, addr++, &data ) == -1) goto done;
400 memcpy( dest, (char *)&data + first_offset, last_offset - first_offset );
401 len--;
403 done:
404 resume_after_ptrace( thread );
406 return !len;
409 /* make sure we can write to the whole address range */
410 /* len is the total size (in longs) */
411 static int check_process_write_access( struct thread *thread, long *addr, data_size_t len )
413 int page = get_page_size() / sizeof(long);
415 for (;;)
417 if (write_thread_long( thread, addr, 0, 0 ) == -1) return 0;
418 if (len <= page) break;
419 addr += page;
420 len -= page;
422 return (write_thread_long( thread, addr + len - 1, 0, 0 ) != -1);
425 /* write data to a process memory space */
426 int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
428 struct thread *thread = get_ptrace_thread( process );
429 int ret = 0;
430 long data = 0;
431 data_size_t len;
432 long *addr;
433 unsigned long first_mask, first_offset, last_mask, last_offset;
435 if (!thread) return 0;
437 if ((unsigned long)ptr != ptr)
439 set_error( STATUS_ACCESS_DENIED );
440 return 0;
443 /* compute the mask for the first long */
444 first_mask = ~0;
445 first_offset = ptr % sizeof(long);
446 memset( &first_mask, 0, first_offset );
448 /* compute the mask for the last long */
449 last_offset = (size + first_offset) % sizeof(long);
450 if (!last_offset) last_offset = sizeof(long);
451 last_mask = 0;
452 memset( &last_mask, 0xff, last_offset );
454 addr = (long *)(unsigned long)(ptr - first_offset);
455 len = (size + first_offset + sizeof(long) - 1) / sizeof(long);
457 if (suspend_for_ptrace( thread ))
459 if (!check_process_write_access( thread, addr, len ))
461 set_error( STATUS_ACCESS_DENIED );
462 goto done;
465 if (len > 3)
467 char procmem[24];
468 int fd;
470 sprintf( procmem, "/proc/%u/mem", process->unix_pid );
471 if ((fd = open( procmem, O_WRONLY )) != -1)
473 ssize_t r = pwrite( fd, src, size, ptr );
474 close( fd );
475 if (r == size)
477 ret = 1;
478 goto done;
483 /* first word is special */
484 if (len > 1)
486 memcpy( (char *)&data + first_offset, src, sizeof(long) - first_offset );
487 src += sizeof(long) - first_offset;
488 if (write_thread_long( thread, addr++, data, first_mask ) == -1) goto done;
489 first_offset = 0;
490 len--;
492 else last_mask &= first_mask;
494 while (len > 1)
496 memcpy( &data, src, sizeof(long) );
497 src += sizeof(long);
498 if (write_thread_long( thread, addr++, data, ~0ul ) == -1) goto done;
499 len--;
502 /* last word is special too */
503 memcpy( (char *)&data + first_offset, src, last_offset - first_offset );
504 if (write_thread_long( thread, addr, data, last_mask ) == -1) goto done;
505 ret = 1;
507 done:
508 resume_after_ptrace( thread );
510 return ret;
513 /* retrieve an LDT selector entry */
514 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
515 unsigned int *limit, unsigned char *flags )
517 if (!thread->process->ldt_copy)
519 set_error( STATUS_ACCESS_DENIED );
520 return;
522 if (entry >= 8192)
524 set_error( STATUS_ACCESS_VIOLATION );
525 return;
527 if (suspend_for_ptrace( thread ))
529 unsigned int flags_buf;
530 unsigned long addr = (unsigned long)thread->process->ldt_copy + (entry * 4);
532 if (read_thread_int( thread, (void *)addr, base ) == -1) goto done;
533 if (read_thread_int( thread, (void *)(addr + (8192 * 4)), limit ) == -1) goto done;
534 addr = (unsigned long)thread->process->ldt_copy + (2 * 8192 * 4) + (entry & ~3);
535 if (read_thread_int( thread, (void *)addr, &flags_buf ) == -1) goto done;
536 *flags = flags_buf >> (entry & 3) * 8;
537 done:
538 resume_after_ptrace( thread );
543 #if defined(linux) && (defined(HAVE_SYS_USER_H) || defined(HAVE_ASM_USER_H)) \
544 && (defined(__i386__) || defined(__x86_64__))
546 #ifdef HAVE_SYS_USER_H
547 #include <sys/user.h>
548 #elif defined(HAVE_ASM_USER_H)
549 #include <asm/user.h>
550 #endif
552 /* debug register offset in struct user */
553 #define DR_OFFSET(dr) ((((struct user *)0)->u_debugreg) + (dr))
555 /* initialize registers in new thread if necessary */
556 void init_thread_context( struct thread *thread )
558 /* Linux doesn't clear all registers, but hopefully enough to avoid spurious breakpoints */
559 thread->system_regs = 0;
562 /* retrieve the thread x86 registers */
563 void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
565 int i, pid = get_ptrace_tid(thread);
566 long data[8];
568 /* all other regs are handled on the client side */
569 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
571 if (!(thread->system_regs & SERVER_CTX_DEBUG_REGISTERS))
573 /* caller has initialized everything to 0 already, just return */
574 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
575 return;
578 if (!suspend_for_ptrace( thread )) return;
580 for (i = 0; i < 8; i++)
582 if (i == 4 || i == 5) continue;
583 errno = 0;
584 data[i] = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(i), 0 );
585 if ((data[i] == -1) && errno)
587 file_set_error();
588 goto done;
591 switch (context->machine)
593 case IMAGE_FILE_MACHINE_I386:
594 context->debug.i386_regs.dr0 = data[0];
595 context->debug.i386_regs.dr1 = data[1];
596 context->debug.i386_regs.dr2 = data[2];
597 context->debug.i386_regs.dr3 = data[3];
598 context->debug.i386_regs.dr6 = data[6];
599 context->debug.i386_regs.dr7 = data[7];
600 break;
601 case IMAGE_FILE_MACHINE_AMD64:
602 context->debug.x86_64_regs.dr0 = data[0];
603 context->debug.x86_64_regs.dr1 = data[1];
604 context->debug.x86_64_regs.dr2 = data[2];
605 context->debug.x86_64_regs.dr3 = data[3];
606 context->debug.x86_64_regs.dr6 = data[6];
607 context->debug.x86_64_regs.dr7 = data[7];
608 break;
609 default:
610 set_error( STATUS_INVALID_PARAMETER );
611 goto done;
613 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
614 done:
615 resume_after_ptrace( thread );
618 /* set the thread x86 registers */
619 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
621 int pid = get_ptrace_tid( thread );
623 /* all other regs are handled on the client side */
624 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
626 if (!suspend_for_ptrace( thread )) return;
628 switch (context->machine)
630 case IMAGE_FILE_MACHINE_I386:
631 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), 0 ) == -1) goto error;
632 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->debug.i386_regs.dr0 ) == -1) goto error;
633 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->debug.i386_regs.dr1 ) == -1) goto error;
634 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->debug.i386_regs.dr2 ) == -1) goto error;
635 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->debug.i386_regs.dr3 ) == -1) goto error;
636 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->debug.i386_regs.dr6 ) == -1) goto error;
637 /* Linux 2.6.33+ needs enable bits set briefly to update value returned by PEEKUSER later */
638 ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->debug.i386_regs.dr7 | 0x55 );
639 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->debug.i386_regs.dr7 ) == -1) goto error;
640 thread->system_regs |= SERVER_CTX_DEBUG_REGISTERS;
641 break;
642 case IMAGE_FILE_MACHINE_AMD64:
643 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), 0 ) == -1) goto error;
644 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->debug.x86_64_regs.dr0 ) == -1) goto error;
645 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->debug.x86_64_regs.dr1 ) == -1) goto error;
646 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->debug.x86_64_regs.dr2 ) == -1) goto error;
647 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->debug.x86_64_regs.dr3 ) == -1) goto error;
648 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->debug.x86_64_regs.dr6 ) == -1) goto error;
649 ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->debug.x86_64_regs.dr7 | 0x55 );
650 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->debug.x86_64_regs.dr7 ) == -1) goto error;
651 thread->system_regs |= SERVER_CTX_DEBUG_REGISTERS;
652 break;
653 default:
654 set_error( STATUS_INVALID_PARAMETER );
656 resume_after_ptrace( thread );
657 return;
658 error:
659 file_set_error();
660 resume_after_ptrace( thread );
663 #elif defined(__i386__) && defined(PTRACE_GETDBREGS) && defined(PTRACE_SETDBREGS) && \
664 (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__))
666 #include <machine/reg.h>
668 /* initialize registers in new thread if necessary */
669 void init_thread_context( struct thread *thread )
671 if (!(thread->system_regs & SERVER_CTX_DEBUG_REGISTERS)) return;
673 /* FreeBSD doesn't clear the debug registers in new threads */
674 if (suspend_for_ptrace( thread ))
676 struct dbreg dbregs;
678 memset( &dbregs, 0, sizeof(dbregs) );
679 ptrace( PTRACE_SETDBREGS, get_ptrace_tid( thread ), (caddr_t)&dbregs, 0 );
680 resume_after_ptrace( thread );
682 thread->system_regs = 0;
685 /* retrieve the thread x86 registers */
686 void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
688 int pid = get_ptrace_tid(thread);
689 struct dbreg dbregs;
691 /* all other regs are handled on the client side */
692 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
694 if (!suspend_for_ptrace( thread )) return;
696 if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) file_set_error();
697 else
699 #ifdef DBREG_DRX
700 /* needed for FreeBSD, the structure fields have changed under 5.x */
701 context->debug.i386_regs.dr0 = DBREG_DRX((&dbregs), 0);
702 context->debug.i386_regs.dr1 = DBREG_DRX((&dbregs), 1);
703 context->debug.i386_regs.dr2 = DBREG_DRX((&dbregs), 2);
704 context->debug.i386_regs.dr3 = DBREG_DRX((&dbregs), 3);
705 context->debug.i386_regs.dr6 = DBREG_DRX((&dbregs), 6);
706 context->debug.i386_regs.dr7 = DBREG_DRX((&dbregs), 7);
707 #elif defined(__NetBSD__)
708 context->debug.i386_regs.dr0 = dbregs.dr[0];
709 context->debug.i386_regs.dr1 = dbregs.dr[1];
710 context->debug.i386_regs.dr2 = dbregs.dr[2];
711 context->debug.i386_regs.dr3 = dbregs.dr[3];
712 context->debug.i386_regs.dr6 = dbregs.dr[6];
713 context->debug.i386_regs.dr7 = dbregs.dr[7];
714 #else
715 context->debug.i386_regs.dr0 = dbregs.dr0;
716 context->debug.i386_regs.dr1 = dbregs.dr1;
717 context->debug.i386_regs.dr2 = dbregs.dr2;
718 context->debug.i386_regs.dr3 = dbregs.dr3;
719 context->debug.i386_regs.dr6 = dbregs.dr6;
720 context->debug.i386_regs.dr7 = dbregs.dr7;
721 #endif
722 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
724 resume_after_ptrace( thread );
727 /* set the thread x86 registers */
728 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
730 int pid = get_ptrace_tid(thread);
731 struct dbreg dbregs;
733 /* all other regs are handled on the client side */
734 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
736 if (!suspend_for_ptrace( thread )) return;
738 #ifdef DBREG_DRX
739 /* needed for FreeBSD, the structure fields have changed under 5.x */
740 DBREG_DRX((&dbregs), 0) = context->debug.i386_regs.dr0;
741 DBREG_DRX((&dbregs), 1) = context->debug.i386_regs.dr1;
742 DBREG_DRX((&dbregs), 2) = context->debug.i386_regs.dr2;
743 DBREG_DRX((&dbregs), 3) = context->debug.i386_regs.dr3;
744 DBREG_DRX((&dbregs), 4) = 0;
745 DBREG_DRX((&dbregs), 5) = 0;
746 DBREG_DRX((&dbregs), 6) = context->debug.i386_regs.dr6;
747 DBREG_DRX((&dbregs), 7) = context->debug.i386_regs.dr7;
748 #elif defined(__NetBSD__)
749 dbregs.dr[0] = context->debug.i386_regs.dr0;
750 dbregs.dr[1] = context->debug.i386_regs.dr1;
751 dbregs.dr[2] = context->debug.i386_regs.dr2;
752 dbregs.dr[3] = context->debug.i386_regs.dr3;
753 dbregs.dr[4] = 0;
754 dbregs.dr[5] = 0;
755 dbregs.dr[6] = context->debug.i386_regs.dr6;
756 dbregs.dr[7] = context->debug.i386_regs.dr7;
757 #else
758 dbregs.dr0 = context->debug.i386_regs.dr0;
759 dbregs.dr1 = context->debug.i386_regs.dr1;
760 dbregs.dr2 = context->debug.i386_regs.dr2;
761 dbregs.dr3 = context->debug.i386_regs.dr3;
762 dbregs.dr4 = 0;
763 dbregs.dr5 = 0;
764 dbregs.dr6 = context->debug.i386_regs.dr6;
765 dbregs.dr7 = context->debug.i386_regs.dr7;
766 #endif
767 if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t)&dbregs, 0 ) != -1)
769 thread->system_regs |= SERVER_CTX_DEBUG_REGISTERS;
771 else file_set_error();
773 resume_after_ptrace( thread );
776 #else /* linux || __FreeBSD__ */
778 /* initialize registers in new thread if necessary */
779 void init_thread_context( struct thread *thread )
783 /* retrieve the thread x86 registers */
784 void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
788 /* set the thread x86 debug registers */
789 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
793 #endif /* linux || __FreeBSD__ */
795 #endif /* USE_PTRACE */