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
27 #include <sys/types.h>
28 #ifdef HAVE_SYS_PTRACE_H
29 # include <sys/ptrace.h>
31 #ifdef HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
41 #define PTRACE_CONT PT_CONTINUE
43 #ifndef PTRACE_SINGLESTEP
44 #define PTRACE_SINGLESTEP PT_STEP
47 #define PTRACE_ATTACH PT_ATTACH
50 #define PTRACE_DETACH PT_DETACH
52 #ifndef PTRACE_PEEKDATA
53 #define PTRACE_PEEKDATA PT_READ_D
55 #ifndef PTRACE_POKEDATA
56 #define PTRACE_POKEDATA PT_WRITE_D
59 #ifndef HAVE_SYS_PTRACE_H
66 inline static int ptrace(int req
, ...) { errno
= EPERM
; return -1; /*FAIL*/ }
67 #endif /* HAVE_SYS_PTRACE_H */
73 static const int use_ptrace
= 1; /* set to 0 to disable ptrace */
75 /* handle a status returned by wait4 */
76 static int handle_child_status( struct thread
*thread
, int pid
, int status
, int want_sig
)
78 if (WIFSTOPPED(status
))
80 int sig
= WSTOPSIG(status
);
81 if (debug_level
&& thread
)
82 fprintf( stderr
, "%04x: *signal* signal=%d\n", thread
->id
, sig
);
85 /* ignore other signals for now */
86 if (thread
&& get_thread_single_step( thread
))
87 ptrace( PTRACE_SINGLESTEP
, pid
, (caddr_t
)1, sig
);
89 ptrace( PTRACE_CONT
, pid
, (caddr_t
)1, sig
);
93 if (thread
&& (WIFSIGNALED(status
) || WIFEXITED(status
)))
96 thread
->unix_pid
= -1;
97 thread
->unix_tid
= -1;
100 if (WIFSIGNALED(status
))
101 fprintf( stderr
, "%04x: *exited* signal=%d\n",
102 thread
->id
, WTERMSIG(status
) );
104 fprintf( stderr
, "%04x: *exited* status=%d\n",
105 thread
->id
, WEXITSTATUS(status
) );
111 /* handle a SIGCHLD signal */
112 void sigchld_callback(void)
118 if (!(pid
= wait4( -1, &status
, WUNTRACED
| WNOHANG
| __WALL
, NULL
))) break;
119 if (pid
!= -1) handle_child_status( get_thread_from_pid(pid
), pid
, status
, -1 );
124 /* wait for a ptraced child to get a certain signal */
125 static void wait4_thread( struct thread
*thread
, int signal
)
131 if ((res
= wait4( get_ptrace_pid(thread
), &status
, WUNTRACED
| __WALL
, NULL
)) == -1)
133 if (errno
== ECHILD
) /* must have died */
135 thread
->unix_pid
= -1;
136 thread
->unix_tid
= -1;
137 thread
->attached
= 0;
139 else perror( "wait4" );
142 res
= handle_child_status( thread
, res
, status
, signal
);
143 } while (res
&& res
!= signal
);
146 /* return the Unix pid to use in ptrace calls for a given thread */
147 int get_ptrace_pid( struct thread
*thread
)
149 if (thread
->unix_tid
!= -1) return thread
->unix_tid
;
150 return thread
->unix_pid
;
153 /* send a Unix signal to a specific thread */
154 int send_thread_signal( struct thread
*thread
, int sig
)
158 if (thread
->unix_pid
!= -1)
160 if (thread
->unix_tid
!= -1)
162 ret
= tkill( thread
->unix_tid
, sig
);
163 if (ret
== -1 && errno
== ENOSYS
) ret
= kill( thread
->unix_pid
, sig
);
165 else ret
= kill( thread
->unix_pid
, sig
);
167 if (ret
== -1 && errno
== ESRCH
) /* thread got killed */
169 thread
->unix_pid
= -1;
170 thread
->unix_tid
= -1;
171 thread
->attached
= 0;
177 /* attach to a Unix thread */
178 static int attach_thread( struct thread
*thread
)
180 /* this may fail if the client is already being debugged */
181 if (!use_ptrace
) return 0;
182 if (ptrace( PTRACE_ATTACH
, get_ptrace_pid(thread
), 0, 0 ) == -1)
184 if (errno
== ESRCH
) thread
->unix_pid
= thread
->unix_tid
= -1; /* thread got killed */
187 if (debug_level
) fprintf( stderr
, "%04x: *attached*\n", thread
->id
);
188 thread
->attached
= 1;
189 wait4_thread( thread
, SIGSTOP
);
193 /* detach from a Unix thread and kill it */
194 void detach_thread( struct thread
*thread
, int sig
)
196 if (thread
->unix_pid
== -1) return;
197 if (thread
->attached
)
199 /* make sure it is stopped */
200 suspend_for_ptrace( thread
);
201 if (sig
) send_thread_signal( thread
, sig
);
202 if (thread
->unix_pid
== -1) return;
203 if (debug_level
) fprintf( stderr
, "%04x: *detached*\n", thread
->id
);
204 ptrace( PTRACE_DETACH
, get_ptrace_pid(thread
), (caddr_t
)1, sig
);
205 thread
->attached
= 0;
207 else if (sig
) send_thread_signal( thread
, sig
);
210 /* attach to a Unix process with ptrace */
211 int attach_process( struct process
*process
)
213 struct thread
*thread
;
216 if (!process
->thread_list
) /* need at least one running thread */
218 set_error( STATUS_ACCESS_DENIED
);
221 for (thread
= process
->thread_list
; thread
; thread
= thread
->proc_next
)
223 if (thread
->attached
) continue;
224 if (suspend_for_ptrace( thread
)) resume_after_ptrace( thread
);
230 /* detach from a ptraced Unix process */
231 void detach_process( struct process
*process
)
233 struct thread
*thread
;
235 for (thread
= process
->thread_list
; thread
; thread
= thread
->proc_next
)
237 if (thread
->attached
) detach_thread( thread
, 0 );
241 /* suspend a thread to allow using ptrace on it */
242 /* you must do a resume_after_ptrace when finished with the thread */
243 int suspend_for_ptrace( struct thread
*thread
)
245 /* can't stop a thread while initialisation is in progress */
246 if (thread
->unix_pid
== -1 || !is_process_init_done(thread
->process
)) goto error
;
248 if (thread
->attached
)
250 send_thread_signal( thread
, SIGSTOP
);
251 wait4_thread( thread
, SIGSTOP
);
254 if (attach_thread( thread
)) return 1;
256 set_error( STATUS_ACCESS_DENIED
);
260 /* resume a thread after we have used ptrace on it */
261 void resume_after_ptrace( struct thread
*thread
)
263 if (thread
->unix_pid
== -1) return;
264 assert( thread
->attached
);
265 ptrace( get_thread_single_step(thread
) ? PTRACE_SINGLESTEP
: PTRACE_CONT
,
266 get_ptrace_pid(thread
), (caddr_t
)1, 0 /* cancel the SIGSTOP */ );
269 /* read an int from a thread address space */
270 int read_thread_int( struct thread
*thread
, const int *addr
, int *data
)
273 *data
= ptrace( PTRACE_PEEKDATA
, get_ptrace_pid(thread
), (caddr_t
)addr
, 0 );
274 if ( *data
== -1 && errno
)
282 /* write an int to a thread address space */
283 int write_thread_int( struct thread
*thread
, int *addr
, int data
, unsigned int mask
)
288 if (read_thread_int( thread
, addr
, &res
) == -1) return -1;
289 data
= (data
& mask
) | (res
& ~mask
);
291 if ((res
= ptrace( PTRACE_POKEDATA
, get_ptrace_pid(thread
), (caddr_t
)addr
, data
)) == -1)