ptrace.c 6.1 KB

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