RegSetValueExA/W: fixed REG_SZ string length handling for Win95.
[wine/multimedia.git] / server / ptrace.c
blobbf9f39f566f69aff53d990ecfc654ebe08e47170
1 /*
2 * Server-side ptrace support
4 * Copyright (C) 1999 Alexandre Julliard
5 */
7 #include "config.h"
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>
22 #include "process.h"
23 #include "thread.h"
26 #ifndef PTRACE_CONT
27 #define PTRACE_CONT PT_CONTINUE
28 #endif
29 #ifndef PTRACE_ATTACH
30 #define PTRACE_ATTACH PT_ATTACH
31 #endif
32 #ifndef PTRACE_DETACH
33 #define PTRACE_DETACH PT_DETACH
34 #endif
35 #ifndef PTRACE_PEEKDATA
36 #define PTRACE_PEEKDATA PT_READ_D
37 #endif
38 #ifndef PTRACE_POKEDATA
39 #define PTRACE_POKEDATA PT_WRITE_D
40 #endif
42 #ifdef HAVE_SYS_PTRACE_H
43 static const int use_ptrace = 1; /* set to 0 to disable ptrace */
44 #else
45 static const int use_ptrace = 0;
47 #define PT_CONTINUE 0
48 #define PT_ATTACH 1
49 #define PT_DETACH 2
50 #define PT_READ_D 3
51 #define PT_WRITE_D 4
53 static int ptrace(int req, ...) { return -1; /*FAIL*/ }
54 #endif
56 /* handle a status returned by wait4 */
57 static int handle_child_status( struct thread *thread, int pid, int status )
59 if (WIFSTOPPED(status))
61 int sig = WSTOPSIG(status);
62 if (debug_level && thread)
63 fprintf( stderr, "%08x: *signal* signal=%d\n", (unsigned int)thread, sig );
64 switch(sig)
66 case SIGSTOP: /* continue at once if not suspended */
67 if (!thread || !(thread->process->suspend + thread->suspend))
68 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
69 break;
70 default: /* ignore other signals for now */
71 ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
72 break;
74 return sig;
76 if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
78 thread->attached = 0;
79 thread->unix_pid = 0;
80 if (debug_level)
82 if (WIFSIGNALED(status))
83 fprintf( stderr, "%08x: *exited* signal=%d\n",
84 (unsigned int)thread, WTERMSIG(status) );
85 else
86 fprintf( stderr, "%08x: *exited* status=%d\n",
87 (unsigned int)thread, WEXITSTATUS(status) );
90 return 0;
93 /* handle a SIGCHLD signal */
94 void sigchld_handler()
96 int pid, status;
98 for (;;)
100 if (!(pid = wait4( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
101 if (pid != -1) handle_child_status( get_thread_from_pid(pid), pid, status );
102 else break;
106 /* wait for a ptraced child to get a certain signal */
107 void wait4_thread( struct thread *thread, int signal )
109 int res, status;
113 if ((res = wait4( thread->unix_pid, &status, WUNTRACED, NULL )) == -1)
115 perror( "wait4" );
116 return;
118 res = handle_child_status( thread, res, status );
119 } while (res && res != signal);
122 /* attach to a Unix thread */
123 static int attach_thread( struct thread *thread )
125 /* this may fail if the client is already being debugged */
126 if (!use_ptrace || (ptrace( PTRACE_ATTACH, thread->unix_pid, 0, 0 ) == -1)) return 0;
127 if (debug_level) fprintf( stderr, "%08x: *attached*\n", (unsigned int)thread );
128 thread->attached = 1;
129 wait4_thread( thread, SIGSTOP );
130 return 1;
133 /* detach from a Unix thread and kill it */
134 void detach_thread( struct thread *thread, int sig )
136 if (!thread->unix_pid) return;
137 if (thread->attached)
139 /* make sure it is stopped */
140 if (!(thread->suspend + thread->process->suspend)) stop_thread( thread );
141 if (sig) kill( thread->unix_pid, sig );
142 if (debug_level) fprintf( stderr, "%08x: *detached*\n", (unsigned int)thread );
143 ptrace( PTRACE_DETACH, thread->unix_pid, (caddr_t)1, sig );
144 thread->attached = 0;
146 else
148 if (sig) kill( thread->unix_pid, sig );
149 if (thread->suspend + thread->process->suspend) continue_thread( thread );
153 /* stop a thread (at the Unix level) */
154 void stop_thread( struct thread *thread )
156 /* can't stop a thread while initialisation is in progress */
157 if (!thread->unix_pid || thread->process->init_event) return;
158 /* first try to attach to it */
159 if (!thread->attached)
160 if (attach_thread( thread )) return; /* this will have stopped it */
161 /* attached already, or attach failed -> send a signal */
162 kill( thread->unix_pid, SIGSTOP );
163 if (thread->attached) wait4_thread( thread, SIGSTOP );
166 /* make a thread continue (at the Unix level) */
167 void continue_thread( struct thread *thread )
169 if (!thread->unix_pid) return;
170 if (!thread->attached) kill( thread->unix_pid, SIGCONT );
171 else ptrace( PTRACE_CONT, thread->unix_pid, (caddr_t)1, SIGSTOP );
174 /* suspend a thread to allow using ptrace on it */
175 /* you must do a resume_thread when finished with the thread */
176 int suspend_for_ptrace( struct thread *thread )
178 if (thread->attached)
180 suspend_thread( thread, 0 );
181 return 1;
183 /* can't stop a thread while initialisation is in progress */
184 if (!thread->unix_pid || thread->process->init_event) goto error;
185 thread->suspend++;
186 if (attach_thread( thread )) return 1;
187 thread->suspend--;
188 error:
189 set_error( STATUS_ACCESS_DENIED );
190 return 0;
193 /* read an int from a thread address space */
194 int read_thread_int( struct thread *thread, const int *addr, int *data )
196 *data = ptrace( PTRACE_PEEKDATA, thread->unix_pid, (caddr_t)addr, 0 );
197 if ( *data == -1 && errno)
199 file_set_error();
200 return -1;
202 return 0;
205 /* write an int to a thread address space */
206 int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
208 int res;
209 if (mask != ~0)
211 if (read_thread_int( thread, addr, &res ) == -1) return -1;
212 data = (data & mask) | (res & ~mask);
214 if ((res = ptrace( PTRACE_POKEDATA, thread->unix_pid, (caddr_t)addr, data )) == -1)
215 file_set_error();
216 return res;