ptrace.c 6.2 KB

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