ptrace.c 5.6 KB

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