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