ptrace.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 <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 *)
  39. task->thread_info + THREAD_SIZE - sizeof(struct pt_regs)
  40. #ifdef CONFIG_SH_DSP
  41. - sizeof(struct pt_dspregs)
  42. #endif
  43. - sizeof(unsigned long);
  44. stack += offset;
  45. return (*((int *)stack));
  46. }
  47. /*
  48. * This routine will put a word on the process kernel stack.
  49. */
  50. static inline int put_stack_long(struct task_struct *task, int offset,
  51. unsigned long data)
  52. {
  53. unsigned char *stack;
  54. stack = (unsigned char *)
  55. task->thread_info + THREAD_SIZE - sizeof(struct pt_regs)
  56. #ifdef CONFIG_SH_DSP
  57. - sizeof(struct pt_dspregs)
  58. #endif
  59. - sizeof(unsigned long);
  60. stack += offset;
  61. *(unsigned long *) stack = data;
  62. return 0;
  63. }
  64. /*
  65. * Called by kernel/ptrace.c when detaching..
  66. *
  67. * Make sure single step bits etc are not set.
  68. */
  69. void ptrace_disable(struct task_struct *child)
  70. {
  71. /* nothing to do.. */
  72. }
  73. asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
  74. {
  75. struct task_struct *child;
  76. struct user * dummy = NULL;
  77. int ret;
  78. lock_kernel();
  79. ret = -EPERM;
  80. if (request == PTRACE_TRACEME) {
  81. /* are we already being traced? */
  82. if (current->ptrace & PT_PTRACED)
  83. goto out;
  84. ret = security_ptrace(current->parent, current);
  85. if (ret)
  86. goto out;
  87. /* set the ptrace bit in the process flags. */
  88. current->ptrace |= PT_PTRACED;
  89. ret = 0;
  90. goto out;
  91. }
  92. ret = -ESRCH;
  93. read_lock(&tasklist_lock);
  94. child = find_task_by_pid(pid);
  95. if (child)
  96. get_task_struct(child);
  97. read_unlock(&tasklist_lock);
  98. if (!child)
  99. goto out;
  100. ret = -EPERM;
  101. if (pid == 1) /* you may not mess with init */
  102. goto out_tsk;
  103. if (request == PTRACE_ATTACH) {
  104. ret = ptrace_attach(child);
  105. goto out_tsk;
  106. }
  107. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  108. if (ret < 0)
  109. goto out_tsk;
  110. switch (request) {
  111. /* when I and D space are separate, these will need to be fixed. */
  112. case PTRACE_PEEKTEXT: /* read word at location addr. */
  113. case PTRACE_PEEKDATA: {
  114. unsigned long tmp;
  115. int copied;
  116. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  117. ret = -EIO;
  118. if (copied != sizeof(tmp))
  119. break;
  120. ret = put_user(tmp,(unsigned long *) data);
  121. break;
  122. }
  123. /* read the word at location addr in the USER area. */
  124. case PTRACE_PEEKUSR: {
  125. unsigned long tmp;
  126. ret = -EIO;
  127. if ((addr & 3) || addr < 0 ||
  128. addr > sizeof(struct user) - 3)
  129. break;
  130. if (addr < sizeof(struct pt_regs))
  131. tmp = get_stack_long(child, addr);
  132. else if (addr >= (long) &dummy->fpu &&
  133. addr < (long) &dummy->u_fpvalid) {
  134. if (!tsk_used_math(child)) {
  135. if (addr == (long)&dummy->fpu.fpscr)
  136. tmp = FPSCR_INIT;
  137. else
  138. tmp = 0;
  139. } else
  140. tmp = ((long *)&child->thread.fpu)
  141. [(addr - (long)&dummy->fpu) >> 2];
  142. } else if (addr == (long) &dummy->u_fpvalid)
  143. tmp = !!tsk_used_math(child);
  144. else
  145. tmp = 0;
  146. ret = put_user(tmp, (unsigned long *)data);
  147. break;
  148. }
  149. /* when I and D space are separate, this will have to be fixed. */
  150. case PTRACE_POKETEXT: /* write the word at location addr. */
  151. case PTRACE_POKEDATA:
  152. ret = 0;
  153. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  154. break;
  155. ret = -EIO;
  156. break;
  157. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  158. ret = -EIO;
  159. if ((addr & 3) || addr < 0 ||
  160. addr > sizeof(struct user) - 3)
  161. break;
  162. if (addr < sizeof(struct pt_regs))
  163. ret = put_stack_long(child, addr, data);
  164. else if (addr >= (long) &dummy->fpu &&
  165. addr < (long) &dummy->u_fpvalid) {
  166. set_stopped_child_used_math(child);
  167. ((long *)&child->thread.fpu)
  168. [(addr - (long)&dummy->fpu) >> 2] = data;
  169. ret = 0;
  170. } else if (addr == (long) &dummy->u_fpvalid) {
  171. conditional_stopped_child_used_math(data, child);
  172. ret = 0;
  173. }
  174. break;
  175. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  176. case PTRACE_CONT: { /* restart after signal. */
  177. ret = -EIO;
  178. if ((unsigned long) data > _NSIG)
  179. break;
  180. if (request == PTRACE_SYSCALL)
  181. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  182. else
  183. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  184. child->exit_code = data;
  185. wake_up_process(child);
  186. ret = 0;
  187. break;
  188. }
  189. /*
  190. * make the child exit. Best I can do is send it a sigkill.
  191. * perhaps it should be put in the status that it wants to
  192. * exit.
  193. */
  194. case PTRACE_KILL: {
  195. ret = 0;
  196. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  197. break;
  198. child->exit_code = SIGKILL;
  199. wake_up_process(child);
  200. break;
  201. }
  202. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  203. long pc;
  204. struct pt_regs *dummy = NULL;
  205. ret = -EIO;
  206. if ((unsigned long) data > _NSIG)
  207. break;
  208. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  209. if ((child->ptrace & PT_DTRACE) == 0) {
  210. /* Spurious delayed TF traps may occur */
  211. child->ptrace |= PT_DTRACE;
  212. }
  213. pc = get_stack_long(child, (long)&dummy->pc);
  214. /* Next scheduling will set up UBC */
  215. if (child->thread.ubc_pc == 0)
  216. ubc_usercnt += 1;
  217. child->thread.ubc_pc = pc;
  218. child->exit_code = data;
  219. /* give it a chance to run. */
  220. wake_up_process(child);
  221. ret = 0;
  222. break;
  223. }
  224. case PTRACE_DETACH: /* detach a process that was attached. */
  225. ret = ptrace_detach(child, data);
  226. break;
  227. #ifdef CONFIG_SH_DSP
  228. case PTRACE_GETDSPREGS: {
  229. unsigned long dp;
  230. ret = -EIO;
  231. dp = ((unsigned long) child) + THREAD_SIZE -
  232. sizeof(struct pt_dspregs);
  233. if (*((int *) (dp - 4)) == SR_FD) {
  234. copy_to_user(addr, (void *) dp,
  235. sizeof(struct pt_dspregs));
  236. ret = 0;
  237. }
  238. break;
  239. }
  240. case PTRACE_SETDSPREGS: {
  241. unsigned long dp;
  242. int i;
  243. ret = -EIO;
  244. dp = ((unsigned long) child) + THREAD_SIZE -
  245. sizeof(struct pt_dspregs);
  246. if (*((int *) (dp - 4)) == SR_FD) {
  247. copy_from_user((void *) dp, addr,
  248. sizeof(struct pt_dspregs));
  249. ret = 0;
  250. }
  251. break;
  252. }
  253. #endif
  254. default:
  255. ret = ptrace_request(child, request, addr, data);
  256. break;
  257. }
  258. out_tsk:
  259. put_task_struct(child);
  260. out:
  261. unlock_kernel();
  262. return ret;
  263. }
  264. asmlinkage void do_syscall_trace(void)
  265. {
  266. struct task_struct *tsk = current;
  267. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  268. return;
  269. if (!(tsk->ptrace & PT_PTRACED))
  270. return;
  271. /* the 0x80 provides a way for the tracing parent to distinguish
  272. between a syscall stop and SIGTRAP delivery */
  273. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  274. ? 0x80 : 0));
  275. /*
  276. * this isn't the same as continuing with a signal, but it will do
  277. * for normal use. strace only continues with a signal if the
  278. * stopping signal is not SIGTRAP. -brl
  279. */
  280. if (tsk->exit_code) {
  281. send_sig(tsk->exit_code, tsk, 1);
  282. tsk->exit_code = 0;
  283. }
  284. }