ptrace.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/audit.h"
  6. #include "linux/ptrace.h"
  7. #include "linux/sched.h"
  8. #include "asm/uaccess.h"
  9. #include "skas_ptrace.h"
  10. void user_enable_single_step(struct task_struct *child)
  11. {
  12. child->ptrace |= PT_DTRACE;
  13. child->thread.singlestep_syscall = 0;
  14. #ifdef SUBARCH_SET_SINGLESTEPPING
  15. SUBARCH_SET_SINGLESTEPPING(child, 1);
  16. #endif
  17. }
  18. void user_disable_single_step(struct task_struct *child)
  19. {
  20. child->ptrace &= ~PT_DTRACE;
  21. child->thread.singlestep_syscall = 0;
  22. #ifdef SUBARCH_SET_SINGLESTEPPING
  23. SUBARCH_SET_SINGLESTEPPING(child, 0);
  24. #endif
  25. }
  26. /*
  27. * Called by kernel/ptrace.c when detaching..
  28. */
  29. void ptrace_disable(struct task_struct *child)
  30. {
  31. user_disable_single_step(child);
  32. }
  33. extern int peek_user(struct task_struct * child, long addr, long data);
  34. extern int poke_user(struct task_struct * child, long addr, long data);
  35. long arch_ptrace(struct task_struct *child, long request,
  36. unsigned long addr, unsigned long data)
  37. {
  38. int i, ret;
  39. unsigned long __user *p = (void __user *)data;
  40. void __user *vp = p;
  41. switch (request) {
  42. /* read word at location addr. */
  43. case PTRACE_PEEKTEXT:
  44. case PTRACE_PEEKDATA:
  45. ret = generic_ptrace_peekdata(child, addr, data);
  46. break;
  47. /* read the word at location addr in the USER area. */
  48. case PTRACE_PEEKUSR:
  49. ret = peek_user(child, addr, data);
  50. break;
  51. /* write the word at location addr. */
  52. case PTRACE_POKETEXT:
  53. case PTRACE_POKEDATA:
  54. ret = generic_ptrace_pokedata(child, addr, data);
  55. break;
  56. /* write the word at location addr in the USER area */
  57. case PTRACE_POKEUSR:
  58. ret = poke_user(child, addr, data);
  59. break;
  60. case PTRACE_SYSEMU:
  61. case PTRACE_SYSEMU_SINGLESTEP:
  62. ret = -EIO;
  63. break;
  64. #ifdef PTRACE_GETREGS
  65. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  66. if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
  67. ret = -EIO;
  68. break;
  69. }
  70. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  71. __put_user(getreg(child, i), p);
  72. p++;
  73. }
  74. ret = 0;
  75. break;
  76. }
  77. #endif
  78. #ifdef PTRACE_SETREGS
  79. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  80. unsigned long tmp = 0;
  81. if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
  82. ret = -EIO;
  83. break;
  84. }
  85. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  86. __get_user(tmp, p);
  87. putreg(child, i, tmp);
  88. p++;
  89. }
  90. ret = 0;
  91. break;
  92. }
  93. #endif
  94. #ifdef PTRACE_GETFPREGS
  95. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  96. ret = get_fpregs(vp, child);
  97. break;
  98. #endif
  99. #ifdef PTRACE_SETFPREGS
  100. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  101. ret = set_fpregs(vp, child);
  102. break;
  103. #endif
  104. case PTRACE_GET_THREAD_AREA:
  105. ret = ptrace_get_thread_area(child, addr, vp);
  106. break;
  107. case PTRACE_SET_THREAD_AREA:
  108. ret = ptrace_set_thread_area(child, addr, vp);
  109. break;
  110. case PTRACE_FAULTINFO: {
  111. /*
  112. * Take the info from thread->arch->faultinfo,
  113. * but transfer max. sizeof(struct ptrace_faultinfo).
  114. * On i386, ptrace_faultinfo is smaller!
  115. */
  116. ret = copy_to_user(p, &child->thread.arch.faultinfo,
  117. sizeof(struct ptrace_faultinfo)) ?
  118. -EIO : 0;
  119. break;
  120. }
  121. #ifdef PTRACE_LDT
  122. case PTRACE_LDT: {
  123. struct ptrace_ldt ldt;
  124. if (copy_from_user(&ldt, p, sizeof(ldt))) {
  125. ret = -EIO;
  126. break;
  127. }
  128. /*
  129. * This one is confusing, so just punt and return -EIO for
  130. * now
  131. */
  132. ret = -EIO;
  133. break;
  134. }
  135. #endif
  136. #ifdef PTRACE_ARCH_PRCTL
  137. case PTRACE_ARCH_PRCTL:
  138. /* XXX Calls ptrace on the host - needs some SMP thinking */
  139. ret = arch_prctl(child, data, (void __user *) addr);
  140. break;
  141. #endif
  142. default:
  143. ret = ptrace_request(child, request, addr, data);
  144. if (ret == -EIO)
  145. ret = subarch_ptrace(child, request, addr, data);
  146. break;
  147. }
  148. return ret;
  149. }
  150. static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
  151. int error_code)
  152. {
  153. struct siginfo info;
  154. memset(&info, 0, sizeof(info));
  155. info.si_signo = SIGTRAP;
  156. info.si_code = TRAP_BRKPT;
  157. /* User-mode eip? */
  158. info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
  159. /* Send us the fake SIGTRAP */
  160. force_sig_info(SIGTRAP, &info, tsk);
  161. }
  162. /*
  163. * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
  164. * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
  165. */
  166. void syscall_trace(struct uml_pt_regs *regs, int entryexit)
  167. {
  168. int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
  169. int tracesysgood;
  170. if (unlikely(current->audit_context)) {
  171. if (!entryexit)
  172. audit_syscall_entry(HOST_AUDIT_ARCH,
  173. UPT_SYSCALL_NR(regs),
  174. UPT_SYSCALL_ARG1(regs),
  175. UPT_SYSCALL_ARG2(regs),
  176. UPT_SYSCALL_ARG3(regs),
  177. UPT_SYSCALL_ARG4(regs));
  178. else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
  179. UPT_SYSCALL_RET(regs));
  180. }
  181. /* Fake a debug trap */
  182. if (is_singlestep)
  183. send_sigtrap(current, regs, 0);
  184. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  185. return;
  186. if (!(current->ptrace & PT_PTRACED))
  187. return;
  188. /*
  189. * the 0x80 provides a way for the tracing parent to distinguish
  190. * between a syscall stop and SIGTRAP delivery
  191. */
  192. tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
  193. ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
  194. if (entryexit) /* force do_signal() --> is_syscall() */
  195. set_thread_flag(TIF_SIGPENDING);
  196. /*
  197. * this isn't the same as continuing with a signal, but it will do
  198. * for normal use. strace only continues with a signal if the
  199. * stopping signal is not SIGTRAP. -brl
  200. */
  201. if (current->exit_code) {
  202. send_sig(current->exit_code, current, 1);
  203. current->exit_code = 0;
  204. }
  205. }