ptrace.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * linux/arch/h8300/kernel/ptrace.c
  3. *
  4. * Yoshinori Sato <ysato@users.sourceforge.jp>
  5. *
  6. * Based on:
  7. * linux/arch/m68k/kernel/ptrace.c
  8. *
  9. * Copyright (C) 1994 by Hamish Macdonald
  10. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  11. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  12. *
  13. * This file is subject to the terms and conditions of the GNU General
  14. * Public License. See the file COPYING in the main directory of
  15. * this archive for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/mm.h>
  20. #include <linux/smp.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/errno.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/user.h>
  25. #include <linux/config.h>
  26. #include <linux/signal.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/page.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/system.h>
  31. #include <asm/processor.h>
  32. #include <asm/signal.h>
  33. /* cpu depend functions */
  34. extern long h8300_get_reg(struct task_struct *task, int regno);
  35. extern int h8300_put_reg(struct task_struct *task, int regno, unsigned long data);
  36. extern void h8300_disable_trace(struct task_struct *child);
  37. extern void h8300_enable_trace(struct task_struct *child);
  38. /*
  39. * does not yet catch signals sent when the child dies.
  40. * in exit.c or in signal.c.
  41. */
  42. inline
  43. static int read_long(struct task_struct * tsk, unsigned long addr,
  44. unsigned long * result)
  45. {
  46. *result = *(unsigned long *)addr;
  47. return 0;
  48. }
  49. void ptrace_disable(struct task_struct *child)
  50. {
  51. h8300_disable_trace(child);
  52. }
  53. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  54. {
  55. int ret;
  56. switch (request) {
  57. case PTRACE_PEEKTEXT: /* read word at location addr. */
  58. case PTRACE_PEEKDATA: {
  59. unsigned long tmp;
  60. ret = read_long(child, addr, &tmp);
  61. if (ret < 0)
  62. break ;
  63. ret = put_user(tmp, (unsigned long *) data);
  64. break ;
  65. }
  66. /* read the word at location addr in the USER area. */
  67. case PTRACE_PEEKUSR: {
  68. unsigned long tmp = 0;
  69. if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
  70. ret = -EIO;
  71. break ;
  72. }
  73. ret = 0; /* Default return condition */
  74. addr = addr >> 2; /* temporary hack. */
  75. if (addr < H8300_REGS_NO)
  76. tmp = h8300_get_reg(child, addr);
  77. else {
  78. switch(addr) {
  79. case 49:
  80. tmp = child->mm->start_code;
  81. break ;
  82. case 50:
  83. tmp = child->mm->start_data;
  84. break ;
  85. case 51:
  86. tmp = child->mm->end_code;
  87. break ;
  88. case 52:
  89. tmp = child->mm->end_data;
  90. break ;
  91. default:
  92. ret = -EIO;
  93. }
  94. }
  95. if (!ret)
  96. ret = put_user(tmp,(unsigned long *) data);
  97. break ;
  98. }
  99. /* when I and D space are separate, this will have to be fixed. */
  100. case PTRACE_POKETEXT: /* write the word at location addr. */
  101. case PTRACE_POKEDATA:
  102. ret = 0;
  103. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  104. break;
  105. ret = -EIO;
  106. break;
  107. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  108. if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
  109. ret = -EIO;
  110. break ;
  111. }
  112. addr = addr >> 2; /* temporary hack. */
  113. if (addr == PT_ORIG_ER0) {
  114. ret = -EIO;
  115. break ;
  116. }
  117. if (addr < H8300_REGS_NO) {
  118. ret = h8300_put_reg(child, addr, data);
  119. break ;
  120. }
  121. ret = -EIO;
  122. break ;
  123. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  124. case PTRACE_CONT: { /* restart after signal. */
  125. ret = -EIO;
  126. if (!valid_signal(data))
  127. break ;
  128. if (request == PTRACE_SYSCALL)
  129. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  130. else
  131. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  132. child->exit_code = data;
  133. wake_up_process(child);
  134. /* make sure the single step bit is not set. */
  135. h8300_disable_trace(child);
  136. ret = 0;
  137. }
  138. /*
  139. * make the child exit. Best I can do is send it a sigkill.
  140. * perhaps it should be put in the status that it wants to
  141. * exit.
  142. */
  143. case PTRACE_KILL: {
  144. ret = 0;
  145. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  146. break;
  147. child->exit_code = SIGKILL;
  148. h8300_disable_trace(child);
  149. wake_up_process(child);
  150. break;
  151. }
  152. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  153. ret = -EIO;
  154. if (!valid_signal(data))
  155. break;
  156. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  157. child->exit_code = data;
  158. h8300_enable_trace(child);
  159. wake_up_process(child);
  160. ret = 0;
  161. break;
  162. }
  163. case PTRACE_DETACH: /* detach a process that was attached. */
  164. ret = ptrace_detach(child, data);
  165. break;
  166. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  167. int i;
  168. unsigned long tmp;
  169. for (i = 0; i < H8300_REGS_NO; i++) {
  170. tmp = h8300_get_reg(child, i);
  171. if (put_user(tmp, (unsigned long *) data)) {
  172. ret = -EFAULT;
  173. break;
  174. }
  175. data += sizeof(long);
  176. }
  177. ret = 0;
  178. break;
  179. }
  180. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  181. int i;
  182. unsigned long tmp;
  183. for (i = 0; i < H8300_REGS_NO; i++) {
  184. if (get_user(tmp, (unsigned long *) data)) {
  185. ret = -EFAULT;
  186. break;
  187. }
  188. h8300_put_reg(child, i, tmp);
  189. data += sizeof(long);
  190. }
  191. ret = 0;
  192. break;
  193. }
  194. default:
  195. ret = -EIO;
  196. break;
  197. }
  198. return ret;
  199. }
  200. asmlinkage void syscall_trace(void)
  201. {
  202. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  203. return;
  204. if (!(current->ptrace & PT_PTRACED))
  205. return;
  206. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  207. ? 0x80 : 0));
  208. /*
  209. * this isn't the same as continuing with a signal, but it will do
  210. * for normal use. strace only continues with a signal if the
  211. * stopping signal is not SIGTRAP. -brl
  212. */
  213. if (current->exit_code) {
  214. send_sig(current->exit_code, current, 1);
  215. current->exit_code = 0;
  216. }
  217. }