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
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_PTRACE_H
30 # include <sys/ptrace.h>
32 #ifdef HAVE_SYS_WAIT_H
33 # include <sys/wait.h>
38 #define WIN32_NO_STATUS
46 #define PTRACE_CONT PT_CONTINUE
48 #ifndef PTRACE_SINGLESTEP
49 #define PTRACE_SINGLESTEP PT_STEP
52 #define PTRACE_ATTACH PT_ATTACH
55 #define PTRACE_DETACH PT_DETACH
57 #ifndef PTRACE_PEEKDATA
58 #define PTRACE_PEEKDATA PT_READ_D
60 #ifndef PTRACE_POKEDATA
61 #define PTRACE_POKEDATA PT_WRITE_D
64 #ifndef HAVE_SYS_PTRACE_H
71 inline static int ptrace(int req
, ...) { errno
= EPERM
; return -1; /*FAIL*/ }
72 #endif /* HAVE_SYS_PTRACE_H */
74 static const int use_ptrace
= 1; /* set to 0 to disable ptrace */
76 /* handle a status returned by wait4 */
77 static int handle_child_status( struct thread
*thread
, int pid
, int status
, int want_sig
)
79 if (WIFSTOPPED(status
))
81 int sig
= WSTOPSIG(status
);
82 if (debug_level
&& thread
)
83 fprintf( stderr
, "%04x: *signal* signal=%d\n", thread
->id
, sig
);
86 /* ignore other signals for now */
87 if (thread
&& get_thread_single_step( thread
))
88 ptrace( PTRACE_SINGLESTEP
, pid
, (caddr_t
)1, sig
);
90 ptrace( PTRACE_CONT
, pid
, (caddr_t
)1, sig
);
94 if (thread
&& (WIFSIGNALED(status
) || WIFEXITED(status
)))
97 thread
->unix_pid
= -1;
98 thread
->unix_tid
= -1;
101 if (WIFSIGNALED(status
))
102 fprintf( stderr
, "%04x: *exited* signal=%d\n",
103 thread
->id
, WTERMSIG(status
) );
105 fprintf( stderr
, "%04x: *exited* status=%d\n",
106 thread
->id
, WEXITSTATUS(status
) );
112 /* wait4 wrapper to handle missing __WALL flag in older kernels */
113 static inline pid_t
wait4_wrapper( pid_t pid
, int *status
, int options
, struct rusage
*usage
)
116 static int wall_flag
= __WALL
;
120 pid_t ret
= wait4( pid
, status
, options
| wall_flag
, usage
);
121 if (ret
!= -1 || !wall_flag
|| errno
!= EINVAL
) return ret
;
125 return wait4( pid
, status
, options
, usage
);
129 /* handle a SIGCHLD signal */
130 void sigchld_callback(void)
136 if (!(pid
= wait4_wrapper( -1, &status
, WUNTRACED
| WNOHANG
, NULL
))) break;
137 if (pid
!= -1) handle_child_status( get_thread_from_pid(pid
), pid
, status
, -1 );
142 /* wait for a ptraced child to get a certain signal */
143 static int wait4_thread( struct thread
*thread
, int signal
)
150 if ((res
= wait4_wrapper( get_ptrace_pid(thread
), &status
, WUNTRACED
, NULL
)) == -1)
154 if (!watchdog_triggered()) continue;
155 if (debug_level
) fprintf( stderr
, "%04x: *watchdog* wait4 aborted\n", thread
->id
);
157 else if (errno
== ECHILD
) /* must have died */
159 thread
->unix_pid
= -1;
160 thread
->unix_tid
= -1;
161 thread
->attached
= 0;
163 else perror( "wait4" );
167 res
= handle_child_status( thread
, res
, status
, signal
);
168 if (!res
|| res
== signal
) break;
171 return (thread
->unix_pid
!= -1);
174 /* return the Unix pid to use in ptrace calls for a given thread */
175 int get_ptrace_pid( struct thread
*thread
)
177 if (thread
->unix_tid
!= -1) return thread
->unix_tid
;
178 return thread
->unix_pid
;
181 /* send a Unix signal to a specific thread */
182 int send_thread_signal( struct thread
*thread
, int sig
)
186 if (thread
->unix_pid
!= -1)
188 if (thread
->unix_tid
!= -1)
190 ret
= tkill( thread
->unix_pid
, thread
->unix_tid
, sig
);
191 if (ret
== -1 && errno
== ENOSYS
) ret
= kill( thread
->unix_pid
, sig
);
193 else ret
= kill( thread
->unix_pid
, sig
);
195 if (ret
== -1 && errno
== ESRCH
) /* thread got killed */
197 thread
->unix_pid
= -1;
198 thread
->unix_tid
= -1;
199 thread
->attached
= 0;
205 /* attach to a Unix process with ptrace */
206 int attach_process( struct process
*process
)
208 struct thread
*thread
;
211 if (list_empty( &process
->thread_list
)) /* need at least one running thread */
213 set_error( STATUS_ACCESS_DENIED
);
217 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
219 if (thread
->attached
) continue;
220 if (suspend_for_ptrace( thread
)) resume_after_ptrace( thread
);
226 /* detach from a ptraced Unix process */
227 void detach_process( struct process
*process
)
229 struct thread
*thread
;
231 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
233 if (thread
->attached
)
235 /* make sure it is stopped */
236 suspend_for_ptrace( thread
);
237 if (thread
->unix_pid
== -1) return;
238 if (debug_level
) fprintf( stderr
, "%04x: *detached*\n", thread
->id
);
239 ptrace( PTRACE_DETACH
, get_ptrace_pid(thread
), (caddr_t
)1, 0 );
240 thread
->attached
= 0;
245 /* suspend a thread to allow using ptrace on it */
246 /* you must do a resume_after_ptrace when finished with the thread */
247 int suspend_for_ptrace( struct thread
*thread
)
249 /* can't stop a thread while initialisation is in progress */
250 if (thread
->unix_pid
== -1 || !is_process_init_done(thread
->process
)) goto error
;
252 if (thread
->attached
)
254 if (!send_thread_signal( thread
, SIGSTOP
)) goto error
;
258 /* this may fail if the client is already being debugged */
259 if (!use_ptrace
) goto error
;
260 if (ptrace( PTRACE_ATTACH
, get_ptrace_pid(thread
), 0, 0 ) == -1)
262 if (errno
== ESRCH
) thread
->unix_pid
= thread
->unix_tid
= -1; /* thread got killed */
265 if (debug_level
) fprintf( stderr
, "%04x: *attached*\n", thread
->id
);
266 thread
->attached
= 1;
268 if (wait4_thread( thread
, SIGSTOP
)) return 1;
269 resume_after_ptrace( thread
);
271 set_error( STATUS_ACCESS_DENIED
);
275 /* resume a thread after we have used ptrace on it */
276 void resume_after_ptrace( struct thread
*thread
)
278 if (thread
->unix_pid
== -1) return;
279 assert( thread
->attached
);
280 ptrace( get_thread_single_step(thread
) ? PTRACE_SINGLESTEP
: PTRACE_CONT
,
281 get_ptrace_pid(thread
), (caddr_t
)1, 0 /* cancel the SIGSTOP */ );
284 /* read an int from a thread address space */
285 int read_thread_int( struct thread
*thread
, const int *addr
, int *data
)
288 *data
= ptrace( PTRACE_PEEKDATA
, get_ptrace_pid(thread
), (caddr_t
)addr
, 0 );
289 if ( *data
== -1 && errno
)
297 /* write an int to a thread address space */
298 int write_thread_int( struct thread
*thread
, int *addr
, int data
, unsigned int mask
)
303 if (read_thread_int( thread
, addr
, &res
) == -1) return -1;
304 data
= (data
& mask
) | (res
& ~mask
);
306 if ((res
= ptrace( PTRACE_POKEDATA
, get_ptrace_pid(thread
), (caddr_t
)addr
, data
)) == -1)