ptrace.c 5.6 KB

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