ptrace.c 5.5 KB

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