2 * Server-side ptrace support
4 * Copyright (C) 1999 Alexandre Julliard
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_PTRACE_H
15 # include <sys/ptrace.h>
17 #ifdef HAVE_SYS_WAIT_H
18 # include <sys/wait.h>
27 #define PTRACE_CONT PT_CONTINUE
30 #define PTRACE_ATTACH PT_ATTACH
33 #define PTRACE_DETACH PT_DETACH
35 #ifndef PTRACE_PEEKDATA
36 #define PTRACE_PEEKDATA PT_READ_D
38 #ifndef PTRACE_POKEDATA
39 #define PTRACE_POKEDATA PT_WRITE_D
42 #ifdef HAVE_SYS_PTRACE_H
43 static const int use_ptrace
= 1; /* set to 0 to disable ptrace */
45 static const int use_ptrace
= 0;
53 static int ptrace(int req
, ...) { return -1; /*FAIL*/ }
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
);
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
);
70 default: /* ignore other signals for now */
71 ptrace( PTRACE_CONT
, pid
, (caddr_t
)1, sig
);
76 if (thread
&& (WIFSIGNALED(status
) || WIFEXITED(status
)))
82 if (WIFSIGNALED(status
))
83 fprintf( stderr
, "%08x: *exited* signal=%d\n",
84 (unsigned int)thread
, WTERMSIG(status
) );
86 fprintf( stderr
, "%08x: *exited* status=%d\n",
87 (unsigned int)thread
, WEXITSTATUS(status
) );
93 /* handle a SIGCHLD signal */
94 void sigchld_handler()
100 if (!(pid
= wait4( -1, &status
, WUNTRACED
| WNOHANG
, NULL
))) break;
101 if (pid
!= -1) handle_child_status( get_thread_from_pid(pid
), pid
, status
);
106 /* wait for a ptraced child to get a certain signal */
107 void wait4_thread( struct thread
*thread
, int signal
)
113 if ((res
= wait4( thread
->unix_pid
, &status
, WUNTRACED
, NULL
)) == -1)
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
);
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;
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 );
183 /* can't stop a thread while initialisation is in progress */
184 if (!thread
->unix_pid
|| thread
->process
->init_event
) goto error
;
186 if (attach_thread( thread
)) return 1;
189 set_error( STATUS_ACCESS_DENIED
);
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
)
205 /* write an int to a thread address space */
206 int write_thread_int( struct thread
*thread
, int *addr
, int data
, unsigned int mask
)
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)