ptrace.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**********************************************************************
  2. ptrace.c
  3. Copyright (C) 1999 Lars Brinkhoff. See the file COPYING for licensing
  4. terms and conditions.
  5. Jeff Dike (jdike@karaya.com) : Modified for integration into uml
  6. **********************************************************************/
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10. #include <sys/types.h>
  11. #include <sys/time.h>
  12. #include <sys/wait.h>
  13. #include "ptproxy.h"
  14. #include "debug.h"
  15. #include "user_util.h"
  16. #include "kern_util.h"
  17. #include "ptrace_user.h"
  18. #include "tt.h"
  19. #include "os.h"
  20. long proxy_ptrace(struct debugger *debugger, int arg1, pid_t arg2,
  21. long arg3, long arg4, pid_t child, int *ret)
  22. {
  23. sigset_t relay;
  24. long result;
  25. int status;
  26. *ret = 0;
  27. if(debugger->debugee->died) return(-ESRCH);
  28. switch(arg1){
  29. case PTRACE_ATTACH:
  30. if(debugger->debugee->traced) return(-EPERM);
  31. debugger->debugee->pid = arg2;
  32. debugger->debugee->traced = 1;
  33. if(is_valid_pid(arg2) && (arg2 != child)){
  34. debugger->debugee->in_context = 0;
  35. kill(arg2, SIGSTOP);
  36. debugger->debugee->event = 1;
  37. debugger->debugee->wait_status = W_STOPCODE(SIGSTOP);
  38. }
  39. else {
  40. debugger->debugee->in_context = 1;
  41. if(debugger->debugee->stopped)
  42. child_proxy(child, W_STOPCODE(SIGSTOP));
  43. else kill(child, SIGSTOP);
  44. }
  45. return(0);
  46. case PTRACE_DETACH:
  47. if(!debugger->debugee->traced) return(-EPERM);
  48. debugger->debugee->traced = 0;
  49. debugger->debugee->pid = 0;
  50. if(!debugger->debugee->in_context)
  51. kill(child, SIGCONT);
  52. return(0);
  53. case PTRACE_CONT:
  54. if(!debugger->debugee->in_context) return(-EPERM);
  55. *ret = PTRACE_CONT;
  56. return(ptrace(PTRACE_CONT, child, arg3, arg4));
  57. #ifdef UM_HAVE_GETFPREGS
  58. case PTRACE_GETFPREGS:
  59. {
  60. long regs[FP_FRAME_SIZE];
  61. int i, result;
  62. result = ptrace(PTRACE_GETFPREGS, child, 0, regs);
  63. if(result == -1) return(-errno);
  64. for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
  65. ptrace(PTRACE_POKEDATA, debugger->pid, arg4 + 4 * i,
  66. regs[i]);
  67. return(result);
  68. }
  69. #endif
  70. #ifdef UM_HAVE_GETFPXREGS
  71. case PTRACE_GETFPXREGS:
  72. {
  73. long regs[FPX_FRAME_SIZE];
  74. int i, result;
  75. result = ptrace(PTRACE_GETFPXREGS, child, 0, regs);
  76. if(result == -1) return(-errno);
  77. for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
  78. ptrace(PTRACE_POKEDATA, debugger->pid, arg4 + 4 * i,
  79. regs[i]);
  80. return(result);
  81. }
  82. #endif
  83. #ifdef UM_HAVE_GETREGS
  84. case PTRACE_GETREGS:
  85. {
  86. long regs[FRAME_SIZE];
  87. int i, result;
  88. result = ptrace(PTRACE_GETREGS, child, 0, regs);
  89. if(result == -1) return(-errno);
  90. for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
  91. ptrace (PTRACE_POKEDATA, debugger->pid,
  92. arg4 + 4 * i, regs[i]);
  93. return(result);
  94. }
  95. break;
  96. #endif
  97. case PTRACE_KILL:
  98. result = ptrace(PTRACE_KILL, child, arg3, arg4);
  99. if(result == -1) return(-errno);
  100. return(result);
  101. case PTRACE_PEEKDATA:
  102. case PTRACE_PEEKTEXT:
  103. case PTRACE_PEEKUSR:
  104. /* The value being read out could be -1, so we have to
  105. * check errno to see if there's an error, and zero it
  106. * beforehand so we're not faked out by an old error
  107. */
  108. errno = 0;
  109. result = ptrace(arg1, child, arg3, 0);
  110. if((result == -1) && (errno != 0)) return(-errno);
  111. result = ptrace(PTRACE_POKEDATA, debugger->pid, arg4, result);
  112. if(result == -1) return(-errno);
  113. return(result);
  114. case PTRACE_POKEDATA:
  115. case PTRACE_POKETEXT:
  116. case PTRACE_POKEUSR:
  117. result = ptrace(arg1, child, arg3, arg4);
  118. if(result == -1) return(-errno);
  119. if(arg1 == PTRACE_POKEUSR) ptrace_pokeuser(arg3, arg4);
  120. return(result);
  121. #ifdef UM_HAVE_SETFPREGS
  122. case PTRACE_SETFPREGS:
  123. {
  124. long regs[FP_FRAME_SIZE];
  125. int i;
  126. for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
  127. regs[i] = ptrace (PTRACE_PEEKDATA, debugger->pid,
  128. arg4 + 4 * i, 0);
  129. result = ptrace(PTRACE_SETFPREGS, child, 0, regs);
  130. if(result == -1) return(-errno);
  131. return(result);
  132. }
  133. #endif
  134. #ifdef UM_HAVE_SETFPXREGS
  135. case PTRACE_SETFPXREGS:
  136. {
  137. long regs[FPX_FRAME_SIZE];
  138. int i;
  139. for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
  140. regs[i] = ptrace (PTRACE_PEEKDATA, debugger->pid,
  141. arg4 + 4 * i, 0);
  142. result = ptrace(PTRACE_SETFPXREGS, child, 0, regs);
  143. if(result == -1) return(-errno);
  144. return(result);
  145. }
  146. #endif
  147. #ifdef UM_HAVE_SETREGS
  148. case PTRACE_SETREGS:
  149. {
  150. long regs[FRAME_SIZE];
  151. int i;
  152. for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
  153. regs[i] = ptrace(PTRACE_PEEKDATA, debugger->pid,
  154. arg4 + 4 * i, 0);
  155. result = ptrace(PTRACE_SETREGS, child, 0, regs);
  156. if(result == -1) return(-errno);
  157. return(result);
  158. }
  159. #endif
  160. case PTRACE_SINGLESTEP:
  161. if(!debugger->debugee->in_context) return(-EPERM);
  162. sigemptyset(&relay);
  163. sigaddset(&relay, SIGSEGV);
  164. sigaddset(&relay, SIGILL);
  165. sigaddset(&relay, SIGBUS);
  166. result = ptrace(PTRACE_SINGLESTEP, child, arg3, arg4);
  167. if(result == -1) return(-errno);
  168. status = wait_for_stop(child, SIGTRAP, PTRACE_SINGLESTEP,
  169. &relay);
  170. child_proxy(child, status);
  171. return(result);
  172. case PTRACE_SYSCALL:
  173. if(!debugger->debugee->in_context) return(-EPERM);
  174. result = ptrace(PTRACE_SYSCALL, child, arg3, arg4);
  175. if(result == -1) return(-errno);
  176. *ret = PTRACE_SYSCALL;
  177. return(result);
  178. case PTRACE_TRACEME:
  179. default:
  180. return(-EINVAL);
  181. }
  182. }
  183. /*
  184. * Overrides for Emacs so that we follow Linus's tabbing style.
  185. * Emacs will notice this stuff at the end of the file and automatically
  186. * adjust the settings for this buffer only. This must remain at the end
  187. * of the file.
  188. * ---------------------------------------------------------------------------
  189. * Local variables:
  190. * c-file-style: "linux"
  191. * End:
  192. */