ptrace.c 7.2 KB

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