ptrace.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Server-side ptrace support
  3. *
  4. * Copyright (C) 1999 Alexandre Julliard
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include "config.h"
  21. #include <assert.h>
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <signal.h>
  25. #include <sys/types.h>
  26. #ifdef HAVE_SYS_PTRACE_H
  27. # include <sys/ptrace.h>
  28. #endif
  29. #ifdef HAVE_SYS_WAIT_H
  30. # include <sys/wait.h>
  31. #endif
  32. #include <unistd.h>
  33. #include "file.h"
  34. #include "process.h"
  35. #include "thread.h"
  36. #ifndef PTRACE_CONT
  37. #define PTRACE_CONT PT_CONTINUE
  38. #endif
  39. #ifndef PTRACE_SINGLESTEP
  40. #define PTRACE_SINGLESTEP PT_STEP
  41. #endif
  42. #ifndef PTRACE_ATTACH
  43. #define PTRACE_ATTACH PT_ATTACH
  44. #endif
  45. #ifndef PTRACE_DETACH
  46. #define PTRACE_DETACH PT_DETACH
  47. #endif
  48. #ifndef PTRACE_PEEKDATA
  49. #define PTRACE_PEEKDATA PT_READ_D
  50. #endif
  51. #ifndef PTRACE_POKEDATA
  52. #define PTRACE_POKEDATA PT_WRITE_D
  53. #endif
  54. #ifndef HAVE_SYS_PTRACE_H
  55. #define PT_CONTINUE 0
  56. #define PT_ATTACH 1
  57. #define PT_DETACH 2
  58. #define PT_READ_D 3
  59. #define PT_WRITE_D 4
  60. #define PT_STEP 5
  61. inline static int ptrace(int req, ...) { errno = EPERM; return -1; /*FAIL*/ }
  62. #endif /* HAVE_SYS_PTRACE_H */
  63. static const int use_ptrace = 1; /* set to 0 to disable ptrace */
  64. /* handle a status returned by wait4 */
  65. static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
  66. {
  67. if (WIFSTOPPED(status))
  68. {
  69. int sig = WSTOPSIG(status);
  70. if (debug_level && thread)
  71. fprintf( stderr, "%04x: *signal* signal=%d\n", thread->id, sig );
  72. if (sig != want_sig)
  73. {
  74. /* ignore other signals for now */
  75. if (thread && get_thread_single_step( thread ))
  76. ptrace( PTRACE_SINGLESTEP, pid, (caddr_t)1, sig );
  77. else
  78. ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
  79. }
  80. return sig;
  81. }
  82. if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
  83. {
  84. thread->attached = 0;
  85. thread->unix_pid = -1;
  86. thread->unix_tid = -1;
  87. if (debug_level)
  88. {
  89. if (WIFSIGNALED(status))
  90. fprintf( stderr, "%04x: *exited* signal=%d\n",
  91. thread->id, WTERMSIG(status) );
  92. else
  93. fprintf( stderr, "%04x: *exited* status=%d\n",
  94. thread->id, WEXITSTATUS(status) );
  95. }
  96. }
  97. return 0;
  98. }
  99. /* wait4 wrapper to handle missing __WALL flag in older kernels */
  100. static inline pid_t wait4_wrapper( pid_t pid, int *status, int options, struct rusage *usage )
  101. {
  102. #ifdef __WALL
  103. static int wall_flag = __WALL;
  104. for (;;)
  105. {
  106. pid_t ret = wait4( pid, status, options | wall_flag, usage );
  107. if (ret != -1 || !wall_flag || errno != EINVAL) return ret;
  108. wall_flag = 0;
  109. }
  110. #else
  111. return wait4( pid, status, options, usage );
  112. #endif
  113. }
  114. /* handle a SIGCHLD signal */
  115. void sigchld_callback(void)
  116. {
  117. int pid, status;
  118. for (;;)
  119. {
  120. if (!(pid = wait4_wrapper( -1, &status, WUNTRACED | WNOHANG, NULL ))) break;
  121. if (pid != -1) handle_child_status( get_thread_from_pid(pid), pid, status, -1 );
  122. else break;
  123. }
  124. }
  125. /* wait for a ptraced child to get a certain signal */
  126. static int wait4_thread( struct thread *thread, int signal )
  127. {
  128. int res, status;
  129. for (;;)
  130. {
  131. if ((res = wait4_wrapper( get_ptrace_pid(thread), &status, WUNTRACED, NULL )) == -1)
  132. {
  133. if (errno == EINTR) continue;
  134. if (errno == ECHILD) /* must have died */
  135. {
  136. thread->unix_pid = -1;
  137. thread->unix_tid = -1;
  138. thread->attached = 0;
  139. }
  140. else perror( "wait4" );
  141. return 0;
  142. }
  143. res = handle_child_status( thread, res, status, signal );
  144. if (!res || res == signal) break;
  145. }
  146. return (thread->unix_pid != -1);
  147. }
  148. /* return the Unix pid to use in ptrace calls for a given thread */
  149. int get_ptrace_pid( struct thread *thread )
  150. {
  151. if (thread->unix_tid != -1) return thread->unix_tid;
  152. return thread->unix_pid;
  153. }
  154. /* send a Unix signal to a specific thread */
  155. int send_thread_signal( struct thread *thread, int sig )
  156. {
  157. int ret = -1;
  158. if (thread->unix_pid != -1)
  159. {
  160. if (thread->unix_tid != -1)
  161. {
  162. ret = tkill( thread->unix_tid, sig );
  163. if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
  164. }
  165. else ret = kill( thread->unix_pid, sig );
  166. if (ret == -1 && errno == ESRCH) /* thread got killed */
  167. {
  168. thread->unix_pid = -1;
  169. thread->unix_tid = -1;
  170. thread->attached = 0;
  171. }
  172. }
  173. return (ret != -1);
  174. }
  175. /* attach to a Unix thread */
  176. static int attach_thread( struct thread *thread )
  177. {
  178. /* this may fail if the client is already being debugged */
  179. if (!use_ptrace) return 0;
  180. if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
  181. {
  182. if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
  183. return 0;
  184. }
  185. if (debug_level) fprintf( stderr, "%04x: *attached*\n", thread->id );
  186. thread->attached = 1;
  187. return wait4_thread( thread, SIGSTOP );
  188. }
  189. /* detach from a Unix thread and kill it */
  190. void detach_thread( struct thread *thread, int sig )
  191. {
  192. if (thread->unix_pid == -1) return;
  193. if (thread->attached)
  194. {
  195. /* make sure it is stopped */
  196. suspend_for_ptrace( thread );
  197. if (sig) send_thread_signal( thread, sig );
  198. if (thread->unix_pid == -1) return;
  199. if (debug_level) fprintf( stderr, "%04x: *detached*\n", thread->id );
  200. ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, sig );
  201. thread->attached = 0;
  202. }
  203. else if (sig) send_thread_signal( thread, sig );
  204. }
  205. /* attach to a Unix process with ptrace */
  206. int attach_process( struct process *process )
  207. {
  208. struct thread *thread;
  209. int ret = 1;
  210. if (!process->thread_list) /* need at least one running thread */
  211. {
  212. set_error( STATUS_ACCESS_DENIED );
  213. return 0;
  214. }
  215. for (thread = process->thread_list; thread; thread = thread->proc_next)
  216. {
  217. if (thread->attached) continue;
  218. if (suspend_for_ptrace( thread )) resume_after_ptrace( thread );
  219. else ret = 0;
  220. }
  221. return ret;
  222. }
  223. /* detach from a ptraced Unix process */
  224. void detach_process( struct process *process )
  225. {
  226. struct thread *thread;
  227. for (thread = process->thread_list; thread; thread = thread->proc_next)
  228. {
  229. if (thread->attached) detach_thread( thread, 0 );
  230. }
  231. }
  232. /* suspend a thread to allow using ptrace on it */
  233. /* you must do a resume_after_ptrace when finished with the thread */
  234. int suspend_for_ptrace( struct thread *thread )
  235. {
  236. /* can't stop a thread while initialisation is in progress */
  237. if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
  238. if (thread->attached)
  239. {
  240. if (!send_thread_signal( thread, SIGSTOP )) goto error;
  241. if (!wait4_thread( thread, SIGSTOP )) goto error;
  242. return 1;
  243. }
  244. if (attach_thread( thread )) return 1;
  245. error:
  246. set_error( STATUS_ACCESS_DENIED );
  247. return 0;
  248. }
  249. /* resume a thread after we have used ptrace on it */
  250. void resume_after_ptrace( struct thread *thread )
  251. {
  252. if (thread->unix_pid == -1) return;
  253. assert( thread->attached );
  254. ptrace( get_thread_single_step(thread) ? PTRACE_SINGLESTEP : PTRACE_CONT,
  255. get_ptrace_pid(thread), (caddr_t)1, 0 /* cancel the SIGSTOP */ );
  256. }
  257. /* read an int from a thread address space */
  258. int read_thread_int( struct thread *thread, const int *addr, int *data )
  259. {
  260. errno = 0;
  261. *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
  262. if ( *data == -1 && errno)
  263. {
  264. file_set_error();
  265. return -1;
  266. }
  267. return 0;
  268. }
  269. /* write an int to a thread address space */
  270. int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
  271. {
  272. int res;
  273. if (mask != ~0)
  274. {
  275. if (read_thread_int( thread, addr, &res ) == -1) return -1;
  276. data = (data & mask) | (res & ~mask);
  277. }
  278. if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
  279. file_set_error();
  280. return res;
  281. }