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 */
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
);
81 case SIGSTOP
: /* continue at once if not suspended */
82 if (thread
&& (thread
->process
->suspend
+ thread
->suspend
)) break;
84 default: /* ignore other signals for now */
85 if (thread
&& get_thread_single_step( thread
))
86 ptrace( PTRACE_SINGLESTEP
, pid
, (caddr_t
)1, sig
);
88 ptrace( PTRACE_CONT
, pid
, (caddr_t
)1, sig
);
93 if (thread
&& (WIFSIGNALED(status
) || WIFEXITED(status
)))
99 if (WIFSIGNALED(status
))
100 fprintf( stderr
, "%08x: *exited* signal=%d\n",
101 (unsigned int)thread
, WTERMSIG(status
) );
103 fprintf( stderr
, "%08x: *exited* status=%d\n",
104 (unsigned int)thread
, WEXITSTATUS(status
) );
110 /* handle a SIGCHLD signal */
111 void sigchld_handler()
117 if (!(pid
= wait4( -1, &status
, WUNTRACED
| WNOHANG
, NULL
))) break;
118 if (pid
!= -1) handle_child_status( get_thread_from_pid(pid
), pid
, status
);
123 /* wait for a ptraced child to get a certain signal */
124 void wait4_thread( struct thread
*thread
, int signal
)
130 if ((res
= wait4( thread
->unix_pid
, &status
, WUNTRACED
, NULL
)) == -1)
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 */
149 if (debug_level
) fprintf( stderr
, "%08x: *attached*\n", (unsigned int)thread
);
150 thread
->attached
= 1;
151 wait4_thread( thread
, SIGSTOP
);
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;
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 );
208 /* can't stop a thread while initialisation is in progress */
209 if (!thread
->unix_pid
|| !is_process_init_done(thread
->process
)) goto error
;
211 if (attach_thread( thread
)) return 1;
214 set_error( STATUS_ACCESS_DENIED
);
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
)
230 /* write an int to a thread address space */
231 int write_thread_int( struct thread
*thread
, int *addr
, int data
, unsigned int mask
)
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)