Added an unknown VxD error code.
[wine] / server / ptrace.c
1 /*
2  * Server-side ptrace support
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_PTRACE_H
15 # include <sys/ptrace.h>
16 #endif
17 #ifdef HAVE_SYS_WAIT_H
18 # include <sys/wait.h>
19 #endif
20 #include <unistd.h>
21
22 #include "process.h"
23 #include "thread.h"
24
25
26 #ifndef PTRACE_CONT
27 #define PTRACE_CONT PT_CONTINUE
28 #endif
29 #ifndef PTRACE_SINGLESTEP
30 #define PTRACE_SINGLESTEP PT_STEP
31 #endif
32 #ifndef PTRACE_ATTACH
33 #define PTRACE_ATTACH PT_ATTACH
34 #endif
35 #ifndef PTRACE_DETACH
36 #define PTRACE_DETACH PT_DETACH
37 #endif
38 #ifndef PTRACE_PEEKDATA
39 #define PTRACE_PEEKDATA PT_READ_D
40 #endif
41 #ifndef PTRACE_POKEDATA
42 #define PTRACE_POKEDATA PT_WRITE_D
43 #endif
44
45 #ifdef HAVE_SYS_PTRACE_H
46 static const int use_ptrace = 1;  /* set to 0 to disable ptrace */
47 #else
48 static const int use_ptrace = 0;
49
50 #define PT_CONTINUE 0
51 #define PT_ATTACH   1
52 #define PT_DETACH   2
53 #define PT_READ_D   3
54 #define PT_WRITE_D  4
55 #define PT_STEP     5
56
57 static int ptrace(int req, ...) { return -1; /*FAIL*/ }
58 #endif
59
60 /* handle a status returned by wait4 */
61 static int handle_child_status( struct thread *thread, int pid, int status )
62 {
63     if (WIFSTOPPED(status))
64     {
65         int sig = WSTOPSIG(status);
66         if (debug_level && thread)
67             fprintf( stderr, "%08x: *signal* signal=%d\n", (unsigned int)thread, sig );
68         switch(sig)
69         {
70         case SIGSTOP:  /* continue at once if not suspended */
71             if (thread && (thread->process->suspend + thread->suspend)) break;
72             /* fall through */
73         default:  /* ignore other signals for now */
74             if (thread && get_thread_single_step( thread ))
75                 ptrace( PTRACE_SINGLESTEP, pid, (caddr_t)1, sig );
76             else
77                 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
78             break;
79         }
80         return sig;
81     }
82     if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
83     {
84         thread->attached = 0;
85         thread->unix_pid = 0;
86         if (debug_level)
87         {
88             if (WIFSIGNALED(status))
89                 fprintf( stderr, "%08x: *exited* signal=%d\n",
90                          (unsigned int)thread, WTERMSIG(status) );
91             else
92                 fprintf( stderr, "%08x: *exited* status=%d\n",
93                          (unsigned int)thread, WEXITSTATUS(status) );
94         }
95     }
96     return 0;
97 }
98
99 /* handle a SIGCHLD signal */
100 void sigchld_handler()
101 {
102     int pid, status;
103
104     for (;;)
105     {
106         if (!(pid = wait4( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
107         if (pid != -1) handle_child_status( get_thread_from_pid(pid), pid, status );
108         else break;
109     }
110 }
111
112 /* wait for a ptraced child to get a certain signal */
113 void wait4_thread( struct thread *thread, int signal )
114 {
115     int res, status;
116
117     do
118     {
119         if ((res = wait4( thread->unix_pid, &status, WUNTRACED, NULL )) == -1)
120         {
121             perror( "wait4" );
122             return;
123         }
124         res = handle_child_status( thread, res, status );
125     } while (res && res != signal);
126 }
127
128 /* attach to a Unix thread */
129 static int attach_thread( struct thread *thread )
130 {
131     /* this may fail if the client is already being debugged */
132     if (!use_ptrace || (ptrace( PTRACE_ATTACH, thread->unix_pid, 0, 0 ) == -1)) return 0;
133     if (debug_level) fprintf( stderr, "%08x: *attached*\n", (unsigned int)thread );
134     thread->attached = 1;
135     wait4_thread( thread, SIGSTOP );
136     return 1;
137 }
138
139 /* detach from a Unix thread and kill it */
140 void detach_thread( struct thread *thread, int sig )
141 {
142     if (!thread->unix_pid) return;
143     if (thread->attached)
144     {
145         /* make sure it is stopped */
146         if (!(thread->suspend + thread->process->suspend)) stop_thread( thread );
147         if (sig) kill( thread->unix_pid, sig );
148         if (debug_level) fprintf( stderr, "%08x: *detached*\n", (unsigned int)thread );
149         ptrace( PTRACE_DETACH, thread->unix_pid, (caddr_t)1, sig );
150         thread->attached = 0;
151     }
152     else
153     {
154         if (sig) kill( thread->unix_pid, sig );
155         if (thread->suspend + thread->process->suspend) continue_thread( thread );
156     }
157 }
158
159 /* stop a thread (at the Unix level) */
160 void stop_thread( struct thread *thread )
161 {
162     /* can't stop a thread while initialisation is in progress */
163     if (!thread->unix_pid || thread->process->init_event) return;
164     /* first try to attach to it */
165     if (!thread->attached)
166         if (attach_thread( thread )) return;  /* this will have stopped it */
167     /* attached already, or attach failed -> send a signal */
168     kill( thread->unix_pid, SIGSTOP );
169     if (thread->attached) wait4_thread( thread, SIGSTOP );
170 }
171
172 /* make a thread continue (at the Unix level) */
173 void continue_thread( struct thread *thread )
174 {
175     if (!thread->unix_pid) return;
176     if (!thread->attached) kill( thread->unix_pid, SIGCONT );
177     else ptrace( get_thread_single_step(thread) ? PTRACE_SINGLESTEP : PTRACE_CONT,
178                  thread->unix_pid, (caddr_t)1, SIGSTOP );
179 }
180
181 /* suspend a thread to allow using ptrace on it */
182 /* you must do a resume_thread when finished with the thread */
183 int suspend_for_ptrace( struct thread *thread )
184 {
185     if (thread->attached)
186     {
187         suspend_thread( thread, 0 );
188         return 1;
189     }
190     /* can't stop a thread while initialisation is in progress */
191     if (!thread->unix_pid || thread->process->init_event) goto error;
192     thread->suspend++;
193     if (attach_thread( thread )) return 1;
194     thread->suspend--;
195  error:
196     set_error( STATUS_ACCESS_DENIED );
197     return 0;
198 }
199
200 /* read an int from a thread address space */
201 int read_thread_int( struct thread *thread, const int *addr, int *data )
202 {
203     *data = ptrace( PTRACE_PEEKDATA, thread->unix_pid, (caddr_t)addr, 0 );
204     if ( *data == -1 && errno)
205     {
206         file_set_error();
207         return -1;
208     }
209     return 0;
210 }
211
212 /* write an int to a thread address space */
213 int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
214 {
215     int res;
216     if (mask != ~0)
217     {
218         if (read_thread_int( thread, addr, &res ) == -1) return -1;
219         data = (data & mask) | (res & ~mask);
220     }
221     if ((res = ptrace( PTRACE_POKEDATA, thread->unix_pid, (caddr_t)addr, data )) == -1)
222         file_set_error();
223     return res;
224 }