ptrace.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * linux/arch/sh/kernel/ptrace.c
  3. *
  4. * Original x86 implementation:
  5. * By Ross Biro 1/23/92
  6. * edited by Linus Torvalds
  7. *
  8. * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/errno.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/user.h>
  18. #include <linux/slab.h>
  19. #include <linux/security.h>
  20. #include <linux/signal.h>
  21. #include <linux/io.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/system.h>
  25. #include <asm/processor.h>
  26. #include <asm/mmu_context.h>
  27. /*
  28. * does not yet catch signals sent when the child dies.
  29. * in exit.c or in signal.c.
  30. */
  31. /*
  32. * This routine will get a word off of the process kernel stack.
  33. */
  34. static inline int get_stack_long(struct task_struct *task, int offset)
  35. {
  36. unsigned char *stack;
  37. stack = (unsigned char *)task_pt_regs(task);
  38. stack += offset;
  39. return (*((int *)stack));
  40. }
  41. /*
  42. * This routine will put a word on the process kernel stack.
  43. */
  44. static inline int put_stack_long(struct task_struct *task, int offset,
  45. unsigned long data)
  46. {
  47. unsigned char *stack;
  48. stack = (unsigned char *)task_pt_regs(task);
  49. stack += offset;
  50. *(unsigned long *) stack = data;
  51. return 0;
  52. }
  53. static void ptrace_disable_singlestep(struct task_struct *child)
  54. {
  55. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  56. /*
  57. * Ensure the UBC is not programmed at the next context switch.
  58. *
  59. * Normally this is not needed but there are sequences such as
  60. * singlestep, signal delivery, and continue that leave the
  61. * ubc_pc non-zero leading to spurious SIGTRAPs.
  62. */
  63. if (child->thread.ubc_pc != 0) {
  64. ubc_usercnt -= 1;
  65. child->thread.ubc_pc = 0;
  66. }
  67. }
  68. /*
  69. * Called by kernel/ptrace.c when detaching..
  70. *
  71. * Make sure single step bits etc are not set.
  72. */
  73. void ptrace_disable(struct task_struct *child)
  74. {
  75. ptrace_disable_singlestep(child);
  76. }
  77. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  78. {
  79. struct user * dummy = NULL;
  80. int ret;
  81. switch (request) {
  82. /* when I and D space are separate, these will need to be fixed. */
  83. case PTRACE_PEEKTEXT: /* read word at location addr. */
  84. case PTRACE_PEEKDATA:
  85. ret = generic_ptrace_peekdata(child, addr, data);
  86. break;
  87. /* read the word at location addr in the USER area. */
  88. case PTRACE_PEEKUSR: {
  89. unsigned long tmp;
  90. ret = -EIO;
  91. if ((addr & 3) || addr < 0 ||
  92. addr > sizeof(struct user) - 3)
  93. break;
  94. if (addr < sizeof(struct pt_regs))
  95. tmp = get_stack_long(child, addr);
  96. else if (addr >= (long) &dummy->fpu &&
  97. addr < (long) &dummy->u_fpvalid) {
  98. if (!tsk_used_math(child)) {
  99. if (addr == (long)&dummy->fpu.fpscr)
  100. tmp = FPSCR_INIT;
  101. else
  102. tmp = 0;
  103. } else
  104. tmp = ((long *)&child->thread.fpu)
  105. [(addr - (long)&dummy->fpu) >> 2];
  106. } else if (addr == (long) &dummy->u_fpvalid)
  107. tmp = !!tsk_used_math(child);
  108. else
  109. tmp = 0;
  110. ret = put_user(tmp, (unsigned long __user *)data);
  111. break;
  112. }
  113. /* when I and D space are separate, this will have to be fixed. */
  114. case PTRACE_POKETEXT: /* write the word at location addr. */
  115. case PTRACE_POKEDATA:
  116. ret = generic_ptrace_pokedata(child, addr, data);
  117. break;
  118. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  119. ret = -EIO;
  120. if ((addr & 3) || addr < 0 ||
  121. addr > sizeof(struct user) - 3)
  122. break;
  123. if (addr < sizeof(struct pt_regs))
  124. ret = put_stack_long(child, addr, data);
  125. else if (addr >= (long) &dummy->fpu &&
  126. addr < (long) &dummy->u_fpvalid) {
  127. set_stopped_child_used_math(child);
  128. ((long *)&child->thread.fpu)
  129. [(addr - (long)&dummy->fpu) >> 2] = data;
  130. ret = 0;
  131. } else if (addr == (long) &dummy->u_fpvalid) {
  132. conditional_stopped_child_used_math(data, child);
  133. ret = 0;
  134. }
  135. break;
  136. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  137. case PTRACE_CONT: { /* restart after signal. */
  138. ret = -EIO;
  139. if (!valid_signal(data))
  140. break;
  141. if (request == PTRACE_SYSCALL)
  142. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  143. else
  144. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  145. ptrace_disable_singlestep(child);
  146. child->exit_code = data;
  147. wake_up_process(child);
  148. ret = 0;
  149. break;
  150. }
  151. /*
  152. * make the child exit. Best I can do is send it a sigkill.
  153. * perhaps it should be put in the status that it wants to
  154. * exit.
  155. */
  156. case PTRACE_KILL: {
  157. ret = 0;
  158. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  159. break;
  160. ptrace_disable_singlestep(child);
  161. child->exit_code = SIGKILL;
  162. wake_up_process(child);
  163. break;
  164. }
  165. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  166. long pc;
  167. struct pt_regs *regs = NULL;
  168. ret = -EIO;
  169. if (!valid_signal(data))
  170. break;
  171. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  172. if ((child->ptrace & PT_DTRACE) == 0) {
  173. /* Spurious delayed TF traps may occur */
  174. child->ptrace |= PT_DTRACE;
  175. }
  176. pc = get_stack_long(child, (long)&regs->pc);
  177. /* Next scheduling will set up UBC */
  178. if (child->thread.ubc_pc == 0)
  179. ubc_usercnt += 1;
  180. child->thread.ubc_pc = pc;
  181. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  182. child->exit_code = data;
  183. /* give it a chance to run. */
  184. wake_up_process(child);
  185. ret = 0;
  186. break;
  187. }
  188. case PTRACE_DETACH: /* detach a process that was attached. */
  189. ret = ptrace_detach(child, data);
  190. break;
  191. #ifdef CONFIG_SH_DSP
  192. case PTRACE_GETDSPREGS: {
  193. unsigned long dp;
  194. ret = -EIO;
  195. dp = ((unsigned long) child) + THREAD_SIZE -
  196. sizeof(struct pt_dspregs);
  197. if (*((int *) (dp - 4)) == SR_FD) {
  198. copy_to_user(addr, (void *) dp,
  199. sizeof(struct pt_dspregs));
  200. ret = 0;
  201. }
  202. break;
  203. }
  204. case PTRACE_SETDSPREGS: {
  205. unsigned long dp;
  206. ret = -EIO;
  207. dp = ((unsigned long) child) + THREAD_SIZE -
  208. sizeof(struct pt_dspregs);
  209. if (*((int *) (dp - 4)) == SR_FD) {
  210. copy_from_user((void *) dp, addr,
  211. sizeof(struct pt_dspregs));
  212. ret = 0;
  213. }
  214. break;
  215. }
  216. #endif
  217. default:
  218. ret = ptrace_request(child, request, addr, data);
  219. break;
  220. }
  221. return ret;
  222. }
  223. asmlinkage void do_syscall_trace(void)
  224. {
  225. struct task_struct *tsk = current;
  226. if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
  227. !test_thread_flag(TIF_SINGLESTEP))
  228. return;
  229. if (!(tsk->ptrace & PT_PTRACED))
  230. return;
  231. /* the 0x80 provides a way for the tracing parent to distinguish
  232. between a syscall stop and SIGTRAP delivery */
  233. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
  234. !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
  235. /*
  236. * this isn't the same as continuing with a signal, but it will do
  237. * for normal use. strace only continues with a signal if the
  238. * stopping signal is not SIGTRAP. -brl
  239. */
  240. if (tsk->exit_code) {
  241. send_sig(tsk->exit_code, tsk, 1);
  242. tsk->exit_code = 0;
  243. }
  244. }