server: Added data_size_t type to represent sizes in the server protocol.
[wine] / server / ptrace.c
1 /*
2  * Server-side ptrace support
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
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>
39
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "winternl.h"
43
44 #include "file.h"
45 #include "process.h"
46 #include "thread.h"
47
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
72
73 #ifdef PT_GETDBREGS
74 #define PTRACE_GETDBREGS PT_GETDBREGS
75 #endif
76 #ifdef PT_SETDBREGS
77 #define PTRACE_SETDBREGS PT_SETDBREGS
78 #endif
79
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 */
89
90 /* handle a status returned by wait4 */
91 static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
92 {
93     if (WIFSTOPPED(status))
94     {
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)
99         {
100             /* ignore other signals for now */
101             ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
102         }
103         return sig;
104     }
105     if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
106     {
107         thread->unix_pid = -1;
108         thread->unix_tid = -1;
109         if (debug_level)
110         {
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) );
117         }
118     }
119     return 0;
120 }
121
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 )
124 {
125 #ifdef __WALL
126     static int wall_flag = __WALL;
127
128     for (;;)
129     {
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;
133     }
134 #else
135     return wait4( pid, status, options, usage );
136 #endif
137 }
138
139 /* handle a SIGCHLD signal */
140 void sigchld_callback(void)
141 {
142     int pid, status;
143
144     for (;;)
145     {
146         if (!(pid = wait4_wrapper( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
147         if (pid != -1) handle_child_status( get_thread_from_pid(pid), pid, status, -1 );
148         else break;
149     }
150 }
151
152 /* return the Unix pid to use in ptrace calls for a given thread */
153 static int get_ptrace_pid( struct thread *thread )
154 {
155     if (thread->unix_tid != -1) return thread->unix_tid;
156     return thread->unix_pid;
157 }
158
159 /* wait for a ptraced child to get a certain signal */
160 static int wait4_thread( struct thread *thread, int signal )
161 {
162     int res, status;
163
164     start_watchdog();
165     for (;;)
166     {
167         if ((res = wait4_wrapper( get_ptrace_pid(thread), &status, WUNTRACED, NULL )) == -1)
168         {
169             if (errno == EINTR)
170             {
171                 if (!watchdog_triggered()) continue;
172                 if (debug_level) fprintf( stderr, "%04x: *watchdog* wait4 aborted\n", thread->id );
173             }
174             else if (errno == ECHILD)  /* must have died */
175             {
176                 thread->unix_pid = -1;
177                 thread->unix_tid = -1;
178             }
179             else perror( "wait4" );
180             stop_watchdog();
181             return 0;
182         }
183         res = handle_child_status( thread, res, status, signal );
184         if (!res || res == signal) break;
185     }
186     stop_watchdog();
187     return (thread->unix_pid != -1);
188 }
189
190 /* send a signal to a specific thread */
191 static inline int tkill( int tgid, int pid, int sig )
192 {
193     int ret = -ENOSYS;
194
195 #ifdef __linux__
196 # ifdef __i386__
197     __asm__( "pushl %%ebx\n\t"
198              "movl %2,%%ebx\n\t"
199              "int $0x80\n\t"
200              "popl %%ebx\n\t"
201              : "=a" (ret)
202              : "0" (270) /*SYS_tgkill*/, "r" (tgid), "c" (pid), "d" (sig) );
203     if (ret == -ENOSYS)
204         __asm__( "pushl %%ebx\n\t"
205                  "movl %2,%%ebx\n\t"
206                  "int $0x80\n\t"
207                  "popl %%ebx\n\t"
208                  : "=a" (ret)
209                  : "0" (238) /*SYS_tkill*/, "r" (pid), "c" (sig) );
210 # elif defined(__x86_64__)
211     __asm__( "syscall" : "=a" (ret)
212              : "0" (200) /*SYS_tkill*/, "D" (pid), "S" (sig) );
213 # endif
214 #endif  /* __linux__ */
215
216     if (ret >= 0) return ret;
217     errno = -ret;
218     return -1;
219 }
220
221 /* send a Unix signal to a specific thread */
222 int send_thread_signal( struct thread *thread, int sig )
223 {
224     int ret = -1;
225
226     if (thread->unix_pid != -1)
227     {
228         if (thread->unix_tid != -1)
229         {
230             ret = tkill( thread->unix_pid, thread->unix_tid, sig );
231             if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
232         }
233         else ret = kill( thread->unix_pid, sig );
234
235         if (ret == -1 && errno == ESRCH) /* thread got killed */
236         {
237             thread->unix_pid = -1;
238             thread->unix_tid = -1;
239         }
240     }
241     return (ret != -1);
242 }
243
244 /* resume a thread after we have used ptrace on it */
245 static void resume_after_ptrace( struct thread *thread )
246 {
247     if (thread->unix_pid == -1) return;
248     if (ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, 0 ) == -1)
249     {
250         if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1;  /* thread got killed */
251     }
252 }
253
254 /* suspend a thread to allow using ptrace on it */
255 /* you must do a resume_after_ptrace when finished with the thread */
256 static int suspend_for_ptrace( struct thread *thread )
257 {
258     /* can't stop a thread while initialisation is in progress */
259     if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
260
261     /* this may fail if the client is already being debugged */
262     if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
263     {
264         if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1;  /* thread got killed */
265         goto error;
266     }
267     if (wait4_thread( thread, SIGSTOP )) return 1;
268     resume_after_ptrace( thread );
269  error:
270     set_error( STATUS_ACCESS_DENIED );
271     return 0;
272 }
273
274 /* read an int from a thread address space */
275 static int read_thread_int( struct thread *thread, const int *addr, int *data )
276 {
277     errno = 0;
278     *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
279     if ( *data == -1 && errno)
280     {
281         file_set_error();
282         return -1;
283     }
284     return 0;
285 }
286
287 /* write an int to a thread address space */
288 static int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
289 {
290     int res;
291     if (mask != ~0)
292     {
293         if (read_thread_int( thread, addr, &res ) == -1) return -1;
294         data = (data & mask) | (res & ~mask);
295     }
296     if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
297         file_set_error();
298     return res;
299 }
300
301 /* return a thread of the process suitable for ptracing */
302 static struct thread *get_ptrace_thread( struct process *process )
303 {
304     struct thread *thread;
305
306     LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
307     {
308         if (thread->unix_pid != -1) return thread;
309     }
310     set_error( STATUS_ACCESS_DENIED );  /* process is dead */
311     return NULL;
312 }
313
314 /* read data from a process memory space */
315 int read_process_memory( struct process *process, const void *ptr, data_size_t size, char *dest )
316 {
317     struct thread *thread = get_ptrace_thread( process );
318     unsigned int first_offset, last_offset, len;
319     int data, *addr;
320
321     if (!thread) return 0;
322
323     first_offset = (unsigned long)ptr % sizeof(int);
324     last_offset = (size + first_offset) % sizeof(int);
325     if (!last_offset) last_offset = sizeof(int);
326
327     addr = (int *)((char *)ptr - first_offset);
328     len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
329
330     if (suspend_for_ptrace( thread ))
331     {
332         if (len > 1)
333         {
334             if (read_thread_int( thread, addr++, &data ) == -1) goto done;
335             memcpy( dest, (char *)&data + first_offset, sizeof(int) - first_offset );
336             dest += sizeof(int) - first_offset;
337             first_offset = 0;
338             len--;
339         }
340
341         while (len > 1)
342         {
343             if (read_thread_int( thread, addr++, &data ) == -1) goto done;
344             memcpy( dest, &data, sizeof(int) );
345             dest += sizeof(int);
346             len--;
347         }
348
349         if (read_thread_int( thread, addr++, &data ) == -1) goto done;
350         memcpy( dest, (char *)&data + first_offset, last_offset - first_offset );
351         len--;
352
353     done:
354         resume_after_ptrace( thread );
355     }
356     return !len;
357 }
358
359 /* make sure we can write to the whole address range */
360 /* len is the total size (in ints) */
361 static int check_process_write_access( struct thread *thread, int *addr, data_size_t len )
362 {
363     int page = get_page_size() / sizeof(int);
364
365     for (;;)
366     {
367         if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
368         if (len <= page) break;
369         addr += page;
370         len -= page;
371     }
372     return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
373 }
374
375 /* write data to a process memory space */
376 int write_process_memory( struct process *process, void *ptr, data_size_t size, const char *src )
377 {
378     struct thread *thread = get_ptrace_thread( process );
379     int ret = 0, data = 0;
380     data_size_t len;
381     int *addr;
382     unsigned int first_mask, first_offset, last_mask, last_offset;
383
384     if (!thread) return 0;
385
386     /* compute the mask for the first int */
387     first_mask = ~0;
388     first_offset = (unsigned long)ptr % sizeof(int);
389     memset( &first_mask, 0, first_offset );
390
391     /* compute the mask for the last int */
392     last_offset = (size + first_offset) % sizeof(int);
393     if (!last_offset) last_offset = sizeof(int);
394     last_mask = 0;
395     memset( &last_mask, 0xff, last_offset );
396
397     addr = (int *)((char *)ptr - first_offset);
398     len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
399
400     if (suspend_for_ptrace( thread ))
401     {
402         if (!check_process_write_access( thread, addr, len ))
403         {
404             set_error( STATUS_ACCESS_DENIED );
405             goto done;
406         }
407         /* first word is special */
408         if (len > 1)
409         {
410             memcpy( (char *)&data + first_offset, src, sizeof(int) - first_offset );
411             src += sizeof(int) - first_offset;
412             if (write_thread_int( thread, addr++, data, first_mask ) == -1) goto done;
413             first_offset = 0;
414             len--;
415         }
416         else last_mask &= first_mask;
417
418         while (len > 1)
419         {
420             memcpy( &data, src, sizeof(int) );
421             src += sizeof(int);
422             if (write_thread_int( thread, addr++, data, ~0 ) == -1) goto done;
423             len--;
424         }
425
426         /* last word is special too */
427         memcpy( (char *)&data + first_offset, src, last_offset - first_offset );
428         if (write_thread_int( thread, addr, data, last_mask ) == -1) goto done;
429         ret = 1;
430
431     done:
432         resume_after_ptrace( thread );
433     }
434     return ret;
435 }
436
437 /* retrieve an LDT selector entry */
438 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
439                          unsigned int *limit, unsigned char *flags )
440 {
441     if (!thread->process->ldt_copy)
442     {
443         set_error( STATUS_ACCESS_DENIED );
444         return;
445     }
446     if (entry >= 8192)
447     {
448         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
449         return;
450     }
451     if (suspend_for_ptrace( thread ))
452     {
453         unsigned char flags_buf[4];
454         int *addr = (int *)thread->process->ldt_copy + entry;
455         if (read_thread_int( thread, addr, (int *)base ) == -1) goto done;
456         if (read_thread_int( thread, addr + 8192, (int *)limit ) == -1) goto done;
457         addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
458         if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
459         *flags = flags_buf[entry & 3];
460     done:
461         resume_after_ptrace( thread );
462     }
463 }
464
465
466 #if defined(linux) && (defined(__i386__) || defined(__x86_64__))
467
468 #ifdef HAVE_SYS_USER_H
469 # include <sys/user.h>
470 #endif
471
472 /* debug register offset in struct user */
473 #define DR_OFFSET(dr) ((((struct user *)0)->u_debugreg) + (dr))
474
475 /* retrieve the thread x86 registers */
476 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
477 {
478     int i, pid = get_ptrace_pid(thread);
479     long data[8];
480
481     /* all other regs are handled on the client side */
482     assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
483
484     if (!suspend_for_ptrace( thread )) return;
485
486     for (i = 0; i < 8; i++)
487     {
488         if (i == 4 || i == 5) continue;
489         errno = 0;
490         data[i] = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(i), 0 );
491         if ((data[i] == -1) && errno)
492         {
493             file_set_error();
494             goto done;
495         }
496     }
497     context->Dr0 = data[0];
498     context->Dr1 = data[1];
499     context->Dr2 = data[2];
500     context->Dr3 = data[3];
501     context->Dr6 = data[6];
502     context->Dr7 = data[7];
503     context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
504 done:
505     resume_after_ptrace( thread );
506 }
507
508 /* set the thread x86 registers */
509 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
510 {
511     int pid = get_ptrace_pid( thread );
512
513     /* all other regs are handled on the client side */
514     assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
515
516     if (!suspend_for_ptrace( thread )) return;
517
518     if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
519     if (thread->context) thread->context->Dr0 = context->Dr0;
520     if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
521     if (thread->context) thread->context->Dr1 = context->Dr1;
522     if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
523     if (thread->context) thread->context->Dr2 = context->Dr2;
524     if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
525     if (thread->context) thread->context->Dr3 = context->Dr3;
526     if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
527     if (thread->context) thread->context->Dr6 = context->Dr6;
528     if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
529     if (thread->context) thread->context->Dr7 = context->Dr7;
530     resume_after_ptrace( thread );
531     return;
532  error:
533     file_set_error();
534     resume_after_ptrace( thread );
535 }
536
537 #elif defined(__i386__) && defined(PTRACE_GETDBREGS) && defined(PTRACE_SETDBREGS) && \
538     (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__))
539
540 #include <machine/reg.h>
541
542 /* retrieve the thread x86 registers */
543 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
544 {
545     int pid = get_ptrace_pid(thread);
546     struct dbreg dbregs;
547
548     /* all other regs are handled on the client side */
549     assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
550
551     if (!suspend_for_ptrace( thread )) return;
552
553     if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) file_set_error();
554     else
555     {
556 #ifdef DBREG_DRX
557         /* needed for FreeBSD, the structure fields have changed under 5.x */
558         context->Dr0 = DBREG_DRX((&dbregs), 0);
559         context->Dr1 = DBREG_DRX((&dbregs), 1);
560         context->Dr2 = DBREG_DRX((&dbregs), 2);
561         context->Dr3 = DBREG_DRX((&dbregs), 3);
562         context->Dr6 = DBREG_DRX((&dbregs), 6);
563         context->Dr7 = DBREG_DRX((&dbregs), 7);
564 #else
565         context->Dr0 = dbregs.dr0;
566         context->Dr1 = dbregs.dr1;
567         context->Dr2 = dbregs.dr2;
568         context->Dr3 = dbregs.dr3;
569         context->Dr6 = dbregs.dr6;
570         context->Dr7 = dbregs.dr7;
571 #endif
572         context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
573     }
574     resume_after_ptrace( thread );
575 }
576
577 /* set the thread x86 registers */
578 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
579 {
580     int pid = get_ptrace_pid(thread);
581     struct dbreg dbregs;
582
583     /* all other regs are handled on the client side */
584     assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
585
586     if (!suspend_for_ptrace( thread )) return;
587
588 #ifdef DBREG_DRX
589     /* needed for FreeBSD, the structure fields have changed under 5.x */
590     DBREG_DRX((&dbregs), 0) = context->Dr0;
591     DBREG_DRX((&dbregs), 1) = context->Dr1;
592     DBREG_DRX((&dbregs), 2) = context->Dr2;
593     DBREG_DRX((&dbregs), 3) = context->Dr3;
594     DBREG_DRX((&dbregs), 4) = 0;
595     DBREG_DRX((&dbregs), 5) = 0;
596     DBREG_DRX((&dbregs), 6) = context->Dr6;
597     DBREG_DRX((&dbregs), 7) = context->Dr7;
598 #else
599     dbregs.dr0 = context->Dr0;
600     dbregs.dr1 = context->Dr1;
601     dbregs.dr2 = context->Dr2;
602     dbregs.dr3 = context->Dr3;
603     dbregs.dr4 = 0;
604     dbregs.dr5 = 0;
605     dbregs.dr6 = context->Dr6;
606     dbregs.dr7 = context->Dr7;
607 #endif
608     if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) file_set_error();
609     else if (thread->context)  /* update the cached values */
610     {
611         thread->context->Dr0 = context->Dr0;
612         thread->context->Dr1 = context->Dr1;
613         thread->context->Dr2 = context->Dr2;
614         thread->context->Dr3 = context->Dr3;
615         thread->context->Dr6 = context->Dr6;
616         thread->context->Dr7 = context->Dr7;
617     }
618     resume_after_ptrace( thread );
619 }
620
621 #else  /* linux || __FreeBSD__ */
622
623 /* retrieve the thread x86 registers */
624 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
625 {
626 }
627
628 /* set the thread x86 debug registers */
629 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
630 {
631 }
632
633 #endif  /* linux || __FreeBSD__ */