Complete ICatInformation implementation.
[wine.git] / server / ptrace.c
blob7aa0c88a955d4a849da334b321d7dd8d3ca99126
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <assert.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <signal.h>
27 #include <sys/types.h>
28 #ifdef HAVE_SYS_PTRACE_H
29 # include <sys/ptrace.h>
30 #endif
31 #ifdef HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
33 #endif
34 #include <unistd.h>
36 #include "process.h"
37 #include "thread.h"
40 #ifndef PTRACE_CONT
41 #define PTRACE_CONT PT_CONTINUE
42 #endif
43 #ifndef PTRACE_SINGLESTEP
44 #define PTRACE_SINGLESTEP PT_STEP
45 #endif
46 #ifndef PTRACE_ATTACH
47 #define PTRACE_ATTACH PT_ATTACH
48 #endif
49 #ifndef PTRACE_DETACH
50 #define PTRACE_DETACH PT_DETACH
51 #endif
52 #ifndef PTRACE_PEEKDATA
53 #define PTRACE_PEEKDATA PT_READ_D
54 #endif
55 #ifndef PTRACE_POKEDATA
56 #define PTRACE_POKEDATA PT_WRITE_D
57 #endif
59 #ifndef HAVE_SYS_PTRACE_H
60 #define PT_CONTINUE 0
61 #define PT_ATTACH 1
62 #define PT_DETACH 2
63 #define PT_READ_D 3
64 #define PT_WRITE_D 4
65 #define PT_STEP 5
66 inline static int ptrace(int req, ...) { errno = EPERM; return -1; /*FAIL*/ }
67 #endif /* HAVE_SYS_PTRACE_H */
69 static const int use_ptrace = 1; /* set to 0 to disable ptrace */
71 /* handle a status returned by wait4 */
72 static int handle_child_status( struct thread *thread, int pid, int status )
74 if (WIFSTOPPED(status))
76 int sig = WSTOPSIG(status);
77 if (debug_level && thread)
78 fprintf( stderr, "%08x: *signal* signal=%d\n", (unsigned int)thread, sig );
79 switch(sig)
81 case SIGSTOP: /* continue at once if not suspended */
82 if (thread && (thread->process->suspend + thread->suspend)) break;
83 /* fall through */
84 default: /* ignore other signals for now */
85 if (thread && get_thread_single_step( thread ))
86 ptrace( PTRACE_SINGLESTEP, pid, (caddr_t)1, sig );
87 else
88 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
89 break;
91 return sig;
93 if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
95 thread->attached = 0;
96 thread->unix_pid = 0;
97 if (debug_level)
99 if (WIFSIGNALED(status))
100 fprintf( stderr, "%08x: *exited* signal=%d\n",
101 (unsigned int)thread, WTERMSIG(status) );
102 else
103 fprintf( stderr, "%08x: *exited* status=%d\n",
104 (unsigned int)thread, WEXITSTATUS(status) );
107 return 0;
110 /* handle a SIGCHLD signal */
111 void sigchld_handler()
113 int pid, status;
115 for (;;)
117 if (!(pid = wait4( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
118 if (pid != -1) handle_child_status( get_thread_from_pid(pid), pid, status );
119 else break;
123 /* wait for a ptraced child to get a certain signal */
124 void wait4_thread( struct thread *thread, int signal )
126 int res, status;
130 if ((res = wait4( thread->unix_pid, &status, WUNTRACED, NULL )) == -1)
132 perror( "wait4" );
133 return;
135 res = handle_child_status( thread, res, status );
136 } while (res && res != signal);
139 /* attach to a Unix thread */
140 static int attach_thread( struct thread *thread )
142 /* this may fail if the client is already being debugged */
143 if (!use_ptrace) return 0;
144 if (ptrace( PTRACE_ATTACH, thread->unix_pid, 0, 0 ) == -1)
146 if (errno == ESRCH) thread->unix_pid = 0; /* process got killed */
147 return 0;
149 if (debug_level) fprintf( stderr, "%08x: *attached*\n", (unsigned int)thread );
150 thread->attached = 1;
151 wait4_thread( thread, SIGSTOP );
152 return 1;
155 /* detach from a Unix thread and kill it */
156 void detach_thread( struct thread *thread, int sig )
158 if (!thread->unix_pid) return;
159 if (thread->attached)
161 /* make sure it is stopped */
162 suspend_thread( thread, 0 );
163 if (sig) kill( thread->unix_pid, sig );
164 if (debug_level) fprintf( stderr, "%08x: *detached*\n", (unsigned int)thread );
165 ptrace( PTRACE_DETACH, thread->unix_pid, (caddr_t)1, sig );
166 thread->suspend = 0; /* detach makes it continue */
167 thread->attached = 0;
169 else
171 if (sig) kill( thread->unix_pid, sig );
172 if (thread->suspend + thread->process->suspend) continue_thread( thread );
176 /* stop a thread (at the Unix level) */
177 void stop_thread( struct thread *thread )
179 /* can't stop a thread while initialisation is in progress */
180 if (!thread->unix_pid || !is_process_init_done(thread->process)) return;
181 /* first try to attach to it */
182 if (!thread->attached)
183 if (attach_thread( thread )) return; /* this will have stopped it */
184 /* attached already, or attach failed -> send a signal */
185 if (!thread->unix_pid) return;
186 kill( thread->unix_pid, SIGSTOP );
187 if (thread->attached) wait4_thread( thread, SIGSTOP );
190 /* make a thread continue (at the Unix level) */
191 void continue_thread( struct thread *thread )
193 if (!thread->unix_pid) return;
194 if (!thread->attached) kill( thread->unix_pid, SIGCONT );
195 else ptrace( get_thread_single_step(thread) ? PTRACE_SINGLESTEP : PTRACE_CONT,
196 thread->unix_pid, (caddr_t)1, SIGSTOP );
199 /* suspend a thread to allow using ptrace on it */
200 /* you must do a resume_thread when finished with the thread */
201 int suspend_for_ptrace( struct thread *thread )
203 if (thread->attached)
205 suspend_thread( thread, 0 );
206 return 1;
208 /* can't stop a thread while initialisation is in progress */
209 if (!thread->unix_pid || !is_process_init_done(thread->process)) goto error;
210 thread->suspend++;
211 if (attach_thread( thread )) return 1;
212 thread->suspend--;
213 error:
214 set_error( STATUS_ACCESS_DENIED );
215 return 0;
218 /* read an int from a thread address space */
219 int read_thread_int( struct thread *thread, const int *addr, int *data )
221 *data = ptrace( PTRACE_PEEKDATA, thread->unix_pid, (caddr_t)addr, 0 );
222 if ( *data == -1 && errno)
224 file_set_error();
225 return -1;
227 return 0;
230 /* write an int to a thread address space */
231 int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
233 int res;
234 if (mask != ~0)
236 if (read_thread_int( thread, addr, &res ) == -1) return -1;
237 data = (data & mask) | (res & ~mask);
239 if ((res = ptrace( PTRACE_POKEDATA, thread->unix_pid, (caddr_t)addr, data )) == -1)
240 file_set_error();
241 return res;